|
1 | | -use crate::bloom; |
2 | 1 | use crate::error::{Error, Result}; |
| 2 | +use bloomfilter::Bloom; |
3 | 3 | use flate2::{read::GzDecoder, write::GzEncoder, Compression}; |
4 | 4 | use serde::Deserialize; |
5 | 5 | use serde::{de::DeserializeOwned, Serialize}; |
@@ -55,13 +55,13 @@ impl Storage { |
55 | 55 | where |
56 | 56 | T: Sync + Send + Clone + Debug + Serialize + 'static, |
57 | 57 | { |
58 | | - if self.filter.contains(&tuple.key) { |
| 58 | + if self.contains(&tuple.key) { |
59 | 59 | return Err(Error::AlreadyExists(format!("Key: {}", tuple.key))); |
60 | 60 | } |
61 | 61 |
|
62 | 62 | let segment = Storage::serialize_value(&tuple.value); |
63 | 63 |
|
64 | | - self.filter.insert(&tuple.key); |
| 64 | + self.filter.insert(tuple.key.to_string()); |
65 | 65 | let offset = self.file.len().unwrap(); |
66 | 66 |
|
67 | 67 | let index_entry = IndexEntry { |
@@ -118,7 +118,6 @@ impl Storage { |
118 | 118 | return Err(Error::NotFound(format!("Key: {}", key))); |
119 | 119 | } |
120 | 120 |
|
121 | | - self.filter.remove(&key); |
122 | 121 | let entry = self.index.remove(key).unwrap(); |
123 | 122 |
|
124 | 123 | let old_value = self |
@@ -167,14 +166,13 @@ impl Storage { |
167 | 166 | } |
168 | 167 |
|
169 | 168 | pub fn clear(&mut self) -> Result<()> { |
170 | | - self.filter.clear(); |
171 | 169 | self.index.clear(); |
172 | 170 |
|
173 | 171 | Ok(()) |
174 | 172 | } |
175 | 173 |
|
176 | 174 | pub fn contains(&self, key: &str) -> bool { |
177 | | - self.filter.contains(key) |
| 175 | + self.filter.contains(key.to_string()) |
178 | 176 | } |
179 | 177 |
|
180 | 178 | fn serialize_value<T>(value: &T) -> Vec<u8> |
@@ -312,7 +310,7 @@ impl Index { |
312 | 310 | .read(true) |
313 | 311 | .write(true) |
314 | 312 | .create(true) |
315 | | - .open(index_path.clone()) |
| 313 | + .open(&index_path) |
316 | 314 | .map_err(Error::IoError)?; |
317 | 315 |
|
318 | 316 | let inner = if file.metadata().unwrap().len() == 0 { |
@@ -394,33 +392,27 @@ impl Drop for Index { |
394 | 392 |
|
395 | 393 | #[derive(Debug)] |
396 | 394 | struct Filter { |
397 | | - bloom: bloom::BloomFilter, |
| 395 | + bloom: Bloom<String>, |
398 | 396 | } |
399 | 397 |
|
| 398 | +const BLOOM_FILTER_SIZE: usize = 100_000; |
| 399 | + |
400 | 400 | impl Filter { |
401 | 401 | pub fn new(keys: Vec<String>) -> Self { |
402 | | - let mut bloom = bloom::BloomFilter::new(0.01, (keys.len() + 50) * 8); |
| 402 | + let mut bloom = Bloom::new_for_fp_rate(BLOOM_FILTER_SIZE, 0.01); |
403 | 403 |
|
404 | 404 | for key in keys { |
405 | | - bloom.insert(&key); |
| 405 | + bloom.set(&key); |
406 | 406 | } |
407 | 407 |
|
408 | 408 | Self { bloom } |
409 | 409 | } |
410 | 410 |
|
411 | | - pub fn insert(&mut self, key: &str) { |
412 | | - self.bloom.insert(key); |
413 | | - } |
414 | | - |
415 | | - pub fn contains(&self, key: &str) -> bool { |
416 | | - self.bloom.contains(key) |
| 411 | + pub fn insert(&mut self, key: String) { |
| 412 | + self.bloom.set(&key); |
417 | 413 | } |
418 | 414 |
|
419 | | - pub fn remove(&mut self, key: &str) { |
420 | | - self.bloom.remove(key); |
421 | | - } |
422 | | - |
423 | | - pub fn clear(&mut self) { |
424 | | - self.bloom.clear(); |
| 415 | + pub fn contains(&self, key: String) -> bool { |
| 416 | + self.bloom.check(&key) |
425 | 417 | } |
426 | 418 | } |
0 commit comments