-
Notifications
You must be signed in to change notification settings - Fork 141
/
float.go
183 lines (152 loc) · 3.87 KB
/
float.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
/*
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 (
"math"
"github.com/openGemini/openGemini/lib/compress"
"github.com/openGemini/openGemini/lib/numberenc"
"github.com/openGemini/openGemini/lib/util"
"github.com/openGemini/openGemini/lib/util/lifted/go-bitstream"
)
const (
// floatCompressedGorilla is a compressed format using the gorilla paper encoding
floatCompressedGorilla = 1
)
type Float struct {
encodingType int
leading uint8
trailing uint8
buf *BytesBuffer
br *bitstream.BitReader
float *compress.Float
}
func NewFloat() *Float {
return &Float{float: compress.NewFloat()}
}
func (enc *Float) SetEncodingType(ty int) {
enc.encodingType = ty
}
func (enc *Float) Encoding(in []byte, out []byte) ([]byte, error) {
if len(in) == 0 {
return out, nil
}
pos := len(out)
buf, err := enc.float.AdaptiveEncoding(in, out[pos:])
if err != nil {
return nil, err
}
if pos == 0 {
return buf, nil
}
out = append(out, buf...)
return out, err
}
func (enc *Float) Decoding(in []byte, out []byte) ([]byte, error) {
enc.encodingType = int(in[0] >> 4)
if enc.encodingType == floatCompressedGorilla {
// compatible with old data files
// will be deleted in later versions
enc.buf = NewBytesBuffer(nil)
return enc.decoding(in[1:], out)
}
pos := len(out)
buf, err := enc.float.AdaptiveDecoding(in, out[pos:])
if err != nil {
return nil, err
}
if pos == 0 {
return buf, nil
}
out = append(out, buf...)
return out, err
}
func (enc *Float) gorillaDecoding(out []float64) error {
v, err := enc.br.ReadBits(64)
if err != nil {
return err
}
out[0] = math.Float64frombits(v)
idx := 1
for {
bit, err := enc.br.ReadBit()
if err != nil {
return err
}
if bit == bitstream.Zero {
out[idx] = out[idx-1]
idx++
} else {
bit, itErr := enc.br.ReadBit()
if itErr != nil {
return err
}
if bit == bitstream.Zero {
} else {
rBits, err := enc.br.ReadBits(5)
if err != nil {
return err
}
enc.leading = uint8(rBits)
rBits, err = enc.br.ReadBits(6)
if err != nil {
return err
}
mbits := uint8(rBits)
// 0 significant bits here means we overflowed and we actually need 64;
if mbits == 0 {
mbits = 64
}
enc.trailing = 64 - enc.leading - mbits
}
mbits := int(64 - enc.leading - enc.trailing)
rBits, err := enc.br.ReadBits(mbits)
if err != nil {
return err
}
vbits := math.Float64bits(out[idx-1])
vbits ^= rBits << enc.trailing
vv := math.Float64frombits(vbits)
if math.IsNaN(vv) {
break
}
out[idx] = vv
idx++
}
}
return nil
}
// Deprecated: this function has function and performance problems.
// It is reserved for compatibility and will be deleted in the next version.
func (enc *Float) decoding(in []byte, out []byte) ([]byte, error) {
count := int(numberenc.UnmarshalUint32(in))
enc.buf.Reset(in[4:])
if enc.br == nil {
enc.br = bitstream.NewReader(enc.buf)
} else {
enc.br.Reset(enc.buf)
}
origLen := len(out)
n := util.Float64SizeBytes * count
if cap(out) < n+origLen {
out = out[:cap(out)]
delta := n + origLen - cap(out)
out = append(out, make([]byte, delta)...)
}
out = out[:origLen+n]
outValues := util.Bytes2Float64Slice(out[origLen:])
if err := enc.gorillaDecoding(outValues); err != nil {
return nil, err
}
return out, nil
}
var _ DataCoder = (*Float)(nil)