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

Generic TopLevelDocument #423

Merged
merged 30 commits into from
Sep 24, 2024
Merged
Show file tree
Hide file tree
Changes from 23 commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
96b8ad6
Lazy loading of extension attributes
antoinebhs Jul 17, 2024
8395b64
Small refacto
antoinebhs Jul 18, 2024
753d2fb
Merge branch 'main' into lazy-loading-extension-attributes
antoinebhs Jul 19, 2024
e5d30f4
Refactor the code to use the existing resource cache, improve robustn…
antoinebhs Jul 24, 2024
3df88d8
Code improvements
antoinebhs Jul 25, 2024
591b34f
Code improvements
antoinebhs Jul 26, 2024
604d8f6
Review: fix condition
antoinebhs Jul 30, 2024
10a853f
Merge branch 'main' into lazy-loading-extension-attributes
antoinebhs Jul 31, 2024
1d2c57e
Review: simplify code in PreloadingNetworkStoreClient, pass the deleg…
antoinebhs Jul 31, 2024
2be0711
Generic TopLevelDocument
antoinebhs Aug 2, 2024
e4533d2
Add exception in mock class
antoinebhs Aug 2, 2024
3638f11
Review: add comments
antoinebhs Aug 2, 2024
aefc41d
Merge branch 'main' of https://github.com/powsybl/powsybl-network-sto…
antoinebhs Aug 30, 2024
02634f5
Merge branch 'main' into lazy-loading-extension-attributes
antoinebhs Sep 2, 2024
8c0d9d7
Avoid using root-level generics to avoid serialization issues with po…
antoinebhs Sep 2, 2024
1fba1e2
Merge branch 'lazy-loading-extension-attributes' of https://github.co…
antoinebhs Sep 2, 2024
a59e93e
Add tests
antoinebhs Sep 2, 2024
b272714
Replace deprecated required
antoinebhs Sep 2, 2024
1548f23
Merge branch 'main' into lazy-loading-extension-attributes
antoinebhs Sep 17, 2024
21866dd
Merge branch 'lazy-loading-extension-attributes' of https://github.co…
antoinebhs Sep 17, 2024
ddfd794
Merge branch 'main' into lazy-loading-extension-attributes
antoinebhs Sep 23, 2024
87c9eee
Merge branch 'lazy-loading-extension-attributes' of https://github.co…
antoinebhs Sep 23, 2024
de1e708
Separate methods for Resource and ExtensionAttributes
antoinebhs Sep 23, 2024
5448047
Suppress warnings for generic types in extension loaders because serv…
antoinebhs Sep 23, 2024
a370698
Review comments
antoinebhs Sep 23, 2024
ba46c90
Merge branch 'lazy-loading-extension-attributes' of https://github.co…
antoinebhs Sep 23, 2024
8ae9c88
Fix URI after review comments
antoinebhs Sep 23, 2024
4be8fe4
Merge branch 'main' into lazy-loading-extension-attributes
antoinebhs Sep 24, 2024
fc6b55f
Merge branch 'lazy-loading-extension-attributes' of https://github.co…
antoinebhs Sep 24, 2024
ebaf86f
Merge branch 'main' of https://github.com/powsybl/powsybl-network-sto…
antoinebhs Sep 24, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -871,4 +871,16 @@ public void removeConfiguredBuses(UUID networkUuid, int variantNum, List<String>
ensureCached(ResourceType.CONFIGURED_BUS, networkUuid, variantNum);
delegate.removeConfiguredBuses(networkUuid, variantNum, configuredBusesId);
}

@Override
public Optional<ExtensionAttributes> getExtensionAttributes(UUID networkUuid, int variantNum, ResourceType resourceType, String identifiableId, String extensionName) {
delegate.getAllExtensionsAttributesByResourceTypeAndExtensionName(networkUuid, variantNum, resourceType, extensionName);
return delegate.getExtensionAttributes(networkUuid, variantNum, resourceType, identifiableId, extensionName);
}

@Override
public Map<String, ExtensionAttributes> getAllExtensionsAttributesByIdentifiableId(UUID networkUuid, int variantNum, ResourceType resourceType, String id) {
delegate.getAllExtensionsAttributesByResourceType(networkUuid, variantNum, resourceType);
return delegate.getAllExtensionsAttributesByIdentifiableId(networkUuid, variantNum, resourceType, id);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
package com.powsybl.network.store.client;

import com.powsybl.network.store.model.Attributes;
import com.powsybl.network.store.model.ExtensionAttributes;
import com.powsybl.network.store.model.IdentifiableAttributes;
import com.powsybl.network.store.model.Resource;
import org.springframework.core.ParameterizedTypeReference;
Expand All @@ -23,6 +24,13 @@ public interface RestClient {

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

/**
* Retrieves one extension attributes from the server.
* @return {@link ExtensionAttributes} which is a subset of an identifiable resource. The extension attributes can be put in the extensionAttributes
* map of an {@link IdentifiableAttributes} or used to load an extension.
*/
Optional<ExtensionAttributes> getOneExtensionAttributes(String url, Object... uriVariables);

<T extends IdentifiableAttributes> List<Resource<T>> getAll(String target, String url, Object... uriVariables);

<T extends Attributes> void updateAll(String url, List<Resource<T>> resources, Object... uriVariables);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,17 +60,16 @@ private static MappingJackson2HttpMessageConverter createMapping() {
return converter;
}

private <T extends IdentifiableAttributes> ResponseEntity<TopLevelDocument<T>> getDocument(String url, Object... uriVariables) {
private <T, D extends AbstractTopLevelDocument<T>> ResponseEntity<D> getDocument(String url, ParameterizedTypeReference<D> parameterizedTypeReference, Object... uriVariables) {
return restTemplate.exchange(url,
HttpMethod.GET,
new HttpEntity<>(new HttpHeaders()),
new ParameterizedTypeReference<TopLevelDocument<T>>() { // this unnecessary type should not be removed!!! https://github.com/powsybl/powsybl-network-store/commit/9744168f47210eab11796861f9dcf4ffdd5aea0c
},
parameterizedTypeReference,
uriVariables);
}

private static <T extends IdentifiableAttributes> TopLevelDocument<T> getBody(ResponseEntity<TopLevelDocument<T>> response) {
TopLevelDocument<T> body = response.getBody();
private static <T, D extends AbstractTopLevelDocument<T>> D getBody(ResponseEntity<D> response) {
D body = response.getBody();
if (body == null) {
throw new PowsyblException("Body is null");
}
Expand All @@ -92,9 +91,20 @@ public <T extends IdentifiableAttributes> void createAll(String url, List<Resour

@Override
public <T extends IdentifiableAttributes> Optional<Resource<T>> getOne(String target, String url, Object... uriVariables) {
ResponseEntity<TopLevelDocument<T>> response = getDocument(url, uriVariables);
return getOneDocument(target, url, new ParameterizedTypeReference<TopLevelDocument<T>>() {
}, uriVariables);
}

@Override
public Optional<ExtensionAttributes> getOneExtensionAttributes(String url, Object... uriVariables) {
return getOneDocument(null, url, new ParameterizedTypeReference<ExtensionAttributesTopLevelDocument>() {
}, uriVariables);
}

public <T, D extends AbstractTopLevelDocument<T>> Optional<T> getOneDocument(String target, String url, ParameterizedTypeReference<D> parameterizedTypeReference, Object... uriVariables) {
ResponseEntity<D> response = getDocument(url, parameterizedTypeReference, uriVariables);
if (response.getStatusCode() == HttpStatus.OK) {
TopLevelDocument<T> body = getBody(response);
AbstractTopLevelDocument<T> body = getBody(response);
return Optional.of(body.getData().get(0));
} else if (response.getStatusCode() == HttpStatus.NOT_FOUND) {
return Optional.empty();
Expand All @@ -105,7 +115,8 @@ public <T extends IdentifiableAttributes> Optional<Resource<T>> getOne(String ta

@Override
public <T extends IdentifiableAttributes> List<Resource<T>> getAll(String target, String url, Object... uriVariables) {
ResponseEntity<TopLevelDocument<T>> response = getDocument(url, uriVariables);
ResponseEntity<TopLevelDocument<T>> response = getDocument(url, new ParameterizedTypeReference<>() {
}, uriVariables);
if (response.getStatusCode() != HttpStatus.OK) {
throw createHttpException(url, "get", response.getStatusCode());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,49 @@ private <T extends IdentifiableAttributes> Optional<Resource<T>> get(String targ
return resource;
}

private Optional<ExtensionAttributes> getExtensionAttributes(String urlTemplate, Object... uriVariables) {
logGetExtensionAttributesUrl(urlTemplate, uriVariables);
Stopwatch stopwatch = Stopwatch.createStarted();
Optional<ExtensionAttributes> extensionAttributes = restClient.getOneExtensionAttributes(urlTemplate, uriVariables);
stopwatch.stop();
logGetExtensionAttributesTime(extensionAttributes.isPresent() ? 1 : 0, stopwatch.elapsed(TimeUnit.MILLISECONDS));

return extensionAttributes;
}

private Map<String, ExtensionAttributes> getExtensionAttributesMap(String urlTemplate, Object... uriVariables) {
logGetExtensionAttributesUrl(urlTemplate, uriVariables);
Stopwatch stopwatch = Stopwatch.createStarted();
Map<String, ExtensionAttributes> extensionAttributes = restClient.get(urlTemplate, new ParameterizedTypeReference<>() { }, uriVariables);
stopwatch.stop();
logGetExtensionAttributesTime(extensionAttributes.size(), stopwatch.elapsed(TimeUnit.MILLISECONDS));

return extensionAttributes;
}

private Map<String, Map<String, ExtensionAttributes>> getExtensionAttributesNestedMap(String urlTemplate, Object... uriVariables) {
logGetExtensionAttributesUrl(urlTemplate, uriVariables);
Stopwatch stopwatch = Stopwatch.createStarted();
Map<String, Map<String, ExtensionAttributes>> extensionAttributes = restClient.get(urlTemplate, new ParameterizedTypeReference<>() { }, uriVariables);
stopwatch.stop();
long loadedAttributesCount = extensionAttributes.values().stream()
.mapToLong(innerMap -> innerMap.values().size())
.sum();
logGetExtensionAttributesTime(loadedAttributesCount, stopwatch.elapsed(TimeUnit.MILLISECONDS));

return extensionAttributes;
}

private static void logGetExtensionAttributesUrl(String urlTemplate, Object... uriVariables) {
if (LOGGER.isInfoEnabled()) {
LOGGER.info("Loading extension attributes {}", UriComponentsBuilder.fromUriString(urlTemplate).build(uriVariables));
}
}

private static void logGetExtensionAttributesTime(long loadedAttributesCount, long timeElapsed) {
LOGGER.info("{} extension attributes loaded in {} ms", loadedAttributesCount, timeElapsed);
}

private <T extends IdentifiableAttributes> void updatePartition(String target, String url, AttributeFilter attributeFilter, List<Resource<T>> resources, Object[] uriVariables) {
if (attributeFilter == null) {
if (LOGGER.isInfoEnabled()) {
Expand Down Expand Up @@ -848,4 +891,29 @@ public void updateGrounds(UUID networkUuid, List<Resource<GroundAttributes>> gro
AttributeFilter attributeFilter) {
updateAll(STR_GROUND, "/networks/{networkUuid}/grounds", groundResources, attributeFilter, networkUuid);
}

@Override
public Optional<ExtensionAttributes> getExtensionAttributes(UUID networkUuid, int variantNum, ResourceType resourceType, String identifiableId, String extensionName) {
return getExtensionAttributes("/networks/{networkUuid}/{variantNum}/identifiables/{identifiableId}/extensions/{extensionName}", networkUuid, variantNum, identifiableId, extensionName);
}

@Override
public Map<String, ExtensionAttributes> getAllExtensionsAttributesByResourceTypeAndExtensionName(UUID networkUuid, int variantNum, ResourceType resourceType, String extensionName) {
return getExtensionAttributesMap("/networks/{networkUuid}/{variantNum}/types/{type}/extensions/{extensionName}", networkUuid, variantNum, resourceType, extensionName);
}

@Override
public Map<String, ExtensionAttributes> getAllExtensionsAttributesByIdentifiableId(UUID networkUuid, int variantNum, ResourceType resourceType, String identifiableId) {
return getExtensionAttributesMap("/networks/{networkUuid}/{variantNum}/identifiables/{identifiableId}/extensions", networkUuid, variantNum, identifiableId);
}

@Override
public Map<String, Map<String, ExtensionAttributes>> getAllExtensionsAttributesByResourceType(UUID networkUuid, int variantNum, ResourceType resourceType) {
return getExtensionAttributesNestedMap("/networks/{networkUuid}/{variantNum}/types/{resourceType}/extensions", networkUuid, variantNum, resourceType);
}

@Override
public void removeExtensionAttributes(UUID networkUuid, int variantNum, ResourceType resourceType, String identifiableId, String extensionName) {
restClient.delete("/networks/{networkUuid}/{variantNum}/identifiables/{identifiableId}/extensions/{extensionName}", networkUuid, variantNum, identifiableId, extensionName);
}
}
Loading
Loading