From 4cc5016cbc70f6030f8dfb837029cce20fc0ed42 Mon Sep 17 00:00:00 2001 From: AbdelHedhili Date: Tue, 3 Mar 2020 17:28:59 +0100 Subject: [PATCH] add a buffered deletetion Signed-off-by: AbdelHedhili --- .../BufferedRestNetworkStoreClient.java | 34 ++++++++++++-- .../network/store/client/Resources.java | 9 ++++ .../store/client/RestNetworkStoreClient.java | 6 ++- .../store/integration/NetworkStoreIT.java | 45 +++++++++++++++++++ 4 files changed, 89 insertions(+), 5 deletions(-) diff --git a/network-store-client/src/main/java/com/powsybl/network/store/client/BufferedRestNetworkStoreClient.java b/network-store-client/src/main/java/com/powsybl/network/store/client/BufferedRestNetworkStoreClient.java index f82ec3ac0..426893854 100644 --- a/network-store-client/src/main/java/com/powsybl/network/store/client/BufferedRestNetworkStoreClient.java +++ b/network-store-client/src/main/java/com/powsybl/network/store/client/BufferedRestNetworkStoreClient.java @@ -45,6 +45,8 @@ public class BufferedRestNetworkStoreClient implements NetworkStoreClient { private final Map>> danglingLineResourcesToFlush = new HashMap<>(); + private final Map> danglingLineToRemove = new HashMap<>(); + private final Map>> twoWindingsTransformerResourcesToFlush = new HashMap<>(); private final Map>> threeWindingsTransformerResourcesToFlush = new HashMap<>(); @@ -445,6 +447,13 @@ public int getHvdcLineCount(UUID networkUuid) { @Override public void createDanglingLines(UUID networkUuid, List> danglingLineResources) { + if (danglingLineToRemove.get(networkUuid) != null) { + int dlToRemoveSize = danglingLineToRemove.size(); + danglingLineResources.forEach(danglingLineResource -> danglingLineToRemove.get(networkUuid).remove(danglingLineResource.getId())); + if (dlToRemoveSize != danglingLineToRemove.size()) { + return; + } + } danglingLineResourcesToFlush.computeIfAbsent(networkUuid, k -> new ArrayList<>()).addAll(danglingLineResources); } @@ -465,10 +474,15 @@ public int getDanglingLineCount(UUID networkUuid) { @Override public void removeDanglingLine(UUID networkUuid, String danglingLineId) { - List> dlToRemove = - danglingLineResourcesToFlush.get(networkUuid).stream().filter(danglingLineAttributesResource -> danglingLineAttributesResource.getId().equals(danglingLineId)).collect(Collectors.toList()); - dlToRemove.forEach(danglingLineAttributesResource -> danglingLineResourcesToFlush.get(networkUuid).remove(danglingLineAttributesResource)); - client.removeDanglingLine(networkUuid, danglingLineId); + if (danglingLineResourcesToFlush.get(networkUuid) != null) { + List> dlToRemove = + danglingLineResourcesToFlush.get(networkUuid).stream().filter(danglingLineAttributesResource -> danglingLineAttributesResource.getId().equals(danglingLineId)).collect(Collectors.toList()); + dlToRemove.forEach(danglingLineAttributesResource -> danglingLineResourcesToFlush.get(networkUuid).remove(danglingLineAttributesResource)); + if (!dlToRemove.isEmpty()) { + return; + } + } + danglingLineToRemove.computeIfAbsent(networkUuid, k -> new ArrayList<>()).add(danglingLineId); } @Override @@ -501,6 +515,16 @@ private static void flushResources(Map> resourcesToDelete, + BiConsumer> deleteFct) { + if (!resourcesToDelete.isEmpty()) { + for (Map.Entry> e : resourcesToDelete.entrySet()) { + deleteFct.accept(e.getKey(), e.getValue()); + } + resourcesToDelete.clear(); + } + } + @Override public void flush() { if (!networkResourcesToFlush.isEmpty()) { @@ -524,5 +548,7 @@ public void flush() { flushResources(threeWindingsTransformerResourcesToFlush, client::createThreeWindingsTransformers); flushResources(lineResourcesToFlush, client::createLines); flushResources(busResourcesToFlush, client::createConfiguredBuses); + + removeResources(danglingLineToRemove, client::removeDanglingLines); } } diff --git a/network-store-client/src/main/java/com/powsybl/network/store/client/Resources.java b/network-store-client/src/main/java/com/powsybl/network/store/client/Resources.java index 218c1dade..d7aa47ef9 100644 --- a/network-store-client/src/main/java/com/powsybl/network/store/client/Resources.java +++ b/network-store-client/src/main/java/com/powsybl/network/store/client/Resources.java @@ -13,8 +13,10 @@ import org.springframework.core.ParameterizedTypeReference; import org.springframework.http.*; import org.springframework.web.client.RestTemplate; +import org.springframework.web.util.UriComponentsBuilder; import java.util.List; +import java.util.Map; import java.util.Objects; import java.util.Optional; @@ -53,6 +55,13 @@ public void delete(String url, Object... uriV restTemplate.delete(url, uriVariables); } + public void delete(String url, Map params, Object... uriVariables) { + UriComponentsBuilder builder = UriComponentsBuilder.fromUriString(url); + params.forEach(builder::queryParam); + builder.buildAndExpand(uriVariables).toUri(); + restTemplate.delete(builder.toUriString()); + } + public Optional> get(String target, String url, Object... uriVariables) { ResponseEntity> response = getDocument(url, uriVariables); if (response.getStatusCode() == HttpStatus.OK) { diff --git a/network-store-client/src/main/java/com/powsybl/network/store/client/RestNetworkStoreClient.java b/network-store-client/src/main/java/com/powsybl/network/store/client/RestNetworkStoreClient.java index fba0d1593..0ec4f8e0e 100644 --- a/network-store-client/src/main/java/com/powsybl/network/store/client/RestNetworkStoreClient.java +++ b/network-store-client/src/main/java/com/powsybl/network/store/client/RestNetworkStoreClient.java @@ -506,7 +506,11 @@ public int getDanglingLineCount(UUID networkUuid) { @Override public void removeDanglingLine(UUID networkUuid, String danglingLineId) { - resources.delete("/networks/{networkUuid}/dangling-lines/{danglingLineId}", networkUuid, danglingLineId); + resources.delete("/networks/{networkUuid}/dangling-lines/{danglingLineId}"); + } + + public void removeDanglingLines(UUID networkUuid, List danglingLinesId) { + danglingLinesId.forEach(danglingLineId -> removeDanglingLine(networkUuid, danglingLineId)); } //ConfiguredBus diff --git a/network-store-integration-test/src/test/java/com/powsybl/network/store/integration/NetworkStoreIT.java b/network-store-integration-test/src/test/java/com/powsybl/network/store/integration/NetworkStoreIT.java index c39a96189..566870d29 100644 --- a/network-store-integration-test/src/test/java/com/powsybl/network/store/integration/NetworkStoreIT.java +++ b/network-store-integration-test/src/test/java/com/powsybl/network/store/integration/NetworkStoreIT.java @@ -765,6 +765,20 @@ public void testUcteNetwork() { } } + @Test + public void testDanglingLineRemove() { + try (NetworkStoreService service = new NetworkStoreService(getBaseUrl())) { + service.flush(createRemoveDL(service.getNetworkFactory())); + } + + try (NetworkStoreService service = new NetworkStoreService(getBaseUrl())) { + Map networkIds = service.getNetworkIds(); + assertEquals(1, networkIds.size()); + Network readNetwork = service.getNetwork(networkIds.keySet().stream().findFirst().get()); + assertEquals(1, readNetwork.getDanglingLineCount()); + } + } + public Network loadUcteNetwork(NetworkFactory networkFactory) { String filePath = "/uctNetwork.uct"; ReadOnlyDataSource dataSource = new ResourceDataSource( @@ -922,4 +936,35 @@ private Network createGeneratorNetwork(NetworkFactory networkFactory, ReactiveLi } return network; } + + private Network createRemoveDL(NetworkFactory networkFactory) { + Network network = networkFactory.createNetwork("DL network", "test"); + Substation s1 = network.newSubstation() + .setId("S1") + .setCountry(Country.ES) + .add(); + VoltageLevel vl1 = s1.newVoltageLevel() + .setId("VL1") + .setNominalV(400f) + .setTopologyKind(TopologyKind.NODE_BREAKER) + .add(); + vl1.newDanglingLine() + .setId("dl1") + .setName("dl1") + .add(); + network.getDanglingLine("dl1").remove(); + vl1.newDanglingLine() + .setId("dl1") + .setName("dl1") + .add(); + vl1.getNodeBreakerView().setNodeCount(2); + vl1.newGenerator() + .setId("GEN") + .setNode(1) + .setMaxP(20) + .setMinP(-20) + .setVoltageRegulatorOn(true) + .add(); + return network; + } }