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