lsm_tree/config/
compression.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
5use crate::CompressionType;
6
7/// Compression policy
8#[derive(Debug, Clone, Eq, PartialEq)]
9pub struct CompressionPolicy(Vec<CompressionType>);
10
11impl std::ops::Deref for CompressionPolicy {
12    type Target = [CompressionType];
13
14    fn deref(&self) -> &Self::Target {
15        &self.0
16    }
17}
18
19impl CompressionPolicy {
20    pub(crate) fn get(&self, level: usize) -> CompressionType {
21        #[expect(clippy::expect_used, reason = "policy is expected not to be empty")]
22        self.0
23            .get(level)
24            .copied()
25            .unwrap_or_else(|| self.last().copied().expect("policy should not be empty"))
26    }
27
28    /// Disables all compression.
29    #[must_use]
30    pub fn disabled() -> Self {
31        Self::all(CompressionType::None)
32    }
33
34    /// Uses the same compression in every level.
35    #[must_use]
36    pub fn all(c: CompressionType) -> Self {
37        Self(vec![c])
38    }
39
40    /// Constructs a custom compression policy.
41    ///
42    /// # Panics
43    ///
44    /// Panics if the policy is empty or contains more than 255 elements.
45    #[must_use]
46    pub fn new(policy: impl Into<Vec<CompressionType>>) -> Self {
47        let policy = policy.into();
48        assert!(!policy.is_empty(), "compression policy may not be empty");
49        assert!(policy.len() <= 255, "compression policy is too large");
50        Self(policy)
51    }
52}