feoxdb/core/store/
mod.rs

1use ahash::RandomState;
2use crossbeam_skiplist::SkipMap;
3use parking_lot::RwLock;
4use scc::HashMap;
5use std::sync::Arc;
6
7use crate::core::record::Record;
8use crate::core::ttl_sweep::TtlSweeper;
9use crate::stats::Statistics;
10use crate::storage::free_space::FreeSpaceManager;
11use crate::storage::metadata::Metadata;
12use crate::storage::write_buffer::WriteBuffer;
13
14// Re-export public types
15pub use self::builder::{StoreBuilder, StoreConfig};
16
17// Module declarations
18pub mod atomic;
19pub mod builder;
20pub mod init;
21pub mod internal;
22pub mod json_patch;
23pub mod operations;
24pub mod persistence;
25pub mod range;
26pub mod recovery;
27pub mod ttl;
28
29/// High-performance embedded key-value store.
30///
31/// `FeoxStore` provides ultra-fast key-value storage with optional persistence.
32/// It uses lock-free data structures for concurrent access and achieves
33/// sub-microsecond latencies for most operations.
34///
35/// # Thread Safety
36///
37/// All methods are thread-safe and can be called concurrently from multiple threads.
38pub struct FeoxStore {
39    // Main hash table with fine-grained locking using AHash
40    pub(super) hash_table: HashMap<Vec<u8>, Arc<Record>, RandomState>,
41
42    // Lock-free skip list for ordered access
43    pub(super) tree: Arc<SkipMap<Vec<u8>, Arc<Record>>>,
44
45    // Central statistics hub
46    pub(super) stats: Arc<Statistics>,
47
48    // Write buffering (optional for memory-only mode)
49    pub(super) write_buffer: Option<Arc<WriteBuffer>>,
50
51    // Free space management
52    pub(super) free_space: Arc<RwLock<FreeSpaceManager>>,
53
54    // Metadata
55    pub(super) _metadata: Arc<RwLock<Metadata>>,
56
57    // Configuration
58    pub(super) memory_only: bool,
59    pub(super) enable_caching: bool,
60    pub(super) max_memory: Option<usize>,
61
62    // Cache (if enabled)
63    pub(super) cache: Option<Arc<super::cache::ClockCache>>,
64    #[cfg(unix)]
65    pub(super) device_fd: Option<i32>,
66    pub(super) device_size: u64,
67    pub(super) device_file: Option<std::fs::File>,
68
69    // Disk I/O
70    pub(super) disk_io: Option<Arc<RwLock<crate::storage::io::DiskIO>>>,
71
72    // TTL sweeper (if enabled)
73    pub(super) ttl_sweeper: Arc<RwLock<Option<TtlSweeper>>>,
74
75    // TTL feature flag
76    pub(super) enable_ttl: bool,
77}
78
79impl FeoxStore {
80    /// Create a builder for configuring FeoxStore.
81    ///
82    /// # Example
83    ///
84    /// ```rust
85    /// use feoxdb::FeoxStore;
86    ///
87    /// # fn main() -> feoxdb::Result<()> {
88    /// let store = FeoxStore::builder()
89    ///     .max_memory(2_000_000_000)
90    ///     .build()?;
91    /// # Ok(())
92    /// # }
93    /// ```
94    pub fn builder() -> StoreBuilder {
95        StoreBuilder::new()
96    }
97
98    // ============ Utility Methods ============
99
100    /// Check if a key exists
101    pub fn contains_key(&self, key: &[u8]) -> bool {
102        self.hash_table.contains(key)
103    }
104
105    /// Get the number of records in the store
106    pub fn len(&self) -> usize {
107        self.stats
108            .record_count
109            .load(std::sync::atomic::Ordering::Acquire) as usize
110    }
111
112    /// Check if the store is empty
113    pub fn is_empty(&self) -> bool {
114        self.len() == 0
115    }
116
117    /// Get memory usage statistics
118    pub fn memory_usage(&self) -> usize {
119        self.stats
120            .memory_usage
121            .load(std::sync::atomic::Ordering::Acquire)
122    }
123
124    /// Get statistics snapshot
125    pub fn stats(&self) -> crate::stats::StatsSnapshot {
126        self.stats.snapshot()
127    }
128
129    /// Flush all pending writes to disk (for persistent mode)
130    pub fn flush(&self) {
131        self.flush_all()
132    }
133}