-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathidhost.go
197 lines (173 loc) · 4.71 KB
/
idhost.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
package k4id
import (
"math/big"
"strings"
"time"
)
const checksumSize = 1
const defaultIDLength = 15
const defaultTimeGenerator = TimeGeneratorMicro
var globalIDHost Generator
type TimeGenerator int
const (
//TimeGeneratorNano 11 Char string
TimeGeneratorNano TimeGenerator = iota
// TimeGeneratorMicro 9 Char string
TimeGeneratorMicro
// TimeGeneratorMilli 7-8 Char string
TimeGeneratorMilli
// TimeGeneratorSecond 6 Char string
TimeGeneratorSecond
// TimeGeneratorMinute 5 Char string
TimeGeneratorMinute
// TimeGeneratorHour 4 Char string
TimeGeneratorHour
// TimeGeneratorDay 3 Char string
TimeGeneratorDay
)
// Generate returns a string representation of the time in the given format
func (t TimeGenerator) Generate(src time.Time) string {
var i big.Int
switch t {
case TimeGeneratorNano:
i.SetInt64(src.UnixNano())
case TimeGeneratorMicro:
i.SetInt64(src.UnixMicro())
case TimeGeneratorMilli:
i.SetInt64(src.UnixMilli())
case TimeGeneratorSecond:
i.SetInt64(src.Unix())
case TimeGeneratorMinute:
i.SetInt64(src.Unix() / 60)
case TimeGeneratorHour:
i.SetInt64(src.Unix() / 3600)
case TimeGeneratorDay:
i.SetInt64(src.Unix() / 86400)
}
return i.Text(62)
}
func (t TimeGenerator) Parse(src string) time.Time {
var i big.Int
i.SetString(src, 62)
switch t {
case TimeGeneratorNano:
return time.Unix(0, i.Int64())
case TimeGeneratorMicro:
return time.Unix(0, i.Int64()*1000)
case TimeGeneratorMilli:
return time.Unix(0, i.Int64()*1000000)
case TimeGeneratorSecond:
return time.Unix(i.Int64(), 0)
case TimeGeneratorMinute:
return time.Unix(i.Int64()*60, 0)
case TimeGeneratorHour:
return time.Unix(i.Int64()*3600, 0)
case TimeGeneratorDay:
return time.Unix(i.Int64()*86400, 0)
}
return time.Time{}
}
func init() {
globalIDHost = DefaultGenerator()
}
// Generator is a unique ID generator that can be configured
type Generator struct {
hostID string
hostIDLength int
idLength int
timeSize TimeGenerator
generation chan bool
withTime *time.Time
}
// DefaultGenerator returns a new Generator with default configuration
func DefaultGenerator() Generator {
h := Generator{
idLength: defaultIDLength,
timeSize: defaultTimeGenerator,
}
h.randomHostID()
h.generation = make(chan bool, 1)
return h
}
// SetHostID sets the host ID to be used when generating IDs
func (h *Generator) SetHostID(id string) {
h.hostID = id
h.hostIDLength = len(h.hostID)
}
// GetHostID returns the current host ID
func (h *Generator) GetHostID() string { return h.hostID }
func (h *Generator) randomHostID() { h.SetHostID(randomString(2)) }
// SetBaseLength sets the length of the base string
func (h *Generator) SetBaseLength(size int) { h.idLength = size }
// SetTimeSize sets the time size
func (h *Generator) SetTimeSize(size TimeGenerator) { h.timeSize = size }
// SetTime sets the time to be used when generating IDs, this may result in duplicate IDs being generated
func (h *Generator) SetTime(when time.Time) { h.withTime = &when }
// ClearTime clears the time to be used when generating IDs, using the current time for ID generation
func (h *Generator) ClearTime() { h.withTime = nil }
// New returns a new ID from the generator
func (h *Generator) New() ID {
if h.hostID == "" {
h.randomHostID()
}
h.generation <- true
i := ID{}
i.uniqueKey = h.randomID()
i.verification = i.checkSum(i.uniqueKey)
if h.timeSize == TimeGeneratorNano && h.idLength < 15 {
//Sleep for a nanosecond to ensure uniqueness when precision is needed
time.Sleep(time.Nanosecond)
}
<-h.generation
return i
}
func (h *Generator) time() time.Time {
if h.withTime == nil {
return time.Now()
}
return *h.withTime
}
func (h *Generator) randomID() string {
tId := h.reverse(h.timeSize.Generate(h.time()))
useLen := h.idLength - len(tId) - len(h.hostID)
return h.fixLen(tId+h.hostID+randomString(useLen), h.idLength)
}
func (h *Generator) reverse(s string) string {
runes := []rune(s)
for i, j := 0, len(runes)-1; i < j; i, j = i+1, j-1 {
runes[i], runes[j] = runes[j], runes[i]
}
return string(runes)
}
func (h *Generator) fixLen(input string, reqLen int) string {
srcLen := len(input)
if srcLen == reqLen {
return input
}
if srcLen > reqLen {
return input[:reqLen]
}
return input + strings.Repeat("X", reqLen-srcLen)
}
func (h *Generator) ExtractTime(fullID string) time.Time {
id := FromString(fullID).uniqueKey
timeSize := 3
switch h.timeSize {
case TimeGeneratorNano:
timeSize = 11
case TimeGeneratorMicro:
timeSize = 9
case TimeGeneratorMilli:
timeSize = 7
case TimeGeneratorSecond:
timeSize = 6
case TimeGeneratorMinute:
timeSize = 5
case TimeGeneratorHour:
timeSize = 4
}
if len(id) < timeSize {
return time.Time{}
}
return h.timeSize.Parse(h.reverse(id[:timeSize]))
}