noxu_dbi/
database_config.rs1use noxu_tree::KeyComparatorFn;
5
6#[derive(Clone)]
14pub struct ConfigComparator {
15 pub identity: String,
17 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#[derive(Debug, Clone)]
33pub struct DatabaseConfig {
34 pub allow_create: bool,
36 pub sorted_duplicates: bool,
38 pub key_prefixing: bool,
40 pub temporary: bool,
42 pub transactional: bool,
44 pub read_only: bool,
46 pub node_max_entries: i32,
48 pub deferred_write: bool,
52 pub btree_comparator: Option<ConfigComparator>,
56 pub duplicate_comparator: Option<ConfigComparator>,
60 pub override_btree_comparator: bool,
63 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 pub fn new() -> Self {
89 Self::default()
90 }
91
92 pub fn set_allow_create(&mut self, allow_create: bool) -> &mut Self {
94 self.allow_create = allow_create;
95 self
96 }
97
98 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 pub fn set_key_prefixing(&mut self, key_prefixing: bool) -> &mut Self {
109 self.key_prefixing = key_prefixing;
110 self
111 }
112
113 pub fn set_temporary(&mut self, temporary: bool) -> &mut Self {
115 self.temporary = temporary;
116 self
117 }
118
119 pub fn set_transactional(&mut self, transactional: bool) -> &mut Self {
121 self.transactional = transactional;
122 self
123 }
124
125 pub fn set_read_only(&mut self, read_only: bool) -> &mut Self {
127 self.read_only = read_only;
128 self
129 }
130
131 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}