lidavidm commented on code in PR #216:
URL: https://github.com/apache/arrow-go/pull/216#discussion_r1885983013


##########
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]))

Review Comment:
   I think this is because Golang only has the unsigned versions, expecting you 
to convert to signed integer yourself
   
   https://pkg.go.dev/encoding/binary#ByteOrder



##########
arrow/ipc/file_reader.go:
##########
@@ -388,34 +498,23 @@ func (src *ipcSource) buffer(i int) *memory.Buffer {
                return memory.NewBufferBytes(nil)
        }
 
-       raw := memory.NewResizableBuffer(src.mem)
+       var raw *memory.Buffer
        if src.codec == nil {
-               raw.Resize(int(buf.Length()))
-               _, err := src.r.ReadAt(raw.Bytes(), buf.Offset())
-               if err != nil {
-                       panic(err)
-               }
+               raw = memory.SliceBuffer(src.rawBytes, int(buf.Offset()), 
int(buf.Length()))
        } else {
-               sr := io.NewSectionReader(src.r, buf.Offset(), buf.Length())
-               var uncompressedSize uint64
-
-               err := binary.Read(sr, binary.LittleEndian, &uncompressedSize)
-               if err != nil {
-                       panic(err)
-               }
+               body := src.rawBytes.Bytes()[buf.Offset() : 
buf.Offset()+buf.Length()]
+               uncompressedSize := binary.LittleEndian.Uint64(body[:8])
 
-               var r io.Reader = sr
                // check for an uncompressed buffer
                if int64(uncompressedSize) != -1 {

Review Comment:
   nit: while this is existing code, maybe it would be safer to have 
`uncompressedSize := int64(...)` so you don't have to remember to convert it on 
use and so it's consistent with above



-- 
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]

Reply via email to