lsm_tree/config/
hash_ratio.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/// Hash ratio policy
6#[derive(Debug, Clone, PartialEq)]
7pub struct HashRatioPolicy(Vec<f32>);
8
9impl std::ops::Deref for HashRatioPolicy {
10    type Target = [f32];
11
12    fn deref(&self) -> &Self::Target {
13        &self.0
14    }
15}
16
17impl HashRatioPolicy {
18    pub(crate) fn get(&self, level: usize) -> f32 {
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: f32) -> 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<f32>>) -> 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}