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