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
use super::*;
impl<State> Store<State> {
/// READ: get a single event by ID.
///
/// # Errors
/// Returns `StoreError::NotFound` if no event with that ID exists.
/// Returns `StoreError::Io` or `StoreError::Serialization` if reading from disk fails.
pub fn get(&self, event_id: u128) -> Result<StoredEvent<serde_json::Value>, StoreError> {
let entry = self
.index
.get_by_id(event_id)
.ok_or(StoreError::NotFound(event_id))?;
self.reader.read_entry(&entry.disk_pos)
}
/// READ: fetch a single event by ID with the payload left as raw
/// MessagePack bytes. Mirrors [`get`](Self::get) but skips the
/// JSON-decode step, suitable for the `RawMsgpackInput` lane of a
/// multi-event reactor.
///
/// # Errors
/// Returns `StoreError::NotFound` if no event with that ID exists.
/// Returns `StoreError::Io` or `StoreError::Serialization` if reading
/// from disk fails.
pub fn get_raw(&self, event_id: u128) -> Result<StoredEvent<Vec<u8>>, StoreError> {
let entry = self
.index
.get_by_id(event_id)
.ok_or(StoreError::NotFound(event_id))?;
self.reader.read_entry_raw(&entry.disk_pos)
}
/// Verify an append receipt against the store's signing-key registry and
/// current index state.
#[must_use]
pub fn verify_append_receipt(&self, receipt: &AppendReceipt) -> bool {
let Some(entry) = self.index.get_by_id(receipt.event_id) else {
return false;
};
self.runtime.signing_registry.verify_append_receipt(
receipt,
&entry.coord,
entry.kind,
entry.hash_chain.prev_hash,
)
}
/// Verify a persisted denial receipt against the store's signing-key
/// registry and current index state.
#[must_use]
pub fn verify_denial_receipt(&self, receipt: &DenialReceipt) -> bool {
let Some(entry) = self.index.get_by_id(receipt.event_id) else {
return false;
};
self.runtime.signing_registry.verify_denial_receipt(
receipt,
&entry.coord,
entry.kind,
entry.hash_chain.prev_hash,
)
}
/// READ: query by Region.
#[must_use]
pub fn query(&self, region: &Region) -> Vec<IndexEntry> {
self.index.query(region)
}
/// READ: walk hash chain ancestors.
pub fn walk_ancestors(
&self,
event_id: u128,
limit: usize,
) -> Vec<StoredEvent<serde_json::Value>> {
ancestry::walk_ancestors(self, event_id, limit)
}
/// PROJECT: reconstruct typed state from events, with cache support.
///
/// # Errors
/// Returns any replay, deserialization, cache, or disk-read error surfaced
/// while reconstructing the projection state.
pub fn project<T>(&self, entity: &str, freshness: &Freshness) -> Result<Option<T>, StoreError>
where
T: EventSourced + serde::Serialize + serde::de::DeserializeOwned + 'static,
T::Input: projection::flow::ReplayInput,
{
projection::flow::project(self, entity, freshness)
}
/// Return the current per-entity generation if the entity exists.
///
/// Generations advance monotonically on every insert for that entity.
/// When entity-group overlays are disabled, this falls back to the entity
/// stream length so callers still get a stable monotonic skip token.
pub fn entity_generation(&self, entity: &str) -> Option<u64> {
self.index.entity_generation(entity)
}
/// Project only when the entity changed since `last_seen_generation`.
///
/// Returns `Ok(None)` when no change is observed. Otherwise returns the
/// generation at which the returned state was materialized together with
/// the freshly projected state. The returned generation is honest: a
/// cache-hit path returns the generation at which the cache was
/// stamped, a replay path returns the generation sampled before replay
/// started. Callers who persist this generation as a watermark (e.g.
/// [`ProjectionWatcher`]) will not silently consume a relevant append
/// against stale state (F5). To preserve that property, this API treats
/// [`Freshness::MaybeStale`] the same as [`Freshness::Consistent`].
///
/// # Errors
/// Returns any error surfaced by [`Store::project`] when the entity has
/// changed and the projection must be rebuilt.
pub fn project_if_changed<T>(
&self,
entity: &str,
last_seen_generation: u64,
freshness: &Freshness,
) -> Result<Option<(u64, Option<T>)>, StoreError>
where
T: EventSourced + serde::Serialize + serde::de::DeserializeOwned + 'static,
T::Input: projection::flow::ReplayInput,
{
projection::flow::project_if_changed(self, entity, last_seen_generation, freshness)
}
/// CONVENIENCE: sugar over index.stream() for exact entity match.
#[must_use]
pub fn stream(&self, entity: &str) -> Vec<IndexEntry> {
self.index.stream(entity)
}
/// READ: query all events in the given scope.
#[must_use]
pub fn by_scope(&self, scope: &str) -> Vec<IndexEntry> {
self.query(&Region::scope(scope))
}
/// READ: query all events of the given event kind across all entities and scopes.
#[must_use]
pub fn by_fact(&self, kind: EventKind) -> Vec<IndexEntry> {
self.query(&Region::all().with_fact(KindFilter::Exact(kind)))
}
/// READ (typed): query all events whose kind matches `T::KIND`.
///
/// Available on both `Store<Open>` and `Store<ReadOnly>`.
#[must_use]
pub fn by_fact_typed<T: EventPayload>(&self) -> Vec<IndexEntry> {
self.by_fact(T::KIND)
}
/// CURSOR: pull-based, ordered delivery from the in-memory index.
///
/// Available on both `Store<Open>` and `Store<ReadOnly>`. This cursor is
/// process-local only: it does not persist its position, so restart-time
/// at-least-once semantics require the checkpoint-bound cursor worker
/// surface rather than this constructor.
pub fn cursor_guaranteed(&self, region: &Region) -> Cursor {
Cursor::new(region.clone(), Arc::clone(&self.index))
}
}