forked from segmentio/ksuid
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathksuid.go
422 lines (341 loc) · 10.2 KB
/
ksuid.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
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
package ksuid
import (
"bytes"
"crypto/rand"
"database/sql/driver"
"encoding/binary"
"fmt"
"io"
"math"
"sync"
"time"
)
const (
// KSUID's epoch starts more recently so that the 32-bit number space gives a
// significantly higher useful lifetime of around 136 years from March 2017.
// This number (14e8) was picked to be easy to remember.
epochStamp int64 = 1400000000
totalLengthInBytes = 20
// Timestamp is a uint32
timestampLengthInBytes = 4
millisecondLengthInBytes = 2
// Payload is 14-bytes
payloadLengthInBytes = totalLengthInBytes - timestampLengthInBytes - millisecondLengthInBytes
// KSUIDs are 20 bytes when binary encoded
byteLength = timestampLengthInBytes + millisecondLengthInBytes + payloadLengthInBytes
// The length of a KSUID when string (base62) encoded
stringEncodedLength = 27
// A string-encoded minimum value for a KSUID
minStringEncoded = "000000000000000000000000000"
// A string-encoded maximum value for a KSUID
maxStringEncoded = "aWgEPTl1tmebfsQzFP4bxwgy80V"
)
// KSUIDs are 20 bytes:
//
// 00-03 byte: uint32 BE UTC timestamp with custom epoch
// 04-09 byte: uint64 BE microsecond timestamp
// 10-19 byte: random "payload"
type KSUID [byteLength]byte
var (
rander = rand.Reader
randMutex = sync.Mutex{}
randBuffer = [payloadLengthInBytes]byte{}
errSize = fmt.Errorf("valid KSUIDs are %v bytes", byteLength)
errStrSize = fmt.Errorf("valid encoded KSUIDs are %v characters", stringEncodedLength)
errStrValue = fmt.Errorf("valid encoded KSUIDs are bounded by %s and %s", minStringEncoded, maxStringEncoded)
errPayloadSize = fmt.Errorf("valid KSUID payloads are %v bytes", payloadLengthInBytes)
// Represents a completely empty (invalid) KSUID
Nil KSUID
// Represents the highest value a KSUID can have
Max = KSUID{255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255}
)
// Append appends the string representation of i to b, returning a slice to a
// potentially larger memory area.
func (i KSUID) Append(b []byte) []byte {
return fastAppendEncodeBase62(b, i[:])
}
// The timestamp portion of the ID as a Time object
func (i KSUID) Time() time.Time {
millis := i.Milliseconds()
ts := int64(i.Timestamp()) + epochStamp
return time.Unix(ts, int64(millis)*1e6)
}
// The timestamp portion of the ID as a bare integer which is uncorrected
// for KSUID's special epoch.
func (i KSUID) Timestamp() uint32 {
return binary.BigEndian.Uint32(i[:timestampLengthInBytes])
}
// The timestamp portion of the ID as a bare integer which is uncorrected
// for KSUID's special epoch.
func (i KSUID) Milliseconds() uint16 {
return binary.BigEndian.Uint16(i[timestampLengthInBytes : timestampLengthInBytes+millisecondLengthInBytes])
}
// The 16-byte random payload without the timestamp
func (i KSUID) Payload() []byte {
return i[timestampLengthInBytes+millisecondLengthInBytes:]
}
// String-encoded representation that can be passed through Parse()
func (i KSUID) String() string {
return string(i.Append(make([]byte, 0, stringEncodedLength)))
}
// Raw byte representation of KSUID
func (i KSUID) Bytes() []byte {
// Safe because this is by-value
return i[:]
}
// IsNil returns true if this is a "nil" KSUID
func (i KSUID) IsNil() bool {
return i == Nil
}
// Get satisfies the flag.Getter interface, making it possible to use KSUIDs as
// part of of the command line options of a program.
func (i KSUID) Get() interface{} {
return i
}
// Set satisfies the flag.Value interface, making it possible to use KSUIDs as
// part of of the command line options of a program.
func (i *KSUID) Set(s string) error {
return i.UnmarshalText([]byte(s))
}
func (i KSUID) MarshalText() ([]byte, error) {
return []byte(i.String()), nil
}
func (i KSUID) MarshalBinary() ([]byte, error) {
return i.Bytes(), nil
}
func (i *KSUID) UnmarshalText(b []byte) error {
id, err := Parse(string(b))
if err != nil {
return err
}
*i = id
return nil
}
func (i *KSUID) UnmarshalBinary(b []byte) error {
id, err := FromBytes(b)
if err != nil {
return err
}
*i = id
return nil
}
// Value converts the KSUID into a SQL driver value which can be used to
// directly use the KSUID as parameter to a SQL query.
func (i KSUID) Value() (driver.Value, error) {
if i.IsNil() {
return nil, nil
}
return i.String(), nil
}
// Scan implements the sql.Scanner interface. It supports converting from
// string, []byte, or nil into a KSUID value. Attempting to convert from
// another type will return an error.
func (i *KSUID) Scan(src interface{}) error {
switch v := src.(type) {
case nil:
return i.scan(nil)
case []byte:
return i.scan(v)
case string:
return i.scan([]byte(v))
default:
return fmt.Errorf("Scan: unable to scan type %T into KSUID", v)
}
}
func (i *KSUID) scan(b []byte) error {
switch len(b) {
case 0:
*i = Nil
return nil
case byteLength:
return i.UnmarshalBinary(b)
case stringEncodedLength:
return i.UnmarshalText(b)
default:
return errSize
}
}
// Parse decodes a string-encoded representation of a KSUID object
func Parse(s string) (KSUID, error) {
if len(s) != stringEncodedLength {
return Nil, errStrSize
}
src := [stringEncodedLength]byte{}
dst := [byteLength]byte{}
copy(src[:], s[:])
if err := fastDecodeBase62(dst[:], src[:]); err != nil {
return Nil, errStrValue
}
return FromBytes(dst[:])
}
// Parse decodes a string-encoded representation of a KSUID object.
// Same behavior as Parse, but returns a Nil KSUID on error.
func ParseOrNil(s string) KSUID {
ksuid, err := Parse(s)
if err != nil {
return Nil
}
return ksuid
}
func timeToCorrectedMilliseconds(t time.Time) uint16 {
return uint16(t.UnixMilli()) % 1000
}
func timeToCorrectedUTCTimestamp(t time.Time) uint32 {
return uint32(t.Unix() - epochStamp)
}
func correctedUTCTimestampToTime(ts uint32, ms uint16) time.Time {
return time.Unix(int64(ts)+epochStamp, int64(ms)*1e6)
}
// Generates a new KSUID. In the strange case that random bytes
// can't be read, it will panic.
func New() KSUID {
ksuid, err := NewRandom()
if err != nil {
panic(fmt.Sprintf("Couldn't generate KSUID, inconceivable! error: %v", err))
}
return ksuid
}
// Generates a new KSUID
func NewRandom() (ksuid KSUID, err error) {
return NewRandomWithTime(time.Now())
}
func NewRandomWithTime(t time.Time) (ksuid KSUID, err error) {
// Go's default random number generators are not safe for concurrent use by
// multiple goroutines, the use of the rander and randBuffer are explicitly
// synchronized here.
randMutex.Lock()
_, err = io.ReadAtLeast(rander, randBuffer[:], len(randBuffer))
copy(ksuid[timestampLengthInBytes+millisecondLengthInBytes:], randBuffer[:])
randMutex.Unlock()
if err != nil {
ksuid = Nil // don't leak random bytes on error
return
}
ts := timeToCorrectedUTCTimestamp(t)
binary.BigEndian.PutUint32(ksuid[:timestampLengthInBytes], ts)
millis := timeToCorrectedMilliseconds(t)
binary.BigEndian.PutUint16(ksuid[timestampLengthInBytes:timestampLengthInBytes+millisecondLengthInBytes+1], millis&0xFFFF)
return
}
// Constructs a KSUID from constituent parts
func FromParts(t time.Time, payload []byte) (KSUID, error) {
if len(payload) != payloadLengthInBytes {
return Nil, errPayloadSize
}
var ksuid KSUID
ts := timeToCorrectedUTCTimestamp(t)
binary.BigEndian.PutUint32(ksuid[:timestampLengthInBytes], ts)
millis := timeToCorrectedMilliseconds(t)
binary.BigEndian.PutUint16(ksuid[timestampLengthInBytes:timestampLengthInBytes+millisecondLengthInBytes+1], millis&0xFFFF)
copy(ksuid[timestampLengthInBytes+millisecondLengthInBytes+millisecondLengthInBytes:], payload)
return ksuid, nil
}
// Constructs a KSUID from constituent parts.
// Same behavior as FromParts, but returns a Nil KSUID on error.
func FromPartsOrNil(t time.Time, payload []byte) KSUID {
ksuid, err := FromParts(t, payload)
if err != nil {
return Nil
}
return ksuid
}
// Constructs a KSUID from a 20-byte binary representation
func FromBytes(b []byte) (KSUID, error) {
var ksuid KSUID
if len(b) != byteLength {
return Nil, errSize
}
copy(ksuid[:], b)
return ksuid, nil
}
// Constructs a KSUID from a 20-byte binary representation.
// Same behavior as FromBytes, but returns a Nil KSUID on error.
func FromBytesOrNil(b []byte) KSUID {
ksuid, err := FromBytes(b)
if err != nil {
return Nil
}
return ksuid
}
// Sets the global source of random bytes for KSUID generation. This
// should probably only be set once globally. While this is technically
// thread-safe as in it won't cause corruption, there's no guarantee
// on ordering.
func SetRand(r io.Reader) {
if r == nil {
rander = rand.Reader
return
}
rander = r
}
// Implements comparison for KSUID type
func Compare(a, b KSUID) int {
return bytes.Compare(a[:], b[:])
}
// Sorts the given slice of KSUIDs
func Sort(ids []KSUID) {
quickSort(ids, 0, len(ids)-1)
}
// IsSorted checks whether a slice of KSUIDs is sorted
func IsSorted(ids []KSUID) bool {
if len(ids) != 0 {
min := ids[0]
for _, id := range ids[1:] {
if bytes.Compare(min[:], id[:]) > 0 {
return false
}
min = id
}
}
return true
}
func quickSort(a []KSUID, lo int, hi int) {
if lo < hi {
pivot := a[hi]
i := lo - 1
for j, n := lo, hi; j != n; j++ {
if bytes.Compare(a[j][:], pivot[:]) < 0 {
i++
a[i], a[j] = a[j], a[i]
}
}
i++
if bytes.Compare(a[hi][:], a[i][:]) < 0 {
a[i], a[hi] = a[hi], a[i]
}
quickSort(a, lo, i-1)
quickSort(a, i+1, hi)
}
}
// Next returns the next KSUID after id.
func (id KSUID) Next() KSUID {
zero := makeUint128(0, 0)
t := id.Timestamp()
u := uint128Payload(id)
v := add128(u, makeUint128(0, 1))
if v == zero { // overflow
t++
}
return v.ksuid(t)
}
// Prev returns the previoud KSUID before id.
func (id KSUID) Prev() KSUID {
max := makeUint128(math.MaxUint64, math.MaxUint64)
t := id.Timestamp()
u := uint128Payload(id)
v := sub128(u, makeUint128(0, 1))
if v == max { // overflow
t--
}
return v.ksuid(t)
}
func DecodeParts(s string) (uint32, uint32, []byte) {
bytes, err := Parse(s)
if err != nil {
panic(err)
}
ts := binary.BigEndian.Uint32(bytes[0:timestampLengthInBytes])
micros := binary.BigEndian.Uint32(bytes[timestampLengthInBytes : timestampLengthInBytes+millisecondLengthInBytes+1])
payload := bytes[timestampLengthInBytes+millisecondLengthInBytes:]
return ts, micros, payload
}