diff options
author | danielhanrahan <daniel.hanrahan@est.tech> | 2023-08-04 11:22:43 +0100 |
---|---|---|
committer | danielhanrahan <daniel.hanrahan@est.tech> | 2024-01-09 10:47:51 +0000 |
commit | 926d40875fecb3c11e4103dfdaf00ac615de3cdc (patch) | |
tree | 3dd976137922160419ef781ac6be34cd00441e1f /cps-ncmp-service/src/main | |
parent | 8e617d64a7725f5fb0f0f31a5c551ff3e1e484de (diff) |
Remove inefficient saveListElementsBatch API
CpsDataService::saveListElementsBatch method is not needed as
saveListElements supports saving multiple list elements in a single
operation. This both simplifies implementation and greatly improves
performance when saving list elements, as the Yang parser need only
run once for the whole batch.
- Change InventoryPersistence to save CM-handles in batches of 100
using existing CpsDataService::saveListElements method.
- Remove not needed CpsDataService::saveListElementBatch.
Issue-ID: CPS-2019
Signed-off-by: danielhanrahan <daniel.hanrahan@est.tech>
Change-Id: I8b74dda2917e094d064b42f2c0e4d57029b90395
Diffstat (limited to 'cps-ncmp-service/src/main')
2 files changed, 18 insertions, 15 deletions
diff --git a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/inventory/InventoryPersistence.java b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/inventory/InventoryPersistence.java index 09de9a7bf3..9024eac331 100644 --- a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/inventory/InventoryPersistence.java +++ b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/inventory/InventoryPersistence.java @@ -1,6 +1,6 @@ /* * ============LICENSE_START======================================================= - * Copyright (C) 2022-2023 Nordix Foundation + * Copyright (C) 2022-2024 Nordix Foundation * Modifications Copyright (C) 2023 TechMahindra Ltd. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); @@ -22,6 +22,7 @@ package org.onap.cps.ncmp.api.impl.inventory; import java.util.Collection; +import java.util.List; import java.util.Map; import org.onap.cps.ncmp.api.impl.ncmppersistence.NcmpPersistence; import org.onap.cps.ncmp.api.impl.yangmodels.YangModelCmHandle; @@ -98,7 +99,7 @@ public interface InventoryPersistence extends NcmpPersistence { * * @param yangModelCmHandles cm handle represented as Yang Models */ - void saveCmHandleBatch(Collection<YangModelCmHandle> yangModelCmHandles); + void saveCmHandleBatch(List<YangModelCmHandle> yangModelCmHandles); /** * Get data node of given cm handle. diff --git a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/inventory/InventoryPersistenceImpl.java b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/inventory/InventoryPersistenceImpl.java index a0aeac3e89..33d6e9a9cc 100644 --- a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/inventory/InventoryPersistenceImpl.java +++ b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/inventory/InventoryPersistenceImpl.java @@ -1,6 +1,6 @@ /* * ============LICENSE_START======================================================= - * Copyright (C) 2022-2023 Nordix Foundation + * Copyright (C) 2022-2024 Nordix Foundation * Modifications Copyright (C) 2022 Bell Canada * Modifications Copyright (C) 2023 TechMahindra Ltd. * ================================================================================ @@ -22,9 +22,11 @@ package org.onap.cps.ncmp.api.impl.inventory; +import com.google.common.collect.Lists; import java.time.OffsetDateTime; import java.util.ArrayList; import java.util.Collection; +import java.util.Collections; import java.util.HashMap; import java.util.List; import java.util.Map; @@ -47,6 +49,8 @@ import org.springframework.stereotype.Component; @Component public class InventoryPersistenceImpl extends NcmpPersistenceImpl implements InventoryPersistence { + private static final int CMHANDLE_BATCH_SIZE = 100; + private final CpsModuleService cpsModuleService; private final CpsAnchorService cpsAnchorService; private final CpsValidator cpsValidator; @@ -131,19 +135,17 @@ public class InventoryPersistenceImpl extends NcmpPersistenceImpl implements Inv @Override public void saveCmHandle(final YangModelCmHandle yangModelCmHandle) { - final String cmHandleJsonData = - createCmHandleJsonData(jsonObjectMapper.asJsonString(yangModelCmHandle)); - cpsDataService.saveListElements(NCMP_DATASPACE_NAME, NCMP_DMI_REGISTRY_ANCHOR, NCMP_DMI_REGISTRY_PARENT, - cmHandleJsonData, NO_TIMESTAMP); + saveCmHandleBatch(Collections.singletonList(yangModelCmHandle)); } @Override - public void saveCmHandleBatch(final Collection<YangModelCmHandle> yangModelCmHandles) { - final List<String> cmHandlesJsonData = new ArrayList<>(); - yangModelCmHandles.forEach(yangModelCmHandle -> cmHandlesJsonData.add( - createCmHandleJsonData(jsonObjectMapper.asJsonString(yangModelCmHandle)))); - cpsDataService.saveListElementsBatch(NCMP_DATASPACE_NAME, NCMP_DMI_REGISTRY_ANCHOR, - NCMP_DMI_REGISTRY_PARENT, cmHandlesJsonData, NO_TIMESTAMP); + public void saveCmHandleBatch(final List<YangModelCmHandle> yangModelCmHandles) { + for (final List<YangModelCmHandle> yangModelCmHandleBatch : + Lists.partition(yangModelCmHandles, CMHANDLE_BATCH_SIZE)) { + final String cmHandlesJsonData = createCmHandlesJsonData(yangModelCmHandleBatch); + cpsDataService.saveListElements(NCMP_DATASPACE_NAME, NCMP_DMI_REGISTRY_ANCHOR, + NCMP_DMI_REGISTRY_PARENT, cmHandlesJsonData, NO_TIMESTAMP); + } } @Override @@ -171,7 +173,7 @@ public class InventoryPersistenceImpl extends NcmpPersistenceImpl implements Inv return "{\"state\":" + state + "}"; } - private static String createCmHandleJsonData(final String yangModelCmHandleAsJson) { - return "{\"cm-handles\":[" + yangModelCmHandleAsJson + "]}"; + private String createCmHandlesJsonData(final List<YangModelCmHandle> yangModelCmHandles) { + return "{\"cm-handles\":" + jsonObjectMapper.asJsonString(yangModelCmHandles) + "}"; } } |