Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Refacto] Add delegate generic type and improve lazy loading extension code in preloading collection #464

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
* @author Franck Lecuyer <franck.lecuyer at rte-france.com>
* @author Etienne Homer <etienne.homer at rte-france.com>
*/
public class BufferedNetworkStoreClient extends AbstractForwardingNetworkStoreClient {
public class BufferedNetworkStoreClient extends AbstractForwardingNetworkStoreClient<RestNetworkStoreClient> {

private static final Logger LOGGER = LoggerFactory.getLogger(BufferedNetworkStoreClient.class);

Expand Down

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,16 @@
*/
package com.powsybl.network.store.client;

import com.powsybl.iidm.network.VariantManagerConstants;
import com.powsybl.network.store.iidm.impl.CachedNetworkStoreClient;
import com.powsybl.network.store.iidm.impl.OfflineNetworkStoreClient;
import com.powsybl.network.store.model.NetworkAttributes;
import com.powsybl.network.store.model.Resource;
import com.powsybl.network.store.model.ResourceType;
import org.junit.Test;

import java.time.ZonedDateTime;
import java.util.List;
import java.util.UUID;
import java.util.concurrent.ForkJoinPool;

Expand All @@ -23,29 +28,39 @@
public class PreloadingAllCollectionsTest {

@Test
public void test() {
var client = new PreloadingNetworkStoreClient(new CachedNetworkStoreClient(new OfflineNetworkStoreClient()), false, ForkJoinPool.commonPool());
public void testWithAllCollections() {
var client = new PreloadingNetworkStoreClient(new CachedNetworkStoreClient(new OfflineNetworkStoreClient()), true, ForkJoinPool.commonPool());
UUID networkUuid = UUID.fromString("7928181c-7977-4592-ba19-88027e4254e4");
assertTrue(client.shouldLoadAllCollectionsNeededForBusView(ResourceType.SUBSTATION));
client.getSubstations(networkUuid, 0);
assertTrue(client.isResourceTypeCached(networkUuid, 0, ResourceType.SUBSTATION));
for (ResourceType resourceType : ResourceType.values()) {
if (resourceType != ResourceType.SUBSTATION) {
assertFalse(client.isResourceTypeCached(networkUuid, 0, ResourceType.GENERATOR));
}
}
assertFalse(client.shouldLoadAllCollectionsNeededForBusView(ResourceType.SUBSTATION));
}

@Test
public void testWithAllCollections() {
public void testWithAllCollectionsDeleteNetwork() {
var client = new PreloadingNetworkStoreClient(new CachedNetworkStoreClient(new OfflineNetworkStoreClient()), true, ForkJoinPool.commonPool());
UUID networkUuid = UUID.fromString("7928181c-7977-4592-ba19-88027e4254e4");
Resource<NetworkAttributes> n1 = Resource.networkBuilder()
.id("n1")
.attributes(NetworkAttributes.builder()
.uuid(networkUuid)
.variantId(VariantManagerConstants.INITIAL_VARIANT_ID)
.caseDate(ZonedDateTime.parse("2015-01-01T00:00:00.000Z"))
.build())
.build();
client.createNetworks(List.of(n1));
assertTrue(client.shouldLoadAllCollectionsNeededForBusView(ResourceType.SUBSTATION));
client.getSubstations(networkUuid, 0);
assertFalse(client.shouldLoadAllCollectionsNeededForBusView(ResourceType.SUBSTATION));
// When we delete the network, we should load again all collection needed for bus view on next get
client.deleteNetwork(networkUuid);
assertTrue(client.shouldLoadAllCollectionsNeededForBusView(ResourceType.SUBSTATION));
client.createNetworks(List.of(n1));
assertTrue(client.shouldLoadAllCollectionsNeededForBusView(ResourceType.SUBSTATION));
client.getSubstations(networkUuid, 0);
for (ResourceType resourceType : ResourceType.values()) {
if (PreloadingNetworkStoreClient.RESOURCE_TYPES_NEEDED_FOR_BUS_VIEW.contains(resourceType)) {
assertTrue(client.isResourceTypeCached(networkUuid, 0, resourceType));
} else {
assertFalse(client.isResourceTypeCached(networkUuid, 0, resourceType));
}
}
assertFalse(client.shouldLoadAllCollectionsNeededForBusView(ResourceType.SUBSTATION));
// When we delete the network variant, we should load again all collection needed for bus view on next get
client.deleteNetwork(networkUuid, 0);
assertTrue(client.shouldLoadAllCollectionsNeededForBusView(ResourceType.SUBSTATION));
}
}

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@
/**
* @author Geoffroy Jamgotchian <geoffroy.jamgotchian at rte-france.com>
*/
public abstract class AbstractForwardingNetworkStoreClient implements NetworkStoreClient {
public abstract class AbstractForwardingNetworkStoreClient<T extends NetworkStoreClient> implements NetworkStoreClient {

@Delegate
protected final NetworkStoreClient delegate;
@Delegate(types = NetworkStoreClient.class)
protected final T delegate;

protected AbstractForwardingNetworkStoreClient(NetworkStoreClient delegate) {
protected AbstractForwardingNetworkStoreClient(T delegate) {
this.delegate = Objects.requireNonNull(delegate);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
* @author Nicolas Noir <nicolas.noir at rte-france.com>
* @author Etienne Homer <etienne.homer at rte-france.com>
*/
public class CachedNetworkStoreClient extends AbstractForwardingNetworkStoreClient implements NetworkStoreClient {
public class CachedNetworkStoreClient extends AbstractForwardingNetworkStoreClient<NetworkStoreClient> implements NetworkStoreClient {

private static final int MAX_GET_IDENTIFIABLE_CALL_COUNT = 10;

Expand Down Expand Up @@ -1076,19 +1076,17 @@ public Optional<ExtensionAttributes> getExtensionAttributes(UUID networkUuid, in
return getCache(resourceType).getCollection(networkUuid, variantNum).getExtensionAttributes(networkUuid, variantNum, resourceType, identifiableId, extensionName);
}

@Override
public Map<String, ExtensionAttributes> getAllExtensionsAttributesByResourceTypeAndExtensionName(UUID networkUuid, int variantNum, ResourceType resourceType, String extensionName) {
return getCache(resourceType).getCollection(networkUuid, variantNum).getAllExtensionsAttributesByResourceTypeAndExtensionName(networkUuid, variantNum, resourceType, extensionName);
public void loadAllExtensionsAttributesByResourceTypeAndExtensionName(UUID networkUuid, int variantNum, ResourceType resourceType, String extensionName) {
getCache(resourceType).getCollection(networkUuid, variantNum).loadAllExtensionsAttributesByResourceTypeAndExtensionName(networkUuid, variantNum, resourceType, extensionName);
}

@Override
public Map<String, ExtensionAttributes> getAllExtensionsAttributesByIdentifiableId(UUID networkUuid, int variantNum, ResourceType resourceType, String identifiableId) {
return getCache(resourceType).getCollection(networkUuid, variantNum).getAllExtensionsAttributesByIdentifiableId(networkUuid, variantNum, resourceType, identifiableId);
}

@Override
public Map<String, Map<String, ExtensionAttributes>> getAllExtensionsAttributesByResourceType(UUID networkUuid, int variantNum, ResourceType resourceType) {
return getCache(resourceType).getCollection(networkUuid, variantNum).getAllExtensionsAttributesByResourceType(networkUuid, variantNum, resourceType);
public void loadAllExtensionsAttributesByResourceType(UUID networkUuid, int variantNum, ResourceType resourceType) {
getCache(resourceType).getCollection(networkUuid, variantNum).loadAllExtensionsAttributesByResourceType(networkUuid, variantNum, resourceType);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -424,9 +424,9 @@ private void addExtensionAttributesToCache(String identifiableId, String extensi
}

/**
* Get the extensions attributes with specified extension name for all the identifiables of the collection in the cache.
* Load all the extensions attributes with specified extension name for all the identifiables of the collection in the cache.
*/
public Map<String, ExtensionAttributes> getAllExtensionsAttributesByResourceTypeAndExtensionName(UUID networkUuid, int variantNum, ResourceType type, String extensionName) {
public void loadAllExtensionsAttributesByResourceTypeAndExtensionName(UUID networkUuid, int variantNum, ResourceType type, String extensionName) {
if (!isFullyLoadedExtension(extensionName)) {
// if collection has not yet been fully loaded we load it from the server
Map<String, ExtensionAttributes> extensionAttributesMap = delegate.getAllExtensionsAttributesByResourceTypeAndExtensionName(networkUuid, variantNum, type, extensionName);
Expand All @@ -435,13 +435,6 @@ public Map<String, ExtensionAttributes> getAllExtensionsAttributesByResourceType
extensionAttributesMap.forEach((identifiableId, extensionAttributes) -> addExtensionAttributesToCache(identifiableId, extensionName, extensionAttributes));
fullyLoadedExtensionsByExtensionName.add(extensionName);
}
//TODO This method is only used to load extension attributes in the collection cache when using preloading collection.
// The return is never used by the client as the call to getAllExtensionsAttributesByResourceTypeAndExtensionName() is always followed
// by a call to getExtensionAttributes(). The latter returns something meaningful for the client
// and it's used in the identifiable.getExtension() method. The map extensionAttributesMap can't be stored in the cache to be returned
// as we can't ensure synchronization with the resources map (if extensions or identifiables are updated/removed).
// We should refactor this method to return void.
return null;
}

/**
Expand Down Expand Up @@ -486,9 +479,9 @@ private void addAllExtensionAttributesToCache(String id, Map<String, ExtensionAt
}

/**
* Get all the extensions attributes for all the identifiables with specified resource type in the cache
* Load all the extensions attributes for all the identifiables with specified resource type in the cache
*/
public Map<String, Map<String, ExtensionAttributes>> getAllExtensionsAttributesByResourceType(UUID networkUuid, int variantNum, ResourceType type) {
public void loadAllExtensionsAttributesByResourceType(UUID networkUuid, int variantNum, ResourceType type) {
if (!fullyLoadedExtensions) {
// if collection has not yet been fully loaded we load it from the server
Map<String, Map<String, ExtensionAttributes>> extensionAttributesMap = delegate.getAllExtensionsAttributesByResourceType(networkUuid, variantNum, type);
Expand All @@ -497,13 +490,6 @@ public Map<String, Map<String, ExtensionAttributes>> getAllExtensionsAttributesB
extensionAttributesMap.forEach(this::addAllExtensionAttributesToCache);
fullyLoadedExtensions = true;
}
//TODO This method is only used to load extension attributes in the collection cache when using preloading collection.
// The return is never used by the client as the call to getAllExtensionsAttributesByResourceType() is always followed
// by a call to getAllExtensionsAttributesByIdentifiableId(). The latter returns something meaningful for the client
// and it's used in the identifiable.getExtensions() method. The map extensionAttributesMap can't be stored in the cache to be returned
// as we can't ensure synchronization with the resources map (if extensions or identifiables are updated/removed).
// We should refactor this method to return void.
return null;
}

public void removeExtensionAttributesByExtensionName(String identifiableId, String extensionName) {
Expand Down
Loading
Loading