-
Notifications
You must be signed in to change notification settings - Fork 1
feat: SQL persistence layer #80
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
ronjaquensel
merged 9 commits into
eclipse-dataplane-core:main
from
FraunhoferISST:feat/52-sql-persistence
May 26, 2026
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
8743f03
feat: SQL stores
ronjaquensel 3749a99
chore: move statements to interface
ronjaquensel 8ceccbe
test: add store tests
ronjaquensel 9800343
chore: license headers
ronjaquensel 10f4a6c
docs: Javadoc
ronjaquensel c9d1832
chore: add builder methods for stores on Dataplane
ronjaquensel 3ae0c53
refactor: SQL stores
ronjaquensel 91d0419
chore: checkstyle
ronjaquensel d82d9bf
chore: PR remarks
ronjaquensel File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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
30 changes: 30 additions & 0 deletions
30
src/main/java/org/eclipse/dataplane/port/exception/PersistenceException.java
This file contains hidden or 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,30 @@ | ||
| /* | ||
| * Copyright (c) 2026 Fraunhofer-Gesellschaft zur Förderung der angewandten Forschung e.V. | ||
| * | ||
| * 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: | ||
| * Fraunhofer-Gesellschaft zur Förderung der angewandten Forschung e.V. - initial API and implementation | ||
| * | ||
| */ | ||
|
|
||
| package org.eclipse.dataplane.port.exception; | ||
|
|
||
| /** | ||
| * Indicates an error during database interactions, i.e. an error occurred persisting, reading or | ||
| * deleting an entry. | ||
| */ | ||
| public class PersistenceException extends RuntimeException { | ||
|
|
||
| public PersistenceException(String message) { | ||
| super(message); | ||
| } | ||
|
|
||
| public PersistenceException(String message, Throwable cause) { | ||
| super(message, cause); | ||
| } | ||
| } |
24 changes: 24 additions & 0 deletions
24
src/main/java/org/eclipse/dataplane/port/store/Stores.java
This file contains hidden or 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,24 @@ | ||
| /* | ||
| * Copyright (c) 2026 Fraunhofer-Gesellschaft zur Förderung der angewandten Forschung e.V. | ||
| * | ||
| * 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: | ||
| * Fraunhofer-Gesellschaft zur Förderung der angewandten Forschung e.V. - initial API and implementation | ||
| * | ||
| */ | ||
|
|
||
| package org.eclipse.dataplane.port.store; | ||
|
|
||
| /** | ||
| * Data class that bundles the stores used by the dataplane. | ||
| * | ||
| * @param dataFlowStore store for data flows | ||
| * @param controlPlaneStore store for control planes | ||
| */ | ||
| public record Stores(DataFlowStore dataFlowStore, ControlPlaneStore controlPlaneStore) { | ||
| } |
92 changes: 92 additions & 0 deletions
92
src/main/java/org/eclipse/dataplane/port/store/sql/AbstractSqlStore.java
This file contains hidden or 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,92 @@ | ||
| /* | ||
| * Copyright (c) 2026 Fraunhofer-Gesellschaft zur Förderung der angewandten Forschung e.V. | ||
| * | ||
| * 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: | ||
| * Fraunhofer-Gesellschaft zur Förderung der angewandten Forschung e.V. - initial API and implementation | ||
| * | ||
| */ | ||
|
|
||
| package org.eclipse.dataplane.port.store.sql; | ||
|
|
||
| import com.fasterxml.jackson.core.JsonProcessingException; | ||
| import com.fasterxml.jackson.core.type.TypeReference; | ||
| import com.fasterxml.jackson.databind.JavaType; | ||
| import com.fasterxml.jackson.databind.ObjectMapper; | ||
| import org.eclipse.dataplane.port.exception.PersistenceException; | ||
|
|
||
| import java.sql.Connection; | ||
| import java.sql.DriverManager; | ||
|
|
||
| /** | ||
| * Base class for SQL-based store implementations that provides methods for common functionality | ||
| * like connection handling and JSON parsing. | ||
| */ | ||
| public abstract class AbstractSqlStore { | ||
|
|
||
| protected ObjectMapper objectMapper; | ||
|
|
||
| private final String databaseUrl; | ||
| private final String databaseUsername; | ||
| private final String databasePassword; | ||
|
|
||
| public AbstractSqlStore(ObjectMapper objectMapper, String databaseUrl, String databaseUsername, String databasePassword) { | ||
| this.objectMapper = objectMapper; | ||
| this.databaseUrl = databaseUrl; | ||
| this.databaseUsername = databaseUsername; | ||
| this.databasePassword = databasePassword; | ||
| } | ||
|
|
||
| protected Connection getConnection() { | ||
| try { | ||
| return DriverManager.getConnection(databaseUrl, databaseUsername, databasePassword); | ||
| } catch (Exception e) { | ||
| throw new PersistenceException("Failed to connect to database.", e); | ||
| } | ||
| } | ||
|
|
||
| protected void closeConnection(Connection connection) { | ||
| try { | ||
| connection.close(); | ||
| } catch (Exception e) { | ||
| throw new PersistenceException("Failed to commit transaction.", e); | ||
| } | ||
| } | ||
|
|
||
| protected String toJson(Object object) { | ||
| if (object == null) { | ||
| return null; | ||
| } | ||
|
|
||
| try { | ||
| return object instanceof String ? object.toString() : objectMapper.writeValueAsString(object); | ||
| } catch (JsonProcessingException e) { | ||
| throw new PersistenceException("Failed to convert object to JSON.", e); | ||
| } | ||
| } | ||
|
|
||
| protected <T> T fromJson(String json, Class<T> type) { | ||
| return fromJson(json, objectMapper.getTypeFactory().constructType(type)); | ||
| } | ||
|
|
||
| protected <T> T fromJson(String json, TypeReference<T> type) { | ||
| return fromJson(json, objectMapper.getTypeFactory().constructType(type)); | ||
| } | ||
|
|
||
| protected <T> T fromJson(String json, JavaType type) { | ||
| if (json == null) { | ||
| return null; | ||
| } | ||
|
|
||
| try { | ||
| return objectMapper.readValue(json, type); | ||
| } catch (JsonProcessingException e) { | ||
| throw new PersistenceException("Failed to convert JSON to object.", e); | ||
| } | ||
| } | ||
| } | ||
130 changes: 130 additions & 0 deletions
130
src/main/java/org/eclipse/dataplane/port/store/sql/PostgresControlPlaneStore.java
This file contains hidden or 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,130 @@ | ||
| /* | ||
| * Copyright (c) 2026 Fraunhofer-Gesellschaft zur Förderung der angewandten Forschung e.V. | ||
| * | ||
| * 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: | ||
| * Fraunhofer-Gesellschaft zur Förderung der angewandten Forschung e.V. - initial API and implementation | ||
| * | ||
| */ | ||
|
|
||
| package org.eclipse.dataplane.port.store.sql; | ||
|
|
||
| import com.fasterxml.jackson.databind.ObjectMapper; | ||
| import org.eclipse.dataplane.domain.Result; | ||
| import org.eclipse.dataplane.domain.controlplane.ControlPlane; | ||
| import org.eclipse.dataplane.domain.registration.AuthorizationProfile; | ||
| import org.eclipse.dataplane.port.exception.PersistenceException; | ||
| import org.eclipse.dataplane.port.exception.ResourceNotFoundException; | ||
| import org.eclipse.dataplane.port.store.ControlPlaneStore; | ||
|
|
||
| import java.net.URI; | ||
|
|
||
| import static java.lang.String.format; | ||
|
|
||
| public class PostgresControlPlaneStore extends AbstractSqlStore implements ControlPlaneStore { | ||
|
|
||
| public PostgresControlPlaneStore(ObjectMapper objectMapper, String databaseUrl, String databaseUsername, String databasePassword) { | ||
| super(objectMapper, databaseUrl, databaseUsername, databasePassword); | ||
| } | ||
|
|
||
| @Override | ||
| public Result<Void> save(ControlPlane controlPlane) { | ||
| var connection = getConnection(); | ||
|
|
||
| try (var statement = connection.prepareStatement(upsertControlPlaneTemplate())) { | ||
| statement.setString(1, controlPlane.getId()); | ||
| statement.setString(2, controlPlane.getEndpoint().toString()); | ||
| statement.setString(3, toJson(controlPlane.getAuthorization())); | ||
|
|
||
| statement.executeUpdate(); | ||
| return Result.success(); | ||
| } catch (Exception e) { | ||
| return Result.failure(new PersistenceException(format("Failed to persist ControlPlane with id %s.", controlPlane.getId()), e)); | ||
| } finally { | ||
| closeConnection(connection); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public Result<ControlPlane> findById(String controlplaneId) { | ||
| var connection = getConnection(); | ||
|
|
||
| try (var statement = connection.prepareStatement(findControlPlaneByIdTemplate())) { | ||
| statement.setString(1, controlplaneId); | ||
| var resultSet = statement.executeQuery(); | ||
|
|
||
| if (!resultSet.next()) { | ||
| return Result.failure(new ResourceNotFoundException(format("ControlPlane with id %s not found.", controlplaneId))); | ||
| } | ||
|
|
||
| var controlplane = ControlPlane.newInstance() | ||
| .id(controlplaneId) | ||
| .endpoint(URI.create(resultSet.getString("endpoint"))) | ||
| .authorization(fromJson(resultSet.getString("auth"), AuthorizationProfile.class)) | ||
| .build(); | ||
| return Result.success(controlplane); | ||
| } catch (Exception e) { | ||
| return Result.failure(new PersistenceException(format("Failed to read ControlPlane with id %s.", controlplaneId), e)); | ||
| } finally { | ||
| closeConnection(connection); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public Result<Void> delete(String id) { | ||
| var connection = getConnection(); | ||
|
|
||
| try (var statement = connection.prepareStatement(deleteControlPlaneByIdTemplate())) { | ||
| statement.setString(1, id); | ||
| var rows = statement.executeUpdate(); | ||
| if (rows < 1) { | ||
| return Result.failure(new ResourceNotFoundException(format("ControlPlane with id %s not found.", id))); | ||
| } | ||
| return Result.success(); | ||
| } catch (Exception e) { | ||
| return Result.failure(new PersistenceException(format("Failed to delete ControlPlane with id %s.", id), e)); | ||
| } finally { | ||
| closeConnection(connection); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public boolean exists(String controlplaneId) { | ||
| var connection = getConnection(); | ||
|
|
||
| try (var statement = connection.prepareStatement(countControlPlaneByIdTemplate())) { | ||
| statement.setString(1, controlplaneId); | ||
| var resultSet = statement.executeQuery(); | ||
| resultSet.next(); | ||
| return resultSet.getInt(1) > 0; | ||
| } catch (Exception e) { | ||
| throw new PersistenceException(format("Failed to check for existence of ControlPlane with id %s.", controlplaneId), e); | ||
| } finally { | ||
| closeConnection(connection); | ||
| } | ||
| } | ||
|
|
||
| private String upsertControlPlaneTemplate() { | ||
| return "INSERT INTO control_planes (id, endpoint, auth) VALUES (?, ?, ?::json)" + | ||
| " ON CONFLICT (id) DO UPDATE SET" + | ||
| " endpoint = EXCLUDED.endpoint," + | ||
| " auth = EXCLUDED.auth"; | ||
| } | ||
|
|
||
| private String findControlPlaneByIdTemplate() { | ||
| return "SELECT * FROM control_planes WHERE id = ?"; | ||
| } | ||
|
|
||
| private String deleteControlPlaneByIdTemplate() { | ||
| return "DELETE FROM control_planes WHERE id = ?"; | ||
| } | ||
|
|
||
| private String countControlPlaneByIdTemplate() { | ||
| return "SELECT COUNT(*) FROM control_planes WHERE id = ?"; | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this makes me think that we should create a refactor issue to permit to setup an external connection pool if needed (very likely in any production environment)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, was thinking about that as well. Will create a follow-up issue once this PR is merged.