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
17// TODO: remove default
18impl Default for BlockSizePolicy {
19    fn default() -> Self {
20        Self::new(&[4 * 1_024])
21    }
22}
23
24impl BlockSizePolicy {
25    pub(crate) fn get(&self, level: usize) -> u32 {
26        self.0
27            .get(level)
28            .copied()
29            .unwrap_or_else(|| self.last().copied().expect("policy should not be empty"))
30    }
31
32    // TODO: accept Vec... Into<Vec<...>>? or owned
33
34    /// Uses the same block size in every level.
35    #[must_use]
36    pub fn all(c: u32) -> Self {
37        Self(vec![c])
38    }
39
40    /// Constructs a custom block size policy.
41    #[must_use]
42    pub fn new(policy: &[u32]) -> Self {
43        assert!(!policy.is_empty(), "compression policy may not be empty");
44        assert!(policy.len() <= 255, "compression policy is too large");
45        Self(policy.into())
46    }
47}