sync_engine/storage/
traits.rs1use async_trait::async_trait;
5use crate::sync_item::SyncItem;
6use crate::search::SqlParam;
7use thiserror::Error;
8
9#[derive(Error, Debug)]
10pub enum StorageError {
11 #[error("Item not found")]
12 NotFound,
13 #[error("Storage backend error: {0}")]
14 Backend(String),
15 #[error("Connection error: {0}")]
16 Connection(String),
17 #[error("Data corruption detected for '{id}': expected hash {expected}, got {actual}")]
18 Corruption {
19 id: String,
20 expected: String,
21 actual: String,
22 },
23}
24
25#[derive(Debug)]
27pub struct BatchWriteResult {
28 pub batch_id: String,
30 pub written: usize,
32 pub verified: bool,
34}
35
36#[async_trait]
37pub trait CacheStore: Send + Sync {
38 async fn get(&self, id: &str) -> Result<Option<SyncItem>, StorageError>;
39 async fn put(&self, item: &SyncItem) -> Result<(), StorageError>;
40 async fn delete(&self, id: &str) -> Result<(), StorageError>;
41
42 async fn exists(&self, id: &str) -> Result<bool, StorageError>;
44
45 async fn put_batch(&self, items: &[SyncItem]) -> Result<BatchWriteResult, StorageError> {
48 self.put_batch_with_ttl(items, None).await
49 }
50
51 async fn put_batch_with_ttl(&self, items: &[SyncItem], ttl_secs: Option<u64>) -> Result<BatchWriteResult, StorageError> {
54 let _ = ttl_secs;
56 for item in items {
57 self.put(item).await?;
58 }
59 Ok(BatchWriteResult {
60 batch_id: String::new(),
61 written: items.len(),
62 verified: true,
63 })
64 }
65
66 async fn ft_create(&self, args: &[String]) -> Result<(), StorageError> {
68 let _ = args;
69 Err(StorageError::Backend("FT.CREATE not supported".into()))
70 }
71
72 async fn ft_dropindex(&self, index: &str) -> Result<(), StorageError> {
74 let _ = index;
75 Err(StorageError::Backend("FT.DROPINDEX not supported".into()))
76 }
77
78 async fn ft_search(&self, index: &str, query: &str, limit: usize) -> Result<Vec<String>, StorageError> {
81 let _ = (index, query, limit);
82 Err(StorageError::Backend("FT.SEARCH not supported".into()))
83 }
84}
85
86#[async_trait]
87pub trait ArchiveStore: Send + Sync {
88 async fn get(&self, id: &str) -> Result<Option<SyncItem>, StorageError>;
89 async fn put(&self, item: &SyncItem) -> Result<(), StorageError>;
90 async fn delete(&self, id: &str) -> Result<(), StorageError>;
91
92 async fn exists(&self, id: &str) -> Result<bool, StorageError>;
94
95 async fn put_batch(&self, items: &mut [SyncItem]) -> Result<BatchWriteResult, StorageError> {
99 for item in items.iter() {
100 self.put(item).await?;
101 }
102 Ok(BatchWriteResult {
103 batch_id: String::new(),
104 written: items.len(),
105 verified: true,
106 })
107 }
108
109 async fn scan_keys(&self, offset: u64, limit: usize) -> Result<Vec<String>, StorageError>;
112
113 async fn count_all(&self) -> Result<u64, StorageError>;
115
116 async fn search(&self, where_clause: &str, params: &[SqlParam], limit: usize) -> Result<Vec<SyncItem>, StorageError> {
119 let _ = (where_clause, params, limit);
120 Err(StorageError::Backend("SQL search not supported".into()))
121 }
122}