forked from elodina/go-avro
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdata_file.go
188 lines (167 loc) · 4.52 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
package avro
import (
"bytes"
"errors"
"fmt"
"io/ioutil"
"math"
)
const (
version byte = 1
sync_size = 16
schema_key = "avro.schema"
codec_key = "avro.codec"
)
var magic []byte = []byte{'O', 'b', 'j', version}
var syncBuffer = make([]byte, sync_size)
// 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 *header
block *DataBlock
dec Decoder
blockDecoder Decoder
datum DatumReader
}
type header struct {
meta map[string][]byte
sync []byte
}
func newHeader() *header {
header := &header{}
header.meta = make(map[string][]byte)
header.sync = make([]byte, sync_size)
return header
}
// 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) {
if buf, err := ioutil.ReadFile(filename); err != nil {
return nil, err
} else {
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,
}
reader.Seek(4) //skip the magic bytes
reader.header = newHeader()
if metaLength, err := dec.ReadMapStart(); err != nil {
return nil, err
} else {
for {
var i int64 = 0
for i < metaLength {
key, err := dec.ReadString()
if err != nil {
return nil, err
}
value, err := dec.ReadBytes()
if err != nil {
return nil, err
}
reader.header.meta[key] = value
i++
}
metaLength, err = dec.MapNext()
if err != nil {
return nil, err
} else if metaLength == 0 {
break
}
}
}
dec.ReadFixed(reader.header.sync)
//TODO codec?
schema, err := ParseSchema(string(reader.header.meta[schema_key]))
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
}
}
// Switches the reading position in this DataFileReader to a provided value.
func (this *DataFileReader) Seek(pos int64) {
this.dec.Seek(pos)
}
func (this *DataFileReader) hasNext() (bool, error) {
if this.block.BlockRemaining == 0 {
if int64(this.block.BlockSize) != this.blockDecoder.Tell() {
return false, BlockNotFinished
}
if this.hasNextBlock() {
if err := this.NextBlock(); err != nil {
return false, err
}
} else {
return false, nil
}
}
return true, nil
}
func (this *DataFileReader) hasNextBlock() bool {
return int64(len(this.data)) > this.dec.Tell()
}
// 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 (this *DataFileReader) Next(v interface{}) (bool, error) {
if hasNext, err := this.hasNext(); err != nil {
return false, err
} else {
if hasNext {
err := this.datum.Read(v, this.blockDecoder)
if err != nil {
return false, err
}
this.block.BlockRemaining--
return true, nil
} else {
return false, nil
}
}
return false, nil
}
// 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 (this *DataFileReader) NextBlock() error {
if blockCount, err := this.dec.ReadLong(); err != nil {
return err
} else {
if blockSize, err := this.dec.ReadLong(); err != nil {
return err
} else {
if blockSize > math.MaxInt32 || blockSize < 0 {
return errors.New(fmt.Sprintf("Block size invalid or too large: %d", blockSize))
}
block := this.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)
this.dec.ReadFixedWithBounds(block.Data, 0, int(block.BlockSize))
this.dec.ReadFixed(syncBuffer)
if !bytes.Equal(syncBuffer, this.header.sync) {
return InvalidSync
}
this.blockDecoder.SetBlock(this.block)
}
}
return nil
}