forked from influxdata/influxdb
/
encoding.go
745 lines (615 loc) · 17.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
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
package tsm1
import (
"encoding/binary"
"fmt"
"sort"
"time"
"github.com/influxdata/influxdb/influxql"
"github.com/influxdata/influxdb/tsdb"
)
const (
// BlockFloat64 designates a block encodes float64 values
BlockFloat64 = byte(0)
// BlockInteger designates a block encodes int64 values
BlockInteger = byte(1)
// BlockBoolean designates a block encodes boolean values
BlockBoolean = byte(2)
// BlockString designates a block encodes string values
BlockString = byte(3)
// encodedBlockHeaderSize is the size of the header for an encoded block. There is one
// byte encoding the type of the block.
encodedBlockHeaderSize = 1
)
type Value interface {
UnixNano() int64
Value() interface{}
Size() int
String() string
internalOnly()
}
func NewValue(t int64, value interface{}) Value {
switch v := value.(type) {
case int64:
return &IntegerValue{unixnano: t, value: v}
case float64:
return &FloatValue{unixnano: t, value: v}
case bool:
return &BooleanValue{unixnano: t, value: v}
case string:
return &StringValue{unixnano: t, value: v}
}
return &EmptyValue{}
}
type EmptyValue struct {
}
func (e *EmptyValue) UnixNano() int64 { return tsdb.EOF }
func (e *EmptyValue) Value() interface{} { return nil }
func (e *EmptyValue) Size() int { return 0 }
func (e *EmptyValue) String() string { return "" }
func (_ *EmptyValue) internalOnly() {}
func (_ *StringValue) internalOnly() {}
func (_ *IntegerValue) internalOnly() {}
func (_ *BooleanValue) internalOnly() {}
func (_ *FloatValue) internalOnly() {}
// Values represented a time ascending sorted collection of Value types.
// the underlying type should be the same across all values, but the interface
// makes the code cleaner.
type Values []Value
func (a Values) MinTime() int64 {
return a[0].UnixNano()
}
func (a Values) MaxTime() int64 {
return a[len(a)-1].UnixNano()
}
func (a Values) Size() int {
sz := 0
for _, v := range a {
sz += v.Size()
}
return sz
}
// Encode converts the values to a byte slice. If there are no values,
// this function panics.
func (a Values) Encode(buf []byte) ([]byte, error) {
if len(a) == 0 {
panic("unable to encode block type")
}
switch a[0].(type) {
case *FloatValue:
return encodeFloatBlock(buf, a)
case *IntegerValue:
return encodeIntegerBlock(buf, a)
case *BooleanValue:
return encodeBooleanBlock(buf, a)
case *StringValue:
return encodeStringBlock(buf, a)
}
return nil, fmt.Errorf("unsupported value type %T", a[0])
}
// InfluxQLType returns the influxql.DataType the values map to.
func (a Values) InfluxQLType() (influxql.DataType, error) {
if len(a) == 0 {
return influxql.Unknown, fmt.Errorf("no values to infer type")
}
switch a[0].(type) {
case *FloatValue:
return influxql.Float, nil
case *IntegerValue:
return influxql.Integer, nil
case *BooleanValue:
return influxql.Boolean, nil
case *StringValue:
return influxql.String, nil
}
return influxql.Unknown, fmt.Errorf("unsupported value type %T", a[0])
}
// BlockType returns the type of value encoded in a block or an error
// if the block type is unknown.
func BlockType(block []byte) (byte, error) {
blockType := block[0]
switch blockType {
case BlockFloat64, BlockInteger, BlockBoolean, BlockString:
return blockType, nil
default:
return 0, fmt.Errorf("unknown block type: %d", blockType)
}
}
func BlockCount(block []byte) int {
if len(block) <= encodedBlockHeaderSize {
panic(fmt.Sprintf("count of short block: got %v, exp %v", len(block), encodedBlockHeaderSize))
}
// first byte is the block type
tb, _ := unpackBlock(block[1:])
return CountTimestamps(tb)
}
// DecodeBlock takes a byte array and will decode into values of the appropriate type
// based on the block.
func DecodeBlock(block []byte, vals []Value) ([]Value, error) {
if len(block) <= encodedBlockHeaderSize {
panic(fmt.Sprintf("decode of short block: got %v, exp %v", len(block), encodedBlockHeaderSize))
}
blockType, err := BlockType(block)
if err != nil {
return nil, err
}
switch blockType {
case BlockFloat64:
decoded, err := DecodeFloatBlock(block, nil)
if len(vals) < len(decoded) {
vals = make([]Value, len(decoded))
}
for i := range decoded {
vals[i] = &decoded[i]
}
return vals[:len(decoded)], err
case BlockInteger:
decoded, err := DecodeIntegerBlock(block, nil)
if len(vals) < len(decoded) {
vals = make([]Value, len(decoded))
}
for i := range decoded {
vals[i] = &decoded[i]
}
return vals[:len(decoded)], err
case BlockBoolean:
decoded, err := DecodeBooleanBlock(block, nil)
if len(vals) < len(decoded) {
vals = make([]Value, len(decoded))
}
for i := range decoded {
vals[i] = &decoded[i]
}
return vals[:len(decoded)], err
case BlockString:
decoded, err := DecodeStringBlock(block, nil)
if len(vals) < len(decoded) {
vals = make([]Value, len(decoded))
}
for i := range decoded {
vals[i] = &decoded[i]
}
return vals[:len(decoded)], err
default:
panic(fmt.Sprintf("unknown block type: %d", blockType))
}
}
// Deduplicate returns a new Values slice with any values that have the same timestamp removed.
// The Value that appears last in the slice is the one that is kept.
func (a Values) Deduplicate() Values {
m := make(map[int64]Value, len(a))
for _, val := range a {
m[val.UnixNano()] = val
}
other := make([]Value, 0, len(m))
for _, val := range m {
other = append(other, val)
}
sort.Sort(Values(other))
return other
}
// Sort methods
func (a Values) Len() int { return len(a) }
func (a Values) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a Values) Less(i, j int) bool { return a[i].UnixNano() < a[j].UnixNano() }
type FloatValue struct {
unixnano int64
value float64
}
func (f *FloatValue) UnixNano() int64 {
return f.unixnano
}
func (f *FloatValue) Value() interface{} {
return f.value
}
func (f *FloatValue) Size() int {
return 16
}
func (f *FloatValue) String() string {
return fmt.Sprintf("%v %v", time.Unix(0, f.unixnano), f.value)
}
func encodeFloatBlock(buf []byte, values []Value) ([]byte, error) {
if len(values) == 0 {
return nil, nil
}
// A float block is encoded using different compression strategies
// for timestamps and values.
// Encode values using Gorilla float compression
venc := NewFloatEncoder()
// Encode timestamps using an adaptive encoder that uses delta-encoding,
// frame-or-reference and run length encoding.
tsenc := NewTimeEncoder()
for _, v := range values {
tsenc.Write(time.Unix(0, v.UnixNano()))
venc.Push(v.(*FloatValue).value)
}
venc.Finish()
// Encoded timestamp values
tb, err := tsenc.Bytes()
if err != nil {
return nil, err
}
// Encoded float values
vb, err := venc.Bytes()
if err != nil {
return nil, err
}
// Prepend the first timestamp of the block in the first 8 bytes and the block
// in the next byte, followed by the block
block := packBlockHeader(BlockFloat64)
block = append(block, packBlock(tb, vb)...)
return block, nil
}
func DecodeFloatBlock(block []byte, a []FloatValue) ([]FloatValue, error) {
// Block type is the next block, make sure we actually have a float block
blockType := block[0]
if blockType != BlockFloat64 {
return nil, fmt.Errorf("invalid block type: exp %d, got %d", BlockFloat64, blockType)
}
block = block[1:]
tb, vb := unpackBlock(block)
// Setup our timestamp and value decoders
dec := NewTimeDecoder(tb)
iter, err := NewFloatDecoder(vb)
if err != nil {
return nil, err
}
// Decode both a timestamp and value
i := 0
for dec.Next() && iter.Next() {
ts := dec.Read()
v := iter.Values()
if i < len(a) {
a[i].unixnano = ts.UnixNano()
a[i].value = v
} else {
a = append(a, FloatValue{ts.UnixNano(), v})
}
i++
}
// Did timestamp decoding have an error?
if dec.Error() != nil {
return nil, dec.Error()
}
// Did float decoding have an error?
if iter.Error() != nil {
return nil, iter.Error()
}
return a[:i], nil
}
// FloatValues represents a slice of float values.
type FloatValues []FloatValue
// Deduplicate returns a new slice with any values that have the same timestamp removed.
// The Value that appears last in the slice is the one that is kept.
func (a FloatValues) Deduplicate() FloatValues {
m := make(map[int64]FloatValue)
for _, val := range a {
m[val.UnixNano()] = val
}
other := make(FloatValues, 0, len(m))
for _, val := range m {
other = append(other, val)
}
sort.Sort(other)
return other
}
// Sort methods
func (a FloatValues) Len() int { return len(a) }
func (a FloatValues) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a FloatValues) Less(i, j int) bool { return a[i].UnixNano() < a[j].UnixNano() }
type BooleanValue struct {
unixnano int64
value bool
}
func (b *BooleanValue) Size() int {
return 9
}
func (b *BooleanValue) UnixNano() int64 {
return b.unixnano
}
func (b *BooleanValue) Value() interface{} {
return b.value
}
func (f *BooleanValue) String() string {
return fmt.Sprintf("%v %v", time.Unix(0, f.unixnano), f.Value())
}
func encodeBooleanBlock(buf []byte, values []Value) ([]byte, error) {
if len(values) == 0 {
return nil, nil
}
// A boolean block is encoded using different compression strategies
// for timestamps and values.
// Encode values using Gorilla float compression
venc := NewBooleanEncoder()
// Encode timestamps using an adaptive encoder
tsenc := NewTimeEncoder()
for _, v := range values {
tsenc.Write(time.Unix(0, v.UnixNano()))
venc.Write(v.(*BooleanValue).value)
}
// Encoded timestamp values
tb, err := tsenc.Bytes()
if err != nil {
return nil, err
}
// Encoded float values
vb, err := venc.Bytes()
if err != nil {
return nil, err
}
// Prepend the first timestamp of the block in the first 8 bytes and the block
// in the next byte, followed by the block
block := packBlockHeader(BlockBoolean)
block = append(block, packBlock(tb, vb)...)
return block, nil
}
func DecodeBooleanBlock(block []byte, a []BooleanValue) ([]BooleanValue, error) {
// Block type is the next block, make sure we actually have a float block
blockType := block[0]
if blockType != BlockBoolean {
return nil, fmt.Errorf("invalid block type: exp %d, got %d", BlockBoolean, blockType)
}
block = block[1:]
tb, vb := unpackBlock(block)
// Setup our timestamp and value decoders
dec := NewTimeDecoder(tb)
vdec := NewBooleanDecoder(vb)
// Decode both a timestamp and value
i := 0
for dec.Next() && vdec.Next() {
ts := dec.Read()
v := vdec.Read()
if i < len(a) {
a[i].unixnano = ts.UnixNano()
a[i].value = v
} else {
a = append(a, BooleanValue{ts.UnixNano(), v})
}
i++
}
// Did timestamp decoding have an error?
if dec.Error() != nil {
return nil, dec.Error()
}
// Did boolean decoding have an error?
if vdec.Error() != nil {
return nil, vdec.Error()
}
return a[:i], nil
}
// BooleanValues represents a slice of boolean values.
type BooleanValues []BooleanValue
// Deduplicate returns a new slice with any values that have the same timestamp removed.
// The Value that appears last in the slice is the one that is kept.
func (a BooleanValues) Deduplicate() BooleanValues {
m := make(map[int64]BooleanValue)
for _, val := range a {
m[val.UnixNano()] = val
}
other := make(BooleanValues, 0, len(m))
for _, val := range m {
other = append(other, val)
}
sort.Sort(other)
return other
}
// Sort methods
func (a BooleanValues) Len() int { return len(a) }
func (a BooleanValues) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a BooleanValues) Less(i, j int) bool { return a[i].UnixNano() < a[j].UnixNano() }
type IntegerValue struct {
unixnano int64
value int64
}
func (v *IntegerValue) Value() interface{} {
return v.value
}
func (v *IntegerValue) UnixNano() int64 {
return v.unixnano
}
func (v *IntegerValue) Size() int {
return 16
}
func (f *IntegerValue) String() string {
return fmt.Sprintf("%v %v", time.Unix(0, f.unixnano), f.Value())
}
func encodeIntegerBlock(buf []byte, values []Value) ([]byte, error) {
tsEnc := NewTimeEncoder()
vEnc := NewIntegerEncoder()
for _, v := range values {
tsEnc.Write(time.Unix(0, v.UnixNano()))
vEnc.Write(v.(*IntegerValue).value)
}
// Encoded timestamp values
tb, err := tsEnc.Bytes()
if err != nil {
return nil, err
}
// Encoded int64 values
vb, err := vEnc.Bytes()
if err != nil {
return nil, err
}
// Prepend the first timestamp of the block in the first 8 bytes
block := packBlockHeader(BlockInteger)
return append(block, packBlock(tb, vb)...), nil
}
func DecodeIntegerBlock(block []byte, a []IntegerValue) ([]IntegerValue, error) {
blockType := block[0]
if blockType != BlockInteger {
return nil, fmt.Errorf("invalid block type: exp %d, got %d", BlockInteger, blockType)
}
block = block[1:]
// The first 8 bytes is the minimum timestamp of the block
tb, vb := unpackBlock(block)
// Setup our timestamp and value decoders
tsDec := NewTimeDecoder(tb)
vDec := NewIntegerDecoder(vb)
// Decode both a timestamp and value
i := 0
for tsDec.Next() && vDec.Next() {
ts := tsDec.Read()
v := vDec.Read()
if i < len(a) {
a[i].unixnano = ts.UnixNano()
a[i].value = v
} else {
a = append(a, IntegerValue{ts.UnixNano(), v})
}
i++
}
// Did timestamp decoding have an error?
if tsDec.Error() != nil {
return nil, tsDec.Error()
}
// Did int64 decoding have an error?
if vDec.Error() != nil {
return nil, vDec.Error()
}
return a[:i], nil
}
// IntegerValues represents a slice of integer values.
type IntegerValues []IntegerValue
// Deduplicate returns a new slice with any values that have the same timestamp removed.
// The Value that appears last in the slice is the one that is kept.
func (a IntegerValues) Deduplicate() IntegerValues {
m := make(map[int64]IntegerValue)
for _, val := range a {
m[val.UnixNano()] = val
}
other := make(IntegerValues, 0, len(m))
for _, val := range m {
other = append(other, val)
}
sort.Sort(other)
return other
}
// Sort methods
func (a IntegerValues) Len() int { return len(a) }
func (a IntegerValues) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a IntegerValues) Less(i, j int) bool { return a[i].UnixNano() < a[j].UnixNano() }
type StringValue struct {
unixnano int64
value string
}
func (v *StringValue) Value() interface{} {
return v.value
}
func (v *StringValue) UnixNano() int64 {
return v.unixnano
}
func (v *StringValue) Size() int {
return 8 + len(v.value)
}
func (f *StringValue) String() string {
return fmt.Sprintf("%v %v", time.Unix(0, f.unixnano), f.Value())
}
func encodeStringBlock(buf []byte, values []Value) ([]byte, error) {
tsEnc := NewTimeEncoder()
vEnc := NewStringEncoder()
for _, v := range values {
tsEnc.Write(time.Unix(0, v.UnixNano()))
vEnc.Write(v.(*StringValue).value)
}
// Encoded timestamp values
tb, err := tsEnc.Bytes()
if err != nil {
return nil, err
}
// Encoded string values
vb, err := vEnc.Bytes()
if err != nil {
return nil, err
}
// Prepend the first timestamp of the block in the first 8 bytes
block := packBlockHeader(BlockString)
return append(block, packBlock(tb, vb)...), nil
}
func DecodeStringBlock(block []byte, a []StringValue) ([]StringValue, error) {
blockType := block[0]
if blockType != BlockString {
return nil, fmt.Errorf("invalid block type: exp %d, got %d", BlockString, blockType)
}
block = block[1:]
// The first 8 bytes is the minimum timestamp of the block
tb, vb := unpackBlock(block)
// Setup our timestamp and value decoders
tsDec := NewTimeDecoder(tb)
vDec, err := NewStringDecoder(vb)
if err != nil {
return nil, err
}
// Decode both a timestamp and value
i := 0
for tsDec.Next() && vDec.Next() {
ts := tsDec.Read()
v := vDec.Read()
if i < len(a) {
a[i].unixnano = ts.UnixNano()
a[i].value = v
} else {
a = append(a, StringValue{ts.UnixNano(), v})
}
i++
}
// Did timestamp decoding have an error?
if tsDec.Error() != nil {
return nil, tsDec.Error()
}
// Did string decoding have an error?
if vDec.Error() != nil {
return nil, vDec.Error()
}
return a[:i], nil
}
// StringValues represents a slice of string values.
type StringValues []StringValue
// Deduplicate returns a new slice with any values that have the same timestamp removed.
// The Value that appears last in the slice is the one that is kept.
func (a StringValues) Deduplicate() StringValues {
m := make(map[int64]StringValue)
for _, val := range a {
m[val.UnixNano()] = val
}
other := make(StringValues, 0, len(m))
for _, val := range m {
other = append(other, val)
}
sort.Sort(other)
return other
}
// Sort methods
func (a StringValues) Len() int { return len(a) }
func (a StringValues) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a StringValues) Less(i, j int) bool { return a[i].UnixNano() < a[j].UnixNano() }
func packBlockHeader(blockType byte) []byte {
return []byte{blockType}
}
func packBlock(ts []byte, values []byte) []byte {
// We encode the length of the timestamp block using a variable byte encoding.
// This allows small byte slices to take up 1 byte while larger ones use 2 or more.
b := make([]byte, 10)
i := binary.PutUvarint(b, uint64(len(ts)))
// block is <len timestamp bytes>, <ts bytes>, <value bytes>
block := append(b[:i], ts...)
// We don't encode the value length because we know it's the rest of the block after
// the timestamp block.
return append(block, values...)
}
func unpackBlock(buf []byte) (ts, values []byte) {
// Unpack the timestamp block length
tsLen, i := binary.Uvarint(buf)
// Unpack the timestamp bytes
ts = buf[int(i) : int(i)+int(tsLen)]
// Unpack the value bytes
values = buf[int(i)+int(tsLen):]
return
}
// ZigZagEncode converts a int64 to a uint64 by zig zagging negative and positive values
// across even and odd numbers. Eg. [0,-1,1,-2] becomes [0, 1, 2, 3]
func ZigZagEncode(x int64) uint64 {
return uint64(uint64(x<<1) ^ uint64((int64(x) >> 63)))
}
// ZigZagDecode converts a previously zigzag encoded uint64 back to a int64
func ZigZagDecode(v uint64) int64 {
return int64((v >> 1) ^ uint64((int64(v&1)<<63)>>63))
}