-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsnowflake.go
372 lines (305 loc) · 10.5 KB
/
snowflake.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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
package snowflake
import (
"encoding/base64"
"encoding/binary"
"errors"
"fmt"
"strconv"
"sync"
"time"
)
const (
epoch = int64(1577808000000) // Set start time (timestamp / millisecond): 2020-01-01 00, valid for 69 years
timestampBits = uint(41) // Number of digits occupied by timestamp
datacenterIDBits = uint(5) // Number of places occupied by id in the data center
workerIDBits = uint(5) // Number of bits occupied by machine id
sequenceBits = uint(12) // The number of digits occupied by the sequence
timestampMax = int64(-1 ^ (-1 << timestampBits)) // Timestamp maximum
datacenterIDMax = int64(-1 ^ (-1 << datacenterIDBits)) // Maximum number of data center id supported.
workerIDMax = int64(-1 ^ (-1 << workerIDBits)) // Maximum number of machine id supported
sequenceMask = int64(-1 ^ (-1 << sequenceBits)) // Maximum number of sequence id supported
workerIDShift = sequenceBits // Number of left shifts of machine id
datacenterIDShift = sequenceBits + workerIDBits // Data Center id left shift
timestampShift = sequenceBits + workerIDBits + datacenterIDBits // Timestamp left shift
)
// An ID is a custom type used for a snowflake ID. This is used so we can
// attach methods onto the ID.
type ID int64
const encodeBase32Map = "ybndrfg8ejkmcpqxot1uwisza345h769"
var decodeBase32Map [256]byte
const encodeBase58Map = "123456789abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ"
var decodeBase58Map [256]byte
// A JSONSyntaxError is returned from UnmarshalJSON if an invalid ID is provided.
type JSONSyntaxError struct{ original []byte }
func (j JSONSyntaxError) Error() string {
return fmt.Sprintf("invalid snowflake ID %q", string(j.original))
}
// ErrInvalidBase58 is returned by ParseBase58 when given an invalid []byte
var ErrInvalidBase58 = errors.New("invalid base58")
// ErrInvalidBase32 is returned by ParseBase32 when given an invalid []byte
var ErrInvalidBase32 = errors.New("invalid base32")
// Create maps for decoding Base58/Base32.
// This speeds up the process tremendously.
func init() {
for i := 0; i < len(encodeBase58Map); i++ {
decodeBase58Map[i] = 0xFF
}
for i := 0; i < len(encodeBase58Map); i++ {
decodeBase58Map[encodeBase58Map[i]] = byte(i)
}
for i := 0; i < len(encodeBase32Map); i++ {
decodeBase32Map[i] = 0xFF
}
for i := 0; i < len(encodeBase32Map); i++ {
decodeBase32Map[encodeBase32Map[i]] = byte(i)
}
}
// Snowflake is a custom type
type Snowflake struct {
sync.Mutex
timestamp int64
workerID int64
datacenterID int64
sequence int64
}
// NewSnowflake returns a new snowflake node that can be used to generate snowflake
func NewSnowflake(datacenterID, workerID int64) (*Snowflake, error) {
if datacenterID < 0 || datacenterID > datacenterIDMax {
return nil, fmt.Errorf("datacenterid must be between 0 and %d", datacenterIDMax-1)
}
if workerID < 0 || workerID > workerIDMax {
return nil, fmt.Errorf("workerid must be between 0 and %d", workerIDMax-1)
}
return &Snowflake{
timestamp: 0,
datacenterID: datacenterID,
workerID: workerID,
sequence: 0,
}, nil
}
// NextVal creates and returns a unique snowflake ID
// To help guarantee uniqueness
// - Make sure your system is keeping accurate system time
// - Make sure you never have multiple nodes running with the same node ID
func (s *Snowflake) NextVal() ID {
s.Lock()
now := time.Now().UnixNano() / 1000000 // 转毫秒
if s.timestamp == now {
// When id is generated multiple times under the same timestamp (precision: millisecond), the sequence number will be increased.
s.sequence = (s.sequence + 1) & sequenceMask
if s.sequence == 0 {
// If the current sequence exceeds the 12bit length, you need to wait for the next millisecond
// Sequence:0 will be used in the next millisecond
for now <= s.timestamp {
now = time.Now().UnixNano() / 1000000
}
}
} else {
// Use the serial number directly under different timestamps (precision: milliseconds): 0
s.sequence = 0
}
t := now - epoch
if t > timestampMax {
s.Unlock()
fmt.Printf("epoch must be between 0 and %d", timestampMax-1)
return 0
}
s.timestamp = now
r := ID((t)<<timestampShift | (s.datacenterID << datacenterIDShift) | (s.workerID << workerIDShift) | (s.sequence))
s.Unlock()
return r
}
// GetDeviceID returns an int64 of the snowflake center ID and machine ID number
func GetDeviceID(sid int64) (datacenterID, workerID int64) {
datacenterID = (sid >> datacenterIDShift) & datacenterIDMax
workerID = (sid >> workerIDShift) & workerIDMax
return
}
// GetTimestamp returns an int64 unix timestamp in milliseconds of the snowflake ID time
func GetTimestamp(sid ID) (timestamp int64) {
timestamp = (int64(sid) >> timestampShift) & timestampMax
return
}
// GetGenTimestamp returns Get the timestamp when the ID was created
func GetGenTimestamp(sid ID) (timestamp int64) {
timestamp = GetTimestamp(sid) + epoch
return
}
// GetGenTime returns Gets the time string when the ID was created (precision: seconds)
func GetGenTime(sid ID) (t string) {
// The timestamp / 1000 obtained by GetGenTimestamp needs to be converted into seconds
t = time.Unix(GetGenTimestamp(sid)/1000, 0).Format("2006-01-02 15:04:05")
return
}
// GetTimestampStatus returns an float64 unix timestamp in milliseconds of the snowflake ID time Get the percentage of timestamps used: range (0.0-1.0)
func GetTimestampStatus() (state float64) {
state = float64(time.Now().UnixNano()/1000000-epoch) / float64(timestampMax)
return
}
// Int64 returns an int64 of the snowflake ID
func (sid ID) Int64() int64 {
return int64(sid)
}
// ParseInt64 converts an int64 into a snowflake ID
func ParseInt64(id int64) ID {
return ID(id)
}
// String returns a string of the snowflake ID
func (sid ID) String() string {
return strconv.FormatInt(int64(sid), 10)
}
// ParseString converts a string into a snowflake ID
func ParseString(sid string) (ID, error) {
i, err := strconv.ParseInt(sid, 10, 64)
return ID(i), err
}
// Base2 returns a string base2 of the snowflake ID
func (sid ID) Base2() string {
return strconv.FormatInt(int64(sid), 2)
}
// ParseBase2 converts a Base2 string into a snowflake ID
func ParseBase2(id string) (ID, error) {
i, err := strconv.ParseInt(id, 2, 64)
return ID(i), err
}
// Base32 uses the z-base-32 character set but encodes and decodes similar
// to base58, allowing it to create an even smaller result string.
// NOTE: There are many different base32 implementations so becareful when
// doing any interoperation.
func (sid ID) Base32() string {
if sid < 32 {
return string(encodeBase32Map[sid])
}
b := make([]byte, 0, 12)
for sid >= 32 {
b = append(b, encodeBase32Map[sid%32])
sid /= 32
}
b = append(b, encodeBase32Map[sid])
for x, y := 0, len(b)-1; x < y; x, y = x+1, y-1 {
b[x], b[y] = b[y], b[x]
}
return string(b)
}
// ParseBase32 parses a base32 []byte into a snowflake ID
// NOTE: There are many different base32 implementations so becareful when
// doing any interoperation.
func ParseBase32(b []byte) (ID, error) {
var id int64
for i := range b {
if decodeBase32Map[b[i]] == 0xFF {
return -1, ErrInvalidBase32
}
id = id*32 + int64(decodeBase32Map[b[i]])
}
return ID(id), nil
}
// Base36 returns a base36 string of the snowflake ID
func (sid ID) Base36() string {
return strconv.FormatInt(int64(sid), 36)
}
// ParseBase36 converts a Base36 string into a snowflake ID
func ParseBase36(id string) (ID, error) {
i, err := strconv.ParseInt(id, 36, 64)
return ID(i), err
}
// Base58 returns a base58 string of the snowflake ID
func (sid ID) Base58() string {
if sid < 58 {
return string(encodeBase58Map[sid])
}
b := make([]byte, 0, 11)
for sid >= 58 {
b = append(b, encodeBase58Map[sid%58])
sid /= 58
}
b = append(b, encodeBase58Map[sid])
for x, y := 0, len(b)-1; x < y; x, y = x+1, y-1 {
b[x], b[y] = b[y], b[x]
}
return string(b)
}
// ParseBase58 parses a base58 []byte into a snowflake ID
func ParseBase58(b []byte) (ID, error) {
var id int64
for i := range b {
if decodeBase58Map[b[i]] == 0xFF {
return -1, ErrInvalidBase58
}
id = id*58 + int64(decodeBase58Map[b[i]])
}
return ID(id), nil
}
// Base64 returns a base64 string of the snowflake ID
func (sid ID) Base64() string {
return base64.StdEncoding.EncodeToString(sid.Bytes())
}
// ParseBase64 converts a base64 string into a snowflake ID
func ParseBase64(id string) (ID, error) {
b, err := base64.StdEncoding.DecodeString(id)
if err != nil {
return -1, err
}
return ParseBytes(b)
}
// Bytes return a byte slice of the snowflake ID
func (sid ID) Bytes() []byte {
return []byte(sid.String())
}
// ParseBytes converts a byte slice into a snowflake ID
func ParseBytes(id []byte) (ID, error) {
i, err := strconv.ParseInt(string(id), 10, 64)
return ID(i), err
}
// IntBytes returns an array of bytes of the snowflake ID, encoded as a
// big endian integer.
func (sid ID) IntBytes() [8]byte {
var b [8]byte
binary.BigEndian.PutUint64(b[:], uint64(sid))
return b
}
// ParseIntBytes converts an array of bytes encoded as big endian integer as
// a snowflake ID
func ParseIntBytes(id [8]byte) ID {
return ID(int64(binary.BigEndian.Uint64(id[:])))
}
// MarshalJSON returns a json byte array string of the snowflake ID.
func (sid ID) MarshalJSON() ([]byte, error) {
buff := make([]byte, 0, 22)
buff = append(buff, '"')
buff = strconv.AppendInt(buff, int64(sid), 10)
buff = append(buff, '"')
return buff, nil
}
// UnmarshalJSON converts a json byte array of a snowflake ID into an ID type.
func (sid *ID) UnmarshalJSON(b []byte) error {
if len(b) < 3 || b[0] != '"' || b[len(b)-1] != '"' {
return JSONSyntaxError{b}
}
i, err := strconv.ParseInt(string(b[1:len(b)-1]), 10, 64)
if err != nil {
return err
}
*sid = ID(i)
return nil
}
// Time returns an int64 unix timestamp in milliseconds of the snowflake ID time
func (sid ID) Time() int64 {
return (int64(sid) >> timestampShift) + epoch
}
// GetTimestampMax Timestamp maximum
func GetTimestampMax() int64 {
return timestampMax
}
// GetDatacenterIDMax Maximum number of data center id supported.
func GetDatacenterIDMax() int64 {
return datacenterIDMax
}
// GetWorkerIDMax Maximum number of machine id supported
func GetWorkerIDMax() int64 {
return workerIDMax
}
// GetSequenceMask Maximum number of sequence id supported
func GetSequenceMask() int64 {
return sequenceMask
}