-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add ParticipantContextStore (mem + sql)
- Loading branch information
1 parent
2a53606
commit 3f4d23f
Showing
26 changed files
with
1,220 additions
and
78 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
119 changes: 119 additions & 0 deletions
119
...b-credentials/src/main/java/org/eclipse/edc/identityhub/defaults/InMemoryEntityStore.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
/* | ||
* Copyright (c) 2024 Metaform Systems, Inc. | ||
* | ||
* This program and the accompanying materials are made available under the | ||
* terms of the Apache License, Version 2.0 which is available at | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* Contributors: | ||
* Metaform Systems, Inc. - initial API and implementation | ||
* | ||
*/ | ||
|
||
package org.eclipse.edc.identityhub.defaults; | ||
|
||
import org.eclipse.edc.spi.query.QueryResolver; | ||
import org.eclipse.edc.spi.query.QuerySpec; | ||
import org.eclipse.edc.spi.result.StoreResult; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.concurrent.locks.ReentrantReadWriteLock; | ||
import java.util.function.Predicate; | ||
import java.util.stream.Stream; | ||
|
||
import static org.eclipse.edc.spi.result.StoreResult.alreadyExists; | ||
import static org.eclipse.edc.spi.result.StoreResult.notFound; | ||
import static org.eclipse.edc.spi.result.StoreResult.success; | ||
|
||
/** | ||
* Base class for in-mem entity stores, that implement basic CRUD operations. | ||
*/ | ||
abstract class InMemoryEntityStore<T> { | ||
protected final Map<String, T> store = new HashMap<>(); | ||
protected final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true); | ||
protected final QueryResolver<T> queryResolver = createQueryResolver(); | ||
|
||
/** | ||
* Creates a new entity if none exists. | ||
* | ||
* @param newObject the new object to insert. | ||
* @return failure if an object with the same ID already exists. | ||
*/ | ||
public StoreResult<Void> create(T newObject) { | ||
lock.writeLock().lock(); | ||
var id = getId(newObject); | ||
try { | ||
if (store.containsKey(id)) { | ||
return alreadyExists("A VerifiableCredentialResource with ID %s already exists".formatted(id)); | ||
} | ||
store.put(id, newObject); | ||
return success(null); | ||
} finally { | ||
lock.writeLock().unlock(); | ||
} | ||
} | ||
|
||
/** | ||
* Performs a query using the given query parameters. | ||
* | ||
* @param querySpec A non-null QuerySpec. | ||
* @return A (potentially empty) Stream of objects. Callers must close the stream. | ||
*/ | ||
public StoreResult<Stream<T>> query(QuerySpec querySpec) { | ||
lock.readLock().lock(); | ||
try { | ||
// if no filter is present, we return true | ||
Predicate<Object> fallback = querySpec.getFilterExpression().isEmpty() ? x -> true : x -> false; | ||
var result = queryResolver.query(store.values().stream(), querySpec, Predicate::or, fallback); | ||
return success(result); | ||
} finally { | ||
lock.readLock().unlock(); | ||
} | ||
} | ||
|
||
/** | ||
* Replaces an existing entity with a new object. | ||
* | ||
* @param newObject the new entity | ||
* @return failure if an object with the same ID was not found. | ||
*/ | ||
public StoreResult<Void> update(T newObject) { | ||
lock.writeLock().lock(); | ||
try { | ||
var id = getId(newObject); | ||
if (!store.containsKey(id)) { | ||
return notFound("A VerifiableCredentialResource with ID %s was not found".formatted(id)); | ||
} | ||
store.put(id, newObject); | ||
return success(); | ||
} finally { | ||
lock.writeLock().unlock(); | ||
} | ||
} | ||
|
||
/** | ||
* Deletes the object with the given ID | ||
* | ||
* @param id The ID of the object to delete. | ||
* @return failure if an object with the given ID was not found. | ||
*/ | ||
public StoreResult<Void> deleteById(String id) { | ||
lock.writeLock().lock(); | ||
try { | ||
if (!store.containsKey(id)) { | ||
return notFound("A VerifiableCredentialResource with ID %s was not found".formatted(id)); | ||
} | ||
store.remove(id); | ||
return success(); | ||
} finally { | ||
lock.writeLock().unlock(); | ||
} | ||
} | ||
|
||
protected abstract String getId(T newObject); | ||
|
||
protected abstract QueryResolver<T> createQueryResolver(); | ||
} |
36 changes: 36 additions & 0 deletions
36
...s/src/main/java/org/eclipse/edc/identityhub/defaults/InMemoryParticipantContextStore.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
/* | ||
* Copyright (c) 2024 Metaform Systems, Inc. | ||
* | ||
* This program and the accompanying materials are made available under the | ||
* terms of the Apache License, Version 2.0 which is available at | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* Contributors: | ||
* Metaform Systems, Inc. - initial API and implementation | ||
* | ||
*/ | ||
|
||
package org.eclipse.edc.identityhub.defaults; | ||
|
||
import org.eclipse.edc.connector.core.store.CriterionToPredicateConverterImpl; | ||
import org.eclipse.edc.connector.core.store.ReflectionBasedQueryResolver; | ||
import org.eclipse.edc.identityhub.spi.model.participant.ParticipantContext; | ||
import org.eclipse.edc.identityhub.spi.store.ParticipantContextStore; | ||
import org.eclipse.edc.spi.query.QueryResolver; | ||
|
||
/** | ||
* In-memory variant of the {@link ParticipantContextStore} that is thread-safe. | ||
*/ | ||
public class InMemoryParticipantContextStore extends InMemoryEntityStore<ParticipantContext> implements ParticipantContextStore { | ||
@Override | ||
protected String getId(ParticipantContext newObject) { | ||
return newObject.getParticipantId(); | ||
} | ||
|
||
@Override | ||
protected QueryResolver<ParticipantContext> createQueryResolver() { | ||
return new ReflectionBasedQueryResolver<>(ParticipantContext.class, new CriterionToPredicateConverterImpl()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
...c/test/java/org/eclipse/edc/identityhub/defaults/InMemoryParticipantContextStoreTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
/* | ||
* Copyright (c) 2024 Metaform Systems, Inc. | ||
* | ||
* This program and the accompanying materials are made available under the | ||
* terms of the Apache License, Version 2.0 which is available at | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* Contributors: | ||
* Metaform Systems, Inc. - initial API and implementation | ||
* | ||
*/ | ||
|
||
package org.eclipse.edc.identityhub.defaults; | ||
|
||
import org.eclipse.edc.identityhub.spi.store.ParticipantContextStore; | ||
import org.eclipse.edc.identityhub.store.test.ParticipantContextStoreTestBase; | ||
|
||
class InMemoryParticipantContextStoreTest extends ParticipantContextStoreTestBase { | ||
|
||
private final InMemoryParticipantContextStore store = new InMemoryParticipantContextStore(); | ||
|
||
@Override | ||
protected ParticipantContextStore getStore() { | ||
return store; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 27 additions & 0 deletions
27
extensions/store/sql/identity-hub-participantcontext-store-sql/build.gradle.kts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
/* | ||
* Copyright (c) 2023 Metaform Systems, Inc. | ||
* | ||
* This program and the accompanying materials are made available under the | ||
* terms of the Apache License, Version 2.0 which is available at | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* Contributors: | ||
* Metaform Systems, Inc. - initial API and implementation | ||
* | ||
*/ | ||
|
||
plugins { | ||
`java-library` | ||
} | ||
|
||
dependencies { | ||
api(project(":spi:identity-hub-store-spi")) | ||
implementation(libs.edc.core.sql) // for the SqlStatements | ||
implementation(libs.edc.spi.transaction.datasource) | ||
|
||
testImplementation(testFixtures(project(":spi:identity-hub-store-spi"))) | ||
testImplementation(testFixtures(libs.edc.core.sql)) | ||
testImplementation(libs.edc.junit) | ||
} |
25 changes: 25 additions & 0 deletions
25
extensions/store/sql/identity-hub-participantcontext-store-sql/docs/schema.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
/* | ||
* Copyright (c) 2024 Metaform Systems, Inc. | ||
* | ||
* This program and the accompanying materials are made available under the | ||
* terms of the Apache License, Version 2.0 which is available at | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* Contributors: | ||
* Metaform Systems, Inc. - initial API and implementation | ||
* | ||
*/ | ||
|
||
-- only intended for and tested with Postgres! | ||
CREATE TABLE participant_context | ||
( | ||
participant_id VARCHAR PRIMARY KEY NOT NULL, -- ID of the ParticipantContext | ||
created_date BIGINT NOT NULL, -- POSIX timestamp of the creation of the PC | ||
last_modified_date BIGINT, -- POSIX timestamp of the last modified date | ||
state INTEGER NOT NULL, -- 0 = CREATED, 1 = ACTIVE, 2 = DEACTIVATED | ||
api_token_alias VARCHAR NOT NULL -- alias under which this PC's api token is stored in the vault | ||
); | ||
CREATE UNIQUE INDEX participant_context_participant_id_uindex ON participant_context USING btree (participant_id); | ||
|
Oops, something went wrong.