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