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
// SPDX-License-Identifier: Apache-2.0
// Copyright (c) 2024-present, fjall-rs
// Copyright (c) 2026-present, Structured World Foundation
use crate::tree::inner::{FlushGuard, VersionsWriteGuard};
use crate::{
AnyTree, BlobTree, Config, Guard, InternalValue, KvPair, Memtable, SeqNo, TableId, Tree,
UserKey, UserValue,
iter_guard::{IterGuardImpl, SeekableGuardIter},
table::Table,
version::Version,
vlog::BlobFile,
};
use alloc::sync::Arc;
#[cfg(not(feature = "std"))]
use alloc::{boxed::Box, vec::Vec};
use core::ops::RangeBounds;
pub type RangeItem = crate::Result<KvPair>;
type FlushToTablesResult = (Vec<Table>, Option<Vec<BlobFile>>);
/// Summary of a checkpoint produced by
/// [`AbstractTree::create_checkpoint`].
///
/// All byte counts are *logical* file sizes — hard links share the
/// underlying inode storage, so a checkpoint's marginal disk usage is
/// typically zero until the original files are compacted away.
#[derive(Debug, Clone, Copy)]
pub struct CheckpointInfo {
/// Number of SST files captured.
pub sst_files: usize,
/// Number of blob (value-log) files captured. Always `0` for a
/// standard [`Tree`].
pub blob_files: usize,
/// Sum of the logical file sizes of every captured SST + blob.
pub total_bytes: u64,
/// The version ID embedded in the checkpoint's `current` pointer.
pub version_id: u64,
/// Lower-bound visible-seqno watermark for the snapshot.
///
/// Captured from the tree's `visible_seqno` generator BEFORE
/// [`AbstractTree::current_version`]. Following the standard
/// "lowest-excluded" watermark convention, `info.seqno = N` means
/// every record with `seqno < N` was committed at sample time and
/// is therefore guaranteed to be present in the snapshot. Records
/// with `seqno == N` may or may not be included (writers can hold
/// a record in the memtable for an instant before publishing the
/// next watermark); records with `seqno > N` may also be present
/// (writers can advance the counter between sample and version
/// snapshot, and those keys still land in the captured memtable).
///
/// PITR consumers MUST use `seqno < info.seqno` as the inclusion
/// gate. Using `<=` (treating this as a max-included ceiling)
/// could move a recovery cutoff past data still needed from WAL
/// or replication; the field is a strict lower-exclusive watermark,
/// not a max-included ceiling.
pub seqno: SeqNo,
}
// Sealed on purpose: this trait is still public as a consumer-side bound
// (`&impl AbstractTree`), but external implementations are no longer part of
// the supported extension surface. Internal flush/version hooks keep evolving
// with crate-owned tree types and must not create downstream semver traps.
//
// `sealed` stays `pub` only so sibling modules in this crate can write
// `crate::abstract_tree::sealed::Sealed` in their impls. The parent module
// `abstract_tree` is not publicly exported from the crate root, so downstream
// crates still cannot name or implement this trait.
pub mod sealed {
pub trait Sealed {}
}
/// Generic Tree API
#[enum_dispatch::enum_dispatch]
pub trait AbstractTree: sealed::Sealed {
/// Debug method for tracing the MVCC history of a key.
#[doc(hidden)]
fn print_trace(&self, key: &[u8]) -> crate::Result<()>;
/// Returns the number of cached table file descriptors.
fn table_file_cache_size(&self) -> usize;
// TODO: remove
#[doc(hidden)]
fn version_memtable_size_sum(&self) -> u64 {
self.get_version_history_lock().memtable_size_sum()
}
#[doc(hidden)]
fn next_table_id(&self) -> TableId;
#[doc(hidden)]
fn id(&self) -> crate::TreeId;
/// Like [`AbstractTree::get`], but returns the actual internal entry, not just the user value.
///
/// Used in tests.
#[doc(hidden)]
fn get_internal_entry(&self, key: &[u8], seqno: SeqNo) -> crate::Result<Option<InternalValue>>;
#[doc(hidden)]
fn current_version(&self) -> Version;
/// Returns a read-only snapshot of the tree's on-disk storage footprint:
/// total used bytes, entry count, the average shape of a stored entry
/// (average key / value bytes), and an estimate of how many more
/// average-shaped entries fit in a byte budget (see
/// [`StorageStats::estimated_remaining_entries`](crate::StorageStats::estimated_remaining_entries)).
///
/// Computed from the live version's table + blob metadata plus one
/// size-stat per live file; it never reads a data block. The default
/// implementation reports [`StorageStatus::Healthy`](crate::StorageStatus::Healthy);
/// the standard tree overrides it to report
/// [`StorageStatus::CompactionInProgress`](crate::StorageStatus::CompactionInProgress) while a
/// compaction runs.
///
/// # Examples
///
/// ```
/// # use lsm_tree::Error as TreeError;
/// use lsm_tree::{AbstractTree, Config};
///
/// let folder = tempfile::tempdir()?;
/// let tree = Config::new(&folder, Default::default(), Default::default()).open()?;
///
/// for i in 0..100u64 {
/// tree.insert(format!("key{i:04}"), "value", i);
/// }
/// tree.flush_active_memtable(0)?;
///
/// let stats = tree.storage_stats()?;
/// assert_eq!(stats.item_count, 100);
/// // Roughly how many more average-shaped entries fit in another 1 MiB.
/// let _headroom = stats.estimated_remaining_entries(1024 * 1024);
/// #
/// # Ok::<(), TreeError>(())
/// ```
///
/// # Errors
///
/// Returns an error if a live file's size cannot be stat-ed.
fn storage_stats(&self) -> crate::Result<crate::StorageStats> {
crate::storage_stats::compute_storage_stats(&self.current_version(), false, true)
}
/// Per-LSM-level and per-segment size + entry-count stats, for tiering and
/// erasure-coding placement decisions (which level / segment is large enough
/// to demote, EC-encode, or migrate).
///
/// Cheap: derived from the live version's metadata plus one file-size stat
/// per segment (no data-block scan). The per-level totals reconcile with
/// [`storage_stats`](Self::storage_stats): summed across levels they equal
/// the SST portion of [`StorageStats::used_bytes`](crate::StorageStats::used_bytes)
/// and [`StorageStats::item_count`](crate::StorageStats::item_count).
///
/// # Examples
///
/// ```
/// # use lsm_tree::Error as TreeError;
/// use lsm_tree::{AbstractTree, Config};
///
/// let folder = tempfile::tempdir()?;
/// let tree = Config::new(&folder, Default::default(), Default::default()).open()?;
/// for i in 0..100u32 {
/// tree.insert(format!("k{i:04}"), "v", 0);
/// }
/// tree.flush_active_memtable(0)?;
///
/// let levels = tree.level_segment_stats()?;
/// let total: u64 = levels.iter().map(|l| l.item_count).sum();
/// assert_eq!(total, tree.storage_stats()?.item_count);
/// #
/// # Ok::<(), TreeError>(())
/// ```
///
/// # Errors
///
/// Returns an error if a segment's file size cannot be stat-ed.
fn level_segment_stats(&self) -> crate::Result<Vec<crate::LevelStats>> {
crate::storage_stats::compute_level_segment_stats(&self.current_version())
}
/// Estimated bytes pending compaction under `strategy`: on-disk data above
/// its level's target that must eventually be rewritten downward (a `RocksDB`
/// `estimate-pending-compaction-bytes` analog), a compaction-debt signal for a
/// scheduler / tiering consumer.
///
/// The strategy is supplied by the caller because the engine does not own a
/// configured compaction strategy (it is injected per compaction run); a
/// `&dyn` keeps this object-safe. Returns `0` for strategies without a
/// size-target notion of debt (FIFO, drop-range), or when the tree is at or
/// below its target shape. See
/// [`CompactionStrategy::pending_compaction_bytes`](crate::compaction::CompactionStrategy::pending_compaction_bytes).
fn compaction_debt(&self, strategy: &dyn crate::compaction::CompactionStrategy) -> u64 {
strategy.pending_compaction_bytes(&self.current_version())
}
/// Computed write-backpressure verdict from the live L0 table count and the
/// strategy's pending-compaction bytes, against the configured
/// [`RuntimeConfig::backpressure`](crate::runtime_config::RuntimeConfig)
/// thresholds.
///
/// Advisory, mirroring [`write_admission`](Self::write_admission): the caller
/// consults it and throttles in its own write loop (sleep `suggested_delay`
/// at [`Slowdown`](crate::Backpressure::Slowdown), pause at
/// [`Stop`](crate::Backpressure::Stop)). The engine never blocks on it,
/// because it does not own the compaction that drains the debt — an internal
/// stall could deadlock the very thread that would compact.
///
/// The strategy is supplied by the caller for the same reason as
/// [`compaction_debt`](Self::compaction_debt). Returns
/// [`Backpressure::None`](crate::Backpressure::None) by default (no
/// thresholds configured).
fn write_backpressure(
&self,
_strategy: &dyn crate::compaction::CompactionStrategy,
) -> crate::Backpressure {
crate::Backpressure::None
}
/// Storage admission gate: `Ok(())` if a write may proceed, or
/// [`Error::StorageFull`](crate::Error::StorageFull) if the tree is
/// over budget and should be treated as read-only.
///
/// Opt-in: returns `Ok(())` unless
/// [`storage_admission_check`](crate::runtime_config::RuntimeConfig::storage_admission_check)
/// is enabled. The predicate is computed (not latched), so once space is
/// freed — the budget raised, a compaction reclaiming space, or disk freed —
/// the next call admits again with no restart.
///
/// Intended as a cheap pre-check the caller consults before applying a
/// write batch. The footprint is cached per version, so the check is a
/// constant-time read on the common path and only re-measures when a new
/// version is installed (flush / compaction). Internal flush / compaction
/// are never gated, so the engine can always reclaim.
///
/// # Errors
///
/// [`Error::StorageFull`](crate::Error::StorageFull) when the live footprint
/// plus reserved headroom exceeds the effective budget.
fn write_admission(&self) -> crate::Result<()> {
Ok(())
}
/// `true` when the admission gate is currently closed (see
/// [`write_admission`](Self::write_admission)). Convenience for callers that
/// want a boolean rather than a `Result`. Always `false` unless admission
/// control is enabled and the tree is over budget.
///
/// Only [`Error::StorageFull`](crate::Error::StorageFull) counts as
/// read-only: an unrelated admission error (e.g. an I/O failure while
/// measuring the footprint) is NOT an out-of-space condition and must not be
/// reported as one.
fn is_read_only(&self) -> bool {
matches!(
self.write_admission(),
Err(crate::Error::StorageFull { .. })
)
}
/// Proactively verifies every block's XXH3 checksum across every SST in
/// the tree's current version — a scrubber for catching bit rot before it
/// surfaces as a user-visible read failure (cron / scrub jobs).
///
/// Reports at block granularity and never aborts early. The returned
/// [`BlockVerifyReport`](crate::verify::BlockVerifyReport) records
/// block-corruption findings with `(file, offset)`, while file-level errors
/// (e.g. [`BlockVerifyError::SstFileUnreadable`](crate::verify::BlockVerifyError::SstFileUnreadable))
/// carry the file only (no offset). It does not surface per-entry indices or
/// ECC-correction counts (when ECC-at-rest is enabled a within-budget corrupt
/// block may still be healed on read as a side effect of the scan, but the
/// report does not tally corrections).
///
/// Filesystems with native per-block integrity (ZFS, Btrfs, `ReFS`, S3 —
/// see [`Fs::capabilities`](crate::fs::Fs::capabilities)) already detect
/// corruption on read; this scrub is the portable check for the rest.
///
/// Use [`Self::verify_checksum_with`] for parallelism / throttle control.
#[cfg(feature = "std")]
fn verify_checksum(&self) -> crate::verify::BlockVerifyReport
where
Self: Sized,
{
crate::verify::verify_block_checksums(self)
}
/// Like [`Self::verify_checksum`] but with configurable parallelism and
/// I/O throttle (see [`VerifyOptions`](crate::verify::VerifyOptions)).
#[cfg(feature = "std")]
fn verify_checksum_with(
&self,
options: &crate::verify::VerifyOptions,
) -> crate::verify::BlockVerifyReport
where
Self: Sized,
{
crate::verify::verify_block_checksums_with(self, options)
}
#[doc(hidden)]
fn get_version_history_lock(&self) -> VersionsWriteGuard<'_>;
/// Creates a hard-linked checkpoint of the tree's on-disk state in
/// `target_path` for point-in-time recovery (PITR) backup.
///
/// The checkpoint is a fully functional tree that can be opened
/// independently via [`Config::open`](crate::Config::open). For the
/// common single-filesystem case all SST files (and blob files, for
/// [`BlobTree`]) are hard-linked rather than copied, so the operation
/// is O(1) per file and consumes zero additional disk space until the
/// original files are compacted away — at which point the inode is
/// kept alive by the checkpoint link.
///
/// # Cross-filesystem / cross-backend fall-back
///
/// When a source file lives on a different filesystem than the
/// checkpoint target — e.g. an SST routed to a hot tier via
/// [`level_routes`](crate::Config::level_routes) on a separate volume,
/// or a backup directory on a foreign mount — the hard link cannot
/// be created (Unix `EXDEV`). In that case the checkpoint silently
/// falls back to a streamed byte copy, which:
///
/// - takes time linear in the file size instead of O(1), and
/// - consumes disk space equal to the copied bytes on the target
/// volume (no inode sharing across filesystems).
///
/// Each fall-back call emits one [`log::debug`] line (deliberately not
/// `warn`: a misconfigured tier could trigger this path once per SST
/// and per blob — thousands of times per snapshot — and per-file
/// warnings would drown real signal). Operators wanting hard-visibility
/// of unexpected full copies should enable debug logging on the `fs`
/// module or watch the `CheckpointInfo.total_bytes` figure (≫ inode
/// link cost means the fallback fired). The same `debug` policy applies
/// when source and target use entirely different [`Fs`](crate::fs::Fs)
/// backends (e.g. [`MemFs`](crate::fs::MemFs) → [`StdFs`](crate::fs::StdFs)
/// in tests).
///
/// # Concurrency
///
/// While the checkpoint is being built, compaction continues normally
/// but the physical removal of obsolete files is deferred until the
/// checkpoint hard-links are in place. This is implemented by an
/// internal reference-counted deletion gate; callers do not have to
/// pause compaction themselves.
///
/// # Errors
///
/// Returns an error if:
/// - the active memtable could not be flushed,
/// - `target_path` already exists (to prevent accidental overwrites),
/// - a hard link / copy fall-back could not be created, or
/// - the manifest / version pointer files could not be replicated.
///
/// On error any partial checkpoint files are removed automatically
/// (best-effort) so callers can safely retry against the same path.
// std-only: checkpoint creation hard-links / copies files via std::fs.
#[cfg(feature = "std")]
fn create_checkpoint(&self, target_path: &crate::path::Path) -> crate::Result<CheckpointInfo>;
/// Seals the active memtable and flushes to table(s).
///
/// If there are already other sealed memtables lined up, those will be flushed as well.
///
/// Only used in tests.
#[doc(hidden)]
fn flush_active_memtable(&self, eviction_seqno: SeqNo) -> crate::Result<()> {
let lock = self.get_flush_lock();
self.rotate_memtable();
self.flush(&lock, eviction_seqno)?;
Ok(())
}
/// Synchronously flushes pending sealed memtables to tables.
///
/// Returns the sum of flushed memtable sizes that were flushed.
///
/// The function may not return a result, if nothing was flushed.
///
/// # Errors
///
/// Returns `Err` on an I/O error, or on a memtable residence-verification
/// failure under [`KvChecksumComputePoint::AtInsert`](crate::runtime_config::KvChecksumComputePoint::AtInsert):
/// a sealed memtable whose insert-time per-KV digests do not verify before
/// flush surfaces [`crate::Error::MemtableKvChecksumMismatch`],
/// [`crate::Error::MemtableKvChecksumCorruptAlgorithm`], or
/// [`crate::Error::InvalidTag`] (corrupt `value_type`).
fn flush(&self, _lock: &FlushGuard<'_>, seqno_threshold: SeqNo) -> crate::Result<Option<u64>> {
use crate::{
compaction::stream::CompactionStream, merge::Merger, range_tombstone::RangeTombstone,
};
let version_history = self.get_version_history_lock();
let latest = version_history.latest_version();
if latest.sealed_memtables.len() == 0 {
return Ok(None);
}
let sealed_ids = latest
.sealed_memtables
.iter()
.map(|mt| mt.id)
.collect::<Vec<_>>();
let flushed_size = latest.sealed_memtables.iter().map(|mt| mt.size()).sum();
// AtInsert residence check: verify each sealed memtable's insert-time
// per-KV digests against a recompute over the entries' current bytes
// before writing them out. A divergence means an entry was corrupted
// (a RAM bit-flip) while it sat in the memtable. Memtables with no
// insert digests (the default) return immediately without walking.
//
// This is a SEPARATE pass over the as-inserted memtable entries, not
// fused into the writer's per-KV footer encode, and deliberately so:
// the footer digest is computed over POST-merge / post-seqno-filter
// bytes (the CompactionStream below applies the merge operator), so a
// merge operator's combined value differs from any single inserted
// value. Comparing the carried insert digest against the footer digest
// would false-positive on every legitimate merge. Residence corruption
// is a property of what was inserted (pre-merge); it must be checked
// here, against the raw memtable entries.
for mt in latest.sealed_memtables.iter() {
mt.verify_kv_residence()?;
}
// Collect range tombstones from sealed memtables
let mut range_tombstones: Vec<RangeTombstone> = Vec::new();
for mt in latest.sealed_memtables.iter() {
range_tombstones.extend(mt.range_tombstones_sorted());
}
range_tombstones
.sort_by(|a, b| a.cmp_with_comparator(b, self.tree_config().comparator.as_ref()));
range_tombstones.dedup();
let merger = Merger::new(
latest
.sealed_memtables
.iter()
.map(|mt| mt.iter().map(Ok))
.collect::<Vec<_>>(),
self.tree_config().comparator.clone(),
);
// RT suppression is not needed here: flush writes both entries and RTs
// to the output tables. Suppression happens at read time, not write time.
let stream = CompactionStream::new(merger, seqno_threshold)
.with_merge_operator(self.tree_config().merge_operator.clone());
drop(version_history);
// Clone needed: flush_to_tables_with_rt consumes the Vec, but on the
// RT-only path (no KV data, tables.is_empty()) we re-insert RTs into the
// active memtable. Flush is infrequent and RT count is small.
if let Some((tables, blob_files)) =
self.flush_to_tables_with_rt(stream, range_tombstones.clone())?
{
// If no tables were produced (RT-only memtable), re-insert RTs
// into active memtable so they aren't lost
if tables.is_empty() && !range_tombstones.is_empty() {
let active = self.active_memtable();
for rt in &range_tombstones {
let _ =
active.insert_range_tombstone(rt.start.clone(), rt.end.clone(), rt.seqno);
}
}
self.register_tables(
&tables,
blob_files.as_deref(),
None,
&sealed_ids,
seqno_threshold,
)?;
}
Ok(Some(flushed_size))
}
/// Returns an iterator that scans through the entire tree.
///
/// Avoid using this function, or limit it as otherwise it may scan a lot of items.
fn iter(
&self,
seqno: SeqNo,
index: Option<(Arc<Memtable>, SeqNo)>,
) -> Box<dyn DoubleEndedIterator<Item = IterGuardImpl> + Send + 'static> {
self.range::<&[u8], _>(.., seqno, index)
}
/// Returns an iterator over a prefixed set of items.
///
/// Avoid using an empty prefix as it may scan a lot of items (unless limited).
fn prefix<K: AsRef<[u8]>>(
&self,
prefix: K,
seqno: SeqNo,
index: Option<(Arc<Memtable>, SeqNo)>,
) -> Box<dyn DoubleEndedIterator<Item = IterGuardImpl> + Send + 'static>;
/// Returns an iterator over a range of items.
///
/// Avoid using full or unbounded ranges as they may scan a lot of items (unless limited).
fn range<K: AsRef<[u8]>, R: RangeBounds<K>>(
&self,
range: R,
seqno: SeqNo,
index: Option<(Arc<Memtable>, SeqNo)>,
) -> Box<dyn DoubleEndedIterator<Item = IterGuardImpl> + Send + 'static>;
/// Returns a range iterator that can reposition (seek) in place without
/// reopening its per-SST readers.
///
/// Unlike [`range`](Self::range)'s boxed `DoubleEndedIterator`, the returned
/// [`SeekableGuardIter`] additionally exposes `seek_to` / `seek_to_for_prev`,
/// so a consumer can jump a live iterator to any key (`RocksDB` `Seek` /
/// `SeekForPrev`) — enabling data-dependent scans (joins, skip-scan) without
/// reopening per-SST readers per jump.
fn range_seekable<K: AsRef<[u8]>, R: RangeBounds<K>>(
&self,
range: R,
seqno: SeqNo,
index: Option<(Arc<Memtable>, SeqNo)>,
) -> Box<dyn SeekableGuardIter + 'static>;
/// Scans a sequence of disjoint, ascending key sub-intervals, reusing one set
/// of per-SST readers across all of them.
///
/// The per-SST setup is paid once (when the underlying seekable iterator is
/// opened); each interval is served by repositioning that iterator. This
/// amortizes the setup across `N` intervals instead of paying it per
/// interval, so multi-interval scan throughput scales with the total rows
/// returned rather than the interval count.
///
/// The interval source is pulled lazily, so intervals may be produced on
/// demand (e.g. computed from rows already returned).
fn batch_range_scan<K: AsRef<[u8]>, R: RangeBounds<K> + 'static, I: IntoIterator<Item = R>>(
&self,
intervals: I,
seqno: SeqNo,
index: Option<(Arc<Memtable>, SeqNo)>,
) -> Box<dyn Iterator<Item = IterGuardImpl> + Send + 'static>
where
I::IntoIter: Send + 'static;
/// Returns the approximate number of tombstones in the tree.
fn tombstone_count(&self) -> u64;
/// Returns the approximate number of weak tombstones (single deletes) in the tree.
fn weak_tombstone_count(&self) -> u64;
/// Returns the approximate number of values reclaimable once weak tombstones can be GC'd.
fn weak_tombstone_reclaimable_count(&self) -> u64;
/// Drops tables that are fully contained in a given range.
///
/// Accepts any `RangeBounds`, including unbounded or exclusive endpoints.
/// If the normalized lower bound is greater than the upper bound, the
/// method returns without performing any work.
///
/// # Errors
///
/// Will return `Err` only if an IO error occurs.
fn drop_range<K: AsRef<[u8]>, R: RangeBounds<K>>(&self, range: R) -> crate::Result<()>;
/// Drops all tables and clears all memtables atomically.
///
/// # Errors
///
/// Will return `Err` only if an IO error occurs.
fn clear(&self) -> crate::Result<()>;
/// Performs major compaction, blocking the caller until it's done.
///
/// Returns a [`crate::compaction::CompactionResult`] describing what action was taken.
///
/// # Garbage-collection / merge-fold watermark (`seqno_threshold`)
///
/// `seqno_threshold` is the MVCC garbage-collection watermark: the engine may
/// collapse history that no snapshot reading at a seqno `< seqno_threshold`
/// can still observe. Concretely, only entries whose seqno is `< seqno_threshold`
/// are eligible for:
///
/// - dropping shadowed versions / GC-ing tombstones, and
/// - **folding merge operands** via the [`crate::MergeOperator`]: a key written
/// only through [`Self::merge`] (no base value) accumulates one operand per
/// call, and reads re-apply the whole chain (`O(operands)` per read) until
/// compaction folds it. Folding a chain into a single value is only
/// MVCC-safe when no live snapshot reads *between* the operands, which is
/// exactly what `seqno_threshold` certifies.
///
/// The engine does **not** track snapshots (unlike a `RocksDB`-style
/// snapshot list); the caller owns snapshot lifecycle and must supply this
/// watermark:
///
/// - To fold/GC everything (no active snapshots), pass a value **above every
/// live seqno** (e.g. the next value from the [`crate::SequenceNumberCounter`]).
/// - With outstanding snapshots, pass the **oldest** snapshot's seqno so their
/// reads stay correct.
/// - `seqno_threshold == 0` certifies nothing as collapsible, so **no folding
/// or GC happens** — `major_compact(target, 0)` only restructures tables and
/// leaves a merge-only key's full operand chain intact.
///
/// # Errors
///
/// Will return `Err` if an IO error occurs.
fn major_compact(
&self,
target_size: u64,
seqno_threshold: SeqNo,
) -> crate::Result<crate::compaction::CompactionResult>;
/// Returns the disk space used by stale blobs.
fn stale_blob_bytes(&self) -> u64 {
0
}
/// Gets the space usage of all filters in the tree.
///
/// May not correspond to the actual memory size because filter blocks may be paged out.
fn filter_size(&self) -> u64;
/// Gets the memory usage of all pinned filters in the tree.
fn pinned_filter_size(&self) -> usize;
/// Gets the memory usage of all pinned index blocks in the tree.
fn pinned_block_index_size(&self) -> usize;
/// Gets the length of the version free list.
fn version_free_list_len(&self) -> usize;
/// Returns the metrics structure.
#[cfg(feature = "metrics")]
fn metrics(&self) -> &Arc<crate::Metrics>;
/// A point-in-time [`CacheStats`](crate::CacheStats) snapshot of block-cache
/// effectiveness (cumulative hit / miss counts and rate) and occupancy
/// (current size against capacity).
///
/// The stable, owned observability view over the block cache, so a consumer
/// can read cache health without holding the mutable
/// [`metrics`](Self::metrics) handle. Counts are cumulative since process
/// start; derive a rate over an interval from the delta between two polls.
#[cfg(feature = "metrics")]
fn cache_stats(&self) -> crate::CacheStats;
/// Acquires the flush lock which is required to call [`Tree::flush`].
fn get_flush_lock(&self) -> FlushGuard<'_>;
/// Synchronously flushes a memtable to a table.
///
/// This method will not make the table immediately available,
/// use [`AbstractTree::register_tables`] for that.
///
/// # Errors
///
/// Will return `Err` if an IO error occurs.
fn flush_to_tables(
&self,
stream: impl Iterator<Item = crate::Result<InternalValue>>,
) -> crate::Result<Option<FlushToTablesResult>> {
self.flush_to_tables_with_rt(stream, Vec::new())
}
/// Like [`AbstractTree::flush_to_tables`], but also writes range tombstones.
///
/// This is an internal extension hook on the crate's sealed tree types and
/// is hidden from generated documentation.
///
/// # Errors
///
/// Will return `Err` if an IO error occurs.
#[doc(hidden)]
fn flush_to_tables_with_rt(
&self,
stream: impl Iterator<Item = crate::Result<InternalValue>>,
range_tombstones: Vec<crate::range_tombstone::RangeTombstone>,
) -> crate::Result<Option<FlushToTablesResult>>;
/// Atomically registers flushed tables into the tree, removing their associated sealed memtables.
///
/// # Errors
///
/// Will return `Err` if an IO error occurs.
fn register_tables(
&self,
tables: &[Table],
blob_files: Option<&[BlobFile]>,
frag_map: Option<crate::blob_tree::FragmentationMap>,
sealed_memtables_to_delete: &[crate::tree::inner::MemtableId],
gc_watermark: SeqNo,
) -> crate::Result<()>;
/// Clears the active memtable atomically.
fn clear_active_memtable(&self);
/// Returns the number of sealed memtables.
fn sealed_memtable_count(&self) -> usize;
/// Performs compaction on the tree's levels, blocking the caller until it's done.
///
/// Returns a [`crate::compaction::CompactionResult`] describing what action was taken.
///
/// # Errors
///
/// Will return `Err` if an IO error occurs.
fn compact(
&self,
strategy: Arc<dyn crate::compaction::CompactionStrategy>,
seqno_threshold: SeqNo,
) -> crate::Result<crate::compaction::CompactionResult>;
/// Returns the next table's ID.
fn get_next_table_id(&self) -> TableId;
/// Returns the tree config.
fn tree_config(&self) -> &Config;
/// Returns the highest sequence number.
fn get_highest_seqno(&self) -> Option<SeqNo> {
let memtable_seqno = self.get_highest_memtable_seqno();
let table_seqno = self.get_highest_persisted_seqno();
memtable_seqno.max(table_seqno)
}
/// Returns the active memtable.
fn active_memtable(&self) -> Arc<Memtable>;
/// Returns the tree type.
fn tree_type(&self) -> crate::TreeType {
if self.tree_config().kv_separation_opts.is_some() {
crate::TreeType::Blob
} else {
crate::TreeType::Standard
}
}
/// Seals the active memtable.
fn rotate_memtable(&self) -> Option<Arc<Memtable>>;
/// Returns the number of tables currently in the tree.
fn table_count(&self) -> usize;
/// Returns the number of tables in `levels[idx]`.
///
/// Returns `None` if the level does not exist (if idx >= 7).
fn level_table_count(&self, idx: usize) -> Option<usize>;
/// Returns the number of disjoint runs in L0.
///
/// Can be used to determine whether to write stall.
fn l0_run_count(&self) -> usize;
/// Returns the number of blob files currently in the tree.
fn blob_file_count(&self) -> usize;
/// Approximates the number of items in the tree.
fn approximate_len(&self) -> usize;
/// Returns the disk space usage.
fn disk_space(&self) -> u64;
/// Estimates the on-disk bytes and entry count contained in `range` at
/// `seqno`, WITHOUT reading any data block.
///
/// The estimate interpolates each overlapping SST's data-block offsets at
/// the range boundaries (block granularity) and adds the active + sealed
/// memtables' in-range share. For a KV-separated tree the per-SST blob
/// bytes are apportioned by the same in-range fraction (blob files are not
/// key-indexed, so a finer estimate is impossible without reading data).
/// Intended for query planning (split-point selection, cost-based join
/// ordering), not exact accounting; accuracy is typically within ~10-15%
/// on roughly-uniform data.
///
/// # Examples
///
/// ```
/// # use lsm_tree::Error as TreeError;
/// use lsm_tree::{AbstractTree, Config};
///
/// let folder = tempfile::tempdir()?;
/// let tree = Config::new(&folder, Default::default(), Default::default()).open()?;
/// for i in 0..100u32 {
/// tree.insert(format!("k{i:04}"), "value", 0);
/// }
/// tree.flush_active_memtable(0)?;
///
/// // The full range covers every entry exactly once it is all flushed —
/// // estimated without reading a single data block.
/// let all = tree.approximate_range_stats::<&str, _>(.., 1)?;
/// assert_eq!(all.key_count, tree.approximate_len() as u64);
/// assert!(all.bytes > 0);
///
/// // A range past every key is empty.
/// let none = tree.approximate_range_stats("zzzz".."zzzzz", 1)?;
/// assert_eq!(none.key_count, 0);
/// assert_eq!(none.bytes, 0);
/// #
/// # Ok::<(), TreeError>(())
/// ```
///
/// # Errors
///
/// Returns an error if a block index or table metadata read fails.
fn approximate_range_stats<K: AsRef<[u8]>, R: RangeBounds<K>>(
&self,
range: R,
seqno: SeqNo,
) -> crate::Result<crate::ApproximateRangeStats>;
/// Estimates the row cardinality and selectivity of `range` at `seqno`,
/// WITHOUT reading any data block.
///
/// Uses the per-data-block zone map (per-block row counts + key ranges) for a
/// block-granularity row count: every data block whose key range overlaps the
/// query contributes its recorded row count, plus the active + sealed
/// memtables' in-range counts. When a table has no zone map the row count
/// falls back to the byte-fraction estimate of [`approximate_range_stats`].
/// Selectivity is `rows / total_rows`, monotonic in predicate tightness, for
/// cost-based planning (join ordering, scan-vs-seek). Not exact accounting.
///
/// [`approximate_range_stats`]: AbstractTree::approximate_range_stats
///
/// # Examples
///
/// ```
/// # use lsm_tree::Error as TreeError;
/// use lsm_tree::{AbstractTree, Config};
///
/// let folder = tempfile::tempdir()?;
/// let tree = Config::new(&folder, Default::default(), Default::default()).open()?;
/// for i in 0..100u32 {
/// tree.insert(format!("k{i:04}"), "v", 0);
/// }
/// tree.flush_active_memtable(0)?;
///
/// // The full range covers every row; selectivity is 1.0.
/// let all = tree.approximate_range_cardinality::<&str, _>(.., 1)?;
/// assert_eq!(all.rows, tree.approximate_len() as u64);
/// assert!((all.selectivity - 1.0).abs() < 1e-9);
///
/// // A tighter range selects fewer rows.
/// let part = tree.approximate_range_cardinality("k0000".."k0050", 1)?;
/// assert!(part.selectivity <= all.selectivity);
/// #
/// # Ok::<(), TreeError>(())
/// ```
///
/// # Errors
///
/// Returns an error if a block index, zone map, or table metadata read fails.
fn approximate_range_cardinality<K: AsRef<[u8]>, R: RangeBounds<K>>(
&self,
range: R,
seqno: SeqNo,
) -> crate::Result<crate::RangeCardinality>;
/// Returns the highest sequence number of the active memtable.
fn get_highest_memtable_seqno(&self) -> Option<SeqNo>;
/// Returns the highest sequence number that is flushed to disk.
fn get_highest_persisted_seqno(&self) -> Option<SeqNo>;
/// Scans the entire tree, returning the number of items.
///
/// ###### Caution
///
/// This operation scans the entire tree: O(n) complexity!
///
/// Never, under any circumstances, use .`len()` == 0 to check
/// if the tree is empty, use [`Tree::is_empty`] instead.
///
/// # Examples
///
/// ```
/// # use lsm_tree::Error as TreeError;
/// use lsm_tree::{AbstractTree, Config, Tree};
///
/// let folder = tempfile::tempdir()?;
/// let tree = Config::new(&folder, Default::default(), Default::default()).open()?;
///
/// assert_eq!(tree.len(0, None)?, 0);
/// tree.insert("1", "abc", 0);
/// tree.insert("3", "abc", 1);
/// tree.insert("5", "abc", 2);
/// assert_eq!(tree.len(3, None)?, 3);
/// #
/// # Ok::<(), TreeError>(())
/// ```
///
/// # Errors
///
/// Will return `Err` if an IO error occurs.
fn len(&self, seqno: SeqNo, index: Option<(Arc<Memtable>, SeqNo)>) -> crate::Result<usize> {
let mut count = 0;
for item in self.iter(seqno, index) {
let _ = item.key()?;
count += 1;
}
Ok(count)
}
/// Returns `true` if the tree is empty.
///
/// This operation has O(log N) complexity.
///
/// # Examples
///
/// ```
/// # let folder = tempfile::tempdir()?;
/// use lsm_tree::{AbstractTree, Config, Tree};
///
/// let tree = Config::new(folder, Default::default(), Default::default()).open()?;
/// assert!(tree.is_empty(0, None)?);
///
/// tree.insert("a", "abc", 0);
/// assert!(!tree.is_empty(1, None)?);
/// #
/// # Ok::<(), lsm_tree::Error>(())
/// ```
///
/// # Errors
///
/// Will return `Err` if an IO error occurs.
fn is_empty(&self, seqno: SeqNo, index: Option<(Arc<Memtable>, SeqNo)>) -> crate::Result<bool> {
Ok(self
.first_key_value(seqno, index)
.map(crate::Guard::key)
.transpose()?
.is_none())
}
/// Returns the first key-value pair in the tree.
/// The key in this pair is the minimum key in the tree.
///
/// # Examples
///
/// ```
/// # use lsm_tree::Error as TreeError;
/// # use lsm_tree::{AbstractTree, Config, Tree, Guard};
/// #
/// # let folder = tempfile::tempdir()?;
/// let tree = Config::new(folder, Default::default(), Default::default()).open()?;
///
/// tree.insert("1", "abc", 0);
/// tree.insert("3", "abc", 1);
/// tree.insert("5", "abc", 2);
///
/// let key = tree.first_key_value(3, None).expect("item should exist").key()?;
/// assert_eq!(&*key, "1".as_bytes());
/// #
/// # Ok::<(), TreeError>(())
/// ```
///
/// # Errors
///
/// Will return `Err` if an IO error occurs.
fn first_key_value(
&self,
seqno: SeqNo,
index: Option<(Arc<Memtable>, SeqNo)>,
) -> Option<IterGuardImpl> {
self.iter(seqno, index).next()
}
/// Returns the last key-value pair in the tree.
/// The key in this pair is the maximum key in the tree.
///
/// # Examples
///
/// ```
/// # use lsm_tree::Error as TreeError;
/// # use lsm_tree::{AbstractTree, Config, Tree, Guard};
/// #
/// # let folder = tempfile::tempdir()?;
/// # let tree = Config::new(folder, Default::default(), Default::default()).open()?;
/// #
/// tree.insert("1", "abc", 0);
/// tree.insert("3", "abc", 1);
/// tree.insert("5", "abc", 2);
///
/// let key = tree.last_key_value(3, None).expect("item should exist").key()?;
/// assert_eq!(&*key, "5".as_bytes());
/// #
/// # Ok::<(), TreeError>(())
/// ```
///
/// # Errors
///
/// Will return `Err` if an IO error occurs.
fn last_key_value(
&self,
seqno: SeqNo,
index: Option<(Arc<Memtable>, SeqNo)>,
) -> Option<IterGuardImpl> {
self.iter(seqno, index).next_back()
}
/// Returns the size of a value if it exists.
///
/// # Examples
///
/// ```
/// # let folder = tempfile::tempdir()?;
/// use lsm_tree::{AbstractTree, Config, Tree};
///
/// let tree = Config::new(folder, Default::default(), Default::default()).open()?;
/// tree.insert("a", "my_value", 0);
///
/// let size = tree.size_of("a", 1)?.unwrap_or_default();
/// assert_eq!("my_value".len() as u32, size);
///
/// let size = tree.size_of("b", 1)?.unwrap_or_default();
/// assert_eq!(0, size);
/// #
/// # Ok::<(), lsm_tree::Error>(())
/// ```
///
/// # Errors
///
/// Will return `Err` if an IO error occurs.
fn size_of<K: AsRef<[u8]>>(&self, key: K, seqno: SeqNo) -> crate::Result<Option<u32>>;
/// Retrieves an item from the tree.
///
/// # Examples
///
/// ```
/// # let folder = tempfile::tempdir()?;
/// use lsm_tree::{AbstractTree, Config, Tree};
///
/// let tree = Config::new(folder, Default::default(), Default::default()).open()?;
/// tree.insert("a", "my_value", 0);
///
/// let item = tree.get("a", 1)?;
/// assert_eq!(Some("my_value".as_bytes().into()), item);
/// #
/// # Ok::<(), lsm_tree::Error>(())
/// ```
///
/// # Errors
///
/// Will return `Err` if an IO error occurs.
fn get<K: AsRef<[u8]>>(&self, key: K, seqno: SeqNo) -> crate::Result<Option<UserValue>>;
/// Retrieves an item from the tree as a [`PinnableSlice`](crate::PinnableSlice).
///
/// When the value is backed by an on-disk data block, implementations
/// may return [`PinnableSlice::Pinned`](crate::PinnableSlice::Pinned) holding a reference to that block's
/// decompressed buffer (avoiding a data copy). Memtable and blob-resolved
/// values use [`PinnableSlice::Owned`](crate::PinnableSlice::Owned). The default implementation always
/// returns `Owned`; only [`Tree`] overrides with the pinned path.
///
/// The existing [`AbstractTree::get`] method is unaffected.
///
/// # Examples
///
/// ```
/// # let folder = tempfile::tempdir()?;
/// use lsm_tree::{AbstractTree, Config, Tree};
///
/// let tree = Config::new(&folder, Default::default(), Default::default()).open()?;
/// tree.insert("a", "my_value", 0);
///
/// let item = tree.get_pinned("a", 1)?;
/// assert_eq!(item.as_ref().map(|v| v.as_ref()), Some("my_value".as_bytes()));
/// #
/// # Ok::<(), lsm_tree::Error>(())
/// ```
///
/// # Errors
///
/// Will return `Err` if an IO error occurs.
fn get_pinned<K: AsRef<[u8]>>(
&self,
key: K,
seqno: SeqNo,
) -> crate::Result<Option<crate::PinnableSlice>> {
// Default: delegate to get() and wrap as Owned
self.get(key, seqno)
.map(|opt| opt.map(crate::PinnableSlice::owned))
}
/// Returns `true` if the tree contains the specified key.
///
/// # Examples
///
/// ```
/// # let folder = tempfile::tempdir()?;
/// # use lsm_tree::{AbstractTree, Config, Tree};
/// #
/// let tree = Config::new(folder, Default::default(), Default::default()).open()?;
/// assert!(!tree.contains_key("a", 0)?);
///
/// tree.insert("a", "abc", 0);
/// assert!(tree.contains_key("a", 1)?);
/// #
/// # Ok::<(), lsm_tree::Error>(())
/// ```
///
/// # Errors
///
/// Will return `Err` if an IO error occurs.
fn contains_key<K: AsRef<[u8]>>(&self, key: K, seqno: SeqNo) -> crate::Result<bool> {
self.get(key, seqno).map(|x| x.is_some())
}
/// Returns `true` if the tree contains any key with the given prefix.
///
/// This is a convenience method that checks whether the corresponding
/// prefix iterator yields at least one item, while surfacing any IO
/// errors via the `Result` return type. Implementations may override
/// this method to provide a more efficient prefix-existence check.
///
/// # Examples
///
/// ```
/// # let folder = tempfile::tempdir()?;
/// use lsm_tree::{AbstractTree, Config, Tree};
///
/// let tree = Config::new(folder, Default::default(), Default::default()).open()?;
/// assert!(!tree.contains_prefix("abc", 0, None)?);
///
/// tree.insert("abc:1", "value", 0);
/// assert!(tree.contains_prefix("abc", 1, None)?);
/// assert!(!tree.contains_prefix("xyz", 1, None)?);
/// #
/// # Ok::<(), lsm_tree::Error>(())
/// ```
///
/// # Errors
///
/// Will return `Err` if an IO error occurs.
fn contains_prefix<K: AsRef<[u8]>>(
&self,
prefix: K,
seqno: SeqNo,
index: Option<(Arc<Memtable>, SeqNo)>,
) -> crate::Result<bool> {
match self.prefix(prefix, seqno, index).next() {
Some(guard) => guard.key().map(|_| true),
None => Ok(false),
}
}
/// Reads multiple keys from the tree.
///
/// Implementations may choose to perform all lookups against a single
/// version snapshot and acquire the version lock only once, which can be
/// more efficient than calling [`AbstractTree::get`] in a loop. The
/// default trait implementation, however, is a convenience wrapper that
/// simply calls [`AbstractTree::get`] for each key and therefore does not
/// guarantee a single-snapshot or single-lock acquisition. Optimized
/// implementations (such as [`Tree`] and [`BlobTree`]) provide the
/// single-snapshot/one-lock behavior.
///
/// # Examples
///
/// ```
/// # let folder = tempfile::tempdir()?;
/// use lsm_tree::{AbstractTree, Config, Tree};
///
/// let tree = Config::new(folder, Default::default(), Default::default()).open()?;
/// tree.insert("a", "value_a", 0);
/// tree.insert("b", "value_b", 1);
///
/// let results = tree.multi_get(["a", "b", "c"], 2)?;
/// assert_eq!(results[0], Some("value_a".as_bytes().into()));
/// assert_eq!(results[1], Some("value_b".as_bytes().into()));
/// assert_eq!(results[2], None);
/// #
/// # Ok::<(), lsm_tree::Error>(())
/// ```
///
/// # Errors
///
/// Will return `Err` if an IO error occurs.
fn multi_get<K: AsRef<[u8]>>(
&self,
keys: impl IntoIterator<Item = K>,
seqno: SeqNo,
) -> crate::Result<Vec<Option<UserValue>>> {
keys.into_iter().map(|key| self.get(key, seqno)).collect()
}
/// Applies a [`WriteBatch`](crate::WriteBatch) with the given sequence number.
///
/// All entries share a single seqno. This is more efficient than individual
/// writes because the version-history lock and memtable size accounting
/// are performed only once for the entire batch.
///
/// **Visibility:** entries become individually visible to concurrent readers
/// as they are inserted. For atomic batch visibility, the caller must
/// publish `seqno` (via `visible_seqno.fetch_max(seqno + 1)`) only
/// **after** this method returns.
///
/// Returns the total bytes added and new size of the memtable.
///
/// # Examples
///
/// ```
/// # let folder = tempfile::tempdir()?;
/// use lsm_tree::{AbstractTree, Config, Tree, WriteBatch};
///
/// let tree = Config::new(&folder, Default::default(), Default::default()).open()?;
///
/// let mut batch = WriteBatch::new();
/// batch.insert("key1", "value1");
/// batch.insert("key2", "value2");
/// batch.remove("key3");
///
/// let (bytes_added, memtable_size) = tree.apply_batch(batch, 0)?;
/// assert!(bytes_added > 0);
/// #
/// # Ok::<(), lsm_tree::Error>(())
/// ```
///
/// # Errors
///
/// Returns [`Error::MixedOperationBatch`](crate::Error::MixedOperationBatch)
/// if the batch contains mixed operation types for the same user key.
fn apply_batch(&self, batch: crate::WriteBatch, seqno: SeqNo) -> crate::Result<(u64, u64)>;
/// Inserts a key-value pair into the tree.
///
/// If the key already exists, the item will be overwritten.
///
/// Returns the added item's size and new size of the memtable.
///
/// # Examples
///
/// ```
/// # let folder = tempfile::tempdir()?;
/// use lsm_tree::{AbstractTree, Config, Tree};
///
/// let tree = Config::new(folder, Default::default(), Default::default()).open()?;
/// tree.insert("a", "abc", 0);
/// #
/// # Ok::<(), lsm_tree::Error>(())
/// ```
///
/// # Errors
///
/// Will return `Err` if an IO error occurs.
fn insert<K: Into<UserKey>, V: Into<UserValue>>(
&self,
key: K,
value: V,
seqno: SeqNo,
) -> (u64, u64);
/// Admission-gated [`insert`](Self::insert): consults
/// [`write_admission`](Self::write_admission) first and declines with
/// [`Error::StorageFull`](crate::Error::StorageFull) when the tree is over
/// budget, otherwise inserts and returns the same `(added_bytes,
/// memtable_size)` tuple. This is the write entry a space-aware caller uses
/// so over-budget writes are refused up front rather than failing a flush
/// later; bare [`insert`](Self::insert) stays infallible for callers that
/// do not opt into admission control.
///
/// # Errors
///
/// [`Error::StorageFull`](crate::Error::StorageFull) when the admission gate
/// is closed.
fn try_insert<K: Into<UserKey>, V: Into<UserValue>>(
&self,
key: K,
value: V,
seqno: SeqNo,
) -> crate::Result<(u64, u64)> {
self.write_admission()?;
Ok(self.insert(key, value, seqno))
}
/// Admission-gated [`merge`](Self::merge). See
/// [`try_insert`](Self::try_insert).
///
/// # Errors
///
/// [`Error::StorageFull`](crate::Error::StorageFull) when the admission gate
/// is closed.
fn try_merge<K: Into<UserKey>, V: Into<UserValue>>(
&self,
key: K,
operand: V,
seqno: SeqNo,
) -> crate::Result<(u64, u64)> {
self.write_admission()?;
Ok(self.merge(key, operand, seqno))
}
/// Admission-gated [`remove`](Self::remove). See
/// [`try_insert`](Self::try_insert).
///
/// Note a tombstone is itself a write that consumes space; an over-budget
/// tree refuses it too. Reclaim space via compaction (never gated) rather
/// than relying on deletes when already read-only.
///
/// # Errors
///
/// [`Error::StorageFull`](crate::Error::StorageFull) when the admission gate
/// is closed.
fn try_remove<K: Into<UserKey>>(&self, key: K, seqno: SeqNo) -> crate::Result<(u64, u64)> {
self.write_admission()?;
Ok(self.remove(key, seqno))
}
/// Admission-gated [`remove_weak`](Self::remove_weak). See
/// [`try_insert`](Self::try_insert).
///
/// # Errors
///
/// [`Error::StorageFull`](crate::Error::StorageFull) when the admission gate
/// is closed.
fn try_remove_weak<K: Into<UserKey>>(&self, key: K, seqno: SeqNo) -> crate::Result<(u64, u64)> {
self.write_admission()?;
Ok(self.remove_weak(key, seqno))
}
/// Admission-gated [`remove_range`](Self::remove_range). See
/// [`try_insert`](Self::try_insert).
///
/// # Errors
///
/// [`Error::StorageFull`](crate::Error::StorageFull) when the admission gate
/// is closed.
fn try_remove_range<K: Into<UserKey>>(
&self,
start: K,
end: K,
seqno: SeqNo,
) -> crate::Result<u64> {
self.write_admission()?;
Ok(self.remove_range(start, end, seqno))
}
/// Removes an item from the tree.
///
/// Returns the added item's size and new size of the memtable.
///
/// # Examples
///
/// ```
/// # let folder = tempfile::tempdir()?;
/// # use lsm_tree::{AbstractTree, Config, Tree};
/// #
/// # let tree = Config::new(folder, Default::default(), Default::default()).open()?;
/// tree.insert("a", "abc", 0);
///
/// let item = tree.get("a", 1)?.expect("should have item");
/// assert_eq!("abc".as_bytes(), &*item);
///
/// tree.remove("a", 1);
///
/// let item = tree.get("a", 2)?;
/// assert_eq!(None, item);
/// #
/// # Ok::<(), lsm_tree::Error>(())
/// ```
///
/// # Errors
///
/// Will return `Err` if an IO error occurs.
fn remove<K: Into<UserKey>>(&self, key: K, seqno: SeqNo) -> (u64, u64);
/// Writes a merge operand for a key.
///
/// The operand is stored as a partial update that will be combined with
/// other operands and/or a base value via the configured [`crate::MergeOperator`]
/// during reads and compaction.
///
/// Returns the added item's size and new size of the memtable.
///
/// # Examples
///
/// ```
/// # let folder = tempfile::tempdir()?;
/// # use lsm_tree::{AbstractTree, Config, MergeOperator, UserValue};
/// # use std::sync::Arc;
/// # struct SumMerge;
/// # impl MergeOperator for SumMerge {
/// # fn merge(&self, _key: &[u8], base: Option<&[u8]>, operands: &[&[u8]]) -> lsm_tree::Result<UserValue> {
/// # let mut sum: i64 = base.map_or(0, |b| i64::from_le_bytes(b.try_into().unwrap()));
/// # for op in operands { sum += i64::from_le_bytes((*op).try_into().unwrap()); }
/// # Ok(sum.to_le_bytes().to_vec().into())
/// # }
/// # }
/// # let tree = Config::new(folder, Default::default(), Default::default())
/// # .with_merge_operator(Some(Arc::new(SumMerge)))
/// # .open()?;
/// tree.merge("counter", 1_i64.to_le_bytes(), 0);
/// # Ok::<(), lsm_tree::Error>(())
/// ```
fn merge<K: Into<UserKey>, V: Into<UserValue>>(
&self,
key: K,
operand: V,
seqno: SeqNo,
) -> (u64, u64);
/// Removes an item from the tree.
///
/// The tombstone marker of this delete operation will vanish when it
/// collides with its corresponding insertion.
/// This may cause older versions of the value to be resurrected, so it should
/// only be used and preferred in scenarios where a key is only ever written once.
///
/// Returns the added item's size and new size of the memtable.
///
/// # Examples
///
/// ```
/// # let folder = tempfile::tempdir()?;
/// # use lsm_tree::{AbstractTree, Config, Tree};
/// #
/// # let tree = Config::new(folder, Default::default(), Default::default()).open()?;
/// tree.insert("a", "abc", 0);
///
/// let item = tree.get("a", 1)?.expect("should have item");
/// assert_eq!("abc".as_bytes(), &*item);
///
/// tree.remove_weak("a", 1);
///
/// let item = tree.get("a", 2)?;
/// assert_eq!(None, item);
/// #
/// # Ok::<(), lsm_tree::Error>(())
/// ```
///
/// # Errors
///
/// Will return `Err` if an IO error occurs.
#[doc(hidden)]
fn remove_weak<K: Into<UserKey>>(&self, key: K, seqno: SeqNo) -> (u64, u64);
/// Deletes all keys in the range `[start, end)` by inserting a range tombstone.
///
/// This is much more efficient than deleting keys individually when
/// removing a contiguous range of keys.
///
/// Returns the approximate size added to the memtable.
/// Returns 0 if `start >= end` (invalid interval is silently ignored).
///
/// This is a required method on the crate's sealed tree types.
fn remove_range<K: Into<UserKey>>(&self, start: K, end: K, seqno: SeqNo) -> u64;
/// Deletes all keys with the given prefix by inserting a range tombstone.
///
/// This is sugar over [`AbstractTree::remove_range`] using prefix bounds.
///
/// Returns the approximate size added to the memtable.
/// Returns 0 for empty prefixes or all-`0xFF` prefixes (cannot form valid half-open range).
fn remove_prefix<K: AsRef<[u8]>>(&self, prefix: K, seqno: SeqNo) -> u64 {
use crate::range::prefix_to_range;
use core::ops::Bound;
let (lo, hi) = prefix_to_range(prefix.as_ref());
let Bound::Included(start) = lo else { return 0 };
// Bound::Unbounded means the prefix is all 0xFF — no representable
// exclusive upper bound exists, so we cannot form a valid range tombstone.
let Bound::Excluded(end) = hi else { return 0 };
self.remove_range(start, end, seqno)
}
}