/
cuckooL.go
277 lines (237 loc) · 7.48 KB
/
cuckooL.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
package cuckoofilter
import (
"math"
"github.com/pierrec/xxHash/xxHash32"
"github.com/pierrec/xxHash/xxHash64"
)
// bucketBitsNum is the number of bits present in a bucket.
const bucketBitsNumL = 32
// bucket can be changed to any Go uint type.
// bucketBitsNum must be changed accordingly.
type bucketL uint32
// fingerprint has fpBitsNum bits
// bucketBitsNum/fpBitsNum fingerprint entries per bucket
const (
// number of bits for a fingerprint
// 8 seems achieves a less than 1% failure rate
// even under a high filter load.
fpBitsNumL = 16
fpMaskL = 1<<fpBitsNumL - 1
bucketNumL = bucketBitsNumL / fpBitsNumL
)
// fingerprint is stored in a bucket.
type fingerprintL uint16
// String prints out a bucket fingerprint items for debugging purposes.
// func (b bucketL) String() string {
// str := "{"
// for s := uint(0); s < bucketBitsNumL; s += fpBitsNumL {
// str += fmt.Sprintf(" %x", int(b>>s&fpMaskL))
// }
// return str + " }"
// }
// FilterL represents a Cuckoo Filter with 4 bytes per item and an error rate of 0.005.
type FilterL struct {
f64 *Filter64L
}
// NewL creates a Filter containing up to n items with 4 bytes per item and an error rate of 0.005.
func NewL(n uint) *FilterL {
return &FilterL{
f64: New64L(n),
}
}
func fpValueL(b []byte) fingerprintL {
h := uint64(xxHash32.Checksum(b, seed))
return fpValue64L(h)
}
// fpValue returns a non-zero n bits fingerprint derived from the item hash.
func fpValue64L(h uint64) fingerprintL {
for ; h&fpMaskL == 0; h >>= 4 {
if h == 0 {
return 1
}
}
return fingerprintL(h & fpMaskL)
}
// func (cf *FilterL) String() string {
// return fmt.Sprint(cf.f64.bucketsL)
// }
// Insert add an item to the Filter and returns whether it was inserted or not.
//
// NB. The same item cannot be inserted more than 2 times.
func (cf *FilterL) Insert(b []byte) bool {
return cf.f64.insert(xxHash64.Checksum(b, seed), fpValueL(b))
}
// Has checks if the item is in the Filter.
func (cf *FilterL) Has(b []byte) bool {
return cf.f64.has(xxHash64.Checksum(b, seed), fpValueL(b))
}
// Delete removes an item from the Filter and returns whether or not it was present.
// To delete an item safely it must have been previously inserted.
func (cf *FilterL) Delete(b []byte) bool {
return cf.f64.delete(xxHash64.Checksum(b, seed), fpValueL(b))
}
// Len returns the number of items in the filter.
func (cf *FilterL) Len() int { return cf.f64.Len() }
// Cap returns the filter capacity.
func (cf *FilterL) Cap() int { return cf.f64.Cap() }
// Filter64L represents a Cuckoo Filter that only stores uint64 items
// and as such is much faster than FilterL.
type Filter64L struct {
buckets []bucketL
mask uint64 // mask to be used for buckets indexing
max int // maximum number of relocations
}
// New64L creates a Filter64L containing up to n uint64 items.
// A Filter64L contains a minimum of 2 items.
func New64L(n uint) *Filter64L {
// each bucket holds bucketBitsNum/fpBitsNum fingerprint entries
// of fpBitsNum bits each
n2 := power2(n)
if n2 < 2 {
n2 = 2
}
// base the maximum numver of relocations on the number of buckets
max := int(20 * math.Log(float64(n2)))
return &Filter64L{
buckets: make([]bucketL, n2, n2),
mask: uint64(n2 - 1),
max: max,
}
}
// index1 computes the first index from the hash and the fingerprint.
func (cf *Filter64L) index1(h uint64, fp fingerprintL) uint64 {
return h & cf.mask
}
// index2 computes the second index from the previous index and the fingerprint.
// Note that index2(index2(i, fp)) == i, which is leveraged when reverting relocation.
func (cf *Filter64L) index2(i uint64, fp fingerprintL) uint64 {
return (i ^ fpHash(uint64(fp))) & cf.mask
}
// insertAt adds fingerprint fp to the filter at index i and returns
// if the insert succeeded.
func (cf *Filter64L) insertAt(idx uint64, fp fingerprintL) bool {
u := cf.buckets[idx]
for i := 0; i < bucketNumL; i++ {
s := uint(i * fpBitsNumL)
if u>>s&fpMaskL == 0 {
cf.buckets[idx] |= bucketL(fp) << s
return true
}
}
return false
}
// Insert add an item to the Filter and returns whether it was inserted or not.
//
// NB. The same item cannot be inserted more than 2 times.
func (cf *Filter64L) Insert(x uint64) bool {
return cf.insert(x, fpValue64L(x))
}
// insert adds an item with hash h and fingerprint fp.
func (cf *Filter64L) insert(h uint64, fp fingerprintL) bool {
// find an empty entry slot at the first index
i := cf.index1(h, fp)
if cf.insertAt(i, fp) {
return true
}
// find an empty entry slot at the second index
j := cf.index2(i, fp)
if cf.insertAt(j, fp) {
return true
}
// no empty slot, kick one entry and relocate it at its next index
// only do it so many times to avoid infinite loops
for r := 0; r < cf.max; r++ {
// kick the first entry located in the current bucket
pfp := fingerprintL(cf.buckets[i] & fpMaskL)
cf.buckets[i] &= ^bucketL(fpMaskL)
cf.buckets[i] |= bucketL(fp)
// find a new location for the previous fingerprint
fp = pfp
i = cf.index2(i, fp)
if cf.insertAt(i, fp) {
return true
}
}
// relocation impossible: restore the relocated items
// since the previous index can be computed via the
// current fingerprint and the current index
for r := 0; r < cf.max; r++ {
i = cf.index2(i, fp)
pfp := fingerprintL(cf.buckets[i] & fpMaskL)
cf.buckets[i] &= ^bucketL(fpMaskL)
cf.buckets[i] |= bucketL(fp)
fp = pfp
}
return false
}
// hasIn checks bucket b for fingerprint fp and returns whether it was found.
func (cf *Filter64L) hasIn(b bucketL, fp fingerprintL) bool {
for i := 0; i < bucketNumL; i++ {
if b>>uint(i*fpBitsNumL)&fpMaskL == bucketL(fp) {
return true
}
}
return false
}
// Has checks if the item is in the Filter.
// If it returns false, then the item is definitely not in the filter.
// If it returns true, then the item *may* be in the filter, although
// with a low probability of not being in it.
func (cf *Filter64L) Has(x uint64) bool {
return cf.has(x, fpValue64L(x))
}
// has checks if the item hash and fingerprint are found.
func (cf *Filter64L) has(h uint64, fp fingerprintL) bool {
// check fingerprint at first index
i := cf.index1(h, fp)
if cf.hasIn(cf.buckets[i], fp) {
return true
}
// check fingerprint at second index
i = cf.index2(i, fp)
return cf.hasIn(cf.buckets[i], fp)
}
// deleteAt removes fingerprint fp from the filter at index i and
// returns whether the fingerprint was found or not.
func (cf *Filter64L) deleteAt(idx uint64, fp fingerprintL) bool {
u := cf.buckets[idx]
for i := 0; i < bucketNumL; i++ {
s := uint(i * fpBitsNumL)
if u>>s&fpMaskL == bucketL(fp) {
cf.buckets[idx] &^= bucketL(fpMaskL) << s
return true
}
}
return false
}
// Delete removes an item from the Filter and returns whether or not it was present.
// To delete an item safely it must have been previously inserted.
func (cf *Filter64L) Delete(x uint64) bool {
return cf.delete(x, fpValue64L(x))
}
// delete removes the item with the corresponding hash and fingerprint.
func (cf *Filter64L) delete(h uint64, fp fingerprintL) bool {
// delete fingerprint at first index
i := cf.index1(h, fp)
if cf.deleteAt(i, fp) {
return true
}
// delete fingerprint at second index
i = cf.index2(i, fp)
return cf.deleteAt(i, fp)
}
// Len returns the number of items in the filter.
func (cf *Filter64L) Len() (n int) {
for _, b := range cf.buckets {
for ; b > 0; b >>= fpBitsNumL {
if b&fpMaskL > 0 {
n++
}
}
}
return
}
// Cap returns the filter capacity (maximum number of items it may contain).
func (cf *Filter64L) Cap() int {
return len(cf.buckets)
}