bloom

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Mar 27, 2026 License: Apache-2.0 Imports: 8 Imported by: 0

Documentation

Overview

Package bloom provides a counting Bloom filter for probabilistic membership testing with element removal support.

NOT safe for concurrent use. Callers must synchronize access externally.

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	ErrEmptyDump    = errors.New("empty dump")
	ErrBitsetTooBig = errors.New("bitset overflow: parameters require more than 2^32 bits")
)

Bloom filter errors.

Functions

This section is empty.

Types

type CountingFilter

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

CountingFilter is a probabilistic data structure that supports both membership testing and element removal.

func NewCounting

func NewCounting(n int, p float64) (*CountingFilter, error)

NewCounting creates an optimized counting bloom filter. It returns ErrBitsetTooBig when n and p require more bits than a 32-bit filter supports.

Example
package main

import (
	"fmt"

	"github.com/FrogoAI/memory/bloom"
)

func main() {
	bf, err := bloom.NewCounting(1000, 0.01)
	if err != nil {
		panic(err)
	}

	bf.Add([]byte("hello"))
	fmt.Println(bf.Test([]byte("hello")))
}
Output:
true

func NewCountingFromBytes

func NewCountingFromBytes(data []byte) (*CountingFilter, error)

NewCountingFromBytes deserializes a byte slice into a CountingFilter.

func (*CountingFilter) Add

func (f *CountingFilter) Add(data []byte)

Add inserts data into the filter by incrementing its counters. It protects against counter overflow (stopping at 255).

Example
package main

import (
	"fmt"

	"github.com/FrogoAI/memory/bloom"
)

func main() {
	bf, _ := bloom.NewCounting(1000, 0.01)

	bf.Add([]byte("apple"))
	bf.Add([]byte("banana"))
	bf.Add([]byte("cherry"))

	fmt.Println(bf.Test([]byte("apple")))
	fmt.Println(bf.Test([]byte("banana")))
	fmt.Println(bf.Test([]byte("cherry")))
}
Output:
true
true
true

func (*CountingFilter) Clear

func (f *CountingFilter) Clear()

Clear resets all counters in the filter.

Example
package main

import (
	"fmt"

	"github.com/FrogoAI/memory/bloom"
)

func main() {
	bf, _ := bloom.NewCounting(1000, 0.01)

	bf.Add([]byte("apple"))
	bf.Add([]byte("banana"))

	fmt.Println(bf.Test([]byte("apple")))

	bf.Clear()
	fmt.Println(bf.Test([]byte("apple")))
}
Output:
true
false

func (*CountingFilter) Copy

func (f *CountingFilter) Copy() *CountingFilter

Copy returns a deep copy of the CountingFilter.

func (*CountingFilter) MarshalBinary

func (f *CountingFilter) MarshalBinary() ([]byte, error)

MarshalBinary implements the encoding.BinaryMarshaler interface.

func (*CountingFilter) Remove

func (f *CountingFilter) Remove(data []byte)

Remove deletes data from the filter by decrementing its counters. It protects against counter underflow (stopping at 0).

Example
package main

import (
	"fmt"

	"github.com/FrogoAI/memory/bloom"
)

func main() {
	bf, _ := bloom.NewCounting(1000, 0.01)

	bf.Add([]byte("apple"))
	fmt.Println(bf.Test([]byte("apple")))

	bf.Remove([]byte("apple"))
	fmt.Println(bf.Test([]byte("apple")))
}
Output:
true
false

func (*CountingFilter) Test

func (f *CountingFilter) Test(data []byte) bool

Test checks if an item is likely in the set. It returns true if the item might be in the filter, false if it is definitely not.

Example
package main

import (
	"fmt"

	"github.com/FrogoAI/memory/bloom"
)

func main() {
	bf, _ := bloom.NewCounting(1000, 0.01)

	bf.Add([]byte("apple"))

	fmt.Println(bf.Test([]byte("apple")))
	fmt.Println(bf.Test([]byte("banana")))
}
Output:
true
false

func (*CountingFilter) ToBytes

func (f *CountingFilter) ToBytes() ([]byte, error)

ToBytes serializes the CountingFilter to a byte slice.

func (*CountingFilter) UnmarshalBinary

func (f *CountingFilter) UnmarshalBinary(data []byte) error

UnmarshalBinary implements the encoding.BinaryUnmarshaler interface.

Jump to

Keyboard shortcuts

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