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