noxu_db/
database_config.rs1use crate::cache_mode::CacheMode;
5
6#[derive(Debug, Clone, PartialEq, Eq)]
13pub struct DatabaseConfig {
14 pub allow_create: bool,
16
17 pub sorted_duplicates: bool,
19
20 pub transactional: bool,
22
23 pub read_only: bool,
25
26 pub temporary: bool,
30
31 pub deferred_write: bool,
35
36 pub override_btree_comparator: bool,
44
45 pub override_duplicate_comparator: bool,
49
50 pub exclusive: bool,
56
57 pub node_max_entries: u32,
59
60 pub replicated: bool,
66
67 pub key_prefixing: bool,
72
73 pub cache_mode: CacheMode,
79
80 pub bin_delta: bool,
85
86 pub use_existing_config: bool,
93}
94
95impl DatabaseConfig {
96 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, use_existing_config: false,
114 }
115 }
116
117 pub fn set_allow_create(&mut self, allow_create: bool) -> &mut Self {
119 self.allow_create = allow_create;
120 self
121 }
122
123 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 pub fn set_transactional(&mut self, transactional: bool) -> &mut Self {
134 self.transactional = transactional;
135 self
136 }
137
138 pub fn set_read_only(&mut self, read_only: bool) -> &mut Self {
140 self.read_only = read_only;
141 self
142 }
143
144 pub fn set_temporary(&mut self, temporary: bool) -> &mut Self {
146 self.temporary = temporary;
147 self
148 }
149
150 pub fn set_deferred_write(&mut self, deferred_write: bool) -> &mut Self {
152 self.deferred_write = deferred_write;
153 self
154 }
155
156 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 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 pub fn set_exclusive(&mut self, exclusive: bool) -> &mut Self {
176 self.exclusive = exclusive;
177 self
178 }
179
180 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 pub fn with_allow_create(mut self, allow_create: bool) -> Self {
188 self.allow_create = allow_create;
189 self
190 }
191
192 pub fn with_sorted_duplicates(mut self, sorted_duplicates: bool) -> Self {
194 self.sorted_duplicates = sorted_duplicates;
195 self
196 }
197
198 pub fn with_transactional(mut self, transactional: bool) -> Self {
200 self.transactional = transactional;
201 self
202 }
203
204 pub fn with_read_only(mut self, read_only: bool) -> Self {
206 self.read_only = read_only;
207 self
208 }
209
210 pub fn with_temporary(mut self, temporary: bool) -> Self {
212 self.temporary = temporary;
213 self
214 }
215
216 pub fn with_deferred_write(mut self, deferred_write: bool) -> Self {
218 self.deferred_write = deferred_write;
219 self
220 }
221
222 pub fn set_replicated(&mut self, replicated: bool) -> &mut Self {
224 self.replicated = replicated;
225 self
226 }
227
228 pub fn with_replicated(mut self, replicated: bool) -> Self {
230 self.replicated = replicated;
231 self
232 }
233
234 pub fn set_key_prefixing(&mut self, key_prefixing: bool) -> &mut Self {
236 self.key_prefixing = key_prefixing;
237 self
238 }
239
240 pub fn with_key_prefixing(mut self, key_prefixing: bool) -> Self {
242 self.key_prefixing = key_prefixing;
243 self
244 }
245
246 pub fn set_cache_mode(&mut self, cache_mode: CacheMode) -> &mut Self {
248 self.cache_mode = cache_mode;
249 self
250 }
251
252 pub fn with_cache_mode(mut self, cache_mode: CacheMode) -> Self {
254 self.cache_mode = cache_mode;
255 self
256 }
257
258 pub fn set_bin_delta(&mut self, bin_delta: bool) -> &mut Self {
260 self.bin_delta = bin_delta;
261 self
262 }
263
264 pub fn with_bin_delta(mut self, bin_delta: bool) -> Self {
266 self.bin_delta = bin_delta;
267 self
268 }
269
270 pub fn set_use_existing_config(&mut self, v: bool) -> &mut Self {
272 self.use_existing_config = v;
273 self
274 }
275
276 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}