/
rio.go
575 lines (461 loc) · 13.6 KB
/
rio.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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
// Copyright 2015 The go-hep Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package rio // import "go-hep.org/x/hep/rio"
import (
"bytes"
"compress/flate"
"encoding/binary"
"io"
"reflect"
"github.com/pkg/errors"
)
const (
gAlign = 0x00000003
rioHdrVersion = Version(0)
gMaskCodec = Options(0x00000fff)
gMaskLevel = Options(0x0000f000)
gMaskCompr = Options(0xffff0000)
// Name of the metadata record holding Metadata informations about the rio stream
MetaRecord = ".rio.meta"
)
// Version describes a rio on-disk version of a serialized block.
type Version uint32
// frameType frames blocks, records and footers
type frameType [4]byte
var (
rioMagic = [4]byte{'r', 'i', 'o', '\x00'}
recFrame = frameType{0xab, 0xad, 0xca, 0xfe} // 0xabadcafe
blkFrame = frameType{0xde, 0xad, 0xbe, 0xef} // 0xdeadbeef
ftrFrame = frameType{0xca, 0xfe, 0xba, 0xbe} // 0xcafebabe
// Endian exposes the endianness of rio streams
Endian = binary.LittleEndian
hdrSize = int(reflect.TypeOf(rioHeader{}).Size())
recSize = int(reflect.TypeOf(rioRecord{}).Size())
blkSize = int(reflect.TypeOf(rioBlock{}).Size())
ftrSize = int(reflect.TypeOf(rioFooter{}).Size())
)
// Marshaler is the interface implemented by an object that can
// marshal itself into a rio-binary form.
//
// RioMarshal marshals the receiver into a rio-binary form, writes that
// binary form to the io.Writer and returns an error if any.
type Marshaler interface {
RioMarshal(w io.Writer) error
}
// Unmarshalr is the interface implemented by an object that can
// unmarshal a rio-binary representation of itself.
//
// RioUnmarshal must be able to unmarshal the form generated by RioMarshal.
type Unmarshaler interface {
RioUnmarshal(r io.Reader) error
}
// Streamer is the interface implemented by an object that can
// marshal/unmarshal a rio-binary representation of itself
// to/from an io.Writer/io.Reader.
type Streamer interface {
Marshaler
Unmarshaler
RioVersion() Version
}
// rioHeader
type rioHeader struct {
// Length of payload in bytes (not counting Len nor Frame).
// Always a multiple of four.
Len uint32
// Framing used to try identifying what kind of payload follows
// (record or block)
Frame frameType
}
func (hdr *rioHeader) RioMarshal(w io.Writer) error {
var err error
err = binary.Write(w, Endian, hdr.Len)
if err != nil {
return errors.Errorf("rio: write header length failed: %v", err)
}
err = binary.Write(w, Endian, hdr.Frame)
if err != nil {
return errors.Errorf("rio: write header frame failed: %v", err)
}
return err
}
func (hdr *rioHeader) RioUnmarshal(r io.Reader) error {
var err error
err = binary.Read(r, Endian, &hdr.Len)
if err != nil {
if err == io.EOF {
return err
}
return errors.Errorf("rio: read header length failed: %v", err)
}
err = binary.Read(r, Endian, &hdr.Frame)
if err != nil {
return errors.Errorf("rio: read header frame failed: %v", err)
}
return err
}
func (hdr *rioHeader) RioVersion() Version {
return rioHdrVersion
}
// Options describes the various options attached to a rio stream
// such as: compression method, compression level, codec, ...
type Options uint32
// CompressorKind extracts the CompressorKind from the Options value
func (o Options) CompressorKind() CompressorKind {
return CompressorKind((o & gMaskCompr) >> 16)
}
// CompressorLevel extracts the compression level from the Options value
func (o Options) CompressorLevel() int {
lvl := int((o & gMaskLevel) >> 12)
if lvl == 0xf {
return flate.DefaultCompression
}
return lvl
}
// CompressorCodec extracts the compression codec from the Options value
func (o Options) CompressorCodec() int {
return int(o & gMaskCodec)
}
// NewOptions returns a new Options value carefully crafted from the CompressorKind and
// compression level
func NewOptions(compr CompressorKind, lvl int, codec int) Options {
if lvl <= flate.DefaultCompression || lvl >= 0xf {
lvl = 0xf
}
if compr == CompressDefault {
compr = CompressZlib
}
// FIXME(sbinet): decide on how to handle different codecs (gob|cbor|xdr|riobin|...)
opts := Options(Options(compr)<<16) |
Options(Options(lvl)<<12) |
Options(Options(codec)&gMaskCodec)
return opts
}
// rioRecord
type rioRecord struct {
Header rioHeader
Options Options // options word (compression method, compression level, codec, ...)
// length of compressed record content.
// Total length in bytes for all the blocks in the record.
// Always a multiple of four.
// If the record is not compressed, same value than XLen.
CLen uint32
// length of un-compressed record content.
// Total length in bytes for all the blocks in the record when decompressed.
// Always a multiple of four.
// When the record is not compressed, it is a count of the bytes that follow in the
// record content.
// When the record is compressed, this number is used to allocate a buffer into which
// the record is decompressed.
XLen uint32
// name of the record. padded with zeros to a four byte boundary
Name string
}
func (rec *rioRecord) MarshalBinary() ([]byte, error) {
buf := bytes.NewBuffer(make([]byte, 0, recSize))
err := rec.RioMarshal(buf)
if err != nil {
return nil, err
}
return buf.Bytes(), err
}
func (rec *rioRecord) UnmarshalBinary(data []byte) error {
r := bytes.NewReader(data)
return rec.RioUnmarshal(r)
}
func (rec *rioRecord) RioMarshal(w io.Writer) error {
var err error
err = rec.Header.RioMarshal(w)
if err != nil {
return errors.Errorf("rio: write record header failed: %v", err)
}
err = binary.Write(w, Endian, rec.Options)
if err != nil {
return errors.Errorf("rio: write record options failed: %v", err)
}
err = binary.Write(w, Endian, rec.CLen)
if err != nil {
return errors.Errorf("rio: write record compr-len failed: %v", err)
}
err = binary.Write(w, Endian, rec.XLen)
if err != nil {
return errors.Errorf("rio: write record len failed: %v", err)
}
err = binary.Write(w, Endian, uint32(len(rec.Name)))
if err != nil {
return errors.Errorf("rio: write record name-len failed: %v", err)
}
name := []byte(rec.Name)
_, err = w.Write(name)
if err != nil {
return errors.Errorf("rio: write record name failed: %v", err)
}
size := rioAlign(len(name))
if size > len(name) {
_, err = w.Write(make([]byte, size-len(name)))
if err != nil {
return errors.Errorf("rio: write record name-padding failed: %v", err)
}
}
return err
}
func (rec *rioRecord) RioUnmarshal(r io.Reader) error {
var err error
err = rec.unmarshalHeader(r)
if err != nil {
return err
}
err = rec.unmarshalData(r)
if err != nil {
return err
}
return err
}
func (rec *rioRecord) unmarshalHeader(r io.Reader) error {
var err error
err = rec.Header.RioUnmarshal(r)
if err != nil {
if err == io.EOF || err == io.ErrUnexpectedEOF {
return err
}
return errors.Errorf("rio: read record header failed: %v", err)
}
if rec.Header.Frame != recFrame {
return errors.Errorf("rio: read record header corrupted (frame=%#v)", rec.Header.Frame)
}
return err
}
func (rec *rioRecord) unmarshalData(r io.Reader) error {
var err error
err = binary.Read(r, Endian, &rec.Options)
if err != nil {
return errors.Errorf("rio: read record options failed: %v", err)
}
err = binary.Read(r, Endian, &rec.CLen)
if err != nil {
return errors.Errorf("rio: read record compr-len failed: %v", err)
}
err = binary.Read(r, Endian, &rec.XLen)
if err != nil {
return errors.Errorf("rio: read record len failed failed: %v", err)
}
nsize := uint32(0)
err = binary.Read(r, Endian, &nsize)
if err != nil {
return errors.Errorf("rio: read record name-len failed: %v", err)
}
buf := make([]byte, rioAlign(int(nsize)))
_, err = r.Read(buf)
if err != nil {
return errors.Errorf("rio: read record name failed: %v", err)
}
rec.Name = string(buf[:int(nsize)])
return err
}
func (rec *rioRecord) RioVersion() Version {
return rioHdrVersion
}
// rioBlock
type rioBlock struct {
Header rioHeader
Version Version // block version
Name string // block name
Data []byte // block payload
}
func (blk *rioBlock) MarshalBinary() ([]byte, error) {
buf := bytes.NewBuffer(make([]byte, 0, recSize))
err := blk.RioMarshal(buf)
if err != nil {
return nil, err
}
return buf.Bytes(), err
}
func (blk *rioBlock) UnmarshalBinary(data []byte) error {
r := bytes.NewReader(data)
return blk.RioUnmarshal(r)
}
func (blk *rioBlock) RioMarshal(w io.Writer) error {
var err error
err = blk.Header.RioMarshal(w)
if err != nil {
return errors.Errorf("rio: write block header failed: %v", err)
}
err = binary.Write(w, Endian, blk.Version)
if err != nil {
return errors.Errorf("rio: write block version failed: %v", err)
}
name := []byte(blk.Name)
err = binary.Write(w, Endian, uint32(len(name)))
if err != nil {
return errors.Errorf("rio: write block name-len failed: %v", err)
}
nb, err := w.Write(name)
if err != nil {
return errors.Errorf("rio: write block name failed: %v", err)
}
if nb != len(name) {
return errors.Errorf("rio: wrote too few bytes (want=%d. got=%d)", len(name), nb)
}
nsize := rioAlign(len(name))
if nsize > len(name) {
nb, err = w.Write(make([]byte, nsize-len(name)))
if err != nil {
return errors.Errorf("rio: write block name-padding failed: %v", err)
}
if nb != nsize-len(name) {
return errors.Errorf("rio: wrote too few bytes (want=%d. got=%d)", nsize-len(name), nb)
}
}
nb, err = w.Write(blk.Data)
if err != nil {
return errors.Errorf("rio: write block data failed: %v", err)
}
if nb != len(blk.Data) {
return errors.Errorf("rio: wrote too few bytes (want=%d. got=%d)", len(blk.Data), nb)
}
dsize := rioAlign(len(blk.Data))
if dsize > len(blk.Data) {
nb, err = w.Write(make([]byte, dsize-len(blk.Data)))
if err != nil {
return errors.Errorf("rio: write block data-padding failed: %v", err)
}
if nb != dsize-len(blk.Data) {
return errors.Errorf("rio: wrote too few bytes (want=%d. got=%d)", dsize-len(blk.Data), nb)
}
}
return err
}
func (blk *rioBlock) RioUnmarshal(r io.Reader) error {
var err error
err = blk.Header.RioUnmarshal(r)
if err != nil {
if err == io.EOF || err == io.ErrUnexpectedEOF {
return err
}
return errors.Errorf("rio: read block header failed: %v", err)
}
if blk.Header.Frame != blkFrame {
return errors.Errorf("rio: read block header corrupted (frame=%#v)", blk.Header.Frame)
}
err = binary.Read(r, Endian, &blk.Version)
if err != nil {
return errors.Errorf("rio: read block version failed: %v", err)
}
nsize := uint32(0)
err = binary.Read(r, Endian, &nsize)
if err != nil {
return errors.Errorf("rio: read block name-len failed: %v", err)
}
name := make([]byte, rioAlign(int(nsize)))
nb, err := io.ReadFull(r, name)
if err != nil {
return errors.Errorf("rio: read block name failed: %v", err)
}
if int(nb) != len(name) {
return errors.Errorf("rio: read too few bytes for name (want=%d. got=%d)", len(name), nb)
}
blk.Name = string(name[:int(nsize)])
data := make([]byte, rioAlign(int(blk.Header.Len)))
nb, err = io.ReadFull(r, data)
if err != nil {
return errors.Errorf("rio: read block data failed: %v", err)
}
if int(nb) != len(data) {
return errors.Errorf("rio: read too few bytes for data (want=%d. got=%d)", len(data), nb)
}
blk.Data = data[:int(blk.Header.Len)]
return err
}
func (blk *rioBlock) RioVersion() Version {
return blk.Version
}
// rioFooter marks the end of a rio stream
type rioFooter struct {
Header rioHeader
Meta int64 // position of the record holding stream metadata, in bytes from rio-magic
}
func (ftr *rioFooter) MarshalBinary() ([]byte, error) {
buf := bytes.NewBuffer(make([]byte, 0, recSize))
err := ftr.RioMarshal(buf)
if err != nil {
return nil, err
}
return buf.Bytes(), err
}
func (ftr *rioFooter) UnmarshalBinary(data []byte) error {
r := bytes.NewReader(data)
return ftr.RioUnmarshal(r)
}
func (ftr *rioFooter) RioVersion() Version {
return rioHdrVersion
}
func (ftr *rioFooter) RioMarshal(w io.Writer) error {
var err error
err = ftr.Header.RioMarshal(w)
if err != nil {
return errors.Errorf("rio: write footer header failed: %v", err)
}
err = binary.Write(w, Endian, ftr.Meta)
if err != nil {
return errors.Errorf("rio: write footer meta failed: %v", err)
}
return err
}
func (ftr *rioFooter) RioUnmarshal(r io.Reader) error {
var err error
err = ftr.unmarshalHeader(r)
if err != nil {
return err
}
err = ftr.unmarshalData(r)
if err != nil {
return err
}
return err
}
func (ftr *rioFooter) unmarshalHeader(r io.Reader) error {
var err error
err = ftr.Header.RioUnmarshal(r)
if err != nil {
if err == io.EOF {
return err
}
return errors.Errorf("rio: read footer header failed: %v", err)
}
if ftr.Header.Frame != ftrFrame {
return errors.Errorf("rio: read footer header corrupted (frame=%#v)", ftr.Header.Frame)
}
return err
}
func (ftr *rioFooter) unmarshalData(r io.Reader) error {
var err error
err = binary.Read(r, Endian, &ftr.Meta)
if err != nil {
return errors.Errorf("rio: read footer meta failed: %v", err)
}
return err
}
// Metadata stores metadata about a rio stream
type Metadata struct {
Records []RecordDesc
Offsets map[string][]Span
}
// RecordDesc provides high-level informations about a Record
type RecordDesc struct {
Name string
Blocks []BlockDesc
}
// BlockDesc provides high-level informations about a Block
type BlockDesc struct {
Name string
Type string
}
// Span is a pair (position, length)
type Span struct {
Pos int64
Len int64
}
type recordsByName []RecordDesc
func (p recordsByName) Len() int { return len(p) }
func (p recordsByName) Swap(i, j int) { p[i], p[j] = p[j], p[i] }
func (p recordsByName) Less(i, j int) bool { return p[i].Name < p[j].Name }