You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

52 lines
1.6 KiB

  1. package idx
  2. import (
  3. "io"
  4. "github.com/chrislusf/seaweedfs/weed/glog"
  5. "github.com/chrislusf/seaweedfs/weed/storage/types"
  6. "github.com/chrislusf/seaweedfs/weed/util"
  7. )
  8. // walks through the index file, calls fn function with each key, offset, size
  9. // stops with the error returned by the fn function
  10. func WalkIndexFile(r io.ReaderAt, fn func(key types.NeedleId, offset types.Offset, size uint32) error) error {
  11. var readerOffset int64
  12. bytes := make([]byte, types.NeedleMapEntrySize*RowsToRead)
  13. count, e := r.ReadAt(bytes, readerOffset)
  14. glog.V(3).Infof("readerOffset %d count %d err: %v", readerOffset, count, e)
  15. readerOffset += int64(count)
  16. var (
  17. key types.NeedleId
  18. offset types.Offset
  19. size uint32
  20. i int
  21. )
  22. for count > 0 && e == nil || e == io.EOF {
  23. for i = 0; i+types.NeedleMapEntrySize <= count; i += types.NeedleMapEntrySize {
  24. key, offset, size = IdxFileEntry(bytes[i : i+types.NeedleMapEntrySize])
  25. if e = fn(key, offset, size); e != nil {
  26. return e
  27. }
  28. }
  29. if e == io.EOF {
  30. return nil
  31. }
  32. count, e = r.ReadAt(bytes, readerOffset)
  33. glog.V(3).Infof("readerOffset %d count %d err: %v", readerOffset, count, e)
  34. readerOffset += int64(count)
  35. }
  36. return e
  37. }
  38. func IdxFileEntry(bytes []byte) (key types.NeedleId, offset types.Offset, size uint32) {
  39. key = types.BytesToNeedleId(bytes[:types.NeedleIdSize])
  40. offset = types.BytesToOffset(bytes[types.NeedleIdSize : types.NeedleIdSize+types.OffsetSize])
  41. size = util.BytesToUint32(bytes[types.NeedleIdSize+types.OffsetSize : types.NeedleIdSize+types.OffsetSize+types.SizeSize])
  42. return
  43. }
  44. const (
  45. RowsToRead = 1024
  46. )