consistent

package module
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: May 7, 2019 License: MIT Imports: 4 Imported by: 1

README

consistent

Consistent hash package for Go.

Performance is better than stathat.com/c/consistent, but no concurrence guarantee.

NumOfReplicas of each shard is adjustable.

Installation

go get github.com/pkumza/consistent@latest

Docs

https://godoc.org/github.com/pkumza/consistent

Example

	c := New()
	c.Add("Bucket1", 20)
	c.Add("Bucket2", 40)
	c.SortHashes()
	bucket, _ := c.Get("apple")
	fmt.Println(bucket) // Bucket2
	bucket, _ = c.Get("banana")
	fmt.Println(bucket) // Bucket1
	bucket, _ = c.Get("pear")
	fmt.Println(bucket) // Bucket2

Documentation

Overview

Package consistent borrows ideas from "stathat.com/c/consistent" simplify it for performance, no concurrence guarantee any more!

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrEmptyCircle is the error returned when trying to get an element when nothing has been added to hash.
	ErrEmptyCircle = errors.New("empty circle")
	// ErrNotSorted is the error returned when trying to get an element when the consistent circle is not sorted.
	ErrNotSorted = errors.New("not sorted")
)

Functions

This section is empty.

Types

type Consistent

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

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

func New

func New() *Consistent

New creates a new Consistent object.

func (*Consistent) Add

func (c *Consistent) Add(elt string, numOfReplicas int)

Add inserts a string element in the consistent hash numOfReplicas times.

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) GetTwo

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

func (*Consistent) SortHashes

func (c *Consistent) SortHashes()

SortHashes before get

Jump to

Keyboard shortcuts

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