Skip to main content

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