ddd_toolkit_core/async_event_store.rs
1#![allow(clippy::type_complexity)]
2
3use crate::{AggregateRoot, AppendError, AppendResult, Entity, ExpectedRevision, LoadResult};
4use std::future::Future;
5use std::pin::Pin;
6
7/// **English:** Asynchronous event-stream storage using the result types of [`crate::EventStore`].
8///
9/// Returned futures are `Send` and borrow the store and identifier for `'a`; no executor or
10/// synchronization is provided. Adapters define atomicity, durability, writer coordination,
11/// panic conditions, and the outcome of cancelling an operation by dropping its future.
12/// Cancellation does not by itself imply that writes were rolled back.
13///
14/// **日本語:** [`crate::EventStore`] と同じ結果型を使う非同期イベントストリームストアです。
15///
16/// 返される Future は `Send` で、ストアと識別子を `'a` の間借用します。
17/// 実行器や同期機構は提供しません。原子性、永続性、書き込みの調整、パニック条件、
18/// Future の破棄によるキャンセルの結果はアダプターが定めます。
19/// キャンセル自体は書き込みのロールバックを意味しません。
20pub trait AsyncEventStore
21where
22 <Self::Aggregate as AggregateRoot>::Event: Send,
23 <Self::Aggregate as Entity>::Id: Sync,
24{
25 /// **English:** Aggregate supplying the stream identifier and transferable event type.
26 /// Its identifier must be `Sync`, and its events must be `Send`.
27 ///
28 /// **日本語:** ストリーム識別子と受け渡し可能なイベント型を定める集約です。
29 /// 識別子には `Sync`、イベントには `Send` が必要です。
30 type Aggregate: AggregateRoot + Send;
31
32 /// **English:** Storage error transferable with the future's result.
33 ///
34 /// **日本語:** Future の結果とともに受け渡せるストレージエラーです。
35 type Error: Send;
36
37 /// **English:** Loads the complete history for `id` or returns [`LoadResult::NoStream`].
38 /// Storage failures use `Self::Error`, not the absent-stream result.
39 ///
40 /// **日本語:** `id` の完全な履歴、または [`LoadResult::NoStream`] を返します。
41 /// ストレージ障害はストリーム不在ではなく `Self::Error` で報告します。
42 fn load<'a>(
43 &'a self,
44 id: &'a <Self::Aggregate as Entity>::Id,
45 ) -> Pin<
46 Box<
47 dyn Future<
48 Output = Result<
49 LoadResult<<Self::Aggregate as AggregateRoot>::Event>,
50 Self::Error,
51 >,
52 > + Send
53 + 'a,
54 >,
55 >;
56
57 /// **English:** Takes ownership of `events` to append in vector order under `expected`.
58 /// The future returns the final revision or an [`AppendError`] for an empty batch,
59 /// revision conflict, revision exhaustion, or adapter failure. The batch is not returned
60 /// on error; partial-write and retry behavior must be documented by the adapter.
61 ///
62 /// **日本語:** `events` の所有権を受け取り、`expected` を条件にベクター順で追加します。
63 /// Future は最終リビジョン、または空バッチ、リビジョン競合、リビジョン枯渇、
64 /// アダプター障害を示す [`AppendError`] を返します。エラー時にバッチは返されません。
65 /// 部分書き込みと再試行の挙動はアダプターが文書化する必要があります。
66 fn append<'a>(
67 &'a mut self,
68 id: &'a <Self::Aggregate as Entity>::Id,
69 expected: ExpectedRevision,
70 events: Vec<<Self::Aggregate as AggregateRoot>::Event>,
71 ) -> Pin<Box<dyn Future<Output = Result<AppendResult, AppendError<Self::Error>>> + Send + 'a>>;
72}