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.

88 lines
2.5 KiB

  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. if size.IsValid() {
  62. index, err = n.readNeedleDataVersion2NonData(metaSlice)
  63. }
  64. n.Checksum = CRC(util.BytesToUint32(metaSlice[index : index+NeedleChecksumSize]))
  65. if version == Version3 {
  66. n.AppendAtNs = util.BytesToUint64(metaSlice[index+NeedleChecksumSize : index+NeedleChecksumSize+TimestampSize])
  67. }
  68. return err
  69. }
  70. func min(x, y int64) int64 {
  71. if x < y {
  72. return x
  73. }
  74. return y
  75. }