Skip to main content

noxu_db/
database_config.rs

1//! Database configuration.
2//!
3
4use crate::cache_mode::CacheMode;
5
6/// Configuration for opening a database.
7///
8/// Specifies the configuration parameters used to open a database within
9/// an environment. Use the builder pattern to configure individual parameters.
10///
11///
12#[derive(Debug, Clone, PartialEq, Eq)]
13pub struct DatabaseConfig {
14    /// Allow creation of a new database if it doesn't exist.
15    pub allow_create: bool,
16
17    /// Whether the database supports sorted duplicates.
18    pub sorted_duplicates: bool,
19
20    /// Whether the database supports transactions.
21    pub transactional: bool,
22
23    /// Open the database in read-only mode.
24    pub read_only: bool,
25
26    /// Whether this is a temporary database.
27    ///
28    /// Temporary databases are not logged and are removed when closed.
29    pub temporary: bool,
30
31    /// Whether to use deferred write mode.
32    ///
33    /// Deferred write databases delay writing to disk for better performance.
34    pub deferred_write: bool,
35
36    /// Override the B-tree key comparator.
37    ///
38    /// **Inert as of v1.6.0**:
39    /// the public API has no setter for an actual comparator function,
40    /// so this flag has nothing to consume.  The flag and the
41    /// `ByteComparator` trait are scheduled for removal in v2.0.
42    /// Setting it to `true` does *not* change the on-disk byte order.
43    pub override_btree_comparator: bool,
44
45    /// Override the duplicate data comparator.
46    ///
47    /// **Inert as of v1.6.0** — see `override_btree_comparator`.
48    pub override_duplicate_comparator: bool,
49
50    /// Whether this database is exclusive to a single thread.
51    ///
52    /// **Inert as of v1.6.0**: the
53    /// `noxu_dbi` engine has no per-database thread-affinity
54    /// enforcement; this flag is recorded but never consulted.
55    pub exclusive: bool,
56
57    /// Node maximum entries (0 = use default).
58    pub node_max_entries: u32,
59
60    /// Whether this database participates in replication.
61    ///
62    /// **Inert as of v1.6.0**: the
63    /// `noxu_dbi::DatabaseConfig` has no `replicated` field; the
64    /// replication scope is set at the env level via `noxu-rep`.
65    pub replicated: bool,
66
67    /// Enable key prefix compression in BIN nodes.
68    ///
69    /// **Plumbed through to `noxu_dbi::DatabaseConfig` as of v1.6.0**
70    ///.
71    pub key_prefixing: bool,
72
73    /// Per-database cache eviction hint.
74    ///
75    /// **Inert as of v1.6.0**: the
76    /// per-DB hint is not yet honoured by the evictor; the env-level
77    /// cache mode is.
78    pub cache_mode: CacheMode,
79
80    /// Write BIN-deltas to the log instead of full BINs (space optimization).
81    ///
82    /// **Inert as of v1.6.0**: the
83    /// engine always emits BIN-deltas where applicable.
84    pub bin_delta: bool,
85
86    /// When true, opening an existing database reuses its stored config
87    /// rather than applying this config.
88    ///
89    /// **Inert as of v1.6.0**: the
90    /// engine does not yet persist per-DB config across runs in a way
91    /// that can be selectively re-applied.
92    pub use_existing_config: bool,
93}
94
95impl DatabaseConfig {
96    /// Creates a new DatabaseConfig with default settings.
97    pub fn new() -> Self {
98        Self {
99            allow_create: false,
100            sorted_duplicates: false,
101            transactional: false,
102            read_only: false,
103            temporary: false,
104            deferred_write: false,
105            override_btree_comparator: false,
106            override_duplicate_comparator: false,
107            exclusive: false,
108            node_max_entries: 0,
109            replicated: false,
110            key_prefixing: false,
111            cache_mode: CacheMode::Default,
112            bin_delta: true, // enabled by default (JE default)
113            use_existing_config: false,
114        }
115    }
116
117    /// Sets whether to allow creation of a new database.
118    pub fn set_allow_create(&mut self, allow_create: bool) -> &mut Self {
119        self.allow_create = allow_create;
120        self
121    }
122
123    /// Sets whether the database supports sorted duplicates.
124    pub fn set_sorted_duplicates(
125        &mut self,
126        sorted_duplicates: bool,
127    ) -> &mut Self {
128        self.sorted_duplicates = sorted_duplicates;
129        self
130    }
131
132    /// Sets whether the database supports transactions.
133    pub fn set_transactional(&mut self, transactional: bool) -> &mut Self {
134        self.transactional = transactional;
135        self
136    }
137
138    /// Sets whether the database is read-only.
139    pub fn set_read_only(&mut self, read_only: bool) -> &mut Self {
140        self.read_only = read_only;
141        self
142    }
143
144    /// Sets whether this is a temporary database.
145    pub fn set_temporary(&mut self, temporary: bool) -> &mut Self {
146        self.temporary = temporary;
147        self
148    }
149
150    /// Sets whether to use deferred write mode.
151    pub fn set_deferred_write(&mut self, deferred_write: bool) -> &mut Self {
152        self.deferred_write = deferred_write;
153        self
154    }
155
156    /// Sets whether to override the B-tree comparator.
157    pub fn set_override_btree_comparator(
158        &mut self,
159        override_btree_comparator: bool,
160    ) -> &mut Self {
161        self.override_btree_comparator = override_btree_comparator;
162        self
163    }
164
165    /// Sets whether to override the duplicate comparator.
166    pub fn set_override_duplicate_comparator(
167        &mut self,
168        override_duplicate_comparator: bool,
169    ) -> &mut Self {
170        self.override_duplicate_comparator = override_duplicate_comparator;
171        self
172    }
173
174    /// Sets whether the database is exclusive.
175    pub fn set_exclusive(&mut self, exclusive: bool) -> &mut Self {
176        self.exclusive = exclusive;
177        self
178    }
179
180    /// Sets the node maximum entries.
181    pub fn set_node_max_entries(&mut self, node_max_entries: u32) -> &mut Self {
182        self.node_max_entries = node_max_entries;
183        self
184    }
185
186    /// Builder-style method to set allow_create.
187    pub fn with_allow_create(mut self, allow_create: bool) -> Self {
188        self.allow_create = allow_create;
189        self
190    }
191
192    /// Builder-style method to set sorted_duplicates.
193    pub fn with_sorted_duplicates(mut self, sorted_duplicates: bool) -> Self {
194        self.sorted_duplicates = sorted_duplicates;
195        self
196    }
197
198    /// Builder-style method to set transactional.
199    pub fn with_transactional(mut self, transactional: bool) -> Self {
200        self.transactional = transactional;
201        self
202    }
203
204    /// Builder-style method to set read_only.
205    pub fn with_read_only(mut self, read_only: bool) -> Self {
206        self.read_only = read_only;
207        self
208    }
209
210    /// Builder-style method to set temporary.
211    pub fn with_temporary(mut self, temporary: bool) -> Self {
212        self.temporary = temporary;
213        self
214    }
215
216    /// Builder-style method to set deferred_write.
217    pub fn with_deferred_write(mut self, deferred_write: bool) -> Self {
218        self.deferred_write = deferred_write;
219        self
220    }
221
222    /// Sets whether this database participates in replication.
223    pub fn set_replicated(&mut self, replicated: bool) -> &mut Self {
224        self.replicated = replicated;
225        self
226    }
227
228    /// Builder-style method to set replicated.
229    pub fn with_replicated(mut self, replicated: bool) -> Self {
230        self.replicated = replicated;
231        self
232    }
233
234    /// Sets whether key prefix compression is enabled.
235    pub fn set_key_prefixing(&mut self, key_prefixing: bool) -> &mut Self {
236        self.key_prefixing = key_prefixing;
237        self
238    }
239
240    /// Builder-style method to set key_prefixing.
241    pub fn with_key_prefixing(mut self, key_prefixing: bool) -> Self {
242        self.key_prefixing = key_prefixing;
243        self
244    }
245
246    /// Sets the per-database cache eviction mode.
247    pub fn set_cache_mode(&mut self, cache_mode: CacheMode) -> &mut Self {
248        self.cache_mode = cache_mode;
249        self
250    }
251
252    /// Builder-style method to set cache_mode.
253    pub fn with_cache_mode(mut self, cache_mode: CacheMode) -> Self {
254        self.cache_mode = cache_mode;
255        self
256    }
257
258    /// Sets whether BIN-deltas are written to the log.
259    pub fn set_bin_delta(&mut self, bin_delta: bool) -> &mut Self {
260        self.bin_delta = bin_delta;
261        self
262    }
263
264    /// Builder-style method to set bin_delta.
265    pub fn with_bin_delta(mut self, bin_delta: bool) -> Self {
266        self.bin_delta = bin_delta;
267        self
268    }
269
270    /// Sets whether to reuse existing config when opening an existing database.
271    pub fn set_use_existing_config(&mut self, v: bool) -> &mut Self {
272        self.use_existing_config = v;
273        self
274    }
275
276    /// Builder-style method to set use_existing_config.
277    pub fn with_use_existing_config(mut self, v: bool) -> Self {
278        self.use_existing_config = v;
279        self
280    }
281}
282
283impl Default for DatabaseConfig {
284    fn default() -> Self {
285        Self::new()
286    }
287}
288
289#[cfg(test)]
290mod tests {
291    use super::*;
292
293    #[test]
294    fn test_new() {
295        let config = DatabaseConfig::new();
296        assert!(!config.allow_create);
297        assert!(!config.sorted_duplicates);
298        assert!(!config.transactional);
299        assert!(!config.read_only);
300        assert!(!config.temporary);
301        assert!(!config.deferred_write);
302    }
303
304    #[test]
305    fn test_set_allow_create() {
306        let mut config = DatabaseConfig::new();
307        config.set_allow_create(true);
308        assert!(config.allow_create);
309    }
310
311    #[test]
312    fn test_set_sorted_duplicates() {
313        let mut config = DatabaseConfig::new();
314        config.set_sorted_duplicates(true);
315        assert!(config.sorted_duplicates);
316    }
317
318    #[test]
319    fn test_set_transactional() {
320        let mut config = DatabaseConfig::new();
321        config.set_transactional(true);
322        assert!(config.transactional);
323    }
324
325    #[test]
326    fn test_set_read_only() {
327        let mut config = DatabaseConfig::new();
328        config.set_read_only(true);
329        assert!(config.read_only);
330    }
331
332    #[test]
333    fn test_set_temporary() {
334        let mut config = DatabaseConfig::new();
335        config.set_temporary(true);
336        assert!(config.temporary);
337    }
338
339    #[test]
340    fn test_set_deferred_write() {
341        let mut config = DatabaseConfig::new();
342        config.set_deferred_write(true);
343        assert!(config.deferred_write);
344    }
345
346    #[test]
347    fn test_with_allow_create() {
348        let config = DatabaseConfig::new().with_allow_create(true);
349        assert!(config.allow_create);
350    }
351
352    #[test]
353    fn test_with_sorted_duplicates() {
354        let config = DatabaseConfig::new().with_sorted_duplicates(true);
355        assert!(config.sorted_duplicates);
356    }
357
358    #[test]
359    fn test_with_transactional() {
360        let config = DatabaseConfig::new().with_transactional(true);
361        assert!(config.transactional);
362    }
363
364    #[test]
365    fn test_with_read_only() {
366        let config = DatabaseConfig::new().with_read_only(true);
367        assert!(config.read_only);
368    }
369
370    #[test]
371    fn test_with_temporary() {
372        let config = DatabaseConfig::new().with_temporary(true);
373        assert!(config.temporary);
374    }
375
376    #[test]
377    fn test_with_deferred_write() {
378        let config = DatabaseConfig::new().with_deferred_write(true);
379        assert!(config.deferred_write);
380    }
381
382    #[test]
383    fn test_builder_chain() {
384        let config = DatabaseConfig::new()
385            .with_allow_create(true)
386            .with_sorted_duplicates(true)
387            .with_transactional(true);
388        assert!(config.allow_create);
389        assert!(config.sorted_duplicates);
390        assert!(config.transactional);
391    }
392
393    #[test]
394    fn test_default() {
395        let config = DatabaseConfig::default();
396        assert!(!config.allow_create);
397        assert!(!config.transactional);
398    }
399
400    #[test]
401    fn test_clone() {
402        let config1 = DatabaseConfig::new().with_allow_create(true);
403        let config2 = config1.clone();
404        assert_eq!(config1, config2);
405    }
406
407    #[test]
408    fn test_equality() {
409        let config1 = DatabaseConfig::new();
410        let config2 = DatabaseConfig::default();
411        assert_eq!(config1, config2);
412
413        let config3 = DatabaseConfig::new().with_allow_create(true);
414        assert_ne!(config1, config3);
415    }
416
417    #[test]
418    fn test_override_comparators() {
419        let mut config = DatabaseConfig::new();
420        config.set_override_btree_comparator(true);
421        config.set_override_duplicate_comparator(true);
422        assert!(config.override_btree_comparator);
423        assert!(config.override_duplicate_comparator);
424    }
425
426    #[test]
427    fn test_exclusive() {
428        let mut config = DatabaseConfig::new();
429        assert!(!config.exclusive);
430        config.set_exclusive(true);
431        assert!(config.exclusive);
432    }
433
434    #[test]
435    fn test_node_max_entries() {
436        let mut config = DatabaseConfig::new();
437        assert_eq!(config.node_max_entries, 0);
438        config.set_node_max_entries(128);
439        assert_eq!(config.node_max_entries, 128);
440    }
441}