-
Notifications
You must be signed in to change notification settings - Fork 5.9k
/
data.go
165 lines (129 loc) · 2.99 KB
/
data.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
// Copyright 2016 PingCAP, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// See the License for the specific language governing permissions and
// limitations under the License.
package main
import (
"fmt"
"sync"
"time"
)
var defaultStep int64 = 1
type datum struct {
sync.Mutex
intValue int64
minIntValue int64
maxIntValue int64
timeValue time.Time
step int64
init bool
useRange bool
}
func newDatum() *datum {
return &datum{intValue: -1, step: 1}
}
func (d *datum) setInitInt64Value(step int64, min int64, max int64) {
d.Lock()
defer d.Unlock()
if d.init {
return
}
d.step = step
if min != -1 {
d.minIntValue = min
d.intValue = min
}
if min < max {
d.maxIntValue = max
d.useRange = true
}
d.init = true
}
func (d *datum) uniqInt64() int64 {
d.Lock()
defer d.Unlock()
data := d.intValue
if d.useRange {
if d.intValue+d.step > d.maxIntValue {
return data
}
}
d.intValue += d.step
return data
}
func (d *datum) uniqFloat64() float64 {
data := d.uniqInt64()
return float64(data)
}
func (d *datum) uniqString(n int) string {
d.Lock()
d.intValue++
data := d.intValue
d.Unlock()
var value []byte
for ; ; n-- {
if n == 0 {
break
}
idx := data % int64(len(alphabet))
data = data / int64(len(alphabet))
value = append(value, alphabet[idx])
if data == 0 {
break
}
}
for i, j := 0, len(value)-1; i < j; i, j = i+1, j-1 {
value[i], value[j] = value[j], value[i]
}
return string(value)
}
func (d *datum) uniqTime() string {
d.Lock()
defer d.Unlock()
if d.timeValue.IsZero() {
d.timeValue = time.Now()
} else {
d.timeValue = d.timeValue.Add(time.Duration(d.step) * time.Second)
}
return fmt.Sprintf("%02d:%02d:%02d", d.timeValue.Hour(), d.timeValue.Minute(), d.timeValue.Second())
}
func (d *datum) uniqDate() string {
d.Lock()
defer d.Unlock()
if d.timeValue.IsZero() {
d.timeValue = time.Now()
} else {
d.timeValue = d.timeValue.AddDate(0, 0, int(d.step))
}
return fmt.Sprintf("%04d-%02d-%02d", d.timeValue.Year(), d.timeValue.Month(), d.timeValue.Day())
}
func (d *datum) uniqTimestamp() string {
d.Lock()
defer d.Unlock()
if d.timeValue.IsZero() {
d.timeValue = time.Now()
} else {
d.timeValue = d.timeValue.Add(time.Duration(d.step) * time.Second)
}
return fmt.Sprintf("%04d-%02d-%02d %02d:%02d:%02d",
d.timeValue.Year(), d.timeValue.Month(), d.timeValue.Day(),
d.timeValue.Hour(), d.timeValue.Minute(), d.timeValue.Second())
}
func (d *datum) uniqYear() string {
d.Lock()
defer d.Unlock()
if d.timeValue.IsZero() {
d.timeValue = time.Now()
} else {
d.timeValue = d.timeValue.AddDate(int(d.step), 0, 0)
}
return fmt.Sprintf("%04d", d.timeValue.Year())
}