/
shard.go
157 lines (135 loc) · 3.25 KB
/
shard.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
// Copyright 2012, Google Inc. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package router
import (
"fmt"
"github.com/dataux/dataux/vendored/mixer/hack"
"hash/crc32"
"strconv"
)
type KeyError string
func NewKeyError(format string, args ...interface{}) KeyError {
return KeyError(fmt.Sprintf(format, args...))
}
func (ke KeyError) Error() string {
return string(ke)
}
func handleError(err *error) {
if x := recover(); x != nil {
*err = x.(KeyError)
}
}
func EncodeValue(value interface{}) string {
switch val := value.(type) {
case int:
return Uint64Key(val).String()
case uint64:
return Uint64Key(val).String()
case int64:
return Uint64Key(val).String()
case string:
return val
case []byte:
return hack.String(val)
}
panic(NewKeyError("Unexpected key variable type %T", value))
}
func HashValue(value interface{}) uint64 {
switch val := value.(type) {
case int:
return uint64(val)
case uint64:
return uint64(val)
case int64:
return uint64(val)
case string:
return uint64(crc32.ChecksumIEEE(hack.Slice(val)))
case []byte:
return uint64(crc32.ChecksumIEEE(val))
}
panic(NewKeyError("Unexpected key variable type %T", value))
}
func NumValue(value interface{}) int64 {
switch val := value.(type) {
case int:
return int64(val)
case uint64:
return int64(val)
case int64:
return int64(val)
case string:
if v, err := strconv.ParseInt(val, 10, 64); err != nil {
panic(NewKeyError("invalid num format %s", v))
} else {
return v
}
case []byte:
if v, err := strconv.ParseInt(hack.String(val), 10, 64); err != nil {
panic(NewKeyError("invalid num format %s", v))
} else {
return v
}
}
panic(NewKeyError("Unexpected key variable type %T", value))
}
type Shard interface {
FindForKey(key interface{}) int
}
type RangeShard interface {
Shard
EqualStart(key interface{}, index int) bool
EqualStop(key interface{}, index int) bool
}
type HashShard struct {
ShardNum int
}
func (s *HashShard) FindForKey(key interface{}) int {
h := HashValue(key)
return int(h % uint64(s.ShardNum))
}
type NumRangeShard struct {
Shards []NumKeyRange
}
func (s *NumRangeShard) FindForKey(key interface{}) int {
v := NumValue(key)
for i, r := range s.Shards {
if r.Contains(v) {
return i
}
}
panic(NewKeyError("Unexpected key %v, not in range", key))
}
func (s *NumRangeShard) EqualStart(key interface{}, index int) bool {
v := NumValue(key)
return s.Shards[index].Start == v
}
func (s *NumRangeShard) EqualStop(key interface{}, index int) bool {
v := NumValue(key)
return s.Shards[index].End == v
}
type KeyRangeShard struct {
Shards []KeyRange
}
func (s *KeyRangeShard) FindForKey(key interface{}) int {
v := KeyspaceId(EncodeValue(key))
for i, r := range s.Shards {
if r.Contains(v) {
return i
}
}
panic(NewKeyError("Unexpected key %v, not in range", key))
}
func (s *KeyRangeShard) EqualStart(key interface{}, index int) bool {
v := KeyspaceId(EncodeValue(key))
return s.Shards[index].Start == v
}
func (s *KeyRangeShard) EqualStop(key interface{}, index int) bool {
v := KeyspaceId(EncodeValue(key))
return s.Shards[index].End == v
}
type DefaultShard struct {
}
func (s *DefaultShard) FindForKey(key interface{}) int {
return 0
}