forked from hamba/avro
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathocf.go
428 lines (357 loc) · 9.16 KB
/
ocf.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
// Package ocf implements encoding and decoding of Avro Object Container Files as defined by the Avro specification.
//
// See the Avro specification for an understanding of Avro: http://avro.apache.org/docs/current/
package ocf
import (
"bytes"
"crypto/rand"
"errors"
"fmt"
"io"
"os"
"github.com/hamba/avro/v2"
"github.com/hamba/avro/v2/internal/bytesx"
)
const (
schemaKey = "avro.schema"
codecKey = "avro.codec"
)
var magicBytes = [4]byte{'O', 'b', 'j', 1}
// HeaderSchema is the Avro schema of a container file header.
var HeaderSchema = avro.MustParse(`{
"type": "record",
"name": "org.apache.avro.file.Header",
"fields": [
{"name": "magic", "type": {"type": "fixed", "name": "Magic", "size": 4}},
{"name": "meta", "type": {"type": "map", "values": "bytes"}},
{"name": "sync", "type": {"type": "fixed", "name": "Sync", "size": 16}}
]
}`)
// Header represents an Avro container file header.
type Header struct {
Magic [4]byte `avro:"magic"`
Meta map[string][]byte `avro:"meta"`
Sync [16]byte `avro:"sync"`
}
// Decoder reads and decodes Avro values from a container file.
type Decoder struct {
reader *avro.Reader
resetReader *bytesx.ResetReader
decoder *avro.Decoder
meta map[string][]byte
sync [16]byte
codec Codec
count int64
}
// NewDecoder returns a new decoder that reads from reader r.
func NewDecoder(r io.Reader) (*Decoder, error) {
reader := avro.NewReader(r, 1024)
h, err := readHeader(reader)
if err != nil {
return nil, fmt.Errorf("decoder: %w", err)
}
decReader := bytesx.NewResetReader([]byte{})
return &Decoder{
reader: reader,
resetReader: decReader,
decoder: avro.NewDecoderForSchema(h.Schema, decReader),
meta: h.Meta,
sync: h.Sync,
codec: h.Codec,
}, nil
}
// Metadata returns the header metadata.
func (d *Decoder) Metadata() map[string][]byte {
return d.meta
}
// HasNext determines if there is another value to read.
func (d *Decoder) HasNext() bool {
if d.count <= 0 {
count := d.readBlock()
d.count = count
}
if d.reader.Error != nil {
return false
}
return d.count > 0
}
// Decode reads the next Avro encoded value from its input and stores it in the value pointed to by v.
func (d *Decoder) Decode(v any) error {
if d.count <= 0 {
return errors.New("decoder: no data found, call HasNext first")
}
d.count--
return d.decoder.Decode(v)
}
// Error returns the last reader error.
func (d *Decoder) Error() error {
if errors.Is(d.reader.Error, io.EOF) {
return nil
}
return d.reader.Error
}
func (d *Decoder) readBlock() int64 {
_ = d.reader.Peek()
if errors.Is(d.reader.Error, io.EOF) {
// There is no next block
return 0
}
count := d.reader.ReadLong()
size := d.reader.ReadLong()
// Read the blocks data
if count > 0 {
data := make([]byte, size)
d.reader.Read(data)
data, err := d.codec.Decode(data)
if err != nil {
d.reader.Error = err
}
d.resetReader.Reset(data)
}
// Read the sync.
var sync [16]byte
d.reader.Read(sync[:])
if d.sync != sync && !errors.Is(d.reader.Error, io.EOF) {
d.reader.Error = errors.New("decoder: invalid block")
}
return count
}
type encoderConfig struct {
BlockLength int
CodecName CodecName
CodecCompression int
Metadata map[string][]byte
Sync [16]byte
EncodingConfig avro.API
}
// EncoderFunc represents a configuration function for Encoder.
type EncoderFunc func(cfg *encoderConfig)
// WithBlockLength sets the block length on the encoder.
func WithBlockLength(length int) EncoderFunc {
return func(cfg *encoderConfig) {
cfg.BlockLength = length
}
}
// WithCodec sets the compression codec on the encoder.
func WithCodec(codec CodecName) EncoderFunc {
return func(cfg *encoderConfig) {
cfg.CodecName = codec
}
}
// WithCompressionLevel sets the compression codec to deflate and
// the compression level on the encoder.
func WithCompressionLevel(compLvl int) EncoderFunc {
return func(cfg *encoderConfig) {
cfg.CodecName = Deflate
cfg.CodecCompression = compLvl
}
}
// WithMetadata sets the metadata on the encoder header.
func WithMetadata(meta map[string][]byte) EncoderFunc {
return func(cfg *encoderConfig) {
cfg.Metadata = meta
}
}
// WithSyncBlock sets the sync block.
func WithSyncBlock(sync [16]byte) EncoderFunc {
return func(cfg *encoderConfig) {
cfg.Sync = sync
}
}
// WithEncodingConfig sets the value encoder config on the OCF encoder.
func WithEncodingConfig(wCfg avro.API) EncoderFunc {
return func(cfg *encoderConfig) {
cfg.EncodingConfig = wCfg
}
}
// Encoder writes Avro container file to an output stream.
type Encoder struct {
writer *avro.Writer
buf *bytes.Buffer
encoder *avro.Encoder
sync [16]byte
codec Codec
blockLength int
count int
}
// NewEncoder returns a new encoder that writes to w using schema s.
//
// If the writer is an existing ocf file, it will append data using the
// existing schema.
func NewEncoder(s string, w io.Writer, opts ...EncoderFunc) (*Encoder, error) {
cfg := encoderConfig{
BlockLength: 100,
CodecName: Null,
CodecCompression: -1,
Metadata: map[string][]byte{},
EncodingConfig: avro.DefaultConfig,
}
for _, opt := range opts {
opt(&cfg)
}
switch file := w.(type) {
case nil:
return nil, errors.New("writer cannot be nil")
case *os.File:
info, err := file.Stat()
if err != nil {
return nil, err
}
if info.Size() > 0 {
reader := avro.NewReader(file, 1024)
h, err := readHeader(reader)
if err != nil {
return nil, err
}
if err = skipToEnd(reader, h.Sync); err != nil {
return nil, err
}
writer := avro.NewWriter(w, 512, avro.WithWriterConfig(cfg.EncodingConfig))
buf := &bytes.Buffer{}
e := &Encoder{
writer: writer,
buf: buf,
encoder: cfg.EncodingConfig.NewEncoder(h.Schema, buf),
sync: h.Sync,
codec: h.Codec,
blockLength: cfg.BlockLength,
}
return e, nil
}
}
schema, err := avro.Parse(s)
if err != nil {
return nil, err
}
cfg.Metadata[schemaKey] = []byte(schema.String())
cfg.Metadata[codecKey] = []byte(cfg.CodecName)
header := Header{
Magic: magicBytes,
Meta: cfg.Metadata,
}
header.Sync = cfg.Sync
if header.Sync == [16]byte{} {
_, _ = rand.Read(header.Sync[:])
}
codec, err := resolveCodec(cfg.CodecName, cfg.CodecCompression)
if err != nil {
return nil, err
}
writer := avro.NewWriter(w, 512, avro.WithWriterConfig(cfg.EncodingConfig))
writer.WriteVal(HeaderSchema, header)
if err = writer.Flush(); err != nil {
return nil, err
}
buf := &bytes.Buffer{}
e := &Encoder{
writer: writer,
buf: buf,
encoder: cfg.EncodingConfig.NewEncoder(schema, buf),
sync: header.Sync,
codec: codec,
blockLength: cfg.BlockLength,
}
return e, nil
}
// Write v to the internal buffer. This method skips the internal encoder and
// therefore the caller is responsible for encoding the bytes. No error will be
// thrown if the bytes does not conform to the schema given to NewEncoder, but
// the final ocf data will be corrupted.
func (e *Encoder) Write(p []byte) (n int, err error) {
n, err = e.buf.Write(p)
if err != nil {
return n, err
}
e.count++
if e.count >= e.blockLength {
if err = e.writerBlock(); err != nil {
return n, err
}
}
return n, e.writer.Error
}
// Encode writes the Avro encoding of v to the stream.
func (e *Encoder) Encode(v any) error {
if err := e.encoder.Encode(v); err != nil {
return err
}
e.count++
if e.count >= e.blockLength {
if err := e.writerBlock(); err != nil {
return err
}
}
return e.writer.Error
}
// Flush flushes the underlying writer.
func (e *Encoder) Flush() error {
if e.count == 0 {
return nil
}
if err := e.writerBlock(); err != nil {
return err
}
return e.writer.Error
}
// Close closes the encoder, flushing the writer.
func (e *Encoder) Close() error {
return e.Flush()
}
func (e *Encoder) writerBlock() error {
e.writer.WriteLong(int64(e.count))
b := e.codec.Encode(e.buf.Bytes())
e.writer.WriteLong(int64(len(b)))
_, _ = e.writer.Write(b)
_, _ = e.writer.Write(e.sync[:])
e.count = 0
e.buf.Reset()
return e.writer.Flush()
}
type ocfHeader struct {
Schema avro.Schema
Codec Codec
Meta map[string][]byte
Sync [16]byte
}
func readHeader(reader *avro.Reader) (*ocfHeader, error) {
var h Header
reader.ReadVal(HeaderSchema, &h)
if reader.Error != nil {
return nil, fmt.Errorf("unexpected error: %w", reader.Error)
}
if h.Magic != magicBytes {
return nil, errors.New("invalid avro file")
}
schema, err := avro.Parse(string(h.Meta[schemaKey]))
if err != nil {
return nil, err
}
codec, err := resolveCodec(CodecName(h.Meta[codecKey]), -1)
if err != nil {
return nil, err
}
return &ocfHeader{
Schema: schema,
Codec: codec,
Meta: h.Meta,
Sync: h.Sync,
}, nil
}
func skipToEnd(reader *avro.Reader, sync [16]byte) error {
for {
_ = reader.ReadLong()
if errors.Is(reader.Error, io.EOF) {
return nil
}
size := reader.ReadLong()
reader.SkipNBytes(int(size))
if reader.Error != nil {
return reader.Error
}
var synMark [16]byte
reader.Read(synMark[:])
if sync != synMark && !errors.Is(reader.Error, io.EOF) {
reader.Error = errors.New("invalid block")
}
}
}