Skip to content

Commit

Permalink
Add option to use FNV32a hash (UseFnv)
Browse files Browse the repository at this point in the history
  • Loading branch information
patrickxb committed Sep 20, 2019
1 parent ad91dc4 commit 30ea17e
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 0 deletions.
15 changes: 15 additions & 0 deletions consistent.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ package consistent // import "stathat.com/c/consistent"
import (
"errors"
"hash/crc32"
"hash/fnv"
"sort"
"strconv"
"sync"
Expand Down Expand Up @@ -50,6 +51,7 @@ type Consistent struct {
NumberOfReplicas int
count int64
scratch [64]byte
UseFnv bool
sync.RWMutex
}

Expand Down Expand Up @@ -236,6 +238,13 @@ func (c *Consistent) GetN(name string, n int) ([]string, error) {
}

func (c *Consistent) hashKey(key string) uint32 {
if c.UseFnv {
return c.hashKeyFnv(key)
}
return c.hashKeyCRC32(key)
}

func (c *Consistent) hashKeyCRC32(key string) uint32 {
if len(key) < 64 {
var scratch [64]byte
copy(scratch[:], key)
Expand All @@ -244,6 +253,12 @@ func (c *Consistent) hashKey(key string) uint32 {
return crc32.ChecksumIEEE([]byte(key))
}

func (c *Consistent) hashKeyFnv(key string) uint32 {
h := fnv.New32a()
h.Write([]byte(key))
return h.Sum32()
}

func (c *Consistent) updateSortedHashes() {
hashes := c.sortedHashes[:0]
//reallocate if we're holding on to too much (1/4th)
Expand Down
50 changes: 50 additions & 0 deletions consistent_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package consistent

import (
"bufio"
"encoding/base64"
"math/rand"
"os"
"runtime"
Expand Down Expand Up @@ -740,3 +741,52 @@ func TestConcurrentGetSet(t *testing.T) {
}
wg.Wait()
}

func TestDistributionFnv(t *testing.T) {
x := New()
x.UseFnv = true
x.Add("abcdefg")
x.Add("hijklmn")
x.Add("opqrstu")
dist := make(map[string]int)
g := make([]byte, 12)
for i := 0; i < 10000; i++ {
_, err := rand.Read(g)
if err != nil {
t.Fatal(err)
}
s := base64.StdEncoding.EncodeToString(g)
r, err := x.Get(s)
if err != nil {
t.Fatal(err)
}
dist[r] = dist[r] + 1
}
for k, v := range dist {
t.Logf("%s: %d", k, v)
}
}

func TestDistributionCRC(t *testing.T) {
x := New()
x.Add("abcdefg")
x.Add("hijklmn")
x.Add("opqrstu")
dist := make(map[string]int)
g := make([]byte, 12)
for i := 0; i < 10000; i++ {
_, err := rand.Read(g)
if err != nil {
t.Fatal(err)
}
s := base64.StdEncoding.EncodeToString(g)
r, err := x.Get(s)
if err != nil {
t.Fatal(err)
}
dist[r] = dist[r] + 1
}
for k, v := range dist {
t.Logf("%s: %d", k, v)
}
}

0 comments on commit 30ea17e

Please sign in to comment.