mappy_core/storage/
mod.rs

1//! Storage layer for mappy
2//! 
3//! Provides different storage backends for persistence and durability.
4
5use 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/// Storage statistics
15#[derive(Debug, Clone, Serialize, Deserialize, Default)]
16pub struct StorageStats {
17    /// Total number of keys stored
18    pub total_keys: u64,
19    /// Memory usage in bytes
20    pub memory_usage: u64,
21    /// Disk usage in bytes
22    pub disk_usage: u64,
23    /// Number of operations performed
24    pub operations_count: u64,
25    /// Average operation latency in microseconds
26    pub avg_latency_us: u64,
27}
28
29
30/// Storage trait for different backends
31#[async_trait]
32pub trait Storage: Send + Sync {
33    /// Get a value by key
34    async fn get(&self, key: &str) -> MapletResult<Option<Vec<u8>>>;
35    
36    /// Set a key-value pair
37    async fn set(&self, key: String, value: Vec<u8>) -> MapletResult<()>;
38    
39    /// Delete a key
40    async fn delete(&self, key: &str) -> MapletResult<bool>;
41    
42    /// Check if a key exists
43    async fn exists(&self, key: &str) -> MapletResult<bool>;
44    
45    /// Get all keys
46    async fn keys(&self) -> MapletResult<Vec<String>>;
47    
48    /// Clear all data
49    async fn clear_database(&self) -> MapletResult<()>;
50    
51    /// Flush any pending writes
52    async fn flush(&self) -> MapletResult<()>;
53    
54    /// Close the storage backend
55    async fn close(&self) -> MapletResult<()>;
56    
57    /// Get storage statistics
58    async fn stats(&self) -> MapletResult<StorageStats>;
59}
60
61/// Storage factory for creating different backends
62pub struct StorageFactory;
63
64impl StorageFactory {
65    /// Create a storage backend based on persistence mode
66    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/// Persistence mode for storage
88#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq)]
89pub enum PersistenceMode {
90    /// In-memory only (no persistence)
91    Memory,
92    /// Append-only file
93    AOF,
94    /// Full durability (synchronous writes)
95    Disk,
96    /// Hybrid (memory + AOF)
97    Hybrid,
98}
99
100/// Storage configuration
101#[derive(Debug, Clone, Serialize, Deserialize)]
102pub struct StorageConfig {
103    /// Data directory for persistent storage
104    pub data_dir: String,
105    /// Maximum memory usage in bytes
106    pub max_memory: Option<u64>,
107    /// Enable compression
108    pub enable_compression: bool,
109    /// Sync interval for AOF mode (seconds)
110    pub sync_interval: u64,
111    /// Buffer size for writes
112    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, // 1MB
123        }
124    }
125}