mindb 0.1.2

Lightweight embedded key–value store with write-ahead log and zstd compression.
Documentation
//! Lightweight bloom-style filter used by segments to skip cold lookups.
#![allow(dead_code)]

use std::collections::HashSet;

/// Simplified bloom filter backed by a hash set. While not space optimal,
/// it is sufficient for the in-repo prototype and keeps the API focused on
/// containment checks.
#[derive(Clone, Debug, Default)]
pub struct BloomFilter {
    entries: HashSet<Vec<u8>>,
}

impl BloomFilter {
    /// Constructs an empty bloom filter.
    pub fn new() -> Self {
        Self {
            entries: HashSet::new(),
        }
    }

    /// Populates a bloom filter with the provided keys.
    pub fn from_keys<I, K>(keys: I) -> Self
    where
        I: IntoIterator<Item = K>,
        K: Into<Vec<u8>>,
    {
        let mut filter = Self::new();
        for key in keys {
            filter.insert(key);
        }
        filter
    }

    /// Inserts a new key into the filter.
    pub fn insert(&mut self, key: impl Into<Vec<u8>>) {
        self.entries.insert(key.into());
    }

    /// Checks whether the key may be contained in the filter.
    pub fn may_contain(&self, key: &[u8]) -> bool {
        self.entries.contains(key)
    }

    /// Returns the number of distinct keys tracked by the filter.
    pub fn len(&self) -> usize {
        self.entries.len()
    }
}