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;
9pub(crate) mod maintenance;
10pub(crate) mod major;
11pub(crate) mod pulldown;
12pub(crate) mod stream;
13pub(crate) mod tiered;
14pub(crate) mod worker;
15
16pub use fifo::Strategy as Fifo;
17pub use leveled::Strategy as Leveled;
18pub use tiered::Strategy as SizeTiered;
19
20use crate::{config::Config, level_manifest::LevelManifest, segment::meta::SegmentId, HashSet};
21
22/// Alias for `Leveled`
23pub type Levelled = Leveled;
24
25#[doc(hidden)]
26pub use pulldown::Strategy as PullDown;
27
28/// Input for compactor.
29///
30/// The compaction strategy chooses which segments to compact and how.
31/// That information is given to the compactor.
32#[derive(Debug, Eq, PartialEq)]
33pub struct Input {
34 /// Segments to compact
35 pub segment_ids: HashSet<SegmentId>,
36
37 /// Level to put the created segments into
38 pub dest_level: u8,
39
40 /// Segment target size
41 ///
42 /// If a segment compaction reaches the level, a new segment is started.
43 /// This results in a sorted "run" of segments
44 pub target_size: u64,
45}
46
47/// Describes what to do (compact or not)
48#[derive(Debug, Eq, PartialEq)]
49pub enum Choice {
50 /// Just do nothing.
51 DoNothing,
52
53 /// Moves segments into another level without rewriting.
54 Move(Input),
55
56 /// Compacts some segments into a new level.
57 Merge(Input),
58
59 /// Delete segments without doing compaction.
60 ///
61 /// This may be used by a compaction strategy that wants to delete old data
62 /// without having to compact it away, like [`fifo::Strategy`].
63 Drop(HashSet<SegmentId>),
64}
65
66/// Trait for a compaction strategy
67///
68/// The strategy receives the levels of the LSM-tree as argument
69/// and emits a choice on what to do.
70#[allow(clippy::module_name_repetitions)]
71pub trait CompactionStrategy {
72 // TODO: could be : Display instead
73 /// Gets the compaction strategy name.
74 fn get_name(&self) -> &'static str;
75
76 /// Decides on what to do based on the current state of the LSM-tree's levels
77 fn choose(&self, _: &LevelManifest, config: &Config) -> Choice;
78}