-
Notifications
You must be signed in to change notification settings - Fork 3
/
sandflake.go
265 lines (210 loc) · 5.25 KB
/
sandflake.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
/*
Copyright 2017 Salim Alami
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package sandflake
import (
"bytes"
"encoding"
"encoding/base32"
"encoding/json"
"fmt"
"time"
)
const (
alphabet = "0123456789ABCDEFGHJKMNPQRSTVWXYZ"
padding = 6
encodedLen = 26
timeUnit = int64(time.Millisecond)
timestampLength = 6
workerIDLength = 4
sequenceLength = 3
randomLength = 3
workerIDOffset = timestampLength
sequenceOffset = timestampLength + workerIDLength
randomOffset = sequenceOffset + sequenceLength
Size = timestampLength + workerIDLength + sequenceLength + randomLength
)
var (
Nil ID
MaxID = ID{255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255}
maxSequence = MaxID.Sequence()
globalGenerator Generator
)
var (
// errors
ErrInvalidLength = fmt.Errorf("expected length of id to be %d", encodedLen)
ErrInvalidBytesLength = fmt.Errorf("expected length of bytes to be %d", Size)
// encoding
encoder = base32.NewEncoding(alphabet)
paddingBytes = bytes.Repeat([]byte{'='}, padding)
)
// 48bit: timestamp
// 32bit: worker id (defaults to MAC Address)
// 24bit: sequence number
// 24bit: random number
type ID [Size]byte
// NewID returns a ID with the provided arguments.
func NewID(t time.Time, workerID WorkerID, seq uint32, randomBytes []byte) ID {
var d ID
ts := uint64(t.UnixNano() / timeUnit)
d.setTimestamp(ts)
copy(d[timestampLength:timestampLength+workerIDLength], workerID[:])
d.setSequence(seq)
copy(d[randomOffset:], randomBytes)
return d
}
func Parse(str string) (ID, error) {
var id ID
err := id.UnmarshalText([]byte(str))
if err != nil {
return id, err
}
return id, nil
}
func ParseBytes(b []byte) (ID, error) {
if len(b) != Size {
return Nil, ErrInvalidBytesLength
}
var id ID
copy(id[:], b)
return id, nil
}
func MustParse(str string) ID {
id, err := Parse(str)
if err != nil {
panic(err)
}
return id
}
func MustParseBytes(b []byte) ID {
id, err := ParseBytes(b)
if err != nil {
panic(err)
}
return id
}
// Next returns the next id from the global generator
// It is safe for concurrent use.
func Next() ID {
return globalGenerator.Next()
}
func (id ID) String() string {
b, _ := id.MarshalText()
return string(b)
}
func (d ID) MarshalText() ([]byte, error) {
dst := make([]byte, encodedLen+padding)
encoder.Encode(dst, d[:])
return dst[:encodedLen], nil
}
func (d *ID) UnmarshalText(b []byte) error {
if len(b) != encodedLen {
return ErrInvalidLength
}
dst := make([]byte, encodedLen)
if _, err := encoder.Decode(dst, append(b, paddingBytes...)); err != nil {
return err
}
copy((*d)[:], dst)
return nil
}
// Time returns the underlying time
func (d ID) Time() time.Time {
var ts uint64
for i := 0; i < timestampLength; i++ {
ts |= uint64(uint64(d[i]) << uint64((timestampLength-i-1)*8))
}
return time.Unix(0, int64(ts)*timeUnit).UTC()
}
func (d *ID) setTimestamp(ts uint64) {
for i := 0; i < timestampLength; i++ {
(*d)[i] = byte(ts >> uint64((timestampLength-i-1)*8))
}
}
func (d *ID) setSequence(ts uint32) {
n := sequenceOffset + sequenceLength
for i := sequenceOffset; i < n; i++ {
(*d)[i] = byte(ts >> uint32((n-i-1)*8))
}
}
// WorkerID returns the underlying worker id
func (d ID) WorkerID() (wid WorkerID) {
copy(wid[:], d[workerIDOffset:workerIDOffset+workerIDLength])
return wid
}
// Sequence returns the underlying sequence number
func (d ID) Sequence() uint32 {
var ts uint32
n := sequenceOffset + sequenceLength
for i := sequenceOffset; i < n; i++ {
ts |= uint32(uint32(d[i]) << uint32((n-i-1)*8))
}
return ts
}
// RandomBytes returns the random stored in the ID
func (d ID) RandomBytes() []byte {
return d[randomOffset:]
}
func (d ID) Before(other ID) bool {
return Compare(d, other) < 0
}
func (d ID) After(other ID) bool {
return Compare(d, other) > 0
}
func Compare(id1, id2 ID) int {
return bytes.Compare(id1[:], id2[:])
}
func (d ID) Size() int {
return Size
}
func (d ID) Equal(other ID) bool {
return Compare(d, other) == 0
}
func (d ID) Marshal() ([]byte, error) {
return d[:], nil
}
func (d ID) MarshalTo(dst []byte) (int, error) {
copy(dst, d[:])
return Size, nil
}
func (d *ID) Unmarshal(b []byte) error {
id, err := ParseBytes(b)
if err != nil {
return err
}
*d = id
return nil
}
func (d ID) MarshalJSON() ([]byte, error) {
return []byte(`"` + d.String() + `"`), nil
}
func (d *ID) UnmarshalJSON(b []byte) error {
if len(b) > 1 && b[0] == '"' && b[len(b)-1] == '"' {
b = bytes.Trim(b, `"`)
id, err := Parse(string(b))
if err != nil {
return err
}
*d = id
return nil
}
return fmt.Errorf("invalid json input: '%v'", b)
}
func (d ID) Bytes() []byte {
return d[:]
}
var (
_ encoding.TextMarshaler = ID{}
_ encoding.TextUnmarshaler = (*ID)(nil)
_ json.Marshaler = ID{}
_ json.Unmarshaler = (*ID)(nil)
)