hash

package
v0.0.0-...-53883ab Latest Latest
Warning

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

Go to latest
Published: Sep 17, 2020 License: MIT Imports: 6 Imported by: 16

Documentation

Overview

Package ringhash implementats a consistent ring hash: https://en.wikipedia.org/wiki/Consistent_hashing

Index

Constants

View Source
const (

	// Init is what 32 bits hash values should be initialized with.
	Init = offset32
)

Variables

This section is empty.

Functions

func New

func New(b []byte) uint32

New returns the hash of bytes.

func WithSalt

func WithSalt(text []byte, salt uint32) uint32

Of returns the hash of bytes. it uses salt to shuffle the slice before calculating hash

Types

type Hash

type Hash func(data []byte) uint32

Hash is a signature of a hash function used by the package.

type Ring

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

Ring is the definition of the ringhash.

func NewRing

func NewRing(replicas int, fn Hash) *Ring

New initializes an empty ringhash with the given number of replicas and a hash function. If the hash function is nil, fnv.New32a() is used.

func (*Ring) Add

func (ring *Ring) Add(keys ...string)

Add adds keys to the ring.

func (*Ring) Get

func (ring *Ring) Get(key string) string

Get returns the closest item in the ring to the provided key.

func (*Ring) Len

func (ring *Ring) Len() int

Len returns the number of keys in the ring.

func (*Ring) Signature

func (ring *Ring) Signature() string

Signature returns the ring's hash signature. Two identical ringhashes will have the same signature. Two hashes with different number of keys or replicas or hash functions will have different signatures.

Jump to

Keyboard shortcuts

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