-
Notifications
You must be signed in to change notification settings - Fork 109
/
cuckoofilter.go
165 lines (148 loc) · 3.58 KB
/
cuckoofilter.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
package cuckoo
import (
"fmt"
"math/bits"
"math/rand"
)
const maxCuckooCount = 500
// Filter is a probabilistic counter
type Filter struct {
buckets []bucket
count uint
bucketPow uint
}
// NewFilter returns a new cuckoofilter with a given capacity.
// A capacity of 1000000 is a normal default, which allocates
// about ~1MB on 64-bit machines.
func NewFilter(capacity uint) *Filter {
capacity = getNextPow2(uint64(capacity)) / bucketSize
if capacity == 0 {
capacity = 1
}
buckets := make([]bucket, capacity)
return &Filter{
buckets: buckets,
count: 0,
bucketPow: uint(bits.TrailingZeros(capacity)),
}
}
// Lookup returns true if data is in the counter
func (cf *Filter) Lookup(data []byte) bool {
i1, fp := getIndexAndFingerprint(data, cf.bucketPow)
if cf.buckets[i1].getFingerprintIndex(fp) > -1 {
return true
}
i2 := getAltIndex(fp, i1, cf.bucketPow)
return cf.buckets[i2].getFingerprintIndex(fp) > -1
}
// Reset ...
func (cf *Filter) Reset() {
for i := range cf.buckets {
cf.buckets[i].reset()
}
cf.count = 0
}
func randi(i1, i2 uint) uint {
if rand.Intn(2) == 0 {
return i1
}
return i2
}
// Insert inserts data into the counter and returns true upon success
func (cf *Filter) Insert(data []byte) bool {
i1, fp := getIndexAndFingerprint(data, cf.bucketPow)
if cf.insert(fp, i1) {
return true
}
i2 := getAltIndex(fp, i1, cf.bucketPow)
if cf.insert(fp, i2) {
return true
}
return cf.reinsert(fp, randi(i1, i2))
}
// InsertUnique inserts data into the counter if not exists and returns true upon success
func (cf *Filter) InsertUnique(data []byte) bool {
if cf.Lookup(data) {
return false
}
return cf.Insert(data)
}
func (cf *Filter) insert(fp fingerprint, i uint) bool {
if cf.buckets[i].insert(fp) {
cf.count++
return true
}
return false
}
func (cf *Filter) reinsert(fp fingerprint, i uint) bool {
for k := 0; k < maxCuckooCount; k++ {
j := rand.Intn(bucketSize)
oldfp := fp
fp = cf.buckets[i][j]
cf.buckets[i][j] = oldfp
// look in the alternate location for that random element
i = getAltIndex(fp, i, cf.bucketPow)
if cf.insert(fp, i) {
return true
}
}
return false
}
// Delete data from counter if exists and return if deleted or not
func (cf *Filter) Delete(data []byte) bool {
i1, fp := getIndexAndFingerprint(data, cf.bucketPow)
if cf.delete(fp, i1) {
return true
}
i2 := getAltIndex(fp, i1, cf.bucketPow)
return cf.delete(fp, i2)
}
func (cf *Filter) delete(fp fingerprint, i uint) bool {
if cf.buckets[i].delete(fp) {
if cf.count > 0 {
cf.count--
}
return true
}
return false
}
// Count returns the number of items in the counter
func (cf *Filter) Count() uint {
return cf.count
}
// Encode returns a byte slice representing a Cuckoofilter
func (cf *Filter) Encode() []byte {
bytes := make([]byte, len(cf.buckets)*bucketSize)
for i, b := range cf.buckets {
for j, f := range b {
index := (i * len(b)) + j
bytes[index] = byte(f)
}
}
return bytes
}
// Decode returns a Cuckoofilter from a byte slice
func Decode(bytes []byte) (*Filter, error) {
var count uint
if len(bytes)%bucketSize != 0 {
return nil, fmt.Errorf("expected bytes to be multiple of %d, got %d", bucketSize, len(bytes))
}
if len(bytes) == 0 {
return nil, fmt.Errorf("bytes can not be empty")
}
buckets := make([]bucket, len(bytes)/4)
for i, b := range buckets {
for j := range b {
index := (i * len(b)) + j
if bytes[index] != 0 {
buckets[i][j] = fingerprint(bytes[index])
count++
}
}
}
return &Filter{
buckets: buckets,
count: count,
bucketPow: uint(bits.TrailingZeros(uint(len(buckets)))),
}, nil
}