From d4a4fed776d0b753b882d48e2cbd9172147e371a Mon Sep 17 00:00:00 2001 From: Mohammad Ghazanfar Ali Danish <62088117+mdanish98@users.noreply.github.com> Date: Mon, 21 Oct 2024 09:35:39 +0200 Subject: [PATCH] Refactors and move InMemoryCrud Repo implementation to common (#496) * Moves InMemory CrudRepository to inmemory core module Signed-off-by: Mohammad Ghazanfar Ali Danish * Refactors *InMemory Crud Repo to common Signed-off-by: Mohammad Ghazanfar Ali Danish * Removes NO_LIMIT_PAGINATION_INFO Signed-off-by: Mohammad Ghazanfar Ali Danish * Addresses review remarks Signed-off-by: Mohammad Ghazanfar Ali Danish --------- Signed-off-by: Mohammad Ghazanfar Ali Danish --- .../pom.xml | 7 +- .../AasDiscoveryInMemoryBackendProvider.java | 3 +- .../AasDiscoveryInMemoryCrudRepository.java | 138 ------------------ .../pom.xml | 4 + .../backend/inmemory/AasInMemoryBackend.java | 117 --------------- .../inmemory/AasInMemoryBackendProvider.java | 5 +- .../pom.xml | 4 + ...AASXFileServerInMemoryBackendProvider.java | 3 +- .../AASXFileServerInMemoryCrudRepository.java | 110 -------------- .../backend/CrudAASXFileServer.java | 2 - .../basyx.backend.inmemory.core/pom.xml | 26 ++++ .../inmemory/core/InMemoryCrudRepository.java | 49 ++++--- basyx.common/pom.xml | 1 + .../pom.xml | 4 + .../ConceptDescriptionInMemoryBackend.java | 116 --------------- ...eptDescriptionInMemoryBackendProvider.java | 3 +- .../pom.xml | 4 + .../SubmodelInMemoryBackendProvider.java | 3 +- .../core/SubmodelRepositorySuite.java | 3 +- pom.xml | 5 + 20 files changed, 94 insertions(+), 513 deletions(-) delete mode 100644 basyx.aasdiscoveryservice/basyx.aasdiscoveryservice-backend-inmemory/src/main/java/org/eclipse/digitaltwin/basyx/aasdiscoveryservice/backend/inmemory/AasDiscoveryInMemoryCrudRepository.java delete mode 100644 basyx.aasrepository/basyx.aasrepository-backend-inmemory/src/main/java/org/eclipse/digitaltwin/basyx/aasrepository/backend/inmemory/AasInMemoryBackend.java delete mode 100644 basyx.aasxfileserver/basyx.aasxfileserver-backend-inmemory/src/main/java/org/eclipse/digitaltwin/basyx/aasxfileserver/AASXFileServerInMemoryCrudRepository.java create mode 100644 basyx.common/basyx.backend.inmemory.core/pom.xml rename basyx.submodelrepository/basyx.submodelrepository-backend-inmemory/src/main/java/org/eclipse/digitaltwin/basyx/submodelrepository/SubmodelInMemoryBackend.java => basyx.common/basyx.backend.inmemory.core/src/main/java/org/eclipse/digitaltwin/basyx/common/backend/inmemory/core/InMemoryCrudRepository.java (69%) delete mode 100644 basyx.conceptdescriptionrepository/basyx.conceptdescriptionrepository-backend-inmemory/src/main/java/org/eclipse/digitaltwin/basyx/conceptdescriptionrepository/ConceptDescriptionInMemoryBackend.java diff --git a/basyx.aasdiscoveryservice/basyx.aasdiscoveryservice-backend-inmemory/pom.xml b/basyx.aasdiscoveryservice/basyx.aasdiscoveryservice-backend-inmemory/pom.xml index 7d7849257..62201b56d 100644 --- a/basyx.aasdiscoveryservice/basyx.aasdiscoveryservice-backend-inmemory/pom.xml +++ b/basyx.aasdiscoveryservice/basyx.aasdiscoveryservice-backend-inmemory/pom.xml @@ -19,8 +19,11 @@ org.eclipse.digitaltwin.basyx basyx.aasdiscoveryservice-backend - - + + + org.eclipse.digitaltwin.basyx + basyx.backend.inmemory.core + org.eclipse.digitaltwin.basyx basyx.aasdiscoveryservice-core diff --git a/basyx.aasdiscoveryservice/basyx.aasdiscoveryservice-backend-inmemory/src/main/java/org/eclipse/digitaltwin/basyx/aasdiscoveryservice/backend/inmemory/AasDiscoveryInMemoryBackendProvider.java b/basyx.aasdiscoveryservice/basyx.aasdiscoveryservice-backend-inmemory/src/main/java/org/eclipse/digitaltwin/basyx/aasdiscoveryservice/backend/inmemory/AasDiscoveryInMemoryBackendProvider.java index 46971ba9c..21720e822 100644 --- a/basyx.aasdiscoveryservice/basyx.aasdiscoveryservice-backend-inmemory/src/main/java/org/eclipse/digitaltwin/basyx/aasdiscoveryservice/backend/inmemory/AasDiscoveryInMemoryBackendProvider.java +++ b/basyx.aasdiscoveryservice/basyx.aasdiscoveryservice-backend-inmemory/src/main/java/org/eclipse/digitaltwin/basyx/aasdiscoveryservice/backend/inmemory/AasDiscoveryInMemoryBackendProvider.java @@ -27,6 +27,7 @@ import org.eclipse.digitaltwin.basyx.aasdiscoveryservice.backend.AasDiscoveryBackendProvider; import org.eclipse.digitaltwin.basyx.aasdiscoveryservice.backend.AasDiscoveryDocument; +import org.eclipse.digitaltwin.basyx.common.backend.inmemory.core.InMemoryCrudRepository; import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression; import org.springframework.data.repository.CrudRepository; import org.springframework.stereotype.Component; @@ -41,7 +42,7 @@ @Component public class AasDiscoveryInMemoryBackendProvider implements AasDiscoveryBackendProvider { - private AasDiscoveryInMemoryCrudRepository repository = new AasDiscoveryInMemoryCrudRepository(); + private CrudRepository repository = new InMemoryCrudRepository(AasDiscoveryDocument::getShellIdentifier); @Override public CrudRepository getCrudRepository() { diff --git a/basyx.aasdiscoveryservice/basyx.aasdiscoveryservice-backend-inmemory/src/main/java/org/eclipse/digitaltwin/basyx/aasdiscoveryservice/backend/inmemory/AasDiscoveryInMemoryCrudRepository.java b/basyx.aasdiscoveryservice/basyx.aasdiscoveryservice-backend-inmemory/src/main/java/org/eclipse/digitaltwin/basyx/aasdiscoveryservice/backend/inmemory/AasDiscoveryInMemoryCrudRepository.java deleted file mode 100644 index 7fdf732c7..000000000 --- a/basyx.aasdiscoveryservice/basyx.aasdiscoveryservice-backend-inmemory/src/main/java/org/eclipse/digitaltwin/basyx/aasdiscoveryservice/backend/inmemory/AasDiscoveryInMemoryCrudRepository.java +++ /dev/null @@ -1,138 +0,0 @@ -/******************************************************************************* - * Copyright (C) 2024 the Eclipse BaSyx Authors - * - * Permission is hereby granted, free of charge, to any person obtaining - * a copy of this software and associated documentation files (the - * "Software"), to deal in the Software without restriction, including - * without limitation the rights to use, copy, modify, merge, publish, - * distribute, sublicense, and/or sell copies of the Software, and to - * permit persons to whom the Software is furnished to do so, subject to - * the following conditions: - * - * The above copyright notice and this permission notice shall be - * included in all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, - * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF - * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND - * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE - * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION - * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION - * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. - * - * SPDX-License-Identifier: MIT - ******************************************************************************/ - -package org.eclipse.digitaltwin.basyx.aasdiscoveryservice.backend.inmemory; - -import java.util.ArrayList; -import java.util.HashSet; -import java.util.List; -import java.util.Optional; -import java.util.Set; -import java.util.concurrent.ConcurrentHashMap; -import java.util.concurrent.ConcurrentMap; -import java.util.stream.StreamSupport; - -import org.eclipse.digitaltwin.aas4j.v3.model.SpecificAssetId; -import org.eclipse.digitaltwin.basyx.aasdiscoveryservice.backend.AasDiscoveryDocument; -import org.eclipse.digitaltwin.basyx.aasdiscoveryservice.core.model.AssetLink; -import org.springframework.data.repository.CrudRepository; -import org.springframework.lang.NonNull; - -/** - * In-memory implementation of the {@link CrudRepository} for the AAS Discovery - * - * @author zielstor, fried, mateusmolina - */ -public class AasDiscoveryInMemoryCrudRepository implements CrudRepository { - - private final ConcurrentMap> assetLinks = new ConcurrentHashMap<>(); - private final ConcurrentMap> assetIds = new ConcurrentHashMap<>(); - - @Override - public synchronized @NonNull S save(@NonNull S entity) { - String shellId = entity.getShellIdentifier(); - - this.assetLinks.put(shellId, entity.getAssetLinks()); - this.assetIds.put(shellId, entity.getSpecificAssetIds()); - - return entity; - } - - @Override - public @NonNull Iterable saveAll(@NonNull Iterable entities) { - entities.forEach(this::save); - return entities; - } - - @Override - public @NonNull Optional findById(@NonNull String id) { - return Optional.ofNullable(buildAasDiscoveryDocument(id)); - } - - @Override - public boolean existsById(@NonNull String id) { - return this.assetLinks.containsKey(id); - } - - @Override - public @NonNull Iterable findAll() { - return assetLinks.keySet().stream().map(this::buildAasDiscoveryDocument).toList(); - } - - @Override - public @NonNull Iterable findAllById(@NonNull Iterable ids) { - return StreamSupport.stream(ids.spliterator(), false).map(this::buildAasDiscoveryDocument).toList(); - } - - @Override - public long count() { - return this.assetLinks.size(); - } - - @Override - public synchronized void deleteById(@NonNull String id) { - this.assetLinks.remove(id); - this.assetIds.remove(id); - } - - @Override - public void delete(@NonNull AasDiscoveryDocument entity) { - this.deleteById(entity.getShellIdentifier()); - } - - @Override - public void deleteAllById(@NonNull Iterable ids) { - for (String id : ids) { - this.deleteById(id); - } - } - - @Override - public void deleteAll(@NonNull Iterable entities) { - for (AasDiscoveryDocument entity : entities) { - this.deleteById(entity.getShellIdentifier()); - } - } - - @Override - public synchronized void deleteAll() { - this.assetLinks.clear(); - this.assetIds.clear(); - } - - private synchronized AasDiscoveryDocument buildAasDiscoveryDocument(String shellId) { - Set assetLinksSet = assetLinks.get(shellId); - List assetIdsList = assetIds.get(shellId); - - if (assetIdsList == null) - assetIdsList = new ArrayList<>(); - - if (assetLinksSet == null) - assetLinksSet = new HashSet<>(); - - return new AasDiscoveryDocument(shellId, assetLinksSet, assetIdsList); - } - -} diff --git a/basyx.aasrepository/basyx.aasrepository-backend-inmemory/pom.xml b/basyx.aasrepository/basyx.aasrepository-backend-inmemory/pom.xml index 3f6b3a9b8..45c8dc476 100644 --- a/basyx.aasrepository/basyx.aasrepository-backend-inmemory/pom.xml +++ b/basyx.aasrepository/basyx.aasrepository-backend-inmemory/pom.xml @@ -22,6 +22,10 @@ org.eclipse.digitaltwin.basyx basyx.aasrepository-backend + + org.eclipse.digitaltwin.basyx + basyx.backend.inmemory.core + org.eclipse.digitaltwin.basyx basyx.aasrepository-core diff --git a/basyx.aasrepository/basyx.aasrepository-backend-inmemory/src/main/java/org/eclipse/digitaltwin/basyx/aasrepository/backend/inmemory/AasInMemoryBackend.java b/basyx.aasrepository/basyx.aasrepository-backend-inmemory/src/main/java/org/eclipse/digitaltwin/basyx/aasrepository/backend/inmemory/AasInMemoryBackend.java deleted file mode 100644 index aeb02fd99..000000000 --- a/basyx.aasrepository/basyx.aasrepository-backend-inmemory/src/main/java/org/eclipse/digitaltwin/basyx/aasrepository/backend/inmemory/AasInMemoryBackend.java +++ /dev/null @@ -1,117 +0,0 @@ -/******************************************************************************* - * Copyright (C) 2023 the Eclipse BaSyx Authors - * - * Permission is hereby granted, free of charge, to any person obtaining - * a copy of this software and associated documentation files (the - * "Software"), to deal in the Software without restriction, including - * without limitation the rights to use, copy, modify, merge, publish, - * distribute, sublicense, and/or sell copies of the Software, and to - * permit persons to whom the Software is furnished to do so, subject to - * the following conditions: - * - * The above copyright notice and this permission notice shall be - * included in all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, - * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF - * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND - * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE - * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION - * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION - * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. - * - * SPDX-License-Identifier: MIT - ******************************************************************************/ - -package org.eclipse.digitaltwin.basyx.aasrepository.backend.inmemory; - -import java.util.Objects; -import java.util.Optional; -import java.util.concurrent.ConcurrentHashMap; -import java.util.concurrent.ConcurrentMap; -import java.util.stream.Collectors; -import java.util.stream.StreamSupport; - -import org.eclipse.digitaltwin.aas4j.v3.model.AssetAdministrationShell; -import org.springframework.data.repository.CrudRepository; -import org.springframework.lang.NonNull; - -/** - * InMemory implementation for the AAS backend - * - * @author mateusmolina - * - */ -public class AasInMemoryBackend implements CrudRepository { - - private final ConcurrentMap inMemoryStore = new ConcurrentHashMap<>(); - - @Override - public @NonNull S save(@NonNull S entity) { - inMemoryStore.put(entity.getId(), entity); - - return entity; - } - - @Override - public @NonNull Iterable saveAll(@NonNull Iterable entities) { - entities.forEach(this::save); - - return entities; - } - - @Override - public @NonNull Optional findById(@NonNull String id) { - return Optional.ofNullable(inMemoryStore.get(id)); - } - - @Override - public boolean existsById(@NonNull String id) { - return inMemoryStore.containsKey(id); - } - - @Override - public @NonNull Iterable findAll() { - return inMemoryStore.values(); - } - - @Override - public @NonNull Iterable findAllById(@NonNull Iterable ids) { - return StreamSupport.stream(ids.spliterator(), false).map(inMemoryStore::get).filter(Objects::nonNull).collect(Collectors.toList()); - } - - @Override - public long count() { - return inMemoryStore.size(); - } - - @Override - public void deleteById(@NonNull String id) { - inMemoryStore.remove(id); - } - - @Override - public void delete(@NonNull AssetAdministrationShell entity) { - inMemoryStore.remove(entity.getId()); - } - - @Override - public void deleteAllById(@NonNull Iterable ids) { - for (String id : ids) - inMemoryStore.remove(id); - } - - @Override - public void deleteAll(@NonNull Iterable entities) { - for (AssetAdministrationShell entity : entities) - inMemoryStore.remove(entity.getId()); - } - - @Override - public void deleteAll() { - inMemoryStore.clear(); - } - - -} - diff --git a/basyx.aasrepository/basyx.aasrepository-backend-inmemory/src/main/java/org/eclipse/digitaltwin/basyx/aasrepository/backend/inmemory/AasInMemoryBackendProvider.java b/basyx.aasrepository/basyx.aasrepository-backend-inmemory/src/main/java/org/eclipse/digitaltwin/basyx/aasrepository/backend/inmemory/AasInMemoryBackendProvider.java index 0a8a4e788..44860711d 100644 --- a/basyx.aasrepository/basyx.aasrepository-backend-inmemory/src/main/java/org/eclipse/digitaltwin/basyx/aasrepository/backend/inmemory/AasInMemoryBackendProvider.java +++ b/basyx.aasrepository/basyx.aasrepository-backend-inmemory/src/main/java/org/eclipse/digitaltwin/basyx/aasrepository/backend/inmemory/AasInMemoryBackendProvider.java @@ -27,6 +27,7 @@ import org.eclipse.digitaltwin.aas4j.v3.model.AssetAdministrationShell; import org.eclipse.digitaltwin.basyx.aasrepository.backend.AasBackendProvider; +import org.eclipse.digitaltwin.basyx.common.backend.inmemory.core.InMemoryCrudRepository; import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression; import org.springframework.data.repository.CrudRepository; import org.springframework.stereotype.Component; @@ -35,7 +36,7 @@ * * InMemory backend provider for the AAS * - * @author mateusmolina + * @author mateusmolina, danish */ @ConditionalOnExpression("'${basyx.backend}'.equals('InMemory')") @Component @@ -43,7 +44,7 @@ public class AasInMemoryBackendProvider implements AasBackendProvider { @Override public CrudRepository getCrudRepository() { - return new AasInMemoryBackend(); + return new InMemoryCrudRepository(AssetAdministrationShell::getId); } } diff --git a/basyx.aasxfileserver/basyx.aasxfileserver-backend-inmemory/pom.xml b/basyx.aasxfileserver/basyx.aasxfileserver-backend-inmemory/pom.xml index 79e8704d8..f601be551 100644 --- a/basyx.aasxfileserver/basyx.aasxfileserver-backend-inmemory/pom.xml +++ b/basyx.aasxfileserver/basyx.aasxfileserver-backend-inmemory/pom.xml @@ -17,6 +17,10 @@ org.eclipse.digitaltwin.basyx basyx.aasxfileserver-core + + org.eclipse.digitaltwin.basyx + basyx.backend.inmemory.core + org.eclipse.digitaltwin.basyx basyx.aasxfileserver-core diff --git a/basyx.aasxfileserver/basyx.aasxfileserver-backend-inmemory/src/main/java/org/eclipse/digitaltwin/basyx/aasxfileserver/AASXFileServerInMemoryBackendProvider.java b/basyx.aasxfileserver/basyx.aasxfileserver-backend-inmemory/src/main/java/org/eclipse/digitaltwin/basyx/aasxfileserver/AASXFileServerInMemoryBackendProvider.java index 68a050e98..b80b64198 100644 --- a/basyx.aasxfileserver/basyx.aasxfileserver-backend-inmemory/src/main/java/org/eclipse/digitaltwin/basyx/aasxfileserver/AASXFileServerInMemoryBackendProvider.java +++ b/basyx.aasxfileserver/basyx.aasxfileserver-backend-inmemory/src/main/java/org/eclipse/digitaltwin/basyx/aasxfileserver/AASXFileServerInMemoryBackendProvider.java @@ -27,6 +27,7 @@ import org.eclipse.digitaltwin.basyx.aasxfileserver.backend.AASXFileServerBackendProvider; import org.eclipse.digitaltwin.basyx.aasxfileserver.model.Package; +import org.eclipse.digitaltwin.basyx.common.backend.inmemory.core.InMemoryCrudRepository; import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression; import org.springframework.data.repository.CrudRepository; import org.springframework.stereotype.Component; @@ -36,7 +37,7 @@ @Component public class AASXFileServerInMemoryBackendProvider implements AASXFileServerBackendProvider { - private AASXFileServerInMemoryCrudRepository repository = new AASXFileServerInMemoryCrudRepository(); + private CrudRepository repository = new InMemoryCrudRepository(Package::getPackageId); @Override public CrudRepository getCrudRepository() { diff --git a/basyx.aasxfileserver/basyx.aasxfileserver-backend-inmemory/src/main/java/org/eclipse/digitaltwin/basyx/aasxfileserver/AASXFileServerInMemoryCrudRepository.java b/basyx.aasxfileserver/basyx.aasxfileserver-backend-inmemory/src/main/java/org/eclipse/digitaltwin/basyx/aasxfileserver/AASXFileServerInMemoryCrudRepository.java deleted file mode 100644 index f1c61802a..000000000 --- a/basyx.aasxfileserver/basyx.aasxfileserver-backend-inmemory/src/main/java/org/eclipse/digitaltwin/basyx/aasxfileserver/AASXFileServerInMemoryCrudRepository.java +++ /dev/null @@ -1,110 +0,0 @@ -/******************************************************************************* -* Copyright (C) 2024 the Eclipse BaSyx Authors -* -* Permission is hereby granted, free of charge, to any person obtaining -* a copy of this software and associated documentation files (the -* "Software"), to deal in the Software without restriction, including -* without limitation the rights to use, copy, modify, merge, publish, -* distribute, sublicense, and/or sell copies of the Software, and to -* permit persons to whom the Software is furnished to do so, subject to -* the following conditions: -* -* The above copyright notice and this permission notice shall be -* included in all copies or substantial portions of the Software. -* -* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, -* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF -* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND -* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE -* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION -* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION -* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -* -* SPDX-License-Identifier: MIT -******************************************************************************/ - -package org.eclipse.digitaltwin.basyx.aasxfileserver; - -import java.util.List; -import java.util.Map.Entry; -import java.util.Optional; -import java.util.concurrent.ConcurrentHashMap; -import java.util.concurrent.ConcurrentMap; -import java.util.stream.Collectors; -import java.util.stream.StreamSupport; - -import org.eclipse.digitaltwin.basyx.aasxfileserver.model.Package; -import org.springframework.data.repository.CrudRepository; -import org.springframework.lang.NonNull; - -public class AASXFileServerInMemoryCrudRepository implements CrudRepository { - - private final ConcurrentMap packageMap = new ConcurrentHashMap<>(); - - @Override - public @NonNull S save(@NonNull S entity) { - packageMap.put(entity.getPackageId(), entity); - - return entity; - } - - @Override - public @NonNull Iterable saveAll(@NonNull Iterable entities) { - entities.forEach(this::save); - - return entities; - } - - @Override - public @NonNull Optional findById(@NonNull String id) { - return Optional.ofNullable(packageMap.get(id)); - } - - @Override - public boolean existsById(@NonNull String id) { - return packageMap.containsKey(id); - } - - @Override - public @NonNull Iterable findAll() { - return packageMap.values(); - } - - @Override - public @NonNull Iterable findAllById(@NonNull Iterable ids) { - List idList = StreamSupport.stream(ids.spliterator(), false).collect(Collectors.toList()); - return packageMap.entrySet().stream().filter(entry -> idList.contains(entry.getKey())).map(Entry::getValue).collect(Collectors.toList()); - } - - @Override - public long count() { - return packageMap.size(); - } - - @Override - public void deleteById(@NonNull String id) { - packageMap.remove(id); - } - - @Override - public void delete(@NonNull Package entity) { - packageMap.remove(entity.getPackageId()); - } - - @Override - public void deleteAllById(@NonNull Iterable ids) { - List idList = StreamSupport.stream(ids.spliterator(), false).collect(Collectors.toList()); - packageMap.keySet().removeAll(idList); - } - - @Override - public void deleteAll(@NonNull Iterable entities) { - List idList = StreamSupport.stream(entities.spliterator(), false).map(Package::getPackageId).collect(Collectors.toList()); - packageMap.keySet().removeAll(idList); - } - - @Override - public void deleteAll() { - packageMap.clear(); - } -} diff --git a/basyx.aasxfileserver/basyx.aasxfileserver-backend/src/main/java/org/eclipse/digitaltwin/basyx/aasxfileserver/backend/CrudAASXFileServer.java b/basyx.aasxfileserver/basyx.aasxfileserver-backend/src/main/java/org/eclipse/digitaltwin/basyx/aasxfileserver/backend/CrudAASXFileServer.java index 599a28c7b..ce5211c52 100644 --- a/basyx.aasxfileserver/basyx.aasxfileserver-backend/src/main/java/org/eclipse/digitaltwin/basyx/aasxfileserver/backend/CrudAASXFileServer.java +++ b/basyx.aasxfileserver/basyx.aasxfileserver-backend/src/main/java/org/eclipse/digitaltwin/basyx/aasxfileserver/backend/CrudAASXFileServer.java @@ -25,7 +25,6 @@ package org.eclipse.digitaltwin.basyx.aasxfileserver.backend; import java.io.InputStream; -import java.util.Collection; import java.util.List; import java.util.TreeMap; import java.util.concurrent.atomic.AtomicInteger; @@ -33,7 +32,6 @@ import java.util.stream.StreamSupport; import org.eclipse.digitaltwin.aas4j.v3.model.PackageDescription; -import org.eclipse.digitaltwin.aas4j.v3.model.Submodel; import org.eclipse.digitaltwin.aas4j.v3.model.impl.DefaultPackageDescription; import org.eclipse.digitaltwin.basyx.aasxfileserver.AASXFileServer; import org.eclipse.digitaltwin.basyx.aasxfileserver.model.Package; diff --git a/basyx.common/basyx.backend.inmemory.core/pom.xml b/basyx.common/basyx.backend.inmemory.core/pom.xml new file mode 100644 index 000000000..579e9c321 --- /dev/null +++ b/basyx.common/basyx.backend.inmemory.core/pom.xml @@ -0,0 +1,26 @@ + + 4.0.0 + + org.eclipse.digitaltwin.basyx + basyx.common + ${revision} + + basyx.backend.inmemory.core + BaSyx InMemory Backend core + BaSyx InMemory Backend core + + + + org.eclipse.digitaltwin.basyx + basyx.core + + + org.springframework.data + spring-data-commons + + + org.eclipse.digitaltwin.aas4j + aas4j-dataformat-json + + + \ No newline at end of file diff --git a/basyx.submodelrepository/basyx.submodelrepository-backend-inmemory/src/main/java/org/eclipse/digitaltwin/basyx/submodelrepository/SubmodelInMemoryBackend.java b/basyx.common/basyx.backend.inmemory.core/src/main/java/org/eclipse/digitaltwin/basyx/common/backend/inmemory/core/InMemoryCrudRepository.java similarity index 69% rename from basyx.submodelrepository/basyx.submodelrepository-backend-inmemory/src/main/java/org/eclipse/digitaltwin/basyx/submodelrepository/SubmodelInMemoryBackend.java rename to basyx.common/basyx.backend.inmemory.core/src/main/java/org/eclipse/digitaltwin/basyx/common/backend/inmemory/core/InMemoryCrudRepository.java index 91afca35d..8c5267266 100644 --- a/basyx.submodelrepository/basyx.submodelrepository-backend-inmemory/src/main/java/org/eclipse/digitaltwin/basyx/submodelrepository/SubmodelInMemoryBackend.java +++ b/basyx.common/basyx.backend.inmemory.core/src/main/java/org/eclipse/digitaltwin/basyx/common/backend/inmemory/core/InMemoryCrudRepository.java @@ -23,45 +23,51 @@ * SPDX-License-Identifier: MIT ******************************************************************************/ -package org.eclipse.digitaltwin.basyx.submodelrepository; +package org.eclipse.digitaltwin.basyx.common.backend.inmemory.core; import java.util.Objects; import java.util.Optional; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentMap; +import java.util.function.Function; import java.util.stream.Collectors; import java.util.stream.StreamSupport; -import org.eclipse.digitaltwin.aas4j.v3.model.Submodel; import org.springframework.data.repository.CrudRepository; import org.springframework.lang.NonNull; /** - * InMemory implementation for the Submodel backend - * - * @author mateusmolina, danish + * CrudRepository implementation for InMemory backends * + * @author danish */ -public class SubmodelInMemoryBackend implements CrudRepository { - - private final ConcurrentMap inMemoryStore = new ConcurrentHashMap<>(); +public class InMemoryCrudRepository implements CrudRepository { + + private final ConcurrentMap inMemoryStore = new ConcurrentHashMap<>(); + private Function idGetter; + public InMemoryCrudRepository(Function idGetter) { + this.idGetter = idGetter; + } + @Override - public @NonNull S save(@NonNull S entity) { - inMemoryStore.put(entity.getId(), entity); + public @NonNull S save(@NonNull S entity) { + String id = idGetter.apply(entity); + + inMemoryStore.put(id, entity); return entity; } @Override - public @NonNull Iterable saveAll(@NonNull Iterable entities) { + public @NonNull Iterable saveAll(@NonNull Iterable entities) { entities.forEach(this::save); return entities; } @Override - public @NonNull Optional findById(String id) { + public @NonNull Optional findById(@NonNull String id) { return Optional.ofNullable(inMemoryStore.get(id)); } @@ -71,12 +77,12 @@ public boolean existsById(@NonNull String id) { } @Override - public @NonNull Iterable findAll() { + public @NonNull Iterable findAll() { return inMemoryStore.values(); } @Override - public @NonNull Iterable findAllById(@NonNull Iterable ids) { + public @NonNull Iterable findAllById(@NonNull Iterable ids) { return StreamSupport.stream(ids.spliterator(), false).map(inMemoryStore::get).filter(Objects::nonNull).collect(Collectors.toList()); } @@ -91,8 +97,10 @@ public void deleteById(@NonNull String id) { } @Override - public void delete(@NonNull Submodel entity) { - inMemoryStore.remove(entity.getId()); + public void delete(@NonNull T entity) { + String id = idGetter.apply(entity); + + inMemoryStore.remove(id); } @Override @@ -102,9 +110,10 @@ public void deleteAllById(@NonNull Iterable ids) { } @Override - public void deleteAll(@NonNull Iterable entities) { - for (Submodel entity : entities) - inMemoryStore.remove(entity.getId()); + public void deleteAll(@NonNull Iterable entities) { + + for (T entity : entities) + inMemoryStore.remove(idGetter.apply(entity)); } @Override @@ -112,5 +121,5 @@ public void deleteAll() { inMemoryStore.clear(); } -} +} diff --git a/basyx.common/pom.xml b/basyx.common/pom.xml index 9ac32b4b3..fe6807eaa 100644 --- a/basyx.common/pom.xml +++ b/basyx.common/pom.xml @@ -20,6 +20,7 @@ basyx.mongocore basyx.authorization basyx.client + basyx.backend.inmemory.core basyx.filerepository-backend basyx.filerepository-backend-inmemory basyx.filerepository-backend-mongodb diff --git a/basyx.conceptdescriptionrepository/basyx.conceptdescriptionrepository-backend-inmemory/pom.xml b/basyx.conceptdescriptionrepository/basyx.conceptdescriptionrepository-backend-inmemory/pom.xml index 3d9e795dd..12471a489 100644 --- a/basyx.conceptdescriptionrepository/basyx.conceptdescriptionrepository-backend-inmemory/pom.xml +++ b/basyx.conceptdescriptionrepository/basyx.conceptdescriptionrepository-backend-inmemory/pom.xml @@ -20,6 +20,10 @@ org.eclipse.digitaltwin.basyx basyx.conceptdescriptionrepository-backend + + + org.eclipse.digitaltwin.basyx + basyx.backend.inmemory.core org.eclipse.digitaltwin.basyx diff --git a/basyx.conceptdescriptionrepository/basyx.conceptdescriptionrepository-backend-inmemory/src/main/java/org/eclipse/digitaltwin/basyx/conceptdescriptionrepository/ConceptDescriptionInMemoryBackend.java b/basyx.conceptdescriptionrepository/basyx.conceptdescriptionrepository-backend-inmemory/src/main/java/org/eclipse/digitaltwin/basyx/conceptdescriptionrepository/ConceptDescriptionInMemoryBackend.java deleted file mode 100644 index 38d8afa27..000000000 --- a/basyx.conceptdescriptionrepository/basyx.conceptdescriptionrepository-backend-inmemory/src/main/java/org/eclipse/digitaltwin/basyx/conceptdescriptionrepository/ConceptDescriptionInMemoryBackend.java +++ /dev/null @@ -1,116 +0,0 @@ -/******************************************************************************* - * Copyright (C) 2024 the Eclipse BaSyx Authors - * - * Permission is hereby granted, free of charge, to any person obtaining - * a copy of this software and associated documentation files (the - * "Software"), to deal in the Software without restriction, including - * without limitation the rights to use, copy, modify, merge, publish, - * distribute, sublicense, and/or sell copies of the Software, and to - * permit persons to whom the Software is furnished to do so, subject to - * the following conditions: - * - * The above copyright notice and this permission notice shall be - * included in all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, - * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF - * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND - * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE - * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION - * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION - * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. - * - * SPDX-License-Identifier: MIT - ******************************************************************************/ - -package org.eclipse.digitaltwin.basyx.conceptdescriptionrepository; - -import java.util.Objects; -import java.util.Optional; -import java.util.concurrent.ConcurrentHashMap; -import java.util.concurrent.ConcurrentMap; -import java.util.stream.Collectors; -import java.util.stream.StreamSupport; - -import org.eclipse.digitaltwin.aas4j.v3.model.ConceptDescription; -import org.springframework.data.repository.CrudRepository; -import org.springframework.lang.NonNull; - -/** - * InMemory implementation for the {@link ConceptDescription} backend - * - * @author danish, mateusmolina - * - */ -public class ConceptDescriptionInMemoryBackend implements CrudRepository { - - private final ConcurrentMap inMemoryStore = new ConcurrentHashMap<>(); - - @Override - public @NonNull S save(@NonNull S entity) { - inMemoryStore.put(entity.getId(), entity); - - return entity; - } - - @Override - public @NonNull Iterable saveAll(@NonNull Iterable entities) { - entities.forEach(this::save); - - return entities; - } - - @Override - public @NonNull Optional findById(@NonNull String id) { - return Optional.ofNullable(inMemoryStore.get(id)); - } - - @Override - public boolean existsById(@NonNull String id) { - return inMemoryStore.containsKey(id); - } - - @Override - public @NonNull Iterable findAll() { - return inMemoryStore.values(); - } - - @Override - public @NonNull Iterable findAllById(@NonNull Iterable ids) { - return StreamSupport.stream(ids.spliterator(), false).map(inMemoryStore::get).filter(Objects::nonNull).collect(Collectors.toList()); - } - - @Override - public long count() { - return inMemoryStore.size(); - } - - @Override - public void deleteById(@NonNull String id) { - inMemoryStore.remove(id); - } - - @Override - public void delete(@NonNull ConceptDescription entity) { - inMemoryStore.remove(entity.getId()); - } - - @Override - public void deleteAllById(@NonNull Iterable ids) { - for (String id : ids) - inMemoryStore.remove(id); - } - - @Override - public void deleteAll(@NonNull Iterable entities) { - for (ConceptDescription entity : entities) - inMemoryStore.remove(entity.getId()); - } - - @Override - public void deleteAll() { - inMemoryStore.clear(); - } - -} - diff --git a/basyx.conceptdescriptionrepository/basyx.conceptdescriptionrepository-backend-inmemory/src/main/java/org/eclipse/digitaltwin/basyx/conceptdescriptionrepository/ConceptDescriptionInMemoryBackendProvider.java b/basyx.conceptdescriptionrepository/basyx.conceptdescriptionrepository-backend-inmemory/src/main/java/org/eclipse/digitaltwin/basyx/conceptdescriptionrepository/ConceptDescriptionInMemoryBackendProvider.java index b2dae7e65..e5775d9e7 100644 --- a/basyx.conceptdescriptionrepository/basyx.conceptdescriptionrepository-backend-inmemory/src/main/java/org/eclipse/digitaltwin/basyx/conceptdescriptionrepository/ConceptDescriptionInMemoryBackendProvider.java +++ b/basyx.conceptdescriptionrepository/basyx.conceptdescriptionrepository-backend-inmemory/src/main/java/org/eclipse/digitaltwin/basyx/conceptdescriptionrepository/ConceptDescriptionInMemoryBackendProvider.java @@ -27,6 +27,7 @@ import org.eclipse.digitaltwin.aas4j.v3.model.ConceptDescription; import org.eclipse.digitaltwin.basyx.aasrepository.backend.ConceptDescriptionBackendProvider; +import org.eclipse.digitaltwin.basyx.common.backend.inmemory.core.InMemoryCrudRepository; import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression; import org.springframework.data.repository.CrudRepository; import org.springframework.stereotype.Component; @@ -42,7 +43,7 @@ public class ConceptDescriptionInMemoryBackendProvider implements ConceptDescrip @Override public CrudRepository getCrudRepository() { - return new ConceptDescriptionInMemoryBackend(); + return new InMemoryCrudRepository(ConceptDescription::getId); } } diff --git a/basyx.submodelrepository/basyx.submodelrepository-backend-inmemory/pom.xml b/basyx.submodelrepository/basyx.submodelrepository-backend-inmemory/pom.xml index 11d97c975..1edfa7be2 100644 --- a/basyx.submodelrepository/basyx.submodelrepository-backend-inmemory/pom.xml +++ b/basyx.submodelrepository/basyx.submodelrepository-backend-inmemory/pom.xml @@ -24,6 +24,10 @@ org.eclipse.digitaltwin.basyx basyx.submodelservice-core + + + org.eclipse.digitaltwin.basyx + basyx.backend.inmemory.core org.eclipse.digitaltwin.basyx diff --git a/basyx.submodelrepository/basyx.submodelrepository-backend-inmemory/src/main/java/org/eclipse/digitaltwin/basyx/submodelrepository/SubmodelInMemoryBackendProvider.java b/basyx.submodelrepository/basyx.submodelrepository-backend-inmemory/src/main/java/org/eclipse/digitaltwin/basyx/submodelrepository/SubmodelInMemoryBackendProvider.java index 79b69eb2a..bf65b85dc 100644 --- a/basyx.submodelrepository/basyx.submodelrepository-backend-inmemory/src/main/java/org/eclipse/digitaltwin/basyx/submodelrepository/SubmodelInMemoryBackendProvider.java +++ b/basyx.submodelrepository/basyx.submodelrepository-backend-inmemory/src/main/java/org/eclipse/digitaltwin/basyx/submodelrepository/SubmodelInMemoryBackendProvider.java @@ -26,6 +26,7 @@ package org.eclipse.digitaltwin.basyx.submodelrepository; import org.eclipse.digitaltwin.aas4j.v3.model.Submodel; +import org.eclipse.digitaltwin.basyx.common.backend.inmemory.core.InMemoryCrudRepository; import org.eclipse.digitaltwin.basyx.submodelrepository.backend.SubmodelBackendProvider; import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression; import org.springframework.data.repository.CrudRepository; @@ -43,7 +44,7 @@ public class SubmodelInMemoryBackendProvider implements SubmodelBackendProvider @Override public CrudRepository getCrudRepository() { - return new SubmodelInMemoryBackend(); + return new InMemoryCrudRepository(Submodel::getId); } } diff --git a/basyx.submodelrepository/basyx.submodelrepository-core/src/test/java/org/eclipse/digitaltwin/basyx/submodelrepository/core/SubmodelRepositorySuite.java b/basyx.submodelrepository/basyx.submodelrepository-core/src/test/java/org/eclipse/digitaltwin/basyx/submodelrepository/core/SubmodelRepositorySuite.java index 82fa5b6b5..ce9c3aa04 100644 --- a/basyx.submodelrepository/basyx.submodelrepository-core/src/test/java/org/eclipse/digitaltwin/basyx/submodelrepository/core/SubmodelRepositorySuite.java +++ b/basyx.submodelrepository/basyx.submodelrepository-core/src/test/java/org/eclipse/digitaltwin/basyx/submodelrepository/core/SubmodelRepositorySuite.java @@ -48,7 +48,6 @@ import org.eclipse.digitaltwin.basyx.core.exceptions.NotInvokableException; import org.eclipse.digitaltwin.basyx.core.pagination.CursorResult; import org.eclipse.digitaltwin.basyx.core.pagination.PaginationInfo; -import org.eclipse.digitaltwin.basyx.http.Base64UrlEncoder; import org.eclipse.digitaltwin.basyx.submodelrepository.SubmodelRepository; import org.eclipse.digitaltwin.basyx.submodelservice.DummySubmodelFactory; import org.eclipse.digitaltwin.basyx.submodelservice.SubmodelService; @@ -95,7 +94,7 @@ public void getAllSubmodelsBySemanticIDPreconfigured() { Collection expectedSubmodels = DummySubmodelFactory.getSubmodelsBySemanticid(DummySubmodelFactory.SUBMODEL_TECHNICAL_DATA_SEMANTIC_ID); SubmodelRepository repo = getSubmodelRepository(expectedSubmodels); - Collection submodels = repo.getAllSubmodels(DummySubmodelFactory.SUBMODEL_TECHNICAL_DATA_SEMANTIC_ID, NO_LIMIT_PAGINATION_INFO).getResult(); + Collection submodels = repo.getAllSubmodels(DummySubmodelFactory.SUBMODEL_TECHNICAL_DATA_SEMANTIC_ID, PaginationInfo.NO_LIMIT).getResult(); assertSubmodelsAreContained(expectedSubmodels, submodels); } diff --git a/pom.xml b/pom.xml index 079e995d4..7ece3f8bb 100644 --- a/pom.xml +++ b/pom.xml @@ -448,6 +448,11 @@ basyx.filerepository-backend-mongodb ${revision} + + org.eclipse.digitaltwin.basyx + basyx.backend.inmemory.core + ${revision} + org.eclipse.digitaltwin.basyx