zeroshade commented on code in PR #216:
URL: https://github.com/apache/arrow-go/pull/216#discussion_r1887443233
##########
arrow/ipc/file_reader.go:
##########
@@ -33,16 +33,166 @@ import (
"github.com/apache/arrow-go/v18/arrow/memory"
)
-// FileReader is an Arrow file reader.
-type FileReader struct {
+type readerImpl interface {
+ getFooterEnd() (int64, error)
+ readFooter(*footerBlock) error
+ dict(memory.Allocator, *footerBlock, int) (dataBlock, error)
+ block(memory.Allocator, *footerBlock, int) (dataBlock, error)
+}
+
+type footerBlock struct {
+ offset int64
+ buffer *memory.Buffer
+ data *flatbuf.Footer
+}
+
+type dataBlock interface {
+ Offset() int64
+ Meta() int32
+ Body() int64
+ NewMessage() (*Message, error)
+}
+
+type basicReaderImpl struct {
r ReadAtSeeker
+}
- footer struct {
- offset int64
- buffer *memory.Buffer
- data *flatbuf.Footer
+func (r *basicReaderImpl) getFooterEnd() (int64, error) {
+ return r.r.Seek(0, io.SeekEnd)
+}
+
+func (r *basicReaderImpl) readFooter(f *footerBlock) error {
+ var err error
+
+ if f.offset <= int64(len(Magic)*2+4) {
+ return fmt.Errorf("arrow/ipc: file too small (size=%d)",
f.offset)
+ }
+
+ eof := int64(len(Magic) + 4)
+ buf := make([]byte, eof)
+ n, err := r.r.ReadAt(buf, f.offset-eof)
+ if err != nil {
+ return fmt.Errorf("arrow/ipc: could not read footer: %w", err)
+ }
+ if n != len(buf) {
+ return fmt.Errorf("arrow/ipc: could not read %d bytes from end
of file", len(buf))
+ }
+
+ if !bytes.Equal(buf[4:], Magic) {
+ return errNotArrowFile
+ }
+
+ size := int64(binary.LittleEndian.Uint32(buf[:4]))
+ if size <= 0 || size+int64(len(Magic)*2+4) > f.offset {
+ return errInconsistentFileMetadata
+ }
+
+ buf = make([]byte, size)
+ n, err = r.r.ReadAt(buf, f.offset-size-eof)
+ if err != nil {
+ return fmt.Errorf("arrow/ipc: could not read footer data: %w",
err)
+ }
+ if n != len(buf) {
+ return fmt.Errorf("arrow/ipc: could not read %d bytes from
footer data", len(buf))
+ }
+
+ f.buffer = memory.NewBufferBytes(buf)
+ f.data = flatbuf.GetRootAsFooter(buf, 0)
+ return err
+}
+
+func (r *basicReaderImpl) block(mem memory.Allocator, f *footerBlock, i int)
(dataBlock, error) {
+ var blk flatbuf.Block
+ if !f.data.RecordBatches(&blk, i) {
+ return fileBlock{}, fmt.Errorf("arrow/ipc: could not extract
file block %d", i)
}
+ return fileBlock{
+ offset: blk.Offset(),
+ meta: blk.MetaDataLength(),
+ body: blk.BodyLength(),
+ r: r.r,
+ mem: mem,
+ }, nil
+}
+
+func (r *basicReaderImpl) dict(mem memory.Allocator, f *footerBlock, i int)
(dataBlock, error) {
+ var blk flatbuf.Block
+ if !f.data.Dictionaries(&blk, i) {
+ return fileBlock{}, fmt.Errorf("arrow/ipc: could not extract
dictionary block %d", i)
+ }
+
+ return fileBlock{
+ offset: blk.Offset(),
+ meta: blk.MetaDataLength(),
+ body: blk.BodyLength(),
+ r: r.r,
+ mem: mem,
+ }, nil
+}
+
+type mappedReaderImpl struct {
+ data []byte
+}
+
+func (r *mappedReaderImpl) getFooterEnd() (int64, error) { return
int64(len(r.data)), nil }
+
+func (r *mappedReaderImpl) readFooter(f *footerBlock) error {
Review Comment:
updated by adding a `getBytes` method to the impls and having a single
implementation for `readFooter` that uses it, unifying the implementations.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]