Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

a lot of new tests to check correctness, refacoring to be able to add… #2

Merged
merged 2 commits into from
Sep 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 54 additions & 0 deletions src/backends.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
// use bitvec::prelude::*;
use std::time::SystemTime;

// Trait for the storage backend
pub trait BloomFilterStorage {
fn new(capacity: usize, max_levels: usize) -> Self;
fn set_bit(&mut self, level: usize, index: usize);
fn get_bit(&self, level: usize, index: usize) -> bool;
fn clear_level(&mut self, level: usize);
fn set_timestamp(&mut self, level: usize, timestamp: SystemTime);
fn get_timestamp(&self, level: usize) -> Option<SystemTime>;
fn num_levels(&self) -> usize;
}

// In-memory storage implementation
pub struct InMemoryStorage {
pub levels: Vec<Vec<bool>>,
timestamps: Vec<SystemTime>,
capacity: usize,
}

impl BloomFilterStorage for InMemoryStorage {
fn new(capacity: usize, max_levels: usize) -> Self {
Self {
levels: vec![vec![false; capacity]; max_levels],
timestamps: vec![SystemTime::now(); max_levels],
capacity,
}
}

fn set_bit(&mut self, level: usize, index: usize) {
self.levels[level][index] = true;
}

fn get_bit(&self, level: usize, index: usize) -> bool {
self.levels[level][index]
}

fn clear_level(&mut self, level: usize) {
self.levels[level] = vec![false; self.capacity];
}

fn set_timestamp(&mut self, level: usize, timestamp: SystemTime) {
self.timestamps[level] = timestamp;
}

fn get_timestamp(&self, level: usize) -> Option<SystemTime> {
Some(self.timestamps[level])
}

fn num_levels(&self) -> usize {
self.levels.len()
}
}
Loading
Loading