Categorygithub.com/lithammer/go-jump-consistent-hash
modulepackage
1.0.2
Repository: https://github.com/lithammer/go-jump-consistent-hash.git
Documentation: pkg.go.dev

# README

Jump Consistent Hash

Build Status Godoc

Go implementation of the jump consistent hash algorithm[1] by John Lamping and Eric Veach.

[1] http://arxiv.org/pdf/1406.2294v1.pdf

Usage

import jump "github.com/lithammer/go-jump-consistent-hash"

func main() {
    h := jump.Hash(256, 1024)  // h = 520
}

Includes a helper function for using a string as key instead of an uint64. This requires a hasher that computes the string into a format accepted by Hash(). Such a hasher that uses CRC-64 (ECMA) is also included for convenience.

h := jump.HashString("127.0.0.1", 8, jump.NewCRC64())  // h = 7

In reality though you probably want to use a Hasher so you won't have to repeat the bucket size and which key hasher used. It also uses more convenient types, like int instead of int32.

hasher := jump.New(8, jump.NewCRC64())
h := hasher.Hash("127.0.0.1")  // h = 7

If you want to use your own algorithm, you must implement the KeyHasher interface, which is a subset of the hash.Hash64 interface available in the standard library.

Here's an example of a custom KeyHasher that uses Google's FarmHash algorithm (the successor of CityHash) to compute the final key.

type FarmHash struct {
    buf bytes.Buffer
}

func (f *FarmHash) Write(p []byte) (n int, err error) {
    return f.buf.Write(p)
}

func (f *FarmHash) Reset() {
    f.buf.Reset()
}

func (f *FarmHash) Sum64() uint64 {
    // https://github.com/dgryski/go-farm
    return farm.Hash64(f.buf.Bytes())
}

hasher := jump.New(8, &FarmHash{})
h := hasher.Hash("127.0.0.1")  // h = 5

License

MIT

# Functions

Hash takes a 64 bit key and the number of buckets.
HashString takes string as key instead of an int and uses a KeyHasher to generate a key compatible with Hash().
New returns a new instance of of Hasher.

# Variables

These are deprecated because they're not safe for concurrent use.
KeyHashers available in the standard library for use with HashString() and Hasher.
KeyHashers available in the standard library for use with HashString() and Hasher.
KeyHashers available in the standard library for use with HashString() and Hasher.
CRC32 uses the 32-bit Cyclic Redundancy Check (CRC-32) with the IEEE polynomial.
CRC64 uses the 64-bit Cyclic Redundancy Check (CRC-64) with the ECMA polynomial.
FNV1 uses the non-cryptographic hash function FNV-1.
FNV1a uses the non-cryptographic hash function FNV-1a.

# Structs

Hasher represents a jump consistent hasher using a string as key.

# Interfaces

KeyHasher is a subset of hash.Hash64 in the standard library.