-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add implementations for
DidResourceStore
- Loading branch information
1 parent
d46a89a
commit f0ebbe3
Showing
26 changed files
with
1,140 additions
and
17 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
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: | ||
* Metform Systems, Inc. - initial API and implementation | ||
* | ||
*/ | ||
|
||
plugins { | ||
`java-library` | ||
} | ||
|
||
dependencies { | ||
api(project(":spi:identity-hub-did-spi")) | ||
implementation(libs.edc.core.sql) // for the SqlStatements | ||
implementation(libs.edc.spi.transaction.datasource) | ||
|
||
testImplementation(testFixtures(project(":spi:identity-hub-did-spi"))) | ||
testImplementation(testFixtures(libs.edc.core.sql)) | ||
testImplementation(libs.edc.junit) | ||
} |
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) 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: | ||
* Metform Systems, Inc. - initial API and implementation | ||
* | ||
*/ | ||
|
||
-- | ||
-- | ||
-- 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: | ||
-- Bayerische Motoren Werke Aktiengesellschaft (BMW AG) - initial API and implementation | ||
-- | ||
|
||
-- only intended for and tested with Postgres! | ||
CREATE TABLE IF NOT EXISTS did_resources | ||
( | ||
did VARCHAR NOT NULL, | ||
create_timestamp BIGINT NOT NULL, | ||
state_timestamp BIGINT NOT NULL, | ||
state INT NOT NULL, | ||
did_document JSON NOT NULL, | ||
PRIMARY KEY (did) | ||
); |
67 changes: 67 additions & 0 deletions
67
...sql/src/main/java/org/eclipse/edc/identityhub/did/store/sql/BaseSqlDialectStatements.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,67 @@ | ||
/* | ||
* 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: | ||
* Metform Systems, Inc. - initial API and implementation | ||
* | ||
*/ | ||
|
||
package org.eclipse.edc.identityhub.did.store.sql; | ||
|
||
import org.eclipse.edc.identityhub.did.store.sql.schema.postgres.DidResourceMapping; | ||
import org.eclipse.edc.spi.query.QuerySpec; | ||
import org.eclipse.edc.sql.translation.SqlQueryStatement; | ||
|
||
import static java.lang.String.format; | ||
|
||
public class BaseSqlDialectStatements implements DidResourceStatements { | ||
@Override | ||
public String getInsertTemplate() { | ||
return executeStatement() | ||
.column(getIdColumn()) | ||
.column(getStateColumn()) | ||
.column(getCreateTimestampColumn()) | ||
.column(getStateTimestampColumn()) | ||
.jsonColumn(getDidDocumentColumn()) | ||
.insertInto(getDidResourceTableName()); | ||
} | ||
|
||
@Override | ||
public String getUpdateTemplate() { | ||
return executeStatement() | ||
.column(getIdColumn()) | ||
.column(getStateColumn()) | ||
.column(getCreateTimestampColumn()) | ||
.column(getStateTimestampColumn()) | ||
.jsonColumn(getDidDocumentColumn()) | ||
.update(getDidResourceTableName(), getIdColumn()); | ||
} | ||
|
||
@Override | ||
public String getDeleteByIdTemplate() { | ||
return executeStatement().delete(getDidResourceTableName(), getIdColumn()); | ||
} | ||
|
||
@Override | ||
public String getFindByIdTemplate() { | ||
return format("SELECT * FROM %s WHERE %s = ?", getDidResourceTableName(), getIdColumn()); | ||
|
||
} | ||
|
||
@Override | ||
public SqlQueryStatement createQuery(QuerySpec querySpec) { | ||
var select = getSelectStatement(); | ||
return new SqlQueryStatement(select, querySpec, new DidResourceMapping(this)); | ||
} | ||
|
||
@Override | ||
public String getSelectStatement() { | ||
return format("SELECT * FROM %s", getDidResourceTableName()); | ||
} | ||
} |
61 changes: 61 additions & 0 deletions
61
...re-sql/src/main/java/org/eclipse/edc/identityhub/did/store/sql/DidResourceStatements.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,61 @@ | ||
/* | ||
* 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: | ||
* Metform Systems, Inc. - initial API and implementation | ||
* | ||
*/ | ||
|
||
package org.eclipse.edc.identityhub.did.store.sql; | ||
|
||
import org.eclipse.edc.spi.query.QuerySpec; | ||
import org.eclipse.edc.sql.statement.SqlStatements; | ||
import org.eclipse.edc.sql.translation.SqlQueryStatement; | ||
|
||
/** | ||
* The DidResourceStatements interface defines the SQL statements required to interact with the `did_resources` table. | ||
* It extends the SqlStatements interface. | ||
*/ | ||
public interface DidResourceStatements extends SqlStatements { | ||
default String getDidResourceTableName() { | ||
return "did_resources"; | ||
} | ||
|
||
default String getIdColumn() { | ||
return "did"; | ||
} | ||
|
||
default String getStateColumn() { | ||
return "state"; | ||
} | ||
|
||
default String getStateTimestampColumn() { | ||
return "state_timestamp"; | ||
} | ||
|
||
default String getCreateTimestampColumn() { | ||
return "create_timestamp"; | ||
} | ||
|
||
default String getDidDocumentColumn() { | ||
return "did_document"; | ||
} | ||
|
||
String getInsertTemplate(); | ||
|
||
String getUpdateTemplate(); | ||
|
||
String getDeleteByIdTemplate(); | ||
|
||
String getFindByIdTemplate(); | ||
|
||
SqlQueryStatement createQuery(QuerySpec query); | ||
|
||
String getSelectStatement(); | ||
} |
144 changes: 144 additions & 0 deletions
144
...tore-sql/src/main/java/org/eclipse/edc/identityhub/did/store/sql/SqlDidResourceStore.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,144 @@ | ||
/* | ||
* 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: | ||
* Metform Systems, Inc. - initial API and implementation | ||
* | ||
*/ | ||
|
||
package org.eclipse.edc.identityhub.did.store.sql; | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import org.eclipse.edc.iam.did.spi.document.DidDocument; | ||
import org.eclipse.edc.identithub.did.spi.model.DidResource; | ||
import org.eclipse.edc.identithub.did.spi.store.DidResourceStore; | ||
import org.eclipse.edc.spi.persistence.EdcPersistenceException; | ||
import org.eclipse.edc.spi.query.QuerySpec; | ||
import org.eclipse.edc.spi.result.StoreResult; | ||
import org.eclipse.edc.sql.QueryExecutor; | ||
import org.eclipse.edc.sql.store.AbstractSqlStore; | ||
import org.eclipse.edc.transaction.datasource.spi.DataSourceRegistry; | ||
import org.eclipse.edc.transaction.spi.TransactionContext; | ||
|
||
import java.sql.ResultSet; | ||
import java.sql.SQLException; | ||
import java.util.Collection; | ||
import java.util.Objects; | ||
|
||
|
||
public class SqlDidResourceStore extends AbstractSqlStore implements DidResourceStore { | ||
|
||
private final DidResourceStatements statements; | ||
|
||
public SqlDidResourceStore(DataSourceRegistry dataSourceRegistry, String dataSourceName, TransactionContext transactionContext, | ||
ObjectMapper objectMapper, QueryExecutor queryExecutor, DidResourceStatements statements) { | ||
super(dataSourceRegistry, dataSourceName, transactionContext, objectMapper, queryExecutor); | ||
this.statements = statements; | ||
} | ||
|
||
|
||
@Override | ||
public StoreResult<Void> save(DidResource resource) { | ||
var did = resource.getDid(); | ||
return transactionContext.execute(() -> { | ||
try (var connection = getConnection()) { | ||
if (findById(did) != null) { | ||
return StoreResult.alreadyExists(alreadyExistsErrorMessage(did)); | ||
} | ||
|
||
var stmt = statements.getInsertTemplate(); | ||
queryExecutor.execute(connection, stmt, | ||
did, | ||
resource.getState(), | ||
resource.getCreateTimestamp(), | ||
resource.getStateTimestamp(), | ||
toJson(resource.getDocument())); | ||
return StoreResult.success(); | ||
} catch (SQLException e) { | ||
throw new EdcPersistenceException(e); | ||
} | ||
}); | ||
} | ||
|
||
@Override | ||
public StoreResult<Void> update(DidResource resource) { | ||
var did = resource.getDid(); | ||
Objects.requireNonNull(resource); | ||
Objects.requireNonNull(did); | ||
return transactionContext.execute(() -> { | ||
try (var connection = getConnection()) { | ||
if (findById(did) != null) { | ||
queryExecutor.execute(connection, statements.getUpdateTemplate(), | ||
did, | ||
resource.getState(), | ||
resource.getCreateTimestamp(), | ||
resource.getStateTimestamp(), | ||
toJson(resource.getDocument()), | ||
did); | ||
return StoreResult.success(); | ||
} | ||
return StoreResult.notFound(notFoundErrorMessage(did)); | ||
} catch (SQLException e) { | ||
throw new EdcPersistenceException(e); | ||
} | ||
}); | ||
} | ||
|
||
@Override | ||
public DidResource findById(String did) { | ||
Objects.requireNonNull(did); | ||
return transactionContext.execute(() -> { | ||
try (var connection = getConnection()) { | ||
var sql = statements.getFindByIdTemplate(); | ||
return queryExecutor.single(connection, false, this::mapResultSet, sql, did); | ||
} catch (Exception exception) { | ||
throw new EdcPersistenceException(exception); | ||
} | ||
}); | ||
} | ||
|
||
@Override | ||
public Collection<DidResource> query(QuerySpec query) { | ||
return transactionContext.execute(() -> { | ||
try (var connection = getConnection()) { | ||
var sql = statements.createQuery(query); | ||
return queryExecutor.query(connection, true, this::mapResultSet, sql.getQueryAsString(), sql.getParameters()).toList(); | ||
} catch (Exception exception) { | ||
throw new EdcPersistenceException(exception); | ||
} | ||
}); | ||
} | ||
|
||
@Override | ||
public StoreResult<Void> deleteById(String did) { | ||
Objects.requireNonNull(did); | ||
return transactionContext.execute(() -> { | ||
try (var connection = getConnection()) { | ||
if (findById(did) != null) { | ||
var stmt = statements.getDeleteByIdTemplate(); | ||
queryExecutor.execute(connection, stmt, did); | ||
return StoreResult.success(); | ||
} | ||
return StoreResult.notFound(notFoundErrorMessage(did)); | ||
} catch (SQLException e) { | ||
throw new EdcPersistenceException(e); | ||
} | ||
}); | ||
} | ||
|
||
private DidResource mapResultSet(ResultSet resultSet) throws Exception { | ||
return DidResource.Builder.newInstance() | ||
.did(resultSet.getString(statements.getIdColumn())) | ||
.createTimestamp(resultSet.getLong(statements.getCreateTimestampColumn())) | ||
.stateTimeStamp(resultSet.getLong(statements.getStateTimestampColumn())) | ||
.document(fromJson(resultSet.getString(statements.getDidDocumentColumn()), DidDocument.class)) | ||
.state(resultSet.getInt(statements.getStateColumn())) | ||
.build(); | ||
} | ||
} |
Oops, something went wrong.