Skip to main content

noxu_dbi/
database_config.rs

1//! Database configuration.
2//!
3
4/// Configuration for a database.
5///
6///
7#[derive(Debug, Clone)]
8pub struct DatabaseConfig {
9    /// Allow database creation if it doesn't exist.
10    pub allow_create: bool,
11    /// Enable sorted duplicates.
12    pub sorted_duplicates: bool,
13    /// Enable key prefixing compression.
14    pub key_prefixing: bool,
15    /// Database is temporary (not persisted).
16    pub temporary: bool,
17    /// Database operations are transactional.
18    pub transactional: bool,
19    /// Database is read-only.
20    pub read_only: bool,
21    /// Maximum entries per node.
22    pub node_max_entries: i32,
23    /// Deferred write: skip WAL logging; flush only at eviction/checkpoint.
24    ///
25    ///
26    pub deferred_write: bool,
27}
28
29impl Default for DatabaseConfig {
30    fn default() -> Self {
31        DatabaseConfig {
32            allow_create: false,
33            sorted_duplicates: false,
34            key_prefixing: false,
35            temporary: false,
36            transactional: false,
37            read_only: false,
38            node_max_entries: 128,
39            deferred_write: false,
40        }
41    }
42}
43
44impl DatabaseConfig {
45    /// Creates a new DatabaseConfig with default values.
46    pub fn new() -> Self {
47        Self::default()
48    }
49
50    /// Sets the allow_create flag.
51    pub fn set_allow_create(&mut self, allow_create: bool) -> &mut Self {
52        self.allow_create = allow_create;
53        self
54    }
55
56    /// Sets the sorted_duplicates flag.
57    pub fn set_sorted_duplicates(
58        &mut self,
59        sorted_duplicates: bool,
60    ) -> &mut Self {
61        self.sorted_duplicates = sorted_duplicates;
62        self
63    }
64
65    /// Sets the key_prefixing flag.
66    pub fn set_key_prefixing(&mut self, key_prefixing: bool) -> &mut Self {
67        self.key_prefixing = key_prefixing;
68        self
69    }
70
71    /// Sets the temporary flag.
72    pub fn set_temporary(&mut self, temporary: bool) -> &mut Self {
73        self.temporary = temporary;
74        self
75    }
76
77    /// Sets the transactional flag.
78    pub fn set_transactional(&mut self, transactional: bool) -> &mut Self {
79        self.transactional = transactional;
80        self
81    }
82
83    /// Sets the read_only flag.
84    pub fn set_read_only(&mut self, read_only: bool) -> &mut Self {
85        self.read_only = read_only;
86        self
87    }
88
89    /// Sets the maximum entries per node.
90    pub fn set_node_max_entries(&mut self, max: i32) -> &mut Self {
91        self.node_max_entries = max;
92        self
93    }
94}
95
96#[cfg(test)]
97mod tests {
98    use super::*;
99
100    #[test]
101    fn test_default() {
102        let config = DatabaseConfig::default();
103        assert!(!config.allow_create);
104        assert!(!config.sorted_duplicates);
105        assert!(!config.key_prefixing);
106        assert!(!config.temporary);
107        assert!(!config.transactional);
108        assert!(!config.read_only);
109        assert_eq!(config.node_max_entries, 128);
110    }
111
112    #[test]
113    fn test_new() {
114        let config = DatabaseConfig::new();
115        assert!(!config.allow_create);
116    }
117
118    #[test]
119    fn test_setters() {
120        let mut config = DatabaseConfig::new();
121
122        config.set_allow_create(true);
123        assert!(config.allow_create);
124
125        config.set_sorted_duplicates(true);
126        assert!(config.sorted_duplicates);
127
128        config.set_key_prefixing(true);
129        assert!(config.key_prefixing);
130
131        config.set_temporary(true);
132        assert!(config.temporary);
133
134        config.set_transactional(true);
135        assert!(config.transactional);
136
137        config.set_read_only(true);
138        assert!(config.read_only);
139
140        config.set_node_max_entries(256);
141        assert_eq!(config.node_max_entries, 256);
142    }
143
144    #[test]
145    fn test_builder_pattern() {
146        let config = DatabaseConfig::new()
147            .set_allow_create(true)
148            .set_transactional(true)
149            .set_node_max_entries(512)
150            .clone();
151
152        assert!(config.allow_create);
153        assert!(config.transactional);
154        assert_eq!(config.node_max_entries, 512);
155    }
156}