Class DefaultSnapshotStore
java.lang.Object
io.fluxzero.sdk.persisting.eventsourcing.DefaultSnapshotStore
- All Implemented Interfaces:
SnapshotStore
Default implementation of the
SnapshotStore interface.
Snapshots are stored as documents, keyed by aggregate id and sequence number. The latest snapshot is loaded by
sorting on the stored sequence number, while older snapshots can be retrieved to support historical
Entity.previous() traversal.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprotected static final record -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptiondeleteSnapshot(Object aggregateId) Deletes the snapshot for the specified aggregate ID.findSnapshot(Object aggregateId, Long sequenceNumberExclusive) getSnapshot(Object aggregateId) Retrieves the most recent snapshot for a given aggregate ID, if available.getSnapshotBefore(Object aggregateId, long sequenceNumberExclusive) Retrieves the most recent snapshot with a sequence number strictly lower than the given value.<T> CompletableFuture<Void> storeSnapshot(Entity<T> snapshot) Stores a new snapshot for the given aggregate entity.protected CompletableFuture<Void> trimSnapshots(Entity<?> snapshot)
-
Constructor Details
-
DefaultSnapshotStore
public DefaultSnapshotStore()
-
-
Method Details
-
storeSnapshot
Description copied from interface:SnapshotStoreStores a new snapshot for the given aggregate entity.The snapshot typically contains the latest known state and metadata such as the aggregate ID and sequence number. Storing a snapshot will overwrite any existing snapshot for the same aggregate ID.
- Specified by:
storeSnapshotin interfaceSnapshotStore- Type Parameters:
T- The aggregate root type.- Parameters:
snapshot- The aggregate entity to be stored as a snapshot.- Returns:
- A
CompletableFutureindicating whether the operation completed successfully.
-
getSnapshot
Description copied from interface:SnapshotStoreRetrieves the most recent snapshot for a given aggregate ID, if available.- Specified by:
getSnapshotin interfaceSnapshotStore- Type Parameters:
T- The expected type of the aggregate.- Parameters:
aggregateId- The ID of the aggregate for which to retrieve a snapshot.- Returns:
- An
Optionalcontaining the aggregate snapshot if present, otherwise empty.
-
getSnapshotBefore
Description copied from interface:SnapshotStoreRetrieves the most recent snapshot with a sequence number strictly lower than the given value.- Specified by:
getSnapshotBeforein interfaceSnapshotStore- Type Parameters:
T- The expected aggregate type.- Parameters:
aggregateId- The aggregate identifier.sequenceNumberExclusive- Upper bound (exclusive) for the snapshot sequence number.- Returns:
- An
Optionalcontaining the matching snapshot if present.
-
deleteSnapshot
Description copied from interface:SnapshotStoreDeletes the snapshot for the specified aggregate ID.- Specified by:
deleteSnapshotin interfaceSnapshotStore- Parameters:
aggregateId- The ID of the aggregate whose snapshot should be deleted.- Returns:
- A
CompletableFutureindicating completion of the deletion operation.
-
findSnapshot
-
trimSnapshots
-
deserializeSnapshot
protected <T> Optional<Entity<T>> deserializeSnapshot(DefaultSnapshotStore.SnapshotDocument document)
-