1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
//! Library implements xor-filter.
//!
//! Provides hasher types:
//!
//! * [NoHash], to be used when hash feature is not needed on [Xor8], [Fuse8] and
//! [Fuse16] types. Note that type methods that accept parametrized key cannot be
//! used.
//! * [BuildHasherDefault] is the default hasher when `H` is not supplied. Note that
//! [DefaultHasher] uses an unspecified internal algorithm and so its hashes should
//! not be relied upon over releases.
//!
//! Refer to original implementation under `github.com/FastFilter` to learn the
//! differences between [Xor8], [Fuse8] and [Fuse16] filters. Otherwise, all the types
//! provides similar methods.
//!
//! **Handling duplicates**
//!
//! * [Fuse16] and [Xor8] implementation uses BTreeMap to make sure all the digests
//! generated from keys are unique, this avoids duplicates but decreases the build
//! performance significantly.
//! * [Fuse8] implementation computes duplicates on the fly leading to significantly
//! better build performance. On the other hand, Fuse8 cannot handle more than
//! few duplicates.
//!
//! This is ported from its original implementation:
//!
//! * [Xor8] from <https://github.com/FastFilter/xorfilter>, written in golang.
//! * [Fuse8] and [Fuse16] from <https://github.com/FastFilter/xor_singleheader> written in C.
#[allow(unused_imports)]
use std::collections::hash_map::DefaultHasher;
use std::{error, fmt, result};
/// Short form to compose Error values.
///
/// Here are few possible ways:
///
/// ```ignore
/// use crate::Error;
/// err_at!(ParseError, msg: format!("bad argument"));
/// ```
///
/// ```ignore
/// use crate::Error;
/// err_at!(ParseError, std::io::read(buf));
/// ```
///
/// ```ignore
/// use crate::Error;
/// err_at!(ParseError, std::fs::read(file_path), format!("read failed"));
/// ```
///
macro_rules! err_at {
($v:ident, msg: $($arg:expr),+) => {{
let prefix = format!("{}:{}", file!(), line!());
Err(Error::$v(prefix, format!($($arg),+)))
}};
($v:ident, $e:expr) => {{
match $e {
Ok(val) => Ok(val),
Err(err) => {
let prefix = format!("{}:{}", file!(), line!());
Err(Error::$v(prefix, format!("{}", err)))
}
}
}};
($v:ident, $e:expr, $($arg:expr),+) => {{
match $e {
Ok(val) => Ok(val),
Err(err) => {
let prefix = format!("{}:{}", file!(), line!());
let msg = format!($($arg),+);
Err(Error::$v(prefix, format!("{} {}", err, msg)))
}
}
}};
}
/// Error variants that are returned by this package's API.
///
/// Each variant carries a prefix, typically identifying the
/// error location.
pub enum Error {
Fatal(String, String),
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter) -> result::Result<(), fmt::Error> {
use Error::*;
match self {
Fatal(p, msg) => write!(f, "{} Fatal: {}", p, msg),
}
}
}
impl fmt::Debug for Error {
fn fmt(&self, f: &mut fmt::Formatter) -> result::Result<(), fmt::Error> {
write!(f, "{}", self)
}
}
impl error::Error for Error {}
/// Type alias for Result return type, used by this package.
pub type Result<T> = result::Result<T, Error>;
mod fuse16;
mod fuse8;
mod hasher;
mod xor8;
pub use fuse16::Fuse16;
pub use fuse8::Fuse8;
pub use hasher::{BuildHasherDefault, NoHash};
pub use xor8::Xor8;