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    // TODO: accept Vec... Into<Vec<...>>? or owned
26
27    /// Uses the same block size in every level.
28    #[must_use]
29    pub fn all(c: bool) -> Self {
30        Self(vec![c])
31    }
32
33    /// Constructs a custom block size policy.
34    #[must_use]
35    pub fn new(policy: &[bool]) -> Self {
36        assert!(!policy.is_empty(), "compression policy may not be empty");
37        assert!(policy.len() <= 255, "compression policy is too large");
38        Self(policy.into())
39    }
40}