/
file_writer.go
394 lines (325 loc) · 8.92 KB
/
file_writer.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
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you 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.
// See the License for the specific language governing permissions and
// limitations under the License.
package ipc
import (
"encoding/binary"
"fmt"
"io"
"github.com/apache/arrow/go/v17/arrow"
"github.com/apache/arrow/go/v17/arrow/bitutil"
"github.com/apache/arrow/go/v17/arrow/internal/dictutils"
"github.com/apache/arrow/go/v17/arrow/internal/flatbuf"
"github.com/apache/arrow/go/v17/arrow/memory"
)
// PayloadWriter is an interface for injecting a different payloadwriter
// allowing more reusability with the Writer object with other scenarios,
// such as with Flight data
type PayloadWriter interface {
Start() error
WritePayload(Payload) error
Close() error
}
type pwriter struct {
w io.WriteSeeker
pos int64
schema *arrow.Schema
dicts []fileBlock
recs []fileBlock
}
func (w *pwriter) Start() error {
var err error
err = w.updatePos()
if err != nil {
return fmt.Errorf("arrow/ipc: could not update position while in start: %w", err)
}
// only necessary to align to 8-byte boundary at the start of the file
_, err = w.Write(Magic)
if err != nil {
return fmt.Errorf("arrow/ipc: could not write magic Arrow bytes: %w", err)
}
err = w.align(kArrowIPCAlignment)
if err != nil {
return fmt.Errorf("arrow/ipc: could not align start block: %w", err)
}
return err
}
func (w *pwriter) WritePayload(p Payload) error {
blk := fileBlock{Offset: w.pos, Meta: 0, Body: p.size}
n, err := writeIPCPayload(w, p)
if err != nil {
return err
}
blk.Meta = int32(n)
err = w.updatePos()
if err != nil {
return fmt.Errorf("arrow/ipc: could not update position while in write-payload: %w", err)
}
switch flatbuf.MessageHeader(p.msg) {
case flatbuf.MessageHeaderDictionaryBatch:
w.dicts = append(w.dicts, blk)
case flatbuf.MessageHeaderRecordBatch:
w.recs = append(w.recs, blk)
}
return nil
}
func (w *pwriter) Close() error {
var err error
// write file footer
err = w.updatePos()
if err != nil {
return fmt.Errorf("arrow/ipc: could not update position while in close: %w", err)
}
pos := w.pos
err = writeFileFooter(w.schema, w.dicts, w.recs, w)
if err != nil {
return fmt.Errorf("arrow/ipc: could not write file footer: %w", err)
}
// write file footer length
err = w.updatePos() // not strictly needed as we passed w to writeFileFooter...
if err != nil {
return fmt.Errorf("arrow/ipc: could not compute file footer length: %w", err)
}
size := w.pos - pos
if size <= 0 {
return fmt.Errorf("arrow/ipc: invalid file footer size (size=%d)", size)
}
buf := make([]byte, 4)
binary.LittleEndian.PutUint32(buf, uint32(size))
_, err = w.Write(buf)
if err != nil {
return fmt.Errorf("arrow/ipc: could not write file footer size: %w", err)
}
_, err = w.Write(Magic)
if err != nil {
return fmt.Errorf("arrow/ipc: could not write Arrow magic bytes: %w", err)
}
return nil
}
func (w *pwriter) updatePos() error {
var err error
w.pos, err = w.w.Seek(0, io.SeekCurrent)
return err
}
func (w *pwriter) align(align int32) error {
remainder := paddedLength(w.pos, align) - w.pos
if remainder == 0 {
return nil
}
_, err := w.Write(paddingBytes[:int(remainder)])
return err
}
func (w *pwriter) Write(p []byte) (int, error) {
n, err := w.w.Write(p)
w.pos += int64(n)
return n, err
}
func writeIPCPayload(w io.Writer, p Payload) (int, error) {
n, err := writeMessage(p.meta, kArrowIPCAlignment, w)
if err != nil {
return n, err
}
// now write the buffers
for _, buf := range p.body {
var (
size int64
padding int64
)
// the buffer might be null if we are handling zero row lengths.
if buf != nil {
size = int64(buf.Len())
padding = bitutil.CeilByte64(size) - size
}
if size > 0 {
_, err = w.Write(buf.Bytes())
if err != nil {
return n, fmt.Errorf("arrow/ipc: could not write payload message body: %w", err)
}
}
if padding > 0 {
_, err = w.Write(paddingBytes[:padding])
if err != nil {
return n, fmt.Errorf("arrow/ipc: could not write payload message padding: %w", err)
}
}
}
return n, err
}
// Payload is the underlying message object which is passed to the payload writer
// for actually writing out ipc messages
type Payload struct {
msg MessageType
meta *memory.Buffer
body []*memory.Buffer
size int64 // length of body
}
// Meta returns the buffer containing the metadata for this payload,
// callers must call Release on the buffer
func (p *Payload) Meta() *memory.Buffer {
if p.meta != nil {
p.meta.Retain()
}
return p.meta
}
// SerializeBody serializes the body buffers and writes them to the provided
// writer.
func (p *Payload) SerializeBody(w io.Writer) error {
for _, data := range p.body {
if data == nil {
continue
}
size := int64(data.Len())
padding := bitutil.CeilByte64(size) - size
if size > 0 {
if _, err := w.Write(data.Bytes()); err != nil {
return fmt.Errorf("arrow/ipc: could not write payload message body: %w", err)
}
if padding > 0 {
if _, err := w.Write(paddingBytes[:padding]); err != nil {
return fmt.Errorf("arrow/ipc: could not write payload message padding bytes: %w", err)
}
}
}
}
return nil
}
func (p *Payload) Release() {
if p.meta != nil {
p.meta.Release()
p.meta = nil
}
for i, b := range p.body {
if b == nil {
continue
}
b.Release()
p.body[i] = nil
}
}
type payloads []Payload
func (ps payloads) Release() {
for i := range ps {
ps[i].Release()
}
}
// FileWriter is an Arrow file writer.
type FileWriter struct {
w io.WriteSeeker
mem memory.Allocator
header struct {
started bool
offset int64
}
footer struct {
written bool
}
pw PayloadWriter
schema *arrow.Schema
mapper dictutils.Mapper
codec flatbuf.CompressionType
compressNP int
minSpaceSavings *float64
// map of the last written dictionaries by id
// so we can avoid writing the same dictionary over and over
// also needed for correctness when writing IPC format which
// does not allow replacements or deltas.
lastWrittenDicts map[int64]arrow.Array
}
// NewFileWriter opens an Arrow file using the provided writer w.
func NewFileWriter(w io.WriteSeeker, opts ...Option) (*FileWriter, error) {
var (
cfg = newConfig(opts...)
err error
)
f := FileWriter{
w: w,
pw: &pwriter{w: w, schema: cfg.schema, pos: -1},
mem: cfg.alloc,
schema: cfg.schema,
codec: cfg.codec,
compressNP: cfg.compressNP,
minSpaceSavings: cfg.minSpaceSavings,
}
pos, err := f.w.Seek(0, io.SeekCurrent)
if err != nil {
return nil, fmt.Errorf("arrow/ipc: could not seek current position: %w", err)
}
f.header.offset = pos
return &f, err
}
func (f *FileWriter) Close() error {
err := f.checkStarted()
if err != nil {
return fmt.Errorf("arrow/ipc: could not write empty file: %w", err)
}
if f.footer.written {
return nil
}
err = f.pw.Close()
if err != nil {
return fmt.Errorf("arrow/ipc: could not close payload writer: %w", err)
}
f.footer.written = true
return nil
}
func (f *FileWriter) Write(rec arrow.Record) error {
schema := rec.Schema()
if schema == nil || !schema.Equal(f.schema) {
return errInconsistentSchema
}
if err := f.checkStarted(); err != nil {
return fmt.Errorf("arrow/ipc: could not write header: %w", err)
}
const allow64b = true
var (
data = Payload{msg: MessageRecordBatch}
enc = newRecordEncoder(f.mem, 0, kMaxNestingDepth, allow64b, f.codec, f.compressNP, f.minSpaceSavings)
)
defer data.Release()
err := writeDictionaryPayloads(f.mem, rec, true, false, &f.mapper, f.lastWrittenDicts, f.pw, enc)
if err != nil {
return fmt.Errorf("arrow/ipc: failure writing dictionary batches: %w", err)
}
enc.reset()
if err := enc.Encode(&data, rec); err != nil {
return fmt.Errorf("arrow/ipc: could not encode record to payload: %w", err)
}
return f.pw.WritePayload(data)
}
func (f *FileWriter) checkStarted() error {
if !f.header.started {
return f.start()
}
return nil
}
func (f *FileWriter) start() error {
f.header.started = true
err := f.pw.Start()
if err != nil {
return err
}
f.mapper.ImportSchema(f.schema)
f.lastWrittenDicts = make(map[int64]arrow.Array)
// write out schema payloads
ps := payloadFromSchema(f.schema, f.mem, &f.mapper)
defer ps.Release()
for _, data := range ps {
err = f.pw.WritePayload(data)
if err != nil {
return err
}
}
return nil
}