allframe_core/cqrs/
backend.rs

1//! Event store backend abstraction
2//!
3//! This module provides a trait-based abstraction for event store backends,
4//! allowing AllFrame to support multiple storage implementations including
5//! in-memory (for testing/MVP) and AllSource Core (for production).
6
7use async_trait::async_trait;
8
9use super::Event;
10
11/// Backend trait for event storage implementations
12#[async_trait]
13pub trait EventStoreBackend<E: Event>: Send + Sync {
14    /// Append events to an aggregate's event stream
15    async fn append(&self, aggregate_id: &str, events: Vec<E>) -> Result<(), String>;
16
17    /// Get all events for a specific aggregate
18    async fn get_events(&self, aggregate_id: &str) -> Result<Vec<E>, String>;
19
20    /// Get all events from all aggregates (for projection rebuild)
21    async fn get_all_events(&self) -> Result<Vec<E>, String>;
22
23    /// Get events after a specific version (for snapshot optimization)
24    async fn get_events_after(&self, aggregate_id: &str, version: u64) -> Result<Vec<E>, String>;
25
26    /// Save a snapshot (optional, return Ok(()) if not supported)
27    async fn save_snapshot(
28        &self,
29        aggregate_id: &str,
30        snapshot_data: Vec<u8>,
31        version: u64,
32    ) -> Result<(), String> {
33        let _ = (aggregate_id, snapshot_data, version);
34        Ok(()) // Default: no-op
35    }
36
37    /// Get latest snapshot (optional, return Err if not supported)
38    async fn get_latest_snapshot(&self, aggregate_id: &str) -> Result<(Vec<u8>, u64), String> {
39        let _ = aggregate_id;
40        Err("Snapshots not supported by this backend".to_string())
41    }
42
43    /// Flush any pending writes (optional, for write-ahead log or batching)
44    async fn flush(&self) -> Result<(), String> {
45        Ok(()) // Default: no-op
46    }
47
48    /// Get backend statistics
49    async fn stats(&self) -> BackendStats {
50        BackendStats::default()
51    }
52}
53
54/// Backend statistics
55#[derive(Debug, Clone, Default)]
56pub struct BackendStats {
57    /// Total number of events stored
58    pub total_events: u64,
59    /// Total number of aggregates
60    pub total_aggregates: u64,
61    /// Total number of snapshots
62    pub total_snapshots: u64,
63    /// Backend-specific stats
64    pub backend_specific: std::collections::HashMap<String, String>,
65}