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