-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathencode.go
269 lines (232 loc) · 6.68 KB
/
encode.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
package fpc
import (
"encoding/binary"
"io"
"math"
)
const (
maxRecordsPerBlock = 32768
blockHeaderSize = 6 // in bytes
)
var byteOrder = binary.LittleEndian
// pairHeader combines the headers for two values into a single byte
type pairHeader struct {
h1 header
h2 header
}
func (ph pairHeader) encode() byte {
return (ph.h1.encode()<<4 | ph.h2.encode())
}
// header is a cotainer for the count of the number of non-zero bytes in an
// encoded value, and the type of predictor used to generate the encoded value
type header struct {
len uint8
pType predictorClass
}
// the top bit is the predictor type bit. Bottom 3 bits encode the number of
// leading zero bytes for the value.
func (h header) encode() byte {
if h.len > 4 {
return byte(h.pType)<<3 | byte(h.len-1)
} else {
return byte(h.pType)<<3 | byte(h.len)
}
}
type blockEncoder struct {
blockSize int // size of blocks in bytes
headers []byte
values []byte
w io.Writer // Destination for encoded bytes
enc *encoder // Underlying machinery for encoding pairs of floats
// Mutable state below
last uint64 // last value received to encode
nRecords int // Count of float64s received in this block
nBytes int // Count of bytes in this block
}
// type block struct {
// header []byte
// }
func newBlockEncoder(w io.Writer, compression uint) *blockEncoder {
return &blockEncoder{
headers: make([]byte, 0, maxRecordsPerBlock),
values: make([]byte, 0, maxRecordsPerBlock*8),
w: w,
enc: newEncoder(compression),
last: 0,
nRecords: 0,
}
}
func (b *blockEncoder) encode(v uint64) error {
// Encode values in pairs
if b.nRecords%2 == 0 {
b.last = v
b.nRecords += 1
return nil
}
header, data := b.enc.encode(b.last, v)
nBytes := 1 + len(data) // 1 for header
// Append data to the block
b.headers = append(b.headers, header.encode())
b.values = append(b.values, data...)
b.nRecords += 1
b.nBytes += nBytes
// Flush if we need to
if b.nRecords == maxRecordsPerBlock {
if err := b.flush(); err != nil {
return err
}
}
return nil
}
func (b *blockEncoder) encodeFloat(f float64) error {
return b.encode(math.Float64bits(f))
}
func (b *blockEncoder) flush() error {
if b.nRecords == 0 {
return nil
}
if b.nRecords%2 == 1 {
// There's an extra record waiting for a partner. Add a dummy value by
// encoding a zero and adding it to data.
h, data := b.enc.encode(b.last, 0)
// Truncate out the dummy value's data. The header remains, but it
// won't do any harm.
data = data[:h.h1.len]
b.headers = append(b.headers, h.encode())
b.values = append(b.values, data...)
}
block := b.encodeBlock()
// Write data out
n, err := b.w.Write(block)
if err != nil {
return err
}
if n < len(block) {
return io.ErrShortWrite
}
// Reset buffer and counters
b.headers = make([]byte, 0, maxRecordsPerBlock)
b.values = make([]byte, 0, maxRecordsPerBlock)
b.nRecords = 0
b.nBytes = 0
return nil
}
func (b *blockEncoder) encodeBlock() []byte {
// The block header is layed out as two little-endian 24-bit unsigned
// integers. The first integer is the number of records in the block, and
// the second is the number of bytes.
nByte := len(b.headers) + len(b.values) + blockHeaderSize
block := make([]byte, 6, nByte)
//First three bytes are the number of records in the block.
block[0] = byte(b.nRecords)
block[1] = byte(b.nRecords >> 8)
block[2] = byte(b.nRecords >> 16)
// Next three bytes are the number of bytes in the block.
block[3] = byte(nByte)
block[4] = byte(nByte >> 8)
block[5] = byte(nByte >> 16)
// Record headers follow the block header
block = append(block, b.headers...)
// After the header is all the rest of the data.
block = append(block, b.values...)
return block
}
type encoder struct {
buf []byte
// predictors
fcm predictor
dfcm predictor
}
func newEncoder(compression uint) *encoder {
tableSize := uint(1 << compression)
return &encoder{
buf: make([]byte, 17),
fcm: newFCM(tableSize),
dfcm: newDFCM(tableSize),
}
}
// compute the difference between v and the best predicted value; return that
// difference and which predictor was the most effective. Updates predictors as
// a side effect.
func (e *encoder) computeDiff(v uint64) (d uint64, h header) {
fcmDelta := e.fcm.predict() ^ v
e.fcm.update(v)
dfcmDelta := e.dfcm.predict() ^ v
e.dfcm.update(v)
if fcmDelta <= dfcmDelta {
d = fcmDelta
h.pType = fcmPredictor
} else {
d = dfcmDelta
h.pType = dfcmPredictor
}
h.len = uint8(8 - clzBytes(d))
// "Since there can be between zero and eight leading zero bytes, i.e.,
// nine possibilities, not all of them can be encoded with a three-bit
// value. We decided not to support a leading zero count of four because
// it occurs only rarely (cf. Section 5.4). Consequently, all xor results
// with four leading zero bytes are treated like values with only three
// leading zero bytes and the fourth zero byte is emitted as part of the
// residual."
//
// Here we add 1, to include one of the leading 0s in the residual.
if h.len == 4 {
h.len += 1
}
return d, h
}
// encode a pair of values
func (e *encoder) encode(v1, v2 uint64) (h pairHeader, data []byte) {
d1, h1 := e.computeDiff(v1)
d2, h2 := e.computeDiff(v2)
h = pairHeader{h1, h2}
e.encodeNonzero(d1, h1.len, e.buf[:h1.len])
e.encodeNonzero(d2, h2.len, e.buf[h1.len:h1.len+h2.len])
return h, e.buf[:h1.len+h2.len]
}
func (e *encoder) encodeNonzero(v uint64, n uint8, into []byte) {
// Starting with the first nonzero byte, copy v's data into the byte slice.
//
// Unrolling this loop into a switch speeds up the computation dramatically.
switch n {
case 8:
into[0] = byte(v & 0xFF)
into[1] = byte((v >> 8) & 0xFF)
into[2] = byte((v >> 16) & 0xFF)
into[3] = byte((v >> 24) & 0xFF)
into[4] = byte((v >> 32) & 0xFF)
into[5] = byte((v >> 40) & 0xFF)
into[6] = byte((v >> 48) & 0xFF)
into[7] = byte((v >> 56) & 0xFF)
case 7:
into[0] = byte(v & 0xFF)
into[1] = byte((v >> 8) & 0xFF)
into[2] = byte((v >> 16) & 0xFF)
into[3] = byte((v >> 24) & 0xFF)
into[4] = byte((v >> 32) & 0xFF)
into[5] = byte((v >> 40) & 0xFF)
into[6] = byte((v >> 48) & 0xFF)
case 6:
into[0] = byte(v & 0xFF)
into[1] = byte((v >> 8) & 0xFF)
into[2] = byte((v >> 16) & 0xFF)
into[3] = byte((v >> 24) & 0xFF)
into[4] = byte((v >> 32) & 0xFF)
into[5] = byte((v >> 40) & 0xFF)
case 5:
into[0] = byte(v & 0xFF)
into[1] = byte((v >> 8) & 0xFF)
into[2] = byte((v >> 16) & 0xFF)
into[3] = byte((v >> 24) & 0xFF)
into[4] = byte((v >> 32) & 0xFF)
case 3:
into[0] = byte(v & 0xFF)
into[1] = byte((v >> 8) & 0xFF)
into[2] = byte((v >> 16) & 0xFF)
case 2:
into[0] = byte(v & 0xFF)
into[1] = byte((v >> 8) & 0xFF)
case 1:
into[0] = byte(v & 0xFF)
}
}