bloomfilter

package
v0.0.0-...-b4c42aa Latest Latest
Warning

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

Go to latest
Published: Jan 7, 2023 License: BSD-2-Clause Imports: 5 Imported by: 0

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

View Source
var ErrInvalidArgument = errors.New("invalid argument")

ErrInvalidArgument represents an invalid argument error.

Functions

func EstimateParameters

func EstimateParameters(n uint64, p float64) (uint64, uint64)

EstimateParameters estimates the capacity of bitset `m` and the number of hash functions `k` for about `n` items with `p` false positive possibility.

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 Hasher

type Hasher func([]byte) (uint64, uint64)

A Hasher transform a byte slice into two uint64(128 bits).

type Option

type Option func(*BloomFilter)

Option represents the option when creating a Bloom filter.

func WithHasher

func WithHasher(h Hasher) Option

WithHasher creates an option of hasher.

Jump to

Keyboard shortcuts

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