lsm_tree/config/
filter.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
5pub use crate::segment::filter::BloomConstructionPolicy;
6
7/// Filter policy entry
8///
9/// Each level can be configured with a different filter type and bits per key
10#[derive(Copy, Debug, Clone, PartialEq)]
11pub enum FilterPolicyEntry {
12    /// Skip filter construction
13    None,
14
15    /// Standard bloom filter with K bits per key
16    Bloom(BloomConstructionPolicy),
17}
18
19/// Filter policy
20#[derive(Debug, Clone, PartialEq)]
21pub struct FilterPolicy(Vec<FilterPolicyEntry>);
22
23impl std::ops::Deref for FilterPolicy {
24    type Target = [FilterPolicyEntry];
25
26    fn deref(&self) -> &Self::Target {
27        &self.0
28    }
29}
30
31// TODO: remove default
32impl Default for FilterPolicy {
33    fn default() -> Self {
34        Self::new(&[FilterPolicyEntry::Bloom(
35            BloomConstructionPolicy::BitsPerKey(10.0),
36        )])
37    }
38}
39
40impl FilterPolicy {
41    pub(crate) fn get(&self, level: usize) -> FilterPolicyEntry {
42        self.0
43            .get(level)
44            .copied()
45            .unwrap_or_else(|| self.last().copied().expect("policy should not be empty"))
46    }
47
48    /// Disables all filters.
49    ///
50    /// **Not recommended unless you know what you are doing!**
51    #[must_use]
52    pub fn disabled() -> Self {
53        Self::all(FilterPolicyEntry::None)
54    }
55
56    // TODO: accept Vec... Into<Vec<...>>? or owned
57
58    /// Uses the same block size in every level.
59    #[must_use]
60    pub fn all(c: FilterPolicyEntry) -> Self {
61        Self(vec![c])
62    }
63
64    /// Constructs a custom block size policy.
65    #[must_use]
66    pub fn new(policy: &[FilterPolicyEntry]) -> Self {
67        assert!(!policy.is_empty(), "compression policy may not be empty");
68        assert!(policy.len() <= 255, "compression policy is too large");
69        Self(policy.into())
70    }
71}