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
// 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
bytes := make([]byte, types.NeedleMapEntrySize*RowsToRead)
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)
var (
key types.NeedleId
@ -35,7 +41,7 @@ func WalkIndexFile(r *os.File, fn func(key types.NeedleId, offset types.Offset,
return nil
}
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)
}
return e