From 8e08dc797c89f6e85347c10cc7a2e877c126432a Mon Sep 17 00:00:00 2001 From: danielhanrahan Date: Fri, 10 Mar 2023 13:27:01 +0000 Subject: Make single deleteDataNode use plural deleteDataNodes - Make deleteDataNode and deleteListDataNode call deleteDataNodes - Add onlySupportListDeletion option to deleteDataNodes to support original deleteListDataNode behaviour - Allow delete root xpath in deleteDataNodes - Fix incorrect use of PathParsingException in deleteDataNode - Update performance tests timings Issue-ID: CPS-1523 Signed-off-by: danielhanrahan Change-Id: I92c3c3ce606a5ab2cb8e6779d1ee0f9853529982 --- .../spi/impl/CpsDataPersistenceServiceImpl.java | 102 +++++++-------------- 1 file changed, 31 insertions(+), 71 deletions(-) (limited to 'cps-ri/src/main') 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 f634008dc6..916baa8c23 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 @@ -253,7 +253,7 @@ public class CpsDataPersistenceServiceImpl implements CpsDataPersistenceService public Collection getDataNodes(final String dataspaceName, final String anchorName, final String xpath, final FetchDescendantsOption fetchDescendantsOption) { - final String targetXpath = isRootXpath(xpath) ? xpath : CpsPathUtil.getNormalizedXpath(xpath); + final String targetXpath = getNormalizedXpath(xpath); final Collection dataNodes = getDataNodesForMultipleXpaths(dataspaceName, anchorName, Collections.singletonList(targetXpath), fetchDescendantsOption); if (dataNodes.isEmpty()) { @@ -411,13 +411,14 @@ public class CpsDataPersistenceServiceImpl implements CpsDataPersistenceService } private static String getNormalizedXpath(final String xpathSource) { - final String normalizedXpath; + if (isRootXpath(xpathSource)) { + return xpathSource; + } try { - normalizedXpath = CpsPathUtil.getNormalizedXpath(xpathSource); + return CpsPathUtil.getNormalizedXpath(xpathSource); } catch (final PathParsingException e) { throw new CpsPathException(e.getMessage()); } - return normalizedXpath; } @Override @@ -583,7 +584,7 @@ public class CpsDataPersistenceServiceImpl implements CpsDataPersistenceService final String listElementXpathPrefix = getListElementXpathPrefix(newListElements); final Map existingListElementFragmentEntitiesByXPath = extractListElementFragmentEntitiesByXPath(parentEntity.getChildFragments(), listElementXpathPrefix); - deleteListElements(parentEntity.getChildFragments(), existingListElementFragmentEntitiesByXPath); + parentEntity.getChildFragments().removeAll(existingListElementFragmentEntitiesByXPath.values()); final Set updatedChildFragmentEntities = new HashSet<>(); for (final DataNode newListElement : newListElements) { final FragmentEntity existingListElementEntity = @@ -602,8 +603,7 @@ public class CpsDataPersistenceServiceImpl implements CpsDataPersistenceService public void deleteDataNodes(final String dataspaceName, final String anchorName) { final DataspaceEntity dataspaceEntity = dataspaceRepository.getByName(dataspaceName); anchorRepository.findByDataspaceAndName(dataspaceEntity, anchorName) - .ifPresent( - anchorEntity -> fragmentRepository.deleteByAnchorIn(Set.of(anchorEntity))); + .ifPresent(anchorEntity -> fragmentRepository.deleteByAnchorIn(Collections.singletonList(anchorEntity))); } @Override @@ -619,6 +619,17 @@ public class CpsDataPersistenceServiceImpl implements CpsDataPersistenceService @Transactional public void deleteDataNodes(final String dataspaceName, final String anchorName, final Collection xpathsToDelete) { + deleteDataNodes(dataspaceName, anchorName, xpathsToDelete, false); + } + + private void deleteDataNodes(final String dataspaceName, final String anchorName, + final Collection xpathsToDelete, final boolean onlySupportListDeletion) { + final boolean haveRootXpath = xpathsToDelete.stream().anyMatch(CpsDataPersistenceServiceImpl::isRootXpath); + if (haveRootXpath) { + deleteDataNodes(dataspaceName, anchorName); + return; + } + final DataspaceEntity dataspaceEntity = dataspaceRepository.getByName(dataspaceName); final AnchorEntity anchorEntity = anchorRepository.getByDataspaceAndName(dataspaceEntity, anchorName); @@ -633,7 +644,13 @@ public class CpsDataPersistenceServiceImpl implements CpsDataPersistenceService final Collection xpathsToExistingContainers = fragmentRepository.findAllXpathByAnchorAndXpathIn(anchorEntity, deleteChecklist); - deleteChecklist.removeAll(xpathsToExistingContainers); + if (onlySupportListDeletion) { + final Collection xpathsToExistingListElements = xpathsToExistingContainers.stream() + .filter(CpsPathUtil::isPathToListElement).collect(Collectors.toList()); + deleteChecklist.removeAll(xpathsToExistingListElements); + } else { + deleteChecklist.removeAll(xpathsToExistingContainers); + } final Collection xpathsToExistingLists = deleteChecklist.stream() .filter(xpath -> fragmentRepository.existsByAnchorAndXpathStartsWith(anchorEntity, xpath + "[")) @@ -663,66 +680,13 @@ public class CpsDataPersistenceServiceImpl implements CpsDataPersistenceService private void deleteDataNode(final String dataspaceName, final String anchorName, final String targetXpath, final boolean onlySupportListNodeDeletion) { - final String parentNodeXpath; - FragmentEntity parentFragmentEntity = null; - boolean targetDeleted; - if (isRootXpath(targetXpath)) { - deleteDataNodes(dataspaceName, anchorName); - targetDeleted = true; - } else { - if (isRootContainerNodeXpath(targetXpath)) { - parentNodeXpath = targetXpath; - } else { - parentNodeXpath = CpsPathUtil.getNormalizedParentXpath(targetXpath); - } - parentFragmentEntity = getFragmentEntity(dataspaceName, anchorName, parentNodeXpath); - if (CpsPathUtil.isPathToListElement(targetXpath)) { - targetDeleted = deleteDataNode(parentFragmentEntity, targetXpath); - } else { - targetDeleted = deleteAllListElements(parentFragmentEntity, targetXpath); - final boolean tryToDeleteDataNode = !targetDeleted && !onlySupportListNodeDeletion; - if (tryToDeleteDataNode) { - targetDeleted = deleteDataNode(parentFragmentEntity, targetXpath); - } - } - } - if (!targetDeleted) { - final String additionalInformation = onlySupportListNodeDeletion - ? "The target is probably not a List." : ""; - throw new DataNodeNotFoundException(parentFragmentEntity.getDataspace().getName(), - parentFragmentEntity.getAnchor().getName(), targetXpath, additionalInformation); - } - } - - private boolean deleteDataNode(final FragmentEntity parentFragmentEntity, final String targetXpath) { - final String normalizedTargetXpath = CpsPathUtil.getNormalizedXpath(targetXpath); - if (parentFragmentEntity.getXpath().equals(normalizedTargetXpath)) { - fragmentRepository.deleteFragmentEntity(parentFragmentEntity.getId()); - return true; - } - if (parentFragmentEntity.getChildFragments() - .removeIf(fragment -> fragment.getXpath().equals(normalizedTargetXpath))) { - fragmentRepository.save(parentFragmentEntity); - return true; - } - return false; - } - - private boolean deleteAllListElements(final FragmentEntity parentFragmentEntity, final String listXpath) { - final String normalizedListXpath = CpsPathUtil.getNormalizedXpath(listXpath); - final String deleteTargetXpathPrefix = normalizedListXpath + "["; - if (parentFragmentEntity.getChildFragments() - .removeIf(fragment -> fragment.getXpath().startsWith(deleteTargetXpathPrefix))) { - fragmentRepository.save(parentFragmentEntity); - return true; + final String normalizedXpath = getNormalizedXpath(targetXpath); + try { + deleteDataNodes(dataspaceName, anchorName, Collections.singletonList(normalizedXpath), + onlySupportListNodeDeletion); + } catch (final DataNodeNotFoundExceptionBatch dataNodeNotFoundExceptionBatch) { + throw new DataNodeNotFoundException(dataspaceName, anchorName, targetXpath); } - return false; - } - - private static void deleteListElements( - final Collection fragmentEntities, - final Map existingListElementFragmentEntitiesByXPath) { - fragmentEntities.removeAll(existingListElementFragmentEntitiesByXPath.values()); } private static String getListElementXpathPrefix(final Collection newListElements) { @@ -755,10 +719,6 @@ public class CpsDataPersistenceServiceImpl implements CpsDataPersistenceService return !existingListElementsByXpath.containsKey(replacementDataNode.getXpath()); } - private static boolean isRootContainerNodeXpath(final String xpath) { - return 0 == xpath.lastIndexOf('/'); - } - private void copyAttributesFromNewListElement(final FragmentEntity existingListElementEntity, final DataNode newListElement) { final FragmentEntity replacementFragmentEntity = -- cgit 1.2.3-korg