X-Git-Url: https://gerrit.onap.org/r/gitweb?a=blobdiff_plain;f=cps-ri%2Fsrc%2Fmain%2Fjava%2Forg%2Fonap%2Fcps%2Fspi%2Frepository%2FFragmentRepositoryCpsPathQueryImpl.java;h=4489cddd306ac198d796a31bfa865fd0bed3f6c2;hb=19a59f369cbb348441d69d5e2675f17e0e1b7f3f;hp=4aa3e5fb36c834334e64238babf2691453fa19a7;hpb=9ac6d71e6b4297d26fddd2c8fb3095e15c2444da;p=cps.git diff --git a/cps-ri/src/main/java/org/onap/cps/spi/repository/FragmentRepositoryCpsPathQueryImpl.java b/cps-ri/src/main/java/org/onap/cps/spi/repository/FragmentRepositoryCpsPathQueryImpl.java index 4aa3e5fb3..4489cddd3 100644 --- a/cps-ri/src/main/java/org/onap/cps/spi/repository/FragmentRepositoryCpsPathQueryImpl.java +++ b/cps-ri/src/main/java/org/onap/cps/spi/repository/FragmentRepositoryCpsPathQueryImpl.java @@ -1,6 +1,6 @@ /*- * ============LICENSE_START======================================================= - * Copyright (C) 2021 Nordix Foundation. + * Copyright (C) 2021-2022 Nordix Foundation. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -20,69 +20,78 @@ package org.onap.cps.spi.repository; -import com.google.gson.Gson; -import com.google.gson.GsonBuilder; +import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import javax.persistence.EntityManager; import javax.persistence.PersistenceContext; import javax.persistence.Query; -import org.jetbrains.annotations.NotNull; -import org.jetbrains.annotations.Nullable; +import javax.transaction.Transactional; +import lombok.RequiredArgsConstructor; import org.onap.cps.cpspath.parser.CpsPathPrefixType; import org.onap.cps.cpspath.parser.CpsPathQuery; import org.onap.cps.spi.entities.FragmentEntity; +import org.onap.cps.utils.JsonObjectMapper; +@RequiredArgsConstructor public class FragmentRepositoryCpsPathQueryImpl implements FragmentRepositoryCpsPathQuery { - public static final String SIMILAR_TO_ABSOLUTE_PATH_PREFIX = "%/"; - public static final String SIMILAR_TO_OPTIONAL_LIST_INDEX_POSTFIX = "(\\[[^/]*])?"; + public static final String REGEX_ABSOLUTE_PATH_PREFIX = ".*\\/"; + public static final String REGEX_OPTIONAL_LIST_INDEX_POSTFIX = "(\\[@(?!.*\\[).*?])?$"; @PersistenceContext private EntityManager entityManager; - - private static final Gson GSON = new GsonBuilder().create(); + private final JsonObjectMapper jsonObjectMapper; @Override + @Transactional public List findByAnchorAndCpsPath(final int anchorId, final CpsPathQuery cpsPathQuery) { - final var sqlStringBuilder = new StringBuilder("SELECT * FROM FRAGMENT WHERE anchor_id = :anchorId"); + final StringBuilder sqlStringBuilder = new StringBuilder("SELECT * FROM FRAGMENT WHERE anchor_id = :anchorId"); final Map queryParameters = new HashMap<>(); queryParameters.put("anchorId", anchorId); - sqlStringBuilder.append(" AND xpath SIMILAR TO :xpathRegex"); - final String xpathRegex = getSimilarToXpathSqlRegex(cpsPathQuery); + sqlStringBuilder.append(" AND xpath ~ :xpathRegex"); + final String xpathRegex = getXpathSqlRegex(cpsPathQuery); queryParameters.put("xpathRegex", xpathRegex); if (cpsPathQuery.hasLeafConditions()) { sqlStringBuilder.append(" AND attributes @> :leafDataAsJson\\:\\:jsonb"); - queryParameters.put("leafDataAsJson", GSON.toJson(cpsPathQuery.getLeavesData())); + queryParameters.put("leafDataAsJson", jsonObjectMapper.asJsonString( + cpsPathQuery.getLeavesData())); } addTextFunctionCondition(cpsPathQuery, sqlStringBuilder, queryParameters); - final var query = entityManager.createNativeQuery(sqlStringBuilder.toString(), FragmentEntity.class); + final Query query = entityManager.createNativeQuery(sqlStringBuilder.toString(), FragmentEntity.class); setQueryParameters(query, queryParameters); - return query.getResultList(); + return getFragmentEntitiesAsStream(query); + } + + private List getFragmentEntitiesAsStream(final Query query) { + final List fragmentEntities = new ArrayList<>(); + query.getResultStream().forEach(fragmentEntity -> { + fragmentEntities.add((FragmentEntity) fragmentEntity); + entityManager.detach(fragmentEntity); + }); + + return fragmentEntities; } - @NotNull - private static String getSimilarToXpathSqlRegex(final CpsPathQuery cpsPathQuery) { - final var xpathRegexBuilder = new StringBuilder(); + private static String getXpathSqlRegex(final CpsPathQuery cpsPathQuery) { + final StringBuilder xpathRegexBuilder = new StringBuilder(); if (CpsPathPrefixType.ABSOLUTE.equals(cpsPathQuery.getCpsPathPrefixType())) { xpathRegexBuilder.append(escapeXpath(cpsPathQuery.getXpathPrefix())); } else { - xpathRegexBuilder.append(SIMILAR_TO_ABSOLUTE_PATH_PREFIX); + xpathRegexBuilder.append(REGEX_ABSOLUTE_PATH_PREFIX); xpathRegexBuilder.append(escapeXpath(cpsPathQuery.getDescendantName())); } - xpathRegexBuilder.append(SIMILAR_TO_OPTIONAL_LIST_INDEX_POSTFIX); + xpathRegexBuilder.append(REGEX_OPTIONAL_LIST_INDEX_POSTFIX); return xpathRegexBuilder.toString(); } - @NotNull private static String escapeXpath(final String xpath) { // See https://jira.onap.org/browse/CPS-500 for limitations of this basic escape mechanism return xpath.replace("[@", "\\[@"); } - @Nullable private static Integer getTextValueAsInt(final CpsPathQuery cpsPathQuery) { try { return Integer.parseInt(cpsPathQuery.getTextFunctionConditionValue()); @@ -100,7 +109,7 @@ public class FragmentRepositoryCpsPathQueryImpl implements FragmentRepositoryCps .append(" OR attributes @> jsonb_build_object(:textLeafName, json_build_array(:textValue))"); queryParameters.put("textLeafName", cpsPathQuery.getTextFunctionConditionLeafName()); queryParameters.put("textValue", cpsPathQuery.getTextFunctionConditionValue()); - final var textValueAsInt = getTextValueAsInt(cpsPathQuery); + final Integer textValueAsInt = getTextValueAsInt(cpsPathQuery); if (textValueAsInt != null) { sqlStringBuilder.append(" OR attributes @> jsonb_build_object(:textLeafName, :textValueAsInt)"); sqlStringBuilder