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        self.0
22            .get(level)
23            .copied()
24            .unwrap_or_else(|| self.last().copied().expect("policy should not be empty"))
25    }
26
27    /// Disables all compression.
28    #[must_use]
29    pub fn disabled() -> Self {
30        Self::all(CompressionType::None)
31    }
32
33    /// Uses the same compression in every level.
34    #[must_use]
35    pub fn all(c: CompressionType) -> Self {
36        Self(vec![c])
37    }
38
39    /// Constructs a custom compression policy.
40    #[must_use]
41    pub fn new(policy: impl Into<Vec<CompressionType>>) -> Self {
42        let policy = policy.into();
43        assert!(!policy.is_empty(), "compression policy may not be empty");
44        assert!(policy.len() <= 255, "compression policy is too large");
45        Self(policy)
46    }
47}