llm_optimizer_processor/storage/
traits.rs1use async_trait::async_trait;
4use chrono::{DateTime, Utc};
5use serde::{de::DeserializeOwned, Serialize};
6use std::fmt::Debug;
7
8use super::error::{StorageError, StorageResult};
9use super::types::{
10 Batch, EntryMetadata, Operation, Query, StorageBackend, StorageEntry, StorageHealth,
11 StorageMetadata, StorageStats, Transaction,
12};
13
14#[async_trait]
16pub trait Storage: Send + Sync + Debug {
17 fn backend(&self) -> StorageBackend;
19
20 async fn metadata(&self) -> StorageResult<StorageMetadata>;
22
23 async fn health_check(&self) -> StorageResult<StorageHealth>;
25
26 async fn stats(&self) -> StorageResult<StorageStats>;
28
29 async fn insert<T: Serialize + DeserializeOwned + Send + Sync>(
33 &self,
34 table: &str,
35 key: &str,
36 value: &T,
37 ) -> StorageResult<()>
38 where
39 Self: Sized;
40
41 async fn insert_with_ttl<T: Serialize + Send + Sync>(
43 &self,
44 table: &str,
45 key: &str,
46 value: &T,
47 ttl_seconds: i64,
48 ) -> StorageResult<()>
49 where
50 Self: Sized;
51
52 async fn get<T: DeserializeOwned + Serialize + Send + Sync>(
54 &self,
55 table: &str,
56 key: &str,
57 ) -> StorageResult<Option<StorageEntry<T>>>
58 where
59 Self: Sized;
60
61 async fn update<T: Serialize + Send + Sync>(
63 &self,
64 table: &str,
65 key: &str,
66 value: &T,
67 ) -> StorageResult<()>
68 where
69 Self: Sized;
70
71 async fn update_versioned<T: Serialize + Send + Sync>(
73 &self,
74 table: &str,
75 key: &str,
76 value: &T,
77 expected_version: u64,
78 ) -> StorageResult<()>
79 where
80 Self: Sized;
81
82 async fn delete(&self, table: &str, key: &str) -> StorageResult<()>;
84
85 async fn exists(&self, table: &str, key: &str) -> StorageResult<bool>;
87
88 async fn execute_batch(&self, batch: Batch) -> StorageResult<()>;
92
93 async fn query<T: DeserializeOwned + Serialize + Send + Sync + 'static>(
97 &self,
98 query: Query,
99 ) -> StorageResult<Vec<StorageEntry<T>>>
100 where
101 Self: Sized;
102
103 async fn count(&self, query: Query) -> StorageResult<usize>;
105
106 async fn begin_transaction(&self) -> StorageResult<Transaction>;
110
111 async fn commit_transaction(&self, transaction: Transaction) -> StorageResult<()>;
113
114 async fn rollback_transaction(&self, transaction: Transaction) -> StorageResult<()>;
116
117 async fn scan_prefix(&self, table: &str, prefix: &str) -> StorageResult<Vec<String>>;
121
122 async fn get_multi<T: DeserializeOwned + Serialize + Send + Sync>(
124 &self,
125 table: &str,
126 keys: Vec<String>,
127 ) -> StorageResult<Vec<Option<StorageEntry<T>>>>
128 where
129 Self: Sized;
130
131 async fn delete_multi(&self, table: &str, keys: Vec<String>) -> StorageResult<()>;
133
134 async fn compare_and_swap<T: Serialize + Send + Sync>(
136 &self,
137 table: &str,
138 key: &str,
139 expected_version: u64,
140 new_value: &T,
141 ) -> StorageResult<bool>
142 where
143 Self: Sized;
144
145 async fn get_modified_since<T: DeserializeOwned + Serialize + Send + Sync + 'static>(
149 &self,
150 table: &str,
151 since: DateTime<Utc>,
152 ) -> StorageResult<Vec<StorageEntry<T>>>
153 where
154 Self: Sized;
155
156 async fn get_expiring_before<T: DeserializeOwned + Serialize + Send + Sync + 'static>(
158 &self,
159 table: &str,
160 before: DateTime<Utc>,
161 ) -> StorageResult<Vec<StorageEntry<T>>>
162 where
163 Self: Sized;
164
165 async fn cleanup_expired(&self, table: &str) -> StorageResult<usize>;
169
170 async fn compact(&self) -> StorageResult<()>;
172
173 async fn flush(&self) -> StorageResult<()>;
175
176 async fn initialize(&mut self) -> StorageResult<()>;
180
181 async fn shutdown(&mut self) -> StorageResult<()>;
183}
184
185#[async_trait]
187pub trait CacheStorage: Storage {
188 async fn ttl(&self, table: &str, key: &str) -> StorageResult<Option<i64>>;
190
191 async fn expire(&self, table: &str, key: &str, ttl_seconds: i64) -> StorageResult<()>;
193
194 async fn persist(&self, table: &str, key: &str) -> StorageResult<()>;
196
197 async fn increment(&self, table: &str, key: &str, delta: i64) -> StorageResult<i64>;
199
200 async fn decrement(&self, table: &str, key: &str, delta: i64) -> StorageResult<i64>;
202
203 async fn cache_stats(&self) -> StorageResult<CacheStats>;
205}
206
207#[derive(Debug, Clone)]
209pub struct CacheStats {
210 pub hits: u64,
211 pub misses: u64,
212 pub evictions: u64,
213 pub memory_used_bytes: u64,
214 pub memory_available_bytes: u64,
215 pub keys_count: usize,
216}
217
218impl CacheStats {
219 pub fn hit_rate(&self) -> f64 {
220 let total = self.hits + self.misses;
221 if total == 0 {
222 0.0
223 } else {
224 self.hits as f64 / total as f64
225 }
226 }
227}
228
229#[async_trait]
231pub trait RelationalStorage: Storage {
232 async fn execute_sql(&self, sql: &str, params: Vec<SqlParam>) -> StorageResult<SqlResult>;
234
235 async fn query_sql<T: DeserializeOwned + Send + Sync>(
237 &self,
238 sql: &str,
239 params: Vec<SqlParam>,
240 ) -> StorageResult<Vec<T>>
241 where
242 Self: Sized;
243
244 async fn create_index(
246 &self,
247 table: &str,
248 index_name: &str,
249 columns: Vec<String>,
250 unique: bool,
251 ) -> StorageResult<()>;
252
253 async fn drop_index(&self, index_name: &str) -> StorageResult<()>;
255
256 async fn vacuum(&self, table: &str) -> StorageResult<()>;
258
259 async fn analyze(&self, table: &str) -> StorageResult<()>;
261}
262
263#[derive(Debug, Clone)]
265pub enum SqlParam {
266 String(String),
267 Integer(i64),
268 Float(f64),
269 Boolean(bool),
270 DateTime(DateTime<Utc>),
271 Null,
272}
273
274#[derive(Debug, Clone)]
276pub struct SqlResult {
277 pub rows_affected: u64,
278}
279
280#[async_trait]
282pub trait KeyValueStorage: Storage {
283 async fn get_bytes(&self, table: &str, key: &str) -> StorageResult<Option<Vec<u8>>>;
285
286 async fn set_bytes(&self, table: &str, key: &str, value: Vec<u8>) -> StorageResult<()>;
288
289 async fn iter_prefix(&self, table: &str, prefix: &str) -> StorageResult<Vec<(String, Vec<u8>)>>;
291
292 async fn get_range(
294 &self,
295 table: &str,
296 start: &str,
297 end: &str,
298 ) -> StorageResult<Vec<(String, Vec<u8>)>>;
299
300 async fn merge(&self, table: &str, key: &str, value: Vec<u8>) -> StorageResult<()>;
302}
303
304#[async_trait]
306pub trait PubSubStorage: Storage {
307 async fn publish(&self, channel: &str, message: &[u8]) -> StorageResult<usize>;
309
310 async fn subscribe(&self, channel: &str) -> StorageResult<Box<dyn Subscriber>>;
312
313 async fn unsubscribe(&self, channel: &str) -> StorageResult<()>;
315}
316
317#[async_trait]
319pub trait Subscriber: Send + Sync {
320 async fn next_message(&mut self) -> StorageResult<Option<Vec<u8>>>;
322}
323
324#[async_trait]
326pub trait Migrator: Send + Sync {
327 async fn migrate(&self) -> StorageResult<usize>;
329
330 async fn rollback(&self) -> StorageResult<()>;
332
333 async fn applied_migrations(&self) -> StorageResult<Vec<Migration>>;
335
336 async fn pending_migrations(&self) -> StorageResult<Vec<Migration>>;
338}
339
340#[derive(Debug, Clone)]
342pub struct Migration {
343 pub id: String,
344 pub name: String,
345 pub applied_at: Option<DateTime<Utc>>,
346 pub checksum: String,
347}
348
349#[async_trait]
351pub trait Backup: Send + Sync {
352 async fn create_backup(&self, path: &str) -> StorageResult<BackupMetadata>;
354
355 async fn restore_backup(&self, path: &str) -> StorageResult<()>;
357
358 async fn list_backups(&self, directory: &str) -> StorageResult<Vec<BackupMetadata>>;
360
361 async fn delete_backup(&self, path: &str) -> StorageResult<()>;
363}
364
365#[derive(Debug, Clone)]
367pub struct BackupMetadata {
368 pub path: String,
369 pub created_at: DateTime<Utc>,
370 pub size_bytes: u64,
371 pub checksum: String,
372}