1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
use super::BatchAppendTicket;
use crate::coordinate::Coordinate;
use crate::event::{EventKind, EventPayload};
use crate::store::{
AppendOptions, AppendReceipt, BatchAppendItem, CausationRef, Open, Store, StoreError,
};
use serde::Serialize;
/// Producer-side staging buffer for batch submission.
pub struct Outbox<'a> {
store: &'a Store<Open>,
fence_token: Option<u64>,
items: Vec<BatchAppendItem>,
}
impl<'a> Outbox<'a> {
pub(crate) fn new(store: &'a Store<Open>, fence_token: Option<u64>) -> Self {
Self {
store,
fence_token,
items: Vec::new(),
}
}
/// Stage a new batch item with default append options and no causation.
///
/// # Errors
/// Returns any serialization or validation error raised while converting
/// the payload into a staged [`BatchAppendItem`].
pub fn stage(
&mut self,
coord: Coordinate,
kind: EventKind,
payload: &impl Serialize,
) -> Result<&mut Self, StoreError> {
self.stage_with_options_and_causation(
coord,
kind,
payload,
AppendOptions::default(),
CausationRef::None,
)
}
/// Stage a new batch item with explicit append options.
///
/// # Errors
/// Returns any serialization or validation error raised while converting
/// the payload into a staged [`BatchAppendItem`].
pub fn stage_with_options(
&mut self,
coord: Coordinate,
kind: EventKind,
payload: &impl Serialize,
options: AppendOptions,
) -> Result<&mut Self, StoreError> {
self.stage_with_options_and_causation(coord, kind, payload, options, CausationRef::None)
}
/// Stage a new batch item with explicit causation and default append options.
///
/// # Errors
/// Returns any serialization or validation error raised while converting
/// the payload into a staged [`BatchAppendItem`].
pub fn stage_with_causation(
&mut self,
coord: Coordinate,
kind: EventKind,
payload: &impl Serialize,
causation: CausationRef,
) -> Result<&mut Self, StoreError> {
self.stage_with_options_and_causation(
coord,
kind,
payload,
AppendOptions::default(),
causation,
)
}
/// Stage a new batch item with explicit append options and causation.
///
/// # Errors
/// Returns any serialization or validation error raised while converting
/// the payload into a staged [`BatchAppendItem`].
pub fn stage_with_options_and_causation(
&mut self,
coord: Coordinate,
kind: EventKind,
payload: &impl Serialize,
options: AppendOptions,
causation: CausationRef,
) -> Result<&mut Self, StoreError> {
let item = BatchAppendItem::new(coord, kind, payload, options, causation)?;
self.items.push(item);
Ok(self)
}
/// Stage a new batch item with a typed payload — kind derived from `T::KIND`.
///
/// # Errors
/// Returns any serialization or validation error raised while converting
/// the payload into a staged [`BatchAppendItem`].
pub fn stage_typed<T: EventPayload>(
&mut self,
coord: Coordinate,
payload: &T,
) -> Result<&mut Self, StoreError> {
self.stage_with_options_and_causation(
coord,
T::KIND,
payload,
AppendOptions::default(),
CausationRef::None,
)
}
/// Stage a typed batch item with explicit append options — kind derived from `T::KIND`.
///
/// # Errors
/// Returns any serialization or validation error raised while converting
/// the payload into a staged [`BatchAppendItem`].
pub fn stage_typed_with_options<T: EventPayload>(
&mut self,
coord: Coordinate,
payload: &T,
options: AppendOptions,
) -> Result<&mut Self, StoreError> {
self.stage_with_options_and_causation(coord, T::KIND, payload, options, CausationRef::None)
}
/// Stage a typed batch item with explicit causation — kind derived from `T::KIND`.
///
/// # Errors
/// Returns any serialization or validation error raised while converting
/// the payload into a staged [`BatchAppendItem`].
pub fn stage_typed_with_causation<T: EventPayload>(
&mut self,
coord: Coordinate,
payload: &T,
causation: CausationRef,
) -> Result<&mut Self, StoreError> {
self.stage_with_options_and_causation(
coord,
T::KIND,
payload,
AppendOptions::default(),
causation,
)
}
/// Stage a typed batch item with explicit append options and causation — kind derived from `T::KIND`.
///
/// # Errors
/// Returns any serialization or validation error raised while converting
/// the payload into a staged [`BatchAppendItem`].
pub fn stage_typed_with_options_and_causation<T: EventPayload>(
&mut self,
coord: Coordinate,
payload: &T,
options: AppendOptions,
causation: CausationRef,
) -> Result<&mut Self, StoreError> {
self.stage_with_options_and_causation(coord, T::KIND, payload, options, causation)
}
/// Stage a fully-formed batch item.
pub fn push_item(&mut self, item: BatchAppendItem) -> &mut Self {
self.items.push(item);
self
}
/// Drain the staged items into a blocking batch append.
///
/// Staged items are consumed from this [`Outbox`] before the enqueue/write
/// path runs. Callers that need retry-after-error behavior must retain
/// their own copy of the batch contents.
///
/// # Errors
/// Returns any enqueue, writer, fence, or batch-append error surfaced by
/// the underlying flush path.
pub fn flush(&mut self) -> Result<Vec<AppendReceipt>, StoreError> {
let items = std::mem::take(&mut self.items);
match self.fence_token {
Some(token) => self.store.submit_batch_with_fence(items, token)?.wait(),
None => self.store.append_batch(items),
}
}
/// Drain the staged items into a nonblocking batch submission.
///
/// Staged items are consumed from this [`Outbox`] before the submission is
/// attempted. Callers that need retry-after-error behavior must retain
/// their own copy of the batch contents.
///
/// # Errors
/// Returns any enqueue, writer, or fence error surfaced while turning the
/// staged items into a batch submission ticket.
pub fn submit_flush(&mut self) -> Result<BatchAppendTicket, StoreError> {
let items = std::mem::take(&mut self.items);
match self.fence_token {
Some(token) => self.store.submit_batch_with_fence(items, token),
None => self.store.submit_batch(items),
}
}
/// Number of currently staged items.
pub fn len(&self) -> usize {
self.items.len()
}
/// True when no items are staged.
pub fn is_empty(&self) -> bool {
self.items.is_empty()
}
}