idx: Switch WalkIndexFile to accept generic io.ReaderAt

This is necessary to use it with in-memory idx files.
This commit is contained in:
James Hartig 2020-07-22 11:38:48 -04:00
parent 7a7e70b6cc
commit 0be3bb4a87

View File

@ -11,11 +11,17 @@ import (
// walks through the index file, calls fn function with each key, offset, size // walks through the index file, calls fn function with each key, offset, size
// stops with the error returned by the fn function // stops with the error returned by the fn function
func WalkIndexFile(r *os.File, fn func(key types.NeedleId, offset types.Offset, size uint32) error) error { func WalkIndexFile(r io.ReaderAt, fn func(key types.NeedleId, offset types.Offset, size uint32) error) error {
var readerOffset int64 var readerOffset int64
bytes := make([]byte, types.NeedleMapEntrySize*RowsToRead) bytes := make([]byte, types.NeedleMapEntrySize*RowsToRead)
count, e := r.ReadAt(bytes, readerOffset) count, e := r.ReadAt(bytes, readerOffset)
glog.V(3).Infoln("file", r.Name(), "readerOffset", readerOffset, "count", count, "e", e) var name string
if f, ok := r.(*os.File); ok {
name = f.Name()
} else {
name = "memory"
}
glog.V(3).Infoln("file", name, "readerOffset", readerOffset, "count", count, "e", e)
readerOffset += int64(count) readerOffset += int64(count)
var ( var (
key types.NeedleId key types.NeedleId
@ -35,7 +41,7 @@ func WalkIndexFile(r *os.File, fn func(key types.NeedleId, offset types.Offset,
return nil return nil
} }
count, e = r.ReadAt(bytes, readerOffset) count, e = r.ReadAt(bytes, readerOffset)
glog.V(3).Infoln("file", r.Name(), "readerOffset", readerOffset, "count", count, "e", e) glog.V(3).Infoln("file", name, "readerOffset", readerOffset, "count", count, "e", e)
readerOffset += int64(count) readerOffset += int64(count)
} }
return e return e