bloom

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: May 21, 2024 License: MIT Imports: 5 Imported by: 0

README

bloom

bloom is a Go library that provides a Bloom Filter implementation using a Trie (prefix tree) under the hood for efficient probabilistic membership testing.

Features

  • Probabilistic Membership Testing: Efficiently checks if an element is possibly in a set.
  • Customizable Hash Functions: Supports the use of different hash functions for better distribution and collision handling (fnv.New64() and fnv.New64a() by default).
  • Thread-Safe: Safe for concurrent use with internal locking mechanisms.

Installation

To install gotrie, use the following command:

go get github.com/binaek/gocoll

Advantages

Efficient

Uses a Trie structure for storing hashed values, ensuring efficient membership testing.

Customizable

Supports various hash functions for better collision handling and distribution.

Thread-Safe

Designed to be safe for concurrent use, making it suitable for multi-threaded applications.

Example:

package main

import (
    "crypto/sha256"
    "crypto/sha512"
    "fmt"
    "hash"

    "github.com/binaek/gocoll/bloom"
)

func main() {
    // Create a new Bloom Filter with custom hashers
    // By default bloom uses fnv.New64() and fnv.New64a()
    hashers := []hash.Hash{
        sha256.New(),
        sha512.New(),
    }
    bf := bloom.NewBloomFilter(bloom.WithHashers(hashers))

    // Add tokens to the Bloom Filter
    bf.Add("apple")
    bf.Add("banana")

    // Test for membership
    fmt.Println("Contains 'apple':", bf.Test("apple"))   // Should be true
    fmt.Println("Contains 'banana':", bf.Test("banana")) // Should be true
    fmt.Println("Contains 'grape':", bf.Test("grape"))   // Should be false
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type BloomConfig

type BloomConfig func(*BloomFilter)

func WithHashers

func WithHashers(hashers []hash.Hash) BloomConfig

type BloomFilter

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

BloomFilter represents a Bloom Filter data structure.

func NewBloomFilter

func NewBloomFilter(config ...BloomConfig) *BloomFilter

NewBloomFilter creates and returns a new BloomFilter instance.

func (*BloomFilter) Add

func (bf *BloomFilter) Add(token string)

Add inserts a token into the Bloom Filter.

func (*BloomFilter) Test

func (bf *BloomFilter) Test(token string) bool

Test checks if a token is possibly in the Bloom Filter.

Jump to

Keyboard shortcuts

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