From: danielhanrahan Date: Fri, 26 Apr 2024 09:09:54 +0000 (+0100) Subject: Faster module searches (CPS-2190 #3) X-Git-Tag: 3.4.8~10 X-Git-Url: https://gerrit.onap.org/r/gitweb?p=cps.git;a=commitdiff_plain;h=2830723b8c5d5bb40c171c88f055adbc1a808f68 Faster module searches (CPS-2190 #3) This greatly improves performance of module searches by eliminating unneeded SQL queries via Hibernate lazy fetching. Issue-ID: CPS-2190 Signed-off-by: danielhanrahan Change-Id: Ie9e65017d0027366456f1741cc37b10679317b25 --- diff --git a/cps-ri/src/main/java/org/onap/cps/spi/impl/CpsAdminPersistenceServiceImpl.java b/cps-ri/src/main/java/org/onap/cps/spi/impl/CpsAdminPersistenceServiceImpl.java index d697fd53f..56a046496 100755 --- a/cps-ri/src/main/java/org/onap/cps/spi/impl/CpsAdminPersistenceServiceImpl.java +++ b/cps-ri/src/main/java/org/onap/cps/spi/impl/CpsAdminPersistenceServiceImpl.java @@ -131,11 +131,10 @@ public class CpsAdminPersistenceServiceImpl implements CpsAdminPersistenceServic } @Override - public Collection queryAnchors(final String dataspaceName, final Collection inputModuleNames) { + public Collection queryAnchorNames(final String dataspaceName, final Collection inputModuleNames) { final DataspaceEntity dataspaceEntity = dataspaceRepository.getByName(dataspaceName); - final Collection anchorEntities = anchorRepository - .getAnchorsByDataspaceIdAndModuleNames(dataspaceEntity.getId(), inputModuleNames, inputModuleNames.size()); - return anchorEntities.stream().map(CpsAdminPersistenceServiceImpl::toAnchor).collect(Collectors.toSet()); + return anchorRepository.getAnchorNamesByDataspaceIdAndModuleNames(dataspaceEntity.getId(), inputModuleNames, + inputModuleNames.size()); } @Override diff --git a/cps-ri/src/main/java/org/onap/cps/spi/repository/AnchorRepository.java b/cps-ri/src/main/java/org/onap/cps/spi/repository/AnchorRepository.java index 19646c523..d78a016c2 100755 --- a/cps-ri/src/main/java/org/onap/cps/spi/repository/AnchorRepository.java +++ b/cps-ri/src/main/java/org/onap/cps/spi/repository/AnchorRepository.java @@ -72,7 +72,7 @@ public interface AnchorRepository extends JpaRepository { @Query(value = """ SELECT - anchor.* + anchor.name FROM yang_resource JOIN schema_set_yang_resources ON schema_set_yang_resources.yang_resource_id = yang_resource.id @@ -89,15 +89,15 @@ public interface AnchorRepository extends JpaRepository { HAVING COUNT(DISTINCT module_name) = :sizeOfModuleNames """, nativeQuery = true) - Collection getAnchorsByDataspaceIdAndModuleNames(@Param("dataspaceId") int dataspaceId, - @Param("moduleNames") String[] moduleNames, - @Param("sizeOfModuleNames") int sizeOfModuleNames); + Collection getAnchorNamesByDataspaceIdAndModuleNames(@Param("dataspaceId") int dataspaceId, + @Param("moduleNames") String[] moduleNames, + @Param("sizeOfModuleNames") int sizeOfModuleNames); - default Collection getAnchorsByDataspaceIdAndModuleNames(final int dataspaceId, - final Collection moduleNames, - final int sizeOfModuleNames) { + default Collection getAnchorNamesByDataspaceIdAndModuleNames(final int dataspaceId, + final Collection moduleNames, + final int sizeOfModuleNames) { final String[] moduleNamesArray = moduleNames.toArray(new String[0]); - return getAnchorsByDataspaceIdAndModuleNames(dataspaceId, moduleNamesArray, sizeOfModuleNames); + return getAnchorNamesByDataspaceIdAndModuleNames(dataspaceId, moduleNamesArray, sizeOfModuleNames); } @Modifying diff --git a/cps-service/src/main/java/org/onap/cps/api/impl/CpsAnchorServiceImpl.java b/cps-service/src/main/java/org/onap/cps/api/impl/CpsAnchorServiceImpl.java index f09a795a6..aa9c45d09 100644 --- a/cps-service/src/main/java/org/onap/cps/api/impl/CpsAnchorServiceImpl.java +++ b/cps-service/src/main/java/org/onap/cps/api/impl/CpsAnchorServiceImpl.java @@ -21,7 +21,6 @@ package org.onap.cps.api.impl; import java.util.Collection; -import java.util.stream.Collectors; import lombok.RequiredArgsConstructor; import org.onap.cps.api.CpsAnchorService; import org.onap.cps.spi.CpsAdminPersistenceService; @@ -87,8 +86,7 @@ public class CpsAnchorServiceImpl implements CpsAnchorService { @Override public Collection queryAnchorNames(final String dataspaceName, final Collection moduleNames) { cpsValidator.validateNameCharacters(dataspaceName); - final Collection anchors = cpsAdminPersistenceService.queryAnchors(dataspaceName, moduleNames); - return anchors.stream().map(Anchor::getName).collect(Collectors.toList()); + return cpsAdminPersistenceService.queryAnchorNames(dataspaceName, moduleNames); } @Override diff --git a/cps-service/src/main/java/org/onap/cps/spi/CpsAdminPersistenceService.java b/cps-service/src/main/java/org/onap/cps/spi/CpsAdminPersistenceService.java index 5a1810f47..2b21619cb 100755 --- a/cps-service/src/main/java/org/onap/cps/spi/CpsAdminPersistenceService.java +++ b/cps-service/src/main/java/org/onap/cps/spi/CpsAdminPersistenceService.java @@ -107,7 +107,7 @@ public interface CpsAdminPersistenceService { * @return a collection of anchor names in the given dataspace. The schema set for each anchor must include all the * given module names */ - Collection queryAnchors(String dataspaceName, Collection moduleNames); + Collection queryAnchorNames(String dataspaceName, Collection moduleNames); /** * Get an anchor in the given dataspace using the anchor name. diff --git a/cps-service/src/test/groovy/org/onap/cps/api/impl/CpsAnchorServiceImplSpec.groovy b/cps-service/src/test/groovy/org/onap/cps/api/impl/CpsAnchorServiceImplSpec.groovy index 3546b8167..c7865386b 100644 --- a/cps-service/src/test/groovy/org/onap/cps/api/impl/CpsAnchorServiceImplSpec.groovy +++ b/cps-service/src/test/groovy/org/onap/cps/api/impl/CpsAnchorServiceImplSpec.groovy @@ -118,7 +118,7 @@ class CpsAnchorServiceImplSpec extends Specification { def 'Query all anchor identifiers for a dataspace and module names.'() { given: 'the persistence service is invoked with the expected parameters and returns a list of anchors' - mockCpsAdminPersistenceService.queryAnchors('some-dataspace-name', ['some-module-name']) >> [new Anchor(name:'some-anchor-identifier')] + mockCpsAdminPersistenceService.queryAnchorNames('some-dataspace-name', ['some-module-name']) >> ['some-anchor-identifier'] when: 'query anchor names is called using a dataspace name and module name' def result = objectUnderTest.queryAnchorNames('some-dataspace-name', ['some-module-name']) then: 'get anchor identifiers returns the same anchor identifier returned by the persistence layer' @@ -130,7 +130,7 @@ class CpsAnchorServiceImplSpec extends Specification { def 'Query all anchors with Module Names Not Found Exception in persistence layer.'() { given: 'the persistence layer throws a Module Names Not Found Exception' def originalException = new ModuleNamesNotFoundException('exception-ds', ['m1', 'm2']) - mockCpsAdminPersistenceService.queryAnchors(*_) >> { throw originalException} + mockCpsAdminPersistenceService.queryAnchorNames(*_) >> { throw originalException} when: 'attempt query anchors' objectUnderTest.queryAnchorNames('some-dataspace-name', []) then: 'the same exception is thrown (up)' diff --git a/integration-test/src/test/groovy/org/onap/cps/integration/performance/cps/ModuleQueryPerfTest.groovy b/integration-test/src/test/groovy/org/onap/cps/integration/performance/cps/ModuleQueryPerfTest.groovy index 6efebd4ea..add931a1a 100644 --- a/integration-test/src/test/groovy/org/onap/cps/integration/performance/cps/ModuleQueryPerfTest.groovy +++ b/integration-test/src/test/groovy/org/onap/cps/integration/performance/cps/ModuleQueryPerfTest.groovy @@ -86,11 +86,11 @@ class ModuleQueryPerfTest extends CpsPerfTestBase { and: 'operation completes with expected resource usage' recordAndAssertResourceUsage("Query for anchors with ${scenario}", expectedTimeInSeconds, resourceMeter.totalTimeInSeconds, - 150, resourceMeter.totalMemoryUsageInMB) + 5, resourceMeter.totalMemoryUsageInMB) where: 'the following parameters are used' scenario | yangModuleName || expectedTimeInSeconds - '1 KB module' | 'module0' || 3 - '1000 KB module' | 'module1' || 3 + '1 KB module' | 'module0' || 0.05 + '1000 KB module' | 'module1' || 0.05 } def 'Module query - Clean up test data.'() {