Skip to main content

noxu_db/
read_options.rs

1//! Read operation options.
2//!
3
4use crate::cache_mode::CacheMode;
5use crate::lock_mode::LockMode;
6
7/// Options for read operations.
8///
9/// Specifies optional parameters that control read behavior, including
10/// locking and caching.
11///
12///
13#[derive(Debug, Clone, PartialEq, Eq)]
14pub struct ReadOptions {
15    /// Lock mode for the read operation.
16    pub lock_mode: LockMode,
17
18    /// Cache mode for the read operation.
19    pub cache_mode: Option<CacheMode>,
20}
21
22impl ReadOptions {
23    /// Creates a new ReadOptions with default settings.
24    pub fn new() -> Self {
25        Self { lock_mode: LockMode::Default, cache_mode: None }
26    }
27
28    /// Sets the lock mode.
29    pub fn with_lock_mode(mut self, lock_mode: LockMode) -> Self {
30        self.lock_mode = lock_mode;
31        self
32    }
33
34    /// Sets the cache mode.
35    pub fn with_cache_mode(mut self, cache_mode: CacheMode) -> Self {
36        self.cache_mode = Some(cache_mode);
37        self
38    }
39
40    /// Creates ReadOptions for read-uncommitted (dirty read).
41    pub fn read_uncommitted() -> Self {
42        Self { lock_mode: LockMode::ReadUncommitted, cache_mode: None }
43    }
44
45    /// Creates ReadOptions for read-modify-write.
46    pub fn read_modify_write() -> Self {
47        Self { lock_mode: LockMode::Rmw, cache_mode: None }
48    }
49
50    /// Creates ReadOptions with evict-after-read cache mode.
51    pub fn evict_after_read() -> Self {
52        Self {
53            lock_mode: LockMode::Default,
54            cache_mode: Some(CacheMode::EvictLn),
55        }
56    }
57}
58
59impl Default for ReadOptions {
60    fn default() -> Self {
61        Self::new()
62    }
63}
64
65#[cfg(test)]
66mod tests {
67    use super::*;
68
69    #[test]
70    fn test_new() {
71        let opts = ReadOptions::new();
72        assert_eq!(opts.lock_mode, LockMode::Default);
73        assert_eq!(opts.cache_mode, None);
74    }
75
76    #[test]
77    fn test_with_lock_mode() {
78        let opts = ReadOptions::new().with_lock_mode(LockMode::Rmw);
79        assert_eq!(opts.lock_mode, LockMode::Rmw);
80    }
81
82    #[test]
83    fn test_with_cache_mode() {
84        let opts = ReadOptions::new().with_cache_mode(CacheMode::KeepHot);
85        assert_eq!(opts.cache_mode, Some(CacheMode::KeepHot));
86    }
87
88    #[test]
89    fn test_read_uncommitted() {
90        let opts = ReadOptions::read_uncommitted();
91        assert_eq!(opts.lock_mode, LockMode::ReadUncommitted);
92        assert_eq!(opts.cache_mode, None);
93    }
94
95    #[test]
96    fn test_read_modify_write() {
97        let opts = ReadOptions::read_modify_write();
98        assert_eq!(opts.lock_mode, LockMode::Rmw);
99    }
100
101    #[test]
102    fn test_evict_after_read() {
103        let opts = ReadOptions::evict_after_read();
104        assert_eq!(opts.cache_mode, Some(CacheMode::EvictLn));
105    }
106
107    #[test]
108    fn test_default() {
109        let opts = ReadOptions::default();
110        assert_eq!(opts.lock_mode, LockMode::Default);
111        assert_eq!(opts.cache_mode, None);
112    }
113
114    #[test]
115    fn test_clone() {
116        let opts1 = ReadOptions::read_uncommitted();
117        let opts2 = opts1.clone();
118        assert_eq!(opts1, opts2);
119    }
120
121    #[test]
122    fn test_builder_chain() {
123        let opts = ReadOptions::new()
124            .with_lock_mode(LockMode::ReadCommitted)
125            .with_cache_mode(CacheMode::EvictBin);
126        assert_eq!(opts.lock_mode, LockMode::ReadCommitted);
127        assert_eq!(opts.cache_mode, Some(CacheMode::EvictBin));
128    }
129
130    #[test]
131    fn test_equality() {
132        let opts1 = ReadOptions::new();
133        let opts2 = ReadOptions::default();
134        assert_eq!(opts1, opts2);
135    }
136
137    #[test]
138    fn test_debug() {
139        let opts = ReadOptions::read_uncommitted();
140        let debug = format!("{:?}", opts);
141        assert!(debug.contains("lock_mode"));
142        assert!(debug.contains("cache_mode"));
143    }
144}