From 1d5a12201981035805add15dbad3637e182ee061 Mon Sep 17 00:00:00 2001 From: sourabh_sourabh Date: Tue, 8 Nov 2022 11:46:05 +0000 Subject: Node API - Find root by anchor method performance - Modified findFirstRootByAnchor - Used fragment extract to build fragment entity to get root data node. Reviewers : Toine, Priyank and Joe Issue-ID: CPS-1171 Signed-off-by: sourabh_sourabh Change-Id: I843f270e4781f91ae496f39b976e2a7f2a14d55e Signed-off-by: sourabh_sourabh --- .../cps/spi/entities/FragmentEntityArranger.java | 6 ++--- .../spi/impl/CpsDataPersistenceServiceImpl.java | 8 +++++-- .../cps/spi/repository/FragmentRepository.java | 26 +++++++++++++++++----- 3 files changed, 29 insertions(+), 11 deletions(-) (limited to 'cps-ri/src/main') diff --git a/cps-ri/src/main/java/org/onap/cps/spi/entities/FragmentEntityArranger.java b/cps-ri/src/main/java/org/onap/cps/spi/entities/FragmentEntityArranger.java index 50187a487b..27891c525e 100644 --- a/cps-ri/src/main/java/org/onap/cps/spi/entities/FragmentEntityArranger.java +++ b/cps-ri/src/main/java/org/onap/cps/spi/entities/FragmentEntityArranger.java @@ -57,6 +57,7 @@ public class FragmentEntityArranger { fragmentEntity.setAttributes(fragmentExtract.getAttributes()); fragmentEntity.setParentId(fragmentExtract.getParentId()); fragmentEntity.setChildFragments(new HashSet<>()); + fragmentEntity.setDataspace(anchorEntity.getDataspace()); return fragmentEntity; } @@ -70,10 +71,7 @@ public class FragmentEntityArranger { parentFragmentEntity.getChildFragments().add(fragmentEntity); } } - if (fragmentEntitiesWithoutParentInResultSet.iterator().hasNext()) { - return fragmentEntitiesWithoutParentInResultSet.iterator().next(); - } - return null; + return fragmentEntitiesWithoutParentInResultSet.stream().findFirst().orElse(null); } } diff --git a/cps-ri/src/main/java/org/onap/cps/spi/impl/CpsDataPersistenceServiceImpl.java b/cps-ri/src/main/java/org/onap/cps/spi/impl/CpsDataPersistenceServiceImpl.java index dc848e657c..ac1be1cd30 100644 --- a/cps-ri/src/main/java/org/onap/cps/spi/impl/CpsDataPersistenceServiceImpl.java +++ b/cps-ri/src/main/java/org/onap/cps/spi/impl/CpsDataPersistenceServiceImpl.java @@ -230,7 +230,11 @@ public class CpsDataPersistenceServiceImpl implements CpsDataPersistenceService final DataspaceEntity dataspaceEntity = dataspaceRepository.getByName(dataspaceName); final AnchorEntity anchorEntity = anchorRepository.getByDataspaceAndName(dataspaceEntity, anchorName); if (isRootXpath(xpath)) { - return fragmentRepository.findFirstRootByDataspaceAndAnchor(dataspaceEntity, anchorEntity); + final List fragmentExtracts = fragmentRepository.getTopLevelFragments(dataspaceEntity, + anchorEntity); + final FragmentEntity fragmentEntity = FragmentEntityArranger.toFragmentEntityTree(anchorEntity, + fragmentExtracts); + return fragmentEntity; } else { final String normalizedXpath = getNormalizedXpath(xpath); final FragmentEntity fragmentEntity; @@ -500,7 +504,7 @@ public class CpsDataPersistenceServiceImpl implements CpsDataPersistenceService final boolean onlySupportListNodeDeletion) { final String parentNodeXpath; FragmentEntity parentFragmentEntity = null; - boolean targetDeleted = false; + boolean targetDeleted; if (isRootXpath(targetXpath)) { deleteDataNodes(dataspaceName, anchorName); targetDeleted = true; diff --git a/cps-ri/src/main/java/org/onap/cps/spi/repository/FragmentRepository.java b/cps-ri/src/main/java/org/onap/cps/spi/repository/FragmentRepository.java index 112ebfda70..2c25a61a7e 100755 --- a/cps-ri/src/main/java/org/onap/cps/spi/repository/FragmentRepository.java +++ b/cps-ri/src/main/java/org/onap/cps/spi/repository/FragmentRepository.java @@ -1,6 +1,6 @@ /* * ============LICENSE_START======================================================= - * Copyright (C) 2020-2021 Nordix Foundation. + * Copyright (C) 2021-2022 Nordix Foundation. * Modifications Copyright (C) 2020-2021 Bell Canada. * Modifications Copyright (C) 2020-2021 Pantheon.tech. * ================================================================================ @@ -58,10 +58,26 @@ public interface FragmentRepository extends JpaRepository, List findRootsByDataspaceAndAnchor(@Param("dataspace") int dataspaceId, @Param("anchor") int anchorId); - default FragmentEntity findFirstRootByDataspaceAndAnchor(@NonNull DataspaceEntity dataspaceEntity, - @NonNull AnchorEntity anchorEntity) { - return findRootsByDataspaceAndAnchor(dataspaceEntity.getId(), anchorEntity.getId()).stream().findFirst() - .orElseThrow(() -> new DataNodeNotFoundException(dataspaceEntity.getName(), anchorEntity.getName())); + @Query(value = "SELECT id, anchor_id AS anchorId, xpath, parent_id AS parentId," + + " CAST(attributes AS TEXT) AS attributes" + + " FROM FRAGMENT WHERE anchor_id = :anchorId", + nativeQuery = true) + List findRootsByAnchorId(@Param("anchorId") int anchorId); + + /** + * find top level fragment by anchor. + * + * @param dataspaceEntity dataspace entity + * @param anchorEntity anchor entity + * @return FragmentEntity fragment entity + */ + default List getTopLevelFragments(DataspaceEntity dataspaceEntity, + AnchorEntity anchorEntity) { + final List fragmentExtracts = findRootsByAnchorId(anchorEntity.getId()); + if (fragmentExtracts.isEmpty()) { + throw new DataNodeNotFoundException(dataspaceEntity.getName(), anchorEntity.getName()); + } + return fragmentExtracts; } List findAllByAnchorAndXpathIn(@NonNull AnchorEntity anchorEntity, -- cgit 1.2.3-korg