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
// Copyright (c) 2024-present, fjall-rs
// This source code is licensed under both the Apache 2.0 and MIT License
// (found in the LICENSE-* files in the repository)
use crate::{
AnyTree, BlobTree, Config, Guard, InternalValue, KvPair, Memtable, SeqNo, TableId, Tree,
UserKey, UserValue, iter_guard::IterGuardImpl, table::Table, version::Version, vlog::BlobFile,
};
use std::{
ops::RangeBounds,
sync::{Arc, MutexGuard, RwLockWriteGuard},
};
pub type RangeItem = crate::Result<KvPair>;
type FlushToTablesResult = (Vec<Table>, Option<Vec<BlobFile>>);
// 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;
#[doc(hidden)]
fn get_version_history_lock(&self) -> RwLockWriteGuard<'_, crate::version::SuperVersions>;
/// 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
///
/// Will return `Err` if an IO error occurs.
fn flush(
&self,
_lock: &MutexGuard<'_, ()>,
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();
// 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 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.
///
/// # 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>;
/// Acquires the flush lock which is required to call [`Tree::flush`].
fn get_flush_lock(&self) -> MutexGuard<'_, ()>;
/// 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;
/// 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;
/// 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`].
///
/// When the value is backed by an on-disk data block, implementations
/// may return [`PinnableSlice::Pinned`] holding a reference to that block's
/// decompressed buffer (avoiding a data copy). Memtable and blob-resolved
/// values use [`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`] 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);
/// 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 std::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)
}
}