-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
feat: add cuckoofilter
to nippy-jar
#4533
Changes from all commits
ee61a13
2434f09
daddaef
13a1d11
97a76f9
a2e7851
bb162e3
72cc9b6
b11edfe
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
use super::Filter; | ||
use crate::NippyJarError; | ||
use cuckoofilter::{self, CuckooFilter, ExportedCuckooFilter}; | ||
use serde::{Deserialize, Deserializer, Serialize, Serializer}; | ||
use std::collections::hash_map::DefaultHasher; | ||
|
||
/// [CuckooFilter](https://www.cs.cmu.edu/~dga/papers/cuckoo-conext2014.pdf). It builds and provides an approximated set-membership filter to answer queries such as "Does this element belong to this set?". Has a theoretical 3% false positive rate. | ||
pub struct Cuckoo { | ||
/// Remaining number of elements that can be added. | ||
/// | ||
/// This is necessary because the inner implementation will fail on adding an element past capacity, **but it will still add it and remove other**: [source](https://github.com/axiomhq/rust-cuckoofilter/tree/624da891bed1dd5d002c8fa92ce0dcd301975561#notes--todos) | ||
remaining: usize, | ||
|
||
/// CuckooFilter. | ||
filter: CuckooFilter<DefaultHasher>, // TODO does it need an actual hasher? | ||
} | ||
|
||
impl Cuckoo { | ||
pub fn new(max_capacity: usize) -> Self { | ||
Cuckoo { remaining: max_capacity, filter: CuckooFilter::with_capacity(max_capacity) } | ||
} | ||
} | ||
|
||
impl Filter for Cuckoo { | ||
fn add(&mut self, element: &[u8]) -> Result<(), NippyJarError> { | ||
if self.remaining == 0 { | ||
return Err(NippyJarError::FilterMaxCapacity) | ||
} | ||
|
||
self.remaining -= 1; | ||
|
||
Ok(self.filter.add(element)?) | ||
} | ||
|
||
fn contains(&self, element: &[u8]) -> Result<bool, NippyJarError> { | ||
Ok(self.filter.contains(element)) | ||
} | ||
} | ||
|
||
impl std::fmt::Debug for Cuckoo { | ||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
f.debug_struct("Cuckoo") | ||
.field("remaining", &self.remaining) | ||
.field("filter_size", &self.filter.memory_usage()) | ||
.finish_non_exhaustive() | ||
} | ||
} | ||
|
||
impl PartialEq for Cuckoo { | ||
fn eq(&self, _other: &Self) -> bool { | ||
self.remaining == _other.remaining && | ||
{ | ||
#[cfg(not(test))] | ||
{ | ||
unimplemented!("No way to figure it out without exporting (expensive), so only allow direct comparison on a test") | ||
} | ||
#[cfg(test)] | ||
{ | ||
let f1 = self.filter.export(); | ||
let f2 = _other.filter.export(); | ||
return f1.length == f2.length && f1.values == f2.values | ||
} | ||
} | ||
} | ||
} | ||
|
||
impl<'de> Deserialize<'de> for Cuckoo { | ||
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> | ||
where | ||
D: Deserializer<'de>, | ||
{ | ||
let (remaining, exported): (usize, ExportedCuckooFilter) = | ||
Deserialize::deserialize(deserializer)?; | ||
|
||
Ok(Cuckoo { remaining, filter: exported.into() }) | ||
} | ||
} | ||
|
||
impl Serialize for Cuckoo { | ||
/// Potentially expensive, but should be used only when creating the file. | ||
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> | ||
where | ||
S: Serializer, | ||
{ | ||
(self.remaining, self.filter.export()).serialize(serializer) | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
use crate::NippyJarError; | ||
use serde::{Deserialize, Serialize}; | ||
|
||
mod cuckoo; | ||
pub use cuckoo::Cuckoo; | ||
|
||
pub trait Filter { | ||
/// Add element to the inclusion list. | ||
fn add(&mut self, element: &[u8]) -> Result<(), NippyJarError>; | ||
|
||
/// Checks if the element belongs to the inclusion list. There might be false positives. | ||
fn contains(&self, element: &[u8]) -> Result<bool, NippyJarError>; | ||
} | ||
|
||
#[derive(Debug, Serialize, Deserialize, PartialEq)] | ||
pub enum Filters { | ||
Cuckoo(Cuckoo), | ||
// Avoids irrefutable let errors. Remove this after adding another one. | ||
Unused, | ||
} | ||
|
||
impl Filter for Filters { | ||
Comment on lines
+15
to
+22
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. what's the goal of this given we have the trait? do you think we'll use cuckoo somewhere and bloom or no filter elsewhere? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. might add bloom later on to benchmark against |
||
fn add(&mut self, element: &[u8]) -> Result<(), NippyJarError> { | ||
match self { | ||
Filters::Cuckoo(c) => c.add(element), | ||
Filters::Unused => todo!(), | ||
} | ||
} | ||
|
||
fn contains(&self, element: &[u8]) -> Result<bool, NippyJarError> { | ||
match self { | ||
Filters::Cuckoo(c) => c.contains(element), | ||
Filters::Unused => todo!(), | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
just wondering if this is something where we could put the PartialEq impl to be just the remaining check, and do a conditional compilation of Eq? maybe not
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
hm, i feel like that's unsafe behaviour..