mirror of
https://github.com/HDT3213/godis.git
synced 2025-10-06 17:26:52 +08:00
86 lines
1.7 KiB
Go
86 lines
1.7 KiB
Go
package consistenthash
|
|
|
|
import (
|
|
"hash/crc32"
|
|
"sort"
|
|
"strconv"
|
|
"strings"
|
|
)
|
|
|
|
// HashFunc defines function to generate hash code
|
|
type HashFunc func(data []byte) uint32
|
|
|
|
// Map stores nodes and you can pick node from Map
|
|
type Map struct {
|
|
hashFunc HashFunc
|
|
replicas int
|
|
keys []int // sorted
|
|
hashMap map[int]string
|
|
}
|
|
|
|
// New creates a new Map
|
|
func New(replicas int, fn HashFunc) *Map {
|
|
m := &Map{
|
|
replicas: replicas,
|
|
hashFunc: fn,
|
|
hashMap: make(map[int]string),
|
|
}
|
|
if m.hashFunc == nil {
|
|
m.hashFunc = crc32.ChecksumIEEE
|
|
}
|
|
return m
|
|
}
|
|
|
|
// IsEmpty returns if there is no node in Map
|
|
func (m *Map) IsEmpty() bool {
|
|
return len(m.keys) == 0
|
|
}
|
|
|
|
// AddNode add the given nodes into consistent hash circle
|
|
func (m *Map) AddNode(keys ...string) {
|
|
for _, key := range keys {
|
|
if key == "" {
|
|
continue
|
|
}
|
|
for i := 0; i < m.replicas; i++ {
|
|
hash := int(m.hashFunc([]byte(strconv.Itoa(i) + key)))
|
|
m.keys = append(m.keys, hash)
|
|
m.hashMap[hash] = key
|
|
}
|
|
}
|
|
sort.Ints(m.keys)
|
|
}
|
|
|
|
// support hash tag
|
|
func getPartitionKey(key string) string {
|
|
beg := strings.Index(key, "{")
|
|
if beg == -1 {
|
|
return key
|
|
}
|
|
end := strings.Index(key, "}")
|
|
if end == -1 || end == beg+1 {
|
|
return key
|
|
}
|
|
return key[beg+1 : end]
|
|
}
|
|
|
|
// PickNode gets the closest item in the hash to the provided key.
|
|
func (m *Map) PickNode(key string) string {
|
|
if m.IsEmpty() {
|
|
return ""
|
|
}
|
|
|
|
partitionKey := getPartitionKey(key)
|
|
hash := int(m.hashFunc([]byte(partitionKey)))
|
|
|
|
// Binary search for appropriate replica.
|
|
idx := sort.Search(len(m.keys), func(i int) bool { return m.keys[i] >= hash })
|
|
|
|
// Means we have cycled back to the first replica.
|
|
if idx == len(m.keys) {
|
|
idx = 0
|
|
}
|
|
|
|
return m.hashMap[m.keys[idx]]
|
|
}
|