consistent

package module
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Mar 2, 2025 License: Apache-2.0 Imports: 7 Imported by: 0

README

weighted-consistent-hashing

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrEmptyCircle = errors.New("empty circle")

ErrEmptyCircle is the error returned when trying to get an element when nothing has been added to hash.

Functions

func WeightedShuffle added in v1.1.0

func WeightedShuffle(cMembers map[string]float64) []string

Types

type Consistent

type Consistent struct {
	NumberOfReplicas int

	UseFnv bool
	sync.RWMutex
	// contains filtered or unexported fields
}

Consistent holds the information about the members of the consistent hash circle.

func New

func New(numberOfReplicas int) *Consistent

New creates a new Consistent object with a default setting of 20 replicas for each entry.

To change the number of replicas, set NumberOfReplicas before adding entries.

func (*Consistent) Add

func (c *Consistent) Add(elt string, wgt float64)

Add inserts a string element in the consistent hash.

func (*Consistent) Get

func (c *Consistent) Get(name string) (string, error)

Get returns an element close to where name hashes to in the circle.

func (*Consistent) GetAll added in v1.1.0

func (c *Consistent) GetAll(name string) ([]string, error)

GetAll returns the N closest distinct elements to the name input in the circle.

func (*Consistent) GetN

func (c *Consistent) GetN(name string, n int) ([]string, error)

GetN returns the N closest distinct elements to the name input in the circle. weight = 0 can get

func (*Consistent) GetTwo

func (c *Consistent) GetTwo(name string) (string, string, error)

GetTwo returns the two closest distinct elements to the name input in the circle.

func (*Consistent) Members

func (c *Consistent) Members() []string

func (*Consistent) Remove

func (c *Consistent) Remove(elt string)

Remove removes an element from the hash.

func (*Consistent) Set

func (c *Consistent) Set(eltMap map[string]float64)

Set sets all the elements in the hash. If there are existing elements not present in elts, they will be removed.

func (*Consistent) UpdateWeight

func (c *Consistent) UpdateWeight(elt string, wgt float64)

UpdateWeight update weight.

type Member

type Member struct {
	Name   string
	Weight float64
}

type WeightedConsistent added in v1.1.0

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

func NewWeightedConsistent added in v1.1.0

func NewWeightedConsistent(name string, numberOfReplicas int, members []Member) *WeightedConsistent

func (*WeightedConsistent) GetAll added in v1.1.0

func (c *WeightedConsistent) GetAll(key string) ([]string, error)

GetAll 一致性hash加权随机

func (*WeightedConsistent) GetRandomAll added in v1.1.0

func (c *WeightedConsistent) GetRandomAll(key string) ([]string, error)

GetRandomAll 加权随机

func (*WeightedConsistent) Len added in v1.1.0

func (c *WeightedConsistent) Len() int

Jump to

Keyboard shortcuts

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