forked from mtchavez/cuckoo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cuckoo.go
193 lines (176 loc) · 3.91 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
package cuckoo
import (
"encoding/binary"
"sync"
farm "github.com/dgryski/go-farm"
)
const magicNumber uint64 = 0x5bd1e995
// Filter ...
//
// Cuckoo filter type
type Filter struct {
sync.RWMutex
buckets []bucket
bucketEntries uint
bucketTotal uint
capacity uint
count uint
fingerprintLength uint
kicks uint
}
// New ...
//
// Create a new Filter with an optional set of ConfigOption to configure settings.
//
// Example: New Filter with custom config option
//
// New(FingerprintLength(4))
//
// Example: New Filter with default config
//
// New()
//
// returns a Filter type
func New(opts ...ConfigOption) (filter *Filter) {
filter = &Filter{}
for _, option := range opts {
option(filter)
}
filter.configureDefaults()
filter.createBuckets()
return
}
// Insert ...
//
// Add a new item of []byte to a Filter
//
// Example:
//
// filter.Insert([]byte("new-item"))
//
// returns a boolean of whether the item was inserted or not
func (f *Filter) Insert(item []byte) bool {
fp := newFingerprint(item, f.fingerprintLength)
i1 := uint(farm.Hash64(item)) % f.capacity
i2 := f.alternateIndex(fp, i1)
if f.insert(fp, i1) || f.insert(fp, i2) {
return true
}
return f.relocationInsert(fp, i2)
}
// InsertUnique ...
//
// Add a new item of []byte to a Filter only if it doesn't already exist.
// Will do a Lookup of item first.
//
// Example:
//
// filter.InsertUnique([]byte("new-item"))
//
// returns a boolean of whether the item was inserted or not
func (f *Filter) InsertUnique(item []byte) bool {
if f.Lookup(item) {
return true
}
return f.Insert(item)
}
// Lookup ...
//
// Check if an item of []byte exists in the Filter
//
// Example:
//
// filter.Lookup([]byte("new-item"))
//
// returns a boolean of whether the item exists or not
func (f *Filter) Lookup(item []byte) bool {
fp := newFingerprint(item, f.fingerprintLength)
i1 := uint(farm.Hash64(item)) % f.capacity
i2 := f.alternateIndex(fp, i1)
if f.lookup(fp, i1) || f.lookup(fp, i2) {
return true
}
return false
}
// Delete ...
//
// Delete an item of []byte if it exists in the Filter
//
// Example:
//
// filter.Delete([]byte("new-item"))
//
// returns a boolean of whether the item was deleted or not
func (f *Filter) Delete(item []byte) bool {
fp := newFingerprint(item, f.fingerprintLength)
i1 := uint(farm.Hash64(item)) % f.capacity
i2 := f.alternateIndex(fp, i1)
if f.delete(fp, i1) || f.delete(fp, i2) {
return true
}
return false
}
// ItemCount ...
//
// Get an estimate of the total items in the Filter. Could be drastically off
// if using Insert with many duplicate items. To get a more accurate total
// using InsertUnique can be used
//
// Example:
//
// filter.ItemCount()
//
// returns an uint of the total items in the Filter
func (f *Filter) ItemCount() uint {
return f.count
}
func (f *Filter) insert(fp fingerprint, idx uint) bool {
f.Lock()
defer f.Unlock()
if f.buckets[idx].insert(fp) {
f.count++
return true
}
return false
}
func (f *Filter) relocationInsert(fp fingerprint, i uint) bool {
f.Lock()
defer f.Unlock()
for k := uint(0); k < f.kicks; k++ {
f.buckets[i].relocate(fp)
i = f.alternateIndex(fp, i)
if f.buckets[i].insert(fp) {
f.count++
return true
}
}
return false
}
func (f *Filter) lookup(fp fingerprint, i uint) bool {
if f.buckets[i].lookup(fp) {
return true
}
return false
}
func (f *Filter) delete(fp fingerprint, idx uint) bool {
if f.buckets[idx].delete(fp) {
f.count--
return true
}
return false
}
func (f *Filter) createBuckets() {
buckets := make([]bucket, f.capacity, f.capacity)
for i := range buckets {
buckets[i] = make([]fingerprint, f.bucketEntries, f.bucketEntries)
}
f.buckets = buckets
}
func (f *Filter) alternateIndex(fp fingerprint, i uint) uint {
bytes := make([]byte, 64, 64)
for i, b := range fp {
bytes[i] = b
}
hash := binary.LittleEndian.Uint64(bytes)
return uint(uint64(i)^(hash*magicNumber)) % f.capacity
}