-
Notifications
You must be signed in to change notification settings - Fork 139
/
util.go
487 lines (398 loc) · 9.73 KB
/
util.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
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
/*
Copyright 2022 Huawei Cloud Computing Technologies Co., Ltd.
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,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package util
import (
"io"
"reflect"
"time"
"unsafe"
"github.com/influxdata/influxdb/models"
"github.com/influxdata/influxdb/toml"
"github.com/openGemini/openGemini/lib/cpu"
"go.uber.org/zap"
)
const (
TierBegin = 0
Hot = 1
Warm = 2
Cold = 3
Moving = 4
TierEnd = 5
)
const (
BooleanSizeBytes = int(unsafe.Sizeof(false))
Uint32SizeBytes = int(unsafe.Sizeof(uint32(0)))
Uint16SizeBytes = int(unsafe.Sizeof(uint16(0)))
Uint64SizeBytes = int(unsafe.Sizeof(uint64(0)))
Int16SizeBytes = int(unsafe.Sizeof(int16(0)))
Int32SizeBytes = int(unsafe.Sizeof(int32(0)))
Int64SizeBytes = int(unsafe.Sizeof(int64(0)))
Float32SizeBytes = int(unsafe.Sizeof(float32(0)))
Float64SizeBytes = int(unsafe.Sizeof(float64(0)))
Int8SizeBytes = int(unsafe.Sizeof(int8(0)))
)
const RowsNumPerFragment int = 8192
const MaxMeasurementLengthWithVersion = 255
// eg, version is:_0000
const MeasurementVersionLength = 5
// the measurement name length should consider MeasurementVersionLength
const MaxMeasurementLength = MaxMeasurementLengthWithVersion - MeasurementVersionLength
var logger *zap.Logger
func SetLogger(lg *zap.Logger) {
logger = lg
}
func MustClose(obj io.Closer) {
if obj == nil || IsObjectNil(obj) {
return
}
err := obj.Close()
if err != nil && logger != nil {
logger.WithOptions(zap.AddCallerSkip(1)).
Error("failed to close", zap.Error(err))
}
}
func IsObjectNil(obj interface{}) bool {
val := reflect.ValueOf(obj)
switch val.Kind() {
case reflect.Chan, reflect.Func, reflect.Map,
reflect.UnsafePointer, reflect.Ptr,
reflect.Interface, reflect.Slice:
return val.IsNil()
}
return false
}
type Corrector struct {
intMin int64
floatMin float64
}
func NewCorrector(intMin int64, floatMin float64) *Corrector {
return &Corrector{intMin: intMin, floatMin: floatMin}
}
func (c *Corrector) Int(v *int, def int) {
if int64(*v) <= c.intMin {
*v = def
}
}
func (c *Corrector) Uint64(v *uint64, def uint64) {
if *v <= uint64(c.intMin) {
*v = def
}
}
func (c *Corrector) Float64(v *float64, def float64) {
if *v <= c.floatMin {
*v = def
}
}
func (c *Corrector) String(v *string, def string) {
if *v == "" {
*v = def
}
}
func (c *Corrector) TomlDuration(v *toml.Duration, def toml.Duration) {
if int64(*v) <= c.intMin {
*v = def
}
}
func (c *Corrector) TomlSize(v *toml.Size, def toml.Size) {
if int64(*v) <= c.intMin {
*v = def
}
}
func TimeCost(option string) func() {
start := time.Now()
return func() {
tc := time.Since(start)
logger.Debug(option, zap.Duration("time cost", tc))
}
}
func CeilToPower2(num uint64) uint64 {
if num > (1 << 63) {
return 1 << 63
}
num--
num |= num >> 1
num |= num >> 2
num |= num >> 4
num |= num >> 8
num |= num >> 16
num |= num >> 32
return num + 1
}
func NumberOfTrailingZeros(num uint64) int {
if num == 0 {
return 64
}
n := 63
num1 := num << 32
if num1 != 0 {
n -= 32
num = num1
}
num1 = num << 16
if num1 != 0 {
n -= 16
num = num1
}
num1 = num << 8
if num1 != 0 {
n -= 8
num = num1
}
num1 = num << 4
if num1 != 0 {
n -= 4
num = num1
}
num1 = num << 2
if num1 != 0 {
n -= 2
num = num1
}
return n - int((num<<1)>>63)
}
func IntLimit(min, max int, v int) int {
if v < min {
return min
}
if v > max {
return max
}
return v
}
func Bytes2Uint16Slice(b []byte) []uint16 {
h := (*reflect.SliceHeader)(unsafe.Pointer(&b))
var res []uint16
s := (*reflect.SliceHeader)(unsafe.Pointer(&res))
s.Data = h.Data
s.Len = h.Len / Uint16SizeBytes
s.Cap = h.Cap / Uint16SizeBytes
return res
}
func Uint16Slice2byte(b []uint16) []byte {
h := (*reflect.SliceHeader)(unsafe.Pointer(&b))
var res []byte
s := (*reflect.SliceHeader)(unsafe.Pointer(&res))
s.Data = h.Data
s.Len = h.Len * Uint16SizeBytes
s.Cap = h.Cap * Uint16SizeBytes
return res
}
func Bytes2Uint32Slice(b []byte) []uint32 {
h := (*reflect.SliceHeader)(unsafe.Pointer(&b))
var res []uint32
s := (*reflect.SliceHeader)(unsafe.Pointer(&res))
s.Data = h.Data
s.Len = h.Len / Uint32SizeBytes
s.Cap = h.Cap / Uint32SizeBytes
return res
}
func Uint32Slice2byte(b []uint32) []byte {
h := (*reflect.SliceHeader)(unsafe.Pointer(&b))
var res []byte
s := (*reflect.SliceHeader)(unsafe.Pointer(&res))
s.Data = h.Data
s.Len = h.Len * Uint32SizeBytes
s.Cap = h.Cap * Uint32SizeBytes
return res
}
func BooleanSlice2byte(b []bool) []byte {
h := (*reflect.SliceHeader)(unsafe.Pointer(&b))
var res []byte
s := (*reflect.SliceHeader)(unsafe.Pointer(&res))
s.Data = h.Data
s.Len = h.Len * BooleanSizeBytes
s.Cap = h.Cap * BooleanSizeBytes
return res
}
func Bytes2BooleanSlice(b []byte) []bool {
h := (*reflect.SliceHeader)(unsafe.Pointer(&b))
var res []bool
s := (*reflect.SliceHeader)(unsafe.Pointer(&res))
s.Data = h.Data
s.Len = h.Len / BooleanSizeBytes
s.Cap = h.Cap / BooleanSizeBytes
return res
}
func Int64Slice2byte(b []int64) []byte {
h := (*reflect.SliceHeader)(unsafe.Pointer(&b))
var res []byte
s := (*reflect.SliceHeader)(unsafe.Pointer(&res))
s.Data = h.Data
s.Len = h.Len * Int64SizeBytes
s.Cap = h.Cap * Int64SizeBytes
return res
}
func Uint64Slice2byte(b []uint64) []byte {
h := (*reflect.SliceHeader)(unsafe.Pointer(&b))
var res []byte
s := (*reflect.SliceHeader)(unsafe.Pointer(&res))
s.Data = h.Data
s.Len = h.Len * Uint64SizeBytes
s.Cap = h.Cap * Uint64SizeBytes
return res
}
func Bytes2Int16Slice(b []byte) []int16 {
h := (*reflect.SliceHeader)(unsafe.Pointer(&b))
var res []int16
s := (*reflect.SliceHeader)(unsafe.Pointer(&res))
s.Data = h.Data
s.Len = h.Len / Int16SizeBytes
s.Cap = h.Cap / Int16SizeBytes
return res
}
func Bytes2Int32Slice(b []byte) []int32 {
h := (*reflect.SliceHeader)(unsafe.Pointer(&b))
var res []int32
s := (*reflect.SliceHeader)(unsafe.Pointer(&res))
s.Data = h.Data
s.Len = h.Len / Int32SizeBytes
s.Cap = h.Cap / Int32SizeBytes
return res
}
func Bytes2Int64Slice(b []byte) []int64 {
h := (*reflect.SliceHeader)(unsafe.Pointer(&b))
var res []int64
s := (*reflect.SliceHeader)(unsafe.Pointer(&res))
s.Data = h.Data
s.Len = h.Len / Int64SizeBytes
s.Cap = h.Cap / Int64SizeBytes
return res
}
func Float32Slice2byte(b []float32) []byte {
h := (*reflect.SliceHeader)(unsafe.Pointer(&b))
var res []byte
s := (*reflect.SliceHeader)(unsafe.Pointer(&res))
s.Data = h.Data
s.Len = h.Len * Float32SizeBytes
s.Cap = h.Cap * Float32SizeBytes
return res
}
func Bytes2Float32Slice(b []byte) []float32 {
h := (*reflect.SliceHeader)(unsafe.Pointer(&b))
var res []float32
s := (*reflect.SliceHeader)(unsafe.Pointer(&res))
s.Data = h.Data
s.Len = h.Len / Float32SizeBytes
s.Cap = h.Cap / Float32SizeBytes
return res
}
func Float64Slice2byte(b []float64) []byte {
h := (*reflect.SliceHeader)(unsafe.Pointer(&b))
var res []byte
s := (*reflect.SliceHeader)(unsafe.Pointer(&res))
s.Data = h.Data
s.Len = h.Len * Float64SizeBytes
s.Cap = h.Cap * Float64SizeBytes
return res
}
func Bytes2Float64Slice(b []byte) []float64 {
h := (*reflect.SliceHeader)(unsafe.Pointer(&b))
var res []float64
s := (*reflect.SliceHeader)(unsafe.Pointer(&res))
s.Data = h.Data
s.Len = h.Len / Float64SizeBytes
s.Cap = h.Cap / Float64SizeBytes
return res
}
func Bytes2Int8Slice(b []byte) []int8 {
h := (*reflect.SliceHeader)(unsafe.Pointer(&b))
var res []int8
s := (*reflect.SliceHeader)(unsafe.Pointer(&res))
s.Data = h.Data
s.Len = h.Len / Int8SizeBytes
s.Cap = h.Cap / Int8SizeBytes
return res
}
func Bytes2Uint64Slice(b []byte) []uint64 {
h := (*reflect.SliceHeader)(unsafe.Pointer(&b))
var res []uint64
s := (*reflect.SliceHeader)(unsafe.Pointer(&res))
s.Data = h.Data
s.Len = h.Len / Uint64SizeBytes
s.Cap = h.Cap / Uint64SizeBytes
return res
}
func Int16Slice2byte(b []int16) []byte {
h := (*reflect.SliceHeader)(unsafe.Pointer(&b))
var res []byte
s := (*reflect.SliceHeader)(unsafe.Pointer(&res))
s.Data = h.Data
s.Len = h.Len * Int16SizeBytes
s.Cap = h.Cap * Int16SizeBytes
return res
}
func Int32Slice2byte(b []int32) []byte {
h := (*reflect.SliceHeader)(unsafe.Pointer(&b))
var res []byte
s := (*reflect.SliceHeader)(unsafe.Pointer(&res))
s.Data = h.Data
s.Len = h.Len * Int32SizeBytes
s.Cap = h.Cap * Int32SizeBytes
return res
}
type TimeRange struct {
Min, Max int64
}
func (t TimeRange) Overlaps(min, max int64) bool {
return t.Min <= max && t.Max >= min
}
func Str2bytes(s string) []byte {
x := (*[2]uintptr)(unsafe.Pointer(&s))
h := [3]uintptr{x[0], x[1], x[1]}
return *(*[]byte)(unsafe.Pointer(&h))
}
func Bytes2str(b []byte) string {
return *(*string)(unsafe.Pointer(&b))
}
func MemorySet(buf []byte) {
for i := range buf {
buf[i] = 0
}
}
func Min(x, y int) int {
if x < y {
return x
}
return y
}
func MaxUint32(x, y uint32) uint32 {
if x < y {
return y
}
return x
}
func MinUint32(x, y uint32) uint32 {
if x < y {
return x
}
return y
}
func Float64ToUint64(v float64) uint64 {
return *(*uint64)(unsafe.Pointer(&v))
}
func Uint64ToFloat64(v uint64) float64 {
return *(*float64)(unsafe.Pointer(&v))
}
type allocItem interface {
byte | models.Row | interface{}
}
func AllocSlice[T allocItem](data []T, size int) ([]T, []T) {
start := len(data)
end := start + size
if end > cap(data) {
data = make([]T, 0, size*cpu.GetCpuNum())
end = size
start = 0
}
return data[:end], data[start:end]
}