-
Notifications
You must be signed in to change notification settings - Fork 0
/
lexid.go
238 lines (204 loc) · 6.99 KB
/
lexid.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
package lexid
import (
"fmt"
"math"
"strings"
"sync/atomic"
"time"
"github.com/kokizzu/gotro/S"
"github.com/kpango/fastime"
)
/* generate based on 3 parts:
1. current second
2. atomic increment
3. server unique id
need to set Identity if you have multiple server/instance
*/
var lastSec int64
var incNano int64
var Config *Generator
var DefaultSeparator = `~`
var DefaultMinCounterLength = 6
var DefaultMinTimeLength = 6
var DefaultMinNanoTimeLength = 11
var DefaultMinDateOffset = int64(0)
var DefaultMinNanoDateOffset = int64(0)
var Offset2020 time.Time // MinDateOffset = Offset2020.Unix() or MinNanoDateOffset = Offset2020.UnixNano()
var SeparatorIdentity = `~0`
func init() {
DefaultMinCounterLength = len(S.EncodeCB63(int64(math.MaxUint32), 0))
DefaultMinTimeLength = len(S.EncodeCB63(time.Now().Unix(), 0))
DefaultMinNanoTimeLength = len(S.EncodeCB63(time.Now().UnixNano(), 0))
Config = NewGenerator(SeparatorIdentity)
Offset2020 = time.Date(2020, time.January, 1, 0, 0, 0, 0, time.UTC)
Config.MinTimeLength = DefaultMinTimeLength
Config.MinNanoTimeLength = DefaultMinNanoTimeLength
Config.MinDateOffset = DefaultMinDateOffset
Config.MinNanoDateOffset = DefaultMinNanoDateOffset
}
func Parse(id string, isNano bool) (*Segments, error) {
return Config.Parse(id, isNano)
}
func FromUnixCounterIdent(time int64, counter uint32, ident string) string {
return Config.FromUnixCounterIdent(time, counter, ident)
}
func FromUnixCounter(time int64, counter uint32) string {
return Config.FromUnixCounter(time, counter)
}
func FromUnix(time int64) string {
return Config.FromUnix(time)
}
func FromNanoCounterIdent(time int64, counter uint32, ident string) string {
return Config.FromNanoCounterIdent(time, counter, ident)
}
func FromNanoCounter(time int64, counter uint32) string {
return Config.FromNanoCounter(time, counter)
}
func FromNano(time int64) string {
return Config.FromNano(time)
}
// generate unique ID (second, smaller)
func ID() string {
return Config.ID()
}
// generate unique ID (accurate)
func NanoID() string {
return Config.NanoID()
}
type Segments struct {
Time int64
Counter uint32
Identity string
IsNano bool
Generator *Generator
}
func (s *Segments) ToTime() time.Time {
if s.IsNano {
return time.Unix(0, s.Time)
}
return time.Unix(s.Time, 0)
}
func (s *Segments) ToID() string {
if s.Generator == nil {
s.Generator = Config
}
if s.IsNano {
return s.Generator.FromNanoCounterIdent(s.Time, s.Counter, s.Identity)
}
return s.Generator.FromUnixCounterIdent(s.Time, s.Counter, s.Identity)
}
// object-oriented version
type Generator struct {
AtomicCounter uint32
Separator string
Identity string
MinCounterLength int
MinTimeLength int
MinNanoTimeLength int
MinDateOffset int64
MinNanoDateOffset int64
}
func NewGenerator(uniqStr string) *Generator {
return &Generator{
AtomicCounter: 0,
Separator: DefaultSeparator,
Identity: uniqStr,
MinCounterLength: DefaultMinCounterLength,
MinTimeLength: DefaultMinTimeLength,
MinNanoTimeLength: DefaultMinNanoTimeLength,
MinDateOffset: DefaultMinDateOffset,
MinNanoDateOffset: DefaultMinNanoDateOffset,
}
}
func (gen *Generator) Reinit() {
gen.AtomicCounter = 0
gen.Separator = DefaultSeparator
gen.MinCounterLength = DefaultMinCounterLength
gen.MinTimeLength = DefaultMinTimeLength // >=6
gen.MinNanoTimeLength = DefaultMinNanoTimeLength // >=11
gen.MinDateOffset = DefaultMinDateOffset
gen.MinNanoDateOffset = DefaultMinNanoDateOffset
}
func (gen *Generator) ID() string {
now := fastime.UnixNow()
counter := atomic.AddUint32(&gen.AtomicCounter, 1)
if now != lastSec { // reset to 0 if not the same second
atomic.SwapInt64(&lastSec, now)
atomic.SwapUint32(&gen.AtomicCounter, 0) // ignore old value
counter = atomic.AddUint32(&gen.AtomicCounter, 1)
}
return S.EncodeCB63(now-gen.MinDateOffset, gen.MinTimeLength) + gen.Separator + S.EncodeCB63(int64(counter), gen.MinCounterLength) + gen.Identity
}
func (gen *Generator) NanoID() string {
counter := atomic.AddUint32(&gen.AtomicCounter, 1)
if counter == 0 { // add 1 nanosecond everytime generating 4 million IDs
atomic.AddInt64(&incNano, 1)
}
return S.EncodeCB63(fastime.UnixNanoNow()+incNano-gen.MinNanoDateOffset, gen.MinTimeLength) + gen.Separator + S.EncodeCB63(int64(counter), gen.MinCounterLength) + gen.Identity
}
func (gen *Generator) FromUnixCounterIdent(time int64, counter uint32, ident string) string {
return S.EncodeCB63(time-gen.MinDateOffset, gen.MinTimeLength) + gen.Separator + S.EncodeCB63(int64(counter), gen.MinCounterLength) + ident
}
func (gen *Generator) FromUnixCounter(time int64, counter uint32) string {
return S.EncodeCB63(time-gen.MinDateOffset, gen.MinTimeLength) + gen.Separator + S.EncodeCB63(int64(counter), gen.MinCounterLength) + gen.Identity
}
func (gen *Generator) FromUnix(time int64) string {
counter := atomic.AddUint32(&gen.AtomicCounter, 1)
return S.EncodeCB63(time-gen.MinDateOffset, gen.MinTimeLength) + gen.Separator + S.EncodeCB63(int64(counter), gen.MinCounterLength) + gen.Identity
}
func (gen *Generator) FromNanoCounterIdent(time int64, counter uint32, ident string) string {
return S.EncodeCB63(time-gen.MinNanoDateOffset, gen.MinNanoTimeLength) + gen.Separator + S.EncodeCB63(int64(counter), gen.MinCounterLength) + ident
}
func (gen *Generator) FromNanoCounter(time int64, counter uint32) string {
return S.EncodeCB63(time-gen.MinNanoDateOffset, gen.MinNanoTimeLength) + gen.Separator + S.EncodeCB63(int64(counter), gen.MinCounterLength) + gen.Identity
}
func (gen *Generator) FromNano(time int64) string {
counter := atomic.AddUint32(&gen.AtomicCounter, 1)
return S.EncodeCB63(time-gen.MinNanoDateOffset, gen.MinNanoTimeLength) + gen.Separator + S.EncodeCB63(int64(counter), gen.MinCounterLength) + gen.Identity
}
func (gen *Generator) Parse(id string, isNano bool) (*Segments, error) {
var segments []string
if gen.Separator == `` {
// try parse as unixnano
start := gen.MinNanoTimeLength
end := start + gen.MinCounterLength
if len(id) < end {
// try parse as unix
start = gen.MinTimeLength
end = start + gen.MinCounterLength
if len(id) < end {
return nil, fmt.Errorf(`invalid lexid length: %s %d < %d+%d`, id, len(id), gen.MinTimeLength, gen.MinCounterLength)
}
}
segments = []string{
id[:start],
id[start:end],
id[end:],
}
} else {
segments = strings.Split(id, gen.Separator)
if len(segments) != 3 {
return nil, fmt.Errorf(`invalid lexid or separator: %#v %s`, segments, gen.Separator)
}
}
timePart, timeOk := S.DecodeCB63[int64](segments[0])
ctrPart, ctrOk := S.DecodeCB63[int64](segments[1])
var err error
if isNano {
timePart += gen.MinNanoDateOffset
} else {
timePart += gen.MinDateOffset
}
res := &Segments{
Time: timePart,
Counter: uint32(ctrPart),
Identity: segments[2],
IsNano: isNano,
}
if !timeOk {
err = fmt.Errorf(`unable to parse time segment: %#v`, segments[0])
} else if !ctrOk {
err = fmt.Errorf(`unable to parse counter segment: %#v`, segments[1])
}
return res, err
}