| 1 |
package cuckoo |
| 2 |
|
| 3 |
import ( |
| 4 |
"fmt" |
| 5 |
"math/bits" |
| 6 |
"math/rand" |
| 7 |
) |
| 8 |
|
| 9 |
const maxCuckooCount = 500 |
| 10 |
|
| 11 |
// Filter is a probabilistic counter |
| 12 |
type Filter struct { |
| 13 |
buckets []bucket |
| 14 |
count uint |
| 15 |
bucketPow uint |
| 16 |
} |
| 17 |
|
| 18 |
// NewFilter returns a new cuckoofilter with a given capacity. |
| 19 |
// A capacity of 1000000 is a normal default, which allocates |
| 20 |
// about ~1MB on 64-bit machines. |
| 21 |
func NewFilter(capacity uint) *Filter { |
| 22 |
capacity = getNextPow2(uint64(capacity)) / bucketSize |
| 23 |
if capacity == 0 { |
| 24 |
capacity = 1 |
| 25 |
} |
| 26 |
buckets := make([]bucket, capacity) |
| 27 |
return &Filter{ |
| 28 |
buckets: buckets, |
| 29 |
count: 0, |
| 30 |
bucketPow: uint(bits.TrailingZeros(capacity)), |
| 31 |
} |
| 32 |
} |
| 33 |
|
| 34 |
// Lookup returns true if data is in the counter |
| 35 |
func (cf *Filter) Lookup(data []byte) bool { |
| 36 |
i1, fp := getIndexAndFingerprint(data, cf.bucketPow) |
| 37 |
if cf.buckets[i1].getFingerprintIndex(fp) > -1 { |
| 38 |
return true |
| 39 |
} |
| 40 |
i2 := getAltIndex(fp, i1, cf.bucketPow) |
| 41 |
return cf.buckets[i2].getFingerprintIndex(fp) > -1 |
| 42 |
} |
| 43 |
|
| 44 |
// Reset ... |
| 45 |
func (cf *Filter) Reset() { |
| 46 |
for i := range cf.buckets { |
| 47 |
cf.buckets[i].reset() |
| 48 |
} |
| 49 |
cf.count = 0 |
| 50 |
} |
| 51 |
|
| 52 |
func randi(i1, i2 uint) uint { |
| 53 |
if rand.Intn(2) == 0 { |
| 54 |
return i1 |
| 55 |
} |
| 56 |
return i2 |
| 57 |
} |
| 58 |
|
| 59 |
// Insert inserts data into the counter and returns true upon success |
| 60 |
func (cf *Filter) Insert(data []byte) bool { |
| 61 |
i1, fp := getIndexAndFingerprint(data, cf.bucketPow) |
| 62 |
if cf.insert(fp, i1) { |
| 63 |
return true |
| 64 |
} |
| 65 |
i2 := getAltIndex(fp, i1, cf.bucketPow) |
| 66 |
if cf.insert(fp, i2) { |
| 67 |
return true |
| 68 |
} |
| 69 |
return cf.reinsert(fp, randi(i1, i2)) |
| 70 |
} |
| 71 |
|
| 72 |
// InsertUnique inserts data into the counter if not exists and returns true upon success |
| 73 |
func (cf *Filter) InsertUnique(data []byte) bool { |
| 74 |
if cf.Lookup(data) { |
| 75 |
return false |
| 76 |
} |
| 77 |
return cf.Insert(data) |
| 78 |
} |
| 79 |
|
| 80 |
func (cf *Filter) insert(fp fingerprint, i uint) bool { |
| 81 |
if cf.buckets[i].insert(fp) { |
| 82 |
cf.count++ |
| 83 |
return true |
| 84 |
} |
| 85 |
return false |
| 86 |
} |
| 87 |
|
| 88 |
func (cf *Filter) reinsert(fp fingerprint, i uint) bool { |
| 89 |
for k := 0; k < maxCuckooCount; k++ { |
| 90 |
j := rand.Intn(bucketSize) |
| 91 |
oldfp := fp |
| 92 |
fp = cf.buckets[i][j] |
| 93 |
cf.buckets[i][j] = oldfp |
| 94 |
|
| 95 |
// look in the alternate location for that random element |
| 96 |
i = getAltIndex(fp, i, cf.bucketPow) |
| 97 |
if cf.insert(fp, i) { |
| 98 |
return true |
| 99 |
} |
| 100 |
} |
| 101 |
return false |
| 102 |
} |
| 103 |
|
| 104 |
// Delete data from counter if exists and return if deleted or not |
| 105 |
func (cf *Filter) Delete(data []byte) bool { |
| 106 |
i1, fp := getIndexAndFingerprint(data, cf.bucketPow) |
| 107 |
if cf.delete(fp, i1) { |
| 108 |
return true |
| 109 |
} |
| 110 |
i2 := getAltIndex(fp, i1, cf.bucketPow) |
| 111 |
return cf.delete(fp, i2) |
| 112 |
} |
| 113 |
|
| 114 |
func (cf *Filter) delete(fp fingerprint, i uint) bool { |
| 115 |
if cf.buckets[i].delete(fp) { |
| 116 |
if cf.count > 0 { |
| 117 |
cf.count-- |
| 118 |
} |
| 119 |
return true |
| 120 |
} |
| 121 |
return false |
| 122 |
} |
| 123 |
|
| 124 |
// Count returns the number of items in the counter |
| 125 |
func (cf *Filter) Count() uint { |
| 126 |
return cf.count |
| 127 |
} |
| 128 |
|
| 129 |
// Encode returns a byte slice representing a Cuckoofilter |
| 130 |
func (cf *Filter) Encode() []byte { |
| 131 |
bytes := make([]byte, len(cf.buckets)*bucketSize) |
| 132 |
for i, b := range cf.buckets { |
| 133 |
for j, f := range b { |
| 134 |
index := (i * len(b)) + j |
| 135 |
bytes[index] = byte(f) |
| 136 |
} |
| 137 |
} |
| 138 |
return bytes |
| 139 |
} |
| 140 |
|
| 141 |
// Decode returns a Cuckoofilter from a byte slice |
| 142 |
func Decode(bytes []byte) (*Filter, error) { |
| 143 |
var count uint |
| 144 |
if len(bytes)%bucketSize != 0 { |
| 145 |
return nil, fmt.Errorf("expected bytes to be multiple of %d, got %d", bucketSize, len(bytes)) |
| 146 |
} |
| 147 |
buckets := make([]bucket, len(bytes)/4) |
| 148 |
for i, b := range buckets { |
| 149 |
for j := range b { |
| 150 |
index := (i * len(b)) + j |
| 151 |
if bytes[index] != 0 { |
| 152 |
buckets[i][j] = fingerprint(bytes[index]) |
| 153 |
count++ |
| 154 |
} |
| 155 |
} |
| 156 |
} |
| 157 |
return &Filter{ |
| 158 |
buckets: buckets, |
| 159 |
count: count, |
| 160 |
bucketPow: uint(bits.TrailingZeros(uint(len(buckets)))), |
| 161 |
}, nil |
| 162 |
} |