forked from linkedin/goavro
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathocf_reader.go
403 lines (370 loc) · 10.9 KB
/
ocf_reader.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
// Copyright 2015 LinkedIn Corp. 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.Copyright [201X] LinkedIn Corp. 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.
package goavro
import (
"bufio"
"bytes"
"compress/flate"
"encoding/binary"
"fmt"
"hash/crc32"
"io"
"io/ioutil"
"github.com/golang/snappy"
)
// ErrReaderInit is returned when the encoder encounters an error.
type ErrReaderInit struct {
Message string
Err error
}
func (e ErrReaderInit) Error() string {
if e.Err == nil {
return "cannot build " + e.Message
}
return "cannot build " + e.Message + ": " + e.Err.Error()
}
func newReaderInitError(a ...interface{}) *ErrReaderInit {
var err error
var format, message string
var ok bool
if len(a) == 0 {
return &ErrReaderInit{"cannot create reader: no reason given", nil}
}
// if last item is error: save it
if err, ok = a[len(a)-1].(error); ok {
a = a[:len(a)-1] // pop it
}
// if items left, first ought to be format string
if len(a) > 0 {
if format, ok = a[0].(string); ok {
a = a[1:] // unshift
message = fmt.Sprintf(format, a...)
}
}
return &ErrReaderInit{message, err}
}
// ErrReaderBlockCount is returned when a reader detects an error
// while attempting to read the block count and block size.
type ErrReaderBlockCount struct {
Err error
}
func (e *ErrReaderBlockCount) Error() string {
return "cannot read block count and size: " + e.Err.Error()
}
// ReaderSetter functions are those those which are used to instantiate
// a new Reader.
type ReaderSetter func(*Reader) error
// BufferFromReader wraps the specified `io.Reader` using a
// `bufio.Reader` to read from a file.
func BufferFromReader(r io.Reader) ReaderSetter {
return func(fr *Reader) error {
fr.r = bufio.NewReader(r)
return nil
}
}
// FromReader specifies the `io.Reader` to use when reading a file.
func FromReader(r io.Reader) ReaderSetter {
return func(fr *Reader) error {
fr.r = r
return nil
}
}
// Reader structure contains data necessary to read Avro files.
type Reader struct {
CompressionCodec string
DataSchema string
Sync []byte
dataCodec Codec
datum Datum
deblocked chan Datum
err error
r io.Reader
}
// NewReader returns a object to read data from an io.Reader using the
// Avro Object Container Files format.
//
// func main() {
// conn, err := net.Dial("tcp", "127.0.0.1:8080")
// if err != nil {
// log.Fatal(err)
// }
// fr, err := goavro.NewReader(goavro.FromReader(conn))
// if err != nil {
// log.Fatal("cannot create Reader: ", err)
// }
// defer func() {
// if err := fr.Close(); err != nil {
// log.Fatal(err)
// }
// }()
//
// for fr.Scan() {
// datum, err := fr.Read()
// if err != nil {
// log.Println("cannot read datum: ", err)
// continue
// }
// fmt.Println("RECORD: ", datum)
// }
// }
func NewReader(setters ...ReaderSetter) (*Reader, error) {
var err error
fr := &Reader{}
for _, setter := range setters {
err = setter(fr)
if err != nil {
return nil, newReaderInitError(err)
}
}
if fr.r == nil {
return nil, newReaderInitError("must specify io.Reader")
}
// read in header information and use it to initialize Reader
magic := make([]byte, 4)
_, err = io.ReadFull(fr.r, magic)
if err != nil {
return nil, newReaderInitError("cannot read magic number", err)
}
if bytes.Compare(magic, []byte(magicBytes)) != 0 {
return nil, &ErrReaderInit{Message: "invalid magic number: " + string(magic)}
}
meta, err := decodeHeaderMetadata(fr.r)
if err != nil {
return nil, newReaderInitError("cannot read header metadata", err)
}
fr.CompressionCodec, err = getHeaderString("avro.codec", meta)
if err != nil {
fr.CompressionCodec = CompressionNull
}
if !IsCompressionCodecSupported(fr.CompressionCodec) {
return nil, newReaderInitError("unsupported codec: %s", fr.CompressionCodec)
}
fr.DataSchema, err = getHeaderString("avro.schema", meta)
if err != nil {
return nil, newReaderInitError("cannot read header metadata", err)
}
if fr.dataCodec, err = NewCodec(fr.DataSchema); err != nil {
return nil, newReaderInitError("cannot compile schema", err)
}
fr.Sync = make([]byte, syncLength)
if _, err = io.ReadFull(fr.r, fr.Sync); err != nil {
return nil, newReaderInitError("cannot read sync marker", err)
}
// setup reading pipeline
toDecompress := make(chan *readerBlock)
toDecode := make(chan *readerBlock)
fr.deblocked = make(chan Datum)
go read(fr, longCodec(), toDecompress)
go decompress(fr, toDecompress, toDecode)
go decode(fr, toDecode)
return fr, nil
}
// Close releases resources and returns any Reader errors.
func (fr *Reader) Close() error {
return fr.err
}
// Scan returns true if more data is ready to be read.
func (fr *Reader) Scan() bool {
var ok bool
fr.datum, ok = <-fr.deblocked
return ok
}
// Read returns the next element from the Reader.
func (fr *Reader) Read() (interface{}, error) {
return fr.datum.Value, fr.datum.Err
}
func decodeHeaderMetadata(r io.Reader) (map[string]interface{}, error) {
md, err := metadataCodec.Decode(r)
if err != nil {
return nil, err
}
return md.(map[string]interface{}), nil
}
func getHeaderString(someKey string, header map[string]interface{}) (string, error) {
v, ok := header[someKey]
if !ok {
return "", fmt.Errorf("header ought to have %v key", someKey)
}
return string(v.([]byte)), nil
}
type readerBlock struct {
datumCount int
err error
r io.Reader
}
// ErrReader is returned when the reader encounters an error.
type ErrReader struct {
Message string
Err error
}
func (e ErrReader) Error() string {
if e.Err == nil {
return "cannot read from reader: " + e.Message
}
return "cannot read from reader: " + e.Message + ": " + e.Err.Error()
}
func newReaderError(a ...interface{}) *ErrReader {
var err error
var format, message string
var ok bool
if len(a) == 0 {
return &ErrReader{"no reason given", nil}
}
// if last item is error: save it
if err, ok = a[len(a)-1].(error); ok {
a = a[:len(a)-1] // pop it
}
// if items left, first ought to be format string
if len(a) > 0 {
if format, ok = a[0].(string); ok {
a = a[1:] // unshift
message = fmt.Sprintf(format, a...)
}
}
return &ErrReader{message, err}
}
func read(fr *Reader, lCodec *codec, toDecompress chan<- *readerBlock) {
// NOTE: these variables created outside loop to reduce churn
sync := make([]byte, syncLength)
blockCount, blockSize, err := readBlockCountAndSize(fr.r, lCodec)
if err != nil {
fr.err = err
blockCount = 0
}
for blockCount != 0 {
// Use a new buffer for every block because it will be shared with other goroutines
bits := make([]byte, blockSize)
if _, err = io.ReadFull(fr.r, bits); err != nil {
fr.err = newReaderError("cannot read block", err)
break
}
toDecompress <- &readerBlock{datumCount: blockCount, r: bytes.NewReader(bits)}
if _, err := io.ReadFull(fr.r, sync); err != nil {
fr.err = newReaderError("cannot read sync marker", err)
break
}
if !bytes.Equal(fr.Sync, sync) {
fr.err = newReaderError(fmt.Sprintf("sync marker mismatch: %#v != %#v", sync, fr.Sync))
break
}
if blockCount, blockSize, fr.err = readBlockCountAndSize(fr.r, lCodec); fr.err != nil {
break
}
}
close(toDecompress)
}
func readBlockCountAndSize(r io.Reader, lcodec *codec) (int, int, error) {
bc, err := lcodec.Decode(r)
if err != nil {
if ed, ok := err.(*ErrDecoder); ok && ed.Err == io.EOF {
return 0, 0, nil // we're done
}
return 0, 0, &ErrReaderBlockCount{err}
}
bs, err := lcodec.Decode(r)
if err != nil {
return 0, 0, &ErrReaderBlockCount{err}
}
return int(bc.(int64)), int(bs.(int64)), nil
}
func decompress(fr *Reader, toDecompress <-chan *readerBlock, toDecode chan<- *readerBlock) {
switch fr.CompressionCodec {
case CompressionNull:
for block := range toDecompress {
toDecode <- block
}
case CompressionDeflate:
var rc io.ReadCloser
var bits []byte
for block := range toDecompress {
rc = flate.NewReader(block.r)
bits, block.err = ioutil.ReadAll(rc)
if block.err != nil {
block.err = newReaderError("cannot read from deflate", block.err)
toDecode <- block
_ = rc.Close() // already have the read error; ignore the close error
continue
}
block.err = rc.Close()
if block.err != nil {
block.err = newReaderError("cannot close deflate", block.err)
toDecode <- block
continue
}
block.r = bytes.NewReader(bits)
toDecode <- block
}
case CompressionSnappy:
var (
src, dst []byte
crc uint32
)
for block := range toDecompress {
src, block.err = ioutil.ReadAll(block.r)
if block.err != nil {
block.err = newReaderError("cannot read", block.err)
toDecode <- block
continue
}
if len(src) < 4 {
block.err = newReaderError(fmt.Sprintf("too small of a block (%d bytes)", len(src)))
toDecode <- block
continue
}
index := len(src) - 4 // last 4 bytes is crc32 of decoded blob
dst, block.err = snappy.Decode(nil, src[:index])
if block.err != nil {
block.err = newReaderError("cannot decompress", block.err)
toDecode <- block
continue
}
block.err = binary.Read(bytes.NewReader(src[index:index+4]), binary.BigEndian, &crc)
if block.err != nil {
block.err = newReaderError("failed to read crc checksum after snappy block", block.err)
toDecode <- block
continue
}
if crc != crc32.ChecksumIEEE(dst) {
block.err = newReaderError("snappy crc checksum mismatch", block.err)
toDecode <- block
continue
}
block.r = bytes.NewReader(dst)
toDecode <- block
}
}
close(toDecode)
}
func decode(fr *Reader, toDecode <-chan *readerBlock) {
decodeLoop:
for block := range toDecode {
if block.err != nil {
fr.deblocked <- Datum{Err: block.err}
} else {
for i := 0; i < block.datumCount; i++ {
var datum Datum
datum.Value, datum.Err = fr.dataCodec.Decode(block.r)
if datum.Value == nil && datum.Err == nil {
break decodeLoop
}
fr.deblocked <- datum
}
}
}
close(fr.deblocked)
}