bloomfilter

package
v0.6.0 Latest Latest
Warning

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

Go to latest
Published: Aug 23, 2022 License: MIT Imports: 4 Imported by: 0

Documentation

Index

Constants

View Source
const (
	MaxInt = 1<<(intSize-1) - 1
)

Integer limit values.

Variables

View Source
var (
	// ErrArguments returns by BloomFilter interface.
	ErrArguments = errors.New("bad arguments")
	// ErrType returns by BloomFilter interface.
	ErrType = errors.New("bad type")
)

Functions

func OptimalNumOfBitsAndNumOfHashFunctions

func OptimalNumOfBitsAndNumOfHashFunctions(
	expectInsertions int64, probabilityOfFalsePositives float64) (int64, int, error)

OptimalNumOfBitsAndNumOfHashFunctions gets the best number of bits and the number of hash functions.

It uses

k = m / n * ln(2);

which k is false positive probability, m is the number of bits and n is the expected insertions. See more detail at: https://en.wikipedia.org/wiki/Bloom_filter#Probability_of_false_positives

Types

type BitSet

type BitSet interface {
	// Set sets those index to true.
	Set(context.Context, []uint64) error
	// Get returns true when all indexes are true.
	Get(context.Context, []uint64) (bool, error)
}

BitSet contains a vector of bits that grows as need. Each component of the bit set contains a bool value.

type BloomFilter

type BloomFilter interface {
	// Add adds an member to set.
	Add(context.Context, []byte) error
	// MayExists test whether the element is in the set.
	// False positive matches are possible
	MayExists(context.Context, []byte) (bool, error)
}

BloomFilter is a space-efficient probabilistic data structure that is used to test whether an element is a member of set.

func New

func New(expectInsertions int64, probabilityOfFalsePositives float64,
	bitset BitSet, options ...Option) (BloomFilter, error)

New creates a BloomFilter.

type DefaultStrategy

type DefaultStrategy struct{}

DefaultStrategy uses double-hashing to generate a sequence of hash value rapidly.

func (*DefaultStrategy) BloomHash

func (d *DefaultStrategy) BloomHash(value []byte, k int, m int64) (bitops []uint64)

BloomHash implements Strategy.BloomHash.

type MemoryBitSet

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

MemoryBitSet implements BitSet interface that stores values in memory.

func (*MemoryBitSet) Get

func (m *MemoryBitSet) Get(ctx context.Context, bitpos []uint64) (bool, error)

Get implements BitSet.Get.

func (*MemoryBitSet) Set

func (m *MemoryBitSet) Set(ctx context.Context, bitpos []uint64) error

Set implements BitSet.Set.

type Option

type Option interface {
	// contains filtered or unexported methods
}

Option for creating a BloomFilter.

func WithStrategy

func WithStrategy(s Strategy) Option

WithStrategy changes the default strategy with custom strategy.

type RedisBitField

type RedisBitField interface {
	BitField(ctx context.Context, key string, args ...interface{}) ([]int64, error)
}

RedisBitField is a client that could executing redis bitfield command.

type RedisBitSet

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

RedisBitSet uses a redis BitField client to implements BitSet interface.

func NewRedisBitSet

func NewRedisBitSet(r RedisBitField, key string) *RedisBitSet

NewRedisBitSet creates a RedisBitSet uses specific redis `key`.

func (*RedisBitSet) Get

func (r *RedisBitSet) Get(ctx context.Context, bitpos []uint64) (bool, error)

Get implements BitSet.Get.

func (*RedisBitSet) Set

func (r *RedisBitSet) Set(ctx context.Context, bitpos []uint64) error

Set implements BitSet.Set.

type Strategy

type Strategy interface {
	BloomHash(value []byte, k int, m int64) []uint64
}

Strategy of

Jump to

Keyboard shortcuts

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