diff options
author | ToineSiebelink <toine.siebelink@est.tech> | 2023-05-04 14:54:21 +0100 |
---|---|---|
committer | Toine Siebelink <toine.siebelink@est.tech> | 2023-05-04 14:00:21 +0000 |
commit | 2438ee427139f03a2b9b39406c3f04088a43bc29 (patch) | |
tree | cbb77f309f38b41532efd327704763cfd353d8fa /cps-ri/src/main/java | |
parent | ce3451d10315b54238b886491779b3975f326e02 (diff) |
Fix for quickfind with descendants incl. list entries
- add specialized regex
- fixed legacy issues with absolute paths (added ^ in regex's)
- split into 2 methods for normal queries and quickfind
- fixed order of private methods
- Most code 'cherry picked by hand' from https://gerrit.onap.org/r/c/cps/+/134481
Issue-ID: CPS-1671
Signed-off-by: ToineSiebelink <toine.siebelink@est.tech>
Change-Id: I37988ac24d9b8d4df7a249b79068910c15a6cd20
Diffstat (limited to 'cps-ri/src/main/java')
-rw-r--r-- | cps-ri/src/main/java/org/onap/cps/spi/impl/CpsDataPersistenceServiceImpl.java | 4 | ||||
-rw-r--r-- | cps-ri/src/main/java/org/onap/cps/spi/repository/FragmentQueryBuilder.java | 52 |
2 files changed, 35 insertions, 21 deletions
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 0a77de234e..7a556bf467 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 @@ -359,7 +359,7 @@ public class CpsDataPersistenceServiceImpl implements CpsDataPersistenceService final AnchorEntity anchorEntity, final CpsPathQuery cpsPathQuery) { Collection<FragmentEntity> fragmentEntities; - final String xpathRegex = FragmentQueryBuilder.getXpathSqlRegex(cpsPathQuery, true); + final String xpathRegex = FragmentQueryBuilder.getXpathSqlRegexForQuickFindWithDescendants(cpsPathQuery); final List<FragmentExtract> fragmentExtracts = fragmentRepository.quickFindWithDescendants(anchorEntity.getId(), xpathRegex); fragmentEntities = FragmentEntityArranger.toFragmentEntityTrees(anchorEntity, fragmentExtracts); @@ -381,7 +381,7 @@ public class CpsDataPersistenceServiceImpl implements CpsDataPersistenceService final FetchDescendantsOption fetchDescendantsOption, final DataspaceEntity dataspaceEntity, final CpsPathQuery cpsPathQuery) { Collection<FragmentEntity> fragmentEntities; - final String xpathRegex = FragmentQueryBuilder.getXpathSqlRegex(cpsPathQuery, true); + final String xpathRegex = FragmentQueryBuilder.getXpathSqlRegexForQuickFindWithDescendants(cpsPathQuery); final List<FragmentExtract> fragmentExtracts = fragmentRepository .quickFindWithDescendantsAcrossAnchor(dataspaceEntity.getId(), xpathRegex); final Map<Long, AnchorEntity> fragmentExtractAnchorMap = getFragmentExtractAnchorMap(fragmentExtracts); diff --git a/cps-ri/src/main/java/org/onap/cps/spi/repository/FragmentQueryBuilder.java b/cps-ri/src/main/java/org/onap/cps/spi/repository/FragmentQueryBuilder.java index 5716304a12..f124a8adf1 100644 --- a/cps-ri/src/main/java/org/onap/cps/spi/repository/FragmentQueryBuilder.java +++ b/cps-ri/src/main/java/org/onap/cps/spi/repository/FragmentQueryBuilder.java @@ -38,10 +38,10 @@ import org.springframework.stereotype.Component; @Slf4j @Component public class FragmentQueryBuilder { - private static final String REGEX_ABSOLUTE_PATH_PREFIX = ".*\\/"; - private static final String REGEX_OPTIONAL_LIST_INDEX_POSTFIX = "(\\[@(?!.*\\[).*?])?"; - private static final String REGEX_DESCENDANT_PATH_POSTFIX = "(\\/.*)?"; - private static final String REGEX_END_OF_INPUT = "$"; + private static final String REGEX_ABSOLUTE_PATH_PREFIX = "^"; + private static final String REGEX_DESCENDANT_PATH_PREFIX = "^.*\\/"; + private static final String REGEX_OPTIONAL_LIST_INDEX_POSTFIX = "(\\[@(?!.*\\[).*?])?$"; + private static final String REGEX_FOR_QUICK_FIND_WITH_DESCENDANTS = "(\\[@.*?])?(\\/.*)?$"; @PersistenceContext private EntityManager entityManager; @@ -60,7 +60,7 @@ public class FragmentQueryBuilder { final Map<String, Object> queryParameters = new HashMap<>(); queryParameters.put("anchorId", anchorId); sqlStringBuilder.append(" AND xpath ~ :xpathRegex"); - final String xpathRegex = getXpathSqlRegex(cpsPathQuery, false); + final String xpathRegex = getXpathSqlRegex(cpsPathQuery); queryParameters.put("xpathRegex", xpathRegex); if (cpsPathQuery.hasLeafConditions()) { sqlStringBuilder.append(" AND attributes @> :leafDataAsJson\\:\\:jsonb"); @@ -84,7 +84,7 @@ public class FragmentQueryBuilder { final StringBuilder sqlStringBuilder = new StringBuilder("SELECT * FROM FRAGMENT WHERE dataspace_id = " + ":dataspaceId AND xpath ~ :xpathRegex"); final Map<String, Object> queryParameters = new HashMap<>(); - final String xpathRegex = getXpathSqlRegex(cpsPathQuery, false); + final String xpathRegex = getXpathSqlRegex(cpsPathQuery); queryParameters.put("dataspaceId", dataspaceId); queryParameters.put("xpathRegex", xpathRegex); if (cpsPathQuery.hasLeafConditions()) { @@ -100,26 +100,40 @@ public class FragmentQueryBuilder { } /** - * Create a regular expression (string) for xpath based on the given cps path query. + * Create a regular expression (string) for matching xpaths based on the given cps path query. * - * @param cpsPathQuery the cps path query to determine the required regular expression - * @param includeDescendants include descendants yes or no + * @param cpsPathQuery the cps path query to determine the required regular expression * @return a string representing the required regular expression */ - public static String getXpathSqlRegex(final CpsPathQuery cpsPathQuery, final boolean includeDescendants) { + public static String getXpathSqlRegex(final CpsPathQuery cpsPathQuery) { + final StringBuilder xpathRegexBuilder = getRegexStringBuilderWithPrefix(cpsPathQuery); + xpathRegexBuilder.append(REGEX_OPTIONAL_LIST_INDEX_POSTFIX); + return xpathRegexBuilder.toString(); + } + + /** + * Create a regular expression (string) for matching xpaths with (all) descendants + * based on the given cps path query. + * + * @param cpsPathQuery the cps path query to determine the required regular expression + * @return a string representing the required regular expression + */ + public static String getXpathSqlRegexForQuickFindWithDescendants(final CpsPathQuery cpsPathQuery) { + final StringBuilder xpathRegexBuilder = getRegexStringBuilderWithPrefix(cpsPathQuery); + xpathRegexBuilder.append(REGEX_FOR_QUICK_FIND_WITH_DESCENDANTS); + return xpathRegexBuilder.toString(); + } + + private static StringBuilder getRegexStringBuilderWithPrefix(final CpsPathQuery cpsPathQuery) { final StringBuilder xpathRegexBuilder = new StringBuilder(); if (CpsPathPrefixType.ABSOLUTE.equals(cpsPathQuery.getCpsPathPrefixType())) { - xpathRegexBuilder.append(escapeXpath(cpsPathQuery.getXpathPrefix())); - } else { xpathRegexBuilder.append(REGEX_ABSOLUTE_PATH_PREFIX); - xpathRegexBuilder.append(escapeXpath(cpsPathQuery.getDescendantName())); - } - xpathRegexBuilder.append(REGEX_OPTIONAL_LIST_INDEX_POSTFIX); - if (includeDescendants) { - xpathRegexBuilder.append(REGEX_DESCENDANT_PATH_POSTFIX); + xpathRegexBuilder.append(escapeXpath(cpsPathQuery.getXpathPrefix())); + return xpathRegexBuilder; } - xpathRegexBuilder.append(REGEX_END_OF_INPUT); - return xpathRegexBuilder.toString(); + xpathRegexBuilder.append(REGEX_DESCENDANT_PATH_PREFIX); + xpathRegexBuilder.append(escapeXpath(cpsPathQuery.getDescendantName())); + return xpathRegexBuilder; } private static String escapeXpath(final String xpath) { |