@@ -8,7 +8,7 @@ use tracing::trace;
8
8
use crate :: {
9
9
error:: { InsertionError , ShardTreeError } ,
10
10
store:: { Checkpoint , ShardStore } ,
11
- IncompleteAt , LocatedPrunableTree , LocatedTree , RetentionFlags , ShardTree , Tree ,
11
+ IncompleteAt , LocatedPrunableTree , LocatedTree , PrunableTree , RetentionFlags , ShardTree , Tree ,
12
12
} ;
13
13
14
14
impl <
@@ -215,7 +215,8 @@ impl<H: Hashable + Clone + PartialEq> LocatedPrunableTree<H> {
215
215
// fragments up the stack until we get the largest possible subtree
216
216
while let Some ( ( potential_sibling, marked) ) = fragments. pop ( ) {
217
217
if potential_sibling. root_addr . parent ( ) == subtree. root_addr . parent ( ) {
218
- subtree = unite ( potential_sibling, subtree, prune_below) ;
218
+ subtree = unite ( potential_sibling, subtree, prune_below)
219
+ . expect ( "subtree is non-Nil" ) ;
219
220
} else {
220
221
// this is not a sibling node, so we push it back on to the stack
221
222
// and are done
@@ -238,7 +239,9 @@ impl<H: Hashable + Clone + PartialEq> LocatedPrunableTree<H> {
238
239
let minimal_tree_addr =
239
240
Address :: from ( position_range. start ) . common_ancestor ( & last_position. into ( ) ) ;
240
241
trace ! ( "Building minimal tree at {:?}" , minimal_tree_addr) ;
241
- build_minimal_tree ( fragments, minimal_tree_addr, prune_below) . map (
242
+ build_minimal_tree ( fragments, minimal_tree_addr, prune_below)
243
+ . expect ( "construction of minimal tree does not result in creation of invalid parent nodes" )
244
+ . map (
242
245
|( to_insert, contains_marked, incomplete) | BatchInsertionResult {
243
246
subtree : to_insert,
244
247
contains_marked,
@@ -263,16 +266,18 @@ fn unite<H: Hashable + Clone + PartialEq>(
263
266
lroot : LocatedPrunableTree < H > ,
264
267
rroot : LocatedPrunableTree < H > ,
265
268
prune_below : Level ,
266
- ) -> LocatedPrunableTree < H > {
269
+ ) -> Result < LocatedPrunableTree < H > , Address > {
267
270
assert_eq ! ( lroot. root_addr. parent( ) , rroot. root_addr. parent( ) ) ;
268
- LocatedTree {
271
+ Ok ( LocatedTree {
269
272
root_addr : lroot. root_addr . parent ( ) ,
270
273
root : if lroot. root_addr . level ( ) < prune_below {
271
- Tree :: unite ( lroot. root_addr . level ( ) , None , lroot. root , rroot. root )
274
+ PrunableTree :: unite ( lroot. root_addr . level ( ) , None , lroot. root , rroot. root )
275
+ . map_err ( |_| lroot. root_addr ) ?
272
276
} else {
273
- Tree :: parent ( None , lroot. root , rroot. root )
277
+ PrunableTree :: parent_checked ( None , lroot. root , rroot. root )
278
+ . map_err ( |_| lroot. root_addr ) ?
274
279
} ,
275
- }
280
+ } )
276
281
}
277
282
278
283
/// Combines the given subtree with an empty sibling node to obtain the next level
@@ -286,7 +291,7 @@ fn combine_with_empty<H: Hashable + Clone + PartialEq>(
286
291
incomplete : & mut Vec < IncompleteAt > ,
287
292
contains_marked : bool ,
288
293
prune_below : Level ,
289
- ) -> LocatedPrunableTree < H > {
294
+ ) -> Result < LocatedPrunableTree < H > , Address > {
290
295
assert_eq ! ( expect_left_child, root. root_addr. is_left_child( ) ) ;
291
296
let sibling_addr = root. root_addr . sibling ( ) ;
292
297
incomplete. push ( IncompleteAt {
@@ -313,27 +318,27 @@ fn build_minimal_tree<H: Hashable + Clone + PartialEq>(
313
318
mut xs : Vec < ( LocatedPrunableTree < H > , bool ) > ,
314
319
root_addr : Address ,
315
320
prune_below : Level ,
316
- ) -> Option < ( LocatedPrunableTree < H > , bool , Vec < IncompleteAt > ) > {
321
+ ) -> Result < Option < ( LocatedPrunableTree < H > , bool , Vec < IncompleteAt > ) > , Address > {
317
322
// First, consume the stack from the right, building up a single tree
318
323
// until we can't combine any more.
319
324
if let Some ( ( mut cur, mut contains_marked) ) = xs. pop ( ) {
320
325
let mut incomplete = vec ! [ ] ;
321
326
while let Some ( ( top, top_marked) ) = xs. pop ( ) {
322
327
while cur. root_addr . level ( ) < top. root_addr . level ( ) {
323
- cur = combine_with_empty ( cur, true , & mut incomplete, top_marked, prune_below) ;
328
+ cur = combine_with_empty ( cur, true , & mut incomplete, top_marked, prune_below) ? ;
324
329
}
325
330
326
331
if cur. root_addr . level ( ) == top. root_addr . level ( ) {
327
332
contains_marked = contains_marked || top_marked;
328
333
if cur. root_addr . is_right_child ( ) {
329
334
// We have a left child and a right child, so unite them.
330
- cur = unite ( top, cur, prune_below) ;
335
+ cur = unite ( top, cur, prune_below) ? ;
331
336
} else {
332
337
// This is a left child, so we build it up one more level and then
333
338
// we've merged as much as we can from the right and need to work from
334
339
// the left
335
340
xs. push ( ( top, top_marked) ) ;
336
- cur = combine_with_empty ( cur, true , & mut incomplete, top_marked, prune_below) ;
341
+ cur = combine_with_empty ( cur, true , & mut incomplete, top_marked, prune_below) ? ;
337
342
break ;
338
343
}
339
344
} else {
@@ -346,15 +351,15 @@ fn build_minimal_tree<H: Hashable + Clone + PartialEq>(
346
351
347
352
// Ensure we can work from the left in a single pass by making this right-most subtree
348
353
while cur. root_addr . level ( ) + 1 < root_addr. level ( ) {
349
- cur = combine_with_empty ( cur, true , & mut incomplete, contains_marked, prune_below) ;
354
+ cur = combine_with_empty ( cur, true , & mut incomplete, contains_marked, prune_below) ? ;
350
355
}
351
356
352
357
// push our accumulated max-height right hand node back on to the stack.
353
358
xs. push ( ( cur, contains_marked) ) ;
354
359
355
360
// From the stack of subtrees, construct a single sparse tree that can be
356
361
// inserted/merged into the existing tree
357
- let res_tree = xs. into_iter ( ) . fold (
362
+ let res_tree = xs. into_iter ( ) . try_fold (
358
363
None ,
359
364
|acc : Option < LocatedPrunableTree < H > > , ( next_tree, next_marked) | {
360
365
if let Some ( mut prev_tree) = acc {
@@ -368,19 +373,19 @@ fn build_minimal_tree<H: Hashable + Clone + PartialEq>(
368
373
& mut incomplete,
369
374
next_marked,
370
375
prune_below,
371
- ) ;
376
+ ) ? ;
372
377
}
373
378
374
- Some ( unite ( prev_tree, next_tree, prune_below) )
379
+ Ok :: < _ , Address > ( Some ( unite ( prev_tree, next_tree, prune_below) ? ) )
375
380
} else {
376
- Some ( next_tree)
381
+ Ok :: < _ , Address > ( Some ( next_tree) )
377
382
}
378
383
} ,
379
- ) ;
384
+ ) ? ;
380
385
381
- res_tree. map ( |t| ( t, contains_marked, incomplete) )
386
+ Ok ( res_tree. map ( |t| ( t, contains_marked, incomplete) ) )
382
387
} else {
383
- None
388
+ Ok ( None )
384
389
}
385
390
}
386
391
0 commit comments