Skip to main content

AggregateSnapshot

Struct AggregateSnapshot 

Source
pub struct AggregateSnapshot<S> { /* private fields */ }
Expand description

English:
Owned aggregate state paired with the last event revision it represents. No identifier or schema version is added; include validation metadata in S when needed.

日本語:
集約状態と、その状態が反映する最後のイベントのリビジョンを所有します。 識別子やスキーマバージョンは追加しません。検証用メタデータは必要に応じて S に含めます。

§Example / 使用例

use ddd_toolkit_core::{AggregateSnapshot, StreamRevision};
let snapshot = AggregateSnapshot::new(42_u64, StreamRevision::new(3));
assert_eq!(*snapshot.state(), 42);
assert_eq!(snapshot.into_parts(), (42, StreamRevision::new(3)));

Implementations§

Source§

impl<S> AggregateSnapshot<S>

Source

pub fn new(state: S, revision: StreamRevision) -> AggregateSnapshot<S>

English:
Pairs state with revision without checking consistency or compatibility.

日本語:
整合性や互換性を検証せず、state と revision を組み合わせます。

Examples found in repository?
examples/event_sourcing.rs (line 158)
151    fn load(
152        &self,
153        id: &u64,
154    ) -> Result<Option<AggregateSnapshot<u32>>, SnapshotLoadError<Infallible>> {
155        Ok(self
156            .0
157            .get(id)
158            .map(|snapshot| AggregateSnapshot::new(*snapshot.state(), snapshot.revision())))
159    }
160    fn save(
161        &mut self,
162        id: &u64,
163        snapshot: AggregateSnapshot<u32>,
164    ) -> Result<(), SnapshotSaveError<Infallible>> {
165        self.0.insert(*id, snapshot);
166        Ok(())
167    }
168}
169fn history(store: &MemoryEvents) -> StreamHistory<Added> {
170    let LoadResult::Found(history) = store.load(&1).unwrap() else {
171        panic!("stream missing");
172    };
173    history
174}
175fn main() {
176    let mut store = MemoryEvents::default();
177    assert!(matches!(store.load(&1).unwrap(), LoadResult::NoStream(_)));
178    let first = store
179        .append(&1, ExpectedRevision::NoStream(NoStream), vec![Added::V1(2)])
180        .unwrap();
181    assert_eq!(first.resulting_revision, StreamRevision::new(0));
182    let mut replayed = replay_aggregate::<Counter>(&1, &history(&store)).unwrap();
183    assert_eq!(replayed.aggregate.value, 2);
184    assert!(replayed.aggregate.pull_events().is_empty());
185    let mut snapshots = MemorySnapshots::default();
186    snapshots
187        .save(
188            &1,
189            AggregateSnapshot::new(replayed.aggregate.value, replayed.revision),
190        )
191        .unwrap();
192    store
193        .append(
194            &1,
195            ExpectedRevision::Revision(replayed.revision),
196            vec![Added::V2 { amount: 3 }],
197        )
198        .unwrap();
199    assert!(matches!(
200        store.append(
201            &1,
202            ExpectedRevision::Revision(replayed.revision),
203            vec![Added::V1(99)]
204        ),
205        Err(AppendError::Conflict(_))
206    ));
207    let history = history(&store);
208    let mut restored =
209        replay_with_optional_snapshot::<Counter>(&1, &history, snapshots.load(&1).unwrap())
210            .unwrap();
211    let full = replay_aggregate::<Counter>(&1, &history).unwrap();
212    assert_eq!(restored.aggregate.value, 5);
213    assert_eq!(restored.aggregate.value, full.aggregate.value);
214    assert_eq!(restored.revision, StreamRevision::new(1));
215    assert!(restored.aggregate.pull_events().is_empty());
216    println!("event_sourcing: upcast v1; rejected stale write; snapshot + tail = 5");
217}
Source

pub fn state(&self) -> &S

English:
Borrows the stored aggregate state.

日本語:
保存された集約状態を借用します。

Examples found in repository?
examples/event_sourcing.rs (line 158)
151    fn load(
152        &self,
153        id: &u64,
154    ) -> Result<Option<AggregateSnapshot<u32>>, SnapshotLoadError<Infallible>> {
155        Ok(self
156            .0
157            .get(id)
158            .map(|snapshot| AggregateSnapshot::new(*snapshot.state(), snapshot.revision())))
159    }
Source

pub fn revision(&self) -> StreamRevision

English:
Returns the last event revision represented by the state.

日本語:
状態が反映する最後のイベントのリビジョンを返します。

Examples found in repository?
examples/event_sourcing.rs (line 158)
151    fn load(
152        &self,
153        id: &u64,
154    ) -> Result<Option<AggregateSnapshot<u32>>, SnapshotLoadError<Infallible>> {
155        Ok(self
156            .0
157            .get(id)
158            .map(|snapshot| AggregateSnapshot::new(*snapshot.state(), snapshot.revision())))
159    }
Source

pub fn into_parts(self) -> (S, StreamRevision)

English:
Consumes the snapshot, returning state and revision without cloning.

日本語:
スナップショットを消費し、複製せずに状態とリビジョンを返します。

Examples found in repository?
examples/event_sourcing.rs (line 74)
71    fn from_snapshot(id: &u64, snapshot: AggregateSnapshot<u32>) -> Result<Self, Infallible> {
72        Ok(Self {
73            id: *id,
74            value: snapshot.into_parts().0,
75            events: vec![],
76        })
77    }

Trait Implementations§

Source§

impl<S> Debug for AggregateSnapshot<S>
where S: Debug,

Source§

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

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

impl<S> Eq for AggregateSnapshot<S>
where S: Eq,

Source§

impl<S> PartialEq for AggregateSnapshot<S>
where S: PartialEq,

Source§

fn eq(&self, other: &AggregateSnapshot<S>) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl<S> StructuralPartialEq for AggregateSnapshot<S>
where S: PartialEq,

Auto Trait Implementations§

§

impl<S> Freeze for AggregateSnapshot<S>
where S: Freeze,

§

impl<S> RefUnwindSafe for AggregateSnapshot<S>
where S: RefUnwindSafe,

§

impl<S> Send for AggregateSnapshot<S>
where S: Send,

§

impl<S> Sync for AggregateSnapshot<S>
where S: Sync,

§

impl<S> Unpin for AggregateSnapshot<S>
where S: Unpin,

§

impl<S> UnsafeUnpin for AggregateSnapshot<S>
where S: UnsafeUnpin,

§

impl<S> UnwindSafe for AggregateSnapshot<S>
where S: UnwindSafe,

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<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

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, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

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

fn try_from(value: U) -> Result<T, !>

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.