forked from elodina/go-avro
-
Notifications
You must be signed in to change notification settings - Fork 0
/
data_file.go
301 lines (259 loc) · 7.88 KB
/
data_file.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
package avro
import (
"bytes"
"fmt"
"io"
"io/ioutil"
"math"
)
// Support decoding the avro Object Container File format.
// Spec: http://avro.apache.org/docs/1.7.7/spec.html#Object+Container+Files
const objHeaderSchemaRaw = `{"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}}
]
}`
var objHeaderSchema = MustParseSchema(objHeaderSchemaRaw)
const (
version byte = 1
syncSize = 16
schemaKey = "avro.schema"
codecKey = "avro.codec"
)
var magic = []byte{'O', 'b', 'j', version}
// DataFileReader is a reader for Avro Object Container Files.
// More here: https://avro.apache.org/docs/current/spec.html#Object+Container+Files
type DataFileReader struct {
data []byte
header *objFileHeader
block *DataBlock
dec Decoder
blockDecoder Decoder
datum DatumReader
}
// The header for object container files
type objFileHeader struct {
Magic []byte `avro:"magic"`
Meta map[string][]byte `avro:"meta"`
Sync []byte `avro:"sync"`
}
func readObjFileHeader(dec *BinaryDecoder) (*objFileHeader, error) {
reader := NewSpecificDatumReader()
reader.SetSchema(objHeaderSchema)
header := &objFileHeader{}
err := reader.Read(header, dec)
return header, err
}
// NewDataFileReader creates a new DataFileReader for a given file and using the given DatumReader to read the data from that file.
// May return an error if the file contains invalid data or is just missing.
func NewDataFileReader(filename string, datumReader DatumReader) (*DataFileReader, error) {
buf, err := ioutil.ReadFile(filename)
if err != nil {
return nil, err
}
return newDataFileReaderBytes(buf, datumReader)
}
// separated out mainly for testing currently, will be refactored later for io.Reader paradigm
func newDataFileReaderBytes(buf []byte, datumReader DatumReader) (reader *DataFileReader, err error) {
if len(buf) < len(magic) || !bytes.Equal(magic, buf[0:4]) {
return nil, NotAvroFile
}
dec := NewBinaryDecoder(buf)
blockDecoder := NewBinaryDecoder(nil)
reader = &DataFileReader{
data: buf,
dec: dec,
blockDecoder: blockDecoder,
datum: datumReader,
}
if reader.header, err = readObjFileHeader(dec); err != nil {
return nil, err
}
schema, err := ParseSchema(string(reader.header.Meta[schemaKey]))
if err != nil {
return nil, err
}
reader.datum.SetSchema(schema)
reader.block = &DataBlock{}
if reader.hasNextBlock() {
if err := reader.NextBlock(); err != nil {
return nil, err
}
}
return reader, nil
}
// Seek switches the reading position in this DataFileReader to a provided value.
func (reader *DataFileReader) Seek(pos int64) {
reader.dec.Seek(pos)
}
func (reader *DataFileReader) hasNext() (bool, error) {
if reader.block.BlockRemaining == 0 {
if int64(reader.block.BlockSize) != reader.blockDecoder.Tell() {
return false, BlockNotFinished
}
if reader.hasNextBlock() {
if err := reader.NextBlock(); err != nil {
return false, err
}
} else {
return false, nil
}
}
return true, nil
}
func (reader *DataFileReader) hasNextBlock() bool {
return int64(len(reader.data)) > reader.dec.Tell()
}
// Next reads the next value from file and fills the given value with data.
// First return value indicates whether the read was successful.
// Second return value indicates whether there was an error while reading data.
// Returns (false, nil) when no more data left to read.
func (reader *DataFileReader) Next(v interface{}) (bool, error) {
hasNext, err := reader.hasNext()
if err != nil {
return false, err
}
if hasNext {
err := reader.datum.Read(v, reader.blockDecoder)
if err != nil {
return false, err
}
reader.block.BlockRemaining--
return true, nil
}
return false, nil
}
// NextBlock tells this DataFileReader to skip current block and move to next one.
// May return an error if the block is malformed or no more blocks left to read.
func (reader *DataFileReader) NextBlock() error {
blockCount, err := reader.dec.ReadLong()
if err != nil {
return err
}
blockSize, err := reader.dec.ReadLong()
if err != nil {
return err
}
if blockSize > math.MaxInt32 || blockSize < 0 {
return fmt.Errorf("Block size invalid or too large: %d", blockSize)
}
block := reader.block
if block.Data == nil || int64(len(block.Data)) < blockSize {
block.Data = make([]byte, blockSize)
}
block.BlockRemaining = blockCount
block.NumEntries = blockCount
block.BlockSize = int(blockSize)
err = reader.dec.ReadFixedWithBounds(block.Data, 0, int(block.BlockSize))
if err != nil {
return err
}
syncBuffer := make([]byte, syncSize)
err = reader.dec.ReadFixed(syncBuffer)
if err != nil {
return err
}
if !bytes.Equal(syncBuffer, reader.header.Sync) {
return InvalidSync
}
reader.blockDecoder.SetBlock(reader.block)
return nil
}
////////// DATA FILE WRITER
// DataFileWriter lets you write object container files.
type DataFileWriter struct {
output io.Writer
outputEnc *BinaryEncoder
datumWriter DatumWriter
sync []byte
// current block is buffered until flush
blockBuf *bytes.Buffer
blockCount int64
blockEnc *BinaryEncoder
}
// NewDataFileWriter creates a new DataFileWriter for given output and schema using the given DatumWriter to write the data to that Writer.
// May return an error if writing fails.
func NewDataFileWriter(output io.Writer, schema Schema, datumWriter DatumWriter) (writer *DataFileWriter, err error) {
encoder := NewBinaryEncoder(output)
datumWriter.SetSchema(schema)
sync := []byte("1234567890abcdef") // TODO come up with other sync value
header := &objFileHeader{
Magic: magic,
Meta: map[string][]byte{
schemaKey: []byte(schema.String()),
codecKey: []byte("null"),
},
Sync: sync,
}
headerWriter := NewSpecificDatumWriter()
headerWriter.SetSchema(objHeaderSchema)
if err = headerWriter.Write(header, encoder); err != nil {
return
}
blockBuf := &bytes.Buffer{}
writer = &DataFileWriter{
output: output,
outputEnc: encoder,
datumWriter: datumWriter,
sync: sync,
blockBuf: blockBuf,
blockEnc: NewBinaryEncoder(blockBuf),
}
return
}
// Write out a single datum.
//
// Encoded datums are buffered internally and will not be written to the
// underlying io.Writer until Flush() is called.
func (w *DataFileWriter) Write(v interface{}) error {
w.blockCount++
err := w.datumWriter.Write(v, w.blockEnc)
return err
}
// Flush out any previously written datums to our underlying io.Writer.
// Does nothing if no datums had previously been written.
//
// It's up to the library user to decide how often to flush; doing it
// often will spend a lot of time on tiny I/O but save memory.
func (w *DataFileWriter) Flush() error {
if w.blockCount > 0 {
return w.actuallyFlush()
}
return nil
}
func (w *DataFileWriter) actuallyFlush() error {
// Write the block count and length directly to output
w.outputEnc.WriteLong(w.blockCount)
w.outputEnc.WriteLong(int64(w.blockBuf.Len()))
// copy the buffer which is the block buf to output
_, err := io.Copy(w.output, w.blockBuf)
if err != nil {
return err
}
// write the sync bytes
_, err = w.output.Write(w.sync)
if err != nil {
return err
}
w.blockBuf.Reset() // allow blockbuf's internal memory to be reused
w.blockCount = 0
return nil
}
// Close this DataFileWriter.
// This is required to finish out the data file format.
// After Close() is called, this DataFileWriter cannot be used anymore.
func (w *DataFileWriter) Close() error {
err := w.Flush() // flush anything remaining
if err == nil {
// Do an empty flush to signal end of data file format
err = w.actuallyFlush()
if err == nil {
// Clean up references.
w.output, w.outputEnc, w.datumWriter = nil, nil, nil
w.blockBuf, w.blockEnc = nil, nil
}
}
return err
}