-
Notifications
You must be signed in to change notification settings - Fork 1
/
ksuid.go
91 lines (77 loc) · 2.47 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
package uksuid
import (
"time"
"github.com/gocql/gocql"
"github.com/pkg/errors"
"github.com/segmentio/ksuid"
)
// Taken from: https://github.com/segmentio/ksuid/blob/master/ksuid.go
// 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.
var epochStamp uint32 = 1400000000
// KSUID is a wrapper around github.com/segmentio/ksuid.
// It implements some convenient Marshalling/Unmarshalling
// interfaces for better compatibility across-databases.
type KSUID struct {
ksuid.KSUID
}
// MarshalCQL converts the KSUID into GoCql-compatible []byte.
func (k KSUID) MarshalCQL(info gocql.TypeInfo) ([]byte, error) {
return k.MarshalBinary()
}
// UnmarshalCQL converts GoCQL bytes to local KSUID.
func (k *KSUID) UnmarshalCQL(info gocql.TypeInfo, data []byte) error {
return k.UnmarshalBinary(data)
}
// Timestamp returns timestamp portion of the ID as a bare integer,
// which is corrected for KSUID's special epoch.
// Timestamp is in Milliseconds.
func (k KSUID) Timestamp() uint32 {
t := k.TimestampUncorrected()
return t + epochStamp
}
// TimestampUncorrected returns timestamp portion of the ID as a bare integer,
// which is uncorrected for KSUID's special epoch.
// Timestamp is in Milliseconds.
func (k KSUID) TimestampUncorrected() uint32 {
return k.KSUID.Timestamp()
}
// New creates a new random KSUID using current time.
func New() (KSUID, error) {
u, err := ksuid.NewRandom()
if err != nil {
err = errors.Wrap(err, "Error generating KSUID")
return KSUID{}, err
}
return KSUID{u}, nil
}
// NewWithTime creates a new random KSUID using specified time.
func NewWithTime(time time.Time) (KSUID, error) {
u, err := ksuid.NewRandomWithTime(time)
if err != nil {
err = errors.Wrapf(
err,
"Error generating KSUID with Time: %s", time.String(),
)
return KSUID{}, err
}
return KSUID{u}, nil
}
// Compare implements comparison for KSUID type.
// The comparison is done using bytes.Compare.
// The result will be 0 if a==b, -1 if a < b, and +1 if a > b.
func Compare(a KSUID, b KSUID) int {
return ksuid.Compare(a.KSUID, b.KSUID)
}
// IsSorted checks whether a slice of KSUIDs is sorted
func IsSorted(ids []KSUID) bool {
return ksuid.IsSorted(ToWrappedKSUID(ids...))
}
// Sort returns sorted KSUID
func Sort(ids []KSUID) []KSUID {
ksids := ToWrappedKSUID(ids...)
ksuid.Sort(ksids)
ids = ToWrapperKSUID(ksids...)
return ids
}