cuckoo

package module
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 21, 2017 License: MIT Imports: 4 Imported by: 0

README

GoDoc Go Report Card Build Status

Cuckoo Filter: Practically Better Than Bloom

Package cuckoo provides a Cuckoo Filter (Practically Better Than Bloom). Cuckoo filters provide the flexibility to add and remove items dynamically. A cuckoo filter is based on cuckoo hashing (and therefore named as cuckoo filter). It is essentially a cuckoo hash table storing each key's fingerprint.

Implementation is based heavily on whitepaper: "Cuckoo Filter: Better Than Bloom" by Bin Fan, Dave Andersen and Michael Kaminsky (https://www.cs.cmu.edu/~dga/papers/cuckoo-conext2014.pdf).

Example
    var item []byte

    // Create a new filter with 32MB capacity
    f := cuckoo.NewFilter(32 * 1024 * 1024)

    if f.Lookup(item) {
        if !f.Delete(item) {
            // Cannot delete the item
            // ...
        }
    } else {
        if err := f.Insert(item); err != nil {
            // Hashtable is considered full
            // ...
        }
    }
Fuzzing - randomized testing
  • get go-fuzz
    $ go get github.com/dvyukov/go-fuzz/go-fuzz
    $ go get github.com/dvyukov/go-fuzz/go-fuzz-build
  • build the test program with necessary instrumentation
    $ go-fuzz-build github.com/kuba--/cuckoo/fuzz/cuckoo
  • ready to go
    $ go-fuzz -bin=./cuckoo-fuzz.zip -workdir=/tmp/cuckoo

Documentation

Overview

Package cuckoo provides a Cuckoo Filter (Practically Better Than Bloom). Cuckoo filters provide the flexibility to add and remove items dynamically. A cuckoo filter is based on cuckoo hashing (and therefore named as cuckoo filter). It is essentially a cuckoo hash table storing each key's fingerprint.

Implementation is based heavily on whitepaper: "Cuckoo Filter: Better Than Bloom" by Bin Fan, Dave Andersen and Michael Kaminsky (https://www.cs.cmu.edu/~dga/papers/cuckoo-conext2014.pdf).

Index

Constants

This section is empty.

Variables

View Source
var DefaultHash32 = fnv.New32a()

DefaultHash32 is a default hash function.

View Source
var MaxNumKicks uint32 = 512

MaxNumKicks stands for maximum number of cuckoo kicks before insert failure.

Functions

This section is empty.

Types

type Filter

type Filter struct {
	// contains filtered or unexported fields
}

Filter keeps hashtable with fingerprints.

func NewFilter

func NewFilter(capacity uint32) *Filter

NewFilter creates a new Cuckoo Filter with given capacity (rounding up to the nearest power of 2).

func (*Filter) Count

func (f *Filter) Count() uint32

Count returns how many items are in the filter.

func (*Filter) Delete

func (f *Filter) Delete(item []byte) bool

Delete deletes an item from the filter. Pseudo code:

f = fingerprint(x);
i1 = hash(x);
i2 = i1 ⊕ hash(f);
if bucket[i1] or bucket[i2] has f then
	remove a copy of f from this bucket;
	return True;
return False;

func (*Filter) Insert

func (f *Filter) Insert(item []byte) error

Insert inserts an item to the filter. Pseudo code:

f = fingerprint(x);
i1 = hash(x);
i2 = i1 ⊕ hash(f);
if bucket[i1] or bucket[i2] has an empty entry then
	add f to that bucket;
	return Done;

// must relocate existing items;
i = randomly pick i1 or i2;
for n = 0; n < MaxNumKicks; n++ do
	randomly select an entry e from bucket[i];
	swap f and the fingerprint stored in entry e;
	i = i ⊕ hash( f );

if bucket[i] has an empty entry then
	add f to bucket[i];
	return Done;

// Hashtable is considered full;
return Failure;

func (*Filter) Lookup

func (f *Filter) Lookup(item []byte) bool

Lookup reports if the item is inserted, with false positive rate. Pseudo code:

f = fingerprint(x);
i1 = hash(x);
i2 = i1 ⊕ hash(f);
if bucket[i1] or bucket[i2] has f then
	return True;
return False;

Directories

Path Synopsis
fuzz

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL