Interface SnapshotPolicy
public interface SnapshotPolicy
Defines a policy for determining when an event-sourced entity should be snapshotted.
A SnapshotPolicy encapsulates the logic that decides whether a snapshot
is required based on events seen and metrics collected during sourcing.
A SnapshotPolicy can trigger snapshotting in two ways:
- During sourcing, by inspecting each event through
shouldSnapshot(EventMessage) - After sourcing completes, by evaluating the
EvolutionResultthroughshouldSnapshot(EvolutionResult)
Policies are immutable and thread-safe, so a single instance can be shared across multiple entities and asynchronous operations.
Policies can be composed using or(SnapshotPolicy) to combine multiple conditions.
Typical usage:
SnapshotPolicy policy = SnapshotPolicy.afterEvents(100)
.or(SnapshotPolicy.whenSourcingTimeExceeds(Duration.ofMillis(50)));
- Since:
- 5.1.0
- Author:
- John Hendrikx
-
Method Summary
Modifier and TypeMethodDescriptionstatic SnapshotPolicyafterEvents(int eventCount) Creates a policy that triggers a snapshot once more than the specified number of events have been applied since the last snapshot.default SnapshotPolicyor(SnapshotPolicy other) Composes this policy with another policy using logical OR.booleanshouldSnapshot(EvolutionResult evolutionResult) Allows triggering a snapshot when sourcing completes, based on the final evolution result.default booleanshouldSnapshot(EventMessage event) Allows triggering a snapshot when sourcing completes, based on an event seen during sourcing.static SnapshotPolicywhenEventMatches(Predicate<EventMessage> predicate) Creates a policy that triggers a snapshot when the predicate matches an event encountered during sourcing.static SnapshotPolicywhenSourcingTimeExceeds(Duration duration) Creates a policy that triggers a snapshot if the sourcing time of the entity exceeds the specified duration.
-
Method Details
-
afterEvents
Creates a policy that triggers a snapshot once more than the specified number of events have been applied since the last snapshot.- Parameters:
eventCount- the minimum number of events after which a snapshot should be made, cannot be negative- Returns:
- a snapshot policy based on event count, never
null - Throws:
IllegalArgumentException- ifeventCountis a negative number
-
whenSourcingTimeExceeds
Creates a policy that triggers a snapshot if the sourcing time of the entity exceeds the specified duration.- Parameters:
duration- the maximum sourcing duration before a snapshot is triggered, cannot benull- Returns:
- a snapshot policy based on sourcing time, never
null - Throws:
NullPointerException- ifdurationisnull
-
whenEventMatches
Creates a policy that triggers a snapshot when the predicate matches an event encountered during sourcing.- Parameters:
predicate- an event message predicate, cannot benull- Returns:
- a snapshot policy that triggers when the predicate matches, never
null - Throws:
NullPointerException- whenpredicateisnull
-
shouldSnapshot
Allows triggering a snapshot when sourcing completes, based on the final evolution result.- Parameters:
evolutionResult- information about the sourcing process to base the decision on, cannot benull- Returns:
trueif a snapshot should be made,falseotherwise- Implementation Note:
- implementations should be thread-safe and side-effect free
-
shouldSnapshot
Allows triggering a snapshot when sourcing completes, based on an event seen during sourcing.- Parameters:
event- the event seen, nevernull- Returns:
trueif a snapshot should be created when sourcing completes, otherwisefalse- Implementation Note:
- implementations should be thread-safe and side-effect free
-
or
Composes this policy with another policy using logical OR.The resulting policy triggers a snapshot if either this policy or the other policy requires it.
- Parameters:
other- another snapshot policy, cannot benull- Returns:
- a new
SnapshotPolicyrepresenting the logical OR of this and the other, nevernull - Throws:
NullPointerException- ifotherisnull
-