bloomfilter

package module
v0.1.3 Latest Latest
Warning

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

Go to latest
Published: Feb 16, 2023 License: MIT Imports: 11 Imported by: 0

README

bloomfilter

Build codecov Go Reference Go Report Card Mentioned in Awesome Go

Overview

Yet another Bloomfilter implementation in Go, compatible with Java's Guava library. This library borrows how Java's Guava libraray implements Bloomfilter hashing strategies to achieve the serialization compatibility.

Installing

First pull the latest version of the library:

go get github.com/OldPanda/bloomfilter

Then import the this library in your code:

import "github.com/OldPanda/bloomfilter"

Usage Examples

Basic Usage
package main

import (
	"fmt"

	"github.com/OldPanda/bloomfilter"
)

func main() {
	// create bloomfilter with expected insertion=500, error rate=0.01
	bf, _ := bloomfilter.NewBloomFilter(500, 0.01)
	// add number 0~199 into bloomfilter
	for i := 0; i < 200; i++ {
		bf.Put(i)
	}

	// check if number 100 and 200 are in bloomfilter
	fmt.Println(bf.MightContain(100))
	fmt.Println(bf.MightContain(200))
}
Serialization
package main

import "github.com/OldPanda/bloomfilter"

func main() {
	// expected insertion=500, error rate=0.01
	bf, _ := bloomfilter.NewBloomFilter(500, 0.01)
	// add 0~199 into bloomfilter
	for i := 0; i < 200; i++ {
		bf.Put(i)
	}

	// serialize bloomfilter to byte array
	bytes := bf.ToBytes()
	// handling the bytes ...
}
Deserialization
package main

import (
	"fmt"

	"github.com/OldPanda/bloomfilter"
)

func main() {
	// create bloomfilter from byte array
	bf, _ := bloomfilter.FromBytes(bytes)
	// check whether number 100 is in bloomfilter
	fmt.Println(bf.MightContain(100))
}

Benchmark

The benchmark testing runs on element insertion and query separately.

» go test -bench . -benchmem ./...
# github.com/OldPanda/bloomfilter.test
goos: darwin
goarch: amd64
pkg: github.com/OldPanda/bloomfilter
cpu: Intel(R) Core(TM) i7-4870HQ CPU @ 2.50GHz
BenchmarkBloomfilterInsertion-8                  4923142               387.1 ns/op            17 B/op          1 allocs/op
BenchmarkBloomfilterQuery-8                      4678299               259.6 ns/op            15 B/op          1 allocs/op
BenchmarkBloomfilterDeserialization-8             162871              7110 ns/op           13200 B/op         52 allocs/op
PASS
ok      github.com/OldPanda/bloomfilter 4.880s

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetBytes

func GetBytes(arg interface{}) []byte

GetBytes ...

Types

type BloomFilter

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

BloomFilter definition includes the number of hash functions, bit array, and strategy for hashing.

func FromBytes

func FromBytes(b []byte) (*BloomFilter, error)

FromBytes creates a new BloomFilter instance by deserializing from byte array.

func NewBloomFilter

func NewBloomFilter(expectedInsertions int, errRate float64) (*BloomFilter, error)

NewBloomFilter creates a new BloomFilter instance with `Murur128Mitz64` as default strategy.

func NewBloomFilterWithStrategy

func NewBloomFilterWithStrategy(expectedInsertions int, errRate float64, strategy Strategy) (*BloomFilter, error)

NewBloomFilterWithStrategy creates a new BloomFilter instance with given expected insertions/capacity, error rate, and strategy. For now the available strategies are * &Murur128Mitz32{} * &Murur128Mitz64{}

func (*BloomFilter) MightContain

func (bf *BloomFilter) MightContain(key interface{}) bool

MightContain returns a boolean value to indicate if given element is in BloomFilter.

func (*BloomFilter) Put

func (bf *BloomFilter) Put(key interface{}) bool

Put inserts element of any type into BloomFilter.

func (*BloomFilter) ToBytes

func (bf *BloomFilter) ToBytes() []byte

ToBytes serializes BloomFilter to byte array, which is compatible with Java's Guava library.

type Murur128Mitz32

type Murur128Mitz32 struct{}

Murur128Mitz32 is the implementation of Guava's MURMUR128_MITZ_32 class in Go. See https://github.com/google/guava/blob/master/guava/src/com/google/common/hash/BloomFilterStrategies.java#L45 for details.

type Murur128Mitz64

type Murur128Mitz64 struct{}

Murur128Mitz64 is the implementation of Guava's MURMUR128_MITZ_64 class in Go. See https://github.com/google/guava/blob/master/guava/src/com/google/common/hash/BloomFilterStrategies.java#L93 for details.

type Strategy

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

Strategy defines necessary functions for a strategy.

Jump to

Keyboard shortcuts

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