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

[WIP] Variants multi-threads access #214

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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 @@ -130,10 +130,6 @@ public void setUp() throws IOException {
.andExpect(method(GET))
.andRespond(withSuccess(breakersJson, MediaType.APPLICATION_JSON));

server.expect(requestTo("/networks/" + networkUuid))
.andExpect(method(GET))
.andRespond(withSuccess(objectMapper.writeValueAsString(List.of(new VariantInfos(VariantManagerConstants.INITIAL_VARIANT_ID, Resource.INITIAL_VARIANT_NUM))), MediaType.APPLICATION_JSON));

// line
Resource<LineAttributes> line = Resource.lineBuilder()
.id("idLine")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,26 +27,35 @@ public abstract class AbstractIdentifiableImpl<I extends Identifiable<I>, D exte

protected final NetworkObjectIndex index;

private Resource<D> resource;
private Ref<Resource<D>> resourceRef;

protected AbstractIdentifiableImpl(NetworkObjectIndex index, Resource<D> resource) {
this.index = index;
this.resource = resource;
this.resourceRef = new SimpleRef<>(resource);
}

public void updateResource() {
index.updateResource(resource);
index.updateResource(resourceRef.get());
}

public Ref<Resource<D>> getResourceRef() {
return resourceRef;
}

public void setResourceRef(Ref<Resource<D>> resourceRef) {
this.resourceRef = resourceRef;
}

public Resource<D> getResource() {
return resource;
return resourceRef.get();
}

public void setResource(Resource<D> resource) {
this.resource = resource;
this.resourceRef.set(resource);
}

protected Resource<D> checkResource() {
Resource<D> resource = resourceRef.get();
if (resource == null) {
throw new PowsyblException("Object has been removed in current variant");
}
Expand All @@ -63,8 +72,8 @@ public String getName() {

@Override
public String getNameOrId() {
Resource<D> r = checkResource();
return r.getAttributes().getName() != null ? r.getAttributes().getName() : r.getId();
Resource<D> resource = checkResource();
return resource.getAttributes().getName() != null ? resource.getAttributes().getName() : resource.getId();
}

@Override
Expand All @@ -74,10 +83,10 @@ public Optional<String> getOptionalName() {

@Override
public Set<String> getAliases() {
Resource<D> r = checkResource();
Resource<D> resource = checkResource();
Set<String> aliases = new HashSet<>();
aliases.addAll(r.getAttributes().getAliasesWithoutType());
aliases.addAll(r.getAttributes().getAliasByType().values());
aliases.addAll(resource.getAttributes().getAliasesWithoutType());
aliases.addAll(resource.getAttributes().getAliasByType().values());
return Collections.unmodifiableSet(aliases);
}

Expand Down Expand Up @@ -111,7 +120,7 @@ public void addAlias(String alias, String aliasType) {
@Override
public void addAlias(String alias, String aliasType, boolean ensureAliasUnicity) {
Objects.requireNonNull(alias);
Resource<D> r = checkResource();
Resource<D> resource = checkResource();
String uniqueAlias = alias;
if (ensureAliasUnicity) {
uniqueAlias = Identifiables.getUniqueId(alias, getNetwork().getIndex()::contains);
Expand All @@ -120,14 +129,14 @@ public void addAlias(String alias, String aliasType, boolean ensureAliasUnicity)
return;
}

if (aliasType != null && r.getAttributes().getAliasByType().containsKey(aliasType)) {
if (aliasType != null && resource.getAttributes().getAliasByType().containsKey(aliasType)) {
throw new PowsyblException(this.getId() + " already has an alias of type " + aliasType);
}

if (aliasType != null && !aliasType.equals("")) {
r.getAttributes().getAliasByType().put(aliasType, uniqueAlias);
resource.getAttributes().getAliasByType().put(aliasType, uniqueAlias);
} else {
r.getAttributes().getAliasesWithoutType().add(uniqueAlias);
resource.getAttributes().getAliasesWithoutType().add(uniqueAlias);
}
getNetwork().addAlias(uniqueAlias, this.getId());
updateResource();
Expand All @@ -136,15 +145,15 @@ public void addAlias(String alias, String aliasType, boolean ensureAliasUnicity)
@Override
public void removeAlias(String alias) {
Objects.requireNonNull(alias);
Resource<D> r = checkResource();
Resource<D> resource = checkResource();
String type = getAliasType(alias).orElse(null);
if (type != null && !type.equals("")) {
r.getAttributes().getAliasByType().remove(type);
resource.getAttributes().getAliasByType().remove(type);
} else {
if (!r.getAttributes().getAliasesWithoutType().contains(alias)) {
if (!resource.getAttributes().getAliasesWithoutType().contains(alias)) {
throw new PowsyblException(String.format("No alias '%s' found in the network", alias));
}
r.getAttributes().getAliasesWithoutType().remove(alias);
resource.getAttributes().getAliasesWithoutType().remove(alias);
}
getNetwork().removeAlias(alias);
updateResource();
Expand All @@ -156,10 +165,10 @@ public boolean hasAliases() {
}

public Properties getProperties() {
Resource<D> r = checkResource();
Resource<D> resource = checkResource();
Properties properties = new Properties();
if (r.getAttributes().getProperties() != null) {
properties.putAll(r.getAttributes().getProperties());
if (resource.getAttributes().getProperties() != null) {
properties.putAll(resource.getAttributes().getProperties());
}
return properties;
}
Expand All @@ -180,11 +189,11 @@ public Set<String> getPropertyNames() {
}

public String setProperty(String key, String value) {
Resource<D> r = checkResource();
Map<String, String> properties = r.getAttributes().getProperties();
Resource<D> resource = checkResource();
Map<String, String> properties = resource.getAttributes().getProperties();
if (properties == null) {
properties = new HashMap<>();
r.getAttributes().setProperties(properties);
resource.getAttributes().setProperties(properties);
updateResource();
}

Expand Down Expand Up @@ -247,9 +256,9 @@ public boolean isFictitious() {
}

public void setFictitious(boolean fictitious) {
Resource<D> r = checkResource();
boolean oldValue = r.getAttributes().isFictitious();
r.getAttributes().setFictitious(fictitious);
Resource<D> resource = checkResource();
boolean oldValue = resource.getAttributes().isFictitious();
resource.getAttributes().setFictitious(fictitious);
updateResource();
index.notifyUpdate(this, "fictitious", oldValue, fictitious);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/**
* Copyright (c) 2021, RTE (http://www.rte-france.com)
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
package com.powsybl.network.store.iidm.impl;

/**
* @author Geoffroy Jamgotchian <geoffroy.jamgotchian at rte-france.com>
*/
public class MultiThreadRef<T> implements Ref<T> {

private final ThreadLocal<T> ref = new ThreadLocal<>();

public MultiThreadRef(T ref) {
this.ref.set(ref);
}

@Override
public void set(T ref) {
this.ref.set(ref);
}

@Override
public T get() {
return ref.get();
}
}
Loading