11use crate :: {
22 changes:: Changes , keys:: InfiniteKeys , min_keys, nearest_node, put:: propagate_changes_up_tree,
3- Child , HyperbeeError , KeyValue , KeyValueData , NodePath , SharedNode , Tree , MAX_KEYS ,
3+ wchildren , Child , HyperbeeError , KeyValue , KeyValueData , NodePath , SharedNode , Tree , MAX_KEYS ,
44} ;
55
66use tracing:: info;
@@ -74,22 +74,14 @@ impl Side {
7474 /// but with bounds checking
7575 async fn get_donor_index ( & self , father : SharedNode , deficient_index : usize ) -> Option < usize > {
7676 match * self {
77- Left => {
78- if deficient_index == 0 {
79- None
80- } else {
81- Some ( deficient_index - 1 )
82- }
83- }
84- Right => {
85- let donor_index = deficient_index + 1 ;
86- let n_children = father. read ( ) . await . n_children ( ) . await ;
87- if donor_index >= n_children {
88- None
89- } else {
90- Some ( donor_index)
91- }
92- }
77+ Left => match deficient_index == 0 {
78+ true => None ,
79+ false => Some ( deficient_index - 1 ) ,
80+ } ,
81+ Right => match deficient_index + 1 >= father. read ( ) . await . n_children ( ) . await {
82+ true => None ,
83+ false => Some ( deficient_index + 1 ) ,
84+ } ,
9385 }
9486 }
9587
@@ -118,16 +110,8 @@ impl Side {
118110 return None ;
119111 }
120112 Some ( match self {
121- Right => donor. read ( ) . await . children . children . write ( ) . await . remove ( 0 ) ,
122- Left => donor
123- . read ( )
124- . await
125- . children
126- . children
127- . write ( )
128- . await
129- . pop ( )
130- . expect ( "children is non empty, checked above" ) ,
113+ Right => wchildren ! ( donor) . remove ( 0 ) ,
114+ Left => wchildren ! ( donor) . pop ( ) . expect ( "node is not a leaf" ) ,
131115 } )
132116 }
133117
@@ -158,27 +142,13 @@ impl Side {
158142 Right => {
159143 deficient_child. write ( ) . await . keys . push ( key) ;
160144 if let Some ( child) = child {
161- deficient_child
162- . read ( )
163- . await
164- . children
165- . children
166- . write ( )
167- . await
168- . push ( child) ;
145+ wchildren ! ( deficient_child) . push ( child) ;
169146 }
170147 }
171148 Left => {
172149 deficient_child. write ( ) . await . keys . insert ( 0 , key) ;
173150 if let Some ( child) = child {
174- deficient_child
175- . read ( )
176- . await
177- . children
178- . children
179- . write ( )
180- . await
181- . insert ( 0 , child) ;
151+ wchildren ! ( deficient_child) . insert ( 0 , child) ;
182152 }
183153 }
184154 }
@@ -228,9 +198,8 @@ impl Side {
228198 )
229199 . await ;
230200
231- father. read ( ) . await . children . children . write ( ) . await [ donor_index] = changes. add_node ( donor) ;
232- father. read ( ) . await . children . children . write ( ) . await [ deficient_index] =
233- changes. add_node ( deficient_child) ;
201+ wchildren ! ( father) [ donor_index] = changes. add_node ( donor) ;
202+ wchildren ! ( father) [ deficient_index] = changes. add_node ( deficient_child) ;
234203
235204 Ok ( father)
236205 }
@@ -291,12 +260,7 @@ impl Side {
291260 Right => deficient_index + 1 ,
292261 Left => deficient_index,
293262 } ;
294- father
295- . read ( )
296- . await
297- . children
298- . splice ( right_child_index..=right_child_index, vec ! [ ] )
299- . await ;
263+ wchildren ! ( father) . splice ( right_child_index..=right_child_index, vec ! [ ] ) ;
300264
301265 // Move donated key from father and RHS keys into LHS
302266 let n_left_keys = left. read ( ) . await . keys . len ( ) ;
@@ -308,24 +272,15 @@ impl Side {
308272 . splice ( n_left_keys..n_left_keys, keys_to_add) ;
309273 // Move RHS children into LHS
310274 let n_left_children = left. read ( ) . await . n_children ( ) . await ;
311- left. write ( )
312- . await
313- . children
314- . splice (
315- n_left_children..n_left_children,
316- right. read ( ) . await . children . children . write ( ) . await . drain ( ..) ,
317- )
318- . await ;
275+ wchildren ! ( left) . splice (
276+ n_left_children..n_left_children,
277+ wchildren ! ( right) . drain ( ..) ,
278+ ) ;
319279 // Replace new LHS child in the father
320280 info ! ( "add merged nodes father changes: {left:#?}" ) ;
321281 let left_ref = changes. add_node ( left. clone ( ) ) ;
322282
323- father
324- . read ( )
325- . await
326- . children
327- . splice ( ( right_child_index - 1 ) ..( right_child_index) , vec ! [ left_ref] )
328- . await ;
283+ wchildren ! ( father) . splice ( ( right_child_index - 1 ) ..( right_child_index) , vec ! [ left_ref] ) ;
329284 Ok ( Some ( father) )
330285 }
331286}
@@ -429,8 +384,10 @@ async fn repair(
429384 . read ( )
430385 . await
431386 . children
432- . get_child_ref ( 0 )
433- . await ;
387+ . children
388+ . read ( )
389+ . await [ 0 ]
390+ . clone ( ) ;
434391 }
435392
436393 // if no more nodes, or father does not need repair, we are done
0 commit comments