coordinode-lsm-tree 5.12.3

Embedded LSM-tree storage engine in pure Rust, no C/C++ dependency. MVCC snapshots, BuRR filters, zstd dictionary compression, columnar PAX blocks, AES-256-GCM at rest, self-healing per-block ECC, compaction on a near-full disk, no_std support.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
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
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
use crate::{AbstractTree, Config, SeqNo, SequenceNumberCounter, config::CompressionPolicy};
use test_log::test;

// NOTE: Tests that versions of the same key stay
// in the same table even if it needs to be rotated
//
// This avoids tables' key ranges overlapping
//
// http://github.com/fjall-rs/lsm-tree/commit/f46b6fe26a1e90113dc2dbb0342db160a295e616
#[test]
fn table_multi_writer_same_key_norotate() -> crate::Result<()> {
    let folder = tempfile::tempdir()?;

    let tree = Config::new(
        &folder,
        SequenceNumberCounter::default(),
        SequenceNumberCounter::default(),
    )
    .data_block_compression_policy(CompressionPolicy::all(crate::CompressionType::None))
    .index_block_compression_policy(CompressionPolicy::all(crate::CompressionType::None))
    .open()?;

    tree.insert("a", "a1".repeat(4_000), 0);
    tree.insert("a", "a2".repeat(4_000), 1);
    tree.insert("a", "a3".repeat(4_000), 2);
    tree.insert("a", "a4".repeat(4_000), 3);
    tree.insert("a", "a5".repeat(4_000), 4);
    tree.flush_active_memtable(0)?;
    assert_eq!(1, tree.table_count());
    assert_eq!(1, tree.len(SeqNo::MAX, None)?);

    tree.major_compact(1_024, 0)?;
    assert_eq!(1, tree.table_count());
    assert_eq!(1, tree.len(SeqNo::MAX, None)?);

    Ok(())
}

/// The blob files a table links are handed to its writer only when it
/// rotates, and it writes them at `finish`: the table is full once they no
/// longer fit its target, before they reach the writer.
#[test]
fn the_linked_blob_files_count_toward_a_full_table() -> crate::Result<()> {
    use crate::{InternalValue, UserKey, fs::StdFs, table::writer::LinkedFile};
    use std::sync::Arc;

    let folder = tempfile::tempdir()?;
    let fs: Arc<dyn crate::fs::Fs> = Arc::new(StdFs);
    let mut mw = super::MultiWriter::new(
        folder.path().to_path_buf(),
        SequenceNumberCounter::default(),
        u64::MAX,
        1,
        fs,
    )?;
    // A block large enough that the table's size, not the state it holds for
    // `finish`, decides.
    mw.write(InternalValue::from_components(
        UserKey::from(b"a" as &[u8]),
        vec![0u8; 65_536],
        0,
        crate::ValueType::Value,
    ))?;
    mw.writer.spill_block()?;
    mw.target_size = mw.writer.output_size_hint() + 100;
    assert!(mw.writer.held_state_bytes() < mw.target_size);
    assert!(!mw.table_full());
    for blob_file_id in 0..10 {
        mw.linked_blobs.insert(
            blob_file_id,
            LinkedFile {
                blob_file_id,
                bytes: 1,
                on_disk_bytes: 1,
                len: 1,
            },
        );
    }
    assert!(mw.table_full(), "ten linked files take 324 bytes");
    Ok(())
}

/// Each linked blob file is an entry in the multi-writer's map and, once the
/// table rotates, a copy in its writer, far more than the 32 bytes it takes
/// on disk: the held state counts both, so a table linking many files is full
/// by its memory before its size.
#[test]
fn the_linked_blob_files_count_toward_the_held_state() -> crate::Result<()> {
    use crate::{InternalValue, UserKey, fs::StdFs, table::writer::LinkedFile};
    use std::sync::Arc;

    const FILES: u64 = 1_000;
    let folder = tempfile::tempdir()?;
    let fs: Arc<dyn crate::fs::Fs> = Arc::new(StdFs);
    let mut mw = super::MultiWriter::new(
        folder.path().to_path_buf(),
        SequenceNumberCounter::default(),
        u64::MAX,
        1,
        fs,
    )?;
    mw.write(InternalValue::from_components(
        UserKey::from(b"a" as &[u8]),
        b"v".to_vec(),
        0,
        crate::ValueType::Value,
    ))?;
    mw.writer.spill_block()?;
    for blob_file_id in 0..FILES {
        mw.linked_blobs.insert(
            blob_file_id,
            LinkedFile {
                blob_file_id,
                bytes: 1,
                on_disk_bytes: 1,
                len: 1,
            },
        );
    }
    // Their bytes on disk fit; their entries in memory do not.
    let linked = crate::table::writer::linked_blob_files_len(mw.linked_blobs.len());
    mw.target_size = mw.writer.output_size_hint() + linked + 100;
    assert!(mw.writer.held_state_bytes() + FILES * 64 >= mw.target_size);
    assert!(mw.table_full(), "{FILES} linked files held in memory");
    Ok(())
}

/// A columnar batch lands whole, so what an output holds before it, not after,
/// is what the next output carries anyway: a first batch holding more than the
/// target fills its table, and the next batch goes to a new one.
#[cfg(feature = "columnar")]
#[test]
fn a_first_columnar_batch_past_the_target_fills_its_table() -> crate::Result<()> {
    use crate::{InternalValue, fs::StdFs, table::columnar::entries_to_column_batch};
    use std::sync::Arc;

    let folder = tempfile::tempdir()?;
    let fs: Arc<dyn crate::fs::Fs> = Arc::new(StdFs);
    let mut mw = super::MultiWriter::new(
        folder.path().to_path_buf(),
        SequenceNumberCounter::default(),
        u64::MAX,
        1,
        fs,
    )?
    .use_columnar(true);
    let entries: Vec<InternalValue> = (0..2_000u32)
        .map(|i| {
            InternalValue::from_components(
                format!("key{i:06}").into_bytes(),
                b"v".to_vec(),
                0,
                crate::ValueType::Value,
            )
        })
        .collect();
    mw.write_columnar_batch(&entries_to_column_batch(&entries)?)?;
    // Its bytes fit the target; the state it holds for `finish` does not.
    mw.target_size = mw.writer.output_size_hint() + 1;
    assert!(mw.writer.held_state_bytes() >= mw.target_size);
    assert!(mw.table_full(), "the first batch holds past the target");
    Ok(())
}

/// Rotation hands the linked blob files to the finishing writer and frees the
/// map, so a large map does not stay allocated under the next table, where
/// nothing counts it.
#[test]
fn rotation_frees_the_linked_blob_map() -> crate::Result<()> {
    use crate::{InternalValue, UserKey, fs::StdFs, table::writer::LinkedFile};
    use std::sync::Arc;

    let folder = tempfile::tempdir()?;
    let fs: Arc<dyn crate::fs::Fs> = Arc::new(StdFs);
    let mut mw = super::MultiWriter::new(
        folder.path().to_path_buf(),
        SequenceNumberCounter::default(),
        u64::MAX,
        1,
        fs,
    )?;
    mw.write(InternalValue::from_components(
        UserKey::from(b"a" as &[u8]),
        b"v".to_vec(),
        0,
        crate::ValueType::Value,
    ))?;
    for blob_file_id in 0..1_000 {
        mw.linked_blobs.insert(
            blob_file_id,
            LinkedFile {
                blob_file_id,
                bytes: 1,
                on_disk_bytes: 1,
                len: 1,
            },
        );
    }
    mw.current_key = Some(UserKey::from(b"b" as &[u8]));
    mw.rotate()?;
    assert_eq!(mw.linked_blobs.capacity(), 0);
    Ok(())
}

// Regression (#32): compaction clip must preserve RT covering gap between
// output tables.  Before the fix, MultiWriter clipped each RT to
// [first_key, upper_bound(last_key)) — RTs in the gap were dropped by all
// tables.  The fix clips to [first_key, next_table_first_key) during
// rotation, covering the gap, and widens key_range so point reads find it.
#[test]
fn clip_preserves_rt_covering_gap_between_output_tables() -> crate::Result<()> {
    use crate::range_tombstone::RangeTombstone;
    use crate::{InternalValue, UserKey, fs::StdFs};
    use std::sync::Arc;

    let folder = tempfile::tempdir()?;
    let base_path = folder.path().join("tables");
    std::fs::create_dir_all(&base_path)?;

    let id_gen = SequenceNumberCounter::default();
    let fs: Arc<dyn crate::fs::Fs> = Arc::new(StdFs);

    // Tiny target_size to force rotation between "l" and "q"
    let mut mw =
        super::MultiWriter::new(base_path.clone(), id_gen, 100, 1, fs)?.use_clip_range_tombstones();

    mw.set_range_tombstones(vec![RangeTombstone::new(
        UserKey::from(b"m" as &[u8]),
        UserKey::from(b"p" as &[u8]),
        20,
    )]);

    // Table 1: keys [a, l]  — values large enough to fill a 4 KiB data
    // block and push file_pos past target_size so rotation fires on "q".
    mw.write(InternalValue::from_components(
        UserKey::from(b"a" as &[u8]),
        vec![0u8; 4_000],
        1,
        crate::ValueType::Value,
    ))?;
    mw.write(InternalValue::from_components(
        UserKey::from(b"l" as &[u8]),
        vec![0u8; 4_000],
        2,
        crate::ValueType::Value,
    ))?;
    // Table 2: keys [q, z]  — rotation happens before "q"
    mw.write(InternalValue::from_components(
        UserKey::from(b"q" as &[u8]),
        vec![0u8; 4_000],
        3,
        crate::ValueType::Value,
    ))?;
    mw.write(InternalValue::from_components(
        UserKey::from(b"z" as &[u8]),
        vec![0u8; 4_000],
        4,
        crate::ValueType::Value,
    ))?;

    let results = mw.finish()?;
    assert!(
        results.len() >= 2,
        "expected 2+ output tables to verify gap, got {}",
        results.len(),
    );

    // Recover each output table and count preserved RTs
    let cache = Arc::new(crate::Cache::with_capacity_bytes(64 * 1_024));
    let comparator: crate::SharedComparator = Arc::new(crate::DefaultUserComparator);
    let mut total_rts = 0;

    for (table_id, checksum) in &results {
        let table = crate::Table::recover(crate::table::RecoverParams::new(
            base_path.join(table_id.to_string()),
            *checksum,
            *table_id,
            Arc::new(StdFs),
            comparator.clone(),
            cache.clone(),
        ))?;
        total_rts += table.range_tombstones().len();
    }

    assert!(
        total_rts > 0,
        "BUG: RT [m,p)@20 was dropped by compaction clip — \
         no output table preserved it (gap between tables)",
    );

    Ok(())
}

// Edge case (#32): RT spans past the next table's first key, so
// clipped.end == clip_upper.  Widening last_key to clip_upper would
// make adjacent tables' key_ranges overlap and break Run::get_for_key_cmp.
// Verify the RT is still written but key_range stays disjoint.
#[test]
fn clip_rt_spanning_next_table_does_not_overlap_key_ranges() -> crate::Result<()> {
    use crate::{InternalValue, UserKey, fs::StdFs};
    use std::sync::Arc;

    let folder = tempfile::tempdir()?;
    let base_path = folder.path().join("tables");
    std::fs::create_dir_all(&base_path)?;

    let id_gen = SequenceNumberCounter::default();
    let fs: Arc<dyn crate::fs::Fs> = Arc::new(StdFs);

    let mut mw =
        super::MultiWriter::new(base_path.clone(), id_gen, 100, 1, fs)?.use_clip_range_tombstones();

    // RT [m, r) — end "r" > next table's first key "q", so after
    // clipping to [first_key, clip_upper="q") the clipped.end == "q".
    mw.set_range_tombstones(vec![crate::range_tombstone::RangeTombstone::new(
        UserKey::from(b"m" as &[u8]),
        UserKey::from(b"r" as &[u8]),
        20,
    )]);

    // Table 1: [a, l]
    mw.write(InternalValue::from_components(
        UserKey::from(b"a" as &[u8]),
        vec![0u8; 4_000],
        1,
        crate::ValueType::Value,
    ))?;
    mw.write(InternalValue::from_components(
        UserKey::from(b"l" as &[u8]),
        vec![0u8; 4_000],
        2,
        crate::ValueType::Value,
    ))?;
    // Table 2: [q, z]
    mw.write(InternalValue::from_components(
        UserKey::from(b"q" as &[u8]),
        vec![0u8; 4_000],
        3,
        crate::ValueType::Value,
    ))?;
    mw.write(InternalValue::from_components(
        UserKey::from(b"z" as &[u8]),
        vec![0u8; 4_000],
        4,
        crate::ValueType::Value,
    ))?;

    let results = mw.finish()?;
    assert!(results.len() >= 2);

    let cache = Arc::new(crate::Cache::with_capacity_bytes(64 * 1_024));
    let comparator: crate::SharedComparator = Arc::new(crate::DefaultUserComparator);

    let mut tables = Vec::new();
    for (table_id, checksum) in &results {
        tables.push(crate::Table::recover(crate::table::RecoverParams::new(
            base_path.join(table_id.to_string()),
            *checksum,
            *table_id,
            Arc::new(StdFs),
            comparator.clone(),
            cache.clone(),
        ))?);
    }

    // Key ranges must be disjoint: table1.max < table2.min
    let t1_max = tables[0].metadata.key_range.max();
    let t2_min = tables[1].metadata.key_range.min();
    assert!(
        t1_max.as_ref() < t2_min.as_ref(),
        "key_ranges must be disjoint: table1.max={t1_max:?} must be < table2.min={t2_min:?}",
    );

    // RT must still be written to at least one output table
    let total_rts: usize = tables.iter().map(|t| t.range_tombstones().len()).sum();
    assert!(
        total_rts > 0,
        "RT [m,r)@20 must be preserved in at least one output table",
    );

    Ok(())
}

// NOTE: Follow-up fix for non-disjoint output
//
// https://github.com/fjall-rs/lsm-tree/commit/1609a57c2314420b858d826790ecd1442aa76720
#[test]
fn table_multi_writer_same_key_norotate_2() -> crate::Result<()> {
    let folder = tempfile::tempdir()?;

    let tree = Config::new(
        &folder,
        SequenceNumberCounter::default(),
        SequenceNumberCounter::default(),
    )
    .data_block_compression_policy(CompressionPolicy::all(crate::CompressionType::None))
    .index_block_compression_policy(CompressionPolicy::all(crate::CompressionType::None))
    .open()?;

    tree.insert("a", "a1".repeat(4_000), 0);
    tree.insert("a", "a1".repeat(4_000), 1);
    tree.insert("a", "a1".repeat(4_000), 2);
    tree.insert("b", "a1".repeat(4_000), 0);
    tree.insert("c", "a1".repeat(4_000), 0);
    tree.insert("c", "a1".repeat(4_000), 1);
    tree.flush_active_memtable(0)?;
    assert_eq!(1, tree.table_count());
    assert_eq!(3, tree.len(SeqNo::MAX, None)?);

    tree.major_compact(1_024, 0)?;
    assert_eq!(3, tree.table_count());
    assert_eq!(3, tree.len(SeqNo::MAX, None)?);

    Ok(())
}

// D5b round-trip: the retrieval-ribbon locator section must survive the real
// table writer + reader path. With the policy enabled every inserted key
// recovers a `(block_id, slot)` from the on-disk section (validating the
// writer's per-key block_id/slot accumulation, not just synthetic inputs);
// with the policy disabled the section is absent entirely (zero bytes, the
// byte-identical guarantee).
#[test]
#[expect(
    clippy::expect_used,
    reason = "test asserts a freshly-written section is present + recovers"
)]
fn locator_section_round_trips_through_writer() -> crate::Result<()> {
    use crate::config::{LocatorPolicyEntry, LocatorPrecision};
    use crate::table::block::BlockType;
    use crate::table::locator::locate;
    use crate::{CompressionType, InternalValue, UserKey, fs::StdFs};
    use std::sync::Arc;

    let folder = tempfile::tempdir()?;
    let fs: Arc<dyn crate::fs::Fs> = Arc::new(StdFs);
    // Small data blocks + many small KVs → many data blocks, so block_id is
    // non-trivial and the accumulation across block boundaries is exercised.
    let n = 2_000u64;

    let write_and_recover =
        |base: &std::path::Path, entry: LocatorPolicyEntry| -> crate::Result<crate::Table> {
            std::fs::create_dir_all(base)?;
            let mut mw = super::MultiWriter::new(
                base.to_path_buf(),
                SequenceNumberCounter::default(),
                64 * 1_024 * 1_024,
                1,
                fs.clone(),
            )?
            .use_data_block_size(4_096)
            .use_locator(entry);
            for i in 0..n {
                mw.write(InternalValue::from_components(
                    UserKey::from(i.to_be_bytes().as_slice()),
                    vec![0u8; 64],
                    1,
                    crate::ValueType::Value,
                ))?;
            }
            let results = mw.finish()?;
            assert_eq!(results.len(), 1, "single output table expected");
            let (table_id, checksum) = results[0];
            crate::Table::recover(crate::table::RecoverParams::new(
                base.join(table_id.to_string()),
                checksum,
                table_id,
                Arc::new(StdFs),
                Arc::new(crate::DefaultUserComparator),
                Arc::new(crate::Cache::with_capacity_bytes(1 << 20)),
            ))
        };

    // 1) Enabled (auto widths): section present, every key recovers.
    let base_on = folder.path().join("on");
    let table = write_and_recover(
        &base_on,
        LocatorPolicyEntry::Enabled {
            precision: LocatorPrecision::Restart,
            block_id_bits: None,
            slot_bits: None,
        },
    )?;
    let handle = table
        .regions
        .locator
        .expect("locator section must be present when enabled");
    let block = table.load_block(
        &handle,
        BlockType::Locator,
        CompressionType::None,
        #[cfg(zstd_any)]
        None,
    )?;
    let section_bytes: &[u8] = &block.data;
    let num_blocks = table.metadata.data_block_count;
    assert!(num_blocks > 1, "test should produce multiple data blocks");
    for i in 0..n {
        let h = crate::hash::hash64(&i.to_be_bytes());
        let (block_id, _slot) =
            locate(section_bytes, h)?.unwrap_or_else(|| panic!("inserted key {i} must locate"));
        assert!(
            block_id < num_blocks,
            "key {i}: block_id {block_id} >= data_block_count {num_blocks}",
        );
    }

    // 2) Disabled (default): no section (zero bytes, byte-identical).
    let base_off = folder.path().join("off");
    let table_off = write_and_recover(&base_off, LocatorPolicyEntry::None)?;
    assert!(
        table_off.regions.locator.is_none(),
        "disabled policy must emit no locator section",
    );

    Ok(())
}

/// A filter transformation whose record TRIGGERS the rotation belongs to the
/// output that would have received it — the NEW one. Rotation runs before
/// the triggering record is inserted, so at that moment the live counter
/// already carries its verdict; judging the finishing output by the live
/// counter would mark the UNTOUCHED old output transformed and leave the
/// transformed new output plain — after manifest loss, repair could then
/// discard the transformed output as derived and resurrect the pre-filter
/// value from its inputs.
#[test]
fn a_transform_on_the_rotation_boundary_belongs_to_the_new_output() -> crate::Result<()> {
    use crate::fs::StdFs;
    use crate::{InternalValue, UserKey};
    use std::sync::Arc;

    let folder = tempfile::tempdir()?;
    let base_path = folder.path().to_path_buf();
    std::fs::create_dir_all(&base_path)?;

    let id_gen = SequenceNumberCounter::default();
    let fs: Arc<dyn crate::fs::Fs> = Arc::new(StdFs);
    let marker = Arc::new(portable_atomic::AtomicU64::new(0));

    // A target the two 4 KiB fillers pass once their block is written, and a
    // single key with the tail every table writes and its filter's bound (a
    // word per slot at every layer's floor) does not: rotation fires on the
    // key AFTER the fillers.
    let mut mw = super::MultiWriter::new(base_path.clone(), id_gen, 16_000, 1, fs)?
        .use_lineage(Some(vec![7, 8]))
        .use_transform_marker(Arc::clone(&marker));

    // Output 1: untouched keys.
    for key in [b"a" as &[u8], b"l"] {
        mw.write(InternalValue::from_components(
            UserKey::from(key),
            vec![0u8; 4_000],
            1,
            crate::ValueType::Value,
        ))?;
    }
    // The filter TRANSFORMS the next record (a Remove verdict replaces it
    // with a tombstone) — the adapter ticks the marker BEFORE the write, and
    // the write's rotation happens before the record is inserted.
    marker.fetch_add(1, core::sync::atomic::Ordering::Relaxed);
    mw.write(InternalValue::from_components(
        UserKey::from(b"q" as &[u8]),
        vec![],
        2,
        crate::ValueType::Tombstone,
    ))?;
    mw.write(InternalValue::from_components(
        UserKey::from(b"z" as &[u8]),
        vec![0u8; 100],
        3,
        crate::ValueType::Value,
    ))?;

    let results = mw.finish()?;
    assert_eq!(
        results.len(),
        2,
        "the boundary transform forces two outputs"
    );

    let cache = Arc::new(crate::Cache::with_capacity_bytes(64 * 1_024));
    let comparator: crate::SharedComparator = Arc::new(crate::DefaultUserComparator);
    let mut lineages = Vec::new();
    for (table_id, checksum) in &results {
        let table = crate::Table::recover(crate::table::RecoverParams::new(
            base_path.join(table_id.to_string()),
            *checksum,
            *table_id,
            Arc::new(StdFs),
            comparator.clone(),
            cache.clone(),
        ))?;
        lineages.push((
            table.metadata.lineage.clone(),
            table.metadata.lineage_transformed,
        ));
    }
    assert_eq!(
        lineages,
        vec![(Some(vec![7, 8]), false), (Some(vec![7, 8]), true),],
        "both outputs keep their lineage; only the output holding the \
         transformed record carries the transformed marker",
    );
    Ok(())
}

/// Regression: output rotation must recognise a NEW user key by byte
/// identity, not by byte order. `MultiWriter::write` used `current_key <
/// new_key` to detect the next key, which under a comparator whose order is
/// not the byte order (here: reversed) is false for every key after the
/// first, so `target_size` was never consulted and the whole stream landed
/// in one table. Keys arrive in comparator order (descending bytes), each
/// with a value that fills a whole 4 KiB data block (so the block spills and
/// the size hint grows past the 100-byte target before the next key):
/// every key after the first must open a new output.
#[test]
fn multi_writer_rotates_under_a_non_lexicographic_comparator() -> crate::Result<()> {
    use crate::fs::StdFs;
    use crate::{InternalValue, UserKey};
    use std::sync::Arc;

    #[derive(Debug)]
    struct ReverseComparator;
    impl crate::comparator::UserComparator for ReverseComparator {
        fn name(&self) -> &'static str {
            "reverse-test"
        }
        fn compare(&self, a: &[u8], b: &[u8]) -> core::cmp::Ordering {
            b.cmp(a)
        }
        fn is_lexicographic(&self) -> bool {
            false
        }
    }

    let folder = tempfile::tempdir()?;
    let base_path = folder.path().to_path_buf();
    std::fs::create_dir_all(&base_path)?;

    let id_gen = SequenceNumberCounter::default();
    let fs: Arc<dyn crate::fs::Fs> = Arc::new(StdFs);
    let mut mw = super::MultiWriter::new(base_path, id_gen, 100, 1, fs)?
        .set_comparator(Arc::new(ReverseComparator));

    // Comparator order for a reversed comparator: byte-descending.
    for key in [b"z" as &[u8], b"l", b"a"] {
        mw.write(InternalValue::from_components(
            UserKey::from(key),
            vec![0u8; 4_096],
            1,
            crate::ValueType::Value,
        ))?;
    }

    let results = mw.finish()?;
    assert_eq!(
        results.len(),
        3,
        "each new key exceeds the 100-byte target, so every key after the first opens a new output"
    );
    Ok(())
}

fn recover_outputs(
    base_path: &std::path::Path,
    results: &[(crate::TableId, crate::Checksum)],
) -> crate::Result<Vec<crate::Table>> {
    use crate::fs::StdFs;
    use std::sync::Arc;

    let cache = Arc::new(crate::Cache::with_capacity_bytes(64 * 1_024));
    let comparator: crate::SharedComparator = Arc::new(crate::DefaultUserComparator);
    results
        .iter()
        .map(|(table_id, checksum)| {
            crate::Table::recover(crate::table::RecoverParams::new(
                base_path.join(table_id.to_string()),
                *checksum,
                *table_id,
                Arc::new(StdFs),
                comparator.clone(),
                cache.clone(),
            ))
        })
        .collect()
}

/// A flush cuts each range tombstone into the zones of the outputs it spans
/// instead of copying the whole set into every output: the pieces of one
/// tombstone chain up to exactly its range, the first output reaches below
/// the memtable's first key and the last one above its last key.
#[test]
fn a_flush_cuts_each_tombstone_into_the_outputs_it_spans() -> crate::Result<()> {
    use crate::{InternalValue, UserKey, fs::StdFs, range_tombstone::RangeTombstone};
    use std::sync::Arc;

    let folder = tempfile::tempdir()?;
    let base_path = folder.path().to_path_buf();
    let fs: Arc<dyn crate::fs::Fs> = Arc::new(StdFs);
    // Every key passes the target with the tail every table writes, and the
    // few tombstones a rotation carries stay well under half of it.
    let mut mw = super::MultiWriter::new(
        base_path.clone(),
        SequenceNumberCounter::default(),
        4_000,
        1,
        fs,
    )?;
    let tombstones = [
        (b"a" as &[u8], b"c" as &[u8], 20),
        (b"k", b"r", 21),
        (b"w", b"zz", 22),
    ];
    mw.set_range_tombstones(
        tombstones
            .iter()
            .map(|&(start, end, seqno)| {
                RangeTombstone::new(UserKey::from(start), UserKey::from(end), seqno)
            })
            .collect(),
    );
    // Each key fills a block of its own and passes the target once written,
    // so the outputs' zones are (.., l), [l, q), [q, x), [x, ..).
    for key in [b"b" as &[u8], b"l", b"q", b"x"] {
        mw.write(InternalValue::from_components(
            UserKey::from(key),
            vec![0u8; 5_000],
            1,
            crate::ValueType::Value,
        ))?;
    }
    let tables = recover_outputs(&base_path, &mw.finish()?)?;
    assert_eq!(tables.len(), 4);

    let mut pieces: Vec<(SeqNo, Vec<u8>, Vec<u8>)> = tables
        .iter()
        .flat_map(|table| table.range_tombstones().iter())
        .map(|rt| (rt.seqno, rt.start.to_vec(), rt.end.to_vec()))
        .collect();
    pieces.sort();
    assert_eq!(pieces.len(), 6, "1 + 3 + 2 zones spanned: {pieces:?}");
    for &(start, end, seqno) in &tombstones {
        let own: Vec<_> = pieces.iter().filter(|p| p.0 == seqno).collect();
        assert_eq!(own.first().map(|p| p.1.as_slice()), Some(start));
        assert_eq!(own.last().map(|p| p.2.as_slice()), Some(end));
        for pair in own.windows(2) {
            assert_eq!(
                pair[0].2, pair[1].1,
                "pieces of @{seqno} must chain: {own:?}"
            );
        }
    }
    assert_eq!(tables[0].metadata.key_range.min().as_ref(), b"a");
    assert_eq!(tables[3].metadata.key_range.max().as_ref(), b"zz");
    Ok(())
}

/// A flush's tombstones starting past its last key reach no key's rotation
/// check: they are checked at their starts, so they spread over outputs of
/// tombstones alone near the target instead of all landing in the last one.
#[test]
fn a_flush_splits_its_tombstones_past_the_last_key() -> crate::Result<()> {
    use crate::{InternalValue, UserKey, fs::StdFs, range_tombstone::RangeTombstone};
    use std::sync::Arc;

    // Small enough that the tail an output of tombstones alone writes is a
    // good part of it.
    const TARGET: u64 = 12 * 1_024;
    const TOMBSTONES: usize = 2_000;

    // Pseudo-random bounds, so no codec shrinks the tombstone block.
    let bound = |seed: usize, mut key: Vec<u8>| {
        let mut state = (seed as u64 + 1).wrapping_mul(0x9E37_79B9_7F4A_7C15);
        while key.len() < 64 {
            state ^= state << 13;
            state ^= state >> 7;
            state ^= state << 17;
            key.extend_from_slice(&state.to_le_bytes());
        }
        key
    };
    let folder = tempfile::tempdir()?;
    let base_path = folder.path().to_path_buf();
    let fs: Arc<dyn crate::fs::Fs> = Arc::new(StdFs);
    let mut mw = super::MultiWriter::new(
        base_path.clone(),
        SequenceNumberCounter::default(),
        TARGET,
        1,
        fs,
    )?;
    let tombstones: Vec<_> = (0..TOMBSTONES)
        .map(|i| {
            let prefix = format!("z{i:08}").into_bytes();
            let mut start = prefix.clone();
            start.push(0);
            let mut end = prefix;
            end.push(1);
            RangeTombstone::new(
                UserKey::from(bound(2 * i, start)),
                UserKey::from(bound(2 * i + 1, end)),
                5,
            )
        })
        .collect();
    mw.set_range_tombstones(tombstones.clone());
    for key in [b"a" as &[u8], b"b", b"c"] {
        mw.write(InternalValue::from_components(
            UserKey::from(key),
            b"v".to_vec(),
            1,
            crate::ValueType::Value,
        ))?;
    }
    let tables = recover_outputs(&base_path, &mw.finish()?)?;
    assert!(tables.len() > 1, "the tombstones must spread over outputs");

    let mut written: Vec<_> = tables
        .iter()
        .flat_map(|table| table.range_tombstones().iter())
        .map(|rt| (rt.start.to_vec(), rt.end.to_vec()))
        .collect();
    written.sort();
    let expected: Vec<_> = tombstones
        .iter()
        .map(|rt| (rt.start.to_vec(), rt.end.to_vec()))
        .collect();
    assert_eq!(written, expected, "each tombstone written once, whole");
    // Each output closes once it reaches the target, counting the tail, and
    // the sentinel block an output of tombstones alone writes: it passes the
    // target by the last entry at most.
    for table in &tables {
        let file_size = std::fs::metadata(base_path.join(table.id().to_string()))?.len();
        assert!(
            file_size <= TARGET + 512,
            "output of {file_size} bytes with {} tombstones overran the {TARGET}-byte target",
            table.range_tombstones().len(),
        );
    }
    Ok(())
}

/// Every table carries its tail, and closing a table does not shed it: a
/// target below the tail and a block still gives tables of a block each, not
/// of one key each.
#[test]
fn a_target_below_the_table_tail_still_fills_a_block() -> crate::Result<()> {
    use crate::{InternalValue, UserKey, fs::StdFs};
    use std::sync::Arc;

    const KEYS: u32 = 4_000;
    let folder = tempfile::tempdir()?;
    let fs: Arc<dyn crate::fs::Fs> = Arc::new(StdFs);
    let mut mw = super::MultiWriter::new(
        folder.path().to_path_buf(),
        SequenceNumberCounter::default(),
        4_096,
        1,
        fs,
    )?;
    let mut bytes = 0;
    for i in 0..KEYS {
        let key = format!("key{i:06}").into_bytes();
        let value = random_bound_of(i as usize, Vec::new(), 24);
        bytes += key.len() + value.len();
        mw.write(InternalValue::from_components(
            UserKey::from(key),
            value,
            1,
            crate::ValueType::Value,
        ))?;
    }
    let tables = mw.finish()?.len();
    // A 4 KiB block each, and a partial one at the end.
    let blocks = bytes / 4_096 + 1;
    assert!(
        tables <= blocks,
        "{tables} tables for {KEYS} keys in {blocks} blocks",
    );
    Ok(())
}

/// An output of tombstones alone carries its synthetic table, which closing
/// it does not shed. With a target below that table, the outputs past the
/// last key still take a block's worth of tombstones each, by their bytes or
/// the entries they hold, not one tombstone each.
#[test]
fn outputs_of_tombstones_alone_below_their_fixed_table_fill_a_block() -> crate::Result<()> {
    use crate::{UserKey, range_tombstone::RangeTombstone};

    const TOMBSTONES: usize = 300;
    let tombstones: Vec<_> = (0..TOMBSTONES)
        .map(|i| {
            let prefix = format!("z{i:08}").into_bytes();
            let mut start = prefix.clone();
            start.push(0);
            let mut end = prefix;
            end.push(1);
            RangeTombstone::new(
                UserKey::from(random_bound(2 * i, start)),
                UserKey::from(random_bound(2 * i + 1, end)),
                5,
            )
        })
        .collect();
    let (_folder, tables) = flush_outputs(4_096, &[b"a"], tombstones)?;
    // A block's worth of these entries in memory is several of them. The
    // first output holds the record, the last whatever is left.
    let alone = tables
        .get(1..tables.len().saturating_sub(1))
        .unwrap_or_default();
    assert!(!alone.is_empty(), "the tombstones must spread over outputs");
    for table in alone {
        assert!(
            table.range_tombstones().len() >= 4,
            "an output of {} tombstones among {} outputs",
            table.range_tombstones().len(),
            tables.len(),
        );
    }
    Ok(())
}

/// An output of tombstones alone builds the filter over its sentinel at
/// `finish`: with an extractor yielding every prefix of a long bound, that
/// build takes a good part of the target, so such outputs close on fewer
/// tombstones than without it.
#[test]
fn outputs_of_tombstones_alone_count_their_sentinel_filter_build() -> crate::Result<()> {
    use crate::{InternalValue, UserKey, fs::StdFs, range_tombstone::RangeTombstone};
    use std::sync::Arc;

    const LEN: usize = 4_000;
    let target =
        2 * crate::table::filter::ribbon::burr::builder::build_peak_bytes(LEN + 1, false) as u64;
    let outputs = |extractor: Option<Arc<dyn crate::prefix::PrefixExtractor>>| {
        let folder = tempfile::tempdir()?;
        let fs: Arc<dyn crate::fs::Fs> = Arc::new(StdFs);
        let mut mw = super::MultiWriter::new(
            folder.path().to_path_buf(),
            SequenceNumberCounter::default(),
            target,
            1,
            fs,
        )?
        .use_prefix_extractor(extractor);
        mw.set_range_tombstones(
            (0..64)
                .map(|i| {
                    let prefix = format!("z{i:08}").into_bytes();
                    let mut start = prefix.clone();
                    start.push(0);
                    let mut end = prefix;
                    end.push(1);
                    RangeTombstone::new(
                        UserKey::from(random_bound_of(2 * i, start, LEN)),
                        UserKey::from(random_bound_of(2 * i + 1, end, LEN)),
                        5,
                    )
                })
                .collect(),
        );
        mw.write(InternalValue::from_components(
            UserKey::from(b"a" as &[u8]),
            b"v".to_vec(),
            1,
            crate::ValueType::Value,
        ))?;
        crate::Result::Ok(mw.finish()?.len())
    };
    let plain = outputs(None)?;
    let with_prefixes = outputs(Some(Arc::new(AllPrefixes)))?;
    assert!(
        2 * with_prefixes >= 3 * plain,
        "{with_prefixes} outputs with the prefixes, {plain} without",
    );
    Ok(())
}

/// A tombstone spanning many keys is carried into every output, like the
/// tail every table ends with, and closing one sheds none of it: with a
/// target below that tail, the outputs still take a block's worth of keys
/// each, not one key each.
#[test]
fn a_carried_tombstone_does_not_close_a_table_on_every_key() -> crate::Result<()> {
    use crate::{UserKey, range_tombstone::RangeTombstone};

    const KEYS: usize = 1_000;
    let keys: Vec<Vec<u8>> = (0..KEYS).map(|i| format!("k{i:05}").into_bytes()).collect();
    let keys: Vec<&[u8]> = keys.iter().map(Vec::as_slice).collect();
    let tombstone = RangeTombstone::new(
        UserKey::from(b"k" as &[u8]),
        UserKey::from(b"l" as &[u8]),
        5,
    );
    let (_folder, tables) = flush_outputs(4_096, &keys, vec![tombstone])?;
    assert!(
        tables.len() <= KEYS / 20,
        "{} outputs for {KEYS} keys",
        tables.len(),
    );
    Ok(())
}

/// A tombstone past the last key with a long bound sizes the synthetic table
/// every output of tombstones alone is estimated with; the short ones before
/// it still pack a block's worth into each such output, not one each.
#[test]
fn one_long_trailing_bound_does_not_fragment_the_tombstones_before_it() -> crate::Result<()> {
    use crate::{UserKey, range_tombstone::RangeTombstone};

    const TOMBSTONES: usize = 200;
    let mut tombstones: Vec<_> = (0..TOMBSTONES)
        .map(|i| {
            let start = format!("x{i:05}").into_bytes();
            let mut end = start.clone();
            end.push(0);
            RangeTombstone::new(UserKey::from(start), UserKey::from(end), 5)
        })
        .collect();
    tombstones.push(RangeTombstone::new(
        UserKey::from(random_bound_of(1, b"y".to_vec(), 20_000)),
        UserKey::from(b"z" as &[u8]),
        5,
    ));
    let (_folder, tables) = flush_outputs(4_096, &[b"a"], tombstones)?;
    assert!(
        tables.len() <= TOMBSTONES / 4,
        "{} outputs for {TOMBSTONES} short tombstones",
        tables.len(),
    );
    Ok(())
}

/// The first tombstones past a flush's last key can all share one start: the
/// output holding the records has no tombstone yet, and the check at that
/// start still counts the group, so it does not land on that output.
#[test]
fn a_flush_counts_a_first_start_group_past_its_records() -> crate::Result<()> {
    use crate::{UserKey, range_tombstone::RangeTombstone};

    const TARGET: u64 = 32 * 1_024;
    let keys: Vec<Vec<u8>> = (0..100).map(|i| format!("k{i:05}").into_bytes()).collect();
    let keys: Vec<&[u8]> = keys.iter().map(Vec::as_slice).collect();
    // The group alone passes the target.
    let group = (0..600)
        .map(|i| {
            RangeTombstone::new(
                UserKey::from(b"y" as &[u8]),
                UserKey::from(random_bound(i, b"y\x01".to_vec())),
                5,
            )
        })
        .collect();
    let (folder, tables) = flush_outputs(TARGET, &keys, group)?;
    let Some(records) = tables.first() else {
        panic!("a flush writes an output");
    };
    let file_size = std::fs::metadata(folder.path().join(records.id().to_string()))?.len();
    assert!(
        file_size <= TARGET + 4 * 1_024,
        "the output of records took {} tombstones in {file_size} bytes",
        records.range_tombstones().len(),
    );
    Ok(())
}

/// Every byte prefix of a key is a token.
struct AllPrefixes;

impl crate::prefix::PrefixExtractor for AllPrefixes {
    fn prefixes<'a>(&self, key: &'a [u8]) -> Box<dyn Iterator<Item = &'a [u8]> + 'a> {
        Box::new((1..=key.len()).filter_map(|end| key.get(..end)))
    }
}

/// An output of tombstones alone writes a sentinel entry, and each section
/// the writer is configured for records it: a partitioned index and filter,
/// the zone map, the seqno bounds, the locator, and a filter holding the
/// sentinel's prefixes. The split counts all of them, so such an output still
/// ends near its target.
#[test]
fn a_flush_counts_every_section_of_an_output_of_tombstones_alone() -> crate::Result<()> {
    use crate::{InternalValue, UserKey, fs::StdFs, range_tombstone::RangeTombstone};
    use std::sync::Arc;

    const TARGET: u64 = 32 * 1_024;
    let folder = tempfile::tempdir()?;
    let base_path = folder.path().to_path_buf();
    let fs: Arc<dyn crate::fs::Fs> = Arc::new(StdFs);
    let mut mw = super::MultiWriter::new(
        base_path.clone(),
        SequenceNumberCounter::default(),
        TARGET,
        1,
        fs,
    )?
    .use_adaptive_index(0)
    .use_partitioned_filter()
    .use_zone_map(true)
    .use_seqno_in_index(true)
    .use_locator(crate::config::LocatorPolicyEntry::Enabled {
        precision: crate::config::LocatorPrecision::Entry,
        block_id_bits: None,
        slot_bits: None,
    })
    .use_prefix_extractor(Some(Arc::new(AllPrefixes)));
    let tombstones: Vec<_> = (0..40)
        .map(|i| {
            let prefix = format!("z{i:08}").into_bytes();
            let mut start = prefix.clone();
            start.push(0);
            let mut end = prefix;
            end.push(1);
            RangeTombstone::new(
                UserKey::from(random_bound_of(2 * i, start, 1_000)),
                UserKey::from(random_bound_of(2 * i + 1, end, 1_000)),
                5,
            )
        })
        .collect();
    mw.set_range_tombstones(tombstones);
    mw.write(InternalValue::from_components(
        UserKey::from(b"a" as &[u8]),
        b"v".to_vec(),
        1,
        crate::ValueType::Value,
    ))?;
    let tables = recover_outputs(&base_path, &mw.finish()?)?;
    assert!(tables.len() > 1, "the tombstones must spread over outputs");
    for table in &tables {
        let file_size = std::fs::metadata(base_path.join(table.id().to_string()))?.len();
        assert!(
            file_size <= TARGET + 512,
            "output of {file_size} bytes with {} tombstones overran the {TARGET}-byte target",
            table.range_tombstones().len(),
        );
    }
    Ok(())
}

/// `key` extended to 64 bytes with a pseudo-random tail from `seed`, so no
/// codec shrinks a block of such bounds.
fn random_bound(seed: usize, key: Vec<u8>) -> Vec<u8> {
    random_bound_of(seed, key, 64)
}

/// `key` extended to at least `len` bytes with a pseudo-random tail.
fn random_bound_of(seed: usize, mut key: Vec<u8>, len: usize) -> Vec<u8> {
    let mut state = (seed as u64 + 1).wrapping_mul(0x9E37_79B9_7F4A_7C15);
    while key.len() < len {
        state ^= state << 13;
        state ^= state >> 7;
        state ^= state << 17;
        key.extend_from_slice(&state.to_le_bytes());
    }
    key
}

/// A flush over `keys` with `tombstones`, recovered.
fn flush_outputs(
    target: u64,
    keys: &[&[u8]],
    tombstones: Vec<crate::range_tombstone::RangeTombstone>,
) -> crate::Result<(tempfile::TempDir, Vec<crate::Table>)> {
    use crate::{InternalValue, UserKey, fs::StdFs};
    use std::sync::Arc;

    let folder = tempfile::tempdir()?;
    let base_path = folder.path().to_path_buf();
    let fs: Arc<dyn crate::fs::Fs> = Arc::new(StdFs);
    let mut mw = super::MultiWriter::new(
        base_path.clone(),
        SequenceNumberCounter::default(),
        target,
        1,
        fs,
    )?;
    mw.set_range_tombstones(tombstones);
    for &key in keys {
        mw.write(InternalValue::from_components(
            UserKey::from(key),
            b"v".to_vec(),
            1,
            crate::ValueType::Value,
        ))?;
    }
    let tables = recover_outputs(&base_path, &mw.finish()?)?;
    Ok((folder, tables))
}

/// The tombstones sharing the greatest start follow no later start at which
/// the output could be checked: the check at their start counts them, so
/// they are split from what came before instead of overrunning its output.
#[test]
fn a_flush_counts_the_last_start_group_before_it_lands() -> crate::Result<()> {
    use crate::{UserKey, range_tombstone::RangeTombstone};

    const TARGET: u64 = 32 * 1_024;
    let disjoint = (0..100).map(|i| {
        let prefix = format!("x{i:08}").into_bytes();
        let mut start = prefix.clone();
        start.push(0);
        let mut end = prefix;
        end.push(1);
        RangeTombstone::new(
            UserKey::from(random_bound(2 * i, start)),
            UserKey::from(random_bound(2 * i + 1, end)),
            5,
        )
    });
    let group = (0..200).map(|i| {
        RangeTombstone::new(
            UserKey::from(b"y" as &[u8]),
            UserKey::from(random_bound(1_000 + i, b"y\x01".to_vec())),
            5,
        )
    });
    let tombstones: Vec<_> = disjoint.chain(group).collect();
    let (folder, tables) = flush_outputs(TARGET, &[b"a", b"b", b"c"], tombstones)?;
    for table in &tables {
        let file_size = std::fs::metadata(folder.path().join(table.id().to_string()))?.len();
        assert!(
            file_size <= TARGET + 4 * 1_024,
            "output of {file_size} bytes with {} tombstones overran the {TARGET}-byte target",
            table.range_tombstones().len(),
        );
    }
    Ok(())
}

/// The check at the last start counts the group starting there by its
/// entries in memory as well as its bytes: a group of short tombstones whose
/// bytes fit but whose entries do not is split from what came before.
#[test]
fn a_flush_counts_the_entries_of_the_last_start_group() -> crate::Result<()> {
    use crate::{UserKey, range_tombstone::RangeTombstone};

    const TARGET: u64 = 32 * 1_024;
    // Sized by an entry: the group alone fits the target, not with the rest.
    let entry = core::mem::size_of::<RangeTombstone>() as u64;
    let group_len = u16::try_from(TARGET * 7 / 8 / entry).unwrap_or(u16::MAX);
    let disjoint_len = u16::try_from(TARGET / 4 / entry).unwrap_or(u16::MAX);
    let disjoint = (0..disjoint_len).map(|i| {
        let mut start = b"x".to_vec();
        start.extend_from_slice(&i.to_be_bytes());
        let mut end = start.clone();
        end.push(0);
        RangeTombstone::new(UserKey::from(start), UserKey::from(end), 5)
    });
    let group = (0..group_len).map(|i| {
        let mut end = b"y\x01".to_vec();
        end.extend_from_slice(&i.to_be_bytes());
        RangeTombstone::new(UserKey::from(b"y" as &[u8]), UserKey::from(end), 5)
    });
    let tombstones: Vec<_> = disjoint.chain(group).collect();
    let (_folder, tables) = flush_outputs(TARGET, &[b"a", b"b", b"c"], tombstones)?;
    for table in &tables {
        let held = table.range_tombstones().len() as u64 * entry;
        assert!(
            held <= TARGET,
            "{} tombstone entries hold {held} bytes against the {TARGET}-byte target",
            table.range_tombstones().len(),
        );
    }
    Ok(())
}

/// Tombstones overlapping one another cannot be split below their overlap:
/// every output from their common span holds a piece of each. An output is
/// not rotated when what it would carry into the next fills half the target,
/// so such a set lands in one output instead of in one per key.
#[test]
fn an_overlapping_set_past_the_target_is_not_carried_from_output_to_output() -> crate::Result<()> {
    use crate::{UserKey, range_tombstone::RangeTombstone};

    const TOMBSTONES: usize = 3_000;
    let tombstones = (0..TOMBSTONES)
        .map(|i| {
            RangeTombstone::new(
                UserKey::from(b"a" as &[u8]),
                UserKey::from(random_bound(i, b"z".to_vec())),
                5,
            )
        })
        .collect();
    let keys: Vec<Vec<u8>> = (0..10).map(|i| format!("k{i}").into_bytes()).collect();
    let keys: Vec<&[u8]> = keys.iter().map(Vec::as_slice).collect();
    let (_folder, tables) = flush_outputs(32 * 1_024, &keys, tombstones)?;
    let pieces: usize = tables.iter().map(|t| t.range_tombstones().len()).sum();
    assert!(
        pieces <= 2 * TOMBSTONES,
        "{pieces} pieces over {} outputs for {TOMBSTONES} tombstones",
        tables.len(),
    );
    Ok(())
}

/// Short overlapping tombstones fill an output by the entries they hold in
/// memory long before their bytes do; what a rotation would carry is judged
/// the same way, so they are not carried from output to output either.
#[test]
fn short_overlapping_tombstones_are_not_carried_by_their_memory() -> crate::Result<()> {
    use crate::{UserKey, range_tombstone::RangeTombstone};

    const TOMBSTONES: u16 = 800;
    let tombstones = (0..TOMBSTONES)
        .map(|i| {
            let mut end = b"z".to_vec();
            end.extend_from_slice(&i.to_be_bytes());
            RangeTombstone::new(UserKey::from(b"a" as &[u8]), UserKey::from(end), 5)
        })
        .collect();
    let keys: Vec<Vec<u8>> = (0..10).map(|i| format!("k{i}").into_bytes()).collect();
    let keys: Vec<&[u8]> = keys.iter().map(Vec::as_slice).collect();
    let (_folder, tables) = flush_outputs(32 * 1_024, &keys, tombstones)?;
    let pieces: usize = tables.iter().map(|t| t.range_tombstones().len()).sum();
    assert!(
        pieces <= 2 * usize::from(TOMBSTONES),
        "{pieces} pieces over {} outputs for {TOMBSTONES} tombstones",
        tables.len(),
    );
    Ok(())
}

/// Each tombstone piece holds its bounds in memory besides its entry, and
/// the block buffer holds them again: an output of long-bounded tombstones
/// is full by both copies, not by its encoded bytes alone.
#[test]
fn a_flush_output_holds_its_tombstone_bounds_within_the_target() -> crate::Result<()> {
    use crate::{UserKey, range_tombstone::RangeTombstone};

    const TARGET: u64 = 64 * 1_024;
    const KEYS: usize = 400;
    let key = |i: usize| format!("{i:05}").into_bytes();
    let tombstones = (0..KEYS)
        .map(|i| {
            let mut start = key(i);
            start.push(0);
            let mut end = key(i);
            end.push(1);
            RangeTombstone::new(
                UserKey::from(random_bound_of(2 * i, start, 500)),
                UserKey::from(random_bound_of(2 * i + 1, end, 500)),
                5,
            )
        })
        .collect();
    let keys: Vec<Vec<u8>> = (0..KEYS).map(key).collect();
    let keys: Vec<&[u8]> = keys.iter().map(Vec::as_slice).collect();
    let (_folder, tables) = flush_outputs(TARGET, &keys, tombstones)?;
    let entry = core::mem::size_of::<RangeTombstone>() as u64;
    for table in &tables {
        // Entries, their bounds, and the block they are encoded into.
        let held: u64 = table
            .range_tombstones()
            .iter()
            .map(|rt| {
                let bounds = (rt.start.len() + rt.end.len()) as u64;
                entry + bounds + 12 + bounds
            })
            .sum();
        assert!(
            held <= TARGET + 2 * 1_024,
            "{} tombstones hold {held} bytes against the {TARGET}-byte target",
            table.range_tombstones().len(),
        );
    }
    Ok(())
}

/// An output of many short tombstone pieces holds each as an entry in memory
/// until `finish`, far larger than its encoded bytes: the held state counts
/// the entries, so such an output rotates by its memory near the target.
#[test]
fn a_flush_output_holds_its_tombstone_entries_within_the_target() -> crate::Result<()> {
    use crate::{UserKey, range_tombstone::RangeTombstone};

    const TARGET: u64 = 32 * 1_024;
    const KEYS: usize = 4_000;
    let key = |i: usize| format!("{i:05}").into_bytes();
    // Four short tombstones in the gap after each key.
    let tombstones = (0..KEYS)
        .flat_map(|i| {
            (0..4_u8).map(move |j| {
                let mut start = key(i);
                start.push(2 * j);
                let mut end = key(i);
                end.push(2 * j + 1);
                RangeTombstone::new(UserKey::from(start), UserKey::from(end), 5)
            })
        })
        .collect();
    let keys: Vec<Vec<u8>> = (0..KEYS).map(key).collect();
    let keys: Vec<&[u8]> = keys.iter().map(Vec::as_slice).collect();
    let (_folder, tables) = flush_outputs(TARGET, &keys, tombstones)?;
    let entry = core::mem::size_of::<RangeTombstone>() as u64;
    for table in &tables {
        let held = table.range_tombstones().len() as u64 * entry;
        assert!(
            held <= TARGET,
            "{} tombstone entries hold {held} bytes against the {TARGET}-byte target",
            table.range_tombstones().len(),
        );
    }
    Ok(())
}

/// An output of tombstones alone takes its key range from them in the
/// comparator's order, not in byte order: under a reversed comparator the
/// byte-wise least start and greatest end leave the ends of the range out.
#[test]
fn an_output_of_tombstones_alone_orders_its_range_by_the_comparator() -> crate::Result<()> {
    use crate::{UserKey, fs::StdFs, range_tombstone::RangeTombstone};
    use std::sync::Arc;

    #[derive(Debug)]
    struct ReverseComparator;
    impl crate::comparator::UserComparator for ReverseComparator {
        fn name(&self) -> &'static str {
            "reverse-test"
        }
        fn compare(&self, a: &[u8], b: &[u8]) -> core::cmp::Ordering {
            b.cmp(a)
        }
        fn is_lexicographic(&self) -> bool {
            false
        }
    }

    let folder = tempfile::tempdir()?;
    let base_path = folder.path().to_path_buf();
    let fs: Arc<dyn crate::fs::Fs> = Arc::new(StdFs);
    let comparator: crate::SharedComparator = Arc::new(ReverseComparator);
    let mut mw = super::MultiWriter::new(
        base_path.clone(),
        SequenceNumberCounter::default(),
        u64::MAX,
        1,
        fs,
    )?
    .set_comparator(comparator.clone());
    // In reverse order "z" < "y" < "m" < "a".
    mw.set_range_tombstones(vec![
        RangeTombstone::new(
            UserKey::from(b"z" as &[u8]),
            UserKey::from(b"m" as &[u8]),
            5,
        ),
        RangeTombstone::new(
            UserKey::from(b"y" as &[u8]),
            UserKey::from(b"a" as &[u8]),
            6,
        ),
    ]);
    let results = mw.finish()?;
    let cache = Arc::new(crate::Cache::with_capacity_bytes(64 * 1_024));
    for (table_id, checksum) in &results {
        let table = crate::Table::recover(crate::table::RecoverParams::new(
            base_path.join(table_id.to_string()),
            *checksum,
            *table_id,
            Arc::new(StdFs),
            comparator.clone(),
            cache.clone(),
        ))?;
        assert_eq!(table.metadata.key_range.min().as_ref(), b"z");
        assert_eq!(table.metadata.key_range.max().as_ref(), b"a");
    }
    assert_eq!(results.len(), 1);
    Ok(())
}

/// Each output of a flush visits only the tombstones overlapping its zone, not
/// the whole set: disjoint tombstones spread over many outputs cost a bounded
/// number of comparisons each, however many outputs the flush writes.
#[test]
fn a_flush_cuts_each_zone_without_rescanning_every_tombstone() -> crate::Result<()> {
    use crate::{InternalValue, UserKey, fs::StdFs, range_tombstone::RangeTombstone};
    use std::sync::{
        Arc,
        atomic::{AtomicU64, Ordering},
    };

    #[derive(Debug, Default)]
    struct CountingComparator(AtomicU64);
    impl crate::comparator::UserComparator for CountingComparator {
        fn name(&self) -> &'static str {
            "counting-test"
        }
        fn compare(&self, a: &[u8], b: &[u8]) -> core::cmp::Ordering {
            self.0.fetch_add(1, Ordering::Relaxed);
            a.cmp(b)
        }
    }

    // Comparisons a flush of `n` keys, each followed by a tombstone of its
    // own and each filling an output, makes.
    let comparisons = |n: u32| -> crate::Result<u64> {
        let folder = tempfile::tempdir()?;
        let fs: Arc<dyn crate::fs::Fs> = Arc::new(StdFs);
        let comparator = Arc::new(CountingComparator::default());
        let mut mw = super::MultiWriter::new(
            folder.path().to_path_buf(),
            SequenceNumberCounter::default(),
            1,
            1,
            fs,
        )?
        .set_comparator(comparator.clone());
        let key = |i: u32| format!("k{i:06}").into_bytes();
        mw.set_range_tombstones(
            (0..n)
                .map(|i| {
                    let (mut start, mut end) = (key(i), key(i));
                    start.push(0);
                    end.push(1);
                    RangeTombstone::new(UserKey::from(start), UserKey::from(end), 5)
                })
                .collect(),
        );
        for i in 0..n {
            mw.write(InternalValue::from_components(
                UserKey::from(key(i)),
                vec![0u8; 5_000],
                1,
                crate::ValueType::Value,
            ))?;
        }
        let outputs = mw.finish()?.len();
        assert!(outputs >= n as usize / 2, "{outputs} outputs for {n} keys");
        Ok(comparator.0.load(Ordering::Relaxed))
    };
    let (small, large) = (comparisons(200)?, comparisons(800)?);
    // Four times the keys, tombstones and outputs: a linear cost grows about
    // fourfold, one rescanning every tombstone per output sixteenfold.
    assert!(
        large < 6 * small,
        "{small} comparisons for 200 keys, {large} for 800"
    );
    Ok(())
}

/// Tombstones open at the first key can fill a small target before any record
/// reaches the writer; the writer still takes that record, so no output holds
/// tombstones alone, written whole past the clip, over its neighbours' keys.
#[test]
fn tombstones_open_at_the_first_key_do_not_rotate_an_empty_output() -> crate::Result<()> {
    use crate::{InternalValue, UserKey, fs::StdFs, range_tombstone::RangeTombstone};
    use std::sync::Arc;

    let folder = tempfile::tempdir()?;
    let base_path = folder.path().to_path_buf();
    let fs: Arc<dyn crate::fs::Fs> = Arc::new(StdFs);
    let mut mw = super::MultiWriter::new(
        base_path.clone(),
        SequenceNumberCounter::default(),
        100,
        1,
        fs,
    )?
    .use_clip_range_tombstones();
    mw.set_range_tombstones(
        (0..10u8)
            .map(|i| {
                RangeTombstone::new(
                    UserKey::from(vec![b'a', i]),
                    UserKey::from(b"z" as &[u8]),
                    20,
                )
            })
            .collect(),
    );
    for key in [b"b" as &[u8], b"l", b"q"] {
        mw.write(InternalValue::from_components(
            UserKey::from(key),
            vec![0u8; 4_000],
            1,
            crate::ValueType::Value,
        ))?;
    }
    let tables = recover_outputs(&base_path, &mw.finish()?)?;
    for table in &tables {
        assert!(
            table.metadata.item_count > 0,
            "an output of tombstones alone: {:?}",
            table.metadata.key_range,
        );
    }
    for pair in tables.windows(2) {
        assert!(
            pair[0].metadata.key_range.max() < pair[1].metadata.key_range.min(),
            "clipped outputs must not overlap: {:?} then {:?}",
            pair[0].metadata.key_range,
            pair[1].metadata.key_range,
        );
    }
    Ok(())
}

/// Tombstones starting at a compaction's last key follow no later key whose
/// check would count them: the check at that key counts them, since they go
/// where the key goes, so they do not land on an output already near its
/// target.
#[test]
fn a_compaction_counts_the_tombstones_starting_at_its_last_key() -> crate::Result<()> {
    use crate::{InternalValue, UserKey, fs::StdFs, range_tombstone::RangeTombstone};
    use std::sync::Arc;

    const TARGET: u64 = 32 * 1_024;
    let folder = tempfile::tempdir()?;
    let base_path = folder.path().to_path_buf();
    let fs: Arc<dyn crate::fs::Fs> = Arc::new(StdFs);
    let mut mw = super::MultiWriter::new(
        base_path.clone(),
        SequenceNumberCounter::default(),
        TARGET,
        1,
        fs,
    )?
    .use_clip_range_tombstones();
    // Clipped to the last key, each piece is a short block entry; a thousand
    // of them do not fit beside the keys before.
    mw.set_range_tombstones(
        (0..1_000)
            .map(|i| {
                RangeTombstone::new(
                    UserKey::from(b"m" as &[u8]),
                    UserKey::from(random_bound(i, b"m\x01".to_vec())),
                    20,
                )
            })
            .collect(),
    );
    let keys = (0..70).map(|i| format!("a{i:03}").into_bytes());
    for (i, key) in keys.chain([b"m".to_vec()]).enumerate() {
        mw.write(InternalValue::from_components(
            UserKey::from(key),
            random_bound_of(10_000 + i, Vec::new(), 250),
            1,
            crate::ValueType::Value,
        ))?;
    }
    let tables = recover_outputs(&base_path, &mw.finish()?)?;
    for table in &tables {
        let file_size = std::fs::metadata(base_path.join(table.id().to_string()))?.len();
        assert!(
            file_size <= TARGET + 4 * 1_024,
            "output of {file_size} bytes with {} tombstones overran the {TARGET}-byte target",
            table.range_tombstones().len(),
        );
    }
    Ok(())
}

/// A flush widens an output's key range to the tombstone pieces it writes, and
/// the meta block holds that range twice: an output whose tombstone reaches
/// below its first key with a long start counts the widened meta.
#[test]
fn a_flush_output_counts_its_key_range_widened_by_tombstones() -> crate::Result<()> {
    use crate::{UserKey, range_tombstone::RangeTombstone};

    const TARGET: u64 = 256 * 1_024;
    let tombstone = RangeTombstone::new(
        UserKey::from(random_bound_of(1, b"0".to_vec(), 30_000)),
        UserKey::from(b"a" as &[u8]),
        5,
    );
    let folder = tempfile::tempdir()?;
    let base_path = folder.path().to_path_buf();
    let fs: std::sync::Arc<dyn crate::fs::Fs> = std::sync::Arc::new(crate::fs::StdFs);
    let mut mw = super::MultiWriter::new(
        base_path.clone(),
        SequenceNumberCounter::default(),
        TARGET,
        1,
        fs,
    )?;
    mw.set_range_tombstones(vec![tombstone]);
    for i in 0..2_000 {
        mw.write(crate::InternalValue::from_components(
            UserKey::from(format!("a{i:05}").into_bytes()),
            random_bound_of(10_000 + i, Vec::new(), 250),
            1,
            crate::ValueType::Value,
        ))?;
    }
    let tables = recover_outputs(&base_path, &mw.finish()?)?;
    assert!(tables.len() > 1, "the keys must spread over outputs");
    for table in &tables {
        let file_size = std::fs::metadata(base_path.join(table.id().to_string()))?.len();
        assert!(
            file_size <= TARGET + 8 * 1_024,
            "output of {file_size} bytes spanning {} bytes of keys overran the {TARGET}-byte target",
            table.metadata.key_range.min().len() + table.metadata.key_range.max().len(),
        );
    }
    Ok(())
}

/// A flush output's share of the range tombstones counts toward a full table,
/// so an output carrying many of them still ends near its target.
#[test]
fn a_flush_output_counts_its_tombstones_toward_a_full_table() -> crate::Result<()> {
    use crate::{InternalValue, UserKey, fs::StdFs, range_tombstone::RangeTombstone};
    use std::sync::Arc;

    const TARGET: u64 = 32 * 1_024;
    const KEYS: usize = 2_000;

    let key = |i: usize| format!("{i:08}").into_bytes();
    // Tombstone bounds take pseudo-random tails, so no codec shrinks the
    // tombstone block below the entries it holds.
    let tail = |seed: usize, mut key: Vec<u8>, len: usize| {
        let mut state = (seed as u64 + 1).wrapping_mul(0x9E37_79B9_7F4A_7C15);
        while key.len() < len {
            state ^= state << 13;
            state ^= state >> 7;
            state ^= state << 17;
            key.extend_from_slice(&state.to_le_bytes());
        }
        key
    };
    let folder = tempfile::tempdir()?;
    let base_path = folder.path().to_path_buf();
    let fs: Arc<dyn crate::fs::Fs> = Arc::new(StdFs);
    let mut mw = super::MultiWriter::new(
        base_path.clone(),
        SequenceNumberCounter::default(),
        TARGET,
        1,
        fs,
    )?;
    // One tombstone in the gap after each key: about 140 bytes of block entry
    // against a few dozen bytes of data.
    mw.set_range_tombstones(
        (0..KEYS)
            .map(|i| {
                let mut start = key(i);
                start.push(0);
                let mut end = key(i);
                end.push(1);
                RangeTombstone::new(
                    UserKey::from(tail(2 * i, start, 64)),
                    UserKey::from(tail(2 * i + 1, end, 64)),
                    5,
                )
            })
            .collect(),
    );
    for i in 0..KEYS {
        mw.write(InternalValue::from_components(
            UserKey::from(key(i)),
            vec![0u8; 8],
            1,
            crate::ValueType::Value,
        ))?;
    }
    let tables = recover_outputs(&base_path, &mw.finish()?)?;

    let tombstones: usize = tables.iter().map(|t| t.range_tombstones().len()).sum();
    assert_eq!(tombstones, KEYS, "each tombstone lies in one zone");
    // The forming data block is outside the estimate, and the filter and index
    // are re-estimated every 256 keys, so an output may pass its target by
    // that block and those keys' entries (4 KiB covers 16 bytes a key). Each
    // output here closes on its first block, whose size is the table's data
    // size.
    for table in &tables {
        assert_eq!(table.metadata.data_block_count, 1);
        let file_size = std::fs::metadata(base_path.join(table.id().to_string()))?.len();
        assert!(
            file_size <= TARGET + table.metadata.file_size + 4_096,
            "output of {file_size} bytes with {} tombstones and {} data bytes \
             overran the {TARGET}-byte target",
            table.range_tombstones().len(),
            table.metadata.file_size,
        );
    }
    Ok(())
}