bloomfilter

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Feb 17, 2023 License: MIT Imports: 9 Imported by: 0

README

go-bloomfilter

GoDoc

bloomfilter for golang

Containers

// Implement this interface and make a custom container
bloomfilter.BFContainer

// Support
bloomfilter.NewMemoryBloomFilter
bloomfilter.NewRedisContainer
bloomfilter.NewRedisClusterContainer

Example

// option 1: memorty bloom filter
bf := bloomfilter.NewMemoryBloomFilter(100000, 0.001)

// option 2: redis bloom filter
rdc := container.NewRedisContainer(&redis.Options{}, "bf_demo", 2)
bf := bloomfilter.NewBloomFilter(100000, 0.001, rdc)

// option 3: redis bloom filter
rdc1 := container.NewRedisClusterContainer(&redis.ClusterOptions{}, "bf_demo", 2)
bf := bloomfilter.NewBloomFilter(100000, 0.001, rdc1)

bf.Add([]byte("hello"))
bf.Add([]byte("world"))
fmt.Printf("Capacity: %d\n", bf.Capacity())
fmt.Printf("Size: %d\n", bf.Size())
fmt.Printf("Error: %f\n", bf.Error())
fmt.Printf("Exists[hello]: %v\n", bf.Contains([]byte("hello")))
fmt.Printf("Exists[world]: %v\n", bf.Contains([]byte("world")))
fmt.Printf("Exists[golang]: %v\n", bf.Contains([]byte("golang")))

// save bloomfilter to file
writer, err := os.OpenFile("./test.bf", os.O_WRONLY|os.O_CREATE, 0666)
if err != nil {
    log.Fatalf("write failed failed: %v", err)
}
bf.Save(writer)

/*
Load bloomfilter from File

It is necessary to ensure that the two values ​​of hashFunction and container.GetMaxBitSize() cannot be changed, otherwise the bloomfilter of load cannot work correctly
*/ 
fmt.Println("Load Bloom Filter")
bf2, err := bloomfilter.LoadBloomFilter("./test.bf", container.NewMemoryContainer(), nil)
if err != nil {
    log.Fatalf("load bloom filter failed: %v", err)
}
fmt.Printf("Exists[hello]: %v\n", bf2.Contains([]byte("hello")))
fmt.Printf("Exists[world]: %v\n", bf2.Contains([]byte("world")))
fmt.Printf("Exists[golang]: %v\n", bf2.Contains([]byte("golang")))
fmt.Printf("Capacity: %d\n", bf2.Capacity())
fmt.Printf("Size: %d\n", bf2.Size())
fmt.Printf("Error: %f\n", bf2.Error())

Run Test

go test

License

MIT License Copyright (c) 2023 coderjun

Documentation

Index

Constants

This section is empty.

Variables

View Source
var DefaultHash = hash.BFSipHash

Functions

func LoadBloomFilter added in v0.0.5

func LoadBloomFilter(filename string, container container.BFContainer, opts ...Option) (*baseBloomFilter, error)

func NewBloomFilter

func NewBloomFilter(capacity int64, errorRate float64, container container.BFContainer, opts ...Option) *baseBloomFilter

func NewMemoryBloomFilter

func NewMemoryBloomFilter(capacity int64, errorRate float64, opts ...Option) *baseBloomFilter

Types

type Option added in v1.0.0

type Option func(*baseBloomFilter)

func WithHashFunction added in v0.0.5

func WithHashFunction(hashFunc hash.BFHash) Option

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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