forked from segmentio/parquet-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
file.go
739 lines (629 loc) · 21 KB
/
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
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
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
package parquet
import (
"bufio"
"encoding/binary"
"fmt"
"hash/crc32"
"io"
"sort"
"sync"
"github.com/segmentio/encoding/thrift"
"github.com/segmentio/parquet-go/format"
)
const (
defaultDictBufferSize = 8192
defaultReadBufferSize = 4096
)
// File represents a parquet file. The layout of a Parquet file can be found
// here: https://github.com/apache/parquet-format#file-format
type File struct {
metadata format.FileMetaData
protocol thrift.CompactProtocol
reader io.ReaderAt
size int64
schema *Schema
root *Column
columnIndexes []format.ColumnIndex
offsetIndexes []format.OffsetIndex
rowGroups []RowGroup
config *FileConfig
}
// OpenFile opens a parquet file and reads the content between offset 0 and the given
// size in r.
//
// Only the parquet magic bytes and footer are read, column chunks and other
// parts of the file are left untouched; this means that successfully opening
// a file does not validate that the pages have valid checksums.
func OpenFile(r io.ReaderAt, size int64, options ...FileOption) (*File, error) {
b := make([]byte, 8)
c, err := NewFileConfig(options...)
if err != nil {
return nil, err
}
f := &File{reader: r, size: size, config: c}
if _, err := r.ReadAt(b[:4], 0); err != nil {
return nil, fmt.Errorf("reading magic header of parquet file: %w", err)
}
if string(b[:4]) != "PAR1" {
return nil, fmt.Errorf("invalid magic header of parquet file: %q", b[:4])
}
if cast, ok := f.reader.(interface{ SetMagicFooterSection(offset, length int64) }); ok {
cast.SetMagicFooterSection(size-8, 8)
}
if n, err := r.ReadAt(b[:8], size-8); n != 8 {
return nil, fmt.Errorf("reading magic footer of parquet file: %w", err)
}
if string(b[4:8]) != "PAR1" {
return nil, fmt.Errorf("invalid magic footer of parquet file: %q", b[4:8])
}
footerSize := int64(binary.LittleEndian.Uint32(b[:4]))
footerData := make([]byte, footerSize)
if cast, ok := f.reader.(interface{ SetFooterSection(offset, length int64) }); ok {
cast.SetFooterSection(size-(footerSize+8), footerSize)
}
if _, err := f.reader.ReadAt(footerData, size-(footerSize+8)); err != nil {
return nil, fmt.Errorf("reading footer of parquet file: %w", err)
}
if err := thrift.Unmarshal(&f.protocol, footerData, &f.metadata); err != nil {
return nil, fmt.Errorf("reading parquet file metadata: %w", err)
}
if len(f.metadata.Schema) == 0 {
return nil, ErrMissingRootColumn
}
if !c.SkipPageIndex {
if f.columnIndexes, f.offsetIndexes, err = f.ReadPageIndex(); err != nil {
return nil, fmt.Errorf("reading page index of parquet file: %w", err)
}
}
if f.root, err = openColumns(f); err != nil {
return nil, fmt.Errorf("opening columns of parquet file: %w", err)
}
schema := NewSchema(f.root.Name(), f.root)
columns := make([]*Column, 0, numLeafColumnsOf(f.root))
f.schema = schema
f.root.forEachLeaf(func(c *Column) { columns = append(columns, c) })
rowGroups := make([]fileRowGroup, len(f.metadata.RowGroups))
for i := range rowGroups {
rowGroups[i].init(f, schema, columns, &f.metadata.RowGroups[i])
}
f.rowGroups = make([]RowGroup, len(rowGroups))
for i := range rowGroups {
f.rowGroups[i] = &rowGroups[i]
}
if !c.SkipBloomFilters {
section := io.NewSectionReader(r, 0, size)
rbuf, rbufpool := getBufioReader(section, c.ReadBufferSize)
defer putBufioReader(rbuf, rbufpool)
header := format.BloomFilterHeader{}
compact := thrift.CompactProtocol{}
decoder := thrift.NewDecoder(compact.NewReader(rbuf))
for i := range rowGroups {
g := &rowGroups[i]
for j := range g.columns {
c := g.columns[j].(*fileColumnChunk)
if offset := c.chunk.MetaData.BloomFilterOffset; offset > 0 {
section.Seek(offset, io.SeekStart)
rbuf.Reset(section)
header = format.BloomFilterHeader{}
if err := decoder.Decode(&header); err != nil {
return nil, fmt.Errorf("decoding bloom filter header: %w", err)
}
offset, _ = section.Seek(0, io.SeekCurrent)
offset -= int64(rbuf.Buffered())
if cast, ok := r.(interface{ SetBloomFilterSection(offset, length int64) }); ok {
bloomFilterOffset := c.chunk.MetaData.BloomFilterOffset
bloomFilterLength := (offset - bloomFilterOffset) + int64(header.NumBytes)
cast.SetBloomFilterSection(bloomFilterOffset, bloomFilterLength)
}
c.bloomFilter = newBloomFilter(r, offset, &header)
}
}
}
}
sortKeyValueMetadata(f.metadata.KeyValueMetadata)
return f, nil
}
// ReadPageIndex reads the page index section of the parquet file f.
//
// If the file did not contain a page index, the method returns two empty slices
// and a nil error.
//
// Only leaf columns have indexes, the returned indexes are arranged using the
// following layout:
//
// - -------------- +
// | col 0: chunk 0 |
// - -------------- +
// | col 1: chunk 0 |
// - -------------- +
// | ... |
// - -------------- +
// | col 0: chunk 1 |
// - -------------- +
// | col 1: chunk 1 |
// - -------------- +
// | ... |
// - -------------- +
//
// This method is useful in combination with the SkipPageIndex option to delay
// reading the page index section until after the file was opened. Note that in
// this case the page index is not cached within the file, programs are expected
// to make use of independently from the parquet package.
func (f *File) ReadPageIndex() ([]format.ColumnIndex, []format.OffsetIndex, error) {
if len(f.metadata.RowGroups) == 0 {
return nil, nil, nil
}
columnIndexOffset := f.metadata.RowGroups[0].Columns[0].ColumnIndexOffset
offsetIndexOffset := f.metadata.RowGroups[0].Columns[0].OffsetIndexOffset
columnIndexLength := int64(0)
offsetIndexLength := int64(0)
forEachColumnChunk := func(do func(int, int, *format.ColumnChunk) error) error {
for i := range f.metadata.RowGroups {
for j := range f.metadata.RowGroups[i].Columns {
c := &f.metadata.RowGroups[i].Columns[j]
if err := do(i, j, c); err != nil {
return err
}
}
}
return nil
}
forEachColumnChunk(func(_, _ int, c *format.ColumnChunk) error {
columnIndexLength += int64(c.ColumnIndexLength)
offsetIndexLength += int64(c.OffsetIndexLength)
return nil
})
if columnIndexLength == 0 && offsetIndexLength == 0 {
return nil, nil, nil
}
numRowGroups := len(f.metadata.RowGroups)
numColumns := len(f.metadata.RowGroups[0].Columns)
numColumnChunks := numRowGroups * numColumns
columnIndexes := make([]format.ColumnIndex, numColumnChunks)
offsetIndexes := make([]format.OffsetIndex, numColumnChunks)
indexBuffer := make([]byte, max(int(columnIndexLength), int(offsetIndexLength)))
if columnIndexOffset > 0 {
columnIndexData := indexBuffer[:columnIndexLength]
if cast, ok := f.reader.(interface{ SetColumnIndexSection(offset, length int64) }); ok {
cast.SetColumnIndexSection(columnIndexOffset, columnIndexLength)
}
if _, err := f.reader.ReadAt(columnIndexData, columnIndexOffset); err != nil {
return nil, nil, fmt.Errorf("reading %d bytes column index at offset %d: %w", columnIndexLength, columnIndexOffset, err)
}
err := forEachColumnChunk(func(i, j int, c *format.ColumnChunk) error {
// Some parquet files are missing the column index on some columns.
//
// An example of this file is testdata/alltypes_tiny_pages_plain.parquet
// which was added in https://github.com/apache/parquet-testing/pull/24.
if c.ColumnIndexOffset > 0 {
offset := c.ColumnIndexOffset - columnIndexOffset
length := int64(c.ColumnIndexLength)
buffer := columnIndexData[offset : offset+length]
if err := thrift.Unmarshal(&f.protocol, buffer, &columnIndexes[(i*numColumns)+j]); err != nil {
return fmt.Errorf("decoding column index: rowGroup=%d columnChunk=%d/%d: %w", i, j, numColumns, err)
}
}
return nil
})
if err != nil {
return nil, nil, err
}
}
if offsetIndexOffset > 0 {
offsetIndexData := indexBuffer[:offsetIndexLength]
if cast, ok := f.reader.(interface{ SetOffsetIndexSection(offset, length int64) }); ok {
cast.SetOffsetIndexSection(offsetIndexOffset, offsetIndexLength)
}
if _, err := f.reader.ReadAt(offsetIndexData, offsetIndexOffset); err != nil {
return nil, nil, fmt.Errorf("reading %d bytes offset index at offset %d: %w", offsetIndexLength, offsetIndexOffset, err)
}
err := forEachColumnChunk(func(i, j int, c *format.ColumnChunk) error {
if c.OffsetIndexOffset > 0 {
offset := c.OffsetIndexOffset - offsetIndexOffset
length := int64(c.OffsetIndexLength)
buffer := offsetIndexData[offset : offset+length]
if err := thrift.Unmarshal(&f.protocol, buffer, &offsetIndexes[(i*numColumns)+j]); err != nil {
return fmt.Errorf("decoding column index: rowGroup=%d columnChunk=%d/%d: %w", i, j, numColumns, err)
}
}
return nil
})
if err != nil {
return nil, nil, err
}
}
return columnIndexes, offsetIndexes, nil
}
// NumRows returns the number of rows in the file.
func (f *File) NumRows() int64 { return f.metadata.NumRows }
// RowGroups returns the list of row group in the file.
func (f *File) RowGroups() []RowGroup { return f.rowGroups }
// Root returns the root column of f.
func (f *File) Root() *Column { return f.root }
// Schema returns the schema of f.
func (f *File) Schema() *Schema { return f.schema }
// Metadata returns the metadata of f.
func (f *File) Metadata() *format.FileMetaData { return &f.metadata }
// Size returns the size of f (in bytes).
func (f *File) Size() int64 { return f.size }
// ReadAt reads bytes into b from f at the given offset.
//
// The method satisfies the io.ReaderAt interface.
func (f *File) ReadAt(b []byte, off int64) (int, error) {
if off < 0 || off >= f.size {
return 0, io.EOF
}
if limit := f.size - off; limit < int64(len(b)) {
n, err := f.reader.ReadAt(b[:limit], off)
if err == nil {
err = io.EOF
}
return n, err
}
return f.reader.ReadAt(b, off)
}
// ColumnIndexes returns the page index of the parquet file f.
//
// If the file did not contain a column index, the method returns an empty slice
// and nil error.
func (f *File) ColumnIndexes() []format.ColumnIndex { return f.columnIndexes }
// OffsetIndexes returns the page index of the parquet file f.
//
// If the file did not contain an offset index, the method returns an empty
// slice and nil error.
func (f *File) OffsetIndexes() []format.OffsetIndex { return f.offsetIndexes }
// Lookup returns the value associated with the given key in the file key/value
// metadata.
//
// The ok boolean will be true if the key was found, false otherwise.
func (f *File) Lookup(key string) (value string, ok bool) {
return lookupKeyValueMetadata(f.metadata.KeyValueMetadata, key)
}
func (f *File) hasIndexes() bool {
return f.columnIndexes != nil && f.offsetIndexes != nil
}
var _ io.ReaderAt = (*File)(nil)
func sortKeyValueMetadata(keyValueMetadata []format.KeyValue) {
sort.Slice(keyValueMetadata, func(i, j int) bool {
switch {
case keyValueMetadata[i].Key < keyValueMetadata[j].Key:
return true
case keyValueMetadata[i].Key > keyValueMetadata[j].Key:
return false
default:
return keyValueMetadata[i].Value < keyValueMetadata[j].Value
}
})
}
func lookupKeyValueMetadata(keyValueMetadata []format.KeyValue, key string) (value string, ok bool) {
i := sort.Search(len(keyValueMetadata), func(i int) bool {
return keyValueMetadata[i].Key >= key
})
if i == len(keyValueMetadata) || keyValueMetadata[i].Key != key {
return "", false
}
return keyValueMetadata[i].Value, true
}
type fileRowGroup struct {
schema *Schema
rowGroup *format.RowGroup
columns []ColumnChunk
sorting []SortingColumn
}
func (g *fileRowGroup) init(file *File, schema *Schema, columns []*Column, rowGroup *format.RowGroup) {
g.schema = schema
g.rowGroup = rowGroup
g.columns = make([]ColumnChunk, len(rowGroup.Columns))
g.sorting = make([]SortingColumn, len(rowGroup.SortingColumns))
fileColumnChunks := make([]fileColumnChunk, len(rowGroup.Columns))
for i := range g.columns {
fileColumnChunks[i] = fileColumnChunk{
file: file,
column: columns[i],
rowGroup: rowGroup,
chunk: &rowGroup.Columns[i],
}
if file.hasIndexes() {
j := (int(rowGroup.Ordinal) * len(columns)) + i
fileColumnChunks[i].columnIndex = &file.columnIndexes[j]
fileColumnChunks[i].offsetIndex = &file.offsetIndexes[j]
}
g.columns[i] = &fileColumnChunks[i]
}
for i := range g.sorting {
g.sorting[i] = &fileSortingColumn{
column: columns[rowGroup.SortingColumns[i].ColumnIdx],
descending: rowGroup.SortingColumns[i].Descending,
nullsFirst: rowGroup.SortingColumns[i].NullsFirst,
}
}
}
func (g *fileRowGroup) Schema() *Schema { return g.schema }
func (g *fileRowGroup) NumRows() int64 { return g.rowGroup.NumRows }
func (g *fileRowGroup) ColumnChunks() []ColumnChunk { return g.columns }
func (g *fileRowGroup) SortingColumns() []SortingColumn { return g.sorting }
func (g *fileRowGroup) Rows() Rows { return &rowGroupRows{rowGroup: g} }
type fileSortingColumn struct {
column *Column
descending bool
nullsFirst bool
}
func (s *fileSortingColumn) Path() []string { return s.column.Path() }
func (s *fileSortingColumn) Descending() bool { return s.descending }
func (s *fileSortingColumn) NullsFirst() bool { return s.nullsFirst }
type fileColumnChunk struct {
file *File
column *Column
bloomFilter *bloomFilter
rowGroup *format.RowGroup
columnIndex *format.ColumnIndex
offsetIndex *format.OffsetIndex
chunk *format.ColumnChunk
}
func (c *fileColumnChunk) Type() Type {
return c.column.Type()
}
func (c *fileColumnChunk) Column() int {
return int(c.column.Index())
}
func (c *fileColumnChunk) Pages() Pages {
r := new(filePages)
r.init(c)
return r
}
func (c *fileColumnChunk) ColumnIndex() ColumnIndex {
if c.columnIndex == nil {
return nil
}
return fileColumnIndex{c}
}
func (c *fileColumnChunk) OffsetIndex() OffsetIndex {
if c.offsetIndex == nil {
return nil
}
return (*fileOffsetIndex)(c.offsetIndex)
}
func (c *fileColumnChunk) BloomFilter() BloomFilter {
if c.bloomFilter == nil {
return nil
}
return c.bloomFilter
}
func (c *fileColumnChunk) NumValues() int64 {
return c.chunk.MetaData.NumValues
}
type filePages struct {
chunk *fileColumnChunk
rbuf *bufio.Reader
rbufpool *sync.Pool
section io.SectionReader
protocol thrift.CompactProtocol
decoder thrift.Decoder
baseOffset int64
dataOffset int64
dictOffset int64
index int
skip int64
dictionary Dictionary
bufferSize int
}
func (f *filePages) init(c *fileColumnChunk) {
f.chunk = c
f.baseOffset = c.chunk.MetaData.DataPageOffset
f.dataOffset = f.baseOffset
f.bufferSize = c.file.config.ReadBufferSize
if c.chunk.MetaData.DictionaryPageOffset != 0 {
f.baseOffset = c.chunk.MetaData.DictionaryPageOffset
f.dictOffset = f.baseOffset
}
f.section = *io.NewSectionReader(c.file, f.baseOffset, c.chunk.MetaData.TotalCompressedSize)
f.rbuf, f.rbufpool = getBufioReader(&f.section, f.bufferSize)
f.decoder.Reset(f.protocol.NewReader(f.rbuf))
}
func (f *filePages) ReadPage() (Page, error) {
if f.chunk == nil {
return nil, io.EOF
}
for {
header := new(format.PageHeader)
if err := f.decoder.Decode(header); err != nil {
return nil, err
}
data, err := f.readPage(header, f.rbuf)
if err != nil {
return nil, err
}
var page Page
switch header.Type {
case format.DataPageV2:
page, err = f.readDataPageV2(header, data)
case format.DataPage:
page, err = f.readDataPageV1(header, data)
case format.DictionaryPage:
// Sometimes parquet files do not have the dictionary page offset
// recorded in the column metadata. We account for this by lazily
// reading dictionary pages when we encounter them.
err = f.readDictionaryPage(header, data)
default:
err = fmt.Errorf("cannot read values of type %s from page", header.Type)
}
data.unref()
if err != nil {
return nil, fmt.Errorf("decoding page %d of column %q: %w", f.index, f.columnPath(), err)
}
if page == nil {
continue
}
f.index++
if f.skip == 0 {
return page, nil
}
// TODO: what about pages that don't embed the number of rows?
// (data page v1 with no offset index in the column chunk).
numRows := page.NumRows()
if numRows > f.skip {
seek := f.skip
f.skip = 0
if seek > 0 {
page = page.Slice(seek, numRows)
}
return page, nil
}
f.skip -= numRows
}
}
func (f *filePages) readDictionary() error {
chunk := io.NewSectionReader(f.chunk.file, f.baseOffset, f.chunk.chunk.MetaData.TotalCompressedSize)
rbuf, pool := getBufioReader(chunk, f.bufferSize)
defer putBufioReader(rbuf, pool)
decoder := thrift.NewDecoder(f.protocol.NewReader(rbuf))
header := new(format.PageHeader)
if err := decoder.Decode(header); err != nil {
return err
}
page := buffers.get(int(header.CompressedPageSize))
defer page.unref()
if _, err := io.ReadFull(rbuf, page.data); err != nil {
return err
}
return f.readDictionaryPage(header, page)
}
func (f *filePages) readDictionaryPage(header *format.PageHeader, page *buffer) error {
if header.DictionaryPageHeader == nil {
return ErrMissingPageHeader
}
d, err := f.chunk.column.decodeDictionary(DictionaryPageHeader{header.DictionaryPageHeader}, page, header.UncompressedPageSize)
if err != nil {
return err
}
f.dictionary = d
return nil
}
func (f *filePages) readDataPageV1(header *format.PageHeader, page *buffer) (Page, error) {
if header.DataPageHeader == nil {
return nil, ErrMissingPageHeader
}
if isDictionaryFormat(header.DataPageHeader.Encoding) && f.dictionary == nil {
if err := f.readDictionary(); err != nil {
return nil, err
}
}
return f.chunk.column.decodeDataPageV1(DataPageHeaderV1{header.DataPageHeader}, page, f.dictionary, header.UncompressedPageSize)
}
func (f *filePages) readDataPageV2(header *format.PageHeader, page *buffer) (Page, error) {
if header.DataPageHeaderV2 == nil {
return nil, ErrMissingPageHeader
}
if isDictionaryFormat(header.DataPageHeaderV2.Encoding) && f.dictionary == nil {
// If the program seeked to a row passed the first page, the dictionary
// page may not have been seen, in which case we have to lazily load it
// from the beginning of column chunk.
if err := f.readDictionary(); err != nil {
return nil, err
}
}
return f.chunk.column.decodeDataPageV2(DataPageHeaderV2{header.DataPageHeaderV2}, page, f.dictionary, header.UncompressedPageSize)
}
func (f *filePages) readPage(header *format.PageHeader, reader *bufio.Reader) (*buffer, error) {
page := buffers.get(int(header.CompressedPageSize))
defer page.unref()
if _, err := io.ReadFull(reader, page.data); err != nil {
return nil, err
}
if header.CRC != 0 {
headerChecksum := uint32(header.CRC)
bufferChecksum := crc32.ChecksumIEEE(page.data)
if headerChecksum != bufferChecksum {
// The parquet specs indicate that corruption errors could be
// handled gracefully by skipping pages, tho this may not always
// be practical. Depending on how the pages are consumed,
// missing rows may cause unpredictable behaviors in algorithms.
//
// For now, we assume these errors to be fatal, but we may
// revisit later and improve error handling to be more resilient
// to data corruption.
return nil, fmt.Errorf("crc32 checksum mismatch in page of column %q: want=0x%08X got=0x%08X: %w",
f.columnPath(),
headerChecksum,
bufferChecksum,
ErrCorrupted,
)
}
}
page.ref()
return page, nil
}
func (f *filePages) SeekToRow(rowIndex int64) (err error) {
if f.chunk == nil {
return io.ErrClosedPipe
}
if f.chunk.offsetIndex == nil {
_, err = f.section.Seek(f.dataOffset-f.baseOffset, io.SeekStart)
f.skip = rowIndex
f.index = 0
if f.dictOffset > 0 {
f.index = 1
}
} else {
pages := f.chunk.offsetIndex.PageLocations
index := sort.Search(len(pages), func(i int) bool {
return pages[i].FirstRowIndex > rowIndex
}) - 1
if index < 0 {
return ErrSeekOutOfRange
}
_, err = f.section.Seek(pages[index].Offset-f.baseOffset, io.SeekStart)
f.skip = rowIndex - pages[index].FirstRowIndex
f.index = index
}
f.rbuf.Reset(&f.section)
return err
}
func (f *filePages) Close() error {
putBufioReader(f.rbuf, f.rbufpool)
f.chunk = nil
f.section = io.SectionReader{}
f.rbuf = nil
f.rbufpool = nil
f.baseOffset = 0
f.dataOffset = 0
f.dictOffset = 0
f.index = 0
f.skip = 0
f.dictionary = nil
return nil
}
func (f *filePages) columnPath() columnPath {
return columnPath(f.chunk.column.Path())
}
type putBufioReaderFunc func()
var (
bufioReaderPoolLock sync.Mutex
bufioReaderPool = map[int]*sync.Pool{}
)
func getBufioReader(r io.Reader, bufferSize int) (*bufio.Reader, *sync.Pool) {
pool := getBufioReaderPool(bufferSize)
rbuf, _ := pool.Get().(*bufio.Reader)
if rbuf == nil {
rbuf = bufio.NewReaderSize(r, bufferSize)
} else {
rbuf.Reset(r)
}
return rbuf, pool
}
func putBufioReader(rbuf *bufio.Reader, pool *sync.Pool) {
if rbuf != nil && pool != nil {
rbuf.Reset(nil)
pool.Put(rbuf)
}
}
func getBufioReaderPool(size int) *sync.Pool {
bufioReaderPoolLock.Lock()
defer bufioReaderPoolLock.Unlock()
if pool := bufioReaderPool[size]; pool != nil {
return pool
}
pool := &sync.Pool{}
bufioReaderPool[size] = pool
return pool
}