lsm_tree/segment/filter/
mod.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 mod bit_array;
6pub mod blocked_bloom;
7pub mod standard_bloom;
8
9use standard_bloom::Builder as StandardBloomFilterBuilder;
10
11#[derive(Copy, Clone, Debug, PartialEq)]
12pub enum BloomConstructionPolicy {
13    BitsPerKey(f32),
14    FpRate(f32), // TODO: 3.0.0 rename: FalsePositiveRate?
15}
16
17impl Default for BloomConstructionPolicy {
18    fn default() -> Self {
19        Self::BitsPerKey(10.0)
20    }
21}
22
23impl BloomConstructionPolicy {
24    #[must_use]
25    pub fn init(&self, n: usize) -> StandardBloomFilterBuilder {
26        use standard_bloom::Builder;
27
28        match self {
29            Self::BitsPerKey(bpk) => Builder::with_bpk(n, *bpk),
30            Self::FpRate(fpr) => Builder::with_fp_rate(n, *fpr),
31        }
32    }
33
34    #[must_use]
35    pub fn is_active(&self) -> bool {
36        match self {
37            Self::BitsPerKey(bpk) => *bpk > 0.0,
38            Self::FpRate(fpr) => *fpr > 0.0,
39        }
40    }
41}
42
43#[derive(Copy, Clone, PartialEq, Eq, Debug)]
44enum FilterType {
45    StandardBloom,
46    BlockedBloom,
47}
48
49impl TryFrom<u8> for FilterType {
50    type Error = crate::Error;
51
52    fn try_from(value: u8) -> Result<Self, Self::Error> {
53        match value {
54            0 => Ok(Self::StandardBloom),
55            1 => Ok(Self::BlockedBloom),
56            _ => Err(crate::Error::Decode(crate::DecodeError::InvalidTag((
57                "FilterType",
58                value,
59            )))),
60        }
61    }
62}
63
64impl From<FilterType> for u8 {
65    fn from(value: FilterType) -> Self {
66        match value {
67            FilterType::StandardBloom => 0,
68            FilterType::BlockedBloom => 1,
69        }
70    }
71}