mappy_core/storage/
mod.rs1use async_trait::async_trait;
6use crate::MapletResult;
7use serde::{Serialize, Deserialize};
8
9pub mod memory;
10pub mod disk;
11pub mod aof;
12pub mod hybrid;
13
14#[derive(Debug, Clone, Serialize, Deserialize, Default)]
16pub struct StorageStats {
17 pub total_keys: u64,
19 pub memory_usage: u64,
21 pub disk_usage: u64,
23 pub operations_count: u64,
25 pub avg_latency_us: u64,
27}
28
29
30#[async_trait]
32pub trait Storage: Send + Sync {
33 async fn get(&self, key: &str) -> MapletResult<Option<Vec<u8>>>;
35
36 async fn set(&self, key: String, value: Vec<u8>) -> MapletResult<()>;
38
39 async fn delete(&self, key: &str) -> MapletResult<bool>;
41
42 async fn exists(&self, key: &str) -> MapletResult<bool>;
44
45 async fn keys(&self) -> MapletResult<Vec<String>>;
47
48 async fn clear_database(&self) -> MapletResult<()>;
50
51 async fn flush(&self) -> MapletResult<()>;
53
54 async fn close(&self) -> MapletResult<()>;
56
57 async fn stats(&self) -> MapletResult<StorageStats>;
59}
60
61pub struct StorageFactory;
63
64impl StorageFactory {
65 pub async fn create_storage(
67 mode: PersistenceMode,
68 config: StorageConfig,
69 ) -> MapletResult<Box<dyn Storage>> {
70 match mode {
71 PersistenceMode::Memory => {
72 Ok(Box::new(memory::MemoryStorage::new(config)?))
73 }
74 PersistenceMode::Disk => {
75 Ok(Box::new(disk::DiskStorage::new(config)?))
76 }
77 PersistenceMode::AOF => {
78 Ok(Box::new(aof::AOFStorage::new(config)?))
79 }
80 PersistenceMode::Hybrid => {
81 Ok(Box::new(hybrid::HybridStorage::new(config)?))
82 }
83 }
84 }
85}
86
87#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq)]
89pub enum PersistenceMode {
90 Memory,
92 AOF,
94 Disk,
96 Hybrid,
98}
99
100#[derive(Debug, Clone, Serialize, Deserialize)]
102pub struct StorageConfig {
103 pub data_dir: String,
105 pub max_memory: Option<u64>,
107 pub enable_compression: bool,
109 pub sync_interval: u64,
111 pub write_buffer_size: usize,
113}
114
115impl Default for StorageConfig {
116 fn default() -> Self {
117 Self {
118 data_dir: "./data".to_string(),
119 max_memory: None,
120 enable_compression: true,
121 sync_interval: 1,
122 write_buffer_size: 1024 * 1024, }
124 }
125}