consistent

package
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Jun 19, 2022 License: Apache-2.0 Imports: 7 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// DefaultVnodes is the default number of virtual nodes per node
	DefaultVnodes = 211
)

Variables

View Source
var (
	// ErrEmpty indicates that a search operation was attempted over an empty hash
	ErrEmpty = errors.New("no hash nodes defined")
)

Functions

This section is empty.

Types

type Config

type Config struct {
	// Algorithm is the name of the algorithm to use.  If this field
	// is unset, medley.DefaultAlgorithm() is used.
	Algorithm string `json:"algorithm"`

	// Vnodes is the number of virtual nodes for each node.  If this field
	// is unset or nonpositive, DefaultVnodes is used.
	Vnodes int `json:"vnodes"`

	// Extensions is an optional set of algorithms beyond this package's builtins.
	// The Algorithm field can refer to a key within this map.
	Extensions map[string]medley.Algorithm `json:"-"`
}

Config represents the available configuration options for a consistent hash.

type Hash

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

Hash represents a consistent hash. This type is backward compatible with https://github.com/billhathaway/consistentHash.

A Hash is safe for concurrent use.

func New

func New(cfg Config) (h *Hash, err error)

New constructs a consistent Hash from configuration

func (*Hash) Add

func (h *Hash) Add(nodes []medley.Node) (added int)

Add inserts nodes and their corresponding vnodes into this hash. Any nodes already present are left intact.

This method reorders the nodes slice in-place using NodeSet.Filter.

func (*Hash) Algorithm

func (h *Hash) Algorithm() medley.Algorithm

Algorithm returns the medley algorithm associated with this hash

func (*Hash) Get

func (h *Hash) Get(k medley.Key) (n medley.Node, err error)

Get obtains the closest Node associated with a Key. The hash ring is walked clockwise to find the nearest node.

func (*Hash) Len

func (h *Hash) Len() (l int)

Len returns the count of nodes associated with this hash. This is not the size of the internal storage of the hash. Len()*Vnodes() would return the total size of the internal hash ring.

func (*Hash) Rehash

func (h *Hash) Rehash(nodes []medley.Node) (added, removed int)

Rehash restructures this hash so that the given nodes are the only ones in the ring. Any nodes currently part of this hash that are not in the set of nodes passed to this method are removed. Nodes passed to this method that are not in this hash are added.

Unlike Add and Remove, this method does not reorder the nodes slice.

The separate counts of nodes added and removed are returned.

func (*Hash) Remove

func (h *Hash) Remove(nodes []medley.Node) (removed int)

Remove deletes nodes and their vnodes from this hash.

This method reorders the nodes slice in-place using NodeSet.Filter.

func (*Hash) Vnodes

func (h *Hash) Vnodes() int

Vnodes returns the number of virtual nodes added for each node

Jump to

Keyboard shortcuts

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