-
Notifications
You must be signed in to change notification settings - Fork 1
/
bucketizer.go
executable file
·85 lines (73 loc) · 2.32 KB
/
bucketizer.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
package xxhash
import (
"fmt"
"github.com/cespare/xxhash"
"github.com/ehsangolshani/bucketizer"
"github.com/pkg/errors"
)
type XXHASHBucketizer struct {
Buckets []bucketizer.Bucket
weightSum uint64
bucketRanges []uint64
seed []byte
}
func (b XXHASHBucketizer) BucketBytes(value []byte) (int, error) {
a := append(value, b.seed...)
hashDigest := xxhash.Sum64(a)
reminder := hashDigest % b.weightSum
n := len(b.bucketRanges)
for i := 0; i < n-1; i++ {
if reminder >= b.bucketRanges[i] && reminder < b.bucketRanges[i+1] {
return i, nil
}
}
return 0, errors.New("invalid reminder value")
}
func (b XXHASHBucketizer) BucketString(value string) (int, error) {
valueInBytes := []byte(value)
return b.BucketBytes(valueInBytes)
}
func (b XXHASHBucketizer) BucketInt(value int) (int, error) {
valueInBytes := []byte(fmt.Sprintf("%d", value))
return b.BucketBytes(valueInBytes)
}
func (b XXHASHBucketizer) BucketInt8(value int8) (int, error) {
valueInBytes := []byte(fmt.Sprintf("%d", value))
return b.BucketBytes(valueInBytes)
}
func (b XXHASHBucketizer) BucketInt16(value int16) (int, error) {
valueInBytes := []byte(fmt.Sprintf("%d", value))
return b.BucketBytes(valueInBytes)
}
func (b XXHASHBucketizer) BucketInt32(value int32) (int, error) {
valueInBytes := []byte(fmt.Sprintf("%d", value))
return b.BucketBytes(valueInBytes)
}
func (b XXHASHBucketizer) BucketInt64(value int64) (int, error) {
valueInBytes := []byte(fmt.Sprintf("%d", value))
return b.BucketBytes(valueInBytes)
}
func (b XXHASHBucketizer) BucketFloat64(value float64) (int, error) {
valueInBytes := []byte(fmt.Sprintf("%g", value))
return b.BucketBytes(valueInBytes)
}
func (b XXHASHBucketizer) BucketFloat32(value float32) (int, error) {
valueInBytes := []byte(fmt.Sprintf("%g", value))
return b.BucketBytes(valueInBytes)
}
func NewXXHASHBucketizer(seed string, buckets ...bucketizer.Bucket) XXHASHBucketizer {
var sumOfWeights uint64 = 0
var bucketRanges []uint64
bucketRanges = append(bucketRanges, 0)
for i, bucket := range buckets {
sumOfWeights += uint64(bucket.Weight)
startFromIndex := bucketRanges[i]
bucketRanges = append(bucketRanges, startFromIndex+uint64(bucket.Weight))
}
return XXHASHBucketizer{
Buckets: buckets,
weightSum: sumOfWeights,
bucketRanges: bucketRanges,
seed: []byte(seed),
}
}