kotoba_storage/
lib.rs

1//! `kotoba-storage`
2//!
3//! This crate defines the core traits (ports) for storage operations
4//! in the Kotoba ecosystem. It provides abstractions for various storage
5//! backends like Key-Value stores, Event stores, and Graph stores.
6
7use anyhow::Result;
8use async_trait::async_trait;
9use std::fmt;
10
11/// Storage backend types
12#[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/// A generic key-value store trait.
32#[async_trait]
33pub trait KeyValueStore: Send + Sync {
34    /// Puts a key-value pair into the store.
35    async fn put(&self, key: &[u8], value: &[u8]) -> Result<()>;
36
37    /// Gets a value for a given key.
38    async fn get(&self, key: &[u8]) -> Result<Option<Vec<u8>>>;
39
40    /// Deletes a key-value pair from the store.
41    async fn delete(&self, key: &[u8]) -> Result<()>;
42
43    /// Scans for key-value pairs with a given prefix.
44    async fn scan(&self, prefix: &[u8]) -> Result<Vec<(Vec<u8>, Vec<u8>)>>;
45
46    /// Returns the storage backend type
47    fn backend_type(&self) -> StorageBackend {
48        StorageBackend::Custom("Unknown")
49    }
50
51    /// Returns storage statistics
52    async fn stats(&self) -> Result<StorageStats> {
53        Ok(StorageStats::default())
54    }
55}
56
57/// Storage statistics
58#[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// TODO: Define EventStore and GraphStore traits later
68
69/// Re-export common storage implementations
70#[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;