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