/
encoding.go
625 lines (513 loc) · 13.8 KB
/
encoding.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
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
/*
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 encoding
import (
"fmt"
"io"
"math"
"sync"
"github.com/openGemini/openGemini/lib/logger"
"github.com/openGemini/openGemini/lib/numberenc"
"github.com/openGemini/openGemini/lib/util"
"github.com/openGemini/openGemini/lib/util/lifted/vm/protoparser/influx"
)
const (
// BlockFloat64 designates a block encodes float64 values.
BlockFloat64 = byte(influx.Field_Type_Float)
// BlockInteger designates a block encodes int64 values.
BlockInteger = byte(influx.Field_Type_Int)
// BlockBoolean designates a block encodes boolean values.
BlockBoolean = byte(influx.Field_Type_Boolean)
// BlockString designates a block encodes string values.
BlockString = byte(influx.Field_Type_String)
// BlockTag designates a block encodes tag values.
BlockTag = byte(influx.Field_Type_Tag)
BlockOneBegin = 16
BlockFloat64One = 17
BlockIntegerOne = 18
BlockBooleanOne = 19
BlockStringOne = 20
BlockOneEnd = 21
BlockFullBegin = 30
BlockFloat64Full = 31
BlockIntegerFull = 32
BlockBooleanFull = 33
BlockStringFull = 34
BlockFullEnd = 35
BlockEmptyBegin = 40
BlockFloat64Empty = 41
BlockIntegerEmpty = 42
BlockBooleanEmpty = 43
BlockStringEmpty = 44
BlockEmptyEnd = 45
)
func IsBlockOne(typ uint8) bool {
return typ > BlockOneBegin && typ < BlockOneEnd
}
func IsBlockFull(typ uint8) bool {
return typ > BlockFullBegin && typ < BlockFullEnd
}
func IsBlockEmpty(typ uint8) bool {
return typ > BlockEmptyBegin && typ < BlockEmptyEnd
}
func RewriteTypeToFull(typ uint8) uint8 {
switch typ {
case BlockFloat64:
return BlockFloat64Full
case BlockInteger:
return BlockIntegerFull
case BlockBoolean:
return BlockBooleanFull
case BlockString:
return BlockStringFull
default:
return typ
}
}
func RewriteTypeToEmpty(typ uint8) uint8 {
switch typ {
case BlockFloat64:
return BlockFloat64Empty
case BlockInteger:
return BlockIntegerEmpty
case BlockBoolean:
return BlockBooleanEmpty
case BlockString:
return BlockStringEmpty
default:
return typ
}
}
type DataCoder interface {
SetEncodingType(ty int)
Encoding(in []byte, out []byte) ([]byte, error)
Decoding(in []byte, out []byte) ([]byte, error)
}
type BytesBuffer struct {
buf []byte
}
func NewBytesBuffer(buf []byte) *BytesBuffer {
return &BytesBuffer{
buf: buf[:0],
}
}
func (w *BytesBuffer) Write(p []byte) (n int, err error) {
w.buf = append(w.buf, p...)
return len(p), nil
}
func (w *BytesBuffer) WriteByte(b byte) error {
w.buf = append(w.buf, b)
return nil
}
func (w *BytesBuffer) Bytes() []byte {
return w.buf
}
func (w *BytesBuffer) Reset(b []byte) {
w.buf = b
}
func (w *BytesBuffer) Read(p []byte) (n int, err error) {
if len(w.buf) < len(p) {
err = io.EOF
}
n = copy(p, w.buf)
w.buf = w.buf[n:]
return
}
func (w *BytesBuffer) ReadByte() (b byte, err error) {
if len(w.buf) < 1 {
err = io.EOF
return
}
b = w.buf[0]
w.buf = w.buf[1:]
return
}
func (w *BytesBuffer) Len() int {
return len(w.buf)
}
func growBuffer(buf []byte, size int) []byte {
pos := len(buf)
if cap(buf) < size+pos {
n := size + pos - cap(buf)
buf = buf[:cap(buf)]
buf = append(buf, make([]byte, n)...)
}
return buf[:pos]
}
var (
intPool = sync.Pool{}
flotPool = sync.Pool{}
boolPool = sync.Pool{}
stringPool = sync.Pool{}
timePool = sync.Pool{}
)
func GetIntCoder() *Integer {
v := intPool.Get()
if v != nil {
return v.(*Integer)
}
return &Integer{buf: NewBytesBuffer(nil)}
}
func GetTimeCoder() *Time {
v := timePool.Get()
if v != nil {
return v.(*Time)
}
return &Time{buf: NewBytesBuffer(nil)}
}
func GetFloatCoder() *Float {
v := flotPool.Get()
if v != nil {
return v.(*Float)
}
return NewFloat()
}
func GetBoolCoder() *Boolean {
v := boolPool.Get()
if v != nil {
return v.(*Boolean)
}
return &Boolean{
buf: NewBytesBuffer(nil),
}
}
func GetStringCoder() *String {
v := stringPool.Get()
if v != nil {
return v.(*String)
}
return &String{buf: NewBytesBuffer(nil)}
}
func PutDataCoder(coder DataCoder) {
switch t := coder.(type) {
case *Integer:
intPool.Put(t)
case *Float:
flotPool.Put(t)
case *Boolean:
boolPool.Put(t)
case *String:
stringPool.Put(t)
case *Time:
timePool.Put(t)
default:
logger.GetLogger().Warn("PutDataCoder coder type unknown")
}
}
type CoderContext struct {
timeCoder *Time
intCoder *Integer
floatCoder *Float
stringCoder *String
boolCoder *Boolean
buf []byte
}
func NewCoderContext() *CoderContext {
return &CoderContext{}
}
func (ctx *CoderContext) Release() {
if ctx.intCoder != nil {
PutDataCoder(ctx.intCoder)
ctx.intCoder = nil
}
if ctx.floatCoder != nil {
PutDataCoder(ctx.floatCoder)
ctx.floatCoder = nil
}
if ctx.boolCoder != nil {
PutDataCoder(ctx.boolCoder)
ctx.boolCoder = nil
}
if ctx.stringCoder != nil {
PutDataCoder(ctx.stringCoder)
ctx.stringCoder = nil
}
if ctx.timeCoder != nil {
PutDataCoder(ctx.timeCoder)
ctx.timeCoder = nil
}
}
func (ctx *CoderContext) GetTimeCoder() *Time {
return ctx.timeCoder
}
func (ctx *CoderContext) SetTimeCoder(timeCoder *Time) {
ctx.timeCoder = timeCoder
}
func (ctx *CoderContext) GetIntCoder() *Integer {
return ctx.intCoder
}
func (ctx *CoderContext) SetIntCoder(intCoder *Integer) {
ctx.intCoder = intCoder
}
func (ctx *CoderContext) GetFloatCoder() *Float {
return ctx.floatCoder
}
func (ctx *CoderContext) SetFloatCoder(floatCoder *Float) {
ctx.floatCoder = floatCoder
}
func (ctx *CoderContext) GetStringCoder() *String {
return ctx.stringCoder
}
func (ctx *CoderContext) SetStringCoder(stringCoder *String) {
ctx.stringCoder = stringCoder
}
func (ctx *CoderContext) GetBoolCoder() *Boolean {
return ctx.boolCoder
}
func (ctx *CoderContext) SetBoolCoder(boolCoder *Boolean) {
ctx.boolCoder = boolCoder
}
func EncodeIntegerBlock(in, out []byte, ctx *CoderContext) ([]byte, error) {
if len(in) == 0 {
return out, nil
}
if ctx.intCoder == nil {
ctx.intCoder = GetIntCoder()
}
return ctx.intCoder.Encoding(in, out)
}
func DecodeIntegerBlock(in []byte, out *[]byte, ctx *CoderContext) ([]int64, error) {
if len(in) == 0 {
return util.Bytes2Int64Slice(*out), nil
}
if ctx.intCoder == nil {
ctx.intCoder = GetIntCoder()
}
values, err := ctx.intCoder.Decoding(in, *out)
if err != nil {
return nil, err
}
*out = values
return util.Bytes2Int64Slice(values), nil
}
func EncodeFloatBlock(in []byte, out []byte, ctx *CoderContext) ([]byte, error) {
if ctx.floatCoder == nil {
ctx.floatCoder = GetFloatCoder()
}
return ctx.floatCoder.Encoding(in, out)
}
func DecodeFloatBlock(in []byte, out *[]byte, ctx *CoderContext) ([]float64, error) {
if len(in) == 0 {
return util.Bytes2Float64Slice(*out), nil
}
if ctx.floatCoder == nil {
ctx.floatCoder = GetFloatCoder()
}
values, err := ctx.floatCoder.Decoding(in, *out)
if err != nil {
return nil, err
}
*out = values
return util.Bytes2Float64Slice(values), nil
}
func EncodeBooleanBlock(in, out []byte, ctx *CoderContext) ([]byte, error) {
if len(in) == 0 {
return out, nil
}
if ctx.boolCoder == nil {
ctx.boolCoder = GetBoolCoder()
}
return ctx.boolCoder.Encoding(in, out)
}
func DecodeBooleanBlock(in []byte, out *[]byte, ctx *CoderContext) ([]bool, error) {
if len(in) == 0 {
return util.Bytes2BooleanSlice(*out), nil
}
if ctx.boolCoder == nil {
ctx.boolCoder = GetBoolCoder()
}
values, err := ctx.boolCoder.Decoding(in, *out)
if err != nil {
return nil, err
}
*out = values
return util.Bytes2BooleanSlice(values), nil
}
const (
StringEncodingV1 uint32 = math.MaxUint32
StringEncodingV2 = math.MaxUint32 - 1
StringEncodingEnd = math.MaxUint32 - 2
)
/*
in:
| string_1 | ... | ... | string_n
return:
| length of strings | string_1 | ... | string_n | length of offset | offset_1 | ... | ... | offset_n |
*/
//lint:ignore U1000 test used
func packStringV1(in []byte, offset []uint32, ctx *CoderContext) []byte {
ctx.buf = numberenc.MarshalUint32Append(ctx.buf[:0], uint32(len(in)))
ctx.buf = append(ctx.buf, in...)
ctx.buf = numberenc.MarshalUint32Append(ctx.buf, uint32(len(offset)*util.Uint32SizeBytes))
ctx.buf = numberenc.MarshalUint32SliceAppend(ctx.buf, offset)
return ctx.buf
}
func unpackStringV1(src []byte, dstOffset []uint32) ([]byte, []uint32, error) {
byteLen := int(numberenc.UnmarshalUint32(src))
src = src[4:]
if len(src) < byteLen+4 {
return nil, nil, fmt.Errorf("too small data for string data, %v < %v", len(src), byteLen+4)
}
in := src[:byteLen]
src = src[byteLen:]
offLen := int(numberenc.UnmarshalUint32(src))
src = src[4:]
if len(src) < offLen {
return nil, nil, fmt.Errorf("too small data for string offset, %v < %v", len(src), offLen)
}
offNum := offLen / util.Uint32SizeBytes
if cap(dstOffset) < offNum {
dstOffset = make([]uint32, offNum)
}
dstOffset = dstOffset[:offNum]
for pos := 0; pos < offNum; pos++ {
dstOffset[pos] = numberenc.UnmarshalUint32(src[pos*util.Uint32SizeBytes:])
}
return in, dstOffset, nil
}
/*
in:
| string_1 | string_2 | ... | string_n
return:
| length of strings | string_1 | ... | string_n | length of len(s) | len(string_1) | ... | len(string_n) |
*/
func packStringV2(in []byte, offset []uint32, ctx *CoderContext) []byte {
ctx.buf = numberenc.MarshalUint32Append(ctx.buf[:0], StringEncodingV2) // append version first
ctx.buf = numberenc.MarshalUint32Append(ctx.buf, uint32(len(in)))
ctx.buf = append(ctx.buf, in...)
ctx.buf = numberenc.MarshalUint32Append(ctx.buf, uint32(len(offset)))
usLen := len(offset) * util.Uint32SizeBytes
preLen := len(ctx.buf)
if cap(ctx.buf)-preLen < usLen {
ctx.buf = append(ctx.buf, make([]byte, usLen)...)
ctx.buf = ctx.buf[:preLen]
}
last := len(offset) - 1
var length uint32
// loop for offset and get the length of each string
for i := 0; i < last; i++ {
length = offset[i+1] - offset[i]
ctx.buf = numberenc.MarshalUint32Append(ctx.buf, length) // length of string_i
}
// for last string
length = uint32(len(in)) - offset[last]
ctx.buf = numberenc.MarshalUint32Append(ctx.buf, length)
return ctx.buf
}
func unpackStringV2(src []byte, dstOffset []uint32) ([]byte, []uint32, error) {
byteLen := int(numberenc.UnmarshalUint32(src))
src = src[4:]
if len(src) < byteLen+4 {
return nil, nil, fmt.Errorf("too small data for string data, %v < %v", len(src), byteLen+4)
}
in := src[:byteLen]
src = src[byteLen:]
offLen := int(numberenc.UnmarshalUint32(src))
src = src[4:]
if len(src) < offLen {
return nil, nil, fmt.Errorf("too small data for string offset, %v < %v", len(src), offLen)
}
if cap(dstOffset) < offLen {
dstOffset = make([]uint32, offLen)
}
dstOffset = dstOffset[:offLen]
dstOffset[0] = 0
for i := 0; i < offLen-1; i++ {
length := numberenc.UnmarshalUint32(src[i*util.Uint32SizeBytes:]) // get length of string_i
dstOffset[i+1] = dstOffset[i] + length
}
return in, dstOffset, nil
}
func packString(in []byte, offset []uint32, ctx *CoderContext) []byte {
return packStringV2(in, offset, ctx) // use latest string encoding version to packString
}
func unpackString(src []byte, dstOffset []uint32) ([]byte, []uint32, error) {
if len(src) < 4 {
return nil, nil, fmt.Errorf("too small data for len, %v", len(src))
}
version := numberenc.UnmarshalUint32(src)
// suppose the length of src NOT greater than StringEncodingEnd
if version < StringEncodingEnd {
version = StringEncodingV1 // for compatibility, set string encoding version as V1
}
switch version {
case StringEncodingV1:
return unpackStringV1(src, dstOffset) // no version for V1
case StringEncodingV2:
return unpackStringV2(src[4:], dstOffset)
default:
return nil, nil, fmt.Errorf("does not support string encoding version %v", version)
}
}
func EncodeStringBlock(in []byte, offset []uint32, out []byte, ctx *CoderContext) ([]byte, error) {
if len(offset) == 0 {
return out, nil
}
if ctx.stringCoder == nil {
ctx.stringCoder = GetStringCoder()
ctx.buf = ctx.buf[:0]
}
src := packString(in, offset, ctx)
return ctx.stringCoder.Encoding(src, out)
}
func DecodeStringBlock(in []byte, out *[]byte, dstOffset *[]uint32, ctx *CoderContext) ([]byte, []uint32, error) {
if len(in) == 0 {
return *out, *dstOffset, nil
}
var err error
if ctx.stringCoder == nil {
ctx.stringCoder = GetStringCoder()
}
ctx.buf, err = ctx.stringCoder.Decoding(in, ctx.buf[:0])
if err != nil {
return nil, nil, err
}
var values []byte
values, *dstOffset, err = unpackString(ctx.buf, (*dstOffset)[:0])
if err != nil {
return nil, nil, err
}
*out = append(*out, values...)
return *out, *dstOffset, nil
}
func EncodeTimestampBlock(in, out []byte, ctx *CoderContext) ([]byte, error) {
if ctx.timeCoder == nil {
ctx.timeCoder = GetTimeCoder()
}
return ctx.timeCoder.Encoding(in, out)
}
func DecodeTimestampBlock(in []byte, out *[]byte, ctx *CoderContext) ([]int64, error) {
if ctx.timeCoder == nil {
ctx.timeCoder = GetTimeCoder()
}
values, err := ctx.timeCoder.Decoding(in, *out)
if err != nil {
return nil, err
}
*out = values
return util.Bytes2Int64Slice(values), nil
}
func EncodeUnsignedBlock(in, out []byte, ctx *CoderContext) ([]byte, error) {
return EncodeIntegerBlock(in, out, ctx)
}
func DecodeUnsignedBlock(in []byte, out *[]byte, ctx *CoderContext) ([]uint64, error) {
if ctx.intCoder == nil {
ctx.intCoder = GetIntCoder()
}
values, err := ctx.intCoder.Decoding(in, *out)
if err != nil {
return nil, err
}
*out = values
return util.Bytes2Uint64Slice(values), nil
}