use crate::MapletResult;
use async_trait::async_trait;
use serde::{Deserialize, Serialize};
pub mod aof;
pub mod disk;
pub mod hybrid;
pub mod memory;
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct StorageStats {
pub total_keys: u64,
pub memory_usage: u64,
pub disk_usage: u64,
pub operations_count: u64,
pub avg_latency_us: u64,
}
#[async_trait]
pub trait Storage: Send + Sync {
async fn get(&self, key: &str) -> MapletResult<Option<Vec<u8>>>;
async fn set(&self, key: String, value: Vec<u8>) -> MapletResult<()>;
async fn delete(&self, key: &str) -> MapletResult<bool>;
async fn exists(&self, key: &str) -> MapletResult<bool>;
async fn keys(&self) -> MapletResult<Vec<String>>;
async fn clear_database(&self) -> MapletResult<()>;
async fn flush(&self) -> MapletResult<()>;
async fn close(&self) -> MapletResult<()>;
async fn stats(&self) -> MapletResult<StorageStats>;
}
pub struct StorageFactory;
impl StorageFactory {
pub async fn create_storage(
mode: PersistenceMode,
config: StorageConfig,
) -> MapletResult<Box<dyn Storage>> {
match mode {
PersistenceMode::Memory => Ok(Box::new(memory::MemoryStorage::new(config)?)),
PersistenceMode::Disk => Ok(Box::new(disk::DiskStorage::new(config)?)),
PersistenceMode::AOF => Ok(Box::new(aof::AOFStorage::new(config)?)),
PersistenceMode::Hybrid => Ok(Box::new(hybrid::HybridStorage::new(config)?)),
}
}
}
#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq)]
pub enum PersistenceMode {
Memory,
AOF,
Disk,
Hybrid,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct StorageConfig {
pub data_dir: String,
pub max_memory: Option<u64>,
pub enable_compression: bool,
pub sync_interval: u64,
pub write_buffer_size: usize,
}
impl Default for StorageConfig {
fn default() -> Self {
Self {
data_dir: "./data".to_string(),
max_memory: None,
enable_compression: true,
sync_interval: 1,
write_buffer_size: 1024 * 1024, }
}
}