Skip to main content

azoth_core/traits/
projection.rs

1use crate::error::Result;
2use crate::types::EventId;
3use std::path::Path;
4
5/// Transaction for projection store operations
6///
7/// Note: Not required to be Send, as some backends have thread-affine transactions
8pub trait ProjectionTxn {
9    /// Apply a single event to the projection
10    fn apply_event(&mut self, id: EventId, bytes: &[u8]) -> Result<()>;
11
12    /// Apply a batch of events to the projection
13    fn apply_batch(&mut self, events: &[(EventId, Vec<u8>)]) -> Result<()> {
14        for (id, bytes) in events {
15            self.apply_event(*id, bytes)?;
16        }
17        Ok(())
18    }
19
20    /// Commit transaction and update cursor
21    fn commit(self: Box<Self>, new_cursor: EventId) -> Result<()>;
22
23    /// Rollback transaction
24    fn rollback(self: Box<Self>);
25}
26
27/// Projection store: SQL database for derived tables
28///
29/// Provides:
30/// - Schema management and migrations
31/// - Cursor tracking for event application
32/// - Transactional batch application
33/// - Backup and restore
34pub trait ProjectionStore: Send + Sync {
35    type Txn<'a>: ProjectionTxn
36    where
37        Self: 'a;
38
39    /// Open a projection store
40    fn open(cfg: crate::config::ProjectionConfig) -> Result<Self>
41    where
42        Self: Sized;
43
44    /// Close the store
45    fn close(&self) -> Result<()>;
46
47    /// Begin a transaction
48    fn begin_txn(&self) -> Result<Self::Txn<'_>>;
49
50    /// Get the current cursor (last applied event ID)
51    fn get_cursor(&self) -> Result<EventId>;
52
53    /// Run migrations to target schema version
54    fn migrate(&self, target_version: u32) -> Result<()>;
55
56    /// Create a backup
57    fn backup_to(&self, path: &Path) -> Result<()>;
58
59    /// Restore from a backup
60    fn restore_from(path: &Path, cfg: crate::config::ProjectionConfig) -> Result<Self>
61    where
62        Self: Sized;
63
64    /// Get the schema version
65    fn schema_version(&self) -> Result<u32>;
66}