-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathBloomFilter.py
More file actions
68 lines (55 loc) · 2.05 KB
/
BloomFilter.py
File metadata and controls
68 lines (55 loc) · 2.05 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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import math
import mmh3
class BloomFilter:
# To create bit array of size n
'''
size - size of bit array calc based the formula
k - no. of hash function used to hash the value
p - probability of false positive
'''
def __init__(self, n):
self.p = 0.05
self.size = math.ceil(-n*math.log(self.p)/(math.log(2)**2))
self.k = math.ceil(self.size/n*math.log(2))
self.bit_array = [0] * self.size
self.validate_array = []
# Func to insert values into BF
def insert(self, value, freq=1):
line_hash = str(mmh3.hash(value,freq))
for i in range(self.k):
index = mmh3.hash(line_hash,i) % self.size
self.bit_array[index] = 1
# To check if the value is present in BF or not
def validate(self, value, freq=1):
line_hash = str(mmh3.hash(value,freq))
for i in range(self.k):
check_at_index = mmh3.hash(line_hash,i) % self.size
if self.validate_array[check_at_index] == 1:
continue
else:
return False
return True
def readBloomFilterFromFile(self,filename):
f = open(filename, "rb")
self.validate_array = list(f.read())
for i in range(0, len(self.validate_array)):
self.validate_array[i] -= 48
print(self.validate_array)
f.close()
def readBloomFilterFromBytes(self,bf_as_bytes):
self.validate_array = list(bf_as_bytes)
for i in range(0, len(self.validate_array)):
self.validate_array[i] -= 48
# Returns the bit array
def getBloomFilter(self):
return self.bit_array
# Returns the size of the bit arry
def getSize(self):
return self.size
def getNFromSize(self,size):
return(math.floor(size*-1*(math.log(2)**2)/math.log(self.p)))
# Returns the # of Hash Functions ie. h1(k), h2(k) ...
def getNumberOfHashFunctions(self):
return self.k
def getAsBytes(self):
return str.encode(''.join([str(i) for i in self.bit_array]))