//! Application state shared across all request handlers. use std::sync::Arc; use tokio::sync::Mutex; use stemedb_query::QueryEngine; use stemedb_storage::{ GenericAdmissionStore, GenericAliasStore, GenericEscalationStore, GenericQuotaStore, GenericTrustRankStore, HybridStore, }; use stemedb_wal::group_commit::{GroupCommitBuffer, GroupCommitConfig}; use stemedb_wal::Journal; /// Quota store type alias for convenience. pub type QuotaStoreImpl = GenericQuotaStore>; /// Escalation store type alias for convenience. pub type EscalationStoreImpl = GenericEscalationStore; /// Alias store type alias for convenience. pub type AliasStoreImpl = GenericAliasStore>; /// Trust rank store type alias for convenience. pub type TrustRankStoreImpl = GenericTrustRankStore>; /// Admission store type alias for convenience. pub type AdmissionStoreImpl = GenericAdmissionStore>; /// Application state shared across all HTTP handlers. /// /// This is passed to every request via axum's `State` extractor. #[derive(Clone)] pub struct AppState { /// Group commit buffer for batched WAL writes (used by write handlers) pub commit_buffer: GroupCommitBuffer, /// Write-ahead log for reading records (IngestWorker uses this) pub journal: Arc>, /// Key-value store for reading assertions pub store: Arc, /// Quota store for economic throttling (The Meter) pub quota_store: Arc, /// Escalation store for high-conflict assertion tracking pub escalation_store: Arc, /// Alias store for cross-scheme entity resolution pub alias_store: Arc, /// Trust rank store for reputation tracking pub trust_rank_store: Arc, /// Admission store for PoW-based admission control (The Shield) pub admission_store: Arc, } impl AppState { /// Create a new application state. /// /// Takes two journals: one for the group commit buffer (writes) and /// one for reading (used by IngestWorker). Both should be opened on /// the same directory. pub fn new(write_journal: Journal, read_journal: Journal, store: Arc) -> Self { let commit_buffer = GroupCommitBuffer::new(write_journal, GroupCommitConfig::default()); let journal = Arc::new(Mutex::new(read_journal)); // Create quota store backed by the same KV store let quota_store = Arc::new(GenericQuotaStore::new(Arc::clone(&store))); // Create escalation store backed by the same KV store let escalation_store = Arc::new(GenericEscalationStore::new(Arc::clone(&store))); // Create alias store for cross-scheme concept resolution let alias_store = Arc::new(GenericAliasStore::new(Arc::clone(&store))); // Create trust rank store for reputation tracking let trust_rank_store = Arc::new(GenericTrustRankStore::new(Arc::clone(&store))); // Create admission store for PoW-based admission control let admission_store = Arc::new(GenericAdmissionStore::new(Arc::clone(&trust_rank_store))); Self { commit_buffer, journal, store, quota_store, escalation_store, alias_store, trust_rank_store, admission_store, } } /// Get a QueryEngine for this state. /// /// Creates a new QueryEngine each time since it cannot be cloned. pub fn query_engine(&self) -> QueryEngine { QueryEngine::new(self.store.clone()) } }