needle_read_page.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. package needle
  2. import (
  3. "fmt"
  4. "github.com/seaweedfs/seaweedfs/weed/glog"
  5. "github.com/seaweedfs/seaweedfs/weed/storage/backend"
  6. . "github.com/seaweedfs/seaweedfs/weed/storage/types"
  7. "github.com/seaweedfs/seaweedfs/weed/util"
  8. "io"
  9. )
  10. // ReadNeedleData uses a needle without n.Data to read the content
  11. // volumeOffset: the offset within the volume
  12. // needleOffset: the offset within the needle Data
  13. func (n *Needle) ReadNeedleData(r backend.BackendStorageFile, volumeOffset int64, data []byte, needleOffset int64) (count int, err error) {
  14. sizeToRead := min(int64(len(data)), int64(n.DataSize)-needleOffset)
  15. if sizeToRead <= 0 {
  16. return 0, io.EOF
  17. }
  18. startOffset := volumeOffset + NeedleHeaderSize + DataSizeSize + needleOffset
  19. count, err = r.ReadAt(data[:sizeToRead], startOffset)
  20. if err != nil {
  21. fileSize, _, _ := r.GetStat()
  22. glog.Errorf("%s read %d %d size %d at offset %d fileSize %d: %v", r.Name(), n.Id, needleOffset, sizeToRead, volumeOffset, fileSize, err)
  23. }
  24. return
  25. }
  26. // ReadNeedleMeta fills all metadata except the n.Data
  27. func (n *Needle) ReadNeedleMeta(r backend.BackendStorageFile, offset int64, size Size, version Version) (err error) {
  28. defer func() {
  29. if r := recover(); r != nil {
  30. err = fmt.Errorf("panic occurred: %+v", r)
  31. }
  32. }()
  33. bytes := make([]byte, NeedleHeaderSize+DataSizeSize)
  34. count, err := r.ReadAt(bytes, offset)
  35. if count != NeedleHeaderSize+DataSizeSize || err != nil {
  36. return err
  37. }
  38. n.ParseNeedleHeader(bytes)
  39. if n.Size != size {
  40. if OffsetSize == 4 && offset < int64(MaxPossibleVolumeSize) {
  41. return ErrorSizeMismatch
  42. }
  43. }
  44. n.DataSize = util.BytesToUint32(bytes[NeedleHeaderSize : NeedleHeaderSize+DataSizeSize])
  45. startOffset := offset + NeedleHeaderSize
  46. if size.IsValid() {
  47. startOffset = offset + NeedleHeaderSize + DataSizeSize + int64(n.DataSize)
  48. }
  49. dataSize := GetActualSize(size, version)
  50. stopOffset := offset + dataSize
  51. metaSize := stopOffset - startOffset
  52. metaSlice := make([]byte, int(metaSize))
  53. count, err = r.ReadAt(metaSlice, startOffset)
  54. if err != nil && int64(count) == metaSize {
  55. err = nil
  56. }
  57. if err != nil {
  58. return err
  59. }
  60. var index int
  61. index, err = n.readNeedleDataVersion2NonData(metaSlice)
  62. n.Checksum = CRC(util.BytesToUint32(metaSlice[index : index+NeedleChecksumSize]))
  63. if version == Version3 {
  64. n.AppendAtNs = util.BytesToUint64(metaSlice[index+NeedleChecksumSize : index+NeedleChecksumSize+TimestampSize])
  65. }
  66. return err
  67. }
  68. func min(x, y int64) int64 {
  69. if x < y {
  70. return x
  71. }
  72. return y
  73. }