Class FilePersistenceService
- All Implemented Interfaces:
PersistenceService
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic final recordStatistics about persisted data -
Constructor Summary
ConstructorsConstructorDescriptionCreate a file persistence service with default directoryFilePersistenceService(Path dataDirectory) Create a file persistence service with custom directoryFilePersistenceService(Path dataDirectory, boolean prettyPrint) Create a file persistence service with custom settings -
Method Summary
Modifier and TypeMethodDescriptioncleanupSnapshots(String agentId, int keepCount) Clean up old snapshots, keeping only the most recent NcreateSnapshot(String agentId, String snapshotId) Creates a named snapshot of the current persisted state of an agent.deleteState(String agentId) Deletes the persisted state of an agent asynchronously.existsState(String agentId) Checks whether a persisted state exists for an agent.getStats()Get statistics about persisted datalistSnapshots(String agentId) Returns the list of snapshot identifiers available for an agent.Loads the persisted state of an agent asynchronously.restoreSnapshot(String agentId, String snapshotId) Restores an agent's state from a previously created snapshot.saveState(String agentId, AgentState state) Saves (or overwrites) the state of an agent asynchronously.
-
Constructor Details
-
FilePersistenceService
public FilePersistenceService()Create a file persistence service with default directory -
FilePersistenceService
Create a file persistence service with custom directory -
FilePersistenceService
Create a file persistence service with custom settings
-
-
Method Details
-
saveState
Description copied from interface:PersistenceServiceSaves (or overwrites) the state of an agent asynchronously.If a state for the given
agentIdalready exists, it is replaced. Implementations should use theAgentState.version()field for optimistic concurrency control when the storage backend supports it.Thread Safety: Concurrent saves for the same
agentIdmust be serialized to prevent data corruption.- Specified by:
saveStatein interfacePersistenceService- Parameters:
agentId- the unique identifier of the agent, must not be null or blankstate- the state to persist, must not be null- Returns:
- a
CompletableFuturethat completes when the save is committed, or completes exceptionally withPersistenceExceptionon I/O or serialization failure - See Also:
-
loadState
Description copied from interface:PersistenceServiceLoads the persisted state of an agent asynchronously.Returns an empty
Optionalif no state has been saved for the givenagentId. Never returns a null future or a future containingnull.- Specified by:
loadStatein interfacePersistenceService- Parameters:
agentId- the unique identifier of the agent, must not be null or blank- Returns:
- a
CompletableFuturecontaining the saved state if present, or an emptyOptionalif not found; completes exceptionally withPersistenceExceptionon failure
-
deleteState
Description copied from interface:PersistenceServiceDeletes the persisted state of an agent asynchronously.Idempotency: If no state exists for the given
agentId, the operation succeeds silently (no-op).Snapshots: This method deletes only the current state. Snapshots created via
PersistenceService.createSnapshot(java.lang.String, java.lang.String)are not automatically removed; use implementation-specific cleanup orPersistenceService.listSnapshots(java.lang.String)to manage them.- Specified by:
deleteStatein interfacePersistenceService- Parameters:
agentId- the unique identifier of the agent, must not be null or blank- Returns:
- a
CompletableFuturethat completes when the state is deleted, or completes exceptionally withPersistenceExceptionon failure
-
existsState
Description copied from interface:PersistenceServiceChecks whether a persisted state exists for an agent.This is a lightweight probe that avoids deserializing the full state. Prefer this over
PersistenceService.loadState(java.lang.String)when you only need to know if state exists, not its content.- Specified by:
existsStatein interfacePersistenceService- Parameters:
agentId- the unique identifier of the agent, must not be null or blank- Returns:
- a
CompletableFuturecontainingtrueif state exists,falseotherwise; completes exceptionally withPersistenceExceptionon failure
-
createSnapshot
Description copied from interface:PersistenceServiceCreates a named snapshot of the current persisted state of an agent.A snapshot is an immutable copy of the state at the time of creation. It can be used to implement rollback, auditing, or checkpoint-based recovery. The current state (as saved via
PersistenceService.saveState(java.lang.String, dev.agenor.core.persistence.AgentState)) must exist before a snapshot can be created.If
snapshotIdisnullor blank, implementations should auto-generate a unique identifier (e.g., timestamp-based).Example:
String snapshotId = persistenceService .createSnapshot("order-processor-1", null) // auto-generated ID .join(); log.info("Snapshot created: {}", snapshotId);- Specified by:
createSnapshotin interfacePersistenceService- Parameters:
agentId- the unique identifier of the agent, must not be null or blanksnapshotId- an optional user-defined snapshot name; if null or blank, the implementation generates one- Returns:
- a
CompletableFuturecontaining the effective snapshot identifier; completes exceptionally withPersistenceExceptionif the current state does not exist or the operation fails - See Also:
-
restoreSnapshot
Description copied from interface:PersistenceServiceRestores an agent's state from a previously created snapshot.The restored state is returned but not automatically written back as the current state. Callers must explicitly call
PersistenceService.saveState(java.lang.String, dev.agenor.core.persistence.AgentState)if they want the snapshot to become the active state.Returns an empty
Optionalif no snapshot with the givensnapshotIdexists for the agent.Example:
persistenceService.restoreSnapshot("order-processor-1", snapshotId) .thenCompose(opt -> { if (opt.isPresent()) { agent.restoreState(opt.get()); return persistenceService.saveState(agent.getAgentId(), opt.get()); } return CompletableFuture.completedFuture(null); });- Specified by:
restoreSnapshotin interfacePersistenceService- Parameters:
agentId- the unique identifier of the agent, must not be null or blanksnapshotId- the identifier of the snapshot to restore, must not be null or blank- Returns:
- a
CompletableFuturecontaining the snapshot state if found, or an emptyOptionalif the snapshot does not exist; completes exceptionally withPersistenceExceptionon failure - See Also:
-
listSnapshots
Description copied from interface:PersistenceServiceReturns the list of snapshot identifiers available for an agent.The order of the returned identifiers is implementation-dependent. Returns an empty list if no snapshots exist; never returns
null.- Specified by:
listSnapshotsin interfacePersistenceService- Parameters:
agentId- the unique identifier of the agent, must not be null or blank- Returns:
- a
CompletableFuturecontaining the (possibly empty) list of snapshot identifiers; completes exceptionally withPersistenceExceptionon failure - See Also:
-
cleanupSnapshots
Clean up old snapshots, keeping only the most recent N -
getStats
Get statistics about persisted data
-