Skip to content

Commit

Permalink
refactor: use ThreadSafeAccess in ThreadSafeAasRepository
Browse files Browse the repository at this point in the history
  • Loading branch information
mateusmolina-iese committed Dec 4, 2024
1 parent 03d8723 commit 36634eb
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 80 deletions.
4 changes: 4 additions & 0 deletions basyx.aasrepository/basyx.aasrepository-backend/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@
<artifactId>basyx.aasrepository-backend</artifactId>

<dependencies>
<dependency>
<groupId>org.eclipse.digitaltwin.basyx</groupId>
<artifactId>basyx.backend</artifactId>
</dependency>
<dependency>
<groupId>org.eclipse.digitaltwin.basyx</groupId>
<artifactId>basyx.aasrepository-core</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
import java.io.File;
import java.io.InputStream;
import java.util.List;
import java.util.concurrent.locks.ReentrantReadWriteLock;

import org.eclipse.digitaltwin.aas4j.v3.model.AssetAdministrationShell;
import org.eclipse.digitaltwin.aas4j.v3.model.AssetInformation;
import org.eclipse.digitaltwin.aas4j.v3.model.Reference;
import org.eclipse.digitaltwin.basyx.aasrepository.AasRepository;
import org.eclipse.digitaltwin.basyx.common.backend.ThreadSafeAccess;
import org.eclipse.digitaltwin.basyx.core.exceptions.CollidingIdentifierException;
import org.eclipse.digitaltwin.basyx.core.exceptions.ElementDoesNotExistException;
import org.eclipse.digitaltwin.basyx.core.exceptions.MissingIdentifierException;
Expand All @@ -18,139 +18,80 @@
public class ThreadSafeAasRepository implements AasRepository {

private final AasRepository decoratedAasRepository;
private final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
private final ThreadSafeAccess access = new ThreadSafeAccess();

public ThreadSafeAasRepository(AasRepository decoratedRepository) {
this.decoratedAasRepository = decoratedRepository;
}

@Override
public CursorResult<List<AssetAdministrationShell>> getAllAas(PaginationInfo pInfo) {
lock.readLock().lock();
try {
return decoratedAasRepository.getAllAas(pInfo);
} finally {
lock.readLock().unlock();
}
return access.read(decoratedAasRepository::getAllAas, pInfo);
}

@Override
public AssetAdministrationShell getAas(String aasId) throws ElementDoesNotExistException {
lock.readLock().lock();
try {
return decoratedAasRepository.getAas(aasId);
} finally {
lock.readLock().unlock();
}
return access.read(decoratedAasRepository::getAas, aasId);
}

@Override
public void createAas(AssetAdministrationShell aas) throws CollidingIdentifierException, MissingIdentifierException {
lock.writeLock().lock();
try {
decoratedAasRepository.createAas(aas);
} finally {
lock.writeLock().unlock();
}
access.write(decoratedAasRepository::createAas, aas);
}

@Override
public void deleteAas(String aasId) {
lock.writeLock().lock();
try {
decoratedAasRepository.deleteAas(aasId);
} finally {
lock.writeLock().unlock();
}
access.write(decoratedAasRepository::deleteAas, aasId);
}

@Override
public void updateAas(String aasId, AssetAdministrationShell aas) {
lock.writeLock().lock();
try {
decoratedAasRepository.updateAas(aasId, aas);
} finally {
lock.writeLock().unlock();
}
access.write(decoratedAasRepository::updateAas, aasId, aas);
}

@Override
public CursorResult<List<Reference>> getSubmodelReferences(String aasId, PaginationInfo pInfo) {
lock.readLock().lock();
try {
return decoratedAasRepository.getSubmodelReferences(aasId, pInfo);
} finally {
lock.readLock().unlock();
}
return access.read(decoratedAasRepository::getSubmodelReferences, aasId, pInfo);
}

@Override
public void addSubmodelReference(String aasId, Reference submodelReference) {
lock.writeLock().lock();
try {
decoratedAasRepository.addSubmodelReference(aasId, submodelReference);
} finally {
lock.writeLock().unlock();
}
access.write(decoratedAasRepository::addSubmodelReference, aasId, submodelReference);
}

@Override
public void removeSubmodelReference(String aasId, String submodelId) {
lock.writeLock().lock();
try {
decoratedAasRepository.removeSubmodelReference(aasId, submodelId);
} finally {
lock.writeLock().unlock();
}
access.write(decoratedAasRepository::removeSubmodelReference, aasId, submodelId);
}

@Override
public void setAssetInformation(String aasId, AssetInformation aasInfo) throws ElementDoesNotExistException {
lock.writeLock().lock();
try {
decoratedAasRepository.setAssetInformation(aasId, aasInfo);
} finally {
lock.writeLock().unlock();
}
access.write(decoratedAasRepository::setAssetInformation, aasId, aasInfo);
}

@Override
public AssetInformation getAssetInformation(String aasId) throws ElementDoesNotExistException {
lock.readLock().lock();
try {
return decoratedAasRepository.getAssetInformation(aasId);
} finally {
lock.readLock().unlock();
}
return access.read(decoratedAasRepository::getAssetInformation, aasId);
}

@Override
public File getThumbnail(String aasId) {
lock.readLock().lock();
try {
return decoratedAasRepository.getThumbnail(aasId);
} finally {
lock.readLock().unlock();
}
return access.read(decoratedAasRepository::getThumbnail, aasId);
}

@Override
public void setThumbnail(String aasId, String fileName, String contentType, InputStream inputStream) {
lock.writeLock().lock();
try {
decoratedAasRepository.setThumbnail(aasId, fileName, contentType, inputStream);
} finally {
lock.writeLock().unlock();
}
access.write(decoratedAasRepository::setThumbnail, aasId, fileName, contentType, inputStream);
}

@Override
public void deleteThumbnail(String aasId) {
lock.writeLock().lock();
try {
decoratedAasRepository.deleteThumbnail(aasId);
} finally {
lock.writeLock().unlock();
}
access.write(decoratedAasRepository::deleteThumbnail, aasId);
}

@Override
public String getName() {
return decoratedAasRepository.getName();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@ public <A, B, C> void write(TriConsumer<A, B, C> consumer, A arg1, B arg2, C arg
runWithLock(consumer, arg1, arg2, arg3, writeLock);
}

public <A, B, C, D> void write(TetraConsumer<A, B, C, D> consumer, A arg1, B arg2, C arg3, D arg4) {
runWithLock(consumer, arg1, arg2, arg3, arg4, writeLock);
}

public <A, T> T read(Function<A, T> func, A arg1) {
return runWithLock(func, arg1, readLock);
}
Expand Down Expand Up @@ -123,10 +127,24 @@ private <A, B, C> void runWithLock(TriConsumer<A, B, C> consumer, A arg1, B arg2
}
}

private <A, B, C, D> void runWithLock(TetraConsumer<A, B, C, D> consumer, A arg1, B arg2, C arg3, D arg4, Lock lock) {
try {
lock.lock();
consumer.accept(arg1, arg2, arg3, arg4);
} finally {
lock.unlock();
}
}

@FunctionalInterface
public static interface TriConsumer<S, T, U> {

void accept(S s, T t, U u);

}

@FunctionalInterface
public static interface TetraConsumer<S, T, U, V> {
void accept(S s, T t, U u, V v);
}
}

0 comments on commit 36634eb

Please sign in to comment.