Skip to main content

noxu_db/
cache_mode.rs

1//! Cache modes for database operations.
2//!
3
4/// Cache mode for database operations.
5///
6/// Specifies how records are cached during database operations.
7/// Allows applications to optimize caching behavior based on
8/// access patterns.
9///
10///
11#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
12pub enum CacheMode {
13    /// Use the default caching behavior.
14    ///
15    /// Records are cached normally according to the LRU policy.
16    #[default]
17    Default,
18
19    /// Keep the record in cache.
20    ///
21    /// The record is pinned in cache and will not be evicted by
22    /// the LRU policy. Use for hot data that should remain cached.
23    KeepHot,
24
25    /// Make the record most recently used.
26    ///
27    /// Moves the record to the MRU position in the LRU list, making
28    /// it less likely to be evicted. This is the default for most operations.
29    MakeCold,
30
31    /// Evict the record after the operation.
32    ///
33    /// The record is immediately evicted from cache after the operation
34    /// completes. Use for one-time access patterns or bulk operations.
35    EvictLn,
36
37    /// Evict both the record and its parent BIN.
38    ///
39    /// More aggressive eviction that also removes the BIN node from cache.
40    /// Use for sequential scans where data won't be accessed again.
41    EvictBin,
42
43    /// Do not modify the cache position.
44    ///
45    /// The record is accessed but its position in the LRU list is not changed.
46    /// Use when you don't want to affect normal cache management.
47    Unchanged,
48}
49
50impl CacheMode {
51    /// Returns whether this mode evicts the record.
52    pub fn evicts_ln(&self) -> bool {
53        matches!(self, CacheMode::EvictLn | CacheMode::EvictBin)
54    }
55
56    /// Returns whether this mode evicts the BIN.
57    pub fn evicts_bin(&self) -> bool {
58        matches!(self, CacheMode::EvictBin)
59    }
60
61    /// Returns whether this mode keeps the record hot.
62    pub fn keeps_hot(&self) -> bool {
63        matches!(self, CacheMode::KeepHot)
64    }
65
66    /// Returns whether this mode modifies cache position.
67    pub fn modifies_cache(&self) -> bool {
68        !matches!(self, CacheMode::Unchanged)
69    }
70}
71
72#[cfg(test)]
73mod tests {
74    use super::*;
75
76    #[test]
77    fn test_default() {
78        assert_eq!(CacheMode::default(), CacheMode::Default);
79    }
80
81    #[test]
82    fn test_evicts_ln() {
83        assert!(CacheMode::EvictLn.evicts_ln());
84        assert!(CacheMode::EvictBin.evicts_ln());
85        assert!(!CacheMode::Default.evicts_ln());
86        assert!(!CacheMode::KeepHot.evicts_ln());
87    }
88
89    #[test]
90    fn test_evicts_bin() {
91        assert!(CacheMode::EvictBin.evicts_bin());
92        assert!(!CacheMode::EvictLn.evicts_bin());
93        assert!(!CacheMode::Default.evicts_bin());
94    }
95
96    #[test]
97    fn test_keeps_hot() {
98        assert!(CacheMode::KeepHot.keeps_hot());
99        assert!(!CacheMode::Default.keeps_hot());
100        assert!(!CacheMode::EvictLn.keeps_hot());
101    }
102
103    #[test]
104    fn test_modifies_cache() {
105        assert!(CacheMode::Default.modifies_cache());
106        assert!(CacheMode::KeepHot.modifies_cache());
107        assert!(!CacheMode::Unchanged.modifies_cache());
108    }
109
110    #[test]
111    fn test_equality() {
112        assert_eq!(CacheMode::Default, CacheMode::Default);
113        assert_ne!(CacheMode::Default, CacheMode::KeepHot);
114    }
115
116    #[test]
117    fn test_clone() {
118        let mode1 = CacheMode::EvictBin;
119        let mode2 = mode1;
120        assert_eq!(mode1, mode2);
121    }
122
123    #[test]
124    fn test_copy() {
125        let mode1 = CacheMode::MakeCold;
126        let mode2 = mode1;
127        assert_eq!(mode1, mode2);
128    }
129
130    #[test]
131    fn test_debug() {
132        let mode = CacheMode::KeepHot;
133        let debug = format!("{:?}", mode);
134        assert_eq!(debug, "KeepHot");
135    }
136}