Skip to main content

subcog/storage/
mod.rs

1//! Storage layer abstraction.
2//!
3//! This module provides a three-layer storage architecture:
4//! - **Persistence**: Authoritative storage (`SQLite`, PostgreSQL, Filesystem)
5//! - **Index**: Full-text search (`SQLite` + FTS5, PostgreSQL, `RediSearch`)
6//! - **Vector**: Embedding similarity search (usearch, pgvector, Redis)
7
8// Allow cast precision loss for score calculations where exact precision is not critical.
9#![allow(clippy::cast_precision_loss)]
10// Allow significant_drop_tightening - dropping database connections slightly early
11// provides no meaningful benefit.
12#![allow(clippy::significant_drop_tightening)]
13// Allow manual_let_else for clearer error handling in some contexts.
14#![allow(clippy::manual_let_else)]
15// Allow match_same_arms for explicit enum handling.
16#![allow(clippy::match_same_arms)]
17// Allow or_fun_call - the error path is uncommon.
18#![allow(clippy::or_fun_call)]
19// Allow unused_self for methods kept for API consistency.
20#![allow(clippy::unused_self)]
21
22pub mod bulkhead;
23pub mod context_template;
24pub mod graph;
25#[cfg(feature = "group-scope")]
26pub mod group;
27pub mod index;
28pub mod migrations;
29pub mod persistence;
30pub mod prompt;
31pub mod resilience;
32pub mod traits;
33pub mod vector;
34
35pub use bulkhead::{
36    BulkheadIndexBackend, BulkheadPersistenceBackend, BulkheadVectorBackend, StorageBulkheadConfig,
37};
38pub use context_template::{
39    ContextTemplateBackendType, ContextTemplateDbStats, ContextTemplateStorage,
40    ContextTemplateStorageFactory, SqliteContextTemplateStorage,
41};
42pub use index::get_user_data_dir;
43pub use prompt::{
44    FilesystemPromptStorage, PostgresPromptStorage, PromptBackendType, PromptStorage,
45    PromptStorageFactory, RedisPromptStorage, SqlitePromptStorage,
46};
47pub use resilience::{
48    ResilientIndexBackend, ResilientPersistenceBackend, ResilientVectorBackend,
49    StorageResilienceConfig, is_retryable_storage_error, retry_connection,
50};
51pub use traits::{IndexBackend, PersistenceBackend, VectorBackend};
52
53// Group storage (feature-gated)
54#[cfg(feature = "group-scope")]
55pub use group::{GroupBackend, GroupBackendType, GroupStorageFactory, SqliteGroupBackend};
56
57/// Composite storage combining all three layers.
58pub struct CompositeStorage<P, I, V>
59where
60    P: PersistenceBackend,
61    I: IndexBackend,
62    V: VectorBackend,
63{
64    persistence: P,
65    index: I,
66    vector: V,
67}
68
69impl<P, I, V> CompositeStorage<P, I, V>
70where
71    P: PersistenceBackend,
72    I: IndexBackend,
73    V: VectorBackend,
74{
75    /// Creates a new composite storage with the given backends.
76    pub const fn new(persistence: P, index: I, vector: V) -> Self {
77        Self {
78            persistence,
79            index,
80            vector,
81        }
82    }
83
84    /// Returns a reference to the persistence backend.
85    pub const fn persistence(&self) -> &P {
86        &self.persistence
87    }
88
89    /// Returns a reference to the index backend.
90    pub const fn index(&self) -> &I {
91        &self.index
92    }
93
94    /// Returns a reference to the vector backend.
95    pub const fn vector(&self) -> &V {
96        &self.vector
97    }
98}