Skip to content

Commit 73567c2

Browse files
committed
add wchildren for ergonomics
1 parent 1dbc509 commit 73567c2

File tree

3 files changed

+45
-96
lines changed

3 files changed

+45
-96
lines changed

src/del.rs

Lines changed: 25 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use 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

66
use 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

src/lib.rs

Lines changed: 9 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ mod external;
2323

2424
use std::{
2525
fmt::Debug,
26-
ops::{Deref, Range, RangeBounds},
26+
ops::{Deref, Range},
2727
sync::Arc,
2828
};
2929

@@ -128,14 +128,10 @@ impl Children {
128128
.splice(index..(index + replace_split_child), new_children);
129129
}
130130

131-
async fn get_child_ref(&self, index: usize) -> Child {
132-
self.children.read().await[index].clone()
133-
}
134-
135131
#[tracing::instrument(skip(self))]
136132
async fn get_child(&self, index: usize) -> Result<Shared<Node>, HyperbeeError> {
137133
let (seq, offset) = {
138-
let child_ref = &self.get_child_ref(index).await;
134+
let child_ref = &self.children.read().await[index].clone();
139135
(child_ref.seq, child_ref.offset)
140136
};
141137
let block = self
@@ -151,22 +147,6 @@ impl Children {
151147
async fn len(&self) -> usize {
152148
self.children.read().await.len()
153149
}
154-
155-
async fn splice<R: RangeBounds<usize>, I: IntoIterator<Item = Child>>(
156-
&self,
157-
range: R,
158-
replace_with: I,
159-
) -> Vec<Child> {
160-
// Leaf node do nothing. Should we Err instead?
161-
if self.children.read().await.is_empty() {
162-
return vec![];
163-
}
164-
self.children
165-
.write()
166-
.await
167-
.splice(range, replace_with)
168-
.collect()
169-
}
170150
}
171151

172152
impl Debug for Children {
@@ -184,6 +164,13 @@ impl Debug for Children {
184164
}
185165
}
186166

167+
macro_rules! wchildren {
168+
($node:expr) => {
169+
$node.read().await.children.children.write().await
170+
};
171+
}
172+
pub(crate) use wchildren;
173+
187174
/// A node of the B-Tree within the [`Hyperbee`]
188175
struct Node {
189176
keys: Vec<KeyValue>,

src/put.rs

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ use tokio::sync::RwLock;
44
use tracing::trace;
55

66
use crate::{
7-
changes::Changes, nearest_node, Child, HyperbeeError, KeyValue, KeyValueData, Node, NodePath,
8-
SharedNode, Tree, MAX_KEYS,
7+
changes::Changes, nearest_node, wchildren, Child, HyperbeeError, KeyValue, KeyValueData, Node,
8+
NodePath, SharedNode, Tree, MAX_KEYS,
99
};
1010

1111
/// After making changes to a tree, this function updates parent references all the way to the
@@ -24,8 +24,8 @@ pub async fn propagate_changes_up_tree(
2424
None => break,
2525
Some(x) => x,
2626
};
27-
node.read().await.children.children.write().await[index] = cur_child;
28-
cur_child = changes.add_node(node.clone());
27+
wchildren!(node)[index] = cur_child;
28+
cur_child = changes.add_node(node);
2929
}
3030
}
3131

@@ -44,13 +44,18 @@ impl Node {
4444
);
4545
let left = Node::new(
4646
self.keys.splice(0..key_median_index, vec![]).collect(),
47-
self.children.splice(0..children_median_index, vec![]).await,
47+
self.children
48+
.children
49+
.write()
50+
.await
51+
.splice(0..children_median_index, vec![])
52+
.collect(),
4853
self.blocks.clone(),
4954
);
5055
let mid_key = self.keys.remove(0);
5156
let right = Node::new(
5257
self.keys.drain(..).collect(),
53-
self.children.splice(0.., vec![]).await,
58+
self.children.children.write().await.drain(..).collect(),
5459
self.blocks.clone(),
5560
);
5661
(

0 commit comments

Comments
 (0)