-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathscalable_bloom.go
243 lines (206 loc) · 5.78 KB
/
scalable_bloom.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
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
package sprout
import (
"log"
"math"
"sync"
)
type ScalableBloomFilter struct {
// The desired false positive rate
err_rate float64
// the number of items intended to be added to the bloom filter
capacity int
db Store
filters []*BloomFilter
ratio float64
// the number of bits per slice(hashFn) for the first filter
m0 int
// growth rate is the rate at which the capacity of the bloom filter grows
growth_rate GrowthRate
path string
opts *BloomOptions
lock *sync.RWMutex
}
type GrowthRate uint
var (
// GrowthSmall represents a small expected set growth
GrowthSmall GrowthRate = 2
// GrowthLarge represents a large expected set growth
GrowthLarge GrowthRate = 4
)
// NewScalableBloom creates a new scalable bloom filter.
// err_rate is the desired false error rate. e.g. 0.001 implies 1 false positive in 1000 lookups
// initial_capacity is the initial capacity of the bloom filter. When the number
// of items exceed the initial capacity, a new filter is created.
//
// The growth rate defaults to 2.
func NewScalableBloom(opts *BloomOptions) *ScalableBloomFilter {
if opts.Err_rate <= 0 || opts.Err_rate >= 1 {
panic("Error rate must be between 0 and 1")
}
if opts.Capacity <= 0 {
panic("Initial capacity must be greater than 0")
}
if opts.GrowthRate == 0 {
opts.GrowthRate = GrowthSmall
}
if opts.Path == "" {
opts.Path = "/tmp/bloom.db"
}
initialFilter := NewBloom(opts)
return &ScalableBloomFilter{
err_rate: opts.Err_rate,
capacity: opts.Capacity,
growth_rate: opts.GrowthRate,
ratio: 0.9, // Source: [1]
m0: initialFilter.m,
filters: []*BloomFilter{initialFilter},
db: opts.Database,
path: opts.Path,
opts: opts,
lock: &sync.RWMutex{},
}
}
// Add adds a key to the scalable bloom filter
// Complexity: O(k)
func (sbf *ScalableBloomFilter) Add(key []byte) {
sbf.add(key)
}
func (sbf *ScalableBloomFilter) add(key []byte) {
if sbf.Top().count >= sbf.Top().capacity {
sbf.grow()
}
// the top filter is the one holding the mmaped bytes
bf := sbf.Top()
indices := bf.candidates(string(key))
for i := 0; i < len(indices); i++ {
idx, mask := bf.getBitIndexN(indices[i])
if int(idx) >= bf.bit_width {
// this should not happen
panic("Error adding key: Index out of bounds")
}
bf.mem[bf.pageOffset+int(idx)] |= mask
}
bf.count++
}
// Put adds a key to the scalable bloom filter, and puts the value in the database
func (sbf *ScalableBloomFilter) Put(key, val []byte) error {
sbf.add(key)
return sbf.db.Put(key, val)
}
// Contains checks if the key is in the bloom filter
// Complexity: O(k*n)
func (sbf *ScalableBloomFilter) Contains(key []byte) bool {
for _, filter := range sbf.filters {
if sbf.contains(filter, key) {
return true
}
}
return false
}
func (sbf *ScalableBloomFilter) contains(bf *BloomFilter, key []byte) bool {
topFilter := sbf.Top()
indices := bf.candidates(string(key))
for i := 0; i < len(indices); i++ {
idx, mask := bf.getBitIndexN(indices[i])
if int(idx) >= bf.bit_width {
return false
}
if bit := topFilter.mem[bf.pageOffset+int(idx)]; bit&mask == 0 {
return false
}
}
return true
}
// Get returns the value associated with the key
func (sbf *ScalableBloomFilter) Get(key []byte) []byte {
for _, filter := range sbf.filters {
if filter.Contains(key) {
return filter.Get(key)
}
}
return nil
}
// Top returns the top filter in the scalable bloom filter
func (sbf *ScalableBloomFilter) Top() *BloomFilter {
return sbf.filters[len(sbf.filters)-1]
}
// grow increases the capacity of the bloom filter by adding a new filter
func (sbf *ScalableBloomFilter) grow() {
// unmap the old top filter
err := sbf.Top().Close()
if err != nil {
log.Panicf("Error unmapping top filter before grow: %v", err)
}
err_rate := sbf.err_rate * math.Pow(sbf.ratio, float64(len(sbf.filters)))
newCapacity := sbf.getNewCap()
opts := &BloomOptions{
Err_rate: err_rate,
Capacity: newCapacity,
Database: sbf.db,
Path: sbf.path,
dataSize: sbf.Top().bit_width,
}
newFilter := NewBloom(opts)
sbf.filters = append(sbf.filters, newFilter)
}
func (sbf *ScalableBloomFilter) getNewCap() int {
i := float64(len(sbf.filters)) - 1.0
newCapacity := float64(sbf.m0) * float64(math.Pow(float64(sbf.growth_rate), i)) * math.Ln2
return int(newCapacity)
}
// Size returns the total capacity of the scalable bloom filter
func (sbf *ScalableBloomFilter) Capacity() int {
sum := 0
for _, filter := range sbf.filters {
sum += filter.capacity
}
return sum
}
// filterSize returns the total filter size
func (sbf *ScalableBloomFilter) filterSize() int {
return sbf.Top().bit_width
}
// DB returns the store used by the scalable bloom filter
func (sbf *ScalableBloomFilter) DB() Store {
return sbf.db
}
// Count returns the number of items added to the bloom filter
func (sbf *ScalableBloomFilter) Count() int {
sum := 0
for _, filter := range sbf.filters {
sum += filter.count
}
return sum
}
// Close closes the scalable bloom filter
func (sbf *ScalableBloomFilter) Close() error {
return sbf.Top().Close()
}
func (sbf *ScalableBloomFilter) prob() float64 {
sum := 1.0
for i := range sbf.filters {
sum *= 1.0 - (sbf.err_rate * math.Pow(sbf.ratio, float64(i)))
}
return 1.0 - sum
}
// Stats returns the stats of the bloom filter
func (sbf *ScalableBloomFilter) Stats() BloomFilterStats {
return BloomFilterStats{
Capacity: sbf.Capacity(),
Count: sbf.Count(),
Size: sbf.filterSize(),
M: sbf.Top().m,
K: sbf.Top().k,
Prob: sbf.prob(),
}
}
// Clear resets all bits in the bloom filter
func (sbf *ScalableBloomFilter) Clear() {
sbf.lock.Lock()
defer sbf.lock.Unlock()
err := sbf.Top().Close()
if err != nil {
log.Panicf("Error closing top filter before clear: %v", err)
}
sbf.filters = []*BloomFilter{NewBloom(sbf.opts)}
}