2019-01-29 22:58:30 +00:00
|
|
|
// Package hrw implements Rendezvous hashing.
|
|
|
|
// http://en.wikipedia.org/wiki/Rendezvous_hashing.
|
|
|
|
package hrw
|
|
|
|
|
|
|
|
import (
|
2019-01-31 09:54:02 +00:00
|
|
|
"encoding/binary"
|
2019-07-05 06:49:24 +00:00
|
|
|
"errors"
|
2019-08-01 09:16:03 +00:00
|
|
|
"math"
|
2019-01-29 22:58:30 +00:00
|
|
|
"reflect"
|
|
|
|
"sort"
|
2019-01-31 09:54:02 +00:00
|
|
|
|
2023-06-01 16:41:22 +00:00
|
|
|
"github.com/twmb/murmur3"
|
2019-01-29 22:58:30 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type (
|
2019-01-31 09:54:02 +00:00
|
|
|
// Hasher interface used by SortSliceByValue
|
2019-01-29 22:58:30 +00:00
|
|
|
Hasher interface{ Hash() uint64 }
|
|
|
|
|
2020-09-11 10:34:16 +00:00
|
|
|
sorter struct {
|
|
|
|
l int
|
|
|
|
less func(i, j int) bool
|
|
|
|
swap func(i, j int)
|
2019-05-27 07:45:29 +00:00
|
|
|
}
|
2019-01-29 22:58:30 +00:00
|
|
|
)
|
|
|
|
|
2019-07-05 06:49:24 +00:00
|
|
|
// Boundaries of valid normalized weights
|
|
|
|
const (
|
|
|
|
NormalizedMaxWeight = 1.0
|
|
|
|
NormalizedMinWeight = 0.0
|
|
|
|
)
|
|
|
|
|
2020-09-11 10:34:16 +00:00
|
|
|
func (s *sorter) Len() int { return s.l }
|
|
|
|
func (s *sorter) Less(i, j int) bool { return s.less(i, j) }
|
|
|
|
func (s *sorter) Swap(i, j int) { s.swap(i, j) }
|
|
|
|
|
2019-07-05 06:49:24 +00:00
|
|
|
func distance(x uint64, y uint64) uint64 {
|
2019-01-29 22:58:30 +00:00
|
|
|
acc := x ^ y
|
2019-01-30 09:32:34 +00:00
|
|
|
// here used mmh3 64 bit finalizer
|
|
|
|
// https://github.com/aappleby/smhasher/blob/61a0530f28277f2e850bfc39600ce61d02b518de/src/MurmurHash3.cpp#L81
|
2019-01-29 22:58:30 +00:00
|
|
|
acc ^= acc >> 33
|
2019-01-30 09:32:34 +00:00
|
|
|
acc = acc * 0xff51afd7ed558ccd
|
2019-01-29 22:58:30 +00:00
|
|
|
acc ^= acc >> 33
|
2019-01-30 09:32:34 +00:00
|
|
|
acc = acc * 0xc4ceb9fe1a85ec53
|
2019-01-29 22:58:30 +00:00
|
|
|
acc ^= acc >> 33
|
|
|
|
return acc
|
|
|
|
}
|
|
|
|
|
2019-01-31 09:54:02 +00:00
|
|
|
// Hash uses murmur3 hash to return uint64
|
|
|
|
func Hash(key []byte) uint64 {
|
|
|
|
return murmur3.Sum64(key)
|
|
|
|
}
|
|
|
|
|
2019-07-05 06:49:24 +00:00
|
|
|
// Sort receive nodes and hash, and sort it by distance
|
2019-05-27 07:45:29 +00:00
|
|
|
func Sort(nodes []uint64, hash uint64) []uint64 {
|
2020-09-11 10:34:16 +00:00
|
|
|
l := len(nodes)
|
|
|
|
sorted := make([]uint64, l)
|
|
|
|
dist := make([]uint64, l)
|
2019-07-05 06:49:24 +00:00
|
|
|
for i := range nodes {
|
2020-09-11 10:34:16 +00:00
|
|
|
sorted[i] = uint64(i)
|
|
|
|
dist[i] = distance(nodes[i], hash)
|
2019-01-29 22:58:30 +00:00
|
|
|
}
|
|
|
|
|
2020-09-11 10:34:16 +00:00
|
|
|
sort.Slice(sorted, func(i, j int) bool {
|
|
|
|
return dist[sorted[i]] < dist[sorted[j]]
|
|
|
|
})
|
|
|
|
return sorted
|
2019-01-29 22:58:30 +00:00
|
|
|
}
|
|
|
|
|
2019-07-05 06:49:24 +00:00
|
|
|
// SortByWeight receive nodes, weights and hash, and sort it by distance * weight
|
|
|
|
func SortByWeight(nodes []uint64, weights []float64, hash uint64) []uint64 {
|
2020-09-11 10:34:16 +00:00
|
|
|
result := make([]uint64, len(nodes))
|
|
|
|
copy(nodes, result)
|
|
|
|
sortByWeight(len(nodes), false, nodes, weights, hash, reflect.Swapper(result))
|
|
|
|
return result
|
2019-05-27 07:45:29 +00:00
|
|
|
}
|
|
|
|
|
2019-07-05 06:49:24 +00:00
|
|
|
// SortSliceByValue received []T and hash to sort by value-distance
|
2019-01-31 09:54:02 +00:00
|
|
|
func SortSliceByValue(slice interface{}, hash uint64) {
|
2019-05-27 07:45:29 +00:00
|
|
|
rule := prepareRule(slice)
|
|
|
|
if rule != nil {
|
|
|
|
swap := reflect.Swapper(slice)
|
2020-09-11 10:34:16 +00:00
|
|
|
sortByDistance(len(rule), false, rule, hash, swap)
|
2019-05-27 07:45:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-27 07:52:20 +00:00
|
|
|
// SortHasherSliceByValue receives []Hasher and hash to sort by value-distance.
|
|
|
|
func SortHasherSliceByValue[T Hasher](slice []T, hash uint64) {
|
2023-06-01 17:33:15 +00:00
|
|
|
if len(slice) == 0 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
dist := make([]uint64, len(slice))
|
|
|
|
for i := range dist {
|
|
|
|
dist[i] = distance(slice[i].Hash(), hash)
|
2023-02-27 07:52:20 +00:00
|
|
|
}
|
2023-06-01 17:33:15 +00:00
|
|
|
sortHasherByDistance(slice, false, dist)
|
2023-02-27 07:52:20 +00:00
|
|
|
}
|
|
|
|
|
2019-07-05 06:49:24 +00:00
|
|
|
// SortSliceByWeightValue received []T, weights and hash to sort by value-distance * weights
|
|
|
|
func SortSliceByWeightValue(slice interface{}, weights []float64, hash uint64) {
|
2019-05-27 07:45:29 +00:00
|
|
|
rule := prepareRule(slice)
|
|
|
|
if rule != nil {
|
|
|
|
swap := reflect.Swapper(slice)
|
2020-09-11 10:34:16 +00:00
|
|
|
sortByWeight(reflect.ValueOf(slice).Len(), false, rule, weights, hash, swap)
|
2019-05-27 07:45:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-27 07:52:20 +00:00
|
|
|
// SortHasherSliceByWeightValue receives []Hasher, weights and hash to sort by value-distance * weights.
|
|
|
|
func SortHasherSliceByWeightValue[T Hasher](slice []T, weights []float64, hash uint64) {
|
2023-06-01 17:33:15 +00:00
|
|
|
if len(slice) == 0 {
|
|
|
|
return
|
2023-06-01 17:22:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if allSameF64(weights) {
|
2023-06-01 17:33:15 +00:00
|
|
|
dist := make([]uint64, len(slice))
|
|
|
|
for i := range dist {
|
|
|
|
dist[i] = distance(slice[i].Hash(), hash)
|
|
|
|
}
|
|
|
|
sortHasherByDistance(slice, false, dist)
|
2023-06-01 17:22:47 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
dist := make([]float64, len(slice))
|
|
|
|
for i := range dist {
|
2023-06-01 17:33:15 +00:00
|
|
|
d := distance(slice[i].Hash(), hash)
|
2023-06-01 17:22:47 +00:00
|
|
|
// `maxUint64 - distance` makes the shorter distance more valuable
|
|
|
|
// it is necessary for operation with normalized values
|
|
|
|
dist[i] = float64(^uint64(0)-d) * weights[i]
|
|
|
|
}
|
|
|
|
|
2023-06-01 17:33:15 +00:00
|
|
|
sort.Sort(&sorter{
|
2023-06-01 17:22:47 +00:00
|
|
|
l: len(slice),
|
|
|
|
swap: func(i, j int) {
|
2023-02-27 07:52:20 +00:00
|
|
|
slice[i], slice[j] = slice[j], slice[i]
|
2023-06-01 17:22:47 +00:00
|
|
|
dist[i], dist[j] = dist[j], dist[i]
|
|
|
|
},
|
|
|
|
less: func(i, j int) bool {
|
|
|
|
return dist[i] > dist[j] // higher distance must be placed lower to be first
|
|
|
|
},
|
2023-06-01 17:33:15 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// sortHasherByDistance is similar to sortByDistance but accepts slice directly.
|
|
|
|
func sortHasherByDistance[T Hasher](slice []T, byIndex bool, dist []uint64) {
|
|
|
|
sort.Sort(&sorter{
|
|
|
|
l: len(slice),
|
|
|
|
swap: func(i, j int) {
|
|
|
|
slice[i], slice[j] = slice[j], slice[i]
|
|
|
|
dist[i], dist[j] = dist[j], dist[i]
|
|
|
|
},
|
|
|
|
less: func(i, j int) bool {
|
|
|
|
return dist[i] < dist[j]
|
|
|
|
},
|
|
|
|
})
|
2023-02-27 07:52:20 +00:00
|
|
|
}
|
|
|
|
|
2019-07-05 06:49:24 +00:00
|
|
|
// SortSliceByIndex received []T and hash to sort by index-distance
|
2019-05-27 07:45:29 +00:00
|
|
|
func SortSliceByIndex(slice interface{}, hash uint64) {
|
2020-09-11 10:34:16 +00:00
|
|
|
length := reflect.ValueOf(slice).Len()
|
2019-05-27 07:45:29 +00:00
|
|
|
swap := reflect.Swapper(slice)
|
2020-09-11 10:34:16 +00:00
|
|
|
sortByDistance(length, true, nil, hash, swap)
|
2019-05-27 07:45:29 +00:00
|
|
|
}
|
|
|
|
|
2019-07-05 06:49:24 +00:00
|
|
|
// SortSliceByWeightIndex received []T, weights and hash to sort by index-distance * weights
|
|
|
|
func SortSliceByWeightIndex(slice interface{}, weights []float64, hash uint64) {
|
2020-09-11 10:34:16 +00:00
|
|
|
length := reflect.ValueOf(slice).Len()
|
2019-05-27 07:45:29 +00:00
|
|
|
swap := reflect.Swapper(slice)
|
2020-09-11 10:34:16 +00:00
|
|
|
sortByWeight(length, true, nil, weights, hash, swap)
|
2019-05-27 07:45:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func prepareRule(slice interface{}) []uint64 {
|
2019-01-29 22:58:30 +00:00
|
|
|
t := reflect.TypeOf(slice)
|
|
|
|
if t.Kind() != reflect.Slice {
|
2021-12-28 11:27:29 +00:00
|
|
|
panic("HRW sort expects slice, got " + t.Kind().String())
|
2019-01-29 22:58:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
val = reflect.ValueOf(slice)
|
|
|
|
length = val.Len()
|
|
|
|
rule = make([]uint64, 0, length)
|
|
|
|
)
|
|
|
|
|
|
|
|
if length == 0 {
|
2019-05-27 07:45:29 +00:00
|
|
|
return nil
|
2019-01-29 22:58:30 +00:00
|
|
|
}
|
|
|
|
|
2019-04-12 11:19:18 +00:00
|
|
|
switch slice := slice.(type) {
|
|
|
|
case []int:
|
2019-01-31 09:54:02 +00:00
|
|
|
var key = make([]byte, 16)
|
2019-01-29 22:58:30 +00:00
|
|
|
for i := 0; i < length; i++ {
|
2019-01-31 09:54:02 +00:00
|
|
|
binary.BigEndian.PutUint64(key, uint64(slice[i]))
|
2019-05-24 09:51:39 +00:00
|
|
|
rule = append(rule, Hash(key))
|
2019-01-29 22:58:30 +00:00
|
|
|
}
|
2019-04-12 11:19:18 +00:00
|
|
|
case []uint:
|
2019-04-12 11:09:49 +00:00
|
|
|
var key = make([]byte, 16)
|
|
|
|
for i := 0; i < length; i++ {
|
|
|
|
binary.BigEndian.PutUint64(key, uint64(slice[i]))
|
2019-05-24 09:51:39 +00:00
|
|
|
rule = append(rule, Hash(key))
|
2019-04-12 11:09:49 +00:00
|
|
|
}
|
2019-04-12 11:19:18 +00:00
|
|
|
case []int8:
|
2019-04-12 11:09:49 +00:00
|
|
|
for i := 0; i < length; i++ {
|
|
|
|
key := byte(slice[i])
|
2019-05-24 09:51:39 +00:00
|
|
|
rule = append(rule, Hash([]byte{key}))
|
2019-04-12 11:09:49 +00:00
|
|
|
}
|
2019-04-12 11:19:18 +00:00
|
|
|
case []uint8:
|
2019-04-12 11:09:49 +00:00
|
|
|
for i := 0; i < length; i++ {
|
|
|
|
key := slice[i]
|
2019-05-24 09:51:39 +00:00
|
|
|
rule = append(rule, Hash([]byte{key}))
|
2019-04-12 11:09:49 +00:00
|
|
|
}
|
2019-04-12 11:19:18 +00:00
|
|
|
case []int16:
|
2019-04-12 11:09:49 +00:00
|
|
|
var key = make([]byte, 8)
|
|
|
|
for i := 0; i < length; i++ {
|
|
|
|
binary.BigEndian.PutUint16(key, uint16(slice[i]))
|
2019-05-24 09:51:39 +00:00
|
|
|
rule = append(rule, Hash(key))
|
2019-04-12 11:09:49 +00:00
|
|
|
}
|
2019-04-12 11:19:18 +00:00
|
|
|
case []uint16:
|
2019-04-12 11:09:49 +00:00
|
|
|
var key = make([]byte, 8)
|
|
|
|
for i := 0; i < length; i++ {
|
|
|
|
binary.BigEndian.PutUint16(key, slice[i])
|
2019-05-24 09:51:39 +00:00
|
|
|
rule = append(rule, Hash(key))
|
2019-04-12 11:09:49 +00:00
|
|
|
}
|
2019-04-12 11:19:18 +00:00
|
|
|
case []int32:
|
2019-02-01 09:57:05 +00:00
|
|
|
var key = make([]byte, 16)
|
|
|
|
for i := 0; i < length; i++ {
|
|
|
|
binary.BigEndian.PutUint32(key, uint32(slice[i]))
|
2019-05-24 09:51:39 +00:00
|
|
|
rule = append(rule, Hash(key))
|
2019-02-01 09:57:05 +00:00
|
|
|
}
|
2019-04-12 11:19:18 +00:00
|
|
|
case []uint32:
|
2019-04-12 11:09:49 +00:00
|
|
|
var key = make([]byte, 16)
|
|
|
|
for i := 0; i < length; i++ {
|
|
|
|
binary.BigEndian.PutUint32(key, slice[i])
|
2019-05-24 09:51:39 +00:00
|
|
|
rule = append(rule, Hash(key))
|
2019-04-12 11:09:49 +00:00
|
|
|
}
|
2019-04-12 11:19:18 +00:00
|
|
|
case []int64:
|
2019-04-12 11:09:49 +00:00
|
|
|
var key = make([]byte, 32)
|
|
|
|
for i := 0; i < length; i++ {
|
|
|
|
binary.BigEndian.PutUint64(key, uint64(slice[i]))
|
2019-05-24 09:51:39 +00:00
|
|
|
rule = append(rule, Hash(key))
|
2019-04-12 11:09:49 +00:00
|
|
|
}
|
2019-04-12 11:19:18 +00:00
|
|
|
case []uint64:
|
2019-04-12 11:09:49 +00:00
|
|
|
var key = make([]byte, 32)
|
|
|
|
for i := 0; i < length; i++ {
|
|
|
|
binary.BigEndian.PutUint64(key, slice[i])
|
2019-05-24 09:51:39 +00:00
|
|
|
rule = append(rule, Hash(key))
|
2019-04-12 11:09:49 +00:00
|
|
|
}
|
2019-04-12 11:19:18 +00:00
|
|
|
case []string:
|
2019-01-29 22:58:30 +00:00
|
|
|
for i := 0; i < length; i++ {
|
2019-05-24 09:51:39 +00:00
|
|
|
rule = append(rule, Hash([]byte(slice[i])))
|
2019-01-29 22:58:30 +00:00
|
|
|
}
|
2019-04-12 11:19:18 +00:00
|
|
|
|
|
|
|
default:
|
|
|
|
if _, ok := val.Index(0).Interface().(Hasher); !ok {
|
2021-12-28 11:27:29 +00:00
|
|
|
panic("slice elements must implement hrw.Hasher")
|
2019-04-12 11:19:18 +00:00
|
|
|
}
|
|
|
|
|
2019-01-29 22:58:30 +00:00
|
|
|
for i := 0; i < length; i++ {
|
|
|
|
h := val.Index(i).Interface().(Hasher)
|
2019-05-24 09:51:39 +00:00
|
|
|
rule = append(rule, h.Hash())
|
2019-01-29 22:58:30 +00:00
|
|
|
}
|
|
|
|
}
|
2019-05-27 07:45:29 +00:00
|
|
|
return rule
|
2019-01-31 09:54:02 +00:00
|
|
|
}
|
2019-07-05 06:49:24 +00:00
|
|
|
|
|
|
|
// ValidateWeights checks if weights are normalized between 0.0 and 1.0
|
|
|
|
func ValidateWeights(weights []float64) error {
|
|
|
|
for i := range weights {
|
2019-08-01 09:16:03 +00:00
|
|
|
if math.IsNaN(weights[i]) || weights[i] > NormalizedMaxWeight || weights[i] < NormalizedMinWeight {
|
2019-07-05 06:49:24 +00:00
|
|
|
return errors.New("weights are not normalized")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2020-09-11 10:34:16 +00:00
|
|
|
|
|
|
|
// sortByWeight sorts nodes by weight using provided swapper.
|
|
|
|
// nodes contains hrw hashes. If it is nil, indices are used.
|
|
|
|
func sortByWeight(l int, byIndex bool, nodes []uint64, weights []float64, hash uint64, swap func(i, j int)) {
|
|
|
|
// if all nodes have the same distance then sort uniformly
|
|
|
|
if allSameF64(weights) {
|
|
|
|
sortByDistance(l, byIndex, nodes, hash, swap)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-06-01 16:34:37 +00:00
|
|
|
dist := make([]float64, l)
|
|
|
|
for i := 0; i < l; i++ {
|
|
|
|
d := getDistance(byIndex, i, nodes, hash)
|
2020-09-11 10:34:16 +00:00
|
|
|
// `maxUint64 - distance` makes the shorter distance more valuable
|
|
|
|
// it is necessary for operation with normalized values
|
2023-06-01 16:34:37 +00:00
|
|
|
dist[i] = float64(^uint64(0)-d) * weights[i]
|
|
|
|
}
|
|
|
|
|
|
|
|
s := &sorter{
|
|
|
|
l: l,
|
|
|
|
swap: func(i, j int) {
|
|
|
|
swap(i, j)
|
|
|
|
dist[i], dist[j] = dist[j], dist[i]
|
|
|
|
},
|
|
|
|
less: func(i, j int) bool {
|
|
|
|
return dist[i] > dist[j] // higher distance must be placed lower to be first
|
|
|
|
},
|
2020-09-11 10:34:16 +00:00
|
|
|
}
|
|
|
|
sort.Sort(s)
|
|
|
|
}
|
|
|
|
|
|
|
|
// sortByDistance sorts nodes by hrw distance using provided swapper.
|
|
|
|
// nodes contains hrw hashes. If it is nil, indices are used.
|
|
|
|
func sortByDistance(l int, byIndex bool, nodes []uint64, hash uint64, swap func(i, j int)) {
|
2023-06-01 16:34:37 +00:00
|
|
|
dist := make([]uint64, l)
|
|
|
|
for i := 0; i < l; i++ {
|
|
|
|
dist[i] = getDistance(byIndex, i, nodes, hash)
|
|
|
|
}
|
|
|
|
|
|
|
|
s := &sorter{
|
|
|
|
l: l,
|
|
|
|
swap: func(i, j int) {
|
|
|
|
swap(i, j)
|
|
|
|
dist[i], dist[j] = dist[j], dist[i]
|
|
|
|
},
|
|
|
|
less: func(i, j int) bool {
|
|
|
|
return dist[i] < dist[j]
|
|
|
|
},
|
2020-09-11 10:34:16 +00:00
|
|
|
}
|
|
|
|
sort.Sort(s)
|
|
|
|
}
|
|
|
|
|
|
|
|
// getDistance return distance from nodes[i] to h.
|
|
|
|
// If byIndex is true, nodes index is used.
|
|
|
|
// Else if nodes[i] != nil, distance is calculated from this value.
|
|
|
|
// Otherwise, and hash from node index is taken.
|
|
|
|
func getDistance(byIndex bool, i int, nodes []uint64, h uint64) uint64 {
|
|
|
|
if nodes != nil {
|
|
|
|
return distance(nodes[i], h)
|
|
|
|
} else if byIndex {
|
|
|
|
return distance(uint64(i), h)
|
|
|
|
} else {
|
|
|
|
buf := make([]byte, 8)
|
|
|
|
binary.LittleEndian.PutUint64(buf, uint64(i))
|
|
|
|
return distance(Hash(buf), h)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func allSameF64(fs []float64) bool {
|
|
|
|
for i := range fs {
|
|
|
|
if fs[i] != fs[0] {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|