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
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
use std::{cmp::Ordering, hash::Hasher, ops::DerefMut};

use siphasher::sip128::{Hasher128, SipHasher24};

use crate::{
    digest::{Digest, PageDigest, ValueDigest},
    node::Node,
    visitor::Visitor,
};

#[derive(Debug)]
pub(crate) enum UpsertResult<K> {
    /// The key & value hash were successfully upserted.
    Complete,

    /// An intermediate page must be inserted between the caller and the callee.
    InsertIntermediate(K),
}

/// A group of [`Node`] instances at the same location within the tree.
///
/// A page within an MST is a probabilistically sized structure, with varying
/// numbers of [`Node`] within. A page has a min/max key range defined by the
/// nodes within it, and the page hash acts as a content hash, describing the
/// state of the page and the nodes within it.
#[derive(Debug, PartialEq, Eq, Clone)]
pub struct Page<const N: usize, K> {
    level: u8,

    /// The cached hash in this page; the cumulation of the hashes of the
    /// sub-tree rooted at this page.
    tree_hash: Option<PageDigest>,

    /// A vector of nodes in this page, ordered min to max by key.
    nodes: Vec<Node<N, K>>,

    /// The page for keys greater-than all keys in nodes.
    high_page: Option<Box<Page<N, K>>>,
}

impl<const N: usize, K> Page<N, K> {
    pub(super) const fn new(level: u8, nodes: Vec<Node<N, K>>) -> Self {
        Self {
            level,
            tree_hash: None,
            nodes,
            high_page: None,
        }
    }

    /// Returns the the [`Node`] in this page.
    pub fn nodes(&self) -> &[Node<N, K>] {
        &self.nodes
    }

    /// Returns the tree level / deterministic / logical hight of this page in
    /// the tree.
    pub const fn level(&self) -> u8 {
        self.level
    }

    /// Return the cached hash of this page if any, covering the nodes and the
    /// sub-tree rooted at `self`.
    pub fn hash(&self) -> Option<&PageDigest> {
        self.tree_hash.as_ref()
    }

    /// Set the high page pointer for this page.
    ///
    /// # Panics
    ///
    /// Panics if this page already has a high page linked, or `p` contains no
    /// nodes.
    pub(crate) fn insert_high_page(&mut self, p: Box<Self>) {
        debug_assert!(self.high_page.is_none());
        debug_assert!(!p.nodes().is_empty());

        // Invalidate the hash of this page.
        self.tree_hash = None;
        self.high_page = Some(p)
    }

    /// Return a pointer to the linked high page, if any.
    pub(crate) fn high_page(&self) -> Option<&Self> {
        self.high_page.as_deref()
    }

    /// Perform a depth-first, in-order traversal, yielding each [`Page`] and
    /// [`Node`] to `visitor`.
    ///
    /// If `high_page` is true, this page was linked to from the parent via a
    /// high page pointer.
    pub(crate) fn in_order_traversal<'a, T>(&'a self, visitor: &mut T, high_page: bool) -> bool
    where
        T: Visitor<'a, N, K>,
    {
        if !visitor.visit_page(self, high_page) {
            return false;
        }

        for node in &self.nodes {
            if !node.depth_first(visitor) {
                return false;
            }
        }

        if !visitor.post_visit_page(self) {
            return false;
        }

        if let Some(h) = &self.high_page {
            if !h.in_order_traversal(visitor, true) {
                return false;
            }
        }

        true
    }

    /// Return the minimum key stored in this page.
    ///
    /// This is an `O(1)` operation.
    ///
    /// # Panics
    ///
    /// Panics if there are no nodes in this page.
    #[inline]
    pub(crate) fn min_key(&self) -> &K {
        self.nodes.first().unwrap().key()
    }

    /// Return the maximum key stored in this page.
    ///
    /// This is an `O(1)` operation.
    ///
    /// # Panics
    ///
    /// Panics if there are no nodes in this page.
    #[inline]
    pub(crate) fn max_key(&self) -> &K {
        self.nodes.last().unwrap().key()
    }

    /// Descend down the minimum (left most) path (if any) and return the
    /// minimum key in the subtree rooted at `p`.
    ///
    /// This is an `O(logN)` operation.
    #[inline]
    pub(crate) fn min_subtree_key(&self) -> &K {
        // This is mildly faster than the iterator chain approach.
        let v = self.nodes().get(0).and_then(|v| v.lt_pointer());
        if let Some(v) = v {
            return v.min_subtree_key();
        }

        self.min_key()
    }

    /// Chase the high page pointers to the maximum page value of the subtree
    /// rooted at `p`.
    ///
    /// This is an `O(logN)` operation.
    #[inline]
    pub(crate) fn max_subtree_key(&self) -> &K {
        self.high_page()
            .map(|v| v.max_subtree_key())
            .unwrap_or_else(|| self.max_key())
    }
}

impl<const N: usize, K> Page<N, K>
where
    K: AsRef<[u8]>,
{
    /// Generate the page hash and cache the value, covering the nodes and the
    /// sub-tree rooted at `self`.
    pub(crate) fn maybe_generate_hash(&mut self, hasher: &SipHasher24) {
        if self.tree_hash.is_some() {
            return;
        }

        let mut h = *hasher;

        // NOTE: changing the ordering of the hashed elements is a breaking
        // change.
        //
        // This order may be changed only if releasing a new major version, as
        // it invalidates existing hashes.

        // Hash all nodes & their child pages
        for n in &mut self.nodes {
            // Hash the lt child page of this node, if any
            if let Some(child_hash) = n.lt_pointer_mut().as_deref_mut().map(|v| {
                v.maybe_generate_hash(hasher);
                v.hash().unwrap()
            }) {
                h.write(child_hash.as_ref());
            }

            // Hash the node value itself
            h.write(n.key().as_ref());
            h.write(n.value_hash().as_ref());
        }

        // Hash the high page, if any
        if let Some(high_hash) = self.high_page.as_deref_mut().map(|v| {
            v.maybe_generate_hash(hasher);
            v.hash().unwrap()
        }) {
            h.write(high_hash.as_ref());
        }

        self.tree_hash = Some(PageDigest::from(Digest::new(h.finish128().as_bytes())));
    }
}

impl<const N: usize, K> Page<N, K>
where
    K: PartialOrd,
{
    /// Insert or update the value hash of `key`, setting it to `value`, found
    /// at tree `level`.
    ///
    /// Returns true if the key was found, or false otherwise.
    ///
    /// If the key is found/modified, the cached page hash is invalidated.
    pub(crate) fn upsert(&mut self, key: K, level: u8, value: ValueDigest<N>) -> UpsertResult<K> {
        match level.cmp(&self.level) {
            // Level is less than this page's level - descend down the tree.
            Ordering::Less => {
                // A non-zero page can never be empty, and level is less than
                // this page, which means this page must be non-zero.
                debug_assert_ne!(!self.level, 0);
                debug_assert!(!self.nodes.is_empty());

                // Find the node that is greater-than-or-equal-to key to descend
                // into.
                //
                // Otherwise insert this node into the high page.
                let ptr = self.nodes.partition_point(|v| key > *v.key());

                let page = match self.nodes.get_mut(ptr) {
                    Some(v) => {
                        debug_assert!(*v.key() > key);
                        v.lt_pointer_mut()
                    }
                    None => &mut self.high_page,
                };

                let page = page.get_or_insert_with(|| Box::new(Self::new(level, vec![])));
                if let UpsertResult::InsertIntermediate(key) =
                    page.upsert(key, level, value.clone())
                {
                    insert_intermediate_page(page, key, level, value);
                }
            }
            Ordering::Equal => self.upsert_node(key, value),
            // Level is more than this page's level
            Ordering::Greater => {
                // This level is lower than the desired level, the parent is
                // higher than the desired level.
                //
                // Returning false will case the parent will insert a new page.
                return UpsertResult::InsertIntermediate(key); // No need to update the hash of this subtree
            }
        }

        // This page, or one below it was modified. Invalidate the pre-computed
        // page hash, if any.
        //
        // This marks the page as "dirty" causing the hash to be recomputed on
        // demand, coalescing multiple updates instead of hashing for each.
        self.tree_hash = None;

        UpsertResult::Complete
    }

    /// Insert a node into this page, splitting any child pages as necessary.
    pub(crate) fn upsert_node(&mut self, key: K, value: ValueDigest<N>) {
        // Find the appropriate child pointer to follow.
        let idx = self.nodes.partition_point(|v| key > *v.key());

        // At this point the new key should be inserted has been identified -
        // node_idx points to the first node greater-than-or-equal to key.
        //
        // In this example, we're inserting the key "C":
        //
        //                                      node_idx
        //                                          ║
        //                                          ║
        //                                          ▼
        //                         ┌──────────┬──────────┐
        //                         │ LT Node  │ GTE Node │
        //                         │    A     │    E     │
        //                         └──────────┴──────────┘
        //                               │          │
        //                        ┌──────┘          │
        //                        ▼                 ▼
        //                  ┌─Page──────┐     ┌─Page──────┐
        //                  │           │     │ ┌───┬───┐ │
        //                  │ Always LT │     │ │ B │ D │ │
        //                  │  new key  │     │ └───┴───┘ │
        //                  └───────────┘     └───────────┘
        //
        // The less-than node never needs splitting, because all the keys within
        // it are strictly less than the insert key.
        //
        // The GTE child page does need splitting - all the keys less than "C"
        // need moving into the new node's less-than page.
        //
        // If the new "C" node will be inserted at the end of the node array,
        // there's no GTE node to check - instead the high page may contain
        // relevant nodes that must be split.

        let page_to_split = match self.nodes.get_mut(idx) {
            Some(n) if *n.key() == key => {
                n.update_value_hash(value);
                return;
            }
            Some(n) => n.lt_pointer_mut(),
            None => &mut self.high_page,
        };

        // Split the higher-page, either within a GTE node or the high page.
        let mut new_lt_page = split_off_lt(page_to_split, &key).map(Box::new);

        if let Some(lt_page) = &mut new_lt_page {
            debug_assert!(self.level > lt_page.level);
            debug_assert!(!lt_page.nodes.is_empty());
            debug_assert!(*lt_page.max_key() < key);

            let high_page_lt = split_off_lt(&mut lt_page.high_page, &key);
            let gte_page = std::mem::replace(&mut lt_page.high_page, high_page_lt.map(Box::new));
            if let Some(gte_page) = gte_page {
                debug_assert!(self.level > gte_page.level);
                debug_assert!(!gte_page.nodes.is_empty());
                debug_assert!(*gte_page.max_key() > key);

                self.insert_high_page(gte_page);
            }
        }

        self.nodes.insert(idx, Node::new(key, value, new_lt_page));
    }
}

/// Split `page`, mutating it such that it contains only nodes with keys ordered
/// strictly-less than `key`, returning a new [`Page`] containing the
/// greater-than-or-equal-to nodes.
///
/// If splitting `page` would leave it with no nodes, it is set to [`None`].
///
/// NOTE: this only splits the page provided - it is up to the caller to split
/// any high pages as necessary.
///
/// # Panics
///
/// This method panics if attempting to split a non-empty page (root pages are
/// never split).
fn split_off_lt<const N: usize, T, K>(page: &mut Option<T>, key: &K) -> Option<Page<N, K>>
where
    K: PartialOrd,
    T: DerefMut<Target = Page<N, K>>,
{
    let page_ref = page.as_deref_mut()?;
    debug_assert!(!page_ref.nodes.is_empty());

    // A page should be split into two parts - one page containing the elements
    // less-than "key", and one containing parts greater-or-equal to "key".
    let partition_idx = page_ref.nodes.partition_point(|v| key > v.key());

    // All the nodes are greater-than-or-equal-to "key" - there's no less-than
    // nodes to return.
    if partition_idx == 0 {
        debug_assert!(page_ref.min_key() > key);

        // The first gte node may have a lt_pointer with nodes that are lt key.
        return match split_off_lt(page_ref.nodes[0].lt_pointer_mut(), key) {
            Some(v) => {
                // Invalidate the page hash as the lt_page was split or the keys
                // moved, changing the content the hash covers.
                page_ref.tree_hash = None;
                Some(v)
            }
            None => None,
        };
    }

    // All the nodes are less than key.
    //
    // As an optimisation, simply return the existing page as the new page
    // (retaining the pre-computed hash if possible) and invalidate the old
    // page.
    if partition_idx == page_ref.nodes.len() {
        debug_assert!(page_ref.max_key() < key);

        // The page may have a high page, which may have nodes within the
        // (max(nodes.key), key) range
        let lt_high_nodes = split_off_lt(&mut page_ref.high_page, key);

        // If existing the high page was split (both sides are non-empty) then
        // invalidate the page hash.
        //
        // This effectively invalidates the page range of the returned lt_page
        // as the cached hash covers the high page (which has now been split,
        // changing the content).
        if lt_high_nodes.is_some() && page_ref.high_page.is_some() {
            page_ref.tree_hash = None;
        }

        // Put the lt nodes back into the high page, taking the gte nodes from
        // the high page.
        //
        // This leaves the lt_high_nodes in the high page link of page_ref.
        let gte_high_page = std::mem::replace(&mut page_ref.high_page, lt_high_nodes.map(Box::new));

        // Initialise the page we're about to return.
        //
        // This puts an empty page into page_ref, taking the new lt nodes in
        // page (potentially with the high page linked to lt_high_nodes)
        let lt_page = Some(std::mem::replace(
            page_ref,
            Page::new(page_ref.level, vec![]),
        ));

        // Put the gte nodes into the input page, if any (page should contain
        // all gte nodes after this split).
        match gte_high_page {
            Some(p) => *page_ref = *p,
            None => *page = None,
        }

        return lt_page;
    }

    // Invalidate the page hash as at least one node will be removed.
    page_ref.tree_hash = None;

    // Obtain the set of nodes that are greater-than-or-equal-to "key".
    let gte_nodes: Vec<_> = page_ref.nodes.drain(partition_idx..).collect();
    debug_assert!(!gte_nodes.is_empty());

    // page_ref now contains the lt nodes, and a high page that may be non-empty
    // and gte than key.

    // Initialise a new page to hold the gte nodes.
    let mut gte_page = Page::new(page_ref.level, gte_nodes);
    debug_assert!(gte_page.max_key() > key);

    // Move the input high page onto the new gte page (which continues to be gte
    // than the nodes taken from the input page).
    if let Some(h) = page_ref.high_page.take() {
        debug_assert!(!h.nodes.is_empty());
        debug_assert!(h.level < page_ref.level);
        debug_assert!(h.min_key() > key);
        gte_page.insert_high_page(h);
    }

    // The first gte node may contain a lt_pointer with keys lt key, recurse
    // into it.
    let lt_key_high_nodes = split_off_lt(gte_page.nodes.first_mut().unwrap().lt_pointer_mut(), key);

    // In which case it is gte all node keys in the lt page (or it wouldn't have
    // been on the gte node).
    //
    // Add this to the new lt_page's high page next.

    // Replace the input page with the gte nodes, taking the page containing the
    // lt nodes and returning them to the caller.
    let mut lt_page = std::mem::replace(page_ref, gte_page);
    debug_assert!(!lt_page.nodes.is_empty());
    debug_assert!(lt_page.max_key() < key);

    // Insert the high page, if any.
    if let Some(h) = lt_key_high_nodes {
        debug_assert!(h.level < page_ref.level);
        debug_assert!(h.max_key() < key);
        debug_assert!(!h.nodes.is_empty());
        lt_page.insert_high_page(Box::new(h));
    }

    Some(lt_page)
}

pub(crate) fn insert_intermediate_page<const N: usize, T, K>(
    child_page: &mut T,
    key: K,
    level: u8,
    value: ValueDigest<N>,
) where
    K: PartialOrd,
    T: DerefMut<Target = Page<N, K>>,
{
    // Terminology:
    //
    //     * parent_page: top of the stack, parent of child_page
    //     * intermediate/new page: intermediate page with level between parent_page
    //       and child_page to be inserted between them.
    //     * child_page: the lower page, child of parent_page
    //

    // The child page asked this page to insert a new intermediate page at this
    // location.
    //
    //                        ┌──────────┐
    //                        │ New Root │
    //                   ┌────│    B     │─────┐         Level N
    //                   │    └──────────┘     │
    //              lt_pointer            high_page
    //                   │                     │
    //                   │                     │
    //          ┌ ─ ─ ─ ─│─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─│─ ─ ─ ─ ─ ─ ─ ─ ─ ┐
    //             ┌─────▼────┐          ┌─────▼────┐
    //          │  │ LT Node  │          │ GTE Node │  Child Page │
    //             │    A     │          │    C     │     Level 0
    //          │  └──────────┘          └──────────┘             │
    //           ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─
    //
    // The child page must be split into nodes less-than key, and those
    // greater-than-or-equal to key to preserve the ordering once this new page
    // containing key is inserted. Both halves must be linked into the new page.

    debug_assert!(child_page.level() < level);
    debug_assert!(!child_page.nodes.is_empty());

    // Split the child page into (less-than, greater-than) pages, split at the
    // point where key would reside.
    //
    // NOTE: this may leave "page" empty if all the nodes moved to the lt page.
    let mut lt_page = {
        let child_page2 = child_page.deref_mut();
        let mut child_page_ref = Some(&mut *child_page2);
        split_off_lt(&mut child_page_ref, &key)
    };

    // If all the nodes moved out of the child_page and into lt_page it
    // indicates that all nodes had keys less-than the new key, meaning there
    // may be nodes in the lt_page high page that need splitting, as it may
    // contain values between max(lt_page.nodes) and key.
    //
    // For example, when inserting 4:
    //
    //                              ┌ ─ ─ ─ ─ ─ ─ ─ ─ ─
    //                                ┌───┐ New Parent │
    //                           ┌──│ │ 4 │    Level 2
    //                           │    └───┘            │
    //                           │  └ ─ ─ ─ ─ ─ ─ ─ ─ ─
    //                           │
    //                ┌ ─ ─ ─ ─ ─│─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─
    //                   ┌───┬───▼───────┐  Child Page │
    //                │  │ 1 │ 2 │ high  │     Level 1
    //                   └───┴───┴───────┘             │
    //                └ ─ ─ ─ ─ ─ ─ ─│─ ─ ─ ─ ─ ─ ─ ─ ─
    //                               │
    //                           ┌ ─ ▼ ─ ─ ─ ─ ─ ─ ─ ─ ┐
    //                             ┌───┬───┐
    //                           │ │ 3 │ 5 │   Level 0 │
    //                             └───┴───┘
    //                           └ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ┘
    //
    // The existing entry of 5 must be moved, as it is greater than the new
    // parent:
    //
    //                              ┌ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─
    //                                            New Parent │
    //                              │ ┌───┬───────┐  Level 2
    //                            ┌───│ 4 │ high  │───┐      │
    //                            │ │ └───┴───────┘   │
    //                            │  ─ ─ ─ ─ ─ ─ ─ ─ ─│─ ─ ─ ┘
    //                            ▼                   │
    //           ┌ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─    │
    //              ┌───┬───┬───────┐  Child Page │   │
    //           │  │ 1 │ 2 │ high  │     Level 1     │
    //              └───┴───┴───────┘             │   │
    //           └ ─ ─ ─ ─ ─ ─ ─│─ ─ ─ ─ ─ ─ ─ ─ ─    │
    //                          ▼                     ▼
    //                  ┌ ─ ─ ─ ─ ─ ─ ─       ┌ ─ ─ ─ ─ ─ ─ ─
    //                   ┌───┐         │       ┌───┐         │
    //                  ││ 3 │ Level 0        ││ 5 │ Level 0
    //                   └───┘         │       └───┘         │
    //                  └ ─ ─ ─ ─ ─ ─ ─       └ ─ ─ ─ ─ ─ ─ ─
    //
    // To do this, we split the high page, attaching the lt_nodes to the lt_page
    // created above, and attach the remaining gte_nodes to the high_page of the
    // intermediate_page.
    let mut gte_page = None;
    if let Some(lt_page) = &mut lt_page {
        debug_assert!(level > lt_page.level);
        debug_assert!(!lt_page.nodes.is_empty());
        debug_assert!(*lt_page.max_key() < key);

        let high_page_lt = split_off_lt(&mut lt_page.high_page, &key);
        gte_page = std::mem::replace(&mut lt_page.high_page, high_page_lt.map(Box::new));
        if let Some(gte_page) = &gte_page {
            debug_assert!(level > gte_page.level);
            debug_assert!(!gte_page.nodes.is_empty());
            debug_assert!(*gte_page.max_key() > key);
        }
    }

    // Create the new node.
    let node = Node::new(key, value, None);

    // Create the new intermediate page, between the parent page and the child
    // page.
    let mut intermediate_page = Page::new(level, vec![node]);
    if let Some(gte_page) = gte_page {
        intermediate_page.insert_high_page(gte_page);
    }

    // Replace the page pointer at this level to point to the new page, taking
    // the page that now contains the lt nodes after the split.
    let gte_page = std::mem::replace(child_page.deref_mut(), intermediate_page);

    // At this point, we have this structure:
    //
    //                         ┌─────────────┐
    //                         │  This Page  │
    //                         └─────────────┘
    //                                │
    //                                ▼
    //                      ┌───────────────────┐
    //                      │ Intermediate Page │
    //                      └───────────────────┘
    //
    // The lt_page and gtw_pages need linking into the new node within the new
    // intermediate page.

    *child_page.nodes[0].lt_pointer_mut() = lt_page.map(Box::new);
    if !gte_page.nodes.is_empty() {
        debug_assert!(gte_page.max_key() > child_page.nodes[0].key()); // "key"
        debug_assert!(level > gte_page.level);
        child_page.high_page = Some(Box::new(gte_page));
    }
}

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

    use super::*;
    use crate::{assert_tree, digest::Digest};

    const MOCK_VALUE: ValueDigest<1> = ValueDigest::new(Digest::new([0; 1]));
    const MOCK_PAGE_HASH: PageDigest = PageDigest::new([0; 16]);

    #[test]
    #[should_panic(expected = "!page_ref.nodes.is_empty()")]
    fn test_split_page_empty() {
        let mut gte_page = Some(Box::new(Page::<1, _>::new(42, vec![])));
        let _lt_page = split_off_lt(&mut gte_page, &5);
    }

    #[test]
    fn test_split_page_single_node_lt() {
        let mut gte_page = Some(Box::new(Page::new(
            42,
            vec![Node::new(2, MOCK_VALUE, None)],
        )));

        gte_page.as_mut().unwrap().tree_hash = Some(MOCK_PAGE_HASH);

        let lt_page = split_off_lt(&mut gte_page, &5);
        assert_matches!(gte_page, None);

        assert_matches!(lt_page, Some(p) => {
            assert_eq!(p.level, 42);

            // The unmodified page has the hash retained.
            assert_eq!(p.tree_hash, Some(MOCK_PAGE_HASH));

            assert_eq!(p.nodes, [
                Node::new(2, MOCK_VALUE, None),
            ]);
        });
    }

    #[test]
    fn test_split_page_single_node_gt() {
        let mut gte_page = Some(Box::new(Page::new(
            42,
            vec![Node::new(2, MOCK_VALUE, None)],
        )));

        gte_page.as_mut().unwrap().tree_hash = Some(MOCK_PAGE_HASH);

        let lt_page = split_off_lt(&mut gte_page, &1);
        assert_matches!(gte_page, Some(p) => {
            assert_eq!(p.level, 42);

            // The unmodified page has the hash retained.
            assert_eq!(p.tree_hash, Some(MOCK_PAGE_HASH));

            assert_eq!(p.nodes, [
                Node::new(2, MOCK_VALUE, None),
            ]);
        });

        assert_matches!(lt_page, None);
    }

    /// Test that a page containing entirely gte nodes, but with a linked high
    /// page that requires splitting, has the page has invalidated.
    #[test]
    fn test_split_page_single_node_gt_with_high_page_split() {
        let mut high_page = Box::new(Page::new(
            40,
            vec![
                Node::new(10, MOCK_VALUE, None),
                Node::new(15, MOCK_VALUE, None),
            ],
        ));
        high_page.tree_hash = Some(MOCK_PAGE_HASH);

        let mut page = Box::new(Page::new(42, vec![Node::new(5, MOCK_VALUE, None)]));
        page.tree_hash = Some(MOCK_PAGE_HASH);
        page.insert_high_page(high_page);

        let mut page = Some(page);

        let lt_page = split_off_lt(&mut page, &12);
        assert_matches!(page, Some(p) => {
            assert_eq!(p.level, 40);

            // The modified page has the hash invalidated as the high page was
            // split.
            assert_eq!(p.tree_hash, None);

            assert_eq!(p.nodes, [
                Node::new(15, MOCK_VALUE, None),
            ]);
            assert_eq!(p.high_page, None);
        });

        assert_matches!(lt_page, Some(p) => {
            assert_eq!(p.level, 42);
            assert_eq!(p.tree_hash, None);

            assert_eq!(p.nodes, [
                Node::new(5, MOCK_VALUE, None),
            ]);

            assert_eq!(p.high_page.as_ref().unwrap().nodes, [
                Node::new(10, MOCK_VALUE, None),
            ]);
            assert_eq!(p.high_page.as_ref().unwrap().tree_hash, None);
        });
    }

    /// Test that a page containing entirely lt nodes, but with a recursively
    /// followed child page that requires splitting, has the page has
    /// invalidated.
    ///
    /// Toe ensure page hashes are recursively invalidated, the split child page
    /// is actually two steps away from the target page.
    #[test]
    fn test_split_page_single_node_gt_with_child_page_split() {
        // The bottom-most/deepest child page that requires splitting.
        let child_2 = Some(Box::new(Page::new(
            40,
            vec![
                Node::new(1, MOCK_VALUE, None),
                // Split at 2
                Node::new(3, MOCK_VALUE, None),
            ],
        )));
        // The parent of child_2
        let child_1 = Some(Box::new(Page::new(
            41,
            vec![Node::new(4, MOCK_VALUE, child_2)],
        )));

        // The parent of child_1
        let mut page = Some(Box::new(Page::new(
            42,
            vec![Node::new(5, MOCK_VALUE, child_1)],
        )));

        page.as_mut().unwrap().tree_hash = Some(MOCK_PAGE_HASH);

        let lt_page = split_off_lt(&mut page, &2);
        assert_matches!(page, Some(p) => {
            assert_eq!(p.level, 42);

            // The modified page has the hash invalidated as the child page was
            // split.
            assert_eq!(p.tree_hash, None);

            assert_eq!(p.nodes, [
                Node::new(5, MOCK_VALUE, Some(Box::new(Page::new(
                    41,
                    vec![Node::new(4, MOCK_VALUE, Some(Box::new(Page::new(
                        40,
                        // 1 split away
                        vec![Node::new(3, MOCK_VALUE, None)],
                    ))))],
                )))),
            ]);
        });

        assert_matches!(lt_page, Some(p) => {
            assert_eq!(p.level, 40);

            assert_eq!(p.nodes, [
                Node::new(1, MOCK_VALUE, None),
            ]);

            assert_eq!(p.tree_hash, None);
        });
    }

    #[test]
    fn test_split_page_eq() {
        let mut gte_page = Some(Box::new(Page::new(
            42,
            vec![
                Node::new(1, MOCK_VALUE, None),
                Node::new(2, MOCK_VALUE, None),
                Node::new(4, MOCK_VALUE, None),
            ],
        )));

        gte_page.as_mut().unwrap().tree_hash = Some(MOCK_PAGE_HASH);

        let lt_page = split_off_lt(&mut gte_page, &2);
        assert_matches!(gte_page, Some(p) => {
            assert_eq!(p.level, 42);
            assert_eq!(p.tree_hash, None);

            assert_eq!(p.nodes, [
                Node::new(2, MOCK_VALUE, None),
                Node::new(4, MOCK_VALUE, None)
            ]);
        });

        assert_matches!(lt_page, Some(p) => {
            assert_eq!(p.level, 42);

            // The modified page has the hash invalidated.
            assert_eq!(p.tree_hash, None);

            assert_eq!(p.nodes, [
                Node::new(1, MOCK_VALUE, None),
            ]);
        });
    }

    #[test]
    fn test_split_page_lt() {
        let mut gte_page = Some(Box::new(Page::new(
            42,
            vec![
                Node::new(1, MOCK_VALUE, None),
                Node::new(2, MOCK_VALUE, None),
                Node::new(4, MOCK_VALUE, None),
            ],
        )));

        gte_page.as_mut().unwrap().tree_hash = Some(MOCK_PAGE_HASH);

        let lt_page = split_off_lt(&mut gte_page, &3);
        assert_matches!(gte_page, Some(p) => {
            assert_eq!(p.level, 42);
            assert_eq!(p.tree_hash, None);

            assert_eq!(p.nodes, [
                Node::new(4, MOCK_VALUE, None)
            ]);
        });

        assert_matches!(lt_page, Some(p) => {
            assert_eq!(p.level, 42);

            // The modified page has the hash invalidated.
            assert_eq!(p.tree_hash, None);

            assert_eq!(p.nodes, [
                Node::new(1, MOCK_VALUE, None),
                Node::new(2, MOCK_VALUE, None),
            ]);
        });
    }

    #[test]
    fn test_split_page_all_gt() {
        let mut gte_page = Some(Box::new(Page::new(
            42,
            vec![
                Node::new(1, MOCK_VALUE, None),
                Node::new(2, MOCK_VALUE, None),
                Node::new(4, MOCK_VALUE, None),
            ],
        )));

        gte_page.as_mut().unwrap().tree_hash = Some(MOCK_PAGE_HASH);

        let lt_page = split_off_lt(&mut gte_page, &0);
        assert_matches!(gte_page, Some(p) => {
            assert_eq!(p.level, 42);

            // The new page containing all the nodes retains the pre-computed
            // hash.
            assert_eq!(p.tree_hash, Some(MOCK_PAGE_HASH));

            assert_eq!(p.nodes, [
                Node::new(1, MOCK_VALUE, None),
                Node::new(2, MOCK_VALUE, None),
                Node::new(4, MOCK_VALUE, None),
            ]);
        });

        assert_matches!(lt_page, None);
    }

    #[test]
    fn test_split_page_all_lt() {
        let mut gte_page = Some(Box::new(Page::new(
            42,
            vec![
                Node::new(1, MOCK_VALUE, None),
                Node::new(2, MOCK_VALUE, None),
                Node::new(4, MOCK_VALUE, None),
            ],
        )));

        gte_page.as_mut().unwrap().tree_hash = Some(MOCK_PAGE_HASH);

        let lt_page = split_off_lt(&mut gte_page, &10);
        assert_matches!(gte_page, None);

        assert_matches!(lt_page, Some(p) => {
            assert_eq!(p.level, 42);

            // The unmodified page retains the pre-computed hash.
            assert_eq!(p.tree_hash, Some(MOCK_PAGE_HASH));

            assert_eq!(p.nodes, [
                Node::new(1, MOCK_VALUE, None),
                Node::new(2, MOCK_VALUE, None),
                Node::new(4, MOCK_VALUE, None),
            ]);
        });
    }

    #[test]
    fn test_upsert_less_than_split_child() {
        let mut p = Page::new(1, vec![Node::new(4, MOCK_VALUE, None)]);

        p.upsert(3, 0, MOCK_VALUE);
        p.upsert(1, 0, MOCK_VALUE);
        p.upsert(2, 1, MOCK_VALUE);

        assert_tree!(page = p);
    }

    #[test]
    fn test_split_page_recursive_lt_pointer() {
        let mut lt_pointer_page = Page::new(52, vec![Node::new(86, MOCK_VALUE, None)]);
        lt_pointer_page.tree_hash = Some(MOCK_PAGE_HASH);

        let mut root = Box::new(Page::new(
            42,
            vec![Node::new(161, MOCK_VALUE, Some(Box::new(lt_pointer_page)))],
        ));
        root.tree_hash = Some(MOCK_PAGE_HASH);

        let key = 160;

        let mut root = Some(root);
        let lt_page = split_off_lt(&mut root, &key);

        assert_matches!(lt_page, Some(p) => {
            assert_eq!(p.level, 52);
            assert_matches!(p.nodes(), [n] if *n.key() == 86)
        });
    }

    #[test]
    fn test_split_page_recursive_high_page() {
        let mut high_page = Page::new(32, vec![Node::new(44, MOCK_VALUE, None)]);
        high_page.tree_hash = Some(MOCK_PAGE_HASH);

        let mut root = Box::new(Page::new(42, vec![Node::new(42, MOCK_VALUE, None)]));
        root.tree_hash = Some(MOCK_PAGE_HASH);
        root.insert_high_page(Box::new(high_page));

        let key = 43;

        let mut root = Some(root);
        let lt_page = split_off_lt(&mut root, &key);

        assert_matches!(lt_page, Some(p) => {
            assert_eq!(p.level, 42);
            assert_matches!(p.nodes(), [n] if *n.key() == 42)
        });
        assert_matches!(root, Some(p) => {
            assert_eq!(p.level, 32);
            assert_matches!(p.nodes(), [n] if *n.key() == 44)
        });
    }
}