1use anyhow::Result;
8use async_trait::async_trait;
9use std::fmt;
10
11#[derive(Debug, Clone, Copy, PartialEq, Eq)]
13pub enum StorageBackend {
14 Memory,
15 RocksDB,
16 Redis,
17 Custom(&'static str),
18}
19
20impl fmt::Display for StorageBackend {
21 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
22 match self {
23 StorageBackend::Memory => write!(f, "Memory"),
24 StorageBackend::RocksDB => write!(f, "RocksDB"),
25 StorageBackend::Redis => write!(f, "Redis"),
26 StorageBackend::Custom(name) => write!(f, "{}", name),
27 }
28 }
29}
30
31#[async_trait]
33pub trait KeyValueStore: Send + Sync {
34 async fn put(&self, key: &[u8], value: &[u8]) -> Result<()>;
36
37 async fn get(&self, key: &[u8]) -> Result<Option<Vec<u8>>>;
39
40 async fn delete(&self, key: &[u8]) -> Result<()>;
42
43 async fn scan(&self, prefix: &[u8]) -> Result<Vec<(Vec<u8>, Vec<u8>)>>;
45
46 fn backend_type(&self) -> StorageBackend {
48 StorageBackend::Custom("Unknown")
49 }
50
51 async fn stats(&self) -> Result<StorageStats> {
53 Ok(StorageStats::default())
54 }
55}
56
57#[derive(Debug, Clone, Default)]
59pub struct StorageStats {
60 pub total_keys: u64,
61 pub total_size_bytes: u64,
62 pub operations_count: u64,
63 pub hit_ratio: f64,
64 pub backend_info: String,
65}
66
67#[cfg(feature = "memory")]
71pub use kotoba_memory::MemoryKeyValueStore;
72
73#[cfg(feature = "rocksdb")]
74pub use kotoba_storage_rocksdb::RocksDbStore;
75
76#[cfg(feature = "redis")]
77pub use kotoba_storage_redis::RedisStore;