1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
use crate::{
raw::{Node, NodePtr, NodeType},
AsBytes,
};
/// This type alias represents an optional pointer to a leaf node.
pub(crate) type OptionalLeafPtr<K, V, const PREFIX_LEN: usize> =
Option<NodePtr<PREFIX_LEN, LeafNode<K, V, PREFIX_LEN>>>;
/// Node that contains a single leaf value.
#[derive(Debug)]
#[repr(align(8))]
pub struct LeafNode<K, V, const PREFIX_LEN: usize> {
/// The leaf value.
value: V,
/// The full key that the `value` was stored with.
key: K,
/// Pointer to the previous leaf node in the trie. If the value is `None`,
/// then this is the first leaf.
pub(crate) previous: OptionalLeafPtr<K, V, PREFIX_LEN>,
/// Pointer to the next leaf node in the trie. If the value is `None`,
/// then this is the last leaf.
pub(crate) next: OptionalLeafPtr<K, V, PREFIX_LEN>,
}
impl<K, V, const PREFIX_LEN: usize> LeafNode<K, V, PREFIX_LEN>
where
K: AsBytes,
{
/// Insert the leaf node pointed to by `this_ptr` into the linked list that
/// `previous_sibling_ptr` belongs to, placing the "this" leaf node after
/// the "previous sibling" in the list.
///
/// # Safety
///
/// This function requires that no other operation is concurrently modifying
/// or reading the `this_ptr` leaf node, the `previous_sibling_ptr` leaf
/// node, and the sibling leaf node of `previous_sibling_ptr`.
pub unsafe fn insert_after(
this_ptr: NodePtr<PREFIX_LEN, Self>,
previous_sibling_ptr: NodePtr<PREFIX_LEN, Self>,
) {
// SAFETY: Covered by safety doc of this function
let (this, previous_sibling) =
unsafe { (this_ptr.as_mut(), previous_sibling_ptr.as_mut()) };
if cfg!(debug_assertions) {
debug_assert!(
this.previous.is_none(),
"previous ptr should be None on insert into linked list"
);
debug_assert!(
this.next.is_none(),
"next ptr should be None on insert into linked list"
);
debug_assert!(
previous_sibling.key.as_bytes() < this.key.as_bytes(),
"sibling must be ordered before this leaf in the trie"
);
}
this.previous = Some(previous_sibling_ptr);
this.next = previous_sibling.next;
if let Some(next_sibling_ptr) = previous_sibling.next {
// SAFETY: Covered by safety doc of this function
let next_sibling = unsafe { next_sibling_ptr.as_mut() };
next_sibling.previous = Some(this_ptr);
}
previous_sibling.next = Some(this_ptr);
}
/// Insert the leaf node pointed to by `this_ptr` into the linked list that
/// `next_sibling_ptr` belongs to, placing the "this" leaf node before
/// the "next sibling" in the list.
///
/// # Safety
///
/// This function requires that no other operation is concurrently modifying
/// or reading the `this_ptr` leaf node, the `next_sibling_ptr` leaf
/// node, and the sibling leaf node of `next_sibling_ptr`.
pub unsafe fn insert_before(
this_ptr: NodePtr<PREFIX_LEN, Self>,
next_sibling_ptr: NodePtr<PREFIX_LEN, Self>,
) {
// SAFETY: Covered by safety doc of this function
let (this, next_sibling) = unsafe { (this_ptr.as_mut(), next_sibling_ptr.as_mut()) };
if cfg!(debug_assertions) {
debug_assert!(
this.previous.is_none(),
"previous ptr should be None on insert into linked list"
);
debug_assert!(
this.next.is_none(),
"next ptr should be None on insert into linked list"
);
debug_assert!(
this.key.as_bytes() < next_sibling.key.as_bytes(),
"this leaf must be ordered before sibling in the trie"
);
}
this.previous = next_sibling.previous;
this.next = Some(next_sibling_ptr);
if let Some(previous_sibling_ptr) = next_sibling.previous {
// SAFETY: Covered by safety doc of this function
let previous_sibling = unsafe { previous_sibling_ptr.as_mut() };
previous_sibling.next = Some(this_ptr);
}
next_sibling.previous = Some(this_ptr);
}
/// Insert the leaf node pointed to by `this_ptr` into the linked list
/// position that `old_leaf` currently occupies, and then remove `old_leaf`
/// from the linked list.
///
/// # Safety
///
/// This function requires that no other operation is concurrently modifying
/// or reading the `this_ptr` leaf node and the sibling leaf nodes of the
/// `old_leaf`.
pub unsafe fn replace(this_ptr: NodePtr<PREFIX_LEN, Self>, old_leaf: &mut Self, force: bool) {
// SAFETY: Covered by safety doc of this function
let this = unsafe { this_ptr.as_mut() };
if cfg!(debug_assertions) {
debug_assert!(
this.previous.is_none(),
"previous ptr should be None on insert into linked list"
);
debug_assert!(
this.next.is_none(),
"next ptr should be None on insert into linked list"
);
if !force {
debug_assert_eq!(
this.key.as_bytes(),
old_leaf.key.as_bytes(),
"To replace a node, the key must be exactly the same"
);
}
}
this.next = old_leaf.next;
this.previous = old_leaf.previous;
if let Some(prev_leaf_ptr) = this.previous {
// SAFETY: Covered by safety doc of this function
let prev_leaf = unsafe { prev_leaf_ptr.as_mut() };
prev_leaf.next = Some(this_ptr);
}
if let Some(next_leaf_ptr) = this.next {
// SAFETY: Covered by safety doc of this function
let next_leaf = unsafe { next_leaf_ptr.as_mut() };
next_leaf.previous = Some(this_ptr);
}
old_leaf.next = None;
old_leaf.previous = None;
}
}
impl<const PREFIX_LEN: usize, K, V> LeafNode<K, V, PREFIX_LEN> {
/// Create a new leaf node with the given value and no siblings.
pub fn with_no_siblings(key: K, value: V) -> Self {
LeafNode {
value,
key,
previous: None,
next: None,
}
}
/// Returns a shared reference to the key contained by this leaf node
pub fn key_ref(&self) -> &K {
&self.key
}
/// Returns a shared reference to the value contained by this leaf node
pub fn value_ref(&self) -> &V {
&self.value
}
/// Returns a mutable reference to the value contained by this leaf node
pub fn value_mut(&mut self) -> &mut V {
&mut self.value
}
/// Return shared references to the key and value contained by this leaf
/// node
pub fn entry_ref(&self) -> (&K, &V) {
(&self.key, &self.value)
}
/// Return mutable references to the key and value contained by this leaf
/// node
pub fn entry_mut(&mut self) -> (&mut K, &mut V) {
(&mut self.key, &mut self.value)
}
/// Consume the leaf node and return a tuple of the key and value
pub fn into_entry(self) -> (K, V) {
(self.key, self.value)
}
/// Check that the provided full key is the same one as the stored key.
pub fn matches_full_key(&self, possible_key: &[u8]) -> bool
where
K: AsBytes,
{
self.key.as_bytes().eq(possible_key)
}
/// Check that the key starts with the given slice.
pub fn starts_with(&self, key: &[u8]) -> bool
where
K: AsBytes,
{
self.key.as_bytes().starts_with(key)
}
/// This function removes this leaf node from its linked list.
///
/// # Safety
///
/// This function requires that no other operation is concurrently modifying
/// or reading the `this_ptr` leaf node and its sibling leaf nodes.
pub unsafe fn remove_self(this_ptr: NodePtr<PREFIX_LEN, Self>) {
// SAFETY: Covered by safety doc of this function
let this = unsafe { this_ptr.as_mut() };
if let Some(sibling_ptr) = this.previous {
// SAFETY: Covered by safety doc of this function
let sibling = unsafe { sibling_ptr.as_mut() };
sibling.next = this.next;
}
if let Some(sibling_ptr) = this.next {
// SAFETY: Covered by safety doc of this function
let sibling = unsafe { sibling_ptr.as_mut() };
sibling.previous = this.previous;
}
// Normally this is where I would reset the `previous`/`next` pointers
// to `None`, but it is useful in the delete case to keep this
// information around.
}
/// Create a copy of this leaf node with the sibling references removed.
pub fn clone_without_siblings(&self) -> Self
where
K: Clone,
V: Clone,
{
Self {
value: self.value.clone(),
key: self.key.clone(),
// We override the default clone behavior to wipe these values out, since its unlikely
// that the cloned leaf should point to the old linked list of leaves
previous: None,
next: None,
}
}
}
impl<const PREFIX_LEN: usize, K, V> Node<PREFIX_LEN> for LeafNode<K, V, PREFIX_LEN> {
type Key = K;
type Value = V;
const TYPE: NodeType = NodeType::Leaf;
}
#[cfg(test)]
mod tests {
use std::boxed::Box;
use super::*;
use crate::{raw::representation::OpaqueValue, tagged_pointer::TaggedPointer};
// This test is important because it verifies that we can transform a tagged
// pointer to a type with large and small alignment and back without issues.
#[test]
fn leaf_node_alignment() {
let mut p0 = TaggedPointer::<OpaqueValue, 3>::new(
Box::into_raw(Box::<LeafNode<[u8; 0], _, 16>>::new(
LeafNode::with_no_siblings([], 3u8),
))
.cast::<OpaqueValue>(),
)
.unwrap();
p0.set_data(0b001);
#[repr(align(64))]
struct LargeAlignment;
let mut p1 = TaggedPointer::<OpaqueValue, 3>::new(
Box::into_raw(Box::<LeafNode<LargeAlignment, _, 16>>::new(
LeafNode::with_no_siblings(LargeAlignment, 2u16),
))
.cast::<OpaqueValue>(),
)
.unwrap();
p1.set_data(0b011);
let mut p2 = TaggedPointer::<OpaqueValue, 3>::new(
Box::into_raw(Box::<LeafNode<_, LargeAlignment, 16>>::new(
LeafNode::with_no_siblings(1u64, LargeAlignment),
))
.cast::<OpaqueValue>(),
)
.unwrap();
p2.set_data(0b111);
unsafe {
// These tests apparently leak memory in Miri's POV unless we explicitly cast
// them back to the original type when we deallocate. The `.cast` calls
// are required, even though the tests pass under normal execution otherwise (I
// guess normal test execution doesn't care about leaked memory?)
drop(Box::from_raw(
p0.to_ptr().cast::<LeafNode<[u8; 0], u8, 16>>().as_ptr(),
));
drop(Box::from_raw(
p1.to_ptr()
.cast::<LeafNode<LargeAlignment, u16, 16>>()
.as_ptr(),
));
drop(Box::from_raw(
p2.to_ptr()
.cast::<LeafNode<u64, LargeAlignment, 16>>()
.as_ptr(),
));
}
}
}