Skip to main content

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