forked from influxdata/influxdb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
batch_integer.go
170 lines (137 loc) · 3.61 KB
/
batch_integer.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
package tsm1
import (
"encoding/binary"
"fmt"
"unsafe"
"github.com/influxdata/influxdb/pkg/encoding/simple8b"
)
var (
integerBatchDecoderFunc = [...]func(b []byte, dst []int64) ([]int64, error){
integerBatchDecodeAllUncompressed,
integerBatchDecodeAllSimple,
integerBatchDecodeAllRLE,
integerBatchDecodeAllInvalid,
}
)
func IntegerBatchDecodeAll(b []byte, dst []int64) ([]int64, error) {
if len(b) == 0 {
return []int64{}, nil
}
encoding := b[0] >> 4
if encoding > intCompressedRLE {
encoding = 3 // integerBatchDecodeAllInvalid
}
return integerBatchDecoderFunc[encoding&3](b, dst)
}
func UnsignedBatchDecodeAll(b []byte, dst []uint64) ([]uint64, error) {
if len(b) == 0 {
return []uint64{}, nil
}
encoding := b[0] >> 4
if encoding > intCompressedRLE {
encoding = 3 // integerBatchDecodeAllInvalid
}
res, err := integerBatchDecoderFunc[encoding&3](b, reintepretUint64ToInt64Slice(dst))
return reintepretInt64ToUint64Slice(res), err
}
func integerBatchDecodeAllUncompressed(b []byte, dst []int64) ([]int64, error) {
b = b[1:]
if len(b)&0x7 != 0 {
return []int64{}, fmt.Errorf("IntegerBatchDecodeAll: expected multiple of 8 bytes")
}
count := len(b) / 8
if cap(dst) < count {
dst = make([]int64, count)
} else {
dst = dst[:count]
}
prev := int64(0)
for i := range dst {
prev += ZigZagDecode(binary.BigEndian.Uint64(b[i*8:]))
dst[i] = prev
}
return dst, nil
}
func integerBatchDecodeAllSimple(b []byte, dst []int64) ([]int64, error) {
b = b[1:]
if len(b) < 8 {
return []int64{}, fmt.Errorf("IntegerBatchDecodeAll: not enough data to decode packed value")
}
count, err := simple8b.CountBytes(b[8:])
if err != nil {
return []int64{}, err
}
count += 1
if cap(dst) < count {
dst = make([]int64, count)
} else {
dst = dst[:count]
}
// first value
dst[0] = ZigZagDecode(binary.BigEndian.Uint64(b))
// decode compressed values
buf := reintepretInt64ToUint64Slice(dst)
n, err := simple8b.DecodeBytesBigEndian(buf[1:], b[8:])
if err != nil {
return []int64{}, err
}
if n != count-1 {
return []int64{}, fmt.Errorf("IntegerBatchDecodeAll: unexpected number of values decoded; got=%d, exp=%d", n, count-1)
}
// calculate prefix sum
prev := dst[0]
for i := 1; i < len(dst); i++ {
prev += ZigZagDecode(uint64(dst[i]))
dst[i] = prev
}
return dst, nil
}
func integerBatchDecodeAllRLE(b []byte, dst []int64) ([]int64, error) {
b = b[1:]
if len(b) < 8 {
return []int64{}, fmt.Errorf("IntegerBatchDecodeAll: not enough data to decode RLE starting value")
}
var k, n int
// Next 8 bytes is the starting value
first := ZigZagDecode(binary.BigEndian.Uint64(b[k : k+8]))
k += 8
// Next 1-10 bytes is the delta value
value, n := binary.Uvarint(b[k:])
if n <= 0 {
return []int64{}, fmt.Errorf("IntegerBatchDecodeAll: invalid RLE delta value")
}
k += n
delta := ZigZagDecode(value)
// Last 1-10 bytes is how many times the value repeats
count, n := binary.Uvarint(b[k:])
if n <= 0 {
return []int64{}, fmt.Errorf("IntegerBatchDecodeAll: invalid RLE repeat value")
}
count += 1
if cap(dst) < int(count) {
dst = make([]int64, count)
} else {
dst = dst[:count]
}
if delta == 0 {
for i := range dst {
dst[i] = first
}
} else {
acc := first
for i := range dst {
dst[i] = acc
acc += delta
}
}
return dst, nil
}
func integerBatchDecodeAllInvalid(b []byte, _ []int64) ([]int64, error) {
return []int64{}, fmt.Errorf("unknown encoding %v", b[0]>>4)
}
func reintepretInt64ToUint64Slice(src []int64) []uint64 {
return *(*[]uint64)(unsafe.Pointer(&src))
}
func reintepretUint64ToInt64Slice(src []uint64) []int64 {
return *(*[]int64)(unsafe.Pointer(&src))
}