Skip to content

Commit 2e11b35

Browse files
committed
feat: enhance node growth and shrinking with error handling
1 parent acf563d commit 2e11b35

File tree

1 file changed

+52
-25
lines changed

1 file changed

+52
-25
lines changed

src-tauri/src/search_engine/art_v5.rs

Lines changed: 52 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -113,9 +113,16 @@ impl ARTNode {
113113
ARTNode::Node4(n) => {
114114
// Check if we need to grow first before taking the child
115115
if n.keys.len() >= NODE4_MAX && !n.keys.contains(&key) {
116-
let grown_node = self.grow();
117-
grown = true;
118-
*self = grown_node;
116+
match self.grow() {
117+
Ok(grown_node) => {
118+
grown = true;
119+
*self = grown_node;
120+
}
121+
Err(e) => {
122+
log_error!("Failed to grow node: {}", e);
123+
return false;
124+
}
125+
}
119126
let added = self.add_child(key, child.take());
120127
added
121128
} else {
@@ -124,9 +131,16 @@ impl ARTNode {
124131
}
125132
ARTNode::Node16(n) => {
126133
if n.keys.len() >= NODE16_MAX && !n.keys.contains(&key) {
127-
let grown_node = self.grow();
128-
grown = true;
129-
*self = grown_node;
134+
match self.grow() {
135+
Ok(grown_node) => {
136+
grown = true;
137+
*self = grown_node;
138+
}
139+
Err(e) => {
140+
log_error!("Failed to grow node: {}", e);
141+
return false;
142+
}
143+
}
130144
let added = self.add_child(key, child.take());
131145
added
132146
} else {
@@ -135,9 +149,16 @@ impl ARTNode {
135149
}
136150
ARTNode::Node48(n) => {
137151
if n.size >= NODE48_MAX && n.child_index[key as usize].is_none() {
138-
let grown_node = self.grow();
139-
grown = true;
140-
*self = grown_node;
152+
match self.grow() {
153+
Ok(grown_node) => {
154+
grown = true;
155+
*self = grown_node;
156+
}
157+
Err(e) => {
158+
log_error!("Failed to grow node: {}", e);
159+
return false;
160+
}
161+
}
141162
let added = self.add_child(key, child.take());
142163
added
143164
} else {
@@ -175,26 +196,32 @@ impl ARTNode {
175196
let removed = n.remove_child(key);
176197
if n.keys.len() < NODE4_MAX / 2 {
177198
// Shrink to Node4
178-
let shrunk = self.shrink();
179-
*self = shrunk;
199+
match self.shrink() {
200+
Ok(shrunk) => *self = shrunk,
201+
Err(e) => log_error!("Failed to shrink node: {}", e),
202+
}
180203
}
181204
removed
182205
}
183206
ARTNode::Node48(n) => {
184207
let removed = n.remove_child(key);
185208
if n.size < NODE16_MAX / 2 {
186209
// Shrink to Node16
187-
let shrunk = self.shrink();
188-
*self = shrunk;
210+
match self.shrink() {
211+
Ok(shrunk) => *self = shrunk,
212+
Err(e) => log_error!("Failed to shrink node: {}", e),
213+
}
189214
}
190215
removed
191216
}
192217
ARTNode::Node256(n) => {
193218
let removed = n.remove_child(key);
194219
if n.size < NODE48_MAX / 2 {
195220
// Shrink to Node48
196-
let shrunk = self.shrink();
197-
*self = shrunk;
221+
match self.shrink() {
222+
Ok(shrunk) => *self = shrunk,
223+
Err(e) => log_error!("Failed to shrink node: {}", e),
224+
}
198225
}
199226
removed
200227
}
@@ -221,7 +248,7 @@ impl ARTNode {
221248
}
222249

223250
// Grow to a larger node type
224-
fn grow(&mut self) -> Self {
251+
fn grow(&mut self) -> Result<Self, String> {
225252
match self {
226253
ARTNode::Node4(n) => {
227254
let mut n16 = Node16::new();
@@ -235,7 +262,7 @@ impl ARTNode {
235262
let child_opt = n.remove_child(key);
236263
n16.add_child(key, child_opt);
237264
}
238-
ARTNode::Node16(n16)
265+
Ok(ARTNode::Node16(n16))
239266
}
240267
ARTNode::Node16(n) => {
241268
let mut n48 = Node48::new();
@@ -248,7 +275,7 @@ impl ARTNode {
248275
n48.add_child(key, Some(child_node));
249276
}
250277
}
251-
ARTNode::Node48(n48)
278+
Ok(ARTNode::Node48(n48))
252279
}
253280
ARTNode::Node48(n) => {
254281
let mut n256 = Node256::new();
@@ -262,17 +289,17 @@ impl ARTNode {
262289
n256.add_child(key, Some(child_node));
263290
}
264291
}
265-
ARTNode::Node256(n256)
292+
Ok(ARTNode::Node256(n256))
266293
}
267294
ARTNode::Node256(_) => {
268295
log_error!("Node256 cannot be grown further");
269-
panic!("Node256 cannot be grown further");
296+
Err("Node256 cannot be grown further".to_string())
270297
}
271298
}
272299
}
273300

274301
// Shrink to a smaller node type
275-
fn shrink(&mut self) -> Self {
302+
fn shrink(&mut self) -> Result<Self, String> {
276303
match self {
277304
ARTNode::Node16(n) => {
278305
let mut n4 = Node4::new();
@@ -283,7 +310,7 @@ impl ARTNode {
283310
n4.keys.push(n.keys[i]);
284311
n4.children.push(n.children[i].take());
285312
}
286-
ARTNode::Node4(n4)
313+
Ok(ARTNode::Node4(n4))
287314
}
288315
ARTNode::Node48(n) => {
289316
let mut n16 = Node16::new();
@@ -303,7 +330,7 @@ impl ARTNode {
303330
}
304331
}
305332
}
306-
ARTNode::Node16(n16)
333+
Ok(ARTNode::Node16(n16))
307334
}
308335
ARTNode::Node256(n) => {
309336
let mut n48 = Node48::new();
@@ -322,11 +349,11 @@ impl ARTNode {
322349
}
323350
}
324351
n48.size = count;
325-
ARTNode::Node48(n48)
352+
Ok(ARTNode::Node48(n48))
326353
}
327354
_ => {
328355
log_error!("Cannot shrink node smaller than Node4");
329-
panic!("Cannot shrink node smaller than Node4");
356+
Err("Cannot shrink node smaller than Node4".to_string())
330357
}
331358
}
332359
}

0 commit comments

Comments
 (0)