sync_engine/storage/
traits.rs1use async_trait::async_trait;
2use crate::sync_item::SyncItem;
3use thiserror::Error;
4
5#[derive(Error, Debug)]
6pub enum StorageError {
7 #[error("Item not found")]
8 NotFound,
9 #[error("Storage backend error: {0}")]
10 Backend(String),
11 #[error("Data corruption detected for '{id}': expected hash {expected}, got {actual}")]
12 Corruption {
13 id: String,
14 expected: String,
15 actual: String,
16 },
17}
18
19#[derive(Debug)]
21pub struct BatchWriteResult {
22 pub batch_id: String,
24 pub written: usize,
26 pub verified: bool,
28}
29
30#[async_trait]
31pub trait CacheStore: Send + Sync {
32 async fn get(&self, id: &str) -> Result<Option<SyncItem>, StorageError>;
33 async fn put(&self, item: &SyncItem) -> Result<(), StorageError>;
34 async fn delete(&self, id: &str) -> Result<(), StorageError>;
35
36 async fn exists(&self, id: &str) -> Result<bool, StorageError>;
38
39 async fn put_batch(&self, items: &[SyncItem]) -> Result<BatchWriteResult, StorageError> {
42 self.put_batch_with_ttl(items, None).await
43 }
44
45 async fn put_batch_with_ttl(&self, items: &[SyncItem], ttl_secs: Option<u64>) -> Result<BatchWriteResult, StorageError> {
48 let _ = ttl_secs;
50 for item in items {
51 self.put(item).await?;
52 }
53 Ok(BatchWriteResult {
54 batch_id: String::new(),
55 written: items.len(),
56 verified: true,
57 })
58 }
59}
60
61#[async_trait]
62pub trait ArchiveStore: Send + Sync {
63 async fn get(&self, id: &str) -> Result<Option<SyncItem>, StorageError>;
64 async fn put(&self, item: &SyncItem) -> Result<(), StorageError>;
65 async fn delete(&self, id: &str) -> Result<(), StorageError>;
66
67 async fn exists(&self, id: &str) -> Result<bool, StorageError>;
69
70 async fn put_batch(&self, items: &mut [SyncItem]) -> Result<BatchWriteResult, StorageError> {
74 for item in items.iter() {
75 self.put(item).await?;
76 }
77 Ok(BatchWriteResult {
78 batch_id: String::new(),
79 written: items.len(),
80 verified: true,
81 })
82 }
83
84 async fn scan_keys(&self, offset: u64, limit: usize) -> Result<Vec<String>, StorageError>;
87
88 async fn count_all(&self) -> Result<u64, StorageError>;
90}