Documentation ¶
Overview ¶
Example ¶
package main import ( "fmt" "github.com/rbee3u/golib/bloomfilter" ) func main() { bf, err := bloomfilter.NewWithEstimate(1000000, 0.03) if err != nil { fmt.Printf("failed to new Bloom filter: %v\n", err) return } bf.Add([]byte("foo")) for _, item := range []string{"foo", "bar"} { if bf.Contains([]byte(item)) { fmt.Printf("%s is possibly in the set\n", item) } else { fmt.Printf("%s is definitely not in the set\n", item) } } }
Output: foo is possibly in the set bar is definitely not in the set
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ErrInvalidArgument = errors.New("invalid argument")
ErrInvalidArgument represents an invalid argument error.
Functions ¶
Types ¶
type BloomFilter ¶
type BloomFilter struct { // A mutex to let concurrency. sync.RWMutex // contains filtered or unexported fields }
BloomFilter implements [Bloom filter](https://en.wikipedia.org/wiki/Bloom_filter), a space-efficient probabilistic data structure used to test whether an item is a member of a set. False positive matches are possible, but false negatives are not. In other words, a query returns either "possibly in set" or "definitely not in set". Elements can be added to the set, but not removed (though this can be addressed with the counting Bloom filter variant); the more elements that are added to the set, the larger the probability of false positives.
func New ¶
func New(m uint64, k uint64, opts ...Option) (*BloomFilter, error)
New creates a Bloom filter with `m` bits storage and `k` hash functions.
func NewWithEstimate ¶
func NewWithEstimate(n uint64, p float64, opts ...Option) (*BloomFilter, error)
NewWithEstimate creates a Bloom filter for about `n` items with `p` false positive possibility.
func (*BloomFilter) Add ¶
func (bf *BloomFilter) Add(item []byte)
Add adds item to the Bloom filter.
func (*BloomFilter) AddWithoutLock ¶
func (bf *BloomFilter) AddWithoutLock(item []byte)
AddWithoutLock is same with Add, but without lock.
func (*BloomFilter) Contains ¶
func (bf *BloomFilter) Contains(item []byte) bool
Contains returns true if the item is in the Bloom filter, false otherwise. If true, the result might be a false positive. If false, the item is definitely not in the set.
func (*BloomFilter) ContainsWithoutLock ¶
func (bf *BloomFilter) ContainsWithoutLock(item []byte) bool
ContainsWithoutLock is same with Contains, but without lock.
type Option ¶
type Option func(*BloomFilter)
Option represents the option when creating a Bloom filter.