-
Notifications
You must be signed in to change notification settings - Fork 49
/
xorfilter.go
301 lines (266 loc) · 7.37 KB
/
xorfilter.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
package xorfilter
import (
"errors"
"math"
"sort"
)
func murmur64(h uint64) uint64 {
h ^= h >> 33
h *= 0xff51afd7ed558ccd
h ^= h >> 33
h *= 0xc4ceb9fe1a85ec53
h ^= h >> 33
return h
}
// returns random number, modifies the seed
func splitmix64(seed *uint64) uint64 {
*seed = *seed + 0x9E3779B97F4A7C15
z := *seed
z = (z ^ (z >> 30)) * 0xBF58476D1CE4E5B9
z = (z ^ (z >> 27)) * 0x94D049BB133111EB
return z ^ (z >> 31)
}
func mixsplit(key, seed uint64) uint64 {
return murmur64(key + seed)
}
func rotl64(n uint64, c int) uint64 {
return (n << uint(c&63)) | (n >> uint((-c)&63))
}
func reduce(hash, n uint32) uint32 {
// http://lemire.me/blog/2016/06/27/a-fast-alternative-to-the-modulo-reduction/
return uint32((uint64(hash) * uint64(n)) >> 32)
}
func fingerprint(hash uint64) uint64 {
return hash ^ (hash >> 32)
}
// Contains tell you whether the key is likely part of the set
func (filter *Xor8) Contains(key uint64) bool {
hash := mixsplit(key, filter.Seed)
f := uint8(fingerprint(hash))
r0 := uint32(hash)
r1 := uint32(rotl64(hash, 21))
r2 := uint32(rotl64(hash, 42))
h0 := reduce(r0, filter.BlockLength)
h1 := reduce(r1, filter.BlockLength) + filter.BlockLength
h2 := reduce(r2, filter.BlockLength) + 2*filter.BlockLength
return f == (filter.Fingerprints[h0] ^ filter.Fingerprints[h1] ^ filter.Fingerprints[h2])
}
func (filter *Xor8) geth0h1h2(k uint64) hashes {
hash := mixsplit(k, filter.Seed)
answer := hashes{}
answer.h = hash
r0 := uint32(hash)
r1 := uint32(rotl64(hash, 21))
r2 := uint32(rotl64(hash, 42))
answer.h0 = reduce(r0, filter.BlockLength)
answer.h1 = reduce(r1, filter.BlockLength)
answer.h2 = reduce(r2, filter.BlockLength)
return answer
}
func (filter *Xor8) geth0(hash uint64) uint32 {
r0 := uint32(hash)
return reduce(r0, filter.BlockLength)
}
func (filter *Xor8) geth1(hash uint64) uint32 {
r1 := uint32(rotl64(hash, 21))
return reduce(r1, filter.BlockLength)
}
func (filter *Xor8) geth2(hash uint64) uint32 {
r2 := uint32(rotl64(hash, 42))
return reduce(r2, filter.BlockLength)
}
// scan for values with a count of one
func scanCount(Qi []keyindex, setsi []xorset) ([]keyindex, int) {
QiSize := 0
// len(setsi) = filter.BlockLength
for i := uint32(0); i < uint32(len(setsi)); i++ {
if setsi[i].count == 1 {
Qi[QiSize].index = i
Qi[QiSize].hash = setsi[i].xormask
QiSize++
}
}
return Qi, QiSize
}
// fill setsi to xorset{0, 0}
func resetSets(setsi []xorset) []xorset {
for i := range setsi {
setsi[i] = xorset{0, 0}
}
return setsi
}
// The maximum number of iterations allowed before the populate function returns an error
var MaxIterations = 1024
// Populate fills the filter with provided keys. For best results,
// the caller should avoid having too many duplicated keys.
// The function may return an error if the set is empty.
func Populate(keys []uint64) (*Xor8, error) {
size := len(keys)
if size == 0 {
return nil, errors.New("provide a non-empty set")
}
capacity := 32 + uint32(math.Ceil(1.23*float64(size)))
capacity = capacity / 3 * 3 // round it down to a multiple of 3
filter := &Xor8{}
var rngcounter uint64 = 1
filter.Seed = splitmix64(&rngcounter)
filter.BlockLength = capacity / 3
// slice capacity defaults to length
filter.Fingerprints = make([]uint8, capacity)
stack := make([]keyindex, size)
Q0 := make([]keyindex, filter.BlockLength)
Q1 := make([]keyindex, filter.BlockLength)
Q2 := make([]keyindex, filter.BlockLength)
sets0 := make([]xorset, filter.BlockLength)
sets1 := make([]xorset, filter.BlockLength)
sets2 := make([]xorset, filter.BlockLength)
iterations := 0
for {
iterations += 1
if iterations > MaxIterations {
// The probability of this happening is lower than the
// the cosmic-ray probability (i.e., a cosmic ray corrupts your system).
return nil, errors.New("too many iterations")
}
for i := 0; i < size; i++ {
key := keys[i]
hs := filter.geth0h1h2(key)
sets0[hs.h0].xormask ^= hs.h
sets0[hs.h0].count++
sets1[hs.h1].xormask ^= hs.h
sets1[hs.h1].count++
sets2[hs.h2].xormask ^= hs.h
sets2[hs.h2].count++
}
// scan for values with a count of one
Q0, Q0size := scanCount(Q0, sets0)
Q1, Q1size := scanCount(Q1, sets1)
Q2, Q2size := scanCount(Q2, sets2)
stacksize := 0
for Q0size+Q1size+Q2size > 0 {
for Q0size > 0 {
Q0size--
keyindexvar := Q0[Q0size]
index := keyindexvar.index
if sets0[index].count == 0 {
continue // not actually possible after the initial scan.
}
hash := keyindexvar.hash
h1 := filter.geth1(hash)
h2 := filter.geth2(hash)
stack[stacksize] = keyindexvar
stacksize++
sets1[h1].xormask ^= hash
sets1[h1].count--
if sets1[h1].count == 1 {
Q1[Q1size].index = h1
Q1[Q1size].hash = sets1[h1].xormask
Q1size++
}
sets2[h2].xormask ^= hash
sets2[h2].count--
if sets2[h2].count == 1 {
Q2[Q2size].index = h2
Q2[Q2size].hash = sets2[h2].xormask
Q2size++
}
}
for Q1size > 0 {
Q1size--
keyindexvar := Q1[Q1size]
index := keyindexvar.index
if sets1[index].count == 0 {
continue
}
hash := keyindexvar.hash
h0 := filter.geth0(hash)
h2 := filter.geth2(hash)
keyindexvar.index += filter.BlockLength
stack[stacksize] = keyindexvar
stacksize++
sets0[h0].xormask ^= hash
sets0[h0].count--
if sets0[h0].count == 1 {
Q0[Q0size].index = h0
Q0[Q0size].hash = sets0[h0].xormask
Q0size++
}
sets2[h2].xormask ^= hash
sets2[h2].count--
if sets2[h2].count == 1 {
Q2[Q2size].index = h2
Q2[Q2size].hash = sets2[h2].xormask
Q2size++
}
}
for Q2size > 0 {
Q2size--
keyindexvar := Q2[Q2size]
index := keyindexvar.index
if sets2[index].count == 0 {
continue
}
hash := keyindexvar.hash
h0 := filter.geth0(hash)
h1 := filter.geth1(hash)
keyindexvar.index += 2 * filter.BlockLength
stack[stacksize] = keyindexvar
stacksize++
sets0[h0].xormask ^= hash
sets0[h0].count--
if sets0[h0].count == 1 {
Q0[Q0size].index = h0
Q0[Q0size].hash = sets0[h0].xormask
Q0size++
}
sets1[h1].xormask ^= hash
sets1[h1].count--
if sets1[h1].count == 1 {
Q1[Q1size].index = h1
Q1[Q1size].hash = sets1[h1].xormask
Q1size++
}
}
}
if stacksize == size {
// success
break
}
if iterations == 10 {
keys = pruneDuplicates(keys)
size = len(keys)
}
sets0 = resetSets(sets0)
sets1 = resetSets(sets1)
sets2 = resetSets(sets2)
filter.Seed = splitmix64(&rngcounter)
}
stacksize := size
for stacksize > 0 {
stacksize--
ki := stack[stacksize]
val := uint8(fingerprint(ki.hash))
if ki.index < filter.BlockLength {
val ^= filter.Fingerprints[filter.geth1(ki.hash)+filter.BlockLength] ^ filter.Fingerprints[filter.geth2(ki.hash)+2*filter.BlockLength]
} else if ki.index < 2*filter.BlockLength {
val ^= filter.Fingerprints[filter.geth0(ki.hash)] ^ filter.Fingerprints[filter.geth2(ki.hash)+2*filter.BlockLength]
} else {
val ^= filter.Fingerprints[filter.geth0(ki.hash)] ^ filter.Fingerprints[filter.geth1(ki.hash)+filter.BlockLength]
}
filter.Fingerprints[ki.index] = val
}
return filter, nil
}
func pruneDuplicates(array []uint64) []uint64 {
sort.Slice(array, func(i, j int) bool {
return array[i] < array[j]
})
pos := 0
for i := 1; i < len(array); i++ {
if array[i] != array[pos] {
array[pos+1] = array[i]
pos += 1
}
}
return array[:pos+1]
}