bloom

package module
v0.0.0-...-98c0524 Latest Latest
Warning

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

Go to latest
Published: Sep 15, 2018 License: Apache-2.0 Imports: 4 Imported by: 7

README

Bloom

This project bloom filter implementation using Murmur3 hash by github.com/spaolacci/murmur3.

Usage

Importing package
import "github.com/AccelByte/bloom"
Creating new bloom filter
// create new filter with size of 100
// with default Murmur3 hashing strategy
// and 1.e-5 expected false positive probability
b := bloom.New(100)
Putting item into bloom filter
b.Put([]byte("an_item"))
Checking if an item exists
b.MightContain([]byte("an_item"))
Exporting bloom filter to JSON
exported, _ := b.MarshalJSON()
Constructing bloom filter from exported JSON
bloomFilterJSON := &bloom.FilterJSON{}
json.Unmarshal(exported, bloomFilterJSON)
newB := bloom.From(bloomFilterJSON.B, bloomFilterJSON.K)

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func EstimateParameters

func EstimateParameters(n uint, p float64) (m uint, k uint)

EstimateParameters estimates requirements for m and k. n: expected insertions p: expected false positive probability

Types

type Filter

type Filter struct {
	// contains filtered or unexported fields
}

Filter is wrapper for bloom filter. Cheat sheet:

m: total bits n: expected insertions b: m/n, bits per insertion p: expected false positive probability k: number of hash functions

1) Optimal k = b * ln2 2) p = (1 - e ^ (-kn/m))^k 3) For optimal k: p = 2 ^ (-k) ~= 0.6185^b 4) For optimal k: m = -nlnp / ((ln2) ^ 2)

func From

func From(bits []uint64, k uint) *Filter

From populates a bloom filter. bits: long array represents bits k: number of hash functions

func FromWithStrategy

func FromWithStrategy(bits []uint64, k uint, s Strategy) *Filter

FromWithStrategy populates a bloom filter with strategy. bits: long array represents bits k: number of hash functions s: index strategy

func New

func New(n uint) *Filter

New creates a new bloom filter with default FPP n: expected insertions

func NewWithFPP

func NewWithFPP(n uint, p float64) *Filter

NewWithFPP creates a new bloom filter. n: expected insertions p: expected false positive probability

func NewWithStrategy

func NewWithStrategy(n uint, p float64, s Strategy) *Filter

NewWithStrategy creates a new bloom filter with strategy. n: expected insertions p: expected false positive probability s: index strategy

func (*Filter) B

func (f *Filter) B() []uint64

B returns the filter's bitset in []uint64 format.

func (*Filter) K

func (f *Filter) K() uint

K returns the number of hash functions used in the Filter.

func (*Filter) M

func (f *Filter) M() uint

M returns the capacity of a Bloom filter.

func (*Filter) MarshalJSON

func (f *Filter) MarshalJSON() ([]byte, error)

MarshalJSON implements json.Marshaler interface.

func (*Filter) MightContain

func (f *Filter) MightContain(data []byte) bool

MightContain query data existence.

func (*Filter) Put

func (f *Filter) Put(data []byte) *Filter

Put data to the Bloom Filter. Returns the filter (allows chaining)

func (*Filter) SetStrategy

func (f *Filter) SetStrategy(s Strategy)

SetStrategy sets strategy. Do not set it manually except only you unmarshal a bloom filter that using different strategy.

func (*Filter) UnmarshalJSON

func (f *Filter) UnmarshalJSON(data []byte) error

UnmarshalJSON implements json.Unmarshaler interface.

type FilterJSON

type FilterJSON struct {
	M uint     `json:"m"`
	K uint     `json:"k"`
	B []uint64 `json:"bits"`
}

FilterJSON class wrapper for marshaling/unmarshaling Filter struct.

type MURMUR128MITZ64

type MURMUR128MITZ64 struct {
}

MURMUR128MITZ64 is a hashing strategy using murmur3

func (*MURMUR128MITZ64) Indexes

func (strategy *MURMUR128MITZ64) Indexes(data []byte, m uint, k uint) []uint

Indexes gets indexes.

type Strategy

type Strategy interface {
	// Indexes gets indexes.
	Indexes(data []byte, m uint, k uint) []uint
}

Strategy a strategy to translate byte array to k bit indexes.

Jump to

Keyboard shortcuts

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