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:

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 Type
    Method
    Description
    afterEvents(int eventCount)
    Creates a policy that triggers a snapshot once more than the specified number of events have been applied since the last snapshot.
    Composes this policy with another policy using logical OR.
    boolean
    Allows triggering a snapshot when sourcing completes, based on the final evolution result.
    default boolean
    Allows triggering a snapshot when sourcing completes, based on an event seen during sourcing.
    Creates a policy that triggers a snapshot when the predicate matches an event encountered during sourcing.
    Creates a policy that triggers a snapshot if the sourcing time of the entity exceeds the specified duration.
  • Method Details

    • afterEvents

      static SnapshotPolicy afterEvents(int eventCount)
      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 - if eventCount is a negative number
    • whenSourcingTimeExceeds

      static SnapshotPolicy whenSourcingTimeExceeds(Duration duration)
      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 be null
      Returns:
      a snapshot policy based on sourcing time, never null
      Throws:
      NullPointerException - if duration is null
    • whenEventMatches

      static SnapshotPolicy whenEventMatches(Predicate<EventMessage> predicate)
      Creates a policy that triggers a snapshot when the predicate matches an event encountered during sourcing.
      Parameters:
      predicate - an event message predicate, cannot be null
      Returns:
      a snapshot policy that triggers when the predicate matches, never null
      Throws:
      NullPointerException - when predicate is null
    • shouldSnapshot

      boolean shouldSnapshot(EvolutionResult evolutionResult)
      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 be null
      Returns:
      true if a snapshot should be made, false otherwise
      Implementation Note:
      implementations should be thread-safe and side-effect free
    • shouldSnapshot

      default boolean shouldSnapshot(EventMessage event)
      Allows triggering a snapshot when sourcing completes, based on an event seen during sourcing.
      Parameters:
      event - the event seen, never null
      Returns:
      true if a snapshot should be created when sourcing completes, otherwise false
      Implementation Note:
      implementations should be thread-safe and side-effect free
    • or

      default SnapshotPolicy or(SnapshotPolicy other)
      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 be null
      Returns:
      a new SnapshotPolicy representing the logical OR of this and the other, never null
      Throws:
      NullPointerException - if other is null