From 0be3bb4a87a511bc7d46af67ab82d8ec07481b4b Mon Sep 17 00:00:00 2001 From: James Hartig Date: Wed, 22 Jul 2020 11:38:48 -0400 Subject: [PATCH] idx: Switch WalkIndexFile to accept generic io.ReaderAt This is necessary to use it with in-memory idx files. --- weed/storage/idx/walk.go | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/weed/storage/idx/walk.go b/weed/storage/idx/walk.go index 90efb75e6..f785ec706 100644 --- a/weed/storage/idx/walk.go +++ b/weed/storage/idx/walk.go @@ -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