This is a simple and thread-safe bloom filter implemented by Go.
Other than the classic Exist
, Set
and Reset
, some more estimation functions are implemented that makes this bloom filter implementation very easy and straight forward to use
in real applications.
- Go version 1.18+
With Go module support, simply add the following import
import "github.com/ALong1997/bloomfilter"
Otherwise, run the following Go command to install the bloomfilter
package:
$ go get -u https://github.com/ALong1997/bloomfilter
package main
import (
"fmt"
"hash"
"hash/crc32"
"hash/fnv"
"github.com/ALong1997/bloomfilter"
)
type hash32 struct{ hash.Hash32 }
func (h *hash32) Encrypt(origin []byte) uint32 {
defer h.Reset()
h.Write(origin)
return h.Sum32()
}
func main() {
bf := bloomfilter.NewBloomFilter(100000, []bloomfilter.Encryptor{&hash32{crc32.NewIEEE()}, &hash32{fnv.New32()}}, false)
bf.Set([]byte("hello world"))
bf.Set([]byte("hello world!"))
// true
fmt.Println(bf.Exist([]byte("hello world")))
// false
fmt.Println(bf.Exist([]byte("bloom filter")))
// 100000
fmt.Println(bf.M())
// 2
fmt.Println(bf.N())
// 2
fmt.Println(bf.K())
bf.Reset()
}