fix golint issues in core/hash (#487)

master
Kevin Wan 4 years ago committed by GitHub
parent f238290dd3
commit 0c094cb2d7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -11,6 +11,7 @@ import (
) )
const ( const (
// TopWeight is the top weight that one entry might set.
TopWeight = 100 TopWeight = 100
minReplicas = 100 minReplicas = 100
@ -18,10 +19,12 @@ const (
) )
type ( type (
HashFunc func(data []byte) uint64 // Func defines the hash method.
Func func(data []byte) uint64
// A ConsistentHash is a ring hash implementation.
ConsistentHash struct { ConsistentHash struct {
hashFunc HashFunc hashFunc Func
replicas int replicas int
keys []uint64 keys []uint64
ring map[uint64][]interface{} ring map[uint64][]interface{}
@ -30,11 +33,13 @@ type (
} }
) )
// NewConsistentHash returns a ConsistentHash.
func NewConsistentHash() *ConsistentHash { func NewConsistentHash() *ConsistentHash {
return NewCustomConsistentHash(minReplicas, Hash) return NewCustomConsistentHash(minReplicas, Hash)
} }
func NewCustomConsistentHash(replicas int, fn HashFunc) *ConsistentHash { // NewCustomConsistentHash returns a ConsistentHash with given replicas and hash func.
func NewCustomConsistentHash(replicas int, fn Func) *ConsistentHash {
if replicas < minReplicas { if replicas < minReplicas {
replicas = minReplicas replicas = minReplicas
} }
@ -92,6 +97,7 @@ func (h *ConsistentHash) AddWithWeight(node interface{}, weight int) {
h.AddWithReplicas(node, replicas) h.AddWithReplicas(node, replicas)
} }
// Get returns the corresponding node from h base on the given v.
func (h *ConsistentHash) Get(v interface{}) (interface{}, bool) { func (h *ConsistentHash) Get(v interface{}) (interface{}, bool) {
h.lock.RLock() h.lock.RLock()
defer h.lock.RUnlock() defer h.lock.RUnlock()
@ -118,6 +124,7 @@ func (h *ConsistentHash) Get(v interface{}) (interface{}, bool) {
} }
} }
// Remove removes the given node from h.
func (h *ConsistentHash) Remove(node interface{}) { func (h *ConsistentHash) Remove(node interface{}) {
nodeRepr := repr(node) nodeRepr := repr(node)

@ -132,8 +132,8 @@ func TestConsistentHash_RemoveInterface(t *testing.T) {
assert.Equal(t, 1, len(ch.nodes)) assert.Equal(t, 1, len(ch.nodes))
node, ok := ch.Get(1) node, ok := ch.Get(1)
assert.True(t, ok) assert.True(t, ok)
assert.Equal(t, key, node.(*MockNode).Addr) assert.Equal(t, key, node.(*mockNode).addr)
assert.Equal(t, 2, node.(*MockNode).Id) assert.Equal(t, 2, node.(*mockNode).id)
} }
func getKeysBeforeAndAfterFailure(t *testing.T, prefix string, index int) (map[int]string, map[int]string) { func getKeysBeforeAndAfterFailure(t *testing.T, prefix string, index int) (map[int]string, map[int]string) {
@ -164,18 +164,18 @@ func getKeysBeforeAndAfterFailure(t *testing.T, prefix string, index int) (map[i
return keys, newKeys return keys, newKeys
} }
type MockNode struct { type mockNode struct {
Addr string addr string
Id int id int
} }
func newMockNode(addr string, id int) *MockNode { func newMockNode(addr string, id int) *mockNode {
return &MockNode{ return &mockNode{
Addr: addr, addr: addr,
Id: id, id: id,
} }
} }
func (n *MockNode) String() string { func (n *mockNode) String() string {
return n.Addr return n.addr
} }

@ -7,16 +7,19 @@ import (
"github.com/spaolacci/murmur3" "github.com/spaolacci/murmur3"
) )
// Hash returns the hash value of data.
func Hash(data []byte) uint64 { func Hash(data []byte) uint64 {
return murmur3.Sum64(data) return murmur3.Sum64(data)
} }
// Md5 returns the md5 bytes of data.
func Md5(data []byte) []byte { func Md5(data []byte) []byte {
digest := md5.New() digest := md5.New()
digest.Write(data) digest.Write(data)
return digest.Sum(nil) return digest.Sum(nil)
} }
// Md5Hex returns the md5 hex string of data.
func Md5Hex(data []byte) string { func Md5Hex(data []byte) string {
return fmt.Sprintf("%x", Md5(data)) return fmt.Sprintf("%x", Md5(data))
} }

Loading…
Cancel
Save