fsqlite-btree 0.1.10

B-tree storage engine
Documentation
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
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
//! Bε-tree: Write-optimized tree with message buffers (§15.2).
//!
//! Interior nodes carry a *message buffer* that accumulates pending
//! insert / update / delete operations. When a buffer overflows
//! (exceeds `buffer_capacity`), its messages are flushed down to the
//! appropriate child. Cascading flushes may occur if children also
//! overflow.
//!
//! The epsilon parameter controls the read/write tradeoff:
//! - Higher epsilon → larger buffers → fewer flushes → better write throughput
//! - Lower epsilon  → smaller buffers → faster reads → more flushes
//!
//! # Design
//!
//! - `BeTree<K, V>` owns a root node and configuration.
//! - `BeNode<K, V>` is either an `Interior` (pivots + children + buffer)
//!   or a `Leaf` (sorted key-value pairs).
//! - `BeMessage<K, V>` represents a deferred operation: `Insert`, `Upsert`,
//!   or `Delete`.
//! - Point lookup drains applicable messages along the root-to-leaf path.
//! - Range scan collects and applies pending messages to produce a
//!   consistent snapshot.

use std::collections::BTreeMap;
use std::fmt;
use std::sync::atomic::{AtomicU64, Ordering};

// ── Metrics ──────────────────────────────────────────────────────────────

static BETREE_BUFFER_FLUSHES_TOTAL: AtomicU64 = AtomicU64::new(0);
static BETREE_MESSAGES_BUFFERED_TOTAL: AtomicU64 = AtomicU64::new(0);
static BETREE_CASCADE_DEPTH_TOTAL: AtomicU64 = AtomicU64::new(0);
static BETREE_INSERTS_TOTAL: AtomicU64 = AtomicU64::new(0);
static BETREE_DELETES_TOTAL: AtomicU64 = AtomicU64::new(0);

/// Snapshot of Bε-tree metrics.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub struct BeTreeMetricsSnapshot {
    /// Total buffer flush events.
    pub buffer_flushes_total: u64,
    /// Total messages buffered across all operations.
    pub messages_buffered_total: u64,
    /// Sum of cascade depths across all flush operations.
    pub cascade_depth_total: u64,
    /// Total inserts.
    pub inserts_total: u64,
    /// Total deletes.
    pub deletes_total: u64,
}

impl fmt::Display for BeTreeMetricsSnapshot {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(
            f,
            "betree_flushes={} betree_msgs_buffered={} betree_cascade_depth={} betree_inserts={} betree_deletes={}",
            self.buffer_flushes_total,
            self.messages_buffered_total,
            self.cascade_depth_total,
            self.inserts_total,
            self.deletes_total,
        )
    }
}

/// Return a snapshot of Bε-tree metrics.
#[must_use]
pub fn betree_metrics_snapshot() -> BeTreeMetricsSnapshot {
    BeTreeMetricsSnapshot {
        buffer_flushes_total: BETREE_BUFFER_FLUSHES_TOTAL.load(Ordering::Relaxed),
        messages_buffered_total: BETREE_MESSAGES_BUFFERED_TOTAL.load(Ordering::Relaxed),
        cascade_depth_total: BETREE_CASCADE_DEPTH_TOTAL.load(Ordering::Relaxed),
        inserts_total: BETREE_INSERTS_TOTAL.load(Ordering::Relaxed),
        deletes_total: BETREE_DELETES_TOTAL.load(Ordering::Relaxed),
    }
}

/// Reset Bε-tree metrics.
pub fn reset_betree_metrics() {
    BETREE_BUFFER_FLUSHES_TOTAL.store(0, Ordering::Relaxed);
    BETREE_MESSAGES_BUFFERED_TOTAL.store(0, Ordering::Relaxed);
    BETREE_CASCADE_DEPTH_TOTAL.store(0, Ordering::Relaxed);
    BETREE_INSERTS_TOTAL.store(0, Ordering::Relaxed);
    BETREE_DELETES_TOTAL.store(0, Ordering::Relaxed);
}

// ── Configuration ────────────────────────────────────────────────────────

/// Configuration for the Bε-tree.
#[derive(Debug, Clone, Copy)]
pub struct BeTreeConfig {
    /// Buffer capacity per interior node. When exceeded, messages are
    /// flushed to the appropriate child.
    pub buffer_capacity: usize,
    /// Maximum number of keys in a leaf node before it splits.
    pub leaf_capacity: usize,
    /// Maximum number of pivots in an interior node before it splits.
    /// (fanout = max_pivots + 1)
    pub max_pivots: usize,
}

impl Default for BeTreeConfig {
    fn default() -> Self {
        Self {
            buffer_capacity: 8,
            leaf_capacity: 16,
            max_pivots: 4,
        }
    }
}

// ── Message ──────────────────────────────────────────────────────────────

/// A buffered message representing a deferred operation.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum BeMessage<K: Ord + Clone, V: Clone> {
    /// Insert or overwrite a key-value pair.
    Insert { key: K, value: V },
    /// Delete a key.
    Delete { key: K },
}

impl<K: Ord + Clone, V: Clone> BeMessage<K, V> {
    /// Return the key this message targets.
    pub fn key(&self) -> &K {
        match self {
            Self::Insert { key, .. } | Self::Delete { key } => key,
        }
    }
}

// ── Node ─────────────────────────────────────────────────────────────────

/// A node in the Bε-tree.
#[derive(Debug, Clone)]
enum BeNode<K: Ord + Clone, V: Clone> {
    /// Leaf node storing sorted key-value pairs.
    Leaf { entries: Vec<(K, V)> },
    /// Interior node with pivots, children, and a message buffer.
    Interior {
        /// Pivot keys separating children.
        /// `children[i]` covers keys < `pivots[i]`.
        /// `children[pivots.len()]` covers keys >= last pivot.
        pivots: Vec<K>,
        /// Child nodes. len() == pivots.len() + 1.
        children: Vec<Self>,
        /// Pending messages waiting to be flushed down.
        buffer: Vec<BeMessage<K, V>>,
    },
}

impl<K: Ord + Clone, V: Clone> BeNode<K, V> {
    fn new_leaf() -> Self {
        Self::Leaf {
            entries: Vec::new(),
        }
    }
}

// ── BeTree ───────────────────────────────────────────────────────────────

/// A Bε-tree: write-optimized tree with message buffers at interior nodes.
///
/// Defers small writes into buffers, flushing them down in batches when
/// buffers overflow. This reduces write amplification compared to standard
/// B-trees for write-heavy workloads.
pub struct BeTree<K: Ord + Clone, V: Clone> {
    root: BeNode<K, V>,
    config: BeTreeConfig,
}

impl<K: Ord + Clone, V: Clone> BeTree<K, V> {
    /// Create a new empty Bε-tree with the given configuration.
    pub fn new(config: BeTreeConfig) -> Self {
        assert!(config.buffer_capacity >= 1, "buffer_capacity must be >= 1");
        assert!(config.leaf_capacity >= 2, "leaf_capacity must be >= 2");
        assert!(config.max_pivots >= 2, "max_pivots must be >= 2");
        Self {
            root: BeNode::new_leaf(),
            config,
        }
    }

    /// Return the number of live key-value pairs in the tree.
    #[must_use]
    pub fn len(&self) -> usize {
        let mut pending: BTreeMap<K, Option<V>> = BTreeMap::new();
        self.collect_all(&self.root, &mut pending);
        pending.into_values().flatten().count()
    }

    /// Return whether the tree is empty.
    #[must_use]
    pub fn is_empty(&self) -> bool {
        self.len() == 0
    }

    /// Return the configuration.
    #[must_use]
    pub fn config(&self) -> &BeTreeConfig {
        &self.config
    }

    /// Return the depth of the tree (leaf = 1).
    #[must_use]
    pub fn depth(&self) -> usize {
        fn depth_of<K: Ord + Clone, V: Clone>(node: &BeNode<K, V>) -> usize {
            match node {
                BeNode::Leaf { .. } => 1,
                BeNode::Interior { children, .. } => 1 + depth_of(&children[0]),
            }
        }
        depth_of(&self.root)
    }

    /// Insert a key-value pair. If the key already exists, it is overwritten.
    pub fn insert(&mut self, key: K, value: V) {
        BETREE_INSERTS_TOTAL.fetch_add(1, Ordering::Relaxed);

        let msg = BeMessage::Insert { key, value };
        self.apply_message(msg);
    }

    /// Delete a key. No-op if the key doesn't exist.
    pub fn delete(&mut self, key: K) {
        BETREE_DELETES_TOTAL.fetch_add(1, Ordering::Relaxed);

        let msg = BeMessage::Delete { key };
        self.apply_message(msg);
    }

    /// Look up a key, returning its value if present.
    pub fn get(&self, key: &K) -> Option<&V> {
        self.get_in_node(&self.root, key)
    }

    /// Collect all key-value pairs in [lo, hi] inclusive.
    pub fn range(&self, lo: &K, hi: &K) -> Vec<(K, V)> {
        let mut pending: BTreeMap<K, Option<V>> = BTreeMap::new();
        self.collect_range(&self.root, lo, hi, &mut pending);

        pending
            .into_iter()
            .filter_map(|(k, v_opt)| v_opt.map(|v| (k, v)))
            .collect()
    }

    /// Return all entries in sorted order (full scan).
    pub fn entries(&self) -> Vec<(K, V)> {
        let mut result = Vec::new();
        let mut pending: BTreeMap<K, Option<V>> = BTreeMap::new();
        self.collect_all(&self.root, &mut pending);

        for (k, v_opt) in pending {
            if let Some(v) = v_opt {
                result.push((k, v));
            }
        }
        result
    }

    /// Return the total number of pending messages in all interior buffers.
    #[must_use]
    pub fn total_buffered_messages(&self) -> usize {
        fn count_msgs<K: Ord + Clone, V: Clone>(node: &BeNode<K, V>) -> usize {
            match node {
                BeNode::Leaf { .. } => 0,
                BeNode::Interior {
                    buffer, children, ..
                } => {
                    let child_msgs: usize = children.iter().map(count_msgs).sum();
                    buffer.len() + child_msgs
                }
            }
        }
        count_msgs(&self.root)
    }

    // ── Internal ─────────────────────────────────────────────────────────

    /// Apply a message to the tree by buffering it at the root.
    fn apply_message(&mut self, msg: BeMessage<K, V>) {
        BETREE_MESSAGES_BUFFERED_TOTAL.fetch_add(1, Ordering::Relaxed);

        match &mut self.root {
            BeNode::Leaf { entries } => {
                // Apply directly to the leaf.
                apply_message_to_leaf(entries, msg);
            }
            BeNode::Interior { buffer, .. } => {
                buffer.push(msg);
            }
        }

        // Flush if root interior buffer overflows.
        self.flush_if_needed();

        // Handle root split.
        self.maybe_split_root();
    }

    /// If the root is an interior node with an overflowing buffer, flush it.
    fn flush_if_needed(&mut self) {
        if let BeNode::Interior { buffer, .. } = &self.root {
            if buffer.len() > self.config.buffer_capacity {
                let cap = self.config.buffer_capacity;
                let leaf_cap = self.config.leaf_capacity;
                let max_pivots = self.config.max_pivots;
                flush_node(&mut self.root, cap, leaf_cap, max_pivots, 0);
            }
        }
    }

    /// If the root leaf is over capacity, convert it to an interior node.
    fn maybe_split_root(&mut self) {
        match &self.root {
            BeNode::Leaf { entries } => {
                if entries.len() > self.config.leaf_capacity {
                    self.split_root_leaf();
                }
            }
            BeNode::Interior { pivots, .. } => {
                if pivots.len() > self.config.max_pivots {
                    self.split_root_interior();
                }
            }
        }
    }

    fn split_root_leaf(&mut self) {
        if let BeNode::Leaf { entries } = &mut self.root {
            let mid = entries.len() / 2;
            let right_entries = entries.split_off(mid);
            let pivot = right_entries[0].0.clone();
            let left = BeNode::Leaf {
                entries: std::mem::take(entries),
            };
            let right = BeNode::Leaf {
                entries: right_entries,
            };
            self.root = BeNode::Interior {
                pivots: vec![pivot],
                children: vec![left, right],
                buffer: Vec::new(),
            };
        }
    }

    fn split_root_interior(&mut self) {
        if let BeNode::Interior {
            pivots,
            children,
            buffer,
        } = &mut self.root
        {
            let mid = pivots.len() / 2;
            let promote_key = pivots[mid].clone();

            let right_pivots = pivots.split_off(mid + 1);
            pivots.pop(); // remove the promoted pivot

            let right_children = children.split_off(mid + 1);

            // Split buffer between left and right based on promoted key.
            let mut left_buf = Vec::new();
            let mut right_buf = Vec::new();
            for msg in buffer.drain(..) {
                if *msg.key() < promote_key {
                    left_buf.push(msg);
                } else {
                    right_buf.push(msg);
                }
            }

            let left = BeNode::Interior {
                pivots: std::mem::take(pivots),
                children: std::mem::take(children),
                buffer: left_buf,
            };
            let right = BeNode::Interior {
                pivots: right_pivots,
                children: right_children,
                buffer: right_buf,
            };
            self.root = BeNode::Interior {
                pivots: vec![promote_key],
                children: vec![left, right],
                buffer: Vec::new(),
            };
        }
    }

    /// Recursive lookup: check buffer messages, then descend.
    #[allow(clippy::self_only_used_in_recursion)]
    fn get_in_node<'a>(&'a self, node: &'a BeNode<K, V>, key: &K) -> Option<&'a V> {
        match node {
            BeNode::Leaf { entries } => entries
                .binary_search_by(|(k, _)| k.cmp(key))
                .ok()
                .map(|idx| &entries[idx].1),
            BeNode::Interior {
                pivots,
                children,
                buffer,
            } => {
                // Check buffer for the most recent message targeting this key
                // (last one wins).
                let mut latest_msg: Option<&BeMessage<K, V>> = None;
                for msg in buffer {
                    if msg.key() == key {
                        latest_msg = Some(msg);
                    }
                }
                if let Some(msg) = latest_msg {
                    return match msg {
                        BeMessage::Insert { value, .. } => Some(value),
                        BeMessage::Delete { .. } => None,
                    };
                }

                // Descend to the appropriate child.
                let child_idx = find_child_index(pivots, key);
                self.get_in_node(&children[child_idx], key)
            }
        }
    }

    /// Collect entries and pending messages in [lo, hi] for range queries.
    ///
    /// Messages are processed top-down: higher-level buffer messages take
    /// priority over lower-level buffer messages and leaf entries. Within
    /// the same buffer, later messages win (iterate in reverse + `or_insert`).
    #[allow(clippy::self_only_used_in_recursion)]
    fn collect_range(
        &self,
        node: &BeNode<K, V>,
        lo: &K,
        hi: &K,
        pending: &mut BTreeMap<K, Option<V>>,
    ) {
        match node {
            BeNode::Leaf { entries } => {
                for (k, v) in entries {
                    if k >= lo && k <= hi {
                        // Don't overwrite decisions from higher-level buffers.
                        pending.entry(k.clone()).or_insert(Some(v.clone()));
                    }
                }
            }
            BeNode::Interior {
                pivots,
                children,
                buffer,
            } => {
                // Apply buffer messages in range. Iterate in reverse so the
                // most recently buffered message for a key is encountered
                // first, then use or_insert to not overwrite higher-level
                // decisions from the caller.
                for msg in buffer.iter().rev() {
                    let mk = msg.key();
                    if mk >= lo && mk <= hi {
                        match msg {
                            BeMessage::Insert { key, value } => {
                                pending.entry(key.clone()).or_insert(Some(value.clone()));
                            }
                            BeMessage::Delete { key } => {
                                pending.entry(key.clone()).or_insert(None);
                            }
                        }
                    }
                }
                // Descend to children that overlap [lo, hi].
                for (i, child) in children.iter().enumerate() {
                    let child_lo_bound = if i > 0 { Some(&pivots[i - 1]) } else { None };
                    let child_hi_bound = if i < pivots.len() {
                        Some(&pivots[i])
                    } else {
                        None
                    };

                    // Skip children whose range doesn't overlap [lo, hi].
                    if let Some(child_lo) = child_lo_bound {
                        if child_lo > hi {
                            continue;
                        }
                    }
                    if let Some(child_hi) = child_hi_bound {
                        if child_hi <= lo {
                            continue;
                        }
                    }

                    self.collect_range(child, lo, hi, pending);
                }
            }
        }
    }

    /// Collect all entries and pending messages (for full scan / entries()).
    ///
    /// Same top-down priority as collect_range.
    #[allow(clippy::self_only_used_in_recursion)]
    fn collect_all(&self, node: &BeNode<K, V>, pending: &mut BTreeMap<K, Option<V>>) {
        match node {
            BeNode::Leaf { entries } => {
                for (k, v) in entries {
                    pending.entry(k.clone()).or_insert(Some(v.clone()));
                }
            }
            BeNode::Interior {
                children, buffer, ..
            } => {
                for msg in buffer.iter().rev() {
                    match msg {
                        BeMessage::Insert { key, value } => {
                            pending.entry(key.clone()).or_insert(Some(value.clone()));
                        }
                        BeMessage::Delete { key } => {
                            pending.entry(key.clone()).or_insert(None);
                        }
                    }
                }
                for child in children {
                    self.collect_all(child, pending);
                }
            }
        }
    }
}

#[allow(clippy::missing_fields_in_debug)]
impl<K: Ord + Clone + fmt::Debug, V: Clone + fmt::Debug> fmt::Debug for BeTree<K, V> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("BeTree")
            .field("len", &self.len())
            .field("depth", &self.depth())
            .field("config", &self.config)
            .finish()
    }
}

// ── Helpers ──────────────────────────────────────────────────────────────

/// Find which child index a key belongs to given the pivots.
/// Returns `i` such that `pivots[i-1] <= key < pivots[i]`
/// (with virtual -inf at left and +inf at right).
fn find_child_index<K: Ord>(pivots: &[K], key: &K) -> usize {
    pivots.partition_point(|p| p <= key)
}

/// Apply a message directly to a leaf's sorted entries.
fn apply_message_to_leaf<K: Ord + Clone, V: Clone>(
    entries: &mut Vec<(K, V)>,
    msg: BeMessage<K, V>,
) {
    match msg {
        BeMessage::Insert { key, value } => match entries.binary_search_by(|(k, _)| k.cmp(&key)) {
            Ok(idx) => entries[idx].1 = value,
            Err(idx) => entries.insert(idx, (key, value)),
        },
        BeMessage::Delete { key } => {
            if let Ok(idx) = entries.binary_search_by(|(k, _)| k.cmp(&key)) {
                entries.remove(idx);
            }
        }
    }
}

/// Flush an interior node's buffer down to children. Recurse if children
/// overflow.
fn flush_node<K: Ord + Clone, V: Clone>(
    node: &mut BeNode<K, V>,
    buffer_cap: usize,
    leaf_cap: usize,
    max_pivots: usize,
    depth: usize,
) {
    let BeNode::Interior {
        pivots,
        children,
        buffer,
    } = node
    else {
        return;
    };

    if buffer.len() <= buffer_cap {
        return;
    }

    BETREE_BUFFER_FLUSHES_TOTAL.fetch_add(1, Ordering::Relaxed);
    BETREE_CASCADE_DEPTH_TOTAL.fetch_add(depth as u64, Ordering::Relaxed);

    // Drain all buffered messages and route them to the appropriate child.
    let messages: Vec<BeMessage<K, V>> = std::mem::take(buffer);

    for msg in messages {
        let child_idx = find_child_index(pivots, msg.key());
        match &mut children[child_idx] {
            BeNode::Leaf { entries } => {
                apply_message_to_leaf(entries, msg);
            }
            BeNode::Interior { buffer: cbuf, .. } => {
                cbuf.push(msg);
            }
        }
    }

    // Recursively flush any children that now overflow.
    for child in children.iter_mut() {
        if let BeNode::Interior { buffer: cbuf, .. } = child {
            if cbuf.len() > buffer_cap {
                flush_node(child, buffer_cap, leaf_cap, max_pivots, depth + 1);
            }
        }
    }

    // Split any leaf children that are over capacity.
    split_oversized_leaves(pivots, children, leaf_cap);

    // Split any interior children that are over capacity.
    split_oversized_interiors(pivots, children, max_pivots);
}

/// Split leaf children that exceed `leaf_cap`.
fn split_oversized_leaves<K: Ord + Clone, V: Clone>(
    pivots: &mut Vec<K>,
    children: &mut Vec<BeNode<K, V>>,
    leaf_cap: usize,
) {
    let mut i = 0;
    while i < children.len() {
        let split_data = if let BeNode::Leaf { entries } = &mut children[i] {
            if entries.len() > leaf_cap {
                let mid = entries.len() / 2;
                let right_entries = entries.split_off(mid);
                let new_pivot = right_entries[0].0.clone();
                let right_leaf = BeNode::Leaf {
                    entries: right_entries,
                };
                Some((new_pivot, right_leaf))
            } else {
                None
            }
        } else {
            None
        };

        if let Some((new_pivot, right_leaf)) = split_data {
            pivots.insert(i, new_pivot);
            children.insert(i + 1, right_leaf);
            // Don't increment i — re-check the left half.
            continue;
        }
        i += 1;
    }
}

/// Split interior children that exceed `max_pivots`.
fn split_oversized_interiors<K: Ord + Clone, V: Clone>(
    pivots: &mut Vec<K>,
    children: &mut Vec<BeNode<K, V>>,
    max_pivots: usize,
) {
    let mut i = 0;
    while i < children.len() {
        let split_data = if let BeNode::Interior {
            pivots: cpivots,
            children: cchildren,
            buffer: cbuffer,
        } = &mut children[i]
        {
            if cpivots.len() > max_pivots {
                let mid = cpivots.len() / 2;
                let promote = cpivots[mid].clone();

                let right_pivots = cpivots.split_off(mid + 1);
                cpivots.pop(); // remove promoted key

                let right_children = cchildren.split_off(mid + 1);

                // Split buffer.
                let mut left_buf = Vec::new();
                let mut right_buf = Vec::new();
                for msg in cbuffer.drain(..) {
                    if *msg.key() < promote {
                        left_buf.push(msg);
                    } else {
                        right_buf.push(msg);
                    }
                }
                *cbuffer = left_buf;

                let right_node = BeNode::Interior {
                    pivots: right_pivots,
                    children: right_children,
                    buffer: right_buf,
                };
                Some((promote, right_node))
            } else {
                None
            }
        } else {
            None
        };

        if let Some((promote, right_node)) = split_data {
            pivots.insert(i, promote);
            children.insert(i + 1, right_node);
            continue;
        }
        i += 1;
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn basic_insert_get() {
        let mut tree = BeTree::new(BeTreeConfig::default());
        tree.insert(5, "five");
        tree.insert(3, "three");
        tree.insert(7, "seven");

        assert_eq!(tree.get(&5), Some(&"five"));
        assert_eq!(tree.get(&3), Some(&"three"));
        assert_eq!(tree.get(&7), Some(&"seven"));
        assert_eq!(tree.get(&1), None);
        assert_eq!(tree.len(), 3);
    }

    #[test]
    fn overwrite_existing_key() {
        let mut tree = BeTree::new(BeTreeConfig::default());
        tree.insert(1, "a");
        tree.insert(1, "b");
        assert_eq!(tree.get(&1), Some(&"b"));
        assert_eq!(tree.len(), 1);
    }

    #[test]
    fn delete_key() {
        let mut tree = BeTree::new(BeTreeConfig::default());
        tree.insert(1, "a");
        tree.insert(2, "b");
        tree.delete(1);
        assert_eq!(tree.get(&1), None);
        assert_eq!(tree.get(&2), Some(&"b"));
        assert_eq!(tree.len(), 1);
    }

    #[test]
    fn range_query() {
        let mut tree = BeTree::new(BeTreeConfig::default());
        for i in 0..20 {
            tree.insert(i, i * 10);
        }
        let result = tree.range(&5, &10);
        let keys: Vec<i32> = result.iter().map(|(k, _)| *k).collect();
        assert_eq!(keys, vec![5, 6, 7, 8, 9, 10]);
    }

    #[test]
    fn entries_sorted() {
        let mut tree = BeTree::new(BeTreeConfig::default());
        for i in (0..10).rev() {
            tree.insert(i, i);
        }
        let entries = tree.entries();
        let keys: Vec<i32> = entries.iter().map(|(k, _)| *k).collect();
        assert_eq!(keys, (0..10).collect::<Vec<_>>());
    }

    #[test]
    fn buffer_flush_on_overflow() {
        let snap_before = betree_metrics_snapshot();
        let config = BeTreeConfig {
            buffer_capacity: 2,
            leaf_capacity: 4,
            max_pivots: 2,
        };
        let mut tree = BeTree::new(config);
        // Insert enough to trigger flushes.
        for i in 0..20 {
            tree.insert(i, i);
        }
        let snap_after = betree_metrics_snapshot();
        let flushes = snap_after
            .buffer_flushes_total
            .saturating_sub(snap_before.buffer_flushes_total);
        assert!(flushes > 0, "expected flush events, got {flushes}");
        assert_eq!(tree.len(), 20);
        // Verify all values.
        for i in 0..20 {
            assert_eq!(tree.get(&i), Some(&i), "missing key {i}");
        }
    }

    #[test]
    fn depth_increases_with_data() {
        let config = BeTreeConfig {
            buffer_capacity: 2,
            leaf_capacity: 4,
            max_pivots: 2,
        };
        let mut tree = BeTree::new(config);
        assert_eq!(tree.depth(), 1);
        for i in 0..100 {
            tree.insert(i, i);
        }
        assert!(tree.depth() > 1, "expected tree to grow deeper");
    }
}