Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add custom hash function #29

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
38 changes: 29 additions & 9 deletions consistent.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,22 +48,39 @@ type Consistent struct {
circle map[uint32]string
members map[string]bool
sortedHashes uints
NumberOfReplicas int
numberOfReplicas int
count int64
scratch [64]byte
UseFnv bool
customHashFunc func(string) uint32
once *sync.Once
sync.RWMutex
}

const _defaultNumofReplicas = 20

// 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 New() *Consistent {
c := new(Consistent)
c.NumberOfReplicas = 20
c.circle = make(map[uint32]string)
c.members = make(map[string]bool)
return c
return &Consistent{
numberOfReplicas: _defaultNumofReplicas,
circle: make(map[uint32]string),
members: make(map[string]bool),
once: &sync.Once{},
}
}

// SetRepNum set the number of replicas
func (c *Consistent) SetRepNum(n int) {
c.once.Do(func() {
c.numberOfReplicas = n
})
}

// NumberOfReplicas return the number of replicas
func (c *Consistent) NumberOfReplicas() int {
return c.numberOfReplicas
}

// eltKey generates a string key for an element with an index.
Expand All @@ -81,7 +98,7 @@ func (c *Consistent) Add(elt string) {

// need c.Lock() before calling
func (c *Consistent) add(elt string) {
for i := 0; i < c.NumberOfReplicas; i++ {
for i := 0; i < c.numberOfReplicas; i++ {
c.circle[c.hashKey(c.eltKey(elt, i))] = elt
}
c.members[elt] = true
Expand All @@ -98,7 +115,7 @@ func (c *Consistent) Remove(elt string) {

// need c.Lock() before calling
func (c *Consistent) remove(elt string) {
for i := 0; i < c.NumberOfReplicas; i++ {
for i := 0; i < c.numberOfReplicas; i++ {
delete(c.circle, c.hashKey(c.eltKey(elt, i)))
}
delete(c.members, elt)
Expand Down Expand Up @@ -238,6 +255,9 @@ func (c *Consistent) GetN(name string, n int) ([]string, error) {
}

func (c *Consistent) hashKey(key string) uint32 {
if c.customHashFunc != nil {
return c.customHashFunc(key)
}
if c.UseFnv {
return c.hashKeyFnv(key)
}
Expand All @@ -262,7 +282,7 @@ func (c *Consistent) hashKeyFnv(key string) uint32 {
func (c *Consistent) updateSortedHashes() {
hashes := c.sortedHashes[:0]
//reallocate if we're holding on to too much (1/4th)
if cap(c.sortedHashes)/(c.NumberOfReplicas*4) > len(c.circle) {
if cap(c.sortedHashes)/(c.numberOfReplicas*4) > len(c.circle) {
hashes = nil
}
for k := range c.circle {
Expand Down
4 changes: 2 additions & 2 deletions consistent_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func TestNew(t *testing.T) {
if x == nil {
t.Errorf("expected obj")
}
checkNum(x.NumberOfReplicas, 20, t)
checkNum(x.NumberOfReplicas(), 20, t)
}

func TestAdd(t *testing.T) {
Expand Down Expand Up @@ -689,7 +689,7 @@ func TestCollisionsCRC(t *testing.T) {
count := 0
for scanner.Scan() {
word := scanner.Text()
for i := 0; i < c.NumberOfReplicas; i++ {
for i := 0; i < c.NumberOfReplicas(); i++ {
ekey := c.eltKey(word, i)
// ekey := word + "|" + strconv.Itoa(i)
k := c.hashKey(ekey)
Expand Down