Skip to content

Commit 6a31f41

Browse files
authored
Lazy loading of extension attributes (#411)
Extension attributes are loaded in the identifiable only when needed (i.e: when getExtension(), getExtensionByName(), getExtensions() or removeExtension() are called). The collection cache is enriched to cache extension attributes similarly to identifiable resources. Signed-off-by: Antoine Bouhours <[email protected]>
1 parent 8604545 commit 6a31f41

File tree

19 files changed

+1938
-163
lines changed

19 files changed

+1938
-163
lines changed

network-store-client/src/main/java/com/powsybl/network/store/client/PreloadingNetworkStoreClient.java

+12
Original file line numberDiff line numberDiff line change
@@ -871,4 +871,16 @@ public void removeConfiguredBuses(UUID networkUuid, int variantNum, List<String>
871871
ensureCached(ResourceType.CONFIGURED_BUS, networkUuid, variantNum);
872872
delegate.removeConfiguredBuses(networkUuid, variantNum, configuredBusesId);
873873
}
874+
875+
@Override
876+
public Optional<ExtensionAttributes> getExtensionAttributes(UUID networkUuid, int variantNum, ResourceType resourceType, String identifiableId, String extensionName) {
877+
delegate.getAllExtensionsAttributesByResourceTypeAndExtensionName(networkUuid, variantNum, resourceType, extensionName);
878+
return delegate.getExtensionAttributes(networkUuid, variantNum, resourceType, identifiableId, extensionName);
879+
}
880+
881+
@Override
882+
public Map<String, ExtensionAttributes> getAllExtensionsAttributesByIdentifiableId(UUID networkUuid, int variantNum, ResourceType resourceType, String id) {
883+
delegate.getAllExtensionsAttributesByResourceType(networkUuid, variantNum, resourceType);
884+
return delegate.getAllExtensionsAttributesByIdentifiableId(networkUuid, variantNum, resourceType, id);
885+
}
874886
}

network-store-client/src/main/java/com/powsybl/network/store/client/RestClient.java

+8
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
package com.powsybl.network.store.client;
88

99
import com.powsybl.network.store.model.Attributes;
10+
import com.powsybl.network.store.model.ExtensionAttributes;
1011
import com.powsybl.network.store.model.IdentifiableAttributes;
1112
import com.powsybl.network.store.model.Resource;
1213
import org.springframework.core.ParameterizedTypeReference;
@@ -23,6 +24,13 @@ public interface RestClient {
2324

2425
<T extends IdentifiableAttributes> Optional<Resource<T>> getOne(String target, String url, Object... uriVariables);
2526

27+
/**
28+
* Retrieves one extension attributes from the server.
29+
* @return {@link ExtensionAttributes} which is a subset of an identifiable resource. The extension attributes can be put in the extensionAttributes
30+
* map of an {@link IdentifiableAttributes} or used to load an extension.
31+
*/
32+
Optional<ExtensionAttributes> getOneExtensionAttributes(String url, Object... uriVariables);
33+
2634
<T extends IdentifiableAttributes> List<Resource<T>> getAll(String target, String url, Object... uriVariables);
2735

2836
<T extends Attributes> void updateAll(String url, List<Resource<T>> resources, Object... uriVariables);

network-store-client/src/main/java/com/powsybl/network/store/client/RestClientImpl.java

+13
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,19 @@ public <T extends IdentifiableAttributes> Optional<Resource<T>> getOne(String ta
103103
}
104104
}
105105

106+
@Override
107+
public Optional<ExtensionAttributes> getOneExtensionAttributes(String url, Object... uriVariables) {
108+
ResponseEntity<ExtensionAttributes> response = restTemplate.exchange(url, HttpMethod.GET, null, new ParameterizedTypeReference<>() { }, uriVariables);
109+
if (response.getStatusCode() == HttpStatus.OK) {
110+
ExtensionAttributes body = response.getBody();
111+
return Optional.of(body);
112+
} else if (response.getStatusCode() == HttpStatus.NOT_FOUND) {
113+
return Optional.empty();
114+
} else {
115+
throw createHttpException(url, "get", response.getStatusCode());
116+
}
117+
}
118+
106119
@Override
107120
public <T extends IdentifiableAttributes> List<Resource<T>> getAll(String target, String url, Object... uriVariables) {
108121
ResponseEntity<TopLevelDocument<T>> response = getDocument(url, uriVariables);

network-store-client/src/main/java/com/powsybl/network/store/client/RestNetworkStoreClient.java

+68
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,49 @@ private <T extends IdentifiableAttributes> Optional<Resource<T>> get(String targ
115115
return resource;
116116
}
117117

118+
private Optional<ExtensionAttributes> getExtensionAttributes(String urlTemplate, Object... uriVariables) {
119+
logGetExtensionAttributesUrl(urlTemplate, uriVariables);
120+
Stopwatch stopwatch = Stopwatch.createStarted();
121+
Optional<ExtensionAttributes> extensionAttributes = restClient.getOneExtensionAttributes(urlTemplate, uriVariables);
122+
stopwatch.stop();
123+
logGetExtensionAttributesTime(extensionAttributes.isPresent() ? 1 : 0, stopwatch.elapsed(TimeUnit.MILLISECONDS));
124+
125+
return extensionAttributes;
126+
}
127+
128+
private Map<String, ExtensionAttributes> getExtensionAttributesMap(String urlTemplate, Object... uriVariables) {
129+
logGetExtensionAttributesUrl(urlTemplate, uriVariables);
130+
Stopwatch stopwatch = Stopwatch.createStarted();
131+
Map<String, ExtensionAttributes> extensionAttributes = restClient.get(urlTemplate, new ParameterizedTypeReference<>() { }, uriVariables);
132+
stopwatch.stop();
133+
logGetExtensionAttributesTime(extensionAttributes.size(), stopwatch.elapsed(TimeUnit.MILLISECONDS));
134+
135+
return extensionAttributes;
136+
}
137+
138+
private Map<String, Map<String, ExtensionAttributes>> getExtensionAttributesNestedMap(String urlTemplate, Object... uriVariables) {
139+
logGetExtensionAttributesUrl(urlTemplate, uriVariables);
140+
Stopwatch stopwatch = Stopwatch.createStarted();
141+
Map<String, Map<String, ExtensionAttributes>> extensionAttributes = restClient.get(urlTemplate, new ParameterizedTypeReference<>() { }, uriVariables);
142+
stopwatch.stop();
143+
long loadedAttributesCount = extensionAttributes.values().stream()
144+
.mapToLong(innerMap -> innerMap.values().size())
145+
.sum();
146+
logGetExtensionAttributesTime(loadedAttributesCount, stopwatch.elapsed(TimeUnit.MILLISECONDS));
147+
148+
return extensionAttributes;
149+
}
150+
151+
private static void logGetExtensionAttributesUrl(String urlTemplate, Object... uriVariables) {
152+
if (LOGGER.isInfoEnabled()) {
153+
LOGGER.info("Loading extension attributes {}", UriComponentsBuilder.fromUriString(urlTemplate).build(uriVariables));
154+
}
155+
}
156+
157+
private static void logGetExtensionAttributesTime(long loadedAttributesCount, long timeElapsed) {
158+
LOGGER.info("{} extension attributes loaded in {} ms", loadedAttributesCount, timeElapsed);
159+
}
160+
118161
private <T extends IdentifiableAttributes> void updatePartition(String target, String url, AttributeFilter attributeFilter, List<Resource<T>> resources, Object[] uriVariables) {
119162
if (attributeFilter == null) {
120163
if (LOGGER.isInfoEnabled()) {
@@ -848,4 +891,29 @@ public void updateGrounds(UUID networkUuid, List<Resource<GroundAttributes>> gro
848891
AttributeFilter attributeFilter) {
849892
updateAll(STR_GROUND, "/networks/{networkUuid}/grounds", groundResources, attributeFilter, networkUuid);
850893
}
894+
895+
@Override
896+
public Optional<ExtensionAttributes> getExtensionAttributes(UUID networkUuid, int variantNum, ResourceType resourceType, String identifiableId, String extensionName) {
897+
return getExtensionAttributes("/networks/{networkUuid}/{variantNum}/identifiables/{identifiableId}/extensions/{extensionName}", networkUuid, variantNum, identifiableId, extensionName);
898+
}
899+
900+
@Override
901+
public Map<String, ExtensionAttributes> getAllExtensionsAttributesByResourceTypeAndExtensionName(UUID networkUuid, int variantNum, ResourceType resourceType, String extensionName) {
902+
return getExtensionAttributesMap("/networks/{networkUuid}/{variantNum}/types/{type}/extensions/{extensionName}", networkUuid, variantNum, resourceType, extensionName);
903+
}
904+
905+
@Override
906+
public Map<String, ExtensionAttributes> getAllExtensionsAttributesByIdentifiableId(UUID networkUuid, int variantNum, ResourceType resourceType, String identifiableId) {
907+
return getExtensionAttributesMap("/networks/{networkUuid}/{variantNum}/identifiables/{identifiableId}/extensions", networkUuid, variantNum, identifiableId);
908+
}
909+
910+
@Override
911+
public Map<String, Map<String, ExtensionAttributes>> getAllExtensionsAttributesByResourceType(UUID networkUuid, int variantNum, ResourceType resourceType) {
912+
return getExtensionAttributesNestedMap("/networks/{networkUuid}/{variantNum}/types/{resourceType}/extensions", networkUuid, variantNum, resourceType);
913+
}
914+
915+
@Override
916+
public void removeExtensionAttributes(UUID networkUuid, int variantNum, ResourceType resourceType, String identifiableId, String extensionName) {
917+
restClient.delete("/networks/{networkUuid}/{variantNum}/identifiables/{identifiableId}/extensions/{extensionName}", networkUuid, variantNum, identifiableId, extensionName);
918+
}
851919
}

0 commit comments

Comments
 (0)