lsm_tree/compaction/mod.rs
1// Copyright (c) 2024-present, fjall-rs
2// This source code is licensed under both the Apache 2.0 and MIT License
3// (found in the LICENSE-* files in the repository)
4
5//! Contains compaction strategies
6
7pub(crate) mod fifo;
8pub(crate) mod leveled;
9// pub(crate) mod maintenance;
10pub(crate) mod drop_range;
11mod flavour;
12pub(crate) mod major;
13pub(crate) mod movedown;
14pub(crate) mod pulldown;
15pub(crate) mod state;
16pub(crate) mod stream;
17pub(crate) mod tiered;
18pub(crate) mod worker;
19
20pub use fifo::Strategy as Fifo;
21pub use leveled::Strategy as Leveled;
22pub use tiered::Strategy as SizeTiered;
23
24use crate::{
25 compaction::state::CompactionState, config::Config, version::Version, HashSet, KvPair, TableId,
26};
27
28/// Alias for `Leveled`
29pub type Levelled = Leveled;
30
31#[doc(hidden)]
32pub use movedown::Strategy as MoveDown;
33
34#[doc(hidden)]
35pub use pulldown::Strategy as PullDown;
36
37/// Input for compactor.
38///
39/// The compaction strategy chooses which tables to compact and how.
40/// That information is given to the compactor.
41#[derive(Debug, Eq, PartialEq)]
42pub struct Input {
43 /// Tables to compact
44 pub table_ids: HashSet<TableId>,
45
46 /// Level to put the created tables into
47 pub dest_level: u8,
48
49 /// The logical level the tables are part of
50 pub canonical_level: u8,
51
52 /// Table target size
53 ///
54 /// If a table merge reaches the size threshold, a new table is started.
55 /// This results in a sorted "run" of tables.
56 pub target_size: u64,
57}
58
59/// Describes what to do (compact or not)
60#[derive(Debug, Eq, PartialEq)]
61pub enum Choice {
62 /// Just do nothing.
63 DoNothing,
64
65 /// Moves tables into another level without rewriting.
66 Move(Input),
67
68 /// Compacts some tables into a new level.
69 Merge(Input),
70
71 /// Delete tables without doing compaction.
72 ///
73 /// This may be used by a compaction strategy that wants to delete old data
74 /// without having to compact it away, like [`fifo::Strategy`].
75 Drop(HashSet<TableId>),
76}
77
78/// Trait for a compaction strategy
79///
80/// The strategy receives the levels of the LSM-tree as argument
81/// and emits a choice on what to do.
82#[expect(clippy::module_name_repetitions)]
83pub trait CompactionStrategy {
84 // TODO: could be : Display instead
85 /// Gets the compaction strategy name.
86 fn get_name(&self) -> &'static str;
87
88 #[doc(hidden)]
89 fn get_config(&self) -> Vec<KvPair> {
90 vec![]
91 }
92
93 /// Decides on what to do based on the current state of the LSM-tree's levels
94 fn choose(&self, version: &Version, config: &Config, state: &CompactionState) -> Choice;
95}