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
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
// Copyright 2015 Pierre-Étienne Meunier and Florent Becker. See the
// COPYRIGHT file at the top-level directory of this distribution and
// at http://pijul.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

use {
    rc, Cursor, Db, Error, MutTxn, PageCursor, PageItem, PageT, Representable, Transaction,
    MAX_RECORD_SIZE, N_CURSORS, PAGE_SIZE_I16, PAGE_SIZE_U16,
};

use rand::Rng;
use skiplist;
use skiplist::{record_size, FIRST_BINDING_SIZE, NIL, SKIPLIST_ROOT};
use std;
use transaction::{Cow, MutPage};

#[derive(Debug)]
enum Deleted<K, V> {
    Merged {
        page: u64,
        left_free: u64,
        right_free: u64,
        middle_ptr: *const u8,
        former_binding_len: u16,
    },
    Rebalanced {
        key: K,
        value: V,
        left: u64,
        right: u64,
        middle_ptr: *const u8,
        former_binding_len: u16,
    },
    Split {
        key: K,
        value: V,
        left: u64,
        right: u64,
        old_page: u64,
    },
    Leaf {
        deleted_ptr: *const u8,
        former_binding_len: u16,
    },
    Updated {
        page: u64,
        replaced_by: u64,
    },
}

impl<K: Representable, V: Representable> Deleted<K, V> {
    fn size_difference(&self) -> i16 {
        match *self {
            Deleted::Merged {
                former_binding_len, ..
            }
            | Deleted::Leaf {
                former_binding_len, ..
            } => -(former_binding_len as i16),
            Deleted::Rebalanced {
                former_binding_len,
                key,
                value,
                ..
            } => record_size(key, value) as i16 - (former_binding_len as i16),
            Deleted::Split { key, value, .. } => record_size(key, value) as i16,
            Deleted::Updated { .. } => 0,
        }
    }
}

#[derive(Debug)]
struct Delete<K, V> {
    cursor: Cursor,

    /// The last merge or rotation performed on the page below, or
    /// `Deleted::None` if it was just a deletion or update.
    last_operation: Deleted<K, V>,

    free_stack: [[u64; 2]; N_CURSORS],

    /// First page on the stack for which the reference count is at least 2.
    rc_level: usize,
}

impl<K, V> std::ops::Index<usize> for Delete<K, V> {
    type Output = PageCursor;
    fn index(&self, i: usize) -> &PageCursor {
        self.cursor.stack.index(i)
    }
}

impl<K, V> std::ops::IndexMut<usize> for Delete<K, V> {
    fn index_mut(&mut self, i: usize) -> &mut PageCursor {
        self.cursor.stack.index_mut(i)
    }
}

impl<K, V> Delete<K, V> {
    fn new(cursors: Cursor) -> Self {
        Delete {
            cursor: cursors,
            last_operation: Deleted::Updated {
                page: 0,
                replaced_by: 0,
            },
            free_stack: [[0; 2]; N_CURSORS],
            rc_level: 200, // anything larger than 64 would do.
        }
    }
    fn current(&mut self) -> &mut PageCursor {
        &mut self.cursor.stack[self.cursor.pointer]
    }
}

#[derive(Copy, Clone, Debug)]
struct Replacement<K, V> {
    page: u64,
    k: K,
    v: V,
    deleted_ptr: *const u8,
    deleted_len: u16,
}

#[derive(Debug, Clone, Copy)]
enum ReplOn {
    None,
    Left,
    Right,
    Middle,
}

impl<'env, T> MutTxn<'env, T> {
    /// Find the smallest right descendant of the element at the given cursor.
    fn find_smallest_descendant<K, V>(&mut self, del: &mut Delete<K, V>) {
        // We need to look for the smallest element in the right
        // subtree (if there's a right subtree).
        let mut page = self.load_cow_page(del.current().page);

        // Position of the deleted element.
        let next = skiplist::next_at_level(&page, del.current()[0], 0);
        // Right child of the deleted element.
        let right_page = skiplist::right_child(&page, next);
        if right_page > 0 {
            // If there's a right child, take its smallest element.
            del.cursor.pointer += 1;
            del.current().page = right_page;
            del.current().cursor.reset();
            loop {
                page = self.load_cow_page(del.current().page);

                if del.cursor.first_rc_level >= N_CURSORS
                    && rc(self, del[del.cursor.pointer].page) >= 2
                {
                    debug!(
                        "first_rc_level: {:?} {:?}",
                        del.cursor.pointer, del.cursor.stack[del.cursor.pointer].page
                    );
                    del.cursor.first_rc_level = del.cursor.pointer
                }

                let left_page = skiplist::right_child(&page, SKIPLIST_ROOT);
                if left_page == 0 {
                    break;
                } else {
                    del.cursor.pointer += 1;
                    del.current().page = left_page;
                    del.current().cursor.reset()
                }
            }
        }
    }

    /// If `value` is `None`, delete one of the bindings associated to
    /// `key` from the database (without any specific
    /// preference). Else, delete the specified binding if present.
    pub fn del<R: Rng, K: Representable, V: Representable>(
        &mut self,
        rng: &mut R,
        db: &mut Db<K, V>,
        key: K,
        value: Option<V>,
    ) -> Result<bool, Error> {
        info!("del starting");
        let mut del = {
            let (stack, found) = self.set_cursors(db, Some((key, value)));
            if !found {
                // debug!("element not present: {:?}", key);
                return Ok(false);
            }
            Delete::new(stack)
        };

        // cursor_pointer0 is a pointer to the stack level where (key,
        // value) is.
        let cursor_pointer0 = del.cursor.pointer;

        // Set the stack to the smallest right descendant of (key,
        // value).
        self.find_smallest_descendant(&mut del);
        debug!("stack {:?}", &del.cursor.stack[..=del.cursor.pointer]);

        // Load the page of the smallest descendant of the deleted entry.
        let page = self.load_cow_page(del.current().page);

        debug!(
            "cursor_pointer: {:?} {:?}",
            del.cursor.pointer, cursor_pointer0
        );

        // Now, two cases:
        if del.cursor.pointer == cursor_pointer0 {
            // We are deleting at a leaf (which might also be the
            // root).
            let next = skiplist::next_at_level(&page, del.current()[0], 0);
            let (key, value) = unsafe { skiplist::read_key_value::<_, K, V>(&page, next) };
            info!(
                "deleting at leaf: {:?} {:?}",
                key.onpage_size(),
                value.onpage_size()
            );
            // debug!("leaf delete {:?}", value);
            del.last_operation = Deleted::Leaf {
                deleted_ptr: page.offset(next as isize),
                former_binding_len: record_size(key, value),
            };

            if cursor_pointer0 < del.cursor.first_rc_level {
                // This page will be copied, and hence will increase
                // the RC of the replacement value, if indirect.
                unsafe {
                    self.rc_decr_value(rng, key)?;
                    self.rc_decr_value(rng, value)?
                }
            }
            del.cursor.pointer -= 1;
            // Update (i.e. merge, rebalance or update) the pages
            // above (if any).
            while del.cursor.pointer >= 1 {
                self.merge_or_rebalance(rng, &mut del, None)?;
                del.cursor.pointer -= 1;
            }
            // The last one (the root) need to be just updated,
            // because there is no binding on the page above the root
            // to try and merge or rebalance.
            self.update_del(rng, &mut del, None)?;

            // The current page now is the one before the root. We
            // need to update the root with what happened:
            self.update_root(rng, db, &mut del)?;
        } else {
            // We're deleting at an internal node, at the position
            // pointed to by cursor_pointer0.  We want to replace the
            // deleted entry with the smallest right descendant of
            // that entry, i.e. the element pointed to by
            // del.cursor_pointer.

            // Load the replacement. Since the stack is already at the
            // position of the smallest descendant, `page` is actually
            // the leaf we want.
            let first = skiplist::next_at_level(&page, SKIPLIST_ROOT, 0);
            let (key, value) = unsafe { skiplist::read_key_value::<_, K, V>(&page, first) };
            info!(
                "deleting internal key, replacement size: {:?} {:?}",
                key.onpage_size(),
                value.onpage_size()
            );

            // Delete<K,V> the replacement on the leaf (actually, schedule
            // it for deletion from a page above).

            del.last_operation = Deleted::Leaf {
                deleted_ptr: page.offset(first as isize),
                former_binding_len: record_size(key, value),
            };

            if del.cursor.pointer >= del.cursor.first_rc_level {
                // This page is present in other trees. Therefore,
                // since `key` and `value` will be inserted in the
                // newly created tree, and also be referenced from
                // here, we need to increase their count at some
                // point, and we choose to do it now.
                for offset in key.page_offsets() {
                    self.incr_rc(rng, offset)?
                }
                for offset in value.page_offsets() {
                    self.incr_rc(rng, offset)?
                }
            }

            // Now, merge_or_rebalances everything from the leaf to the
            // root.
            let repl = {
                // k0, v0: deleted key/value.
                let page0 = self.load_cow_page(del[cursor_pointer0].page);
                debug!("reading at {:?}", del[cursor_pointer0][0]);
                let ptr0 = skiplist::next_at_level(&page0, del[cursor_pointer0][0], 0);
                let (k0, v0) = { unsafe { skiplist::read_key_value::<_, K, V>(&page0, ptr0) } };
                info!(
                    "deleting k0, v0: {:?} {:?}",
                    k0.onpage_size(),
                    v0.onpage_size()
                );
                if cursor_pointer0 < del.cursor.first_rc_level {
                    // This page will be copied, and hence will increase
                    // the RC of the replacement value, if indirect.
                    unsafe {
                        self.rc_decr_value(rng, k0)?;
                        self.rc_decr_value(rng, v0)?
                    }
                }
                // debug!("deleting {:?}", std::str::from_utf8(k0));
                Replacement {
                    page: del[cursor_pointer0].page,
                    k: key,
                    v: value,
                    deleted_ptr: page0.offset(ptr0 as isize),
                    deleted_len: record_size(k0, v0),
                }
            };
            debug!(
                "Internal, updating {:?}",
                &del.cursor.stack[..=del.cursor.pointer]
            );
            del.cursor.pointer -= 1;
            while del.cursor.pointer >= 1 {
                info!(
                    "merge_or_rebalance, del.cursor.pointer = {:?}",
                    del.cursor.pointer
                );
                self.merge_or_rebalance(rng, &mut del, Some(repl))?;
                del.cursor.pointer -= 1;
            }
            debug!("Internal, done updating");
            self.update_del(rng, &mut del, Some(repl))?;
            self.update_root(rng, db, &mut del)?;
        }
        // debug!("last {:?}", del.last_operation);
        debug!("stack {:?}", del.cursor.stack[1]);
        unsafe {
            let level = del.cursor.first_rc_level + 1;
            for freed in del.free_stack.iter().take(level) {
                if freed[0] > 0 {
                    self.rc_decr_del(rng, freed[0])?;
                    if freed[1] > 0 {
                        assert!(freed[0] != freed[1]);
                        self.rc_decr_del(rng, freed[1])?
                    }
                }
            }
        }
        Ok(true)
    }

    /// From a cursor, a (possibly null) pointer to a key which needs
    /// to be forgotten, a pointer to the last operation
    /// (merge/rebalance/nothing), decide what to do (merge, rebalance
    /// or nothing) at the point indicated by `cursor`, and prepare the
    /// children in consequence.
    ///
    /// This function does not update the page pointed to by the
    /// cursor, because these operations might cascade, and the update
    /// will be done by another call to this function.
    fn merge_or_rebalance<R: Rng, K: Representable, V: Representable>(
        &mut self,
        rng: &mut R,
        del: &mut Delete<K, V>,
        replacement: Option<Replacement<K, V>>,
    ) -> Result<(), Error> {
        // In order to update the page pointers appropriately, record
        // whether the cursor was moved backwards (for instance if
        // we're at the end of a page and need to merge).
        debug!("merge or rebalance");
        let mut cursor_was_moved_backwards = false;

        let page = self.load_cow_page(del.current().page);
        debug!("page = {:?}", page);
        let mut next = skiplist::next_at_level(&page, del.current()[0], 0);
        if next == NIL {
            // If the current element is the last element in the
            // skiplist, move one element backwards.
            cursor_was_moved_backwards = true;
            debug!("moving backwards in page {:?}", page);
            let backwards = skiplist::move_cursor_backward(&page, &mut del.current().cursor);
            assert!(backwards);
            next = skiplist::next_at_level(&page, del.current()[0], 0)
        }

        let page_a = skiplist::right_child(&page, del.current()[0]);
        let page_a = self.load_cow_page(page_a);
        let page_b = skiplist::right_child(&page, next);
        let page_b = self.load_cow_page(page_b);

        let next_ptr: *const u8 = page.offset(next as isize);
        let (middle_key, middle_value) = unsafe { skiplist::read_key_value(&page, next) };
        let middle_size = record_size(middle_key, middle_value);

        let (repl_pos, repl_diff) = match (&del.last_operation, replacement) {
            (_, Some(repl)) if repl.deleted_ptr == next_ptr => {
                let diff = record_size(repl.k, repl.v) as i16 - repl.deleted_len as i16;
                (ReplOn::Middle, diff)
            }
            (Deleted::Merged { .. }, _) | (Deleted::Rebalanced { .. }, _) => {
                // If the pages below have been merged/rebalanced,
                // then even if the replacement occured on the page
                // below, the replacement has already been accounted
                // for in the size changes.
                (ReplOn::None, 0)
            }
            (_, Some(repl)) => {
                // Else, if the replacement occurs on one of page_a
                // and page_b, or at the binding we're trying to
                // delete by merging two neighbouring pages, we need
                // to count the size difference introduced by the
                // replacement.
                let diff = record_size(repl.k, repl.v) as i16 - repl.deleted_len as i16;
                if repl.page == page_a.page_offset() {
                    (ReplOn::Left, diff)
                } else if repl.page == page_b.page_offset() {
                    (ReplOn::Right, diff)
                } else {
                    (ReplOn::None, 0)
                }
            }
            _ => {
                // If there was no replacement, no need to do anything.
                (ReplOn::None, 0)
            }
        };

        // Can we merge two pages below the current page?
        let new_total_len = {
            (skiplist::occupied(&page_a) + skiplist::occupied(&page_b) + middle_size) as i16
                + del.last_operation.size_difference()
                + repl_diff
                - FIRST_BINDING_SIZE as i16
        };
        debug!(
            "page_a = {:?}, page_b = {:?}, middle = {:?}, last.size_diff = {:?}, repl_diff = {:?}",
            skiplist::occupied(&page_a),
            skiplist::occupied(&page_b),
            middle_size,
            del.last_operation.size_difference(),
            repl_diff
        );
        debug!("repl = {:?} {:?}", repl_pos, repl_diff);
        debug!("last_operation = {:?}", del.last_operation);
        if new_total_len as u16 <= PAGE_SIZE_U16 {
            // We can merge!
            let new_page = self.merge(
                rng,
                page_a,
                page_b,
                next_ptr,
                middle_key,
                middle_value,
                del,
                replacement,
            )?;
            let new_page = self.load_cow_page(new_page);
            assert_eq!(skiplist::occupied(&new_page), new_total_len as u16);
            return Ok(());
        }

        let (new_a, new_b) = if cursor_was_moved_backwards {
            // In this case, the edited page is b.
            (
                skiplist::occupied(&page_a) as i16
                    + if let ReplOn::Left = repl_pos {
                        repl_diff
                    } else {
                        0
                    },
                skiplist::occupied(&page_b) as i16
                    + if let ReplOn::Right = repl_pos {
                        repl_diff
                    } else {
                        0
                    }
                    + del.last_operation.size_difference(),
            )
        } else {
            // Else, the edited page is a.
            (
                skiplist::occupied(&page_a) as i16
                    + if let ReplOn::Left = repl_pos {
                        repl_diff
                    } else {
                        0
                    }
                    + del.last_operation.size_difference(),
                skiplist::occupied(&page_b) as i16
                    + if let ReplOn::Right = repl_pos {
                        repl_diff
                    } else {
                        0
                    },
            )
        };

        // Should we rebalance?
        if new_a as u16 <= (MAX_RECORD_SIZE + FIRST_BINDING_SIZE)
            || new_b as u16 <= (MAX_RECORD_SIZE + FIRST_BINDING_SIZE)
        {
            self.rebalance(
                rng,
                page_a,
                page_b,
                next_ptr,
                middle_key,
                middle_value,
                new_total_len as u16 + FIRST_BINDING_SIZE,
                del,
                replacement,
            )?;

            return Ok(());
        }

        // Else, just update.
        if cursor_was_moved_backwards {
            let forward = skiplist::move_cursor_forward(&page, &mut del.current().cursor);
            assert!(forward)
        }
        self.update_del(rng, del, replacement)
    }

    /// Updates the root, taking care of merges in pages below.
    fn update_root<R: Rng, K: Representable, V: Representable>(
        &mut self,
        rng: &mut R,
        db: &mut Db<K, V>,
        del: &mut Delete<K, V>,
    ) -> Result<(), Error> {
        debug!("UPDATE ROOT: {:?}", del.last_operation);
        debug!("stack: {:?}", &del.cursor.stack[..(del.cursor.pointer + 1)]);
        match del.last_operation {
            Deleted::Split {
                key,
                value,
                left,
                right,
                ..
            } => {
                db.0 = self.split_root(rng, &mut del.cursor, left, right, key, value)?;
                debug!("Split {:?}", db.0);
            }
            Deleted::Updated { replaced_by, .. } => {
                db.0 = replaced_by;
                // There is a special case here, when the last binding
                // of the root was removed in a merge at the previous
                // step.
                let root = self.load_cow_page(db.0);
                // If there was only one binding here.
                let next = skiplist::next_at_level(&root, SKIPLIST_ROOT, 0);
                debug!(
                    "next = {:?} {:?}",
                    root,
                    skiplist::next_at_level(&root, next, 0)
                );
                if skiplist::next_at_level(&root, SKIPLIST_ROOT, 0) == NIL {
                    let right = skiplist::right_child(&root, SKIPLIST_ROOT);
                    debug!("freeing");
                    if right > 0 {
                        del.free_stack[1][0] = del[1].page;
                        db.0 = right
                    }
                }
            }
            _ => {
                // Since we just updated the root, with the current
                // page being the "nil" page, the last operation could
                // not possibly be a Rebalance.
            }
        }
        Ok(())
    }

    /// This function is called when the page below the current page
    /// has no candidate for merging or rebalancing. If this is the
    /// case, we just update it with the last operation. After this
    /// function, the last operation will be None.
    fn update_del<R: Rng, K: Representable, V: Representable>(
        &mut self,
        rng: &mut R,
        del: &mut Delete<K, V>,
        replacement: Option<Replacement<K, V>>,
    ) -> Result<(), Error> {
        let mut page = self.load_cow_page(del[del.cursor.pointer + 1].page);
        debug!(
            "pointers/rc: {:?} {:?}",
            del.cursor.pointer, del.cursor.first_rc_level
        );
        debug!("update_del {:?}", page);
        match page {
            Cow::MutPage(ref mut p) if del.cursor.pointer + 1 < del.cursor.first_rc_level => {
                debug!("mut page, no RC");
                self.update_mut(rng, del, replacement, p)?
            }
            Cow::MutPage(ref p) => {
                debug!("mut page, RC");
                self.update_const(rng, del, replacement, p)?
            }
            Cow::Page(ref p) => {
                debug!("const page");
                self.update_const(rng, del, replacement, p)?
            }
        }
        Ok(())
    }

    /// Update the page below the current page, if that page is
    /// mutable (i.e. allocated in this transaction and referenced
    /// once).
    fn update_mut<R: Rng, K: Representable, V: Representable>(
        &mut self,
        rng: &mut R,
        del: &mut Delete<K, V>,
        replacement: Option<Replacement<K, V>>,
        p: &mut MutPage,
    ) -> Result<(), Error> {
        match del.last_operation {
            Deleted::Leaf { .. } => {
                // If the page below the current page is a leaf, we
                // just delete the next binding there.
                debug_assert_eq!(p.offset, del[del.cursor.pointer + 1].page);
                p.skiplist_cursor_delete_next::<K, V>(&del[del.cursor.pointer + 1].cursor);
                // self.free_last_operation(rng, del)?;
                del.last_operation = Deleted::Updated {
                    page: 0,
                    replaced_by: p.page_offset(),
                }
            }
            Deleted::Merged { page, .. } => {
                // If we deleted at a leaf, there is no replacement
                // here. If two pages were merged, the replacement (if
                // any) is already included in the merged page.
                debug_assert_eq!(p.offset, del[del.cursor.pointer + 1].page);
                p.skiplist_cursor_delete_next::<K, V>(&del[del.cursor.pointer + 1].cursor);
                p.set_right_child(del[del.cursor.pointer + 1][0], page);
                // self.free_last_operation(rng, del)?;
                del.last_operation = Deleted::Updated {
                    page: 0,
                    replaced_by: p.page_offset(),
                }
            }
            Deleted::Updated { replaced_by, .. } => {
                // The last operation just updated a page, but if the
                // binding there was the one we're deleting, that
                // binding must be replaced, which is what the
                // following call does:
                self.update_mut_updated(rng, del, replacement, p, replaced_by)?
            }
            Deleted::Rebalanced {
                key,
                value,
                right,
                left,
                former_binding_len,
                ..
            } => {
                self.update_mut_rebalance(rng, del, key, value, right, left, former_binding_len, p)?
            }
            Deleted::Split {
                key,
                value,
                right,
                left,
                ..
            } => {
                // The current binding on the page below the current
                // page might very well be the one we want to delete.
                debug!("update_mut, split, {:?} {:?}", left, right);
                debug!("current = {:?}", del.cursor.pointer);

                let size_diff = record_size(key, value) as i16
                    + (if let Some(repl) = replacement {
                        if repl.page == p.offset {
                            record_size(repl.k, repl.v) as i16 - repl.deleted_len as i16
                        } else {
                            0
                        }
                    } else {
                        0
                    });
                if skiplist::occupied(p) as i16 + size_diff < PAGE_SIZE_I16 {
                    if skiplist::first_free(p) as i16 + size_diff < PAGE_SIZE_I16 {
                        debug_assert_eq!(p.offset, del[del.cursor.pointer + 1].page);

                        {
                            let ref mut skip_cursor =
                                del.cursor.stack[del.cursor.pointer + 1].cursor;

                            if let Some(repl) = replacement {
                                if repl.page == p.offset {
                                    // Deleting the old version.
                                    p.skiplist_cursor_delete_next::<K, V>(&skip_cursor);
                                    // Adding the replacement
                                    p.skiplist_insert_after_cursor(
                                        rng,
                                        skip_cursor,
                                        repl.k,
                                        repl.v,
                                        left,
                                    );
                                }
                            }
                        }
                        // Whether or not we replaced, we can still
                        // set the left child (it was already set if
                        // we replaced).
                        p.set_right_child(del[del.cursor.pointer + 1][0], left);
                        // Then insert the new binding resulting from
                        // the split.
                        {
                            let ref mut skip_cursor =
                                del.cursor.stack[del.cursor.pointer + 1].cursor;
                            p.skiplist_insert_after_cursor(rng, skip_cursor, key, value, right);
                        }
                        // Free the old (unsplit) page.
                        // self.free_last_operation(rng, del)?;
                        del.last_operation = Deleted::Updated {
                            page: 0,
                            replaced_by: p.page_offset(),
                        };
                    } else {
                        debug!("update_mut, needs compaction");
                        self.update_const(rng, del, replacement, p)?
                    }
                } else {
                    // Split
                    debug!("cascading split");
                    self.split(rng, p, del, replacement)?;
                }
            }
        }
        Ok(())
    }

    /// Update in the case of an immutable page (i.e. allocated in a
    /// previous transaction).
    fn update_const<R: Rng, P: PageT + Sized, K: Representable, V: Representable>(
        &mut self,
        rng: &mut R,
        del: &mut Delete<K, V>,
        replacement: Option<Replacement<K, V>>,
        p: &P,
    ) -> Result<(), Error> {
        // Build an iterator, and spread it on one or two freshly
        // allocated pages, depending on the total size of the
        // iterator.
        // Delete<K,V> del.forgotten_key, possibly reinserting a replacement

        // debug!("update_const, last_operation: {:?}", del.last_operation);

        // What's added (0 if things are only removed).
        let it_size = skiplist::occupied(p) as i16
            + del.last_operation.size_difference()
            + (if let Some(repl) = replacement {
                if repl.page == p.page_offset() {
                    record_size(repl.k, repl.v) as i16
                } else {
                    0
                }
            } else {
                0
            });
        if it_size <= PAGE_SIZE_I16 {
            let new_page = {
                debug!("it on page {:?}", p.page_offset());
                let incr_children_rc = del.cursor.pointer + 1 >= del.cursor.first_rc_level;
                debug!("{:?} {:?}", del.cursor.pointer, del.cursor.first_rc_level);
                let it = skiplist::iter_all(p).map(|(a, b, c)| PageItem {
                    ptr: a,
                    kv: b,
                    right: c,
                    incr_right_rc: incr_children_rc,
                    incr_kv_rc: incr_children_rc,
                });
                let it = WithChanges::new(del, it, replacement);
                self.spread_on_one_page(rng, it)?
            };
            debug!("new_page: {:?}", new_page);
            let curs = del.cursor.pointer;
            del[curs + 1].page = new_page;
            debug!("curs: {:?}", &del.cursor.stack[..10]);

            del.free_stack[del.cursor.pointer + 1][0] = p.page_offset();
            del.last_operation = Deleted::Updated {
                page: p.page_offset(),
                replaced_by: new_page,
            };
            Ok(())
        } else {
            debug!("update_const, splitting");
            self.split(rng, p, del, replacement)
        }
    }

    /// Update a mutable page when the page below the current page has
    /// no candidate for merging, splitting or rebalancing, and is not
    /// a leaf.
    fn update_mut_updated<R: Rng, K: Representable, V: Representable>(
        &mut self,
        rng: &mut R,
        del: &mut Delete<K, V>,
        replacement: Option<Replacement<K, V>>,
        p: &mut MutPage,
        replaced_by: u64,
    ) -> Result<(), Error> {
        debug!("update_mut_no_operation, page = {:?}", p.offset);
        debug!("replacement = {:?}", replacement);
        if let Some(repl) = replacement {
            if p.offset == repl.page {
                let size = record_size(repl.k, repl.v);
                if skiplist::occupied(p) - repl.deleted_len + size <= PAGE_SIZE_U16 {
                    if skiplist::first_free(p) + size < PAGE_SIZE_U16 {
                        // Delete<K,V> next.
                        let pcurs = del.cursor.pointer;
                        {
                            let ref mut skip_cursor = del[pcurs + 1].cursor;
                            p.skiplist_cursor_delete_next::<K, V>(&skip_cursor);

                            // Insert replacement
                            p.skiplist_insert_after_cursor(
                                rng,
                                skip_cursor,
                                repl.k,
                                repl.v,
                                replaced_by,
                            );
                        }
                        // self.free_last_operation(rng, del)?;
                        del.last_operation = Deleted::Updated {
                            page: 0,
                            replaced_by: p.page_offset(),
                        }
                    } else {
                        // compact + delete next.
                        debug!("compact and delete");
                        self.update_const(rng, del, replacement, p)?
                    }
                } else {
                    // Split
                    self.split(rng, p, del, replacement)?;
                }
                return Ok(());
            }
        }
        let cur = del[del.cursor.pointer + 1][0];
        p.set_right_child(cur, replaced_by);
        // self.free_last_operation(rng, del)?;
        del.last_operation = Deleted::Updated {
            page: 0,
            replaced_by: p.page_offset(),
        };
        Ok(())
    }

    fn update_mut_rebalance<R: Rng, K: Representable, V: Representable>(
        &mut self,
        rng: &mut R,
        del: &mut Delete<K, V>,
        key: K,
        value: V,
        right: u64,
        left: u64,
        former_binding_len: u16,
        p: &mut MutPage,
    ) -> Result<(), Error> {
        // If two pages below were rebalanced, the replacement
        // cannot be the binding on the page below the current
        // one. This is because a rebalance can never yield
        // the same page repartition, and therefore the
        // replacement must be one one of the two new pages.
        debug!("update_mut, rebalance");
        let size = record_size(key, value);
        // Does the new binding fit in this page?
        if skiplist::occupied(p) + size - former_binding_len < PAGE_SIZE_U16 {
            // If so, do we need to compact the page?
            if skiplist::first_free(p) + size < PAGE_SIZE_U16 {
                // No need to compact.
                debug_assert_eq!(p.offset, del[del.cursor.pointer + 1].page);
                // Delete the next binding.
                p.skiplist_cursor_delete_next::<K, V>(&del[del.cursor.pointer + 1].cursor);
                // Set the right page of the current binding
                // to `left`.
                p.set_right_child(del[del.cursor.pointer + 1][0], left);
                // Insert the new binding instead.
                {
                    let ref mut skip_cursor = del.cursor.stack[del.cursor.pointer + 1].cursor;
                    p.skiplist_insert_after_cursor(rng, skip_cursor, key, value, right);
                }
                // Then free the old pages from this rebalance.
                // self.free_last_operation(rng, del)?;

                // Finally, this page wasn't replaced.
                del.last_operation = Deleted::Updated {
                    page: 0,
                    replaced_by: p.page_offset(),
                }
            } else {
                debug!("update_mut, needs compaction");
                self.update_const(rng, del, None, p)?
            }
        } else {
            // Split
            self.split(rng, p, del, None)?
        }
        Ok(())
    }

    unsafe fn rc_decr_del<R: Rng>(&mut self, rng: &mut R, page: u64) -> Result<(), Error> {
        let rc = rc(self, page);
        debug!("rc_decr_del: {:?} {:?}", page, rc);
        if rc == 1 {
            self.remove_rc(rng, page)?;
            self.free_page(page)
        } else if rc == 0 {
            self.free_page(page)
        } else {
            self.set_rc(rng, page, rc - 1)?
        }
        Ok(())
    }

    /// Split the page, i.e. create an iterator including the
    /// replacement, and spread it over two pages. The iterator length
    /// must obviously be strictly more than one full page.
    fn split<R: Rng, P: PageT + Sized, K: Representable, V: Representable>(
        &mut self,
        rng: &mut R,
        p: &P,
        del: &mut Delete<K, V>,
        replacement: Option<Replacement<K, V>>,
    ) -> Result<(), Error> {
        debug!("splitting {:?}", del[del.cursor.pointer + 1].page);
        debug!("last = {:?}", del.last_operation);
        let (left, right, key, value) = {
            let incr_children_rc = del.cursor.pointer + 1 >= del.cursor.first_rc_level;
            let it = skiplist::iter_all(p).map(|(a, b, c)| PageItem {
                ptr: a,
                kv: b,
                right: c,
                incr_right_rc: incr_children_rc,
                incr_kv_rc: incr_children_rc,
            });
            let it = WithChanges::new(del, it, replacement);
            self.spread_on_two_pages(rng, it, skiplist::occupied(p))?
        };
        debug!("split onto {:?} and {:?}", left, right);

        del.free_stack[del.cursor.pointer + 1][0] = p.page_offset();
        del.last_operation = Deleted::Split {
            key,
            value,
            left,
            right,
            old_page: p.page_offset(),
        };
        Ok(())
    }

    /// Merge `page_a` and `page_b`, add `(middle_key, middle_value)`
    /// into the resulting page, and free `page_a` and `page_b`.
    fn merge<R: Rng, K: Representable, V: Representable>(
        &mut self,
        rng: &mut R,
        page_a: Cow,
        page_b: Cow,
        middle_ptr: *const u8,
        middle_key: K,
        middle_value: V,
        del: &mut Delete<K, V>,
        replacement: Option<Replacement<K, V>>,
    ) -> Result<u64, Error> {
        let incr_middle = del.cursor.pointer >= del.cursor.first_rc_level;
        let incr_a_children = incr_middle || rc(self, page_a.page_offset()) >= 2;
        let incr_b_children = incr_middle || rc(self, page_b.page_offset()) >= 2;
        let it_a = skiplist::iter_all(&page_a).map(|(ptr, kv, right)| PageItem {
            ptr,
            kv,
            right,
            incr_right_rc: incr_a_children,
            incr_kv_rc: incr_a_children,
        });
        let mut it_b = skiplist::iter_all(&page_b).map(|(ptr, kv, right)| PageItem {
            ptr,
            kv,
            right,
            incr_right_rc: incr_b_children,
            incr_kv_rc: incr_b_children,
        });
        debug!(
            "MERGE {:?} {:?} {:?}",
            del.cursor.pointer, del.cursor.first_rc_level, middle_key
        );
        let b0 = it_b.next().unwrap();
        debug_assert!(b0.kv.is_none());
        let new_page = {
            let it = it_a
                .chain(std::iter::once(PageItem {
                    ptr: middle_ptr,
                    kv: Some((middle_key, middle_value)),
                    right: b0.right,
                    incr_kv_rc: incr_middle,
                    incr_right_rc: incr_b_children,
                }))
                .chain(it_b);
            let it = WithChanges::new(del, it, replacement);
            self.spread_on_one_page(rng, it)?
        };
        debug!("finished {:?}", new_page);
        del.free_stack[del.cursor.pointer + 1][0] = page_a.page_offset();
        del.free_stack[del.cursor.pointer + 1][1] = page_b.page_offset();
        del.last_operation = Deleted::Merged {
            page: new_page,
            left_free: page_a.page_offset(),
            right_free: page_b.page_offset(),
            middle_ptr: middle_ptr,
            former_binding_len: record_size(middle_key, middle_value),
        };
        Ok(new_page)
    }
    /// rebalance, i.e. allocate two pages, spread the contents of
    /// `page_a` and `page_b` on the newly allocated pages, and
    /// schedule `page_a` and `page_b` for freeing (the
    /// separator key of the two new pages is on one of
    /// {`page_a`,`page_b'}, so we cannot free these now).
    fn rebalance<R: Rng, K: Representable, V: Representable>(
        &mut self,
        rng: &mut R,
        page_a: Cow,
        page_b: Cow,
        middle_ptr: *const u8,
        middle_key: K,
        middle_value: V,
        total_child_size: u16,
        del: &mut Delete<K, V>,
        replacement: Option<Replacement<K, V>>,
    ) -> Result<(), Error> {
        let incr_middle = del.cursor.pointer >= del.cursor.first_rc_level;
        let incr_a_children = incr_middle || rc(self, page_a.page_offset()) >= 2;
        let incr_b_children = incr_middle || rc(self, page_b.page_offset()) >= 2;
        let it_a = skiplist::iter_all(&page_a).map(|(ptr, kv, right)| PageItem {
            ptr,
            kv,
            right,
            incr_right_rc: incr_a_children,
            incr_kv_rc: incr_a_children,
        });
        let mut it_b = skiplist::iter_all(&page_b).map(|(ptr, kv, right)| PageItem {
            ptr,
            kv,
            right,
            incr_right_rc: incr_b_children,
            incr_kv_rc: incr_b_children,
        });

        // let (_, b0, right, _) = it_b.next().unwrap();
        let b0 = it_b.next().unwrap();
        debug_assert!(b0.kv.is_none());
        debug!(
            "REBALANCE {:?} {:?}",
            del.cursor.pointer, del.cursor.first_rc_level
        );
        let (new_a, new_b, k, v) = {
            // Fill the two new pages (`new_a` and `new_b`), write the
            // new separator into `middle`.
            let it = it_a
                .chain(std::iter::once(PageItem {
                    ptr: middle_ptr,
                    kv: Some((middle_key, middle_value)),
                    right: b0.right,
                    incr_right_rc: incr_b_children,
                    incr_kv_rc: incr_middle,
                }))
                .chain(it_b);
            let it = WithChanges::new(del, it, replacement);
            // Write the new separator for the next round.
            self.spread_on_two_pages(rng, it, total_child_size)?
        };
        debug!(
            "rebalanced {:?} {:?} onto {:?} {:?}",
            page_a.page_offset(),
            page_b.page_offset(),
            new_a,
            new_b
        );
        del.free_stack[del.cursor.pointer + 1][0] = page_a.page_offset();
        del.free_stack[del.cursor.pointer + 1][1] = page_b.page_offset();
        del.last_operation = Deleted::Rebalanced {
            key: k,
            value: v,
            left: new_a,
            right: new_b,
            former_binding_len: record_size(middle_key, middle_value),
            middle_ptr,
        };
        Ok(())
    }
}

/// A page iterator taking care of replacements
struct WithChanges<'b, I: Iterator, K: 'b, V: 'b> {
    /// The current `Delete<K,V>` record.
    del: &'b mut Delete<K, V>,

    next: Option<PageItem<K, V>>,

    /// The initial iterator (without the replacement)
    it: std::iter::Peekable<I>,

    /// Replacement
    replacement: Option<Replacement<K, V>>,
}

impl<'b, K, V, I: Iterator> WithChanges<'b, I, K, V> {
    fn new(
        del: &'b mut Delete<K, V>,
        it: I,
        replacement: Option<Replacement<K, V>>,
    ) -> WithChanges<'b, I, K, V> {
        WithChanges {
            del: del,
            next: None,
            it: it.peekable(),
            replacement: replacement,
        }
    }
}

impl<'b, K: Representable, V: Representable, P: Iterator<Item = PageItem<K, V>>> Iterator
    for WithChanges<'b, P, K, V>
{
    // The first boolean tells whether we must increment the RC of
    // children, the second whether we must increment the RC of the
    // key and value.
    type Item = PageItem<K, V>;

    fn next(&mut self) -> Option<Self::Item> {
        let result = self.iter_next();
        debug!("{:?}", result);
        result
    }
}

impl<'b, K: Representable, V: Representable, P: Iterator<Item = PageItem<K, V>>>
    WithChanges<'b, P, K, V>
{
    fn iter_next(&mut self) -> Option<PageItem<K, V>> {
        loop {
            if let Some(next) = self.next.take() {
                return Some(next);
            }
            // let (ptr, mut binding, right_, incr_rc) = if let Some(next) = self.it.next() {
            let mut item = if let Some(next) = self.it.next() {
                next
            } else {
                return None;
            };
            if let Some(replacement) = self.replacement.take() {
                // If the current binding is the one we want to
                // delete, replace it.
                if item.ptr == replacement.deleted_ptr {
                    item.kv = Some((replacement.k, replacement.v))
                } else {
                    self.replacement = Some(replacement)
                }
            }
            match self.del.last_operation {
                Deleted::Rebalanced {
                    key,
                    value,
                    right,
                    left,
                    middle_ptr,
                    ..
                } => {
                    debug!(
                        "Rebalanced {:?} {:?} {:?} {:?} {:?}",
                        key, value, right, left, middle_ptr
                    );
                    if middle_ptr == item.ptr {
                        // middle_ptr is the pointer of the previous
                        // middle element. Hence, here `item.kv` is
                        // already in the pages below, and must never
                        // be increased.
                        debug_assert!(item.kv.is_some());
                        return Some(PageItem {
                            ptr: std::ptr::null(),
                            kv: Some((key, value)),
                            right,
                            incr_kv_rc: false,
                            incr_right_rc: item.incr_right_rc,
                        });
                    } else if let Some(item_) = self.it.peek() {
                        if middle_ptr == item_.ptr {
                            return Some(PageItem {
                                ptr: std::ptr::null(),
                                kv: item.kv,
                                right: left,
                                incr_kv_rc: false,
                                incr_right_rc: item.incr_right_rc,
                            });
                        }
                    }
                }
                Deleted::Split {
                    left,
                    key,
                    value,
                    right,
                    old_page,
                    ..
                } => {
                    debug!("split {:?} {:?} {:?}", item.ptr, item.right, key);
                    if old_page == item.right {
                        // Here, (key, value) comes from a
                        // split. Therefore, its RC has already been
                        // incremented by the call of
                        // spread_on_two_pages that created the
                        // split. Hence, we must not increment it
                        // again.
                        //
                        // Moreover `left` and `right` are fresh pages
                        // created by the split, so we won't increment
                        // them in the next iteration.
                        self.next = Some(PageItem {
                            ptr: std::ptr::null(),
                            kv: Some((key, value)),
                            right,
                            incr_right_rc: false,
                            incr_kv_rc: false,
                        });
                        return Some(PageItem {
                            ptr: std::ptr::null(),
                            kv: item.kv,
                            right: left,
                            incr_right_rc: false,
                            incr_kv_rc: false,
                        });
                    }
                }
                Deleted::Merged {
                    page, middle_ptr, ..
                } => {
                    debug!("merged {:?} {:?}", middle_ptr, item.ptr);
                    if item.ptr == middle_ptr {
                        // In case of a merge, `item.kv` needs to be
                        // dropped, and we already returned `page` in
                        // the last iteration (see the "else" case).
                        continue;
                    } else if let Some(item_) = self.it.peek() {
                        if item_.ptr == middle_ptr {
                            return Some(PageItem {
                                ptr: std::ptr::null(),
                                kv: item.kv,
                                right: page,
                                incr_kv_rc: item.incr_kv_rc,
                                incr_right_rc: item.incr_right_rc,
                            });
                        }
                    }
                }
                Deleted::Updated { page, replaced_by } => {
                    debug!(
                        "Updated {:?} {:?} {:?}",
                        page, replaced_by, item.incr_right_rc
                    );
                    if item.right == page {
                        // `replaced_by` is a fresh page, so we don't increment it.
                        return Some(PageItem {
                            ptr: std::ptr::null(),
                            kv: item.kv,
                            right: replaced_by,
                            incr_kv_rc: item.incr_kv_rc,
                            incr_right_rc: false,
                        });
                    } else {
                        return Some(item);
                    }
                }
                Deleted::Leaf { deleted_ptr, .. } => {
                    debug!("Deleted? {:?} {:?}", item.ptr, deleted_ptr);
                    if item.ptr == deleted_ptr {
                        continue;
                    }
                }
            }
            return Some(item);
        }
    }
}