Skip to content

Commit

Permalink
finish w1/d7, Task 1: Bloom Filters
Browse files Browse the repository at this point in the history
  • Loading branch information
AbnerZheng committed May 17, 2024
1 parent 7134080 commit 659b7d2
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 7 deletions.
20 changes: 17 additions & 3 deletions mini-lsm-starter/src/table/bloom.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

use anyhow::Result;
use bytes::{BufMut, Bytes, BytesMut};
use log::warn;

/// Implements a bloom filter
pub struct Bloom {
Expand Down Expand Up @@ -79,7 +80,14 @@ impl Bloom {
let mut filter = BytesMut::with_capacity(nbytes);
filter.resize(nbytes, 0);

// TODO: build the bloom filter
keys.iter().for_each(|h| {
let mut h = *h;
let delta = (h >> 17) | (h << 15);
for _ in 0..k {
h = h.wrapping_add(delta);
filter.set_bit(h as usize % nbits, true);
}
});

Self {
filter: filter.freeze(),
Expand All @@ -88,15 +96,21 @@ impl Bloom {
}

/// Check if a bloom filter may contain some data
pub fn may_contain(&self, h: u32) -> bool {
pub fn may_contain(&self, mut h: u32) -> bool {
if self.k > 30 {
// potential new encoding for short bloom filters
true
} else {
let nbits = self.filter.bit_len();
let delta = (h >> 17) | (h << 15);

// TODO: probe the bloom filter
for _ in 0..self.k {
h = h.wrapping_add(delta);
let x = self.filter.get_bit(h as usize % nbits);
if !x {
return false;
}
}

true
}
Expand Down
8 changes: 4 additions & 4 deletions mini-lsm-starter/src/tests.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
//! DO NOT MODIFY -- Mini-LSM tests modules
//! This file will be automatically rewritten by the copy-test command.
mod week1_day6;
mod harness;
mod week1_day1;
mod week1_day2;
mod week1_day3;
mod week1_day7;
mod harness;
mod week1_day4;
mod week1_day5;
mod week1_day1;
mod week1_day6;
mod week1_day7;

0 comments on commit 659b7d2

Please sign in to comment.