-
Notifications
You must be signed in to change notification settings - Fork 4
/
cuckoo.go
450 lines (368 loc) · 9.2 KB
/
cuckoo.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
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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
package cuckoo
import (
"encoding/binary"
"encoding/gob"
"fmt"
"hash"
"io"
"math/rand"
"sync"
"github.com/spaolacci/murmur3"
)
const (
defaultBucketSize = 8
maxBucketSize = 16
defaultTotalBuckets = 4 << 20
defaultMaxKicks = 500
seed = 59053
)
// fingerprint of the item
type fingerprint uint16
// emptyFingerprint
var emptyFingerprint fingerprint
// bucket with n fingerprints
type bucket struct {
Track uint16
FPs []fingerprint
}
// Filter is the cuckoo-filter
type Filter struct {
count uint32
buckets []bucket
bucketSize uint8
totalBuckets uint32
hash hash.Hash32
maxKicks uint16
// protects above fields
L sync.RWMutex
}
// gobFilter for encoding and decoding the Filter
type gobFilter struct {
Count uint32
Buckets []bucket
BucketSize uint8
TotalBuckets uint32
MaxKicks uint16
}
// initBuckets initialises the buckets
func initBuckets(totalBuckets uint32, bucketSize uint8) []bucket {
buckets := make([]bucket, totalBuckets, totalBuckets)
for i := range buckets {
buckets[i] = bucket{FPs: make([]fingerprint, bucketSize, bucketSize)}
}
return buckets
}
// StdFilter returns Standard Cuckoo-Filter
func StdFilter() *Filter {
return newFilter(defaultTotalBuckets, defaultBucketSize, murmur3.New32WithSeed(seed))
}
func newFilter(tb uint32, bs uint8, hash hash.Hash32) *Filter {
return &Filter{
buckets: initBuckets(tb, bs),
bucketSize: bs,
totalBuckets: tb,
hash: hash,
maxKicks: defaultMaxKicks,
}
}
func NewFilter(count uint32) *Filter {
b := nextPowerOf2(count) / defaultBucketSize
return newFilter(b, defaultBucketSize, murmur3.New32WithSeed(seed))
}
func NewFilterWithBucketSize(count uint32, bs uint8) (*Filter, error) {
if bs > maxBucketSize {
return nil, fmt.Errorf("doesn't support %d bucket size. Max bucket size is %d", bs, maxBucketSize)
}
b := nextPowerOf2(count) / uint32(bs)
return newFilter(b, bs, murmur3.New32WithSeed(seed)), nil
}
// nextPowerOf2 returns the next power 2 >= v
func nextPowerOf2(v uint32) (n uint32) {
var i uint32
for i = 2; i < 32; i++ {
n = 1 << i
if n >= v {
break
}
}
return n
}
// isSet returns true if the i th bit in the Track is 1
func isSet(track uint16, i uint8) bool {
return track|(1<<i) == track
}
// unSet un sets the i th bit in Track
func unSet(track uint16, i uint8) uint16 {
return track ^ 1<<i
}
func set(track uint16, i uint8) uint16 {
return track | 1<<i
}
// deleteFrom deletes fingerprint from bucket if exists
func deleteFrom(b *bucket, bs uint8, fp fingerprint) bool {
for i := uint8(0); i < bs; i++ {
if !isSet(b.Track, i) || b.FPs[i] != fp {
continue
}
b.FPs[i] = emptyFingerprint
b.Track = unSet(b.Track, i)
return true
}
return false
}
// containsIn returns if the given fingerprint exists in bucket
func containsIn(b bucket, bs uint8, fp fingerprint) bool {
for i := uint8(0); i < bs; i++ {
if isSet(b.Track, i) && b.FPs[i] == fp {
return true
}
}
return false
}
// addToBucket will add fp to the bucket i in filter
func addToBucket(b *bucket, bs uint8, fp fingerprint) bool {
for i := uint8(0); i < bs; i++ {
if isSet(b.Track, i) {
continue
}
b.FPs[i] = fp
b.Track = set(b.Track, i)
return true
}
return false
}
// hashOf returns the 32-bit hash
func hashOf(x []byte, hash hash.Hash32) (uint32, []byte) {
hash.Reset()
hash.Write(x)
h := hash.Sum32()
return h, hash.Sum(nil)
}
// fingerprintOf returns the fingerprint of x with size using hash
func fingerprintOf(xb []byte) (fp fingerprint) {
return fingerprint(binary.BigEndian.Uint16(xb))
}
func fingerprintHash(fp fingerprint, hash hash.Hash32) (fph uint32) {
b := make([]byte, 2, 2)
binary.BigEndian.PutUint16(b, uint16(fp))
fph, _ = hashOf(b, hash)
return fph
}
// indicesOf returns the indices of item x using given hash
func indicesOf(xh, fph, totalBuckets uint32) (i1, i2 uint32) {
i1 = xh % totalBuckets
i2 = alternateIndex(totalBuckets, i1, fph)
return i1, i2
}
// alternateIndex returns the alternate index of i
func alternateIndex(totalBuckets, i, fph uint32) (j uint32) {
return (i ^ fph) % totalBuckets
}
// estimatedLoadFactor returns an estimated max load factor based on bucket size
func estimatedLoadFactor(bucketSize uint8) float64 {
switch {
case bucketSize < 8:
return 0.955
case bucketSize < 16:
return 0.985
default:
return 0.994
}
}
// isReliable returns if the filter is reliable for another insert
func isReliable(f *Filter) bool {
clf := f.ULoadFactor()
elf := estimatedLoadFactor(f.bucketSize)
if clf < elf {
return true
}
return false
}
// swapFingerprint swaps a random fp from the bucket with provided fp
func swapFingerprint(b *bucket, fp fingerprint) fingerprint {
var sfp fingerprint
k := rand.Intn(len(b.FPs))
sfp, b.FPs[k] = b.FPs[k], fp
return sfp
}
// insert inserts the item into filter
func insert(f *Filter, x []byte) (ok bool) {
xh, xb := hashOf(x, f.hash)
fp := fingerprintOf(xb)
fph := fingerprintHash(fp, f.hash)
i1, i2 := indicesOf(xh, fph, f.totalBuckets)
defer func() {
if ok {
f.count++
}
}()
if addToBucket(&f.buckets[i1], f.bucketSize, fp) || addToBucket(&f.buckets[i2], f.bucketSize, fp) {
return true
}
ri := []uint32{i1, i2}[rand.Intn(2)]
var k uint16
for k = 0; k < f.maxKicks; k++ {
fp = swapFingerprint(&f.buckets[ri], fp)
fph = fingerprintHash(fp, f.hash)
ri = alternateIndex(f.totalBuckets, ri, fph)
if addToBucket(&f.buckets[ri], f.bucketSize, fp) {
return true
}
}
return false
}
// lookup checks if the item x existence in filter
func lookup(f *Filter, x []byte) bool {
xh, xb := hashOf(x, f.hash)
fp := fingerprintOf(xb)
fph := fingerprintHash(fp, f.hash)
i1, i2 := indicesOf(xh, fph, f.totalBuckets)
if containsIn(f.buckets[i1], f.bucketSize, fp) || containsIn(f.buckets[i2], f.bucketSize, fp) {
return true
}
return false
}
// deleteItem deletes item if present from the filter
func deleteItem(f *Filter, x []byte) (ok bool) {
xh, xb := hashOf(x, f.hash)
fp := fingerprintOf(xb)
fph := fingerprintHash(fp, f.hash)
i1, i2 := indicesOf(xh, fph, f.totalBuckets)
defer func() {
if ok {
f.count--
}
}()
if deleteFrom(&f.buckets[i1], f.bucketSize, fp) || deleteFrom(&f.buckets[i2], f.bucketSize, fp) {
return true
}
return false
}
// sanitize the bytes
func sanitize(x []byte) ([]byte, bool) {
if len(x) == 0 {
return nil, false
}
if len(x) == 1 {
x = []byte{0, x[0]}
}
return x, true
}
// Insert inserts the item to the filter
func (f *Filter) Insert(x []byte) bool {
f.L.Lock()
defer f.L.Unlock()
return f.UInsert(x)
}
// UInsert inserts the item to the filter. Not thread safe
func (f *Filter) UInsert(x []byte) bool {
if !isReliable(f) {
return false
}
x, ok := sanitize(x)
if !ok {
return false
}
return insert(f, x)
}
// InsertUnique inserts only unique items
func (f *Filter) InsertUnique(x []byte) bool {
f.L.Lock()
defer f.L.Unlock()
return f.UInsertUnique(x)
}
// UInsertUnique inserts only unique items. Not thread safe
func (f *Filter) UInsertUnique(x []byte) bool {
if !isReliable(f) {
return false
}
x, ok := sanitize(x)
if !ok {
return false
}
if f.ULookup(x) {
return true
}
return insert(f, x)
}
// Lookup checks if item exists in filter
func (f *Filter) Lookup(x []byte) bool {
f.L.Lock()
defer f.L.Unlock()
return f.ULookup(x)
}
// ULookup checks if item exists in filter. Not thread safe
func (f *Filter) ULookup(x []byte) bool {
x, ok := sanitize(x)
if !ok {
return false
}
return lookup(f, x)
}
// Delete deletes the item from the filter
func (f *Filter) Delete(x []byte) bool {
f.L.Lock()
defer f.L.Unlock()
return f.UDelete(x)
}
// UDelete deletes the item from the filter. Not thread safe
func (f *Filter) UDelete(x []byte) bool {
x, ok := sanitize(x)
if !ok {
return false
}
return deleteItem(f, x)
}
// Count returns total inserted items into filter
func (f *Filter) Count() uint32 {
f.L.RLock()
defer f.L.RUnlock()
return f.UCount()
}
// UCount returns total inserted items into filter. Not thread safe
func (f *Filter) UCount() uint32 {
return f.count
}
// LoadFactor returns the load factor of the filter
func (f *Filter) LoadFactor() float64 {
f.L.RLock()
defer f.L.RUnlock()
return f.ULoadFactor()
}
// ULoadFactor returns the load factor of the filter. Not thread safe
func (f *Filter) ULoadFactor() float64 {
return float64(f.count) / (float64(uint32(f.bucketSize) * f.totalBuckets))
}
// Encode gob encodes the filter to passed writer
func (f *Filter) Encode(w io.Writer) error {
// hold the read lock till we encode the data to the writer
f.L.RLock()
defer f.L.RUnlock()
gf := &gobFilter{
Count: f.count,
Buckets: f.buckets,
BucketSize: f.bucketSize,
TotalBuckets: f.totalBuckets,
MaxKicks: f.maxKicks,
}
ge := gob.NewEncoder(w)
return ge.Encode(gf)
}
// Decode decodes and returns the filter instance
func Decode(r io.Reader) (*Filter, error) {
gd := gob.NewDecoder(r)
gf := &gobFilter{}
err := gd.Decode(gf)
if err != nil {
return nil, fmt.Errorf("failed to decode filter: %v", err)
}
f := &Filter{
count: gf.Count,
buckets: gf.Buckets,
bucketSize: gf.BucketSize,
totalBuckets: gf.TotalBuckets,
hash: murmur3.New32WithSeed(seed),
maxKicks: gf.MaxKicks,
}
return f, nil
}