Skip to main content

Snapshot

Struct Snapshot 

Source
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: StreamId

The stream this snapshot covers.

§sequence_number: u64

The 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: u32

Schema version of the serialized state payload.

§state: Value

The 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

Source

pub fn new( stream_id: StreamId, sequence_number: u64, state_schema_version: u32, state: Value, ) -> Self

Construct a new snapshot.

Source

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));

Trait Implementations§

Source§

impl Clone for Snapshot

Source§

fn clone(&self) -> Snapshot

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Snapshot

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<'de> Deserialize<'de> for Snapshot

Source§

fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl Serialize for Snapshot

Source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more