pub struct Snapshot {
pub stream_id: StreamId,
pub sequence_number: u64,
pub state_schema_version: u32,
pub state: Value,
}Expand description
A point-in-time snapshot of an aggregate’s state.
A snapshot carries the serialized state at a specific sequence_number.
During state reconstruction, the engine loads the snapshot (if any) and
then replays only the events that arrived after it.
§Schema versioning
state_schema_version mirrors EventEnvelope::schema_version. Increment
it when the serialized state layout changes incompatibly. The engine must
discard snapshots whose schema version it does not recognise.
Fields§
§stream_id: StreamIdThe stream this snapshot covers.
sequence_number: u64The sequence number of the last event incorporated into this snapshot.
Events with a higher sequence number must still be replayed on top.
state_schema_version: u32Schema version of the serialized state payload.
state: ValueThe serialized aggregate state at sequence_number.
Stored as serde_json::Value so the engine layer remains
domain-agnostic; the domain crate deserializes it into the concrete
Workflow::State type.
Implementations§
Source§impl Snapshot
impl Snapshot
Sourcepub fn new(
stream_id: StreamId,
sequence_number: u64,
state_schema_version: u32,
state: Value,
) -> Self
pub fn new( stream_id: StreamId, sequence_number: u64, state_schema_version: u32, state: Value, ) -> Self
Construct a new snapshot.
Sourcepub fn should_take(
event_count: u64,
last_snapshot_at: u64,
interval: u64,
) -> bool
pub fn should_take( event_count: u64, last_snapshot_at: u64, interval: u64, ) -> bool
Return true when a snapshot should be taken.
Returns true when event_count - last_snapshot_at >= interval,
i.e. at least interval new events have accumulated since the last
snapshot. This avoids the exact-multiple trap: if snapshotting is
skipped at count 100, it still triggers at 101, 102, etc. until a
snapshot is taken.
Set last_snapshot_at to 0 when no snapshot has ever been taken.
Returns false when interval is 0 (snapshotting disabled).
§Example
use mako_engine::snapshot::Snapshot;
// First snapshot: no prior snapshot (last_snapshot_at = 0).
assert!(!Snapshot::should_take(99, 0, 100));
assert!( Snapshot::should_take(100, 0, 100));
assert!( Snapshot::should_take(101, 0, 100)); // non-exact still triggers
// After a snapshot at seq 100, next triggers at 200+.
assert!(!Snapshot::should_take(101, 100, 100));
assert!( Snapshot::should_take(200, 100, 100));
assert!( Snapshot::should_take(201, 100, 100));
// interval = 0 disables snapshotting.
assert!(!Snapshot::should_take(1000, 0, 0));