-
Notifications
You must be signed in to change notification settings - Fork 457
/
Copy pathencoder.go
347 lines (300 loc) · 10.3 KB
/
encoder.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
// Copyright (c) 2016 Uber Technologies, Inc
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE
package msgpack
import (
"bytes"
"github.com/m3db/m3/src/dbnode/persist/schema"
"gopkg.in/vmihailenco/msgpack.v2"
)
type encodeVersionFn func(value int)
type encodeNumObjectFieldsForFn func(value objectType)
type encodeVarintFn func(value int64)
type encodeVarUintFn func(value uint64)
type encodeFloat64Fn func(value float64)
type encodeBytesFn func(value []byte)
type encodeArrayLenFn func(value int)
// Encoder encodes data in msgpack format for persistence.
type Encoder struct {
buf *bytes.Buffer
enc *msgpack.Encoder
err error
encodeVersionFn encodeVersionFn
encodeNumObjectFieldsForFn encodeNumObjectFieldsForFn
encodeVarintFn encodeVarintFn
encodeVarUintFn encodeVarUintFn
encodeFloat64Fn encodeFloat64Fn
encodeBytesFn encodeBytesFn
encodeArrayLenFn encodeArrayLenFn
legacy legacyEncodingOptions
}
type legacyEncodingIndexInfoVersion int
const (
// List in reverse order to ensure default value is current version.
legacyEncodingIndexVersionCurrent legacyEncodingIndexInfoVersion = iota
legacyEncodingIndexVersionV2
legacyEncodingIndexVersionV1
)
type legacyEncodingOptions struct {
encodeLegacyIndexInfoVersion legacyEncodingIndexInfoVersion
decodeLegacyIndexInfoVersion legacyEncodingIndexInfoVersion
encodeLegacyV1IndexEntry bool
decodeLegacyV1IndexEntry bool
}
var defaultlegacyEncodingOptions = legacyEncodingOptions{
encodeLegacyIndexInfoVersion: legacyEncodingIndexVersionCurrent,
decodeLegacyIndexInfoVersion: legacyEncodingIndexVersionCurrent,
encodeLegacyV1IndexEntry: false,
decodeLegacyV1IndexEntry: false,
}
// NewEncoder creates a new encoder.
func NewEncoder() *Encoder {
return newEncoder(defaultlegacyEncodingOptions)
}
func newEncoder(legacy legacyEncodingOptions) *Encoder {
buf := bytes.NewBuffer(nil)
enc := &Encoder{
buf: buf,
enc: msgpack.NewEncoder(buf),
}
enc.encodeVersionFn = enc.encodeVersion
enc.encodeNumObjectFieldsForFn = enc.encodeNumObjectFieldsFor
enc.encodeVarintFn = enc.encodeVarint
enc.encodeVarUintFn = enc.encodeVarUint
enc.encodeFloat64Fn = enc.encodeFloat64
enc.encodeBytesFn = enc.encodeBytes
enc.encodeArrayLenFn = enc.encodeArrayLen
// Used primarily for testing.
enc.legacy = legacy
return enc
}
// Reset resets the buffer.
func (enc *Encoder) Reset() {
enc.buf.Truncate(0)
enc.err = nil
}
// Bytes returns the encoded bytes.
func (enc *Encoder) Bytes() []byte { return enc.buf.Bytes() }
// EncodeIndexInfo encodes index info.
func (enc *Encoder) EncodeIndexInfo(info schema.IndexInfo) error {
if enc.err != nil {
return enc.err
}
enc.encodeRootObject(indexInfoVersion, indexInfoType)
if enc.legacy.encodeLegacyIndexInfoVersion == legacyEncodingIndexVersionV1 {
enc.encodeIndexInfoV1(info)
} else if enc.legacy.encodeLegacyIndexInfoVersion == legacyEncodingIndexVersionV2 {
enc.encodeIndexInfoV2(info)
} else {
enc.encodeIndexInfoV3(info)
}
return enc.err
}
// EncodeIndexEntry encodes index entry.
func (enc *Encoder) EncodeIndexEntry(entry schema.IndexEntry) error {
if enc.err != nil {
return enc.err
}
enc.encodeRootObject(indexEntryVersion, indexEntryType)
if enc.legacy.encodeLegacyV1IndexEntry {
enc.encodeIndexEntryV1(entry)
} else {
enc.encodeIndexEntryV2(entry)
}
return enc.err
}
// EncodeIndexSummary encodes index summary.
func (enc *Encoder) EncodeIndexSummary(summary schema.IndexSummary) error {
if enc.err != nil {
return enc.err
}
enc.encodeRootObject(indexSummaryVersion, indexSummaryType)
enc.encodeIndexSummary(summary)
return enc.err
}
// EncodeLogInfo encodes commit log info.
func (enc *Encoder) EncodeLogInfo(info schema.LogInfo) error {
if enc.err != nil {
return enc.err
}
enc.encodeRootObject(logInfoVersion, logInfoType)
enc.encodeLogInfo(info)
return enc.err
}
// EncodeLogEntry encodes commit log entry.
func (enc *Encoder) EncodeLogEntry(entry schema.LogEntry) error {
if enc.err != nil {
return enc.err
}
enc.encodeRootObject(logEntryVersion, logEntryType)
enc.encodeLogEntry(entry)
return enc.err
}
// EncodeLogMetadata encodes commit log metadata
func (enc *Encoder) EncodeLogMetadata(entry schema.LogMetadata) error {
if enc.err != nil {
return enc.err
}
enc.encodeRootObject(logMetadataVersion, logMetadataType)
enc.encodeLogMetadata(entry)
return enc.err
}
// We only keep this method around for the sake of testing
// backwards-compatbility.
func (enc *Encoder) encodeIndexInfoV1(info schema.IndexInfo) {
// Manually encode num fields for testing purposes.
enc.encodeArrayLenFn(6) // V1 had 6 fields.
enc.encodeVarintFn(info.BlockStart)
enc.encodeVarintFn(info.BlockSize)
enc.encodeVarintFn(info.Entries)
enc.encodeVarintFn(info.MajorVersion)
enc.encodeIndexSummariesInfo(info.Summaries)
enc.encodeIndexBloomFilterInfo(info.BloomFilter)
}
// We only keep this method around for the sake of testing
// backwards-compatbility.
func (enc *Encoder) encodeIndexInfoV2(info schema.IndexInfo) {
// Manually encode num fields for testing purposes.
enc.encodeNumObjectFieldsForFn(8) // V2 had 8 fields.
enc.encodeVarintFn(info.BlockStart)
enc.encodeVarintFn(info.BlockSize)
enc.encodeVarintFn(info.Entries)
enc.encodeVarintFn(info.MajorVersion)
enc.encodeIndexSummariesInfo(info.Summaries)
enc.encodeIndexBloomFilterInfo(info.BloomFilter)
enc.encodeVarintFn(info.SnapshotTime)
enc.encodeVarintFn(int64(info.FileType))
}
func (enc *Encoder) encodeIndexInfoV3(info schema.IndexInfo) {
enc.encodeNumObjectFieldsForFn(indexInfoType)
enc.encodeVarintFn(info.BlockStart)
enc.encodeVarintFn(info.BlockSize)
enc.encodeVarintFn(info.Entries)
enc.encodeVarintFn(info.MajorVersion)
enc.encodeIndexSummariesInfo(info.Summaries)
enc.encodeIndexBloomFilterInfo(info.BloomFilter)
enc.encodeVarintFn(info.SnapshotTime)
enc.encodeVarintFn(int64(info.FileType))
enc.encodeBytesFn(info.SnapshotID)
}
func (enc *Encoder) encodeIndexSummariesInfo(info schema.IndexSummariesInfo) {
enc.encodeNumObjectFieldsForFn(indexSummariesInfoType)
enc.encodeVarintFn(info.Summaries)
}
func (enc *Encoder) encodeIndexBloomFilterInfo(info schema.IndexBloomFilterInfo) {
enc.encodeNumObjectFieldsForFn(indexBloomFilterInfoType)
enc.encodeVarintFn(info.NumElementsM)
enc.encodeVarintFn(info.NumHashesK)
}
// We only keep this method around for the sake of testing
// backwards-compatbility.
func (enc *Encoder) encodeIndexEntryV1(entry schema.IndexEntry) {
// Manually encode num fields for testing purposes.
enc.encodeArrayLenFn(5) // V1 had 5 fields.
enc.encodeVarintFn(entry.Index)
enc.encodeBytesFn(entry.ID)
enc.encodeVarintFn(entry.Size)
enc.encodeVarintFn(entry.Offset)
enc.encodeVarintFn(entry.Checksum)
}
func (enc *Encoder) encodeIndexEntryV2(entry schema.IndexEntry) {
enc.encodeNumObjectFieldsForFn(indexEntryType)
enc.encodeVarintFn(entry.Index)
enc.encodeBytesFn(entry.ID)
enc.encodeVarintFn(entry.Size)
enc.encodeVarintFn(entry.Offset)
enc.encodeVarintFn(entry.Checksum)
enc.encodeBytesFn(entry.EncodedTags)
}
func (enc *Encoder) encodeIndexSummary(summary schema.IndexSummary) {
enc.encodeNumObjectFieldsForFn(indexSummaryType)
enc.encodeVarintFn(summary.Index)
enc.encodeBytesFn(summary.ID)
enc.encodeVarintFn(summary.IndexEntryOffset)
}
func (enc *Encoder) encodeLogInfo(info schema.LogInfo) {
enc.encodeNumObjectFieldsForFn(logInfoType)
enc.encodeVarintFn(info.Start)
enc.encodeVarintFn(info.Duration)
enc.encodeVarintFn(info.Index)
}
func (enc *Encoder) encodeLogEntry(entry schema.LogEntry) {
enc.encodeNumObjectFieldsForFn(logEntryType)
// Encode the index first because the commitlog reader needs this information first
// to distribute the rest of the decoding to a group of workers.
enc.encodeVarUintFn(entry.Index)
enc.encodeVarintFn(entry.Create)
enc.encodeBytesFn(entry.Metadata)
enc.encodeVarintFn(entry.Timestamp)
enc.encodeFloat64Fn(entry.Value)
enc.encodeVarUintFn(uint64(entry.Unit))
enc.encodeBytesFn(entry.Annotation)
}
func (enc *Encoder) encodeLogMetadata(metadata schema.LogMetadata) {
enc.encodeNumObjectFieldsForFn(logMetadataType)
enc.encodeBytesFn(metadata.ID)
enc.encodeBytesFn(metadata.Namespace)
enc.encodeVarUintFn(uint64(metadata.Shard))
enc.encodeBytesFn(metadata.EncodedTags)
}
func (enc *Encoder) encodeRootObject(version int, objType objectType) {
enc.encodeVersionFn(version)
enc.encodeNumObjectFieldsForFn(rootObjectType)
enc.encodeObjectType(objType)
}
func (enc *Encoder) encodeVersion(version int) {
enc.encodeVarintFn(int64(version))
}
func (enc *Encoder) encodeNumObjectFieldsFor(objType objectType) {
_, curr := numFieldsForType(objType)
enc.encodeArrayLenFn(curr)
}
func (enc *Encoder) encodeObjectType(objType objectType) {
enc.encodeVarintFn(int64(objType))
}
func (enc *Encoder) encodeVarint(value int64) {
if enc.err != nil {
return
}
enc.err = enc.enc.EncodeInt64(value)
}
func (enc *Encoder) encodeVarUint(value uint64) {
if enc.err != nil {
return
}
enc.err = enc.enc.EncodeUint64(value)
}
func (enc *Encoder) encodeFloat64(value float64) {
if enc.err != nil {
return
}
enc.err = enc.enc.EncodeFloat64(value)
}
func (enc *Encoder) encodeBytes(value []byte) {
if enc.err != nil {
return
}
enc.err = enc.enc.EncodeBytes(value)
}
func (enc *Encoder) encodeArrayLen(value int) {
if enc.err != nil {
return
}
enc.err = enc.enc.EncodeArrayLen(value)
}