Skip to main content

ddd_toolkit_core/
async_snapshot.rs

1#![allow(clippy::type_complexity)]
2
3use crate::{AggregateRoot, AggregateSnapshot, Entity, SnapshotLoadError, SnapshotSaveError};
4use std::future::Future;
5use std::pin::Pin;
6
7/// **English:**<br>
8/// Asynchronous storage for snapshots, separate from event-stream appends.
9///
10/// Futures are boxed and `Send`, borrowing the store and identifier for `'a`.
11/// Adapters define replacement, durability, concurrency, panic conditions, and cancellation
12/// outcomes. This port does not verify snapshots against an event stream or provide an executor.
13///
14/// **日本語:**<br>
15/// イベントストリームへの追加とは別にスナップショットを非同期保存します。
16///
17/// Future はボックス化された `Send` で、ストアと識別子を `'a` の間借用します。
18/// 置換、永続性、並行処理、パニック条件、キャンセルの結果はアダプターが定めます。
19/// このポートはストリームに対するスナップショット検証や実行器を提供しません。
20pub trait AsyncSnapshotStore
21where
22    Self::State: Send,
23    Self::Error: Send,
24    <Self::Aggregate as Entity>::Id: Sync,
25{
26    /// **English:**<br>
27    /// Aggregate defining the snapshot identifier, whose type must be `Sync`.
28    ///
29    /// **日本語:**<br>
30    /// スナップショット識別子を定める集約です。識別子の型には `Sync` が必要です。
31    type Aggregate: AggregateRoot + Send;
32
33    /// **English:**<br>
34    /// Owned snapshot state transferable across threads.
35    ///
36    /// **日本語:**<br>
37    /// スレッド間で受け渡せる所有されたスナップショット状態です。
38    type State: Send;
39
40    /// **English:**<br>
41    /// Adapter failure carried by snapshot load and save errors.
42    ///
43    /// **日本語:**<br>
44    /// スナップショットの読み込み・保存エラーが保持するアダプター障害です。
45    type Error: Send;
46
47    /// **English:**<br>
48    /// Returns a future loading the snapshot for `id`; `Ok(None)` means absence.
49    /// Storage failures are reported through [`SnapshotLoadError::Adapter`].
50    ///
51    /// **日本語:**<br>
52    /// `id` のスナップショットを読み込む Future を返し、`Ok(None)` は不在を表します。
53    /// ストレージ障害は [`SnapshotLoadError::Adapter`] で報告します。
54    fn load<'a>(
55        &'a self,
56        id: &'a <Self::Aggregate as Entity>::Id,
57    ) -> Pin<
58        Box<
59            dyn Future<
60                    Output = Result<
61                        Option<AggregateSnapshot<Self::State>>,
62                        SnapshotLoadError<Self::Error>,
63                    >,
64                > + Send
65                + 'a,
66        >,
67    >;
68
69    /// **English:**<br>
70    /// Takes ownership of `snapshot` and returns a future saving it for `id`.
71    /// Completion is `Ok(())` or [`SnapshotSaveError::Adapter`]; the snapshot is not returned
72    /// on failure. Neither an error nor cancellation specifies whether storage changed.
73    ///
74    /// **日本語:**<br>
75    /// `snapshot` の所有権を受け取り、`id` に対して保存する Future を返します。
76    /// 結果は `Ok(())` または [`SnapshotSaveError::Adapter`] で、失敗時に値は返されません。
77    /// エラーやキャンセルだけではストレージが変更されたかどうかは決まりません。
78    fn save<'a>(
79        &'a mut self,
80        id: &'a <Self::Aggregate as Entity>::Id,
81        snapshot: AggregateSnapshot<Self::State>,
82    ) -> Pin<Box<dyn Future<Output = Result<(), SnapshotSaveError<Self::Error>>> + Send + 'a>>;
83}