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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
extern crate byteorder;
extern crate digest;
extern crate murmurhash3;
use byteorder::ReadBytesExt;
use murmurhash3::murmurhash3_x86_32;
use std::io::{Error, ErrorKind};
struct BitSlice<'a> {
bytes: &'a [u8],
bit_len: usize,
}
impl<'a> BitSlice<'a> {
fn new(bytes: &'a [u8], bit_len: usize) -> BitSlice<'a> {
if bit_len > bytes.len() * 8 {
panic!(
"bit_len too large for given data: {} > {} * 8",
bit_len,
bytes.len()
);
}
BitSlice { bytes, bit_len }
}
fn get(&self, bit_index: usize) -> bool {
if bit_index >= self.bit_len {
panic!(
"bit index out of range for bit slice: {} >= {}",
bit_index, self.bit_len
);
}
let byte_index = bit_index / 8;
let final_bit_index = bit_index % 8;
let byte = self.bytes[byte_index];
let test_value = match final_bit_index {
0 => byte & 0b00000001u8,
1 => byte & 0b00000010u8,
2 => byte & 0b00000100u8,
3 => byte & 0b00001000u8,
4 => byte & 0b00010000u8,
5 => byte & 0b00100000u8,
6 => byte & 0b01000000u8,
7 => byte & 0b10000000u8,
_ => panic!("impossible final_bit_index value: {}", final_bit_index),
};
test_value > 0
}
}
struct Bloom<'a> {
level: u32,
n_hash_funcs: u32,
size: usize,
bit_slice: BitSlice<'a>,
}
impl<'a> Bloom<'a> {
pub fn from_bytes(bytes: &'a [u8]) -> Result<(Bloom<'a>, &'a [u8]), Error> {
let mut cursor = bytes;
let size = cursor.read_u32::<byteorder::LittleEndian>()? as usize;
let n_hash_funcs = cursor.read_u32::<byteorder::LittleEndian>()?;
let level = cursor.read_u32::<byteorder::LittleEndian>()?;
let shifted_size = size.wrapping_shr(3);
let byte_count = if size % 8 != 0 {
shifted_size + 1
} else {
shifted_size
};
if byte_count > cursor.len() {
return Err(Error::new(
ErrorKind::InvalidData,
"Invalid Bloom filter: too short",
));
}
let (bits_bytes, rest_of_bytes) = cursor.split_at(byte_count);
let bloom = Bloom {
level,
n_hash_funcs,
size,
bit_slice: BitSlice::new(bits_bytes, size),
};
Ok((bloom, rest_of_bytes))
}
fn hash(&self, n_fn: u32, key: &[u8]) -> usize {
let hash_seed = (n_fn << 16) + self.level;
let h = murmurhash3_x86_32(key, hash_seed) as usize % self.size;
h
}
pub fn has(&self, item: &[u8]) -> bool {
for i in 0..self.n_hash_funcs {
if !self.bit_slice.get(self.hash(i, item)) {
return false;
}
}
true
}
}
pub struct Cascade<'a> {
filter: Bloom<'a>,
child_layer: Option<Box<Cascade<'a>>>,
}
impl<'a> Cascade<'a> {
pub fn from_bytes(bytes: &'a [u8]) -> Result<Option<Box<Cascade<'a>>>, Error> {
if bytes.len() == 0 {
return Ok(None);
}
let mut cursor = bytes;
let version = cursor.read_u16::<byteorder::LittleEndian>()?;
if version != 1 {
return Err(Error::new(ErrorKind::InvalidData, "Invalid version"));
}
let (filter, rest_of_bytes) = Bloom::from_bytes(cursor)?;
Ok(Some(Box::new(Cascade {
filter,
child_layer: Cascade::from_bytes(rest_of_bytes)?,
})))
}
pub fn has(&self, entry: &[u8]) -> bool {
if self.filter.has(&entry) {
match self.child_layer {
Some(ref child) => {
let child_value = !child.has(entry);
return child_value;
}
None => {
return true;
}
}
}
return false;
}
}
#[cfg(test)]
mod tests {
use Bloom;
use Cascade;
#[test]
fn bloom_test_from_bytes() {
let src: Vec<u8> = vec![
0x09, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x41, 0x00,
];
match Bloom::from_bytes(&src) {
Ok((bloom, rest_of_bytes)) => {
assert!(rest_of_bytes.len() == 0);
assert!(bloom.has(b"this") == true);
assert!(bloom.has(b"that") == true);
assert!(bloom.has(b"other") == false);
}
Err(_) => {
panic!("Parsing failed");
}
};
let short: Vec<u8> = vec![
0x09, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x41,
];
assert!(Bloom::from_bytes(&short).is_err());
}
#[test]
fn bloom_test_from_file() {
let v = include_bytes!("../test_data/test_bf");
let (bloom, rest_of_bytes) = Bloom::from_bytes(v).expect("parsing Bloom should succeed");
assert!(rest_of_bytes.len() == 0);
assert!(bloom.has(b"this") == true);
assert!(bloom.has(b"that") == true);
assert!(bloom.has(b"yet another test") == false);
}
#[test]
fn cascade_from_file_bytes_test() {
let v = include_bytes!("../test_data/test_mlbf");
let cascade = Cascade::from_bytes(v)
.expect("parsing Cascade should succeed")
.expect("Cascade should be Some");
assert!(cascade.has(b"test") == true);
assert!(cascade.has(b"another test") == true);
assert!(cascade.has(b"yet another test") == true);
assert!(cascade.has(b"blah") == false);
assert!(cascade.has(b"blah blah") == false);
assert!(cascade.has(b"blah blah blah") == false);
let v = include_bytes!("../test_data/test_short_mlbf");
assert!(Cascade::from_bytes(v).is_err());
}
}