Skip to content

Commit

Permalink
add a buffered deletetion
Browse files Browse the repository at this point in the history
Signed-off-by: AbdelHedhili <[email protected]>
  • Loading branch information
AbdelHedhili committed Mar 4, 2020
1 parent c4c51c1 commit 4cc5016
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ public class BufferedRestNetworkStoreClient implements NetworkStoreClient {

private final Map<UUID, List<Resource<DanglingLineAttributes>>> danglingLineResourcesToFlush = new HashMap<>();

private final Map<UUID, List<String>> danglingLineToRemove = new HashMap<>();

private final Map<UUID, List<Resource<TwoWindingsTransformerAttributes>>> twoWindingsTransformerResourcesToFlush = new HashMap<>();

private final Map<UUID, List<Resource<ThreeWindingsTransformerAttributes>>> threeWindingsTransformerResourcesToFlush = new HashMap<>();
Expand Down Expand Up @@ -445,6 +447,13 @@ public int getHvdcLineCount(UUID networkUuid) {

@Override
public void createDanglingLines(UUID networkUuid, List<Resource<DanglingLineAttributes>> 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);
}

Expand All @@ -465,10 +474,15 @@ public int getDanglingLineCount(UUID networkUuid) {

@Override
public void removeDanglingLine(UUID networkUuid, String danglingLineId) {
List<Resource<DanglingLineAttributes>> 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<Resource<DanglingLineAttributes>> 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
Expand Down Expand Up @@ -501,6 +515,16 @@ private static <T extends IdentifiableAttributes> void flushResources(Map<UUID,
}
}

private static void removeResources(Map<UUID, List<String>> resourcesToDelete,
BiConsumer<UUID, List<String>> deleteFct) {
if (!resourcesToDelete.isEmpty()) {
for (Map.Entry<UUID, List<String>> e : resourcesToDelete.entrySet()) {
deleteFct.accept(e.getKey(), e.getValue());
}
resourcesToDelete.clear();
}
}

@Override
public void flush() {
if (!networkResourcesToFlush.isEmpty()) {
Expand All @@ -524,5 +548,7 @@ public void flush() {
flushResources(threeWindingsTransformerResourcesToFlush, client::createThreeWindingsTransformers);
flushResources(lineResourcesToFlush, client::createLines);
flushResources(busResourcesToFlush, client::createConfiguredBuses);

removeResources(danglingLineToRemove, client::removeDanglingLines);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -53,6 +55,13 @@ public <T extends IdentifiableAttributes> void delete(String url, Object... uriV
restTemplate.delete(url, uriVariables);
}

public <T extends IdentifiableAttributes> void delete(String url, Map<String, Object> params, Object... uriVariables) {
UriComponentsBuilder builder = UriComponentsBuilder.fromUriString(url);
params.forEach(builder::queryParam);
builder.buildAndExpand(uriVariables).toUri();
restTemplate.delete(builder.toUriString());
}

public <T extends IdentifiableAttributes> Optional<Resource<T>> get(String target, String url, Object... uriVariables) {
ResponseEntity<TopLevelDocument<T>> response = getDocument(url, uriVariables);
if (response.getStatusCode() == HttpStatus.OK) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> danglingLinesId) {
danglingLinesId.forEach(danglingLineId -> removeDanglingLine(networkUuid, danglingLineId));
}

//ConfiguredBus
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<UUID, String> 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(
Expand Down Expand Up @@ -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;
}
}

0 comments on commit 4cc5016

Please sign in to comment.