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.

59 lines
1.7 KiB

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