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

Replace Sort With Slices #28

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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 20 additions & 3 deletions hashring.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package hashring
import (
"crypto/md5"
"fmt"
"sort"
"slices"
"strconv"
)

Expand All @@ -17,6 +17,7 @@ var defaultHashFunc = func() HashFunc {

type HashKey interface {
Less(other HashKey) bool
Compare(other HashKey) int
}
type HashKeyOrder []HashKey

Expand All @@ -25,6 +26,9 @@ func (h HashKeyOrder) Swap(i, j int) { h[i], h[j] = h[j], h[i] }
func (h HashKeyOrder) Less(i, j int) bool {
return h[i].Less(h[j])
}
func (h HashKeyOrder) Compare(i, j HashKey) int {
return i.Compare(j)
}

type HashFunc func([]byte) HashKey

Expand All @@ -42,6 +46,19 @@ func (k Uint32HashKey) Less(other HashKey) bool {
return k < other.(Uint32HashKey)
}

func (k Uint32HashKey) Compare(other HashKey) int {
if k.Less(other) {
return -1
}

o := other.(Uint32HashKey)
if k == o {
return 0
}

return 1
}

func New(nodes []string) *HashRing {
return NewWithHash(nodes, defaultHashFunc)
}
Expand Down Expand Up @@ -133,7 +150,7 @@ func (h *HashRing) generateCircle() {
}
}

sort.Sort(HashKeyOrder(h.sortedKeys))
slices.SortFunc(HashKeyOrder(h.sortedKeys), HashKeyOrder(h.sortedKeys).Compare)
}

func (h *HashRing) GetNode(stringKey string) (node string, ok bool) {
Expand All @@ -152,7 +169,7 @@ func (h *HashRing) GetNodePos(stringKey string) (pos int, ok bool) {
key := h.GenKey(stringKey)

nodes := h.sortedKeys
pos = sort.Search(len(nodes), func(i int) bool { return key.Less(nodes[i]) })
pos, _ = slices.BinarySearchFunc(HashKeyOrder(h.sortedKeys), key, HashKeyOrder(h.sortedKeys).Compare)

if pos == len(nodes) {
// Wrap the search, should return First node
Expand Down
13 changes: 13 additions & 0 deletions key.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,19 @@ func (k *Int64PairHashKey) Less(other HashKey) bool {
return k.High == o.High && k.Low < o.Low
}

func (k *Int64PairHashKey) Compare(other HashKey) int {
o := other.(*Int64PairHashKey)
if k.Less(o) {
return -1
}

if k.High == o.High && k.Low == o.Low {
return 0
}

return 1
}

func NewInt64PairHashKey(bytes []byte) (HashKey, error) {
const expected = 16
if len(bytes) != expected {
Expand Down