diff options
Diffstat (limited to 'cps-ri/src/main')
6 files changed, 144 insertions, 34 deletions
diff --git a/cps-ri/src/main/java/org/onap/cps/spi/impl/CpsModulePersistenceServiceImpl.java b/cps-ri/src/main/java/org/onap/cps/spi/impl/CpsModulePersistenceServiceImpl.java index 647d6cdef..e9e945a49 100755 --- a/cps-ri/src/main/java/org/onap/cps/spi/impl/CpsModulePersistenceServiceImpl.java +++ b/cps-ri/src/main/java/org/onap/cps/spi/impl/CpsModulePersistenceServiceImpl.java @@ -30,7 +30,6 @@ import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.InputStream; import java.nio.charset.StandardCharsets; -import java.util.ArrayList; import java.util.Collection; import java.util.HashMap; import java.util.List; @@ -40,13 +39,14 @@ import java.util.Set; import java.util.regex.Pattern; import java.util.stream.Collectors; import javax.transaction.Transactional; -import lombok.AllArgsConstructor; +import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.apache.commons.codec.digest.DigestUtils; import org.apache.commons.lang3.StringUtils; import org.hibernate.exception.ConstraintViolationException; import org.onap.cps.spi.CpsAdminPersistenceService; import org.onap.cps.spi.CpsModulePersistenceService; +import org.onap.cps.spi.entities.DataspaceEntity; import org.onap.cps.spi.entities.SchemaSetEntity; import org.onap.cps.spi.entities.YangResourceEntity; import org.onap.cps.spi.entities.YangResourceModuleReference; @@ -72,7 +72,7 @@ import org.springframework.stereotype.Component; @Slf4j @Component -@AllArgsConstructor +@RequiredArgsConstructor public class CpsModulePersistenceServiceImpl implements CpsModulePersistenceService { private static final String YANG_RESOURCE_CHECKSUM_CONSTRAINT_NAME = "yang_resource_checksum_key"; @@ -80,15 +80,15 @@ public class CpsModulePersistenceServiceImpl implements CpsModulePersistenceServ private static final Pattern RFC6020_RECOMMENDED_FILENAME_PATTERN = Pattern .compile("([\\w-]+)@(\\d{4}-\\d{2}-\\d{2})(?:\\.yang)?", Pattern.CASE_INSENSITIVE); - private YangResourceRepository yangResourceRepository; + private final YangResourceRepository yangResourceRepository; - private SchemaSetRepository schemaSetRepository; + private final SchemaSetRepository schemaSetRepository; - private DataspaceRepository dataspaceRepository; + private final DataspaceRepository dataspaceRepository; - private CpsAdminPersistenceService cpsAdminPersistenceService; + private final CpsAdminPersistenceService cpsAdminPersistenceService; - private ModuleReferenceRepository moduleReferenceRepository; + private final ModuleReferenceRepository moduleReferenceRepository; @Override public Map<String, String> getYangSchemaResources(final String dataspaceName, final String schemaSetName) { @@ -164,13 +164,11 @@ public class CpsModulePersistenceServiceImpl implements CpsModulePersistenceServ final Map<String, String> newModuleNameToContentMap, final Collection<ModuleReference> moduleReferences) { storeSchemaSet(dataspaceName, schemaSetName, newModuleNameToContentMap); - final var dataspaceEntity = dataspaceRepository.getByName(dataspaceName); - final var schemaSetEntity = + final DataspaceEntity dataspaceEntity = dataspaceRepository.getByName(dataspaceName); + final SchemaSetEntity schemaSetEntity = schemaSetRepository.getByDataspaceAndName(dataspaceEntity, schemaSetName); - final List<Long> listOfYangResourceIds = new ArrayList<>(); - moduleReferences.forEach(moduleReference -> - listOfYangResourceIds.add(yangResourceRepository.getIdByModuleNameAndRevision( - moduleReference.getModuleName(), moduleReference.getRevision()))); + final List<Long> listOfYangResourceIds = + yangResourceRepository.getResourceIdsByModuleReferences(moduleReferences); yangResourceRepository.insertSchemaSetIdYangResourceId(schemaSetEntity.getId(), listOfYangResourceIds); } 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 37202498f..f07f7f8d5 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 @@ -20,12 +20,14 @@ package org.onap.cps.spi.repository; +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 javax.transaction.Transactional; import lombok.RequiredArgsConstructor; import org.onap.cps.cpspath.parser.CpsPathPrefixType; import org.onap.cps.cpspath.parser.CpsPathQuery; @@ -43,8 +45,9 @@ public class FragmentRepositoryCpsPathQueryImpl implements FragmentRepositoryCps private final JsonObjectMapper jsonObjectMapper; @Override + @Transactional public List<FragmentEntity> 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<String, Object> queryParameters = new HashMap<>(); queryParameters.put("anchorId", anchorId); sqlStringBuilder.append(" AND xpath SIMILAR TO :xpathRegex"); @@ -57,13 +60,23 @@ public class FragmentRepositoryCpsPathQueryImpl implements FragmentRepositoryCps } 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<FragmentEntity> getFragmentEntitiesAsStream(final Query query) { + final List<FragmentEntity> fragmentEntities = new ArrayList<>(); + query.getResultStream().forEach(fragmentEntity -> { + fragmentEntities.add((FragmentEntity) fragmentEntity); + entityManager.detach(fragmentEntity); + }); + + return fragmentEntities; } private static String getSimilarToXpathSqlRegex(final CpsPathQuery cpsPathQuery) { - final var xpathRegexBuilder = new StringBuilder(); + final StringBuilder xpathRegexBuilder = new StringBuilder(); if (CpsPathPrefixType.ABSOLUTE.equals(cpsPathQuery.getCpsPathPrefixType())) { xpathRegexBuilder.append(escapeXpath(cpsPathQuery.getXpathPrefix())); } else { @@ -96,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 diff --git a/cps-ri/src/main/java/org/onap/cps/spi/repository/SchemaSetYangResourceRepositoryImpl.java b/cps-ri/src/main/java/org/onap/cps/spi/repository/SchemaSetYangResourceRepositoryImpl.java index 04eaa453e..c87e15adb 100644 --- a/cps-ri/src/main/java/org/onap/cps/spi/repository/SchemaSetYangResourceRepositoryImpl.java +++ b/cps-ri/src/main/java/org/onap/cps/spi/repository/SchemaSetYangResourceRepositoryImpl.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,26 +20,40 @@ package org.onap.cps.spi.repository; +import java.sql.PreparedStatement; import java.util.List; import javax.persistence.EntityManager; import javax.persistence.PersistenceContext; +import org.hibernate.Session; import org.springframework.transaction.annotation.Transactional; + @Transactional public class SchemaSetYangResourceRepositoryImpl implements SchemaSetYangResourceRepository { + private static final int MAX_INSERT_BATCH_SIZE = 100; + @PersistenceContext private EntityManager entityManager; @Override - public void insertSchemaSetIdYangResourceId(final Integer schemaSetId, final List<Long> yangResourceId) { - final var query = "INSERT INTO SCHEMA_SET_YANG_RESOURCES (SCHEMA_SET_ID, YANG_RESOURCE_ID) " - + "VALUES ( :schemaSetId, :yangResourceId)"; - yangResourceId.forEach(id -> - entityManager.createNativeQuery(query) - .setParameter("schemaSetId", schemaSetId) - .setParameter("yangResourceId", id) - .executeUpdate() - ); + public void insertSchemaSetIdYangResourceId(final Integer schemaSetId, final List<Long> yangResourceIds) { + final Session session = entityManager.unwrap(Session.class); + session.doWork(connection -> { + try (PreparedStatement preparedStatement = connection.prepareStatement( + "INSERT INTO SCHEMA_SET_YANG_RESOURCES (SCHEMA_SET_ID, YANG_RESOURCE_ID) VALUES ( ?, ?)")) { + int sqlQueryCount = 1; + for (final long yangResourceId : yangResourceIds) { + preparedStatement.setInt(1, schemaSetId); + preparedStatement.setLong(2, yangResourceId); + preparedStatement.addBatch(); + if (sqlQueryCount % MAX_INSERT_BATCH_SIZE == 0 || sqlQueryCount == yangResourceIds.size()) { + preparedStatement.executeBatch(); + } + sqlQueryCount++; + } + } + }); } } + diff --git a/cps-ri/src/main/java/org/onap/cps/spi/repository/YangResourceNativeRepository.java b/cps-ri/src/main/java/org/onap/cps/spi/repository/YangResourceNativeRepository.java new file mode 100644 index 000000000..335c971f9 --- /dev/null +++ b/cps-ri/src/main/java/org/onap/cps/spi/repository/YangResourceNativeRepository.java @@ -0,0 +1,31 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.onap.cps.spi.repository; + +import java.util.Collection; +import java.util.List; +import org.onap.cps.spi.model.ModuleReference; + +public interface YangResourceNativeRepository { + + List<Long> getResourceIdsByModuleReferences(Collection<ModuleReference> moduleReferences); + +} diff --git a/cps-ri/src/main/java/org/onap/cps/spi/repository/YangResourceNativeRepositoryImpl.java b/cps-ri/src/main/java/org/onap/cps/spi/repository/YangResourceNativeRepositoryImpl.java new file mode 100644 index 000000000..e21fecb2b --- /dev/null +++ b/cps-ri/src/main/java/org/onap/cps/spi/repository/YangResourceNativeRepositoryImpl.java @@ -0,0 +1,58 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.onap.cps.spi.repository; + +import java.util.Collection; +import java.util.List; +import java.util.StringJoiner; +import javax.persistence.EntityManager; +import javax.persistence.PersistenceContext; +import javax.persistence.Query; +import org.hibernate.type.StandardBasicTypes; +import org.onap.cps.spi.model.ModuleReference; +import org.springframework.stereotype.Repository; +import org.springframework.transaction.annotation.Transactional; + +@Repository +public class YangResourceNativeRepositoryImpl implements YangResourceNativeRepository { + + @PersistenceContext + private EntityManager entityManager; + + @Override + @Transactional + public List<Long> getResourceIdsByModuleReferences(final Collection<ModuleReference> moduleReferences) { + final Query query = entityManager.createNativeQuery(getCombinedSelectSqlQuery(moduleReferences)) + .unwrap(org.hibernate.query.NativeQuery.class) + .addScalar("id", StandardBasicTypes.LONG); + return query.getResultList(); + } + + private String getCombinedSelectSqlQuery(final Collection<ModuleReference> moduleReferences) { + final StringJoiner sqlQueryJoiner = new StringJoiner(" UNION ALL "); + moduleReferences.stream().forEach(moduleReference -> { + sqlQueryJoiner.add(String.format("SELECT id FROM yang_resource WHERE module_name='%s' and revision='%s'", + moduleReference.getModuleName(), + moduleReference.getRevision())); + }); + return sqlQueryJoiner.toString(); + } +} diff --git a/cps-ri/src/main/java/org/onap/cps/spi/repository/YangResourceRepository.java b/cps-ri/src/main/java/org/onap/cps/spi/repository/YangResourceRepository.java index 98306d868..6ca4fff4f 100644 --- a/cps-ri/src/main/java/org/onap/cps/spi/repository/YangResourceRepository.java +++ b/cps-ri/src/main/java/org/onap/cps/spi/repository/YangResourceRepository.java @@ -34,7 +34,7 @@ import org.springframework.stereotype.Repository; @Repository public interface YangResourceRepository extends JpaRepository<YangResourceEntity, Long>, - SchemaSetYangResourceRepository { + YangResourceNativeRepository, SchemaSetYangResourceRepository { List<YangResourceEntity> findAllByChecksumIn(@NotNull Set<String> checksum); @@ -91,10 +91,6 @@ public interface YangResourceRepository extends JpaRepository<YangResourceEntity Set<YangResourceModuleReference> findAllModuleReferencesByDataspaceAndModuleNames( @Param("dataspaceName") String dataspaceName, @Param("moduleNames") Collection<String> moduleNames); - - @Query(value = "SELECT id FROM yang_resource WHERE module_name=:name and revision=:revision", nativeQuery = true) - Long getIdByModuleNameAndRevision(@Param("name") String moduleName, @Param("revision") String revision); - @Modifying @Query(value = "DELETE FROM yang_resource yr WHERE NOT EXISTS " + "(SELECT 1 FROM schema_set_yang_resources ssyr WHERE ssyr.yang_resource_id = yr.id)", nativeQuery = true) |