-
Notifications
You must be signed in to change notification settings - Fork 12
/
hash.go
52 lines (44 loc) · 913 Bytes
/
hash.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package main
import (
"bufio"
"fmt"
"os"
"github.com/benbjohnson/jmphash"
"github.com/cespare/xxhash"
)
var (
replicationFactor = 2
numberOfNodes = 6
hasher *jmphash.Hasher
table []hostRange
)
func main() {
hasher = jmphash.NewHasher(numberOfNodes)
scanner := bufio.NewScanner(os.Stdin)
for scanner.Scan() {
metricName := scanner.Text()
fmt.Printf("%v # %s\n", Lookup(metricName), metricName)
}
}
func Lookup(item string) []int {
hashValue := xxhash.Sum64String(item)
node := hasher.Hash(hashValue)
var result []int
for n := 0; n < replicationFactor; n++ {
result = append(result, (node+n)%numberOfNodes)
}
for _, r := range table {
if hashValue >= r.start && hashValue <= r.end {
result = append(result, r.hostIndex)
}
}
return result
}
type rangeInfo struct {
start uint64
end uint64
}
type hostRange struct {
hostIndex int
rangeInfo
}