Skip to main content

synapto_interface/
storage.rs

1#![doc = include_str!("storage.md")]
2
3use async_trait::async_trait;
4
5/// A marker trait for safe DB connection pooling
6pub trait StorageProviderPool: Send + Sync + 'static {}
7
8#[derive(Default)]
9pub struct StorageRegistry {
10    map: tokio::sync::Mutex<
11        std::collections::HashMap<
12            std::any::TypeId,
13            std::sync::Arc<dyn std::any::Any + Send + Sync>,
14        >,
15    >,
16}
17
18impl StorageRegistry {
19    /// Lazily initializes a global shared resource. If the resource already exists,
20    /// it is returned immediately. This allows multiple plugins to safely share a
21    /// single connection pool without requiring manual initialization in main.rs.
22    pub async fn get_or_init<T: StorageProviderPool, F, Fut, E>(
23        &self,
24        init: F,
25    ) -> Result<std::sync::Arc<T>, E>
26    where
27        F: FnOnce() -> Fut,
28        Fut: std::future::Future<Output = Result<T, E>>,
29    {
30        let mut map = self.map.lock().await;
31        let type_id = std::any::TypeId::of::<T>();
32
33        if let Some(resource) = map.get(&type_id) {
34            // This can only fail if the TypeId of T doesn't match the Arc's inner type,
35            // which is impossible since we keyed the HashMap by TypeId::of::<T>().
36            return Ok(resource
37                .clone()
38                .downcast::<T>()
39                .unwrap_or_else(|_| unreachable!("TypeId mismatch in StorageRegistry")));
40        }
41
42        let resource = std::sync::Arc::new(init().await?);
43        map.insert(type_id, resource.clone());
44        Ok(resource)
45    }
46}
47
48pub trait StorageConfigResolver: Send + Sync + 'static {
49    fn resolve_config(
50        &self,
51        crate_name: &str,
52        storage_type_name: &str,
53    ) -> Option<serde_json::Value>;
54}
55
56/// The entry point for a generic storage adapter.
57/// It guarantees that plugins can seamlessly initialize their underlying connection
58/// using the shared StorageRegistry without requiring manual setup in main.rs.
59#[async_trait]
60pub trait StorageConnection: Send + Sync + Sized + 'static {
61    type Config: serde::de::DeserializeOwned + Send + Sync;
62
63    async fn connect(
64        config: Self::Config,
65        storage_registry: std::sync::Arc<StorageRegistry>,
66        data_dir: &std::path::Path,
67        plugin_namespace: &str,
68    ) -> Result<Self, String>;
69}
70
71use serde::{Deserialize, Serialize, de::DeserializeOwned};
72
73#[derive(Debug, Clone, Serialize, Deserialize, Default)]
74pub struct EmptyStorageConfig {}
75
76#[async_trait]
77pub trait RecordStore: Send + Sync + 'static {
78    /// Inserts or updates an individual record.
79    /// If the key is a Timestamp or ULID, time-based ordering is natively maintained.
80    async fn upsert_record<T>(&self, collection: &str, key: &str, value: T) -> Result<(), String>
81    where
82        T: Serialize + Send + Sync + 'static;
83
84    /// Retrieves records guaranteed to be sorted by their key.
85    /// Allows pagination to avoid loading the entire history into RAM.
86    async fn get_ordered_records<T>(
87        &self,
88        collection: &str,
89        limit: Option<usize>,
90        reverse: bool,
91    ) -> Result<Vec<(String, T)>, String>
92    where
93        T: DeserializeOwned + Send + Sync + 'static;
94
95    /// Deletes a specific record.
96    async fn delete_record(&self, collection: &str, key: &str) -> Result<(), String>;
97
98    /// Atomically deletes all records with a key smaller than `cutoff_key`.
99    /// This natively delegates sliding-window "VecDeque::pop_front()" operations to the DB.
100    async fn trim_records_before(&self, collection: &str, cutoff_key: &str) -> Result<(), String>;
101}
102
103/// For storing and retrieving items by a unique string ID.
104#[async_trait]
105pub trait KeyValueStore: Send + Sync + 'static {
106    async fn set<T>(&self, collection: &str, key: &str, value: T) -> Result<(), String>
107    where
108        T: Serialize + Send + Sync + 'static;
109
110    async fn get<T>(&self, collection: &str, key: &str) -> Result<Option<T>, String>
111    where
112        T: DeserializeOwned + Send + Sync + 'static;
113
114    async fn delete(&self, collection: &str, key: &str) -> Result<(), String>;
115
116    async fn get_all<T>(&self, collection: &str) -> Result<Vec<T>, String>
117    where
118        T: DeserializeOwned + Send + Sync + 'static;
119}
120
121/// Trait for storing, retrieving, and deleting raw binary files.
122#[async_trait]
123pub trait FileStore: Send + Sync + 'static {
124    /// Saves raw bytes under the specified collection and unique file identifier.
125    async fn save_file(
126        &self,
127        collection: &str,
128        file_id: &str,
129        content: Vec<u8>,
130    ) -> Result<(), String>;
131
132    /// Retrieves raw bytes by its identifier.
133    async fn get_file(&self, collection: &str, file_id: &str) -> Result<Option<Vec<u8>>, String>;
134
135    /// Deletes a file.
136    async fn delete_file(&self, collection: &str, file_id: &str) -> Result<(), String>;
137}
138#[async_trait]
139pub trait VectorStore: Send + Sync + 'static {
140    /// Ensures a collection is ready for vector operations.
141    ///
142    /// This method is typically called during the application boot sequence or when a service starts up.
143    /// It should be idempotent.
144    ///
145    /// Depending on the underlying database, this method might:
146    /// - Define a schema or table if it doesn't exist.
147    /// - Create necessary vector search indexes (e.g., M-Tree, HNSW).
148    /// - Do absolutely nothing if the database manages indexing transparently (e.g., Firestore).
149    ///
150    /// By default, this does nothing and returns `Ok(())`. Storage providers that require
151    /// explicit schema or index definition must override this implementation.
152    async fn setup_collection(&self, _collection: &str, _dimension: u32) -> Result<(), String> {
153        Ok(())
154    }
155
156    async fn insert_vectors<T>(&self, collection: &str, records: Vec<T>) -> Result<(), String>
157    where
158        T: Serialize + Send + Sync + 'static;
159
160    async fn search_vectors<T>(
161        &self,
162        collection: &str,
163        vector: Vec<f32>,
164        limit: u32,
165    ) -> Result<Vec<T>, String>
166    where
167        T: DeserializeOwned + Send + Sync + 'static;
168
169    async fn delete_vectors(
170        &self,
171        collection: &str,
172        filter_field: &str,
173        filter_value: &str,
174    ) -> Result<(), String>;
175}