-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBloomFilter.py
More file actions
47 lines (38 loc) · 1.37 KB
/
Copy pathBloomFilter.py
File metadata and controls
47 lines (38 loc) · 1.37 KB
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
import mmh3
from bitarray import bitarray
import json
import math
class BloomFilter:
def __init__(self, n=None, p=None, size=None, hash_count=None):
if n and p:
self.size = int(- (n * math.log(p)) / (math.log(2) ** 2))
self.hash_count = int((self.size / n) * math.log(2))
elif size and hash_count:
self.size = size
self.hash_count = hash_count
else:
raise ValueError("Provide either (n, p) or (size, hash_count)")
self.bit_array = bitarray(self.size)
self.bit_array.setall(0)
def add(self, item: str):
for i in range(self.hash_count):
digest = mmh3.hash(item, i) % self.size
self.bit_array[digest] = 1
def check(self, item: str) -> bool:
for i in range(self.hash_count):
digest = mmh3.hash(item, i) % self.size
if self.bit_array[digest] == 0:
return False
return True
def build_bloom_from_json(segment_file):
# Example: expecting ~1000 keys, 1% false positive rate
bf = BloomFilter(n=1000, p=0.01)
with open(segment_file, "r") as f:
data = json.load(f)
if isinstance(data, dict):
for key in data.keys():
bf.add(key)
elif isinstance(data, list):
for entry in data:
bf.add(entry["key"])
return bf