项目作者: mtchavez

项目描述 :
Cuckoo Filter in golang for set membership approximation
高级语言: Go
项目地址: git://github.com/mtchavez/cuckoo.git
创建时间: 2017-06-30T04:51:30Z
项目社区:https://github.com/mtchavez/cuckoo

开源协议:Other

下载


Cuckoo Filter

Latest Version
Test
Go Documentation
Go Report Card
Maintainability
Test Coverage

Cuckoo Filter in Go

Install

Install via go get

go get -v github.com/mtchavez/cuckoo

Usage

New Filter

Create a new filter with default configuration

  1. package main
  2. import "github.com/mtchavez/cuckoo"
  3. func main() {
  4. cuckoo.New()
  5. }

Configuration

You can configure a filter via a ConfigOption type and the composed config option
functions provided.

  1. package main
  2. import "github.com/mtchavez/cuckoo"
  3. func main() {
  4. options := []cuckoo.ConfigOption{
  5. cuckoo.BucketEntries(uint(24)),
  6. cuckoo.BucketTotal(uint(1 << 16)),
  7. cuckoo.FingerprintLength(uint(1)),
  8. cuckoo.Kicks(uint(250)),
  9. }
  10. cuckoo.New(options...)
  11. }

Insert

Inserting items into a filter

  1. package main
  2. import "github.com/mtchavez/cuckoo"
  3. func main() {
  4. filter := cuckoo.New()
  5. filter.Insert([]byte("special-items"))
  6. }

Insert Unique

Inserting items into a filter only if they do not already exist

  1. package main
  2. import (
  3. "fmt"
  4. "github.com/mtchavez/cuckoo"
  5. )
  6. func main() {
  7. filter := cuckoo.New()
  8. filter.InsertUnique([]byte("special-items"))
  9. filter.InsertUnique([]byte("special-items"))
  10. if filter.ItemCount() != 1 {
  11. fmt.Println("Expected only 1 item")
  12. }
  13. }

Lookup

Check if items exist in the filter using Lookup

  1. package main
  2. import (
  3. "fmt"
  4. "github.com/mtchavez/cuckoo"
  5. )
  6. func main() {
  7. filter := cuckoo.New()
  8. filter.Insert([]byte("special-items"))
  9. found := filter.Lookup([]byte("special-items"))
  10. if !found {
  11. fmt.Println("Expected to find item in filter")
  12. }
  13. }

Delete

Deleting an item if it exists in the filter

  1. package main
  2. import (
  3. "fmt"
  4. "github.com/mtchavez/cuckoo"
  5. )
  6. func main() {
  7. filter := cuckoo.New()
  8. filter.Insert([]byte("special-items"))
  9. deleted := filter.Delete([]byte("special-items"))
  10. if !deleted {
  11. fmt.Println("Expected to delete item from filter")
  12. }
  13. }

Item Count

Getting the item count of filter. Using Insert with duplicates will cause the
item count to be more like a total items inserted count
. Using InsertUnique
and checking the ItemCount will be more of a real item count.

  1. package main
  2. import (
  3. "fmt"
  4. "github.com/mtchavez/cuckoo"
  5. )
  6. func main() {
  7. filter := cuckoo.New()
  8. filter.InsertUnique([]byte("special-items"))
  9. filter.InsertUnique([]byte("special-items"))
  10. if filter.ItemCount() != 1 {
  11. fmt.Println("Expected only 1 item")
  12. }
  13. }

Save

Encode and save a filter to be able to re-build or re-load back into memory.

  1. package main
  2. import (
  3. "fmt"
  4. "github.com/mtchavez/cuckoo"
  5. )
  6. func main() {
  7. filter := New()
  8. item := []byte("Largo")
  9. filter.InsertUnique(item)
  10. filter.Save("./tmp/example_save.gob")
  11. }

Load

Load a filter back into memory from an encoded filter saved to a file.

  1. package main
  2. import (
  3. "fmt"
  4. "github.com/mtchavez/cuckoo"
  5. )
  6. func main() {
  7. filter := New()
  8. item := []byte("Largo")
  9. filter.InsertUnique(item)
  10. filter.Save("./tmp/example_save.gob")
  11. loadedFilter, _ := Load("./tmp/example_save.gob")
  12. fmt.Printf("Loaded filter has same item? %v\n\n", loadedFilter.Lookup(item))
  13. }

Benchmarks

There are benchmark tests to check performance of the filter. The following results
were ran on a 2.3 GHz Intel Core i7

  1. # Updated: 2022-07-01
  2. goos: darwin
  3. goarch: arm64
  4. pkg: github.com/mtchavez/cuckoo
  5. BenchmarkCuckooNew-10 48 23354917 ns/op
  6. BenchmarkInsert-10 3342568 806.5 ns/op
  7. BenchmarkInsertUnique-10 6203035 194.7 ns/op
  8. BenchmarkLookup-10 6465182 196.3 ns/op

Tests

Run tests via go test or with provided Makefile

go test -v -cover ./... or make test