fastcheck

package
v2.0.57 Latest Latest
Warning

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

Go to latest
Published: Nov 23, 2026 License: Apache-2.0, Apache-2.0 Imports: 12 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

This package contains implementation for checking if an item has been seen previously. This is different from a bloom-filter as it prioritizes false-negatives (return absence even if the object was seen previously), instead of false-positives. A Bloom filter prioritizes false-positives (return presence even if the object was not seen previously) and thus is able to provide a high compression. This is similar to a cache, with the difference being that it does not store a value and can use other optimized representations to compress the keys and optimize for storage. The simplest implementation for the fastcheck.Filter is an LRU cache the package provides alternative implementations and is inspired by the code in https://github.com/jmhodges/opposite_of_a_bloom_filter/ and the related blog https://www.somethingsimilar.com/2012/05/21/the-opposite-of-a-bloom-filter/

The fastcheck.OppoBloomFilter provides separate Contains, Add, and Remove methods. This makes it possible to check and then optionally add the value. It is possible that two threads may race and add it multiple times.

Index

Constants

This section is empty.

Variables

View Source
var ErrSizeTooLarge = errors.New("oppobloom: size given too large to round to a power of 2")
View Source
var ErrSizeTooSmall = errors.New("oppobloom: filter cannot have a zero or negative size")
View Source
var MaxFilterSize = 1 << 30

Functions

This section is empty.

Types

type Filter

type Filter interface {
	// Contains returns a True if the id was previously seen or false otherwise
	// It may return a false, even if a item may have previously occurred.
	Contains(ctx context.Context, id []byte) bool

	// Adds the element id to the Filter and returns if a previous object was evicted in the process
	Add(ctx context.Context, id []byte) (evicted bool)

	// Remove removes id from the Filter and returns whether it was present.
	Remove(ctx context.Context, id []byte) (removed bool)
}

type LRUCacheFilter

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

Implements the fastcheck.Filter interface using an underlying LRUCache from cache.Cache the underlying lru cache implementation is thread-safe

func NewLRUCacheFilter

func NewLRUCacheFilter(size int, scope string, provider metric.MeterProvider) (*LRUCacheFilter, error)

NewLRUCacheFilter creates a fixed-capacity LRU filter with OTEL metrics. Scope identifies the filter in metrics. A nil provider disables metrics.

func (LRUCacheFilter) Add

func (l LRUCacheFilter) Add(_ context.Context, id []byte) bool

func (LRUCacheFilter) Contains

func (l LRUCacheFilter) Contains(ctx context.Context, id []byte) bool

Simply uses Contains from the LRUCacheFilter

func (LRUCacheFilter) Remove added in v2.0.56

func (l LRUCacheFilter) Remove(_ context.Context, id []byte) bool

type Metrics

type Metrics struct {
	Hit  metric.Int64Counter
	Miss metric.Int64Counter
	// Size reports entries currently stored, not capacity.
	Size metric.Int64ObservableGauge
	// contains filtered or unexported fields
}

Metrics contains the OTEL instruments shared by filter implementations.

type OppoBloomFilter

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

Implementation of the oppoFilter proposed in https://github.com/jmhodges/opposite_of_a_bloom_filter/ and the related blog https://www.somethingsimilar.com/2012/05/21/the-opposite-of-a-bloom-filter/

func NewOppoBloomFilter

func NewOppoBloomFilter(size int, scope string, provider metric.MeterProvider) (*OppoBloomFilter, error)

NewOppoBloomFilter creates a new Opposite of Bloom filter proposed in https://github.com/jmhodges/opposite_of_a_bloom_filter/ and the related blog https://www.somethingsimilar.com/2012/05/21/the-opposite-of-a-bloom-filter/. Scope identifies the filter in OTEL metrics. A nil provider disables metrics.

func (*OppoBloomFilter) Add

func (f *OppoBloomFilter) Add(_ context.Context, id []byte) bool

func (*OppoBloomFilter) Contains

func (f *OppoBloomFilter) Contains(ctx context.Context, id []byte) bool

func (*OppoBloomFilter) Remove added in v2.0.56

func (f *OppoBloomFilter) Remove(_ context.Context, id []byte) bool

Jump to

Keyboard shortcuts

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