GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Why Go
  • Case Studies
  • Use Cases
  • Security
  • Learn
  • Effective Go
  • Go User Manual
  • Standard library
  • Release Notes
  • API
  • Packages
  • Recorded Talks
  • Meetups
  • Conferences

This view

  • Original page
  • New address

GeezerVison is showing pkg.go.dev — ring package - github.com/tannerryan/ring - Go Packages

Details

  • checked Valid go.mod file

    The Go module system was introduced in Go 1.11 and is the official dependency management solution for Go.

  • checked Redistributable license

    Redistributable licenses place minimal restrictions on how software can be used, modified, and redistributed.

  • checked Tagged version

    Modules with tagged versions give importers more predictable builds.

  • checked Stable version

    When a project reaches major version v1 it is considered stable.

  • Learn more about best practices

Repository

github.com/tannerryan/ring

Links

  • Open Source Insights Logo Open Source Insights

README ¶

ring

Go Reference License

A small, thread-safe Bloom filter for Go. A Bloom filter can rule out membership with certainty. Positive results are probabilistic.

Go 1.27 or newer is required.

Installation

go get github.com/tannerryan/ring@latest

Usage

package main

import (
	"fmt"

	"github.com/tannerryan/ring"
)

func main() {
	filter, err := ring.Init(100_000, 0.001)
	if err != nil {
		panic(err)
	}

	filter.Add([]byte("hello"))
	fmt.Println(filter.Test([]byte("hello"))) // true
	fmt.Println(filter.Test([]byte("world"))) // probably false
}

Init takes the expected number of elements and the desired false-positive rate. Adding more than the expected number increases the rate. After initialization, all methods are safe for concurrent use. Use InitByParameters when the exact bit count and number of hash rounds are already known. Both values must be positive, and the hash rounds cannot exceed the bit count. Entries cannot be removed individually. Reset clears the entire filter.

Persistence

encoded, err := filter.MarshalBinary()
if err != nil {
	panic(err)
}

var restored ring.Ring
err = restored.UnmarshalBinary(encoded)
if err != nil {
	panic(err)
}

fmt.Println(restored.Test([]byte("hello"))) // true

MarshalBinary and UnmarshalBinary save and restore a filter. Merge combines filters with the same sizing and binary version. Version 1 filters remain readable and retain their original hash behavior. New encodings use version 2 and cannot be read by older releases.

The implementation uses MurmurHash3, a fast non-cryptographic hash. Treat the filter as a probabilistic data structure, not as a security boundary.

Development

Install the development tools with make deps, then run make check.

License

This project is available under the BSD 2-Clause License.

Documentation ¶

Overview ¶

Package ring provides a thread-safe Bloom filter with binary persistence.

Index ¶

  • type Ring
    • func Init(elements int, falsePositive float64) (*Ring, error)
    • func InitByParameters(size, hashes uint64) (*Ring, error)
    • func (r *Ring) Add(data []byte)
    • func (r *Ring) MarshalBinary() ([]byte, error)
    • func (r *Ring) Merge(m *Ring) error
    • func (r *Ring) Reset()
    • func (r *Ring) Test(data []byte) bool
    • func (r *Ring) UnmarshalBinary(data []byte) error

Constants ¶

This section is empty.

Variables ¶

This section is empty.

Functions ¶

This section is empty.

Types ¶

type Ring ¶

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

Ring is a thread-safe Bloom filter. A Ring must be created with Init or decoded with UnmarshalBinary before use. Ring values should not be copied.

func Init ¶

func Init(elements int, falsePositive float64) (*Ring, error)

Init returns a filter sized for a positive element count and a false-positive rate between 0 and 1.

func InitByParameters ¶ added in v1.2.0

func InitByParameters(size, hashes uint64) (*Ring, error)

InitByParameters returns a filter with the given bit count and hash rounds. Both values must be positive, and hashes cannot exceed size.

func (*Ring) Add ¶

func (r *Ring) Add(data []byte)

Add records data in the filter.

func (*Ring) MarshalBinary ¶ added in v1.1.0

func (r *Ring) MarshalBinary() ([]byte, error)

MarshalBinary encodes the filter. New filters use format version 2.

func (*Ring) Merge ¶ added in v1.1.0

func (r *Ring) Merge(m *Ring) error

Merge adds another filter with the same parameters and binary version to r.

func (*Ring) Reset ¶

func (r *Ring) Reset()

Reset clears the ring.

func (*Ring) Test ¶

func (r *Ring) Test(data []byte) bool

Test reports whether data may be in the filter. A false result rules out membership.

func (*Ring) UnmarshalBinary ¶ added in v1.1.0

func (r *Ring) UnmarshalBinary(data []byte) error

UnmarshalBinary replaces the filter with a valid version 1 or 2 encoding.

Source Files ¶

View all Source files
  • doc.go
  • hash.go
  • ring.go

Directories ¶

Path Synopsis
example
Click to show internal directories.
Click to hide internal directories.
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.