-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcuckoo.go
226 lines (191 loc) · 4.26 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
package cuckoo
import (
"bytes"
"encoding/binary"
"errors"
"github.com/cespare/xxhash"
"hash/fnv"
"math"
"math/rand"
)
var (
ERR_FILTER_FULL = errors.New("full")
)
func calculateFingerprintSize(b uint, epsilon float64) uint {
f := uint(math.Ceil(math.Log(2 * float64(b) / epsilon)))
f = f / 8
if f <= 0 {
f = 1
}
return f
}
func max(x, y uint) uint {
if x > y {
return x
}
return y
}
func hash1(data []byte) []byte {
h := fnv.New32()
h.Write(data)
return h.Sum(nil)
}
func hash2(data []byte) []byte {
h := xxhash.New()
h.Write(data)
return h.Sum(nil)
}
/*
This procedure may repeat until a vacant bucket
is found as illustrated in Figure 1(b), or until
a maximum number of displacements is reached
(e.g., 500 times in our implementation).
*/
const maxCuckooKicks = 500
const bucketSize = uint(4)
/*
Fig 1(c)
A cuckoo filter, two hash per item and
functions and four entries per bucket
*/
type fingerprint []byte
func newBucket(size uint) bucket {
return make(bucket, size)
}
type bucket []fingerprint
func (b bucket) contains(f fingerprint) bool {
return b.indexOf(f) != -1
}
func (b bucket) indexOf(f fingerprint) int {
for i, item := range b {
if bytes.Equal(f, item) {
return i
}
}
return -1
}
func (b bucket) delete(f fingerprint) bool {
for i, item := range b {
if bytes.Equal(f, item) {
b[i] = nil
return true
}
}
return false
}
func (b bucket) getEmptyEntry() (int, error) {
for i, item := range b {
if item == nil {
return i, nil
}
}
return -1, ERR_FILTER_FULL
}
/*
Fig 1(c)
A cuckoo filter, two hash per item and
functions and four entries per bucket
*/
func NewCuckooFilter(capacity uint, fpRate float64) *CuckooFilter {
capacity = power2(capacity) / bucketSize
if capacity == 0 {
capacity = 1
}
buckets := make([]bucket, capacity)
for i := range buckets {
buckets[i] = newBucket(bucketSize)
}
return &CuckooFilter{
fpSize: calculateFingerprintSize(bucketSize, fpRate),
numbuckets: capacity,
buckets: buckets,
entryPerBucket: uint(4),
}
}
type CuckooFilter struct {
numbuckets uint
entryPerBucket uint
fpSize uint
size uint // number of items in filter
buckets []bucket
}
func (c *CuckooFilter) getCuckooParams(data []byte) (uint, uint, []byte) {
hash := c.computeHash(data)
f := hash[0:c.fpSize]
i1 := uint(binary.BigEndian.Uint32(hash2(hash))) % c.numbuckets
i2 := (i1 ^ uint(binary.BigEndian.Uint32(hash1(f)))) % c.numbuckets
return i1, i2, f
}
func (c *CuckooFilter) computeHash(data []byte) []byte {
return hash1(data)
}
func (c *CuckooFilter) insert(i1 uint, i2 uint, f []byte) error {
// insert into bucket1
b1 := c.buckets[i1]
if idx, err := b1.getEmptyEntry(); err == nil {
b1[idx] = f
return nil
}
// insert into bucket2
b2 := c.buckets[i2]
if idx, err := b2.getEmptyEntry(); err == nil {
b2[idx] = f
return nil
}
// must relocate existing items
// i = randomly pick i1 or i2;
// for n = 0; n < MaxNumKicks; n++ do
// randomly select an entry e from bucket[i];
// swap f and the fingerprint stored in entry e;
// i = i ⊕ hash(f);
// if bucket[i] has an empty entry then
// add f to bucket[i]
i := i1
for n := 0; n < maxCuckooKicks; n++ {
// randomly select an entry e from bucket[i];
rIdx := rand.Intn(len(c.buckets[i]) - 1)
f, c.buckets[i][rIdx] = c.buckets[i][rIdx], f
i = (i ^ uint(binary.BigEndian.Uint32(hash2(f)))) % c.numbuckets
b := c.buckets[i]
if idx, err := b.getEmptyEntry(); err == nil {
b[idx] = f
return nil
}
}
return ERR_FILTER_FULL
}
func (c *CuckooFilter) Insert(data []byte) error {
i1, i2, f := c.getCuckooParams(data)
return c.insert(i1, i2, f)
}
func (c *CuckooFilter) Lookup(data []byte) bool {
// f = fingerprint(x);
// i1 = hash(x);
// i2 = i1 ⊕ hash(f);
// if bucket[i1] or bucket[i2] has f then
// return True;
// return False;
i1, i2, f := c.getCuckooParams(data)
if c.buckets[i1].contains(f) || c.buckets[i2].contains(f) {
return true
}
return false
}
func (c *CuckooFilter) Delete(data []byte) bool {
i1, i2, f := c.getCuckooParams(data)
if c.buckets[i1].delete(f) || c.buckets[i2].delete(f) {
return true
}
return false
}
func power2(x uint) uint {
x--
x |= x >> 1
x |= x >> 2
x |= x >> 4
x |= x >> 8
x |= x >> 16
x |= x >> 32
x++
return x
}