-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathkey.go
More file actions
74 lines (57 loc) · 1.29 KB
/
key.go
File metadata and controls
74 lines (57 loc) · 1.29 KB
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
package ssp
import (
"hash/fnv"
"github.com/affo/ssp/values"
)
type KeySelector interface {
GetKey(v values.Value) values.Key
}
type fixedKeySelector struct{}
func NewFixedKeySelector() KeySelector {
return fixedKeySelector{}
}
func (s fixedKeySelector) GetKey(v values.Value) values.Key {
return 0
}
func (s fixedKeySelector) String() string {
return "fixedKeySelector"
}
type FnKeySelector func(v values.Value) values.Key
func (s FnKeySelector) GetKey(v values.Value) values.Key {
return s(v)
}
type roundRobinKeySelector struct {
n int
i int
}
func NewRoundRobinKeySelector(n int) KeySelector {
return &roundRobinKeySelector{
n: n,
}
}
func (s *roundRobinKeySelector) GetKey(v values.Value) values.Key {
k := values.Key(s.i)
s.i++
if s.i == s.n {
s.i = 0
}
return k
}
func (s *roundRobinKeySelector) String() string {
return "roundRobinKeySelector"
}
type stringValueKeySelector struct {
f func(v values.Value) string
}
func NewStringValueKeySelector(f func(v values.Value) string) KeySelector {
return &stringValueKeySelector{f: f}
}
func (s *stringValueKeySelector) GetKey(v values.Value) values.Key {
st := s.f(v)
h := fnv.New64a()
_, _ = h.Write([]byte(st))
return values.Key(h.Sum64())
}
func (s *stringValueKeySelector) String() string {
return "stringValueKeySelector"
}