2019-05-22 05:41:20 +00:00
|
|
|
package idx
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io"
|
|
|
|
|
2022-07-29 07:17:28 +00:00
|
|
|
"github.com/seaweedfs/seaweedfs/weed/glog"
|
|
|
|
"github.com/seaweedfs/seaweedfs/weed/storage/types"
|
2019-05-22 05:41:20 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// walks through the index file, calls fn function with each key, offset, size
|
|
|
|
// stops with the error returned by the fn function
|
2022-07-20 01:26:06 +00:00
|
|
|
func WalkIndexFile(r io.ReaderAt, startFrom uint64, fn func(key types.NeedleId, offset types.Offset, size types.Size) error) error {
|
|
|
|
readerOffset := int64(startFrom * types.NeedleMapEntrySize)
|
2022-07-19 01:20:45 +00:00
|
|
|
bytes := make([]byte, types.NeedleMapEntrySize*RowsToRead)
|
|
|
|
count, e := r.ReadAt(bytes, readerOffset)
|
|
|
|
if count == 0 && e == io.EOF {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
glog.V(3).Infof("readerOffset %d count %d err: %v", readerOffset, count, e)
|
2019-05-22 05:41:20 +00:00
|
|
|
readerOffset += int64(count)
|
|
|
|
var (
|
|
|
|
key types.NeedleId
|
|
|
|
offset types.Offset
|
2020-08-19 00:04:28 +00:00
|
|
|
size types.Size
|
2019-05-22 05:41:20 +00:00
|
|
|
i int
|
|
|
|
)
|
|
|
|
|
|
|
|
for count > 0 && e == nil || e == io.EOF {
|
|
|
|
for i = 0; i+types.NeedleMapEntrySize <= count; i += types.NeedleMapEntrySize {
|
|
|
|
key, offset, size = IdxFileEntry(bytes[i : i+types.NeedleMapEntrySize])
|
|
|
|
if e = fn(key, offset, size); e != nil {
|
|
|
|
return e
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if e == io.EOF {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
count, e = r.ReadAt(bytes, readerOffset)
|
2020-07-22 23:11:17 +00:00
|
|
|
glog.V(3).Infof("readerOffset %d count %d err: %v", readerOffset, count, e)
|
2019-05-22 05:41:20 +00:00
|
|
|
readerOffset += int64(count)
|
|
|
|
}
|
|
|
|
return e
|
|
|
|
}
|
|
|
|
|
2020-08-19 00:04:28 +00:00
|
|
|
func IdxFileEntry(bytes []byte) (key types.NeedleId, offset types.Offset, size types.Size) {
|
2019-05-22 05:41:20 +00:00
|
|
|
key = types.BytesToNeedleId(bytes[:types.NeedleIdSize])
|
|
|
|
offset = types.BytesToOffset(bytes[types.NeedleIdSize : types.NeedleIdSize+types.OffsetSize])
|
2020-08-19 00:04:28 +00:00
|
|
|
size = types.BytesToSize(bytes[types.NeedleIdSize+types.OffsetSize : types.NeedleIdSize+types.OffsetSize+types.SizeSize])
|
2019-05-22 05:41:20 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
const (
|
|
|
|
RowsToRead = 1024
|
|
|
|
)
|