clasp_journal/journal.rs
1//! Journal trait definition
2
3use async_trait::async_trait;
4use clasp_core::SignalType;
5
6use crate::entry::{JournalEntry, ParamSnapshot};
7use crate::error::Result;
8
9/// Event journal for recording state changes and events.
10///
11/// The journal provides append-only storage for all state mutations,
12/// enabling crash recovery, state replay, and federation sync.
13#[async_trait]
14pub trait Journal: Send + Sync {
15 /// Append an entry to the journal. Returns the assigned sequence number.
16 async fn append(&self, entry: JournalEntry) -> Result<u64>;
17
18 /// Query entries matching a pattern within a time range.
19 async fn query(
20 &self,
21 pattern: &str,
22 from: Option<u64>,
23 to: Option<u64>,
24 limit: Option<u32>,
25 types: &[SignalType],
26 ) -> Result<Vec<JournalEntry>>;
27
28 /// Get entries since a given sequence number.
29 async fn since(&self, seq: u64, limit: Option<u32>) -> Result<Vec<JournalEntry>>;
30
31 /// Get the latest sequence number.
32 async fn latest_seq(&self) -> Result<u64>;
33
34 /// Save a state snapshot for faster recovery.
35 async fn snapshot(&self, state: &[ParamSnapshot]) -> Result<u64>;
36
37 /// Load the most recent snapshot.
38 async fn load_snapshot(&self) -> Result<Option<Vec<ParamSnapshot>>>;
39
40 /// Remove entries older than the given sequence number.
41 /// Returns the number of entries removed.
42 async fn compact(&self, before_seq: u64) -> Result<u64>;
43
44 /// Get the total number of entries in the journal.
45 async fn len(&self) -> Result<usize>;
46
47 /// Check whether the journal is empty.
48 async fn is_empty(&self) -> Result<bool> {
49 Ok(self.len().await? == 0)
50 }
51}