-
Notifications
You must be signed in to change notification settings - Fork 0
/
bloom_filter.go
171 lines (139 loc) · 3.22 KB
/
bloom_filter.go
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
package bloom_filter
import (
"encoding/binary"
"math"
"sync"
)
type (
BloomFilter struct {
// m:the number of bits
// k:the number of encryptor
// n:the number of inserted bits
m, k, n uint32
// bitmap:len(bitmap) = m/32 + 1
bitmap []uint32
// encryptor: encrypt data of type any to type uint32
encryptor []Encryptor
// concurrent
isConcurrent bool
sync.RWMutex
}
)
type (
Encryptor interface {
Encrypt(origin []byte) uint32
}
)
// OptimalK calculates the optimal k value for creating a new Bloom filter
// maxN is the maximum anticipated number of elements
// optimal k = ceiling( m * ln2 / n )
func OptimalK(m, maxN uint32) uint32 {
return uint32(math.Ceil(float64(m) * math.Ln2 / float64(maxN)))
}
// OptimalM calculates the optimal m value for creating a new Bloom filter
// maxN is the maximum anticipated number of elements
// p is the desired false positive probability
// optimal m = ceiling( - n * ln(p) / (ln2)^2 )
func OptimalM(maxN uint32, p float64) uint32 {
return uint32(math.Ceil(-float64(maxN) * math.Log(p) / math.Pow(math.Ln2, 2)))
}
func NewBloomFilter(m uint32, encryptor []Encryptor, isConcurrent bool) *BloomFilter {
if m == 0 || len(encryptor) == 0 {
return nil
}
return &BloomFilter{
m: m,
k: uint32(len(encryptor)),
bitmap: make([]uint32, m>>5+1),
encryptor: encryptor,
isConcurrent: isConcurrent,
}
}
func (bf *BloomFilter) M() uint32 {
if bf == nil {
return 0
}
return bf.m
}
func (bf *BloomFilter) N() uint32 {
if bf == nil {
return 0
}
return bf.n
}
func (bf *BloomFilter) K() uint32 {
if bf == nil {
return 0
}
return bf.k
}
// P returns false positive probability
// P = (1 - e^(-kn/m))^k
func (bf *BloomFilter) P() float64 {
if bf == nil {
return 0
}
return math.Pow(1-math.Exp(float64(-bf.k*bf.n)/float64(bf.m)), float64(bf.k))
}
func (bf *BloomFilter) Bitmap() []uint32 {
if bf.isConcurrent {
bf.RLock()
defer bf.RUnlock()
}
bitmap := make([]uint32, len(bf.bitmap))
copy(bitmap, bf.bitmap)
return bitmap
}
func (bf *BloomFilter) Exist(val []byte) bool {
if bf == nil {
return false
}
offsets := bf.getOffsets(val)
if bf.isConcurrent {
bf.RLock()
defer bf.RUnlock()
}
for _, offset := range offsets {
if bf.bitmap[offset>>5]&(1<<(offset&31)) == 0 {
// bf.bitmap[offset / 32]&(1<<(offset % 32))
return false
}
}
return true
}
func (bf *BloomFilter) Set(val []byte) {
offsets := bf.getOffsets(val)
if bf.isConcurrent {
bf.Lock()
defer bf.Unlock()
}
for _, offset := range offsets {
// bf.bitmap[offset / 32] |= 1<<(offset % 32)
bf.bitmap[offset>>5] |= 1 << (offset & 31)
}
bf.n++
}
func (bf *BloomFilter) Reset() []uint32 {
if bf.isConcurrent {
bf.Lock()
defer bf.Unlock()
}
oldBitmap := bf.bitmap
bf.bitmap = make([]uint32, bf.m>>5+1)
bf.n = 0
return oldBitmap
}
func (bf *BloomFilter) getOffsets(val []byte) []uint32 {
if bf == nil {
return nil
}
origin := val
var offsets = make([]uint32, 0, bf.k)
for _, e := range bf.encryptor {
offset := e.Encrypt(origin) % bf.m
offsets = append(offsets, offset)
// add suffix to avoid getting the same offset after when use same encryptor
origin = binary.AppendVarint(origin, int64(offset))
}
return offsets
}