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