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
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
// SPDX-License-Identifier: Apache-2.0
// Copyright (c) 2024-present, fjall-rs
// Copyright (c) 2026-present, Dmitry Prudnikov
mod cut_alignment;
mod tombstone_share;
pub use cut_alignment::Boundary;
use super::{filter::BloomConstructionPolicy, writer::Writer};
use crate::{
Checksum, CompressionType, TableId, UserKey,
blob_tree::handle::BlobIndirection,
encryption::EncryptionProvider,
fs::{Fs, SyncMode},
prefix::PrefixExtractor,
range_tombstone::RangeTombstone,
table::writer::{LinkedBlobFiles, LinkedFile},
value::InternalValue,
vlog::BlobFileId,
};
use alloc::sync::Arc;
#[cfg(not(feature = "std"))]
use alloc::{string::ToString, vec::Vec};
use crate::path::PathBuf;
/// Like `Writer` but will rotate to a new table, once a table grows larger than `target_size`
///
/// This results in a sorted "run" of tables
#[expect(
clippy::struct_excessive_bools,
reason = "writer config: each bool is an independent feature toggle carried \
across table rotations (partitioned filter, range-tombstone clip, \
seqno-in-index, disable-CoW); enums would obscure the per-feature wiring"
)]
pub struct MultiWriter {
pub(crate) fs: Arc<dyn Fs>,
pub(crate) base_path: PathBuf,
data_block_hash_ratio: f32,
data_block_size: u32,
index_partition_size: u32,
filter_partition_size: u32,
row_group_size: u32,
columnar_page_size: u32,
column_encoding: crate::config::ColumnEncoding,
data_block_restart_interval: u8,
index_block_restart_interval: u8,
use_partitioned_filter: bool,
/// `Some(threshold)` selects the size-adaptive index (single-level
/// until the index exceeds `threshold` bytes, then a streaming
/// partitioned index). `None` leaves the writer's default single-level
/// index. Re-applied to each rotated table writer. Pure
/// always-partition is `Some(0)` (spill on the first entry).
index_spill_threshold: Option<u64>,
/// Target size of tables in bytes
///
/// If a table reaches the target size, a new one is started,
/// resulting in a sorted "run" of tables
pub target_size: u64,
results: Vec<(TableId, Checksum)>,
/// Where each output's file comes from.
files: TableFiles,
/// Whether every output's directory entry was durable before its table
/// was written, so the install that names the outputs syncs no folder for
/// them.
entries_durable: bool,
/// The first output's id when its file was made as this writer started,
/// the id taken from the counter then; `None` when it came from a pool,
/// whose ids were taken when their batch was made.
first_made_now: Option<TableId>,
pub writer: Writer,
pub data_block_compression: CompressionType,
pub index_block_compression: CompressionType,
bloom_policy: BloomConstructionPolicy,
/// Sizes every output's filters against the tree's filter budget.
filter_sizing: Option<crate::filter_budget::FilterPlan>,
current_key: Option<UserKey>,
comparator: crate::SharedComparator,
linked_blobs: LinkedBlobFiles,
/// The blob objects the current table's cell rows own, as `(blob file,
/// offset)`: what dropping the table releases, handed to its writer at
/// rotation for its `owned_blob_objects` section.
owned_objects: Vec<(BlobFileId, u64)>,
/// Range tombstones to distribute across output tables, ordered by start.
/// During compaction these are clipped to each table's key range; during
/// flush each is cut into the zones of the outputs it spans, the first and
/// last zones open-ended so they still cover keys in older SSTs.
range_tombstones: Vec<RangeTombstone>,
/// When true, range tombstones are clipped to each output table's KV key range
/// via `intersect_opt`. This is correct for compaction (input tables are consumed)
/// but wrong for flush (RTs must cover keys in older SSTs outside the memtable's range).
clip_range_tombstones: bool,
/// The current output's first key, the lower bound of its share of the
/// range tombstones; `None` for the first output.
output_lower: Option<UserKey>,
/// The bytes of the current output's share of the range tombstones.
tombstone_share: tombstone_share::TombstoneShare,
/// The current output's held state and tombstone bytes at its first
/// record, the tombstones carried into it included: what every output
/// carries, which closing one does not shed. `None` until the output
/// takes a record.
output_base: Option<(u64, u64)>,
/// Level the tables are written to
initial_level: u8,
prefix_extractor: Option<Arc<dyn PrefixExtractor>>,
encryption: Option<Arc<dyn EncryptionProvider>>,
/// The tree's clock, handed to every successor [`Writer`].
clock: Option<Arc<dyn crate::Clock>>,
/// Resolved Page ECC scheme — preserved here so the rotation path
/// can stamp the same scheme on every successor [`Writer`].
ecc: Option<crate::table::block::EccParams>,
/// `Config::sync_mode` — preserved so every successor [`Writer`]
/// finishes its SST with the same durability level.
sync_mode: SyncMode,
/// `Config::writeback_bytes`, preserved for every successor [`Writer`].
writeback_bytes: u64,
/// Per-KV checksum policy + algorithm (from the runtime
/// `kv_checksums` config) — preserved here so the rotation path
/// stamps the same setting on every successor [`Writer`].
kv_checksum: Option<(
crate::runtime_config::KvChecksumPolicy,
crate::runtime_config::ChecksumAlgorithm,
)>,
/// `seqno_in_index` runtime config — preserved here so the rotation
/// path sets the same flag on every successor [`Writer`], so all SSTs
/// of one flush / compaction uniformly emit (or omit) the `seqno_bounds`
/// section.
use_seqno_in_index: bool,
/// Preserved like `use_seqno_in_index` so every successor [`Writer`] of one
/// flush / compaction uniformly emits (or omits) the `zone_map` section.
use_zone_map: bool,
/// Preserved across writer rotation so every successor [`Writer`] of one
/// flush / compaction compresses under the same strategy.
#[cfg(zstd_any)]
use_zstd_two_pass_seed: bool,
/// Preserved across writer rotation so every successor [`Writer`] of one
/// flush / compaction uniformly writes columnar (or row-major) data blocks.
use_columnar: bool,
/// How the current output of a columnar write stores each value: whole
/// after a row, split after an ingested batch, `None` before either. A
/// table records one layout, so a write of the other one rotates first.
value_layout: Option<crate::table::meta::ValueLayout>,
/// Preserved across writer rotation: every table of a tree whose rows may
/// be written as cells splits them into their fields (see
/// [`Writer::use_cell_rows`]).
cell_rows: bool,
/// Preserved across writer rotation so every successor [`Writer`] of one
/// bulk ingest is uniformly flagged bulk-ingested (see
/// [`Writer::use_bulk_ingested`]).
bulk_ingested: bool,
/// L0 recency key stamped on every successor [`Writer`], preserved across
/// rotation (see [`Writer::use_recency`]): a compaction sets its inputs'
/// highest recency so manifest repair can place each output where its
/// content belongs, not where its id falls, and a flush the id it takes
/// when it starts, since a pooled file's id was taken when its batch was
/// made. `None` (an ingest, whose install sets the recency) stamps each
/// output with ITS OWN id.
recency: Option<TableId>,
/// Compaction lineage stamped on this and every rotated successor (see
/// [`Writer::use_lineage`]): the input ids a compaction merges, identical
/// for every output of the run. `None` (flush / ingest) omits the key.
lineage: Option<Vec<TableId>>,
/// The compaction inputs every output takes its age from (see
/// [`Writer::finish_deferring_dir_sync`]); `None` for a flush or an ingest.
inherited_age: Option<super::writer::AgeSweep>,
/// Counter of compaction-filter TRANSFORMATIONS (any non-`Keep` verdict),
/// shared with the filter adapter. An output whose window saw one is not
/// derivable from its inputs, so it is marked transformed before its meta
/// is written (see [`Writer::mark_lineage_transformed`]); untouched
/// outputs of the same run stay plain. `None` — no filter configured.
transform_marker: Option<alloc::sync::Arc<portable_atomic::AtomicU64>>,
/// The marker's value when the CURRENT output started, so each output is
/// judged by the verdicts of its own window alone.
transforms_at_output_start: u64,
/// This writer receives the run's ENTIRE merged stream (a serial
/// compaction), so its final output may carry the `lineage_last` marker.
/// `false` for parallel sub-compactions and tight-space slices: several
/// writers share one lineage there, and a slice's last output closing
/// the run would let a surviving slice supersede inputs whose records
/// live only in a LOST sibling slice.
owns_whole_run: bool,
/// The id of the writer CURRENTLY receiving records, so rotation can
/// stamp the successor's `lineage_prev` adjacency link.
current_writer_id: TableId,
/// The marker's value observed AFTER the last record actually written.
/// Rotation happens BEFORE the triggering record is inserted, so at that
/// moment the live counter already includes verdicts belonging to the NEW
/// output (the triggering record's own, and any removals between the two
/// writes — the size threshold is a property of the finished old output,
/// so every verdict since its last write belongs to the successor). The
/// finishing output is therefore judged by this milestone, never the live
/// counter.
transforms_after_last_write: u64,
/// Delete strategy applied to every successor [`Writer`], preserved across
/// rotation. Under copy-on-write the writers persist no delete-bitmap; under
/// merge-on-read / adaptive a populated bitmap is written.
delete_strategy: crate::config::DeleteStrategy,
/// When `true`, each output SST has per-file copy-on-write cleared at
/// creation (Btrfs `FS_NOCOW_FL`) so write-once SSTs avoid the
/// copy-on-write fragmentation penalty. Preserved across rotations so every successor
/// table in the run is flagged the same way. No-op on non-CoW filesystems.
disable_cow_on_sst: bool,
/// Resolved retrieval-ribbon locator policy entry for this run's level,
/// preserved here so the rotation path stamps the same setting on every
/// successor [`Writer`]. Each table gets its own per-SST locator section
/// (block ordinals reset per table). Defaults to `None` (disabled).
locator_entry: crate::config::LocatorPolicyEntry,
#[cfg(zstd_any)]
zstd_dictionary: Option<Arc<crate::compression::ZstdDictionary>>,
/// Optional parallel block compression, preserved here so every successor
/// [`Writer`] of a rotated run shares the same pool and settings.
#[cfg(feature = "std")]
parallel: Option<crate::table::writer::ParallelCompression>,
/// Where a compaction learns of every table file this writer creates, so
/// it can remove the ones it never installs. `None` outside compaction.
outputs: Option<crate::compaction::output_ledger::OutputLedger>,
/// The boundaries of the level a compaction's outputs are merged into
/// next, which an output prefers to end on. `None` cuts at the target
/// size alone.
alignment: Option<cut_alignment::CutAlignment>,
}
/// What a row a page of a partly carried group re-encodes may add past its key
/// and value: its fixed fields (sequence number, value type, offsets) and its
/// share of the rebuilt page's framing, generously.
#[cfg(feature = "columnar")]
const REBUILT_ROW_OVERHEAD: u64 = 64;
/// A bound on the bytes a partly carried group of `rows` writes, its source
/// group `raw_len` bytes: the copied pages come out of the source, and the
/// pages rebuilt from rows can grow past it (a value a filter replaced, a
/// merged one), so every row counts at its key and value plus
/// [`REBUILT_ROW_OVERHEAD`].
#[cfg(feature = "columnar")]
fn partly_carried_bound(raw_len: usize, rows: &[InternalValue]) -> u64 {
let rebuilt: u64 = rows
.iter()
.map(|row| row.key.user_key.len() as u64 + row.value.len() as u64 + REBUILT_ROW_OVERHEAD)
.sum();
raw_len as u64 + rebuilt
}
/// What the current table weighs, measured once and judged against any size
/// target: by its bytes, counting what `finish` appends, or by the state it
/// holds until `finish`, which is judged against the target whatever size the
/// table is cut at, since that bounds memory.
#[derive(Clone, Copy, Debug)]
struct OutputLoad {
size: u64,
held: u64,
/// The least size target its bytes are judged against; `None` while the
/// table holds no content its bytes could be cut for.
size_floor: Option<u64>,
held_target: u64,
}
impl OutputLoad {
/// Whether the table is full at `size_target`.
fn reaches(self, size_target: u64) -> bool {
self.size_floor
.is_some_and(|floor| self.size >= size_target.max(floor))
|| self.held >= self.held_target
}
}
/// Where a [`MultiWriter`]'s output files come from: its folder's pool, each
/// id above `above` when given.
struct TableFiles {
pools: Arc<super::file_pool::TableFilePools>,
above: Option<TableId>,
/// Whether an output was opened already: only the first refills the
/// pool (see [`super::file_pool::TableFilePools::claim`]).
opened: bool,
}
impl TableFiles {
/// A writer for the next output in `folder`, and whether its file's
/// directory entry is durable already.
fn open(
&mut self,
folder: &crate::path::Path,
level: u8,
fs: &Arc<dyn Fs>,
) -> crate::Result<(Writer, bool)> {
let may_refill = !self.opened;
self.opened = true;
let claimed = self.pools.claim(folder, fs, self.above, may_refill)?;
let durable = claimed.durable_entry;
Ok((Writer::into_claimed(claimed, level, fs.clone())?, durable))
}
}
impl MultiWriter {
/// A `MultiWriter` whose outputs take their ids from `table_id_generator`
/// and their files as they start, with no pool.
#[cfg(test)]
pub fn new(
base_path: PathBuf,
table_id_generator: crate::SequenceNumberCounter,
target_size: u64,
initial_level: u8,
fs: Arc<dyn Fs>,
) -> crate::Result<Self> {
let pools = super::file_pool::TableFilePools::new(table_id_generator, 0, SyncMode::Normal);
Self::from_pool(
base_path,
Arc::new(pools),
None,
target_size,
initial_level,
fs,
)
}
/// Sets up a `MultiWriter` at the given tables folder, its outputs written
/// into files claimed from `pools`, each output's id above `above` when
/// given.
pub(crate) fn from_pool(
base_path: PathBuf,
pools: Arc<super::file_pool::TableFilePools>,
above: Option<TableId>,
target_size: u64,
initial_level: u8,
fs: Arc<dyn Fs>,
) -> crate::Result<Self> {
let mut files = TableFiles {
pools,
above,
opened: false,
};
// Own-id recency until `use_recency` overrides it (see the `recency`
// field).
let (writer, entries_durable) = files.open(&base_path, initial_level, &fs)?;
let current_table_id = writer.table_id;
Ok(Self {
fs,
initial_level,
base_path,
data_block_hash_ratio: 0.0,
data_block_size: 4_096,
index_partition_size: 4_096,
filter_partition_size: 4_096,
row_group_size: crate::config::DEFAULT_COLUMNAR_ROW_GROUP_SIZE,
columnar_page_size: crate::config::DEFAULT_COLUMNAR_PAGE_SIZE,
column_encoding: crate::config::ColumnEncoding::Plain,
data_block_restart_interval: 16,
index_block_restart_interval: 1,
target_size,
results: Vec::new(),
files,
entries_durable,
first_made_now: (!entries_durable).then_some(current_table_id),
writer,
data_block_compression: CompressionType::None,
index_block_compression: CompressionType::None,
use_partitioned_filter: false,
index_spill_threshold: None,
bloom_policy: BloomConstructionPolicy::default(),
filter_sizing: None,
current_key: None,
comparator: crate::comparator::default_comparator(),
linked_blobs: LinkedBlobFiles::default(),
owned_objects: Vec::new(),
range_tombstones: Vec::new(),
clip_range_tombstones: false,
output_lower: None,
tombstone_share: tombstone_share::TombstoneShare::new(),
output_base: None,
prefix_extractor: None,
encryption: None,
clock: None,
ecc: None,
sync_mode: SyncMode::Normal,
writeback_bytes: 0,
kv_checksum: None,
use_seqno_in_index: false,
use_zone_map: false,
#[cfg(zstd_any)]
use_zstd_two_pass_seed: true,
use_columnar: false,
value_layout: None,
cell_rows: false,
bulk_ingested: false,
recency: None,
lineage: None,
inherited_age: None,
transform_marker: None,
transforms_at_output_start: 0,
transforms_after_last_write: 0,
owns_whole_run: false,
current_writer_id: current_table_id,
delete_strategy: crate::config::DeleteStrategy::default(),
disable_cow_on_sst: false,
locator_entry: crate::config::LocatorPolicyEntry::None,
#[cfg(zstd_any)]
zstd_dictionary: None,
#[cfg(feature = "std")]
parallel: None,
outputs: None,
alignment: None,
})
}
/// Prefers to end outputs on `boundaries`, the boundaries between the
/// tables of the level the outputs are merged into next, sorted by the
/// writer's comparator (see `cut_alignment`); `upper`, when known, is the
/// last key the writer can be handed. An empty list cuts at the target size
/// alone.
#[must_use]
pub(crate) fn use_cut_alignment(
mut self,
boundaries: Arc<[Boundary]>,
upper: Option<UserKey>,
) -> Self {
self.alignment =
(!boundaries.is_empty()).then(|| cut_alignment::CutAlignment::new(boundaries, upper));
self
}
/// Bounds the keys this writer can be handed by `upper`: a writer of one
/// key range of a compaction does not wait for a boundary past it.
pub(crate) fn limit_cut_alignment(&mut self, upper: UserKey) {
if let Some(alignment) = &mut self.alignment {
alignment.limit(upper);
}
}
/// Whether the current output ends before `key`, the next new key, and
/// where `key` falls among the boundaries, which [`Self::commit_cut`] takes
/// once the output it goes into is known. Without boundaries the output
/// ends at the target size.
fn decide_cut(&self, key: &[u8]) -> (bool, Option<cut_alignment::Crossing>) {
self.decide_cut_at(key, self.output_load())
}
/// [`Self::decide_cut`] for an output weighing `load`, measured once for
/// every share of the target the rule compares it with.
fn decide_cut_at(
&self,
key: &[u8],
load: Option<OutputLoad>,
) -> (bool, Option<cut_alignment::Crossing>) {
let Some(alignment) = &self.alignment else {
let full = load.is_some_and(|load| load.reaches(self.target_size));
return (full && self.rotation_sheds(key, (0, 0)), None);
};
let comparator = self.comparator.as_ref();
let mut crossing = alignment.crossing(key, comparator);
// An output with no record yet is below half of any target.
let Some(load) = load else {
crossing.counted = 0;
return (false, Some(crossing));
};
let full = |percent| load.reaches(cut_alignment::share_of(self.target_size, percent));
let past_half = full(50);
// A boundary passed below half the target is no candidate, so it does
// not raise the share the output is cut at later.
if !past_half {
crossing.counted = 0;
}
let cut = past_half
&& (full(200)
|| if crossing.counted > 0 {
full(alignment.floor_percent(crossing))
} else {
!alignment.ahead(crossing, comparator) && full(100)
});
(cut && self.rotation_sheds(key, (0, 0)), Some(crossing))
}
/// Takes the key `crossing` was decided at into the output: a new one when
/// `cut`.
fn commit_cut(&mut self, crossing: Option<cut_alignment::Crossing>, cut: bool) {
if let (Some(alignment), Some(crossing)) = (&mut self.alignment, crossing) {
alignment.commit(crossing, cut);
}
}
/// Records the table file already open and every one a rotation creates
/// in `outputs`.
#[must_use]
pub(crate) fn use_output_ledger(
mut self,
outputs: crate::compaction::output_ledger::OutputLedger,
) -> Self {
outputs.record_table(
self.current_writer_id,
self.writer.path.clone(),
self.fs.clone(),
);
self.outputs = Some(outputs);
self
}
/// Enables parallel block compression for this run, shared by every
/// rotated successor table. No-op when `parallel` is `None`.
#[cfg(feature = "std")]
#[must_use]
pub fn use_parallel_compression(
mut self,
parallel: Option<crate::table::writer::ParallelCompression>,
) -> Self {
if let Some(parallel) = parallel {
self.writer = self.writer.use_parallel_compression(parallel.clone());
self.parallel = Some(parallel);
}
self
}
/// Enables RT clipping: each tombstone is intersected with the output
/// Sets the user comparator used for output ordering and RT clipping.
#[must_use]
pub fn set_comparator(mut self, comparator: crate::SharedComparator) -> Self {
self.comparator = comparator;
self
}
/// Enables RT clipping to the output table's responsibility range.
///
/// Clipped RTs may extend beyond the table's own KV key range to cover the
/// gap up to the next output table. Use this for compaction where input
/// tables are consumed; do NOT use for flush where RTs must cover older
/// SSTs.
#[must_use]
pub fn use_clip_range_tombstones(mut self) -> Self {
self.clip_range_tombstones = true;
self
}
/// Sets range tombstones to be distributed across output tables.
pub fn set_range_tombstones(&mut self, mut tombstones: Vec<RangeTombstone>) {
let comparator = self.comparator.as_ref();
tombstones.sort_by(|a, b| comparator.compare(&a.start, &b.start));
self.tombstone_share = tombstone_share::TombstoneShare::new();
self.range_tombstones = tombstones;
}
/// Writes range tombstones to the given writer, respecting the clip mode.
/// `tombstones`, in order by start, are those that can overlap the table's
/// zone (see [`tombstone_share::TombstoneShare::zone`]), so a run of many
/// outputs visits each tombstone in the outputs it spans only.
///
/// - **clip=true** (compaction): intersect each RT with the table's
/// "responsibility range". For intermediate tables (rotation)
/// `clip_upper` is the first key of the *next* output table, so the
/// range extends past the table's last KV key and covers the gap.
/// For the final table `clip_upper` is `None` and we fall back to
/// `upper_bound_exclusive(last_key)`.
/// - **clip=false** (flush): cut each RT to the table's zone, from `lower`
/// (`None` for the first output) to `clip_upper` (`None` for the last),
/// so the outputs together hold every RT once and still cover keys in
/// older SSTs outside this memtable's key range.
fn write_rts_to_writer<'t>(
tombstones: impl IntoIterator<Item = &'t RangeTombstone>,
clip: bool,
writer: &mut Writer,
lower: Option<&UserKey>,
clip_upper: Option<&UserKey>,
comparator: &dyn crate::comparator::UserComparator,
) {
if let (Some(first_key), Some(last_key)) =
(writer.meta.first_key.clone(), writer.meta.last_key.clone())
{
if clip {
// Compaction mode: clip RTs to this table's responsibility range.
//
// For intermediate tables (rotation) `clip_upper` is the first key
// of the next output table — the range [first_key, next_key) covers
// the gap between tables so RTs spanning it are preserved.
//
// For the final table `clip_upper` is None and we derive the
// exclusive upper bound from the table's last KV key.
let derived_upper;
let max_exclusive: Option<&[u8]> = if let Some(upper) = clip_upper {
Some(upper.as_ref())
} else {
derived_upper =
crate::range_tombstone::upper_bound_exclusive(last_key.as_ref());
derived_upper.as_deref()
};
if let Some(max_exclusive) = max_exclusive {
for rt in tombstones {
if let Some(clipped) =
rt.intersect_opt_with(first_key.as_ref(), max_exclusive, comparator)
{
// Widen last_key so point reads for keys in the
// gap will consult this table for RT suppression.
//
// Only widen during rotation (clip_upper is Some)
// where we know the exact boundary. For the final
// table (clip_upper is None) widening with the
// derived exclusive bound could overlap a non-
// compacted adjacent table at the same level.
//
// Even during rotation, clipped.end must be
// strictly less than clip_upper (the next table's
// first key) — equality would make key_ranges
// overlap, breaking Run::get_for_key_cmp.
//
// Only last_key needs widening: intersect_opt
// already clamps clipped.start >= first_key.
if let Some(existing) = &mut writer.meta.last_key {
let safe = clip_upper.is_some_and(|upper| {
comparator.compare(&clipped.end, upper.as_ref())
== core::cmp::Ordering::Less
});
if safe
&& comparator.compare(&clipped.end, existing.as_ref())
== core::cmp::Ordering::Greater
{
*existing = clipped.end.clone();
}
}
writer.write_range_tombstone(clipped);
}
}
} else {
// `last_key` is the lexicographically maximal encodable user
// key, so there is no strict successor. In that case clip
// only on the lower bound and keep the persisted key_range
// unchanged; widening it during compaction would break the
// disjoint-run invariant that point reads rely on.
for rt in tombstones {
let clipped_start = if comparator.compare(&rt.start, first_key.as_ref())
== core::cmp::Ordering::Greater
{
rt.start.as_ref()
} else {
first_key.as_ref()
};
if comparator.compare(clipped_start, &rt.end) == core::cmp::Ordering::Less {
writer.write_range_tombstone(RangeTombstone::new(
UserKey::from(clipped_start),
rt.end.clone(),
rt.seqno,
));
}
}
}
} else {
Self::write_zone_cut(tombstones, writer, lower, clip_upper, comparator);
}
} else {
// An output of tombstones alone takes its key range from them, in
// the comparator's order: a compaction with no KV items at all
// writes them whole, a flush cuts them to the output's zone.
let coverage = if clip {
let mut coverage = None;
for rt in tombstones {
Self::widen_coverage(&mut coverage, &rt.start, &rt.end, comparator);
writer.write_range_tombstone(rt.clone());
}
coverage
} else {
Self::write_zone_cut(tombstones, writer, lower, clip_upper, comparator)
};
if let Some((start, end)) = coverage {
writer.cover_range_tombstones(start, end);
}
}
}
/// Widens `coverage` to hold `start..end` under `comparator`. The pieces
/// arrive in the order of their starts, so the first start is the least.
fn widen_coverage(
coverage: &mut Option<(UserKey, UserKey)>,
start: &UserKey,
end: &UserKey,
comparator: &dyn crate::comparator::UserComparator,
) {
use core::cmp::Ordering;
match coverage {
None => *coverage = Some((start.clone(), end.clone())),
Some((least, greatest)) => {
debug_assert_ne!(comparator.compare(start, least), Ordering::Less);
if comparator.compare(end, greatest) == Ordering::Greater {
*greatest = end.clone();
}
}
}
}
/// Flush mode: the outputs split the whole key space, so each tombstone is
/// written once, cut into the zones it spans, from `lower` (`None` for the
/// first output) to `upper` (`None` for the last). The first output's zone
/// opens below its first key and the last one's above its last key, so a
/// tombstone reaching past this memtable's keys still covers them in older
/// tables.
///
/// The key range widens to each piece written, so a point read for a key
/// under it, in older tables or in the gap before the next output, consults
/// this table. Flush outputs are separate L0 runs, which may overlap, so the
/// widening may reach the next output's first key. Using the exclusive end
/// as an inclusive upper bound over-approximates but does not lose entries.
///
/// Returns the coverage of the pieces written, under `comparator`.
fn write_zone_cut<'t>(
tombstones: impl IntoIterator<Item = &'t RangeTombstone>,
writer: &mut Writer,
lower: Option<&UserKey>,
upper: Option<&UserKey>,
comparator: &dyn crate::comparator::UserComparator,
) -> Option<(UserKey, UserKey)> {
use core::cmp::Ordering;
let mut coverage = None;
for rt in tombstones {
let start = match lower {
Some(lower) if comparator.compare(&rt.start, lower) == Ordering::Less => lower,
_ => &rt.start,
};
let end = match upper {
Some(upper) if comparator.compare(&rt.end, upper) == Ordering::Greater => upper,
_ => &rt.end,
};
if comparator.compare(start, end) != Ordering::Less {
continue;
}
if let Some(existing) = &mut writer.meta.first_key
&& comparator.compare(start, existing.as_ref()) == Ordering::Less
{
*existing = start.clone();
}
if let Some(existing) = &mut writer.meta.last_key
&& comparator.compare(end, existing.as_ref()) == Ordering::Greater
{
*existing = end.clone();
}
Self::widen_coverage(&mut coverage, start, end, comparator);
writer.write_range_tombstone(RangeTombstone::new(start.clone(), end.clone(), rt.seqno));
}
coverage
}
/// Records that the entry just written points into `indirection`'s blob
/// file. Called after [`Self::write`] of that entry: rotation happens
/// before a write, so the entry's key is the current key and belongs to
/// the current table, and keys arrive in order, so the first key seen for
/// a blob file is its first and the latest its last.
///
/// # Panics
///
/// In debug builds, if no entry has been written yet.
pub fn register_blob(&mut self, indirection: BlobIndirection) {
debug_assert!(
self.current_key.is_some(),
"a blob is registered after the entry that points into it"
);
let Some(key) = self.current_key.as_ref() else {
return;
};
self.linked_blobs.register(
indirection.vhandle.blob_file_id,
u64::from(indirection.size),
u64::from(indirection.vhandle.on_disk_size),
key,
true,
);
}
/// Records the blob references of `rows`, written into the current table
/// not one by one but as a copied row group, each under its own row's key,
/// as [`Self::register_blob`] and [`Self::register_cell_row`] record a
/// written row's under the current key.
///
/// # Errors
///
/// Returns an error if a row tagged as an indirection or a cell row does
/// not decode as one.
#[cfg(feature = "columnar")]
pub(crate) fn register_carried_rows(&mut self, rows: &[InternalValue]) -> crate::Result<()> {
use crate::coding::Decode;
for row in rows {
let key = &row.key.user_key;
if row.key.value_type.is_cell_row() {
for (indirection, owned) in crate::blob_tree::field_row::row_refs(&row.value)? {
self.linked_blobs.register(
indirection.vhandle.blob_file_id,
u64::from(indirection.size),
u64::from(indirection.vhandle.on_disk_size),
key,
owned,
);
if owned {
self.owned_objects
.push((indirection.vhandle.blob_file_id, indirection.vhandle.offset));
}
}
} else if row.key.value_type.is_indirection() {
let indirection = BlobIndirection::decode_from(&mut &row.value[..])?;
self.linked_blobs.register(
indirection.vhandle.blob_file_id,
u64::from(indirection.size),
u64::from(indirection.vhandle.on_disk_size),
key,
true,
);
}
}
Ok(())
}
/// Records the blob files the cell row just written references, as
/// [`Self::register_blob`] does for an indirection: each owned reference
/// adds to its file's counts, each borrowed one only links the file.
///
/// # Errors
///
/// Returns an error if `row` is not a well-formed cell row.
pub fn register_cell_row(&mut self, row: &[u8]) -> crate::Result<()> {
debug_assert!(
self.current_key.is_some(),
"a cell row is registered after it is written"
);
let Some(key) = self.current_key.as_ref() else {
return Ok(());
};
for (indirection, owned) in crate::blob_tree::field_row::row_refs(row)? {
self.linked_blobs.register(
indirection.vhandle.blob_file_id,
u64::from(indirection.size),
u64::from(indirection.vhandle.on_disk_size),
key,
owned,
);
if owned {
self.owned_objects
.push((indirection.vhandle.blob_file_id, indirection.vhandle.offset));
}
}
Ok(())
}
#[must_use]
pub fn use_adaptive_index(mut self, spill_threshold: u64) -> Self {
self.index_spill_threshold = Some(spill_threshold);
self.writer = self.writer.use_adaptive_index(spill_threshold);
self
}
#[must_use]
pub fn use_partitioned_filter(mut self) -> Self {
self.use_partitioned_filter = true;
self.writer = self.writer.use_partitioned_filter();
self
}
#[must_use]
pub fn use_data_block_restart_interval(mut self, interval: u8) -> Self {
self.data_block_restart_interval = interval;
self.writer = self.writer.use_data_block_restart_interval(interval);
self
}
#[must_use]
pub fn use_index_block_restart_interval(mut self, interval: u8) -> Self {
self.index_block_restart_interval = interval;
self.writer = self.writer.use_index_block_restart_interval(interval);
self
}
#[must_use]
pub fn use_data_block_hash_ratio(mut self, ratio: f32) -> Self {
self.data_block_hash_ratio = ratio;
self.writer = self.writer.use_data_block_hash_ratio(ratio);
self
}
#[must_use]
pub(crate) fn use_data_block_size(mut self, size: u32) -> Self {
assert!(
size <= crate::config::MAX_BLOCK_SIZE,
"data block size must be <= 4 MiB",
);
self.data_block_size = size;
self.writer = self.writer.use_data_block_size(size);
self
}
/// Sets the size a partitioned block index cuts its partitions at; see
/// [`Writer::use_index_partition_size`].
#[must_use]
pub(crate) fn use_index_partition_size(mut self, size: u32) -> Self {
self.index_partition_size = size;
self.writer = self.writer.use_index_partition_size(size);
self
}
/// Sets the size a partitioned filter cuts its partitions at; see
/// [`Writer::use_filter_partition_size`].
#[must_use]
pub(crate) fn use_filter_partition_size(mut self, size: u32) -> Self {
self.filter_partition_size = size;
self.writer = self.writer.use_filter_partition_size(size);
self
}
/// Sets the size a columnar table's row groups are cut at; see
/// [`Writer::use_row_group_size`].
#[must_use]
pub(crate) fn use_row_group_size(mut self, size: u32) -> Self {
self.row_group_size = size;
self.writer = self.writer.use_row_group_size(size);
self
}
/// Sets the size a columnar row group's rows are cut into row pages at;
/// see [`Writer::use_columnar_page_size`].
#[must_use]
pub(crate) fn use_columnar_page_size(mut self, size: u32) -> Self {
self.columnar_page_size = size;
self.writer = self.writer.use_columnar_page_size(size);
self
}
/// Sets how the tables' column pages store their values.
#[must_use]
pub(crate) fn use_column_encoding(mut self, encoding: crate::config::ColumnEncoding) -> Self {
self.column_encoding = encoding;
self.writer = self.writer.use_column_encoding(encoding);
self
}
#[must_use]
pub fn use_data_block_compression(mut self, compression: CompressionType) -> Self {
self.data_block_compression = compression;
self.writer = self.writer.use_data_block_compression(compression);
self
}
#[must_use]
pub fn use_index_block_compression(mut self, compression: CompressionType) -> Self {
self.index_block_compression = compression;
self.writer = self.writer.use_index_block_compression(compression);
self
}
#[must_use]
pub fn use_bloom_policy(mut self, bloom_policy: BloomConstructionPolicy) -> Self {
self.bloom_policy = bloom_policy;
self.writer = self.writer.use_bloom_policy(bloom_policy);
self
}
/// Sizes every output's filters by probe load against the tree's filter
/// budget; `None` builds them at the bloom policy.
#[must_use]
pub(crate) fn use_filter_sizing(
mut self,
sizing: Option<crate::filter_budget::FilterPlan>,
) -> Self {
self.filter_sizing.clone_from(&sizing);
self.writer = self.writer.use_filter_sizing(sizing);
self
}
/// The filter plan this writer sizes its outputs by, for its operation to
/// hold until the outputs are installed.
pub(crate) fn filter_sizing(&self) -> Option<crate::filter_budget::FilterPlan> {
self.filter_sizing.clone()
}
#[must_use]
pub fn use_prefix_extractor(mut self, extractor: Option<Arc<dyn PrefixExtractor>>) -> Self {
self.prefix_extractor.clone_from(&extractor);
self.writer = self.writer.use_prefix_extractor(extractor);
self
}
#[must_use]
pub fn use_encryption(mut self, encryption: Option<Arc<dyn EncryptionProvider>>) -> Self {
self.encryption.clone_from(&encryption);
self.writer = self.writer.use_encryption(encryption);
self
}
/// Reads the time on the tree's `clock` in every table written (see
/// [`Writer::use_clock`]).
#[must_use]
pub(crate) fn use_clock(mut self, clock: Option<Arc<dyn crate::Clock>>) -> Self {
self.clock.clone_from(&clock);
self.writer = self.writer.use_clock(clock);
self
}
/// Wires the tree's `Config::page_ecc` flag through to the
/// inner [`Writer`] and preserves it across rotations so every
/// successor writer stamps the same setting on its blocks.
#[must_use]
pub fn use_ecc(mut self, ecc: Option<crate::table::block::EccParams>) -> Self {
self.ecc = ecc;
self.writer = self.writer.use_ecc(ecc);
self
}
/// Convenience: resolves `(page_ecc, EccScheme)` into the per-block
/// scheme and applies it via [`Self::use_ecc`]. Mirrors
/// [`crate::table::writer::Writer::use_page_ecc`].
#[must_use]
pub fn use_page_ecc(self, page_ecc: bool, scheme: crate::runtime_config::EccScheme) -> Self {
self.use_ecc(crate::table::writer::resolve_ecc(page_ecc, scheme))
}
/// Wires the tree's `Config::sync_mode` through to the inner [`Writer`]
/// and preserves it across rotations so every successor SST is finished
/// at the same durability level.
#[must_use]
pub fn use_sync_mode(mut self, sync_mode: SyncMode) -> Self {
self.sync_mode = sync_mode;
self.writer = self.writer.use_sync_mode(sync_mode);
self
}
/// Wires the tree's `Config::writeback_bytes` through to the inner
/// [`Writer`] and every successor.
#[must_use]
pub fn use_writeback_bytes(mut self, bytes: u64) -> Self {
self.writeback_bytes = bytes;
self.writer = self.writer.use_writeback_bytes(bytes);
self
}
/// Wires the runtime `kv_checksums` policy + algorithm through to the
/// inner [`Writer`] and preserves it across rotations so every
/// successor writer applies the same per-KV checksum setting. `Off`
/// emits no per-KV footer and leaves the `KV_CHECKSUM_FOOTER` flag clear
/// (the data-block payload encoding is unchanged; the V5 header/meta
/// layout still differs from pre-V5 regardless).
#[must_use]
pub fn use_kv_checksums(
mut self,
policy: crate::runtime_config::KvChecksumPolicy,
algo: crate::runtime_config::ChecksumAlgorithm,
) -> Self {
self.kv_checksum = if matches!(policy, crate::runtime_config::KvChecksumPolicy::Off) {
None
} else {
Some((policy, algo))
};
self.writer = self.writer.use_kv_checksums(policy, algo);
self
}
/// Wires the `seqno_in_index` runtime config through to the inner
/// [`Writer`] and preserves it across rotations so every successor
/// writer emits a matching `seqno_bounds` section.
#[must_use]
pub fn use_seqno_in_index(mut self, seqno_in_index: bool) -> Self {
self.use_seqno_in_index = seqno_in_index;
self.writer = self.writer.use_seqno_in_index(seqno_in_index);
self
}
/// Wires the `zstd_two_pass_seed` runtime config through to the inner
/// [`Writer`] and preserves it across rotations, so every SST of one flush
/// or compaction is written under the same strategy.
#[cfg(zstd_any)]
#[must_use]
pub fn use_zstd_two_pass_seed(mut self, enabled: bool) -> Self {
self.use_zstd_two_pass_seed = enabled;
self.writer = self.writer.use_zstd_two_pass_seed(enabled);
self
}
/// Enables the `zone_map` section on this writer and every successor it
/// rotates to, so all SSTs of one flush / compaction emit it.
#[must_use]
pub fn use_zone_map(mut self, zone_map: bool) -> Self {
self.use_zone_map = zone_map;
self.writer = self.writer.use_zone_map(zone_map);
self
}
/// Whether the tables this writes keep a zone map, so a group copied into
/// them needs its per-column statistics.
#[cfg(feature = "columnar")]
pub(crate) const fn keeps_zone_map(&self) -> bool {
self.use_zone_map
}
/// The bytes of rows a columnar group written here is closed at.
#[cfg(feature = "columnar")]
pub(crate) const fn row_group_size(&self) -> u32 {
self.row_group_size
}
#[must_use]
pub fn use_columnar(mut self, columnar: bool) -> Self {
self.use_columnar = columnar;
self.writer = self.writer.use_columnar(columnar);
self
}
/// Splits the cell rows of every table of this run into their fields
/// (see [`Writer::use_cell_rows`]), re-applied to each rotated successor.
#[must_use]
pub(crate) fn use_cell_rows(mut self, cell_rows: bool) -> Self {
self.cell_rows = cell_rows;
self.writer = self.writer.use_cell_rows(cell_rows);
self
}
/// Marks every table in this run as bulk-ingested (re-applied to each rotated
/// successor), so manifest repair can recognize their manifest-only
/// `global_seqno` dependence. See [`Writer::use_bulk_ingested`].
#[must_use]
pub(crate) fn use_bulk_ingested(mut self, bulk_ingested: bool) -> Self {
self.bulk_ingested = bulk_ingested;
// A multi-writer only produces flush / compaction / ingest output, whose
// provenance is always KNOWN — never the unknown (`None`) case that only
// salvage's mirror path yields.
self.writer = self.writer.use_bulk_ingested(Some(bulk_ingested));
self
}
/// The first output's id when its file was made as this writer started:
/// taken from the tree's counter then, it stands for when the write
/// began. `None` for a pooled file.
pub(crate) fn first_made_now(&self) -> Option<TableId> {
self.first_made_now
}
/// Stamps a FIXED L0 recency key on this and every rotated successor
/// writer (see [`Writer::use_recency`]). `None` keeps the default own-id
/// stamping (see the `recency` field): the current writer already carries
/// its own id, and each successor stamps its own.
#[must_use]
pub(crate) fn use_recency(mut self, recency: Option<TableId>) -> Self {
self.recency = recency;
if let Some(recency) = recency {
self.writer = self.writer.use_recency(recency);
}
self
}
/// Stamps the compaction lineage on this and every rotated successor
/// writer (see [`Writer::use_lineage`]).
#[must_use]
pub(crate) fn use_lineage(mut self, lineage: Option<Vec<TableId>>) -> Self {
self.lineage.clone_from(&lineage);
self.writer = self.writer.use_lineage(lineage);
self
}
/// Lets this and every rotated successor writer take its age from the
/// compaction `inputs` its key range meets (see
/// [`Writer::finish_deferring_dir_sync`]), swept as this chain's outputs
/// advance.
#[must_use]
pub(crate) fn use_inherited_age(mut self, inputs: super::writer::AgeSweep) -> Self {
self.inherited_age = Some(inputs);
self
}
/// Declares that this writer receives the run's ENTIRE merged stream
/// (see the `owns_whole_run` field), allowing its final output to carry
/// the `lineage_last` marker. Never set on a parallel sub-compaction or
/// tight-space slice.
#[must_use]
pub(crate) fn use_lineage_whole_run(mut self, whole_run: bool) -> Self {
self.owns_whole_run = whole_run;
self
}
/// Wires the compaction-filter transform counter (see the
/// `transform_marker` field). Call before writing starts.
#[must_use]
pub(crate) fn use_transform_marker(
mut self,
marker: alloc::sync::Arc<portable_atomic::AtomicU64>,
) -> Self {
let now = marker.load(core::sync::atomic::Ordering::Relaxed);
self.transforms_at_output_start = now;
self.transforms_after_last_write = now;
self.transform_marker = Some(marker);
self
}
/// The transform counter's value now; without a counter, the milestone
/// as it stands, so settling it changes nothing.
#[cfg(feature = "columnar")]
pub(crate) fn transforms_seen(&self) -> u64 {
self.transform_marker
.as_ref()
.map_or(self.transforms_after_last_write, |marker| {
marker.load(core::sync::atomic::Ordering::Relaxed)
})
}
/// Takes `seen`, the counter's value when the record just put in the
/// current output was emitted, as the after-last-write milestone, in
/// place of the value read as it was written: a record written after the
/// merge held it would otherwise take the verdicts ticked meanwhile,
/// which belong to the next output (see `transforms_after_last_write`).
#[cfg(feature = "columnar")]
pub(crate) fn settle_transforms(&mut self, seen: u64) {
self.transforms_after_last_write = seen;
}
/// Sets the delete strategy for this and every rotated successor writer.
#[must_use]
pub fn delete_strategy(mut self, strategy: crate::config::DeleteStrategy) -> Self {
self.delete_strategy = strategy;
self.writer = self.writer.delete_strategy(strategy);
self
}
/// Wires the resolved retrieval-ribbon locator policy entry through to the
/// inner [`Writer`] and preserves it across rotations, so every successor
/// table in the run emits its own per-SST locator section (or none, when
/// disabled). Block ordinals reset per table.
#[must_use]
pub fn use_locator(mut self, entry: crate::config::LocatorPolicyEntry) -> Self {
self.locator_entry = entry;
self.writer = self.writer.use_locator(entry);
self
}
/// Wires the `disable_cow_on_sst_files` runtime config through to the inner
/// [`Writer`] (clearing per-file copy-on-write on the current output file) and
/// preserves it across rotations so every successor SST is flagged the same
/// way. A no-op on non-CoW filesystems.
#[must_use]
pub fn use_disable_cow_on_sst(mut self, disable_cow: bool) -> Self {
self.disable_cow_on_sst = disable_cow;
self.writer = self.writer.use_disable_cow(disable_cow);
self
}
#[cfg(zstd_any)]
#[must_use]
pub fn use_zstd_dictionary(
mut self,
dictionary: Option<Arc<crate::compression::ZstdDictionary>>,
) -> Self {
self.zstd_dictionary.clone_from(&dictionary);
self.writer = self.writer.use_zstd_dictionary(dictionary);
self
}
/// Flushes the current writer, stores its metadata, and sets up a new writer for the next table
fn rotate(&mut self) -> crate::Result<()> {
log::debug!("Rotating table writer");
self.output_base = None;
self.value_layout = None;
let (new_writer, durable) =
self.files
.open(&self.base_path, self.initial_level, &self.fs)?;
self.entries_durable &= durable;
let new_table_id = new_writer.table_id;
// Recorded once it exists: a failed constructor created nothing ours.
if let Some(outputs) = &self.outputs {
outputs.record_table(new_table_id, new_writer.path.clone(), self.fs.clone());
}
let mut new_writer = new_writer
.use_data_block_compression(self.data_block_compression)
.use_index_block_compression(self.index_block_compression)
.use_data_block_size(self.data_block_size)
.use_index_partition_size(self.index_partition_size)
.use_filter_partition_size(self.filter_partition_size)
.use_row_group_size(self.row_group_size)
.use_columnar_page_size(self.columnar_page_size)
.use_column_encoding(self.column_encoding)
.use_data_block_restart_interval(self.data_block_restart_interval)
.use_index_block_restart_interval(self.index_block_restart_interval)
.use_bloom_policy(self.bloom_policy)
.use_filter_sizing(self.filter_sizing.clone())
.use_data_block_hash_ratio(self.data_block_hash_ratio);
if let Some(threshold) = self.index_spill_threshold {
new_writer = new_writer.use_adaptive_index(threshold);
}
if self.use_partitioned_filter {
new_writer = new_writer.use_partitioned_filter();
}
new_writer = new_writer.use_prefix_extractor(self.prefix_extractor.clone());
new_writer = new_writer.use_encryption(self.encryption.clone());
new_writer = new_writer.use_clock(self.clock.clone());
new_writer = new_writer.use_ecc(self.ecc);
new_writer = new_writer
.use_sync_mode(self.sync_mode)
.use_writeback_bytes(self.writeback_bytes);
if let Some((policy, algo)) = self.kv_checksum {
new_writer = new_writer.use_kv_checksums(policy, algo);
}
new_writer = new_writer.use_seqno_in_index(self.use_seqno_in_index);
#[cfg(zstd_any)]
{
new_writer = new_writer.use_zstd_two_pass_seed(self.use_zstd_two_pass_seed);
}
new_writer = new_writer.use_zone_map(self.use_zone_map);
new_writer = new_writer.use_columnar(self.use_columnar);
new_writer = new_writer.use_cell_rows(self.cell_rows);
new_writer = new_writer.use_bulk_ingested(Some(self.bulk_ingested));
if let Some(recency) = self.recency {
new_writer = new_writer.use_recency(recency);
}
new_writer = new_writer.use_lineage(self.lineage.clone());
// The adjacency link: this successor follows the writer being closed,
// which is what lets manifest repair union UNBROKEN sibling chains.
new_writer = new_writer.use_lineage_prev(Some(self.current_writer_id));
self.current_writer_id = new_table_id;
new_writer = new_writer.delete_strategy(self.delete_strategy);
new_writer = new_writer.use_disable_cow(self.disable_cow_on_sst);
new_writer = new_writer.use_locator(self.locator_entry);
#[cfg(zstd_any)]
{
new_writer = new_writer.use_zstd_dictionary(self.zstd_dictionary.clone());
}
#[cfg(feature = "std")]
if let Some(parallel) = self.parallel.clone() {
new_writer = new_writer.use_parallel_compression(parallel);
}
let mut old_writer = core::mem::replace(&mut self.writer, new_writer);
// The finishing output's meta is about to be written: if a compaction
// filter transformed a record within ITS window, the output is not
// derivable from its inputs — mark the lineage transformed before it
// lands, so manifest repair supersedes the inputs it covers instead
// of trading it back for them. Judged by the AFTER-LAST-WRITE
// milestone, not the live counter: rotation runs before the
// triggering record is inserted, so verdicts ticked since the old
// output's last write (the trigger's own, and removals in between)
// belong to the NEW output.
if self.transforms_after_last_write > self.transforms_at_output_start {
old_writer.mark_lineage_transformed();
}
self.transforms_at_output_start = self.transforms_after_last_write;
old_writer.spill_block()?;
// Write range tombstones to the finishing writer, cut to its zone:
// up to current_key, the first key of the NEW table, so the gap
// between tables stays covered. The share has advanced to that key.
if !self.range_tombstones.is_empty() {
Self::write_rts_to_writer(
self.tombstone_share.zone(&self.range_tombstones, false),
self.clip_range_tombstones,
&mut old_writer,
self.output_lower.as_ref(),
self.current_key.as_ref(),
self.comparator.as_ref(),
);
}
self.output_lower.clone_from(&self.current_key);
for linked in self.linked_blobs.take() {
old_writer.link_blob_file(linked);
}
old_writer.own_blob_objects(core::mem::take(&mut self.owned_objects));
// The install that names the tables syncs their folder once.
let path = old_writer.path.clone();
if let Some((table_id, checksum)) =
old_writer.finish_deferring_dir_sync(self.inherited_age.as_mut())?
{
self.results.push((table_id, checksum));
// Before the next output takes more room.
if let Some(outputs) = &self.outputs {
outputs.landed(&*self.fs, &path)?;
}
}
Ok(())
}
/// The current table reached its target: by its bytes, counting what
/// `finish` will append, or by the heap its per-key state holds for
/// `finish`. Rows that compress well reach the second first: their data
/// stays small while the filter, index and locator state grow per key.
#[cfg(any(test, feature = "columnar"))]
fn table_full(&self) -> bool {
self.output_load()
.is_some_and(|load| load.reaches(self.target_size))
}
/// What the current table weighs at the current key, measured once so it
/// can be judged against several size targets; `None` before its first
/// record, which no target counts as full: closing an empty table would
/// write its tombstones alone, unclipped, over its successors' keys.
fn output_load(&self) -> Option<OutputLoad> {
if self.writer.meta.key_count == 0 {
return None;
}
// The blob files this table links, and its share of the range
// tombstones, are handed to its writer only when it rotates, and it
// writes them at `finish`. The tombstone block is encoded into a buffer
// it holds while writing. The tombstones themselves are the caller's
// input and the share tracks only those open at the current key:
// neither grows with the table, and rotating frees neither. The
// tombstones starting at the key go where the key goes, and the last
// key has no successor whose check would count them, so they count here.
let (tombstones, pieces, longest) = self.tombstones_at_key();
Some(self.load_with_tombstones(tombstones, pieces, longest, (0, 0)))
}
/// The current output's share of the range tombstones at the current key:
/// its encoded bytes, its entries and its longest bound, counting the
/// tombstones starting at the key, which go where the key goes.
fn tombstones_at_key(&self) -> (u64, u64, u64) {
match &self.current_key {
Some(key) if !self.range_tombstones.is_empty() => {
let group = self.tombstone_share.group(
&self.range_tombstones,
key,
self.comparator.as_ref(),
);
(
self.tombstone_share.bytes(key) + group.bytes,
self.tombstone_share.pieces() + group.entries,
self.tombstone_share.longest_bound(key).max(group.longest),
)
}
_ => (0, self.tombstone_share.pieces(), 0),
}
}
/// What the current table weighs if it closes holding `pieces`
/// range-tombstone entries of `tombstones` encoded bytes, with bounds of
/// up to `longest` bytes. `alone` is what a table of tombstones alone
/// writes and holds for its synthetic entry, which its writer does not
/// count, and zero for a table with records. The writer holds each entry
/// until `finish`, which encodes them into a block buffer and frames that
/// when the block is transformed. The table's key range widens to the
/// entries' bounds.
fn load_with_tombstones(
&self,
tombstones: u64,
pieces: u64,
longest: u64,
alone: (u64, u64),
) -> OutputLoad {
use crate::table::block::{BlockType, framed_len_bound};
let linked = self.linked_blobs.section_len();
let owned = if self.owned_objects.is_empty() {
0
} else {
framed_len_bound(
crate::table::writer::owned_blob_objects_len(self.owned_objects.len()),
BlockType::OwnedBlobObjects,
CompressionType::None,
self.encryption.as_deref(),
self.ecc,
)
};
let (tombstone_block, tombstones_held) = if tombstones == 0 {
(0, 0)
} else {
let (range_out, range_held) = self.writer.widened_range_growth(longest);
(
framed_len_bound(
tombstones,
BlockType::RangeTombstone,
CompressionType::None,
self.encryption.as_deref(),
self.ecc,
) + range_out,
self.tombstones_held(tombstones, pieces) + range_held,
)
};
// Each linked blob file is an entry in the map here and, at rotation,
// a copy in the writer's list.
let linked_held = self.linked_blobs.len() as u64
* (core::mem::size_of::<(BlobFileId, LinkedFile)>()
+ 1
+ core::mem::size_of::<LinkedFile>()) as u64;
// The owned objects' list moves to the writer at rotation, which sorts
// it in place.
let owned_held =
self.owned_objects.capacity() as u64 * core::mem::size_of::<(BlobFileId, u64)>() as u64;
let (alone_written, alone_held) = alone;
let size_hint = self.writer.output_size_hint();
let writer_held = self.writer.held_state_bytes();
let size = size_hint + alone_written + linked + owned + tombstone_block;
let held = writer_held + alone_held + tombstones_held + linked_held + owned_held;
// Closing a table sheds none of its metadata or of what it held at its
// first record, the tombstones carried into it included, which the
// next table carries alike: a target below that counts only once the
// table holds more. A table with records closes by size once it has
// written data or its tombstones grew by a block, and by state once it
// holds a block's worth past its first record; one of tombstones
// alone, once they take a block past its synthetic table. Otherwise
// every key, or every tombstone, would get a table of its own, none of
// them smaller.
let block = self.writer.block_len();
if alone_written > 0 {
return OutputLoad {
size,
held,
size_floor: Some(alone_written + block),
held_target: self.target_size.max(writer_held + alone_held + block),
};
}
let (base_held, base_tombstones) = self.output_base.unwrap_or((0, 0));
let holds_content = size_hint > self.writer.finish_metadata_bytes()
|| tombstones >= base_tombstones + block;
let held_target = if self.output_base.is_some() {
self.target_size.max(base_held + block)
} else {
self.target_size
};
OutputLoad {
size,
held,
size_floor: holds_content.then_some(0),
held_target,
}
}
/// Closing the current table at `key` sheds what it holds: the next one
/// starts with a piece of every tombstone open at `key`. When those alone
/// fill half the target, closing would carry them from output to output
/// without shedding any, so the table grows past its target instead; a
/// set of tombstones overlapping one another cannot be split below them.
/// What is carried is judged as fullness is: by the bytes it encodes to
/// and by the entries it holds in memory, against the part of the target
/// left past what the next output carries anyway, `next_alone` for one of
/// tombstones alone (see [`Self::load_with_tombstones`]).
fn rotation_sheds(&self, key: &[u8], next_alone: (u64, u64)) -> bool {
let entries = self.tombstone_share.open_count();
if entries == 0 {
return true;
}
let carry = self.tombstone_share.carry(key);
let held = self.tombstones_held(carry, entries);
let block = self.writer.block_len();
let budget = |fixed: u64| {
if fixed == 0 {
self.target_size
} else {
self.target_size.max(fixed + block) - fixed
}
};
carry < budget(next_alone.0) / 2 && held < budget(next_alone.1) / 2
}
/// Heap an output holding `pieces` tombstone entries of `tombstones`
/// encoded bytes takes for them until `finish`: the entries and the
/// bounds they own, which the encoded bytes bound from above, the block
/// buffer they are encoded into, and its frame when the block is
/// transformed.
fn tombstones_held(&self, tombstones: u64, pieces: u64) -> u64 {
use crate::table::block::{BlockType, transform_scratch_bound};
pieces * core::mem::size_of::<RangeTombstone>() as u64
+ 2 * tombstones
+ transform_scratch_bound(
tombstones,
BlockType::RangeTombstone,
CompressionType::None,
self.encryption.as_deref(),
self.ecc,
)
}
/// Writes an item
pub fn write(&mut self, item: InternalValue) -> crate::Result<()> {
// A new user key is a change of byte IDENTITY, never of byte order:
// the stream arrives in comparator order, and a comparator whose order
// is not the byte order (reversed, numeric) makes `<` false for keys
// that are new, which silently disabled rotation. Same-key versions
// stay in one output because identity does not change between them.
let is_next_key = !self
.current_key
.as_ref()
.is_some_and(|c| crate::comparator::same_user_key(c, &item.key.user_key));
if is_next_key {
let first_key = self.current_key.is_none();
self.current_key = Some(item.key.user_key.clone());
if !self.range_tombstones.is_empty() {
self.tombstone_share.advance(
&self.range_tombstones,
&item.key.user_key,
self.comparator.as_ref(),
);
// Clipping bounds the first output's zone by its first key too.
if first_key && self.clip_range_tombstones {
self.tombstone_share.open_output(&item.key.user_key);
}
}
// A table records one value layout, so a row after an ingested
// batch starts the next table.
let layout_changes = self.value_layout == Some(crate::table::meta::ValueLayout::Split);
let (cut, crossing) = self.decide_cut(&item.key.user_key);
if layout_changes || cut {
self.rotate()?;
self.tombstone_share.open_output(&item.key.user_key);
}
self.commit_cut(crossing, layout_changes || cut);
}
self.writer.write(item)?;
if self.use_columnar {
self.value_layout = Some(if self.cell_rows {
crate::table::meta::ValueLayout::Cells
} else {
crate::table::meta::ValueLayout::Whole
});
}
self.note_output_base();
// The transform-attribution milestone: verdicts ticked up to a
// record that actually LANDED belong to the output holding it (see
// the `transforms_after_last_write` field).
if let Some(marker) = &self.transform_marker {
self.transforms_after_last_write = marker.load(core::sync::atomic::Ordering::Relaxed);
}
Ok(())
}
/// Writes `raw`, a row group of another table copied whole, in place of its
/// `rows`, which the compaction would otherwise write one by one; `group`
/// is its index entry there, `layout` how its values are stored and
/// `columns` its zone-map statistics. The group's first key is taken as
/// [`Self::write`] takes a new key, rotating first when the table is full.
///
/// Returns `false` when the group cannot be copied here, having written
/// none of it: one that does not start a new key, or one the current
/// table refuses (see [`Writer::append_carried_row_group`]). The caller
/// then writes `rows` through [`Self::write`].
///
/// # Errors
///
/// Any error of the rotation or of the copy.
#[cfg(feature = "columnar")]
pub(crate) fn carry_row_group(
&mut self,
(raw, source): (&[u8], super::writer::VerbatimSource),
group: super::index_block::RowGroupRef,
(compression, layout): (CompressionType, crate::table::meta::ValueLayout),
rows: &[InternalValue],
columns: Option<Vec<crate::table::zone_map::ColumnStats>>,
) -> crate::Result<bool> {
self.carry_into(
(compression, layout),
(rows, raw.len() as u64),
|writer, comparator| {
writer.append_carried_row_group(
(raw, source),
group,
layout,
rows,
columns,
comparator,
)
},
)
}
/// Writes `rows` as one group that copies the pages `carry` names from
/// another table's group, encoding the rest (see
/// [`Writer::append_partly_carried_row_group`]); `compression` is the
/// source's data codec. The group's first key is taken as [`Self::write`]
/// takes a new key.
///
/// Returns `false`, having written none of it, when the group cannot be
/// written so; the caller then writes `rows` through [`Self::write`].
///
/// # Errors
///
/// Any error of the rotation or of the write.
#[cfg(feature = "columnar")]
pub(crate) fn carry_row_pages(
&mut self,
rows: &[InternalValue],
carry: &super::writer::PageCarry<'_>,
compression: CompressionType,
) -> crate::Result<bool> {
self.carry_into(
(compression, crate::table::meta::ValueLayout::Whole),
(rows, partly_carried_bound(carry.raw.len(), rows)),
|writer, comparator| writer.append_partly_carried_row_group(rows, carry, comparator),
)
}
/// Whether a group about to be copied whole, `bytes` on disk and ending
/// at `last`, is cheaper written key by key so the output can end on the
/// boundary between its keys: the current output, not cut before it, is
/// already past the share of the target it would be cut at there. Copied,
/// the output straddles the boundary and the next merge reads and writes
/// again every table past it that the group reaches, several where runs
/// overlap there; written key by key, the group's bytes are
/// encoded again now. The cheaper of the two is taken.
#[cfg(feature = "columnar")]
fn splits_cheaper_than_straddling(
&self,
load: Option<OutputLoad>,
crossing: Option<cut_alignment::Crossing>,
last: &InternalValue,
bytes: u64,
) -> bool {
let (Some(alignment), Some(crossing), Some(load)) = (&self.alignment, crossing, load)
else {
return false;
};
let Some(boundary) =
alignment.inside(crossing, &last.key.user_key, self.comparator.as_ref())
else {
return false;
};
// One boundary past those `crossing` counts: the one inside. Counts
// are bounded by the boundaries, one per table of a level.
let inside = cut_alignment::Crossing {
passed: crossing.passed + 1,
counted: crossing.counted + 1,
};
bytes < boundary.reached_bytes(&last.key.user_key, self.comparator.as_ref())
&& load.reaches(cut_alignment::share_of(
self.target_size,
alignment.floor_percent(inside),
))
}
/// Whether a group about to be copied whole, `bytes` on disk and ending at
/// `last`, is written key by key instead, given whether the output
/// `rotates` before it: when the copy would take an aligned output past
/// twice the target, where written rows would have ended it, or when
/// splitting it costs less than straddling a boundary inside it.
#[cfg(feature = "columnar")]
fn writes_group_by_rows(
&self,
load: Option<OutputLoad>,
rotates: bool,
crossing: Option<cut_alignment::Crossing>,
last: &InternalValue,
bytes: u64,
) -> bool {
if self.alignment.is_some() {
let ceiling = cut_alignment::share_of(self.target_size, 200);
let past_ceiling = if rotates {
bytes > ceiling
} else {
// A group past the ceiling on its own takes any output with a
// record past it; a smaller one, an output past what it leaves.
load.is_some_and(|load| bytes > ceiling || load.reaches(ceiling - bytes))
};
if past_ceiling {
return true;
}
}
!rotates && self.splits_cheaper_than_straddling(load, crossing, last, bytes)
}
/// Takes `rows`' first key as [`Self::write`] takes a new key and has
/// `write` put the group in the current table, when this run writes the
/// copied pages' transform; `false`, having written nothing, otherwise.
#[cfg(feature = "columnar")]
fn carry_into(
&mut self,
(compression, layout): (CompressionType, crate::table::meta::ValueLayout),
(rows, bytes): (&[InternalValue], u64),
write: impl FnOnce(&mut Writer, &crate::comparator::SharedComparator) -> crate::Result<bool>,
) -> crate::Result<bool> {
let (Some(first), Some(last)) = (rows.first(), rows.last()) else {
return Ok(false);
};
// The copied pages keep the transform they were written under: this
// run's must be the same, so a read of the copy decodes them with the
// codec and dictionary its table names, and an encrypted or parity
// run gets blocks bound to its own identity and scheme.
if self.encryption.is_some()
|| self.ecc.is_some()
|| self.data_block_compression != compression
{
return Ok(false);
}
// A key's versions stay in one output, and a group that continues the
// key written last would split them across the copy's boundary.
if self
.current_key
.as_ref()
.is_some_and(|c| crate::comparator::same_user_key(c, &first.key.user_key))
{
return Ok(false);
}
let previous_key = self.current_key.replace(first.key.user_key.clone());
if !self.range_tombstones.is_empty() {
self.tombstone_share.advance(
&self.range_tombstones,
&first.key.user_key,
self.comparator.as_ref(),
);
// A tombstone starting among the group's keys could take the table
// past its target between two of them, where rows written one by
// one would close it: those rows are written so. Advancing to the
// first key again for them changes nothing.
if self.tombstone_share.starts_within(
&self.range_tombstones,
(&first.key.user_key, &last.key.user_key),
self.comparator.as_ref(),
) {
self.current_key = previous_key;
return Ok(false);
}
if previous_key.is_none() && self.clip_range_tombstones {
self.tombstone_share.open_output(&first.key.user_key);
}
}
// A table records one value layout: a group stored the other way
// starts the next table, a run of groups stored one way shares it.
let layout_changes = self.value_layout.is_some_and(|current| current != layout);
// The output is weighed once for the cut and for the copy's checks.
let load = self.output_load();
let (cut, crossing) = self.decide_cut_at(&first.key.user_key, load);
if self.writes_group_by_rows(load, layout_changes || cut, crossing, last, bytes) {
self.current_key = previous_key;
return Ok(false);
}
if layout_changes || cut {
self.rotate()?;
self.tombstone_share.open_output(&first.key.user_key);
}
// Taken now: rows written one by one after a refused copy reach the
// same key in the same output, which has passed these boundaries.
self.commit_cut(crossing, layout_changes || cut);
let comparator = self.comparator.clone();
if !write(&mut self.writer, &comparator)? {
// The rows are written next, as rows: their first key must reach
// [`Self::write`] as a new key, so it rotates for their layout as
// it would without this attempt. Advancing the tombstone share to
// that key and opening an output there do the same again.
self.current_key = previous_key;
return Ok(false);
}
// The group's later keys move the tombstone share and the boundaries
// as written rows would; both only move forward, so its last key is
// enough.
if !self.range_tombstones.is_empty() {
self.tombstone_share.advance(
&self.range_tombstones,
&last.key.user_key,
self.comparator.as_ref(),
);
}
// A boundary counts once the output holds half the target when a key
// crosses it. Where among the group's keys the copy took the output
// past half is unknown, so its boundaries count only when the output
// held half before it: never overstating the floor rows would reach.
let past_half = self.alignment.is_some()
&& !(layout_changes || cut)
&& load.is_some_and(|load| load.reaches(cut_alignment::share_of(self.target_size, 50)));
if let Some(alignment) = &mut self.alignment {
alignment.pass_through(&last.key.user_key, past_half, self.comparator.as_ref());
}
self.current_key = Some(last.key.user_key.clone());
self.value_layout = Some(layout);
self.note_output_base();
if let Some(marker) = &self.transform_marker {
self.transforms_after_last_write = marker.load(core::sync::atomic::Ordering::Relaxed);
}
Ok(true)
}
/// Writes a consumer-provided columnar batch as one columnar block, rotating
/// to a fresh table first if the current one has reached the target size (a
/// batch is a block boundary, mirroring the new-key rotation in
/// [`Self::write`]). Forwards to the inner writer's columnar-ingest path.
#[cfg(feature = "columnar")]
pub(crate) fn write_columnar_batch(
&mut self,
batch: &crate::table::columnar::ColumnBatch,
) -> crate::Result<Option<crate::UserKey>> {
// A table records one value layout, so a batch after rows starts the
// next table.
if self.table_full()
|| matches!(
self.value_layout,
Some(
crate::table::meta::ValueLayout::Whole | crate::table::meta::ValueLayout::Cells
)
)
{
self.rotate()?;
}
// A batch lands whole, so the output's base is what it held before
// it: closing after the batch sheds all of the batch's state.
self.note_output_base();
let comparator = self.comparator.clone();
let last = self.writer.write_columnar_batch(batch, &comparator)?;
self.value_layout = Some(crate::table::meta::ValueLayout::Split);
Ok(last)
}
/// Records what the current output carries at its first record, or before
/// its first batch: the state its writer holds and the tombstones carried
/// into it.
fn note_output_base(&mut self) {
if self.output_base.is_none() {
let (tombstones, pieces, _) = self.tombstones_at_key();
let tombstones_held = if tombstones == 0 {
0
} else {
self.tombstones_held(tombstones, pieces)
};
self.output_base = Some((self.writer.held_state_bytes() + tombstones_held, tombstones));
}
}
/// Validates a columnar batch against the ingest contract without writing,
/// so the ingestion can reject a malformed batch eagerly while block emission
/// stays deferred. Forwards to the inner writer's validation.
#[cfg(feature = "columnar")]
pub(crate) fn validate_columnar_batch(
&self,
batch: &crate::table::columnar::ColumnBatch,
) -> crate::Result<()> {
let comparator = self.comparator.clone();
self.writer.validate_columnar_batch(batch, &comparator)
}
/// A flush's tombstones past its last key reach no rotation check on a
/// key, and the last output's zone is open above. They are checked at the
/// points where the entries change, each start and each end, in order:
/// where the output would pass its target, it closes at that point and an
/// output of tombstones alone takes the zone above. At a start, the check
/// counts the tombstones starting there, whole, so a group sharing the
/// last start is split from what came before. Such outputs are separate
/// L0 runs, like every flush output. Clipping drops these tombstones from
/// a compaction's outputs instead.
fn split_tombstones_past_the_last_key(&mut self) -> crate::Result<()> {
use core::cmp::Ordering;
if self.clip_range_tombstones {
return Ok(());
}
let comparator = self.comparator.clone();
// The longest bound any output's key range and sentinel can take.
let longest = self
.range_tombstones
.iter()
.map(|rt| rt.start.len().max(rt.end.len()))
.max()
.unwrap_or(0) as u64;
// The most filter prefixes an output's sentinel can yield: it is one of
// the tombstones' bounds.
let prefixes = self.prefix_extractor.as_ref().map_or(0, |extractor| {
self.range_tombstones
.iter()
.flat_map(|rt| [&rt.start, &rt.end])
.map(|bound| extractor.prefixes(bound).count())
.max()
.unwrap_or(0)
});
// An output of tombstones alone writes a synthetic table around them,
// which its writer, holding no record, does not count yet. Every
// output is configured alike, so the table is the same for each. It is
// sized for the longest bound of the whole set, an upper bound for any
// one zone; since such an output closes only once its tombstones take
// a block past that table, a long bound further on does not split the
// short ones before it.
let alone_overhead = self.writer.tombstone_only_overhead(longest, prefixes)?;
loop {
let tombstones = &self.range_tombstones;
let share = &self.tombstone_share;
let start = tombstones.get(share.next_pending()).map(|rt| &rt.start);
let end = share.first_open_end(tombstones);
let point = match (start, end) {
(Some(start), Some(end)) => {
if comparator.compare(start, end) == Ordering::Greater {
end.clone()
} else {
start.clone()
}
}
(Some(point), None) | (None, Some(point)) => point.clone(),
(None, None) => break,
};
self.tombstone_share
.advance(&self.range_tombstones, &point, comparator.as_ref());
let bytes = self.tombstone_share.bytes(&point);
let group =
self.tombstone_share
.group(&self.range_tombstones, &point, comparator.as_ref());
let alone = if self.writer.meta.key_count == 0 {
alone_overhead
} else {
(0, 0)
};
// An empty zone is never closed: that output would hold nothing,
// nor is the last one, with nothing above it. An output holding
// records is not empty, whatever tombstones it has so far.
if (bytes > 0 || self.writer.meta.key_count > 0)
&& self.tombstone_share.has_more(&self.range_tombstones)
&& self
.load_with_tombstones(
bytes + group.bytes,
self.tombstone_share.pieces() + group.entries,
self.tombstone_share
.longest_bound(&point)
.max(group.longest),
alone,
)
.reaches(self.target_size)
// The output after this one holds tombstones alone.
&& self.rotation_sheds(&point, alone_overhead)
{
self.current_key = Some(point.clone());
self.rotate()?;
self.tombstone_share.open_output(&point);
}
self.tombstone_share
.open_through(&self.range_tombstones, &point, comparator.as_ref());
}
Ok(())
}
/// Finishes the last table, making sure all data is written durably
///
/// Returns the created tables and whether their directory entries are
/// durable: the outputs opened here, for tombstones past the last key,
/// count as much as those opened before.
pub fn finish(mut self) -> crate::Result<Written> {
self.split_tombstones_past_the_last_key()?;
// Same judgment as `rotate` for the LAST output's window — by the
// LIVE counter here: with no successor output, trailing verdicts
// (removals after the final write) have nowhere else to land, and a
// record they removed would otherwise be resurrected by the dedup
// trading this output back for its inputs.
if let Some(marker) = &self.transform_marker
&& marker.load(core::sync::atomic::Ordering::Relaxed) > self.transforms_at_output_start
{
self.writer.mark_lineage_transformed();
}
// This output closes the run: together with the run's first output
// (no `lineage_prev`) and an unbroken adjacency chain, the marker
// proves a surviving set is the COMPLETE output set. Only a writer
// that owned the WHOLE merged stream may claim it (see
// `owns_whole_run`). Rotation always feeds the successor its
// triggering record, so the writer finishing here is the run's last
// non-empty output (or the run produced nothing at all and no meta
// is written).
if self.owns_whole_run {
self.writer.mark_lineage_last();
}
self.writer.spill_block()?;
// Write range tombstones to the last writer. No next table exists,
// so clip_upper=None falls back to upper_bound_exclusive(last_key)
// under clipping, and leaves the flush zone open above.
if !self.range_tombstones.is_empty() {
Self::write_rts_to_writer(
self.tombstone_share.zone(&self.range_tombstones, true),
self.clip_range_tombstones,
&mut self.writer,
self.output_lower.as_ref(),
None,
self.comparator.as_ref(),
);
}
for linked in self.linked_blobs.take() {
self.writer.link_blob_file(linked);
}
self.writer
.own_blob_objects(core::mem::take(&mut self.owned_objects));
let path = self.writer.path.clone();
if let Some((table_id, checksum)) = self
.writer
.finish_deferring_dir_sync(self.inherited_age.as_mut())?
{
self.results.push((table_id, checksum));
if let Some(outputs) = &self.outputs {
outputs.landed(&*self.fs, &path)?;
}
}
Ok(Written {
tables: self.results,
entries_durable: self.entries_durable,
})
}
}
/// The tables a [`MultiWriter`] wrote.
pub struct Written {
pub tables: Vec<(TableId, Checksum)>,
/// Whether every table's directory entry was durable before its table was
/// written, so the install naming them syncs no folder for them (see
/// [`super::Table::mark_entry_durable`]).
pub entries_durable: bool,
}
#[cfg(test)]
#[allow(
clippy::unwrap_used,
clippy::indexing_slicing,
clippy::useless_vec,
reason = "test code"
)]
mod tests;