summaryrefslogtreecommitdiffstats
path: root/cps-ri
diff options
context:
space:
mode:
Diffstat (limited to 'cps-ri')
-rw-r--r--cps-ri/src/main/java/org/onap/cps/spi/impl/CpsDataPersistenceServiceImpl.java2
-rw-r--r--cps-ri/src/main/java/org/onap/cps/spi/repository/FragmentQueryBuilder.java60
2 files changed, 38 insertions, 24 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 95270786f9..78983ee0df 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
@@ -357,7 +357,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 = (anchorEntity == ALL_ANCHORS)
? fragmentRepository.quickFindWithDescendantsAcrossAnchor(xpathRegex) :
fragmentRepository.quickFindWithDescendants(anchorEntity.getId(), xpathRegex);
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 212eb93cb2..0134873347 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
@@ -41,10 +41,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;
@@ -78,15 +78,40 @@ public class FragmentQueryBuilder {
return getQuery(cpsPathQuery, sqlStringBuilder, queryParameters);
}
+ /**
+ * 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
+ * @return a string representing the required regular expression
+ */
+ 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 Query getQuery(final CpsPathQuery cpsPathQuery, final StringBuilder sqlStringBuilder,
final Map<String, Object> queryParameters) {
- final String xpathRegex = getXpathSqlRegex(cpsPathQuery, false);
+ final String xpathRegex = getXpathSqlRegex(cpsPathQuery);
queryParameters.put("xpathRegex", xpathRegex);
final List<String> queryBooleanOperatorsType = cpsPathQuery.getBooleanOperatorsType();
if (cpsPathQuery.hasLeafConditions()) {
sqlStringBuilder.append(" AND (");
final Queue<String> booleanOperatorsQueue = (queryBooleanOperatorsType == null) ? null : new LinkedList<>(
- queryBooleanOperatorsType);
+ queryBooleanOperatorsType);
cpsPathQuery.getLeavesData().entrySet().forEach(entry -> {
sqlStringBuilder.append(" attributes @> ");
sqlStringBuilder.append("'" + jsonObjectMapper.asJsonString(entry) + "'");
@@ -103,27 +128,16 @@ public class FragmentQueryBuilder {
return query;
}
- /**
- * Create a regular expression (string) for xpath 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
- * @return a string representing the required regular expression
- */
- public static String getXpathSqlRegex(final CpsPathQuery cpsPathQuery, final boolean includeDescendants) {
+ 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) {