flashkv/storage/
mod.rs

1//! Storage Engine Module
2//!
3//! This module provides the core storage functionality for FlashKV.
4//! It includes a thread-safe, sharded key-value store with TTL support
5//! and a background expiry sweeper.
6//!
7//! ## Architecture
8//!
9//! ```text
10//! ┌─────────────────────────────────────────────────────────────┐
11//! │                     StorageEngine                           │
12//! │  ┌─────────┐ ┌─────────┐ ┌─────────┐ ┌─────────┐           │
13//! │  │ Shard 0 │ │ Shard 1 │ │ Shard 2 │ │...64    │           │
14//! │  │ RwLock  │ │ RwLock  │ │ RwLock  │ │ shards  │           │
15//! │  └─────────┘ └─────────┘ └─────────┘ └─────────┘           │
16//! └─────────────────────────────────────────────────────────────┘
17//!                            ▲
18//!                            │
19//!              ┌─────────────┴─────────────┐
20//!              │     ExpirySweeper         │
21//!              │  (Background Tokio Task)  │
22//!              └───────────────────────────┘
23//! ```
24//!
25//! ## Features
26//!
27//! - **Sharded Storage**: 64 independent shards reduce lock contention
28//! - **RwLock**: Multiple concurrent readers, exclusive writers
29//! - **TTL Support**: Keys can have time-to-live expiry
30//! - **Lazy Expiry**: Expired keys are cleaned on access
31//! - **Active Expiry**: Background sweeper cleans orphaned expired keys
32//!
33//! ## Example
34//!
35//! ```
36//! use flashkv::storage::{StorageEngine, ExpirySweeper, ExpiryConfig};
37//! use bytes::Bytes;
38//! use std::sync::Arc;
39//! use std::time::Duration;
40//!
41//! // Create the storage engine
42//! let engine = Arc::new(StorageEngine::new());
43//!
44//! // Basic operations
45//! engine.set(Bytes::from("name"), Bytes::from("Ariz"));
46//! let value = engine.get(&Bytes::from("name"));
47//! assert_eq!(value, Some(Bytes::from("Ariz")));
48//!
49//! // Set with TTL
50//! engine.set_with_ttl(
51//!     Bytes::from("session"),
52//!     Bytes::from("token123"),
53//!     Duration::from_secs(3600)
54//! );
55//! ```
56
57pub mod engine;
58pub mod expiry;
59
60// Re-export commonly used types
61pub use engine::{Entry, MemoryInfo, StorageEngine, StorageStats};
62pub use expiry::{start_expiry_sweeper, ExpiryConfig, ExpirySweeper};