Skip to content

Commit

Permalink
refactor: simplify ThreadSafeAcess
Browse files Browse the repository at this point in the history
  • Loading branch information
mateusmolina-iese committed Dec 4, 2024
1 parent 89964fb commit bde3ea0
Show file tree
Hide file tree
Showing 8 changed files with 82 additions and 183 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,22 +50,22 @@ public ThreadSafeAasDiscovery(AasDiscoveryService decoratedAasDiscovery) {

@Override
public CursorResult<List<String>> getAllAssetAdministrationShellIdsByAssetLink(PaginationInfo pInfo, List<AssetLink> assetIds) {
return access.read(decoratedAasDiscovery::getAllAssetAdministrationShellIdsByAssetLink, pInfo, assetIds);
return access.read(() -> decoratedAasDiscovery.getAllAssetAdministrationShellIdsByAssetLink(pInfo, assetIds));
}

@Override
public List<SpecificAssetId> getAllAssetLinksById(String shellIdentifier) {
return access.read(decoratedAasDiscovery::getAllAssetLinksById, shellIdentifier);
return access.read(() -> decoratedAasDiscovery.getAllAssetLinksById(shellIdentifier));
}

@Override
public List<SpecificAssetId> createAllAssetLinksById(String shellIdentifier, List<SpecificAssetId> assetIds) {
return access.write(decoratedAasDiscovery::createAllAssetLinksById, shellIdentifier, assetIds);
return access.write(() -> decoratedAasDiscovery.createAllAssetLinksById(shellIdentifier, assetIds));
}

@Override
public void deleteAllAssetLinksById(String shellIdentifier) {
access.write(decoratedAasDiscovery::deleteAllAssetLinksById, shellIdentifier);
access.write(() -> decoratedAasDiscovery.deleteAllAssetLinksById(shellIdentifier));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,37 +51,37 @@ public class ThreadSafeAasRegistryStorageDecorator implements AasRegistryStorage

@Override
public CursorResult<List<AssetAdministrationShellDescriptor>> getAllAasDescriptors(@NonNull PaginationInfo pRequest, @NonNull DescriptorFilter filter) {
return access.read(storage::getAllAasDescriptors, pRequest, filter);
return access.read(() -> storage.getAllAasDescriptors(pRequest, filter));
}

@Override
public void removeAasDescriptor(@NonNull String aasDescriptorId) {
access.write(storage::removeAasDescriptor, aasDescriptorId);
access.write(() -> storage.removeAasDescriptor(aasDescriptorId));
}

@Override
public AssetAdministrationShellDescriptor getAasDescriptor(@NonNull String aasDescriptorId) throws AasDescriptorNotFoundException {
return access.read(storage::getAasDescriptor, aasDescriptorId);
return access.read(() -> storage.getAasDescriptor(aasDescriptorId));
}

@Override
public CursorResult<List<SubmodelDescriptor>> getAllSubmodels(@NonNull String aasDescriptorId, @NonNull PaginationInfo pRequest) throws AasDescriptorNotFoundException {
return access.read(storage::getAllSubmodels, aasDescriptorId, pRequest);
return access.read(() -> storage.getAllSubmodels(aasDescriptorId, pRequest));
}

@Override
public SubmodelDescriptor getSubmodel(@NonNull String aasDescriptorId, @NonNull String submodelId) {
return access.read(storage::getSubmodel, aasDescriptorId, submodelId);
return access.read(() -> storage.getSubmodel(aasDescriptorId, submodelId));
}

@Override
public void insertSubmodel(@NonNull String aasDescriptorId, @NonNull SubmodelDescriptor submodel) {
access.write(storage::insertSubmodel, aasDescriptorId, submodel);
access.write(() -> storage.insertSubmodel(aasDescriptorId, submodel));
}

@Override
public void removeSubmodel(@NonNull String aasDescrId, @NonNull String submodelId) {
access.write(storage::removeSubmodel, aasDescrId, submodelId);
access.write(() -> storage.removeSubmodel(aasDescrId, submodelId));
}

@Override
Expand All @@ -91,21 +91,21 @@ public Set<String> clear() {

@Override
public ShellDescriptorSearchResponse searchAasDescriptors(ShellDescriptorSearchRequest request) {
return access.read(storage::searchAasDescriptors, request);
return access.read(() -> storage.searchAasDescriptors(request));
}

@Override
public void insertAasDescriptor(@Valid AssetAdministrationShellDescriptor descr) throws AasDescriptorAlreadyExistsException {
access.write(storage::insertAasDescriptor, descr);
access.write(() -> storage.insertAasDescriptor(descr));
}

@Override
public void replaceAasDescriptor(@NonNull String aasDescritorId, @NonNull AssetAdministrationShellDescriptor descriptor) throws AasDescriptorNotFoundException {
access.write(storage::replaceAasDescriptor, aasDescritorId, descriptor);
access.write(() -> storage.replaceAasDescriptor(aasDescritorId, descriptor));
}

@Override
public void replaceSubmodel(@NonNull String aasDescriptorId, @NonNull String submodelId, @NonNull SubmodelDescriptor submodel) throws AasDescriptorNotFoundException, SubmodelNotFoundException {
access.write(storage::replaceSubmodel, aasDescriptorId, submodelId, submodel);
access.write(() -> storage.replaceSubmodel(aasDescriptorId, submodelId, submodel));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,67 +56,67 @@ public ThreadSafeAasRepository(AasRepository decoratedRepository) {

@Override
public CursorResult<List<AssetAdministrationShell>> getAllAas(PaginationInfo pInfo) {
return access.read(decoratedAasRepository::getAllAas, pInfo);
return access.read(() -> decoratedAasRepository.getAllAas(pInfo));
}

@Override
public AssetAdministrationShell getAas(String aasId) throws ElementDoesNotExistException {
return access.read(decoratedAasRepository::getAas, aasId);
return access.read(() -> decoratedAasRepository.getAas(aasId));
}

@Override
public void createAas(AssetAdministrationShell aas) throws CollidingIdentifierException, MissingIdentifierException {
access.write(decoratedAasRepository::createAas, aas);
access.write(() -> decoratedAasRepository.createAas(aas));
}

@Override
public void deleteAas(String aasId) {
access.write(decoratedAasRepository::deleteAas, aasId);
access.write(() -> decoratedAasRepository.deleteAas(aasId));
}

@Override
public void updateAas(String aasId, AssetAdministrationShell aas) {
access.write(decoratedAasRepository::updateAas, aasId, aas);
access.write(() -> decoratedAasRepository.updateAas(aasId, aas));
}

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

@Override
public void addSubmodelReference(String aasId, Reference submodelReference) {
access.write(decoratedAasRepository::addSubmodelReference, aasId, submodelReference);
access.write(() -> decoratedAasRepository.addSubmodelReference(aasId, submodelReference));
}

@Override
public void removeSubmodelReference(String aasId, String submodelId) {
access.write(decoratedAasRepository::removeSubmodelReference, aasId, submodelId);
access.write(() -> decoratedAasRepository.removeSubmodelReference(aasId, submodelId));
}

@Override
public void setAssetInformation(String aasId, AssetInformation aasInfo) throws ElementDoesNotExistException {
access.write(decoratedAasRepository::setAssetInformation, aasId, aasInfo);
access.write(() -> decoratedAasRepository.setAssetInformation(aasId, aasInfo));
}

@Override
public AssetInformation getAssetInformation(String aasId) throws ElementDoesNotExistException {
return access.read(decoratedAasRepository::getAssetInformation, aasId);
return access.read(() -> decoratedAasRepository.getAssetInformation(aasId));
}

@Override
public File getThumbnail(String aasId) {
return access.read(decoratedAasRepository::getThumbnail, aasId);
return access.read(() -> decoratedAasRepository.getThumbnail(aasId));
}

@Override
public void setThumbnail(String aasId, String fileName, String contentType, InputStream inputStream) {
access.write(decoratedAasRepository::setThumbnail, aasId, fileName, contentType, inputStream);
access.write(() -> decoratedAasRepository.setThumbnail(aasId, fileName, contentType, inputStream));
}

@Override
public void deleteThumbnail(String aasId) {
access.write(decoratedAasRepository::deleteThumbnail, aasId);
access.write(() -> decoratedAasRepository.deleteThumbnail(aasId));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,41 +43,41 @@
*/
public class ThreadSafeAASXFileServer implements AASXFileServer {

private final AASXFileServer decoreatedAasxFileServer;
private final AASXFileServer decoratedAasxFileServer;
private final ThreadSafeAccess access = new ThreadSafeAccess();

public ThreadSafeAASXFileServer(AASXFileServer aasxFileServer) {
this.decoreatedAasxFileServer = aasxFileServer;
this.decoratedAasxFileServer = aasxFileServer;
}

@Override
public CursorResult<List<PackageDescription>> getAllAASXPackageIds(String shellId, PaginationInfo pInfo) {
return access.read(decoreatedAasxFileServer::getAllAASXPackageIds, shellId, pInfo);
return access.read(() -> decoratedAasxFileServer.getAllAASXPackageIds(shellId, pInfo));
}

@Override
public InputStream getAASXByPackageId(String packageId) throws ElementDoesNotExistException {
return access.read(decoreatedAasxFileServer::getAASXByPackageId, packageId);
return access.read(() -> decoratedAasxFileServer.getAASXByPackageId(packageId));
}

@Override
public void updateAASXByPackageId(String packageId, List<String> shellIds, InputStream file, String filename) throws ElementDoesNotExistException {
access.write(decoreatedAasxFileServer::updateAASXByPackageId, packageId, shellIds, file, filename);
access.write(() -> decoratedAasxFileServer.updateAASXByPackageId(packageId, shellIds, file, filename));
}

@Override
public PackageDescription createAASXPackage(List<String> shellIds, InputStream file, String filename) {
return access.write(decoreatedAasxFileServer::createAASXPackage, shellIds, file, filename);
return access.write(() -> decoratedAasxFileServer.createAASXPackage(shellIds, file, filename));
}

@Override
public void deleteAASXByPackageId(String packageId) throws ElementDoesNotExistException {
access.write(decoreatedAasxFileServer::deleteAASXByPackageId, packageId);
access.write(() -> decoratedAasxFileServer.deleteAASXByPackageId(packageId));
}

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

}
Original file line number Diff line number Diff line change
Expand Up @@ -26,63 +26,33 @@

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock.ReadLock;
import java.util.concurrent.locks.ReentrantReadWriteLock.WriteLock;
import java.util.function.BiConsumer;
import java.util.function.BiFunction;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Supplier;

/**
* Utility class for thread-safe access
*
* @author Gerhard Sonnenberg DFKI GmbH
* @author Gerhard Sonnenberg DFKI GmbH, mateusmolina
*/
public class ThreadSafeAccess {

private final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
private final ReadLock readLock = lock.readLock();
private final WriteLock writeLock = lock.writeLock();
private final Lock readLock = lock.readLock();
private final Lock writeLock = lock.writeLock();

public <T> T write(Supplier<T> supplier) {
return runWithLock(supplier, writeLock);
}

public <A> void write(Consumer<A> consumer, A arg1) {
runWithLock(consumer, arg1, readLock);
}

public <A, B> void write(BiConsumer<A, B> consumer, A arg1, B arg2) {
runWithLock(consumer, arg1, arg2, writeLock);
}

public <A, B, T> T write(BiFunction<A, B, T> function, A arg1, B arg2) {
return runWithLock(function, arg1, arg2, writeLock);
}
public <A, B, C> void write(TriConsumer<A, B, C> consumer, A arg1, B arg2, C arg3) {
runWithLock(consumer, arg1, arg2, arg3, writeLock);
}

public <A, B, C, T> T write(TriFunction<A, B, C, T> function, A arg1, B arg2, C arg3) {
return runWithLock(function, 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);
public void write(Runnable action) {
runWithLock(action, writeLock);
}

public <A, B, T> T read(BiFunction<A, B, T> func, A arg1, B arg2) {
return runWithLock(func, arg1, arg2, readLock);
public <T> T read(Supplier<T> supplier) {
return runWithLock(supplier, readLock);
}

public <A, B, C, T> T read(TriFunction<A, B, C, T> func, A arg1, B arg2, C arg3) {
return runWithLock(func, arg1, arg2, arg3, readLock);
public void read(Runnable action) {
runWithLock(action, readLock);
}

private <T> T runWithLock(Supplier<T> supplier, Lock lock) {
try {
lock.lock();
Expand All @@ -92,83 +62,12 @@ private <T> T runWithLock(Supplier<T> supplier, Lock lock) {
}
}

private <A> void runWithLock(Consumer<A> consumer, A arg1, Lock lock) {
try {
lock.lock();
consumer.accept(arg1);
} finally {
lock.unlock();
}
}

private <T, A> T runWithLock(Function<A, T> func, A arg1, Lock lock) {
try {
lock.lock();
return func.apply(arg1);
} finally {
lock.unlock();
}
}

private <A, B, T> T runWithLock(BiFunction<A, B, T> func, A arg1, B arg2, Lock lock) {
try {
lock.lock();
return func.apply(arg1, arg2);
} finally {
lock.unlock();
}
}

private <A, B, C, T> T runWithLock(TriFunction<A, B, C, T> func, A arg1, B arg2, C arg3, Lock lock) {
try {
lock.lock();
return func.apply(arg1, arg2, arg3);
} finally {
lock.unlock();
}
}

private <A, B> void runWithLock(BiConsumer<A, B> consumer, A arg1, B arg2, Lock lock) {
try {
lock.lock();
consumer.accept(arg1, arg2);
} finally {
lock.unlock();
}
}

private <A, B, C> void runWithLock(TriConsumer<A, B, C> consumer, A arg1, B arg2, C arg3, Lock lock) {
private void runWithLock(Runnable action, Lock lock) {
try {
lock.lock();
consumer.accept(arg1, arg2, arg3);
action.run();
} finally {
lock.unlock();
}
}

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);
}

@FunctionalInterface
public static interface TriFunction<S, T, U, R> {
R apply(S s, T t, U u);
}
}
Loading

0 comments on commit bde3ea0

Please sign in to comment.