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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
//! Traits for abstracting over leaf node WIDTH variants.
//!
//! This module defines [`TreePermutation`] and [`TreeLeafNode`] traits that
//! enable generic tree operations.
use std::cmp::Ordering;
use std::fmt::Debug;
use crate::nodeversion::NodeVersion;
use crate::policy::{LeafPolicy, RetireHandle, SlotKind, SlotState};
use seize::LocalGuard;
// ============================================================================
// Split Types
// ============================================================================
/// Split point for leaf node splitting.
#[derive(Debug, Clone, Copy)]
pub struct SplitPoint {
/// Logical position where to split (in post-insert coordinates).
pub pos: usize,
/// The ikey that will be the first key of the new (right) leaf.
pub split_ikey: u64,
}
/// Which leaf to insert into after a split.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum InsertTarget {
/// Insert into the original (left) leaf.
Left,
/// Insert into the new (right) leaf.
Right,
}
// ============================================================================
// Split+Insert Types (Atomic Split+Insert Operation)
// ============================================================================
/// Data for inserting a key during a split operation.
#[derive(Debug)]
pub struct SplitInsertData<'a, P: LeafPolicy> {
/// The 8-byte key to insert.
pub ikey: u64,
/// The keylenx value (0-8 for inline, 64 for suffix, >=128 for layer).
pub keylenx: u8,
/// The suffix bytes, if any (present when `keylenx == KSUF_KEYLENX`).
pub suffix: Option<&'a [u8]>,
/// The typed value to insert.
pub value: P::Output,
}
/// Result of an atomic split+insert operation.
#[derive(Debug, Clone, Copy)]
pub struct SplitInsertResult {
/// The key that separates the left and right leaves.
pub split_ikey: u64,
/// Which leaf received the new key.
pub insert_target: InsertTarget,
}
// ============================================================================
// TreePermutation Trait
// ============================================================================
/// Trait for permutation types used in leaf nodes.
pub trait TreePermutation: Copy + Clone + Eq + Debug + Send + Sync + Sized + 'static {
/// Raw storage type for atomic operations.
type Raw: Copy + Clone + Eq + Debug + Send + Sync + 'static;
/// Number of slots this permutation supports.
const WIDTH: usize;
// ========================================================================
// Construction
// ========================================================================
/// Create an empty permutation with size = 0.
fn empty() -> Self;
/// Create a sorted permutation with `n` elements in slots `0..n`.
fn make_sorted(n: usize) -> Self;
/// Create a permutation from a raw storage value.
fn from_value(raw: Self::Raw) -> Self;
// ========================================================================
// Accessors
// ========================================================================
/// Get the raw storage value.
fn value(&self) -> Self::Raw;
/// Get the number of slots in use.
fn size(&self) -> usize;
/// Get the physical slot at logical position `i`.
fn get(&self, i: usize) -> usize;
/// Get the slot at the back (next free slot to allocate).
fn back(&self) -> usize;
/// Get the slot at `back()` with an offset into the free region.
fn back_at_offset(&self, offset: usize) -> usize;
// ========================================================================
// Mutation
// ========================================================================
/// Allocate a slot from back and insert at position `i`.
fn insert_from_back(&mut self, i: usize) -> usize;
/// Compute insert result without mutation (for CAS operations).
fn insert_from_back_immutable(&self, i: usize) -> (Self, usize);
/// Swap two slots in the free region (positions >= size).
fn swap_free_slots(&mut self, pos_i: usize, pos_j: usize);
/// Set the size without changing slot positions.
fn set_size(&mut self, n: usize);
/// Remove the slot at logical position `i`.
fn remove(&mut self, i: usize);
}
// ============================================================================
// TreeInternode Trait
// ============================================================================
/// Trait for internode types used in a `MassTree`.
pub trait TreeInternode: Sized + Send + Sync + 'static {
/// Node width (max number of children).
const WIDTH: usize;
// ========================================================================
// Version / Locking
// ========================================================================
/// Get reference to node version.
fn version(&self) -> &NodeVersion;
// ========================================================================
// Structure
// ========================================================================
/// Get the height of this internode.
fn height(&self) -> u32;
/// Check if children are leaves (height == 0).
fn children_are_leaves(&self) -> bool;
/// Get number of keys.
fn nkeys(&self) -> usize;
/// Get number of keys using Relaxed ordering.
fn nkeys_relaxed(&self) -> usize;
/// Set number of keys.
fn set_nkeys(&self, n: u8);
/// Increment nkeys by 1.
fn inc_nkeys(&self);
/// Check if this internode is full.
fn is_full(&self) -> bool;
// ========================================================================
// Keys
// ========================================================================
/// Get key at index (Acquire ordering).
fn ikey(&self, idx: usize) -> u64;
/// Get key at index using Relaxed ordering.
fn ikey_relaxed(&self, idx: usize) -> u64;
/// Get raw pointer to the ikey array for SIMD operations.
fn ikey_ptr(&self) -> *const u64;
/// Set key at index.
fn set_ikey(&self, idx: usize, key: u64);
/// Compare key at position with search key.
fn compare_key(&self, search_ikey: u64, p: usize) -> Ordering;
/// Find insert position for a key.
fn find_insert_position(&self, insert_ikey: u64) -> usize;
// ========================================================================
// Children
// ========================================================================
/// Get child pointer at index.
fn child(&self, idx: usize) -> *mut u8;
/// Set child pointer at index.
fn set_child(&self, idx: usize, child: *mut u8);
/// Assign key and right child at position.
fn assign(&self, p: usize, ikey: u64, right_child: *mut u8);
/// Insert key and child at position, shifting existing entries.
fn insert_key_and_child(&self, p: usize, new_ikey: u64, new_child: *mut u8);
// ========================================================================
// Navigation
// ========================================================================
/// Get parent pointer.
fn parent(&self) -> *mut u8;
/// Set parent pointer.
fn set_parent(&self, parent: *mut u8);
/// Check if this is a root node.
fn is_root(&self) -> bool;
// ========================================================================
// Split Support
// ========================================================================
/// Shift entries from another internode.
fn shift_from(&self, dst_pos: usize, src: &Self, src_pos: usize, count: usize);
/// Split this internode into a new sibling while inserting a key/child.
#[must_use = "popup_key must be inserted into parent node to complete the split"]
fn split_into(
&self,
new_right: &mut Self,
new_right_ptr: *mut Self,
insert_pos: usize,
insert_ikey: u64,
insert_child: *mut u8,
) -> (u64, bool);
}
// ============================================================================
// TreeLeafNode Trait
// ============================================================================
/// Trait for abstracting over leaf node WIDTH variants.
pub trait TreeLeafNode<P: LeafPolicy>: Sized + Send + Sync + 'static {
/// The permutation type for this leaf.
type Perm: TreePermutation;
/// The internode type for this tree variant.
type Internode: TreeInternode;
/// Node width (number of slots).
const WIDTH: usize;
/// Split threshold (trigger split when size >= this).
const SPLIT_THRESHOLD: usize;
/// Inline suffix storage capacity in bytes.
#[cfg(not(feature = "small-suffix-capacity"))]
const INLINE_KSUF_CAPACITY: usize = 512;
/// Inline suffix storage capacity (small variant, 256 bytes).
#[cfg(feature = "small-suffix-capacity")]
const INLINE_KSUF_CAPACITY: usize = 256;
// ========================================================================
// Construction
// ========================================================================
/// Create a new leaf node (boxed, non-root).
fn new_boxed() -> Box<Self>;
/// Create a new root leaf node (boxed).
fn new_root_boxed() -> Box<Self>;
/// Create a new layer root leaf node (boxed).
fn new_layer_root_boxed() -> Box<Self>;
// ========================================================================
// Version / Lock
// ========================================================================
/// Get reference to the node's version for OCC protocol.
fn version(&self) -> &NodeVersion;
// ========================================================================
// Permutation
// ========================================================================
/// Load permutation with Acquire ordering.
fn permutation(&self) -> Self::Perm;
/// Store permutation with Release ordering.
fn set_permutation(&self, perm: Self::Perm);
/// Store permutation with Relaxed ordering (for split setup).
fn set_permutation_relaxed(&self, perm: Self::Perm);
/// Get raw permutation value for atomic operations.
fn permutation_raw(&self) -> <Self::Perm as TreePermutation>::Raw;
// ========================================================================
// Key Operations
// ========================================================================
/// Get ikey at slot with Acquire ordering.
fn ikey(&self, slot: usize) -> u64;
/// Get ikey at slot with Relaxed ordering (after Acquire fence).
fn ikey_relaxed(&self, slot: usize) -> u64;
/// Set ikey at slot with Release ordering.
fn set_ikey(&self, slot: usize, ikey: u64);
/// Set ikey at slot with Relaxed ordering (for split setup).
fn set_ikey_relaxed(&self, slot: usize, ikey: u64);
/// Get the ikey bound (ikey at slot 0, used for B-link routing).
fn ikey_bound(&self) -> u64;
/// Find all slots matching target ikey, returning a bitmask.
fn find_ikey_matches(&self, target_ikey: u64) -> u32;
/// Get keylenx at slot.
fn keylenx(&self, slot: usize) -> u8;
/// Get keylenx with Relaxed ordering (for OCC search loops).
fn keylenx_relaxed(&self, slot: usize) -> u8;
/// Set keylenx at slot with Release ordering.
fn set_keylenx(&self, slot: usize, keylenx: u8);
/// Set keylenx at slot with Relaxed ordering (for split setup).
fn set_keylenx_relaxed(&self, slot: usize, keylenx: u8);
/// Check if slot contains a layer pointer.
fn is_layer(&self, slot: usize) -> bool;
/// Check if slot has a key suffix.
fn has_ksuf(&self, slot: usize) -> bool;
// ========================================================================
// Value Operations (typed, replacing pointer API)
// ========================================================================
/// Load the terminal value at a slot, returning `None` if empty.
fn load_value(&self, slot: usize) -> Option<P::Output>;
/// Store a terminal value at a slot with Release ordering.
fn store_value(&self, slot: usize, output: &P::Output);
/// Store a terminal value at a slot with Relaxed ordering (for split setup).
fn store_value_relaxed(&self, slot: usize, output: &P::Output);
/// Update value in place, returning a handle for retiring the old value.
fn update_value_in_place(&self, slot: usize, output: &P::Output) -> RetireHandle;
/// Update value in place with Relaxed store ordering (for use under lock).
fn update_value_in_place_relaxed(&self, slot: usize, output: &P::Output) -> RetireHandle;
/// Take the terminal value from a slot, leaving it empty.
fn take_value(&self, slot: usize) -> Option<P::Output>;
/// Load the layer pointer at a slot.
fn load_layer(&self, slot: usize) -> *mut u8;
/// Store a layer pointer at a slot.
fn store_layer(&self, slot: usize, ptr: *mut u8);
/// Check if a slot is empty (no value or layer pointer).
fn is_slot_empty(&self, slot: usize) -> bool;
/// Classify a slot as empty, value, or layer, returning the value if present.
fn classify_slot(&self, slot: usize) -> SlotKind<P::Output>;
/// Classify a slot without extracting the value (lightweight variant).
fn classify_slot_light(&self, slot: usize) -> SlotState;
/// Move a value from one leaf/slot to another leaf/slot.
fn move_value_to(&self, dst: &Self, src_slot: usize, dst_slot: usize);
/// Clear the value at a slot (set to empty).
fn clear_value(&self, slot: usize);
// ========================================================================
// Slot Clearing
// ========================================================================
/// Clear all data at a slot (value, keylenx, etc.).
fn clear_slot(&self, slot: usize);
/// Clear a slot and remove it from the permutation.
fn clear_slot_and_permutation(&self, slot: usize);
// ========================================================================
// Size Operations (default impls)
// ========================================================================
/// Get the number of keys in this leaf.
#[inline(always)]
fn size(&self) -> usize {
self.permutation().size()
}
/// Check if the leaf is empty.
#[inline(always)]
fn is_empty(&self) -> bool {
self.size() == 0
}
/// Check if the leaf is full.
#[inline(always)]
fn is_full(&self) -> bool {
self.size() >= Self::WIDTH
}
// ========================================================================
// Navigation
// ========================================================================
/// Get the next leaf pointer, masking the mark bit (unguarded).
fn safe_next(&self) -> *mut Self;
/// Check if the next pointer is marked (split in progress).
fn next_is_marked(&self) -> bool;
/// Set the next leaf pointer.
fn set_next(&self, next: *mut Self);
/// Mark the next pointer (set LSB for split coordination).
fn mark_next(&self);
/// Unmark the next pointer (clear LSB).
fn unmark_next(&self);
/// Get the previous leaf pointer (unguarded).
fn prev(&self) -> *mut Self;
/// Set the previous leaf pointer.
fn set_prev(&self, prev: *mut Self);
/// Get the parent internode pointer (unguarded).
fn parent(&self) -> *mut u8;
/// Set the parent internode pointer.
fn set_parent(&self, parent: *mut u8);
/// Unlink this leaf from the doubly-linked chain.
///
/// # Safety
///
/// Caller must hold the lock and ensure neighbors are valid.
unsafe fn unlink_from_chain(&self);
/// Get the raw next pointer including mark bit (unguarded).
fn next_raw(&self) -> *mut Self;
/// Spin-wait until split completes on this leaf.
fn wait_for_split(&self);
// ========================================================================
// Slot Assignment
// ========================================================================
/// Check if slot 0 can be reused for a new key.
fn can_reuse_slot0(&self, new_ikey: u64) -> bool;
// ========================================================================
// Split Operations
// ========================================================================
/// Calculate the optimal split point for this leaf.
fn calculate_split_point(&self, insert_pos: usize, insert_ikey: u64) -> Option<SplitPoint>;
/// Split this leaf into a preallocated sibling.
///
/// # Safety
/// Caller must hold the lock and `new_leaf_ptr` must be valid.
unsafe fn split_into_preallocated(
&self,
split_pos: usize,
new_leaf_ptr: *mut Self,
guard: &LocalGuard<'_>,
) -> (u64, InsertTarget);
/// Split all entries to the right sibling (sequential-append optimization).
///
/// # Safety
/// Caller must hold the lock and `new_leaf_ptr` must be valid.
unsafe fn split_all_to_right_preallocated(
&self,
new_leaf_ptr: *mut Self,
guard: &LocalGuard<'_>,
) -> (u64, InsertTarget);
/// Atomically split this leaf and insert a new key.
///
/// # Safety
/// Caller must hold the lock, `new_leaf_ptr` must be valid.
unsafe fn split_and_insert(
&self,
split_pos: usize,
new_leaf_ptr: *mut Self,
insert_pos: usize,
insert_data: &SplitInsertData<'_, P>,
guard: &LocalGuard<'_>,
) -> SplitInsertResult;
/// Link a new sibling into the B-link chain.
///
/// # Safety
/// Caller must hold the lock.
unsafe fn link_sibling(&self, new_sibling: *mut Self);
// ========================================================================
// Suffix Operations
// ========================================================================
/// Get the suffix at a slot, if present.
fn ksuf(&self, slot: usize) -> Option<&[u8]>;
/// Assign a suffix to a slot, returning the old suffix bag pointer.
///
/// # Safety
/// Caller must hold the lock.
unsafe fn assign_ksuf(&self, slot: usize, suffix: &[u8], guard: &LocalGuard<'_>) -> *mut u8;
/// Retire a suffix bag pointer for deferred reclamation.
///
/// # Safety
/// `ptr` must be a valid suffix bag pointer.
unsafe fn retire_suffix_bag_ptr(ptr: *mut u8, guard: &LocalGuard<'_>);
/// Assign a suffix during initial slot setup.
///
/// # Safety
/// Caller must hold the lock.
unsafe fn assign_ksuf_init(&self, slot: usize, suffix: &[u8], guard: &LocalGuard<'_>);
/// Clear the suffix at a slot.
///
/// # Safety
/// Caller must hold the lock.
unsafe fn clear_ksuf(&self, slot: usize, guard: &LocalGuard<'_>);
// ========================================================================
// Suffix Comparison
// ========================================================================
/// Check if the suffix at a slot equals the given suffix.
fn ksuf_equals(&self, slot: usize, suffix: &[u8]) -> bool;
/// Compare the suffix at a slot with the given suffix.
fn ksuf_compare(&self, slot: usize, suffix: &[u8]) -> Option<Ordering>;
/// Get the suffix at a slot, or empty slice if none.
fn ksuf_or_empty(&self, slot: usize) -> &[u8];
/// Check if the suffix at a slot matches the given ikey and suffix.
fn ksuf_matches(&self, slot: usize, ikey: u64, suffix: &[u8]) -> bool;
/// Compare suffix, returning match result code.
fn ksuf_match_result(&self, slot: usize, keylenx: u8, suffix: &[u8]) -> i32;
// ========================================================================
// Cache Optimization
// ========================================================================
/// Prefetch leaf data for range scans.
fn prefetch(&self);
/// Prefetch the ikey at a slot.
fn prefetch_ikey(&self, slot: usize);
/// Prefetch for point lookup (permutation + keys).
fn prefetch_for_search(&self);
/// Size-aware prefetch: only fetch cache lines that will be accessed.
fn prefetch_for_search_adaptive(&self, size: usize);
// ========================================================================
// Modification State
// ========================================================================
/// Get the modification state.
fn modstate(&self) -> u8;
/// Set the modification state.
fn set_modstate(&self, state: u8);
/// Check if the layer has been deleted.
fn deleted_layer(&self) -> bool;
/// Mark the layer as deleted.
fn mark_deleted_layer(&self);
/// Mark the node for removal.
fn mark_remove(&self);
/// Check if the node is being removed.
fn is_removing(&self) -> bool;
/// Check if the node is in empty state.
fn is_empty_state(&self) -> bool;
/// Mark the node as empty.
fn mark_empty(&self);
/// Clear the empty state flag.
fn clear_empty_state(&self);
}
// ============================================================================
// Tests
// ============================================================================
#[cfg(test)]
mod unit_tests;