Interface SnapshotStore
- All Superinterfaces:
Namespaced<SnapshotStore>
- All Known Implementing Classes:
DefaultSnapshotStore, NoOpSnapshotStore
Snapshots are serialized representations of an aggregate's state at a certain point in time (i.e., after a certain sequence number). They allow the system to avoid replaying the entire event history for aggregates with large event logs.
This interface defines methods for storing, loading, and deleting such snapshots. Snapshots are typically
used in combination with AggregateRepository and
EventStore to optimize aggregate loading.
Usage of this interface is optional—if no snapshot is found, the aggregate is reconstructed from its full event history.
-
Method Summary
Modifier and TypeMethodDescriptiondeleteSnapshot(Object aggregateId) Deletes the snapshot for the specified aggregate ID.default SnapshotStoreforNamespace(String namespace) Returns this snapshot store scoped to the requested namespace.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.Methods inherited from interface Namespaced
forApplicationNamespace, forDefaultNamespace
-
Method Details
-
forNamespace
Returns this snapshot store scoped to the requested namespace.Implementations that do not use namespace-aware backing storage may keep returning the same instance. The default implementation switches both its document and event stores to the requested namespace.
- Specified by:
forNamespacein interfaceNamespaced<SnapshotStore>- Parameters:
namespace- the namespace to which the returned store is scoped- Returns:
- the snapshot store associated with the specified namespace
-
storeSnapshot
Stores 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.
- 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
Retrieves the most recent snapshot for a given aggregate ID, if available.- 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
Retrieves the most recent snapshot with a sequence number strictly lower than the given value.- 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
Deletes the snapshot for the specified aggregate ID.- Parameters:
aggregateId- The ID of the aggregate whose snapshot should be deleted.- Returns:
- A
CompletableFutureindicating completion of the deletion operation.
-