Class PeriodicSnapshotTrigger

java.lang.Object
io.fluxcapacitor.javaclient.persisting.eventsourcing.PeriodicSnapshotTrigger
All Implemented Interfaces:
SnapshotTrigger

public class PeriodicSnapshotTrigger extends Object implements SnapshotTrigger
A SnapshotTrigger that triggers snapshot creation at fixed intervals based on the aggregate's sequence number.

This is the default strategy used when a snapshot period is specified, e.g., via @Aggregate(snapshotPeriod = 100). It ensures that a snapshot is created every period events.

For example, if the period is 100:

  • A snapshot is created after event 100 (sequence number 99)
  • Then again after event 200, 300, and so on.

This is a useful strategy for reducing the number of events that need to be replayed when rehydrating an aggregate, improving performance over time without incurring snapshot overhead after every event.

See Also:
  • Constructor Details

    • PeriodicSnapshotTrigger

      public PeriodicSnapshotTrigger(int period)
      Constructs a periodic snapshot trigger with the given interval.
      Parameters:
      period - The number of events between snapshots. Must be ≥ 1.
      Throws:
      IllegalArgumentException - if period is less than 1.
  • Method Details

    • shouldCreateSnapshot

      public boolean shouldCreateSnapshot(Entity<?> model, List<AppliedEvent> newEvents)
      Determines whether a new snapshot should be created. A snapshot is triggered if the current period index has increased compared to the period index before the latest batch of events was applied.
      Specified by:
      shouldCreateSnapshot in interface SnapshotTrigger
      Parameters:
      model - The current aggregate instance.
      newEvents - The list of newly applied (but not yet committed) events.
      Returns:
      true if a snapshot should be created, false otherwise.
    • periodIndex

      protected long periodIndex(long sequenceNumber)