Skip to main content

nstreams_core/
store.rs

1use async_trait::async_trait;
2
3use crate::event::{EventVersion, StreamEvent};
4use crate::namespace::Namespace;
5
6/// Persistent event storage keyed by namespace.
7#[async_trait]
8pub trait EventStore: Send + Sync {
9    /// Ensure storage exists for the namespace.
10    async fn ensure_namespace<N: Namespace>(&self, namespace: &N) -> crate::Result<()>;
11
12    /// Allocate the next monotonic version and persist the event atomically.
13    async fn persist_next<N: Namespace>(
14        &self,
15        namespace: &N,
16        payload: &[u8],
17        filter_value: Option<&str>,
18    ) -> crate::Result<StreamEvent>;
19
20    /// Load up to `limit` most recent events in ascending version order.
21    async fn load_history<N: Namespace>(
22        &self,
23        namespace: &N,
24        limit: u64,
25    ) -> crate::Result<Vec<StreamEvent>>;
26
27    /// Load events with version strictly greater than `after_version`.
28    async fn load_after_version<N: Namespace>(
29        &self,
30        namespace: &N,
31        after_version: EventVersion,
32        limit: u64,
33    ) -> crate::Result<Vec<StreamEvent>>;
34
35    /// Latest assigned version for the namespace, if any events exist.
36    async fn latest_version<N: Namespace>(
37        &self,
38        namespace: &N,
39    ) -> crate::Result<Option<EventVersion>>;
40}