Skip to content

Commit 3ecfc46

Browse files
committed
Improving documentation for major release.
1 parent 9fcef70 commit 3ecfc46

File tree

2 files changed

+21
-16
lines changed

2 files changed

+21
-16
lines changed

cuckoofilter.go

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import (
1010
// is attempted.
1111
const maxCuckooKickouts = 500
1212

13-
// Filter is a probabilistic counter
13+
// Filter is a probabilistic counter.
1414
type Filter struct {
1515
buckets []bucket
1616
count uint
@@ -19,10 +19,10 @@ type Filter struct {
1919
bucketIndexMask uint
2020
}
2121

22-
// NewFilter returns a new cuckoofilter sutable for the given number of elements.
23-
// When inserting more elements, insertion speed will drop significantly.
22+
// NewFilter returns a new cuckoofilter suitable for the given number of elements.
23+
// When inserting more elements, insertion speed will drop significantly and insertions might fail altogether.
2424
// A capacity of 1000000 is a normal default, which allocates
25-
// about ~1MB on 64-bit machines.
25+
// about ~2MB on 64-bit machines.
2626
func NewFilter(numElements uint) *Filter {
2727
numBuckets := getNextPow2(uint64(numElements / bucketSize))
2828
if float64(numElements)/float64(numBuckets*bucketSize) > 0.96 {
@@ -58,14 +58,10 @@ func (cf *Filter) Reset() {
5858
cf.count = 0
5959
}
6060

61-
func randi(i1, i2 uint) uint {
62-
if rand.Int31()%2 == 0 {
63-
return i1
64-
}
65-
return i2
66-
}
67-
68-
// Insert inserts data into the counter and returns true upon success
61+
// Insert data into the filter. Returns false if insertion failed. In the resulting state, the filter
62+
// * Might return false negatives
63+
// * Deletes are not guaranteed to work
64+
// To increase success rate of inserts, create a larger filter.
6965
func (cf *Filter) Insert(data []byte) bool {
7066
i1, fp := getIndexAndFingerprint(data, cf.bucketIndexMask)
7167
if cf.insert(fp, i1) {
@@ -101,7 +97,7 @@ func (cf *Filter) reinsert(fp fingerprint, i uint) bool {
10197
return false
10298
}
10399

104-
// Delete data from counter if exists and return if deleted or not
100+
// Delete data from the filter. Returns true if the data was found and deleted.
105101
func (cf *Filter) Delete(data []byte) bool {
106102
i1, fp := getIndexAndFingerprint(data, cf.bucketIndexMask)
107103
i2 := getAltIndex(fp, i1, cf.bucketIndexMask)
@@ -116,7 +112,7 @@ func (cf *Filter) delete(fp fingerprint, i uint) bool {
116112
return false
117113
}
118114

119-
// Count returns the number of items in the counter
115+
// Count returns the number of items in the filter.
120116
func (cf *Filter) Count() uint {
121117
return cf.count
122118
}
@@ -126,7 +122,7 @@ func (cf *Filter) LoadFactor() float64 {
126122
return float64(cf.count) / float64(len(cf.buckets)*bucketSize)
127123
}
128124

129-
// Encode returns a byte slice representing a Cuckoofilter
125+
// Encode returns a byte slice representing a Cuckoofilter.
130126
func (cf *Filter) Encode() []byte {
131127
bytes := make([]byte, 0, len(cf.buckets)*bucketSize*fingerprintSizeBits/8)
132128
for _, b := range cf.buckets {
@@ -139,7 +135,7 @@ func (cf *Filter) Encode() []byte {
139135
return bytes
140136
}
141137

142-
// Decode returns a Cuckoofilter from a byte slice
138+
// Decode returns a Cuckoofilter from a byte slice created using Encode.
143139
func Decode(bytes []byte) (*Filter, error) {
144140
var count uint
145141
if len(bytes)%bucketSize != 0 {

util.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,19 @@ package cuckoo
22

33
import (
44
"encoding/binary"
5+
"math/rand"
56

67
metro "github.com/dgryski/go-metro"
78
)
89

10+
// randi returns either i1 or i2 randomly.
11+
func randi(i1, i2 uint) uint {
12+
if rand.Int31()%2 == 0 {
13+
return i1
14+
}
15+
return i2
16+
}
17+
918
func getAltIndex(fp fingerprint, i uint, bucketIndexMask uint) uint {
1019
b := make([]byte, 2)
1120
binary.LittleEndian.PutUint16(b, uint16(fp))

0 commit comments

Comments
 (0)