-
Notifications
You must be signed in to change notification settings - Fork 83
/
bytes.go
300 lines (269 loc) · 8.09 KB
/
bytes.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
// Licensed to Apache Software Foundation (ASF) under one or more contributor
// license agreements. See the NOTICE file distributed with
// this work for additional information regarding copyright
// ownership. Apache Software Foundation (ASF) licenses this file to you 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"
"github.com/apache/skywalking-banyandb/pkg/bytes"
"github.com/apache/skywalking-banyandb/pkg/compress/zstd"
)
// EncodeBytes encodes a string into dst.
func EncodeBytes(dst, b []byte) []byte {
dst = VarUint64ToBytes(dst, uint64(len(b)))
dst = append(dst, b...)
return dst
}
// DecodeBytes decodes a string from src.
func DecodeBytes(src []byte) ([]byte, []byte, error) {
tail, n, err := BytesToVarUint64(src)
if err != nil {
return nil, nil, fmt.Errorf("cannot decode string size: %w", err)
}
src = tail
if uint64(len(src)) < n {
return nil, nil, fmt.Errorf("src is too short for reading string with size %d; len(src)=%d", n, len(src))
}
return src[n:], src[:n], nil
}
// EncodeBytesBlock encodes a block of strings into dst.
func EncodeBytesBlock(dst []byte, a [][]byte) []byte {
u64s := GenerateUint64List(len(a))
aLens := u64s.L[:0]
for _, s := range a {
aLens = append(aLens, uint64(len(s)))
}
u64s.L = aLens
dst = encodeUint64Block(dst, u64s.L)
ReleaseUint64List(u64s)
bb := bbPool.Generate()
b := bb.Buf
for _, s := range a {
b = append(b, s...)
}
bb.Buf = b
dst = compressBlock(dst, bb.Buf)
bbPool.Release(bb)
return dst
}
// BytesBlockDecoder decodes a block of strings from src.
// It reuses the underlying buffer for storing the decoded strings.
type BytesBlockDecoder struct {
data []byte
}
// Reset resets the decoder.
func (bbd *BytesBlockDecoder) Reset() {
bbd.data = bbd.data[:0]
}
// Decode decodes a block of strings from src.
func (bbd *BytesBlockDecoder) Decode(dst [][]byte, src []byte, itemsCount uint64) ([][]byte, error) {
u64List := GenerateUint64List(0)
defer ReleaseUint64List(u64List)
var tail []byte
var err error
u64List.L, tail, err = decodeUint64Block(u64List.L[:0], src, itemsCount)
if err != nil {
return dst, fmt.Errorf("cannot decode string lengths: %w", err)
}
aLens := u64List.L
src = tail
dataLen := len(bbd.data)
bbd.data, tail, err = decompressBlock(bbd.data, src)
if err != nil {
return dst, fmt.Errorf("cannot decode bytes block with strings: %w", err)
}
if len(tail) > 0 {
return dst, fmt.Errorf("unexpected non-empty tail after reading bytes block with strings; len(tail)=%d", len(tail))
}
data := bbd.data[dataLen:]
for _, sLen := range aLens {
if uint64(len(data)) < sLen {
return dst, fmt.Errorf("cannot decode a string with the length %d bytes from %d bytes", sLen, len(data))
}
if sLen == 0 {
dst = append(dst, nil)
continue
}
dst = append(dst, data[:sLen])
data = data[sLen:]
}
return dst, nil
}
func encodeUint64Block(dst []byte, a []uint64) []byte {
bb := bbPool.Generate()
bb.Buf = encodeUint64List(bb.Buf[:0], a)
dst = compressBlock(dst, bb.Buf)
bbPool.Release(bb)
return dst
}
func decodeUint64Block(dst []uint64, src []byte, itemsCount uint64) ([]uint64, []byte, error) {
bb := bbPool.Generate()
defer bbPool.Release(bb)
var err error
bb.Buf, src, err = decompressBlock(bb.Buf[:0], src)
if err != nil {
return dst, src, fmt.Errorf("cannot decode bytes block: %w", err)
}
dst, err = decodeUint64List(dst, bb.Buf, itemsCount)
if err != nil {
return dst, src, fmt.Errorf("cannot decode %d uint64 items from bytes block of length %d bytes: %w", itemsCount, len(bb.Buf), err)
}
return dst, src, nil
}
const (
uintBlockType8 = 0
uintBlockType16 = 1
uintBlockType32 = 2
uintBlockType64 = 3
)
func encodeUint64List(dst []byte, a []uint64) []byte {
nMax := uint64(0)
for _, n := range a {
if n > nMax {
nMax = n
}
}
switch {
case nMax < (1 << 8):
dst = append(dst, uintBlockType8)
for _, n := range a {
dst = append(dst, byte(n))
}
case nMax < (1 << 16):
dst = append(dst, uintBlockType16)
for _, n := range a {
dst = Uint16ToBytes(dst, uint16(n))
}
case nMax < (1 << 32):
dst = append(dst, uintBlockType32)
for _, n := range a {
dst = Uint32ToBytes(dst, uint32(n))
}
default:
dst = append(dst, uintBlockType64)
for _, n := range a {
dst = Uint64ToBytes(dst, n)
}
}
return dst
}
func decodeUint64List(dst []uint64, src []byte, itemsCount uint64) ([]uint64, error) {
if len(src) < 1 {
return dst, fmt.Errorf("cannot decode uint64 block type from empty src")
}
blockType := src[0]
src = src[1:]
switch blockType {
case uintBlockType8:
if uint64(len(src)) != itemsCount {
return dst, fmt.Errorf("unexpected block length for %d items; got %d bytes; want %d bytes", itemsCount, len(src), itemsCount)
}
for _, v := range src {
dst = append(dst, uint64(v))
}
case uintBlockType16:
if uint64(len(src)) != 2*itemsCount {
return dst, fmt.Errorf("unexpected block length for %d items; got %d bytes; want %d bytes", itemsCount, len(src), 2*itemsCount)
}
for len(src) > 0 {
v := BytesToUint16(src)
src = src[2:]
dst = append(dst, uint64(v))
}
case uintBlockType32:
if uint64(len(src)) != 4*itemsCount {
return dst, fmt.Errorf("unexpected block length for %d items; got %d bytes; want %d bytes", itemsCount, len(src), 4*itemsCount)
}
for len(src) > 0 {
v := BytesToUint32(src)
src = src[4:]
dst = append(dst, uint64(v))
}
case uintBlockType64:
if uint64(len(src)) != 8*itemsCount {
return dst, fmt.Errorf("unexpected block length for %d items; got %d bytes; want %d bytes", itemsCount, len(src), 8*itemsCount)
}
for len(src) > 0 {
v := BytesToUint64(src)
src = src[8:]
dst = append(dst, v)
}
default:
return dst, fmt.Errorf("unexpected uint64 block type: %d; want 0, 1, 2 or 3", blockType)
}
return dst, nil
}
const (
compressTypePlain = 0
compressTypeZSTD = 1
)
func compressBlock(dst, src []byte) []byte {
if len(src) < 128 {
dst = append(dst, compressTypePlain, byte(len(src)))
return append(dst, src...)
}
dst = append(dst, compressTypeZSTD)
bb := bbPool.Generate()
bb.Buf = zstd.Compress(bb.Buf[:0], src, 1)
dst = VarUint64ToBytes(dst, uint64(len(bb.Buf)))
dst = append(dst, bb.Buf...)
bbPool.Release(bb)
return dst
}
func decompressBlock(dst, src []byte) ([]byte, []byte, error) {
if len(src) < 1 {
return dst, src, fmt.Errorf("cannot decode block type from empty src")
}
blockType := src[0]
src = src[1:]
switch blockType {
case compressTypePlain:
if len(src) < 1 {
return dst, src, fmt.Errorf("cannot decode plain block size from empty src")
}
blockLen := int(src[0])
src = src[1:]
if len(src) < blockLen {
return dst, src, fmt.Errorf("cannot read plain block with the size %d bytes from %b bytes", blockLen, len(src))
}
dst = append(dst, src[:blockLen]...)
src = src[blockLen:]
return dst, src, nil
case compressTypeZSTD:
tail, blockLen, err := BytesToVarUint64(src)
if err != nil {
return dst, src, fmt.Errorf("cannot decode compressed block size: %w", err)
}
src = tail
if uint64(len(src)) < blockLen {
return dst, src, fmt.Errorf("cannot read compressed block with the size %d bytes from %d bytes", blockLen, len(src))
}
compressedBlock := src[:blockLen]
src = src[blockLen:]
// Decompress the block
bb := bbPool.Generate()
bb.Buf, err = zstd.Decompress(bb.Buf[:0], compressedBlock)
if err != nil {
return dst, src, fmt.Errorf("cannot decompress block: %w", err)
}
// Copy the decompressed block to dst.
dst = append(dst, bb.Buf...)
bbPool.Release(bb)
return dst, src, nil
default:
return dst, src, fmt.Errorf("unexpected block type: %d; supported types: 0, 1", blockType)
}
}
var bbPool bytes.BufferPool