Skip to main content

noxu_dbi/
database_config.rs

1//! Database configuration.
2//!
3
4use noxu_tree::KeyComparatorFn;
5
6/// A persisted-identity + comparison-function pair threaded from the public
7/// `noxu_db::Comparator` down to `DatabaseImpl`.
8///
9/// The `identity` is the stable string persisted in the database record (the
10/// NameLN data) and re-checked at open; the `func` is the actual comparison
11/// closure threaded into the `Tree`.  JE `DatabaseImpl.btreeComparator` plus
12/// the persisted `btreeComparatorBytes` (the serialized class name).
13#[derive(Clone)]
14pub struct ConfigComparator {
15    /// Stable identity persisted in the database record.
16    pub identity: String,
17    /// The comparison closure threaded into the tree.
18    pub func: KeyComparatorFn,
19}
20
21impl std::fmt::Debug for ConfigComparator {
22    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
23        f.debug_struct("ConfigComparator")
24            .field("identity", &self.identity)
25            .finish()
26    }
27}
28
29/// Configuration for a database.
30///
31///
32#[derive(Debug, Clone)]
33pub struct DatabaseConfig {
34    /// Allow database creation if it doesn't exist.
35    pub allow_create: bool,
36    /// Enable sorted duplicates.
37    pub sorted_duplicates: bool,
38    /// Enable key prefixing compression.
39    pub key_prefixing: bool,
40    /// Database is temporary (not persisted).
41    pub temporary: bool,
42    /// Database operations are transactional.
43    pub transactional: bool,
44    /// Database is read-only.
45    pub read_only: bool,
46    /// Maximum entries per node.
47    pub node_max_entries: i32,
48    /// Deferred write: skip WAL logging; flush only at eviction/checkpoint.
49    ///
50    ///
51    pub deferred_write: bool,
52    /// User-supplied B-tree key comparator (DBI-14).
53    ///
54    /// JE `DatabaseImpl.btreeComparator`.  `None` = unsigned-byte order.
55    pub btree_comparator: Option<ConfigComparator>,
56    /// User-supplied duplicate-data comparator (DBI-14).
57    ///
58    /// JE `DatabaseImpl.duplicateComparator`.
59    pub duplicate_comparator: Option<ConfigComparator>,
60    /// JE `DatabaseConfig.overrideBtreeComparator`: replace a persisted
61    /// comparator instead of rejecting a mismatch.
62    pub override_btree_comparator: bool,
63    /// JE `DatabaseConfig.overrideDuplicateComparator`.
64    pub override_duplicate_comparator: bool,
65}
66
67impl Default for DatabaseConfig {
68    fn default() -> Self {
69        DatabaseConfig {
70            allow_create: false,
71            sorted_duplicates: false,
72            key_prefixing: false,
73            temporary: false,
74            transactional: false,
75            read_only: false,
76            node_max_entries: 128,
77            deferred_write: false,
78            btree_comparator: None,
79            duplicate_comparator: None,
80            override_btree_comparator: false,
81            override_duplicate_comparator: false,
82        }
83    }
84}
85
86impl DatabaseConfig {
87    /// Creates a new DatabaseConfig with default values.
88    pub fn new() -> Self {
89        Self::default()
90    }
91
92    /// Sets the allow_create flag.
93    pub fn set_allow_create(&mut self, allow_create: bool) -> &mut Self {
94        self.allow_create = allow_create;
95        self
96    }
97
98    /// Sets the sorted_duplicates flag.
99    pub fn set_sorted_duplicates(
100        &mut self,
101        sorted_duplicates: bool,
102    ) -> &mut Self {
103        self.sorted_duplicates = sorted_duplicates;
104        self
105    }
106
107    /// Sets the key_prefixing flag.
108    pub fn set_key_prefixing(&mut self, key_prefixing: bool) -> &mut Self {
109        self.key_prefixing = key_prefixing;
110        self
111    }
112
113    /// Sets the temporary flag.
114    pub fn set_temporary(&mut self, temporary: bool) -> &mut Self {
115        self.temporary = temporary;
116        self
117    }
118
119    /// Sets the transactional flag.
120    pub fn set_transactional(&mut self, transactional: bool) -> &mut Self {
121        self.transactional = transactional;
122        self
123    }
124
125    /// Sets the read_only flag.
126    pub fn set_read_only(&mut self, read_only: bool) -> &mut Self {
127        self.read_only = read_only;
128        self
129    }
130
131    /// Sets the maximum entries per node.
132    pub fn set_node_max_entries(&mut self, max: i32) -> &mut Self {
133        self.node_max_entries = max;
134        self
135    }
136}
137
138#[cfg(test)]
139mod tests {
140    use super::*;
141
142    #[test]
143    fn test_default() {
144        let config = DatabaseConfig::default();
145        assert!(!config.allow_create);
146        assert!(!config.sorted_duplicates);
147        assert!(!config.key_prefixing);
148        assert!(!config.temporary);
149        assert!(!config.transactional);
150        assert!(!config.read_only);
151        assert_eq!(config.node_max_entries, 128);
152    }
153
154    #[test]
155    fn test_new() {
156        let config = DatabaseConfig::new();
157        assert!(!config.allow_create);
158    }
159
160    #[test]
161    fn test_setters() {
162        let mut config = DatabaseConfig::new();
163
164        config.set_allow_create(true);
165        assert!(config.allow_create);
166
167        config.set_sorted_duplicates(true);
168        assert!(config.sorted_duplicates);
169
170        config.set_key_prefixing(true);
171        assert!(config.key_prefixing);
172
173        config.set_temporary(true);
174        assert!(config.temporary);
175
176        config.set_transactional(true);
177        assert!(config.transactional);
178
179        config.set_read_only(true);
180        assert!(config.read_only);
181
182        config.set_node_max_entries(256);
183        assert_eq!(config.node_max_entries, 256);
184    }
185
186    #[test]
187    fn test_builder_pattern() {
188        let config = DatabaseConfig::new()
189            .set_allow_create(true)
190            .set_transactional(true)
191            .set_node_max_entries(512)
192            .clone();
193
194        assert!(config.allow_create);
195        assert!(config.transactional);
196        assert_eq!(config.node_max_entries, 512);
197    }
198}