-
Notifications
You must be signed in to change notification settings - Fork 0
/
bloomspike.py
309 lines (223 loc) · 9.54 KB
/
bloomspike.py
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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
import hashlib
import math
import struct
from typing import Callable, Iterable, Union
import aerospike
import aerospike_helpers.operations.bitwise_operations as bits
class BaseBloomHash():
def setDesiredBytes(self, n_bytes: int):
raise NotImplementedError
def hash(self, values: Iterable[Union[bytearray, bytes, int, str]]):
raise NotImplementedError
class Blake2sBloomHash(BaseBloomHash):
def __init__(self, salt: bytes = None, desired_bytes: int = 32):
self._salt = b"" if salt is None else salt
self.setDesiredBytes(desired_bytes)
def setDesiredBytes(self, n_bytes: int):
if n_bytes < 1:
raise ValueError("n_bytes must be > 1")
if n_bytes <= 32:
self._n_bytes = n_bytes
else:
self._n_bytes = 32
return self._n_bytes
def hash(self, values: Iterable[Union[bytearray, bytes, int, str]]):
h = hashlib.blake2s(salt=self._salt, digest_size=self._n_bytes)
for v in values:
if isinstance(v, int):
h.update(v.to_bytes(8, "little"))
elif isinstance(v, str):
h.update(bytes(v, encoding="utf8"))
else:
h.update(v)
return h.digest()
def init_default_hash() -> BaseBloomHash:
return Blake2sBloomHash()
def is_power2(value: int) -> int:
return ((value - 1) & value) == 0
def round_up_power2(value: int) -> int:
return 1 << int(math.ceil(math.log2(value)))
def round_down_power2(value: int) -> int:
return 1 << int(math.floor(math.log2(value)))
class BloomSpike():
"""
Distributed bloom filter.
Filter is sharded across R records.
Each record may contain S slices when hash_fn[s] sets a bit.
"""
def __init__(self, key: tuple, capacity: int, error_rate: float,
max_shard_sz: int = 1024 ** 2 - 500,
bin_name_perfix: str = "_bf",
hash_init_fn: Callable[[], BaseBloomHash] = init_default_hash):
"""
key : tuple
how to identify the bloom filters in Aerospike
capacity : int
number of items expected to be stored in a filter
error_rate : float
false positive probability.
max_shard_sz : int : 1024 ** 2
size (bytes) a bloom filter may occupy on a single record
bin_name_prefix : string : "_bf"
record will contain several bins (on per hash_fn used) named
"{}{}".format(bin_name_prefix, hash_fn_index)
"""
self._key_ns, self._key_set, self._key_value = key
self._capacity = capacity
self._error_rate = error_rate
self._max_shard_sz = max_shard_sz * 8
self._bin_name_prefix = bin_name_perfix
self._bloom_hash = hash_init_fn()
max_prefix = 15 - 2
if len(self._bin_name_prefix) > max_prefix:
raise ValueError(
"Prefix \"{}\" too long, max prefix size is {}".format(
self._bin_name_prefix, max_prefix))
self._setOptimalSize()
self._setOptimalNHashes()
self._setSliceSize()
self._setShardInfo()
self._setDigestInfo()
# print(self.__dict__)
def _setOptimalSize(self) -> None:
"""
From: https://en.wikipedia.org/wiki/Bloom_filter
Optimal number of hash functions
m = -((n ln e) / ((ln 2) ** 2))
where m = self._bit_size
n = self._capacity
e = self._error_rate
"""
error_rate = self._error_rate
cap = self._capacity
self._optimal_bit_sz = int(math.ceil(
-((cap * math.log(error_rate)) / (math.log(2) ** 2))))
def _setOptimalNHashes(self) -> None:
"""
From: https://en.wikipedia.org/wiki/Bloom_filter
Optimal number of hash functions
k = (m / n) * lg(2)
where k = self._n_hashes
m = self._size
n = self._capacity
"""
cap = self._capacity
bit_sz = self._optimal_bit_sz
self._n_hashes = int(math.ceil((bit_sz / cap) * math.log(2)))
if self._n_hashes > 100:
raise ValueError("Too many hash functions required {}.".format(
self._n_hashes))
def _setSliceSize(self) -> None:
# For performance reasons, each slice will occupy a separate bin.
# A slice needs to be a power of 2 to ensure even distribution.
max_shard_sz = self._max_shard_sz
slice_sz = max_shard_sz // self._n_hashes
slice_sz = ((slice_sz + 7) // 8) * 8 # round up to a byte
if not is_power2(slice_sz):
slice_sz = round_down_power2(slice_sz)
self._hash_slice_sz = slice_sz
def _setShardInfo(self) -> None:
self._shard_sz = self._hash_slice_sz * self._n_hashes
self._n_shards = int(math.ceil(self._optimal_bit_sz / self._shard_sz))
self._actual_size = self._shard_sz * self._n_shards
def _setDigestInfo(self) -> None:
# For an even distribution, we would like for n_shards to be a power of
# 2, but this would cause the filter to be too large. Instead, we will
# always use a 4 byte value to determine which shard to use which.
self._dbytes_shard = 4
self._dbytes_slice = int((math.log2(self._shard_sz) + 7) // 8)
hash_sz_needed = self._dbytes_shard + (self._dbytes_slice *
self._n_hashes)
self._hash_sz = self._bloom_hash.setDesiredBytes(hash_sz_needed)
self._n_times = int(math.ceil(hash_sz_needed / self._hash_sz))
def _makeHash(self, value) -> bytes:
return b''.join(self._bloom_hash.hash((value, bytes((i,))))
for i in range(self._n_times))
def _makeKey(self, shard: int) -> tuple:
return (self._key_ns, self._key_set, "{}{:06}".format(
self._key_value, shard))
def _makeSliceBin(self, slice_id: int) -> str:
return "{}{:02}".format(self._bin_name_prefix, slice_id)
def _readShard(self, data: bytes) -> tuple:
dbytes_shard = self._dbytes_shard
shard_format = ">{}s".format(dbytes_shard)
shard_raw: bytes = struct.unpack_from(shard_format, data)[0]
shard = int.from_bytes(shard_raw, "big",
signed=False) % self._n_shards
offset = dbytes_shard
return offset, shard
def _readSliceOffset(self, data: bytes, offset: int) -> tuple:
dbytes_slice = self._dbytes_slice
slice_val_format = ">{}s".format(dbytes_slice)
slice_raw: bytes = struct.unpack_from(slice_val_format, data,
offset=offset)[0]
slice_bit = int.from_bytes(
slice_raw, "big", signed=False) % self._hash_slice_sz
offset += dbytes_slice
return offset, slice_bit
def clear(self, client: aerospike.Client):
bins = {self._makeSliceBin(slice): aerospike.null()
for slice in range(self._n_hashes)}
for shard in range(self._n_shards):
key = self._makeKey(shard)
try:
client.put(key, bins)
except aerospike.exception.RecordNotFound:
continue
def add(self, client: aerospike.Client, value, policy=None):
hashed = self._makeHash(value)
offset, shard = self._readShard(hashed)
ops = []
for i in range(self._n_hashes):
offset, slice_bit = self._readSliceOffset(hashed, offset)
bin_name = self._makeSliceBin(i)
ops.append(bits.bit_resize(bin_name, self._hash_slice_sz // 8))
ops.append(bits.bit_set(bin_name, slice_bit, 1, 1, b'\x80'))
key = self._makeKey(shard)
# print(ops)
client.operate(key, ops)
def mayContain(self, client: aerospike.Client, value, policy=None) -> bool:
hashed = self._makeHash(value)
offset, shard = self._readShard(hashed)
ops = []
for i in range(self._n_hashes):
offset, slice_bit = self._readSliceOffset(hashed, offset)
bin_name = self._makeSliceBin(i)
ops.append(bits.bit_get(bin_name, slice_bit, 1))
key = self._makeKey(shard)
try:
_, _, bins = client.operate(key, ops)
except (aerospike.exception.BinIncompatibleType,
aerospike.exception.RecordNotFound):
return False
return (all(int.from_bytes(v, "big") != 0 for v in bins.values()))
def main():
capacity = 10 ** 6
error = 10 ** -5
config = {"hosts": [("174.22.0.1", 3000), ("174.22.0.2", 3000)]}
client = aerospike.client(config).connect()
b = BloomSpike(("test", "test", "test"), capacity, error)
b.clear(client)
assert not b.mayContain(client, b"1234")
b.add(client, b"1234")
assert b.mayContain(client, b"1234")
print(b.__dict__)
for i, v in enumerate(range(capacity)):
if i % 1000 == 0:
print("inserting", i, "of", capacity)
b.add(client, v)
print("checking false negatives")
for i, v in enumerate(range(capacity)):
if i % 1000 == 0:
print("checking fn", i, "of", capacity)
assert b.mayContain(client, v)
print("checking false positives")
false_positives = 0
for i, v in enumerate(range(capacity, capacity * 3)):
if i % 1000 == 0:
print("checking fp", i, "of", capacity * 2)
if (b.mayContain(client, v)):
false_positives += 1
print(dict(fp=false_positives, err=error))
b.clear(client)
main()