Reduce dataspace/anchor lookups in CpsDataPersistenceService
[cps.git] / cps-ri / src / main / java / org / onap / cps / spi / repository / FragmentRepository.java
index 51ebcb4..996e55e 100755 (executable)
@@ -25,10 +25,7 @@ package org.onap.cps.spi.repository;
 import java.util.Collection;\r
 import java.util.List;\r
 import java.util.Optional;\r
-import javax.validation.constraints.NotNull;\r
-import org.checkerframework.checker.nullness.qual.NonNull;\r
 import org.onap.cps.spi.entities.AnchorEntity;\r
-import org.onap.cps.spi.entities.DataspaceEntity;\r
 import org.onap.cps.spi.entities.FragmentEntity;\r
 import org.onap.cps.spi.entities.FragmentExtract;\r
 import org.onap.cps.spi.exceptions.DataNodeNotFoundException;\r
@@ -42,56 +39,26 @@ import org.springframework.stereotype.Repository;
 public interface FragmentRepository extends JpaRepository<FragmentEntity, Long>, FragmentRepositoryCpsPathQuery,\r
         FragmentRepositoryMultiPathQuery, FragmentNativeRepository {\r
 \r
-    Optional<FragmentEntity> findByDataspaceAndAnchorAndXpath(@NonNull DataspaceEntity dataspaceEntity,\r
-                                                              @NonNull AnchorEntity anchorEntity,\r
-                                                              @NonNull String xpath);\r
+    Optional<FragmentEntity> findByAnchorAndXpath(AnchorEntity anchorEntity, String xpath);\r
 \r
-    default FragmentEntity getByDataspaceAndAnchorAndXpath(@NonNull DataspaceEntity dataspaceEntity,\r
-                                                           @NonNull AnchorEntity anchorEntity,\r
-                                                           @NonNull String xpath) {\r
-        return findByDataspaceAndAnchorAndXpath(dataspaceEntity, anchorEntity, xpath)\r
-            .orElseThrow(() -> new DataNodeNotFoundException(dataspaceEntity.getName(), anchorEntity.getName(), xpath));\r
+    default FragmentEntity getByAnchorAndXpath(final AnchorEntity anchorEntity, final String xpath) {\r
+        return findByAnchorAndXpath(anchorEntity, xpath).orElseThrow(() ->\r
+            new DataNodeNotFoundException(anchorEntity.getDataspace().getName(), anchorEntity.getName(), xpath));\r
     }\r
 \r
-    @Query(\r
-        value = "SELECT * FROM FRAGMENT WHERE anchor_id = :anchor AND dataspace_id = :dataspace AND parent_id is NULL",\r
-        nativeQuery = true)\r
-    List<FragmentEntity> findRootsByDataspaceAndAnchor(@Param("dataspace") int dataspaceId,\r
-                                                       @Param("anchor") int anchorId);\r
-\r
-    @Query(value = "SELECT id, anchor_id AS anchorId, xpath, parent_id AS parentId,"\r
-            + " CAST(attributes AS TEXT) AS attributes"\r
-            + " FROM FRAGMENT WHERE anchor_id = :anchorId",\r
-            nativeQuery = true)\r
-    List<FragmentExtract> findRootsByAnchorId(@Param("anchorId") int anchorId);\r
+    boolean existsByAnchorId(int anchorId);\r
 \r
-    /**\r
-     * find top level fragment by anchor.\r
-     *\r
-     * @param dataspaceEntity dataspace entity\r
-     * @param anchorEntity anchor entity\r
-     * @return FragmentEntity fragment entity\r
-     */\r
-    default List<FragmentExtract> getTopLevelFragments(DataspaceEntity dataspaceEntity,\r
-                                                       AnchorEntity anchorEntity) {\r
-        final List<FragmentExtract> fragmentExtracts = findRootsByAnchorId(anchorEntity.getId());\r
-        if (fragmentExtracts.isEmpty()) {\r
-            throw new DataNodeNotFoundException(dataspaceEntity.getName(), anchorEntity.getName());\r
-        }\r
-        return fragmentExtracts;\r
-    }\r
+    @Query("SELECT f FROM FragmentEntity f WHERE anchor = :anchor")\r
+    List<FragmentExtract> findAllExtractsByAnchor(@Param("anchor") AnchorEntity anchorEntity);\r
 \r
     @Modifying\r
-    @Query("DELETE FROM FragmentEntity fe WHERE fe.anchor IN (:anchors)")\r
-    void deleteByAnchorIn(@NotNull @Param("anchors") Collection<AnchorEntity> anchorEntities);\r
+    @Query("DELETE FROM FragmentEntity WHERE anchor IN (:anchors)")\r
+    void deleteByAnchorIn(@Param("anchors") Collection<AnchorEntity> anchorEntities);\r
 \r
-    @Query(value = "SELECT id, anchor_id AS anchorId, xpath, parent_id AS parentId,"\r
-        + " CAST(attributes AS TEXT) AS attributes"\r
-        + " FROM FRAGMENT WHERE anchor_id = :anchorId"\r
-        + " AND ( xpath = :parentXpath OR xpath LIKE CONCAT(:parentXpath,'/%') )",\r
-           nativeQuery = true)\r
-    List<FragmentExtract> findByAnchorIdAndParentXpath(@Param("anchorId") int anchorId,\r
-                                                       @Param("parentXpath") String parentXpath);\r
+    @Query("SELECT f FROM FragmentEntity f WHERE anchor = :anchor"\r
+        + " AND (xpath = :parentXpath OR xpath LIKE CONCAT(:parentXpath,'/%'))")\r
+    List<FragmentExtract> findByAnchorAndParentXpath(@Param("anchor") AnchorEntity anchorEntity,\r
+                                                     @Param("parentXpath") String parentXpath);\r
 \r
     @Query(value = "SELECT id, anchor_id AS anchorId, xpath, parent_id AS parentId,"\r
         + " CAST(attributes AS TEXT) AS attributes"\r
@@ -101,7 +68,7 @@ public interface FragmentRepository extends JpaRepository<FragmentEntity, Long>,
     List<FragmentExtract> quickFindWithDescendants(@Param("anchorId") int anchorId,\r
                                                    @Param("xpathRegex") String xpathRegex);\r
 \r
-    @Query("SELECT f.xpath FROM FragmentEntity f WHERE f.anchor = :anchor AND f.xpath IN :xpaths")\r
+    @Query("SELECT xpath FROM FragmentEntity f WHERE anchor = :anchor AND xpath IN :xpaths")\r
     List<String> findAllXpathByAnchorAndXpathIn(@Param("anchor") AnchorEntity anchorEntity,\r
                                                 @Param("xpaths") Collection<String> xpaths);\r
 \r