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.

171 lines
5.2 KiB

  1. package needle
  2. import (
  3. "bytes"
  4. "fmt"
  5. "github.com/seaweedfs/seaweedfs/weed/glog"
  6. "github.com/seaweedfs/seaweedfs/weed/storage/backend"
  7. . "github.com/seaweedfs/seaweedfs/weed/storage/types"
  8. "github.com/seaweedfs/seaweedfs/weed/util"
  9. "github.com/seaweedfs/seaweedfs/weed/util/buffer_pool"
  10. "math"
  11. )
  12. func (n *Needle) prepareWriteBuffer(version Version, writeBytes *bytes.Buffer) (Size, int64, error) {
  13. writeBytes.Reset()
  14. switch version {
  15. case Version1:
  16. header := make([]byte, NeedleHeaderSize)
  17. CookieToBytes(header[0:CookieSize], n.Cookie)
  18. NeedleIdToBytes(header[CookieSize:CookieSize+NeedleIdSize], n.Id)
  19. n.Size = Size(len(n.Data))
  20. SizeToBytes(header[CookieSize+NeedleIdSize:CookieSize+NeedleIdSize+SizeSize], n.Size)
  21. size := n.Size
  22. actualSize := NeedleHeaderSize + int64(n.Size)
  23. writeBytes.Write(header)
  24. writeBytes.Write(n.Data)
  25. padding := PaddingLength(n.Size, version)
  26. util.Uint32toBytes(header[0:NeedleChecksumSize], uint32(n.Checksum))
  27. writeBytes.Write(header[0 : NeedleChecksumSize+padding])
  28. return size, actualSize, nil
  29. case Version2, Version3:
  30. header := make([]byte, NeedleHeaderSize+TimestampSize) // adding timestamp to reuse it and avoid extra allocation
  31. CookieToBytes(header[0:CookieSize], n.Cookie)
  32. NeedleIdToBytes(header[CookieSize:CookieSize+NeedleIdSize], n.Id)
  33. if len(n.Name) >= math.MaxUint8 {
  34. n.NameSize = math.MaxUint8
  35. } else {
  36. n.NameSize = uint8(len(n.Name))
  37. }
  38. n.DataSize, n.MimeSize = uint32(len(n.Data)), uint8(len(n.Mime))
  39. if n.DataSize > 0 {
  40. n.Size = 4 + Size(n.DataSize) + 1
  41. if n.HasName() {
  42. n.Size = n.Size + 1 + Size(n.NameSize)
  43. }
  44. if n.HasMime() {
  45. n.Size = n.Size + 1 + Size(n.MimeSize)
  46. }
  47. if n.HasLastModifiedDate() {
  48. n.Size = n.Size + LastModifiedBytesLength
  49. }
  50. if n.HasTtl() {
  51. n.Size = n.Size + TtlBytesLength
  52. }
  53. if n.HasPairs() {
  54. n.Size += 2 + Size(n.PairsSize)
  55. }
  56. } else {
  57. n.Size = 0
  58. }
  59. SizeToBytes(header[CookieSize+NeedleIdSize:CookieSize+NeedleIdSize+SizeSize], n.Size)
  60. writeBytes.Write(header[0:NeedleHeaderSize])
  61. if n.DataSize > 0 {
  62. util.Uint32toBytes(header[0:4], n.DataSize)
  63. writeBytes.Write(header[0:4])
  64. writeBytes.Write(n.Data)
  65. util.Uint8toBytes(header[0:1], n.Flags)
  66. writeBytes.Write(header[0:1])
  67. if n.HasName() {
  68. util.Uint8toBytes(header[0:1], n.NameSize)
  69. writeBytes.Write(header[0:1])
  70. writeBytes.Write(n.Name[:n.NameSize])
  71. }
  72. if n.HasMime() {
  73. util.Uint8toBytes(header[0:1], n.MimeSize)
  74. writeBytes.Write(header[0:1])
  75. writeBytes.Write(n.Mime)
  76. }
  77. if n.HasLastModifiedDate() {
  78. util.Uint64toBytes(header[0:8], n.LastModified)
  79. writeBytes.Write(header[8-LastModifiedBytesLength : 8])
  80. }
  81. if n.HasTtl() && n.Ttl != nil {
  82. n.Ttl.ToBytes(header[0:TtlBytesLength])
  83. writeBytes.Write(header[0:TtlBytesLength])
  84. }
  85. if n.HasPairs() {
  86. util.Uint16toBytes(header[0:2], n.PairsSize)
  87. writeBytes.Write(header[0:2])
  88. writeBytes.Write(n.Pairs)
  89. }
  90. }
  91. padding := PaddingLength(n.Size, version)
  92. util.Uint32toBytes(header[0:NeedleChecksumSize], uint32(n.Checksum))
  93. if version == Version2 {
  94. writeBytes.Write(header[0 : NeedleChecksumSize+padding])
  95. } else {
  96. // version3
  97. util.Uint64toBytes(header[NeedleChecksumSize:NeedleChecksumSize+TimestampSize], n.AppendAtNs)
  98. writeBytes.Write(header[0 : NeedleChecksumSize+TimestampSize+padding])
  99. }
  100. return Size(n.DataSize), GetActualSize(n.Size, version), nil
  101. }
  102. return 0, 0, fmt.Errorf("Unsupported Version! (%d)", version)
  103. }
  104. func (n *Needle) Append(w backend.BackendStorageFile, version Version) (offset uint64, size Size, actualSize int64, err error) {
  105. if end, _, e := w.GetStat(); e == nil {
  106. defer func(w backend.BackendStorageFile, off int64) {
  107. if err != nil {
  108. if te := w.Truncate(end); te != nil {
  109. glog.V(0).Infof("Failed to truncate %s back to %d with error: %v", w.Name(), end, te)
  110. }
  111. }
  112. }(w, end)
  113. offset = uint64(end)
  114. } else {
  115. err = fmt.Errorf("Cannot Read Current Volume Position: %v", e)
  116. return
  117. }
  118. if offset >= MaxPossibleVolumeSize && len(n.Data) != 0 {
  119. err = fmt.Errorf("Volume Size %d Exceeded %d", offset, MaxPossibleVolumeSize)
  120. return
  121. }
  122. bytesBuffer := buffer_pool.SyncPoolGetBuffer()
  123. defer buffer_pool.SyncPoolPutBuffer(bytesBuffer)
  124. size, actualSize, err = n.prepareWriteBuffer(version, bytesBuffer)
  125. if err == nil {
  126. _, err = w.WriteAt(bytesBuffer.Bytes(), int64(offset))
  127. if err != nil {
  128. err = fmt.Errorf("failed to write %d bytes to %s at offset %d: %v", actualSize, w.Name(), offset, err)
  129. }
  130. }
  131. return offset, size, actualSize, err
  132. }
  133. func WriteNeedleBlob(w backend.BackendStorageFile, dataSlice []byte, size Size, appendAtNs uint64, version Version) (offset uint64, err error) {
  134. if end, _, e := w.GetStat(); e == nil {
  135. defer func(w backend.BackendStorageFile, off int64) {
  136. if err != nil {
  137. if te := w.Truncate(end); te != nil {
  138. glog.V(0).Infof("Failed to truncate %s back to %d with error: %v", w.Name(), end, te)
  139. }
  140. }
  141. }(w, end)
  142. offset = uint64(end)
  143. } else {
  144. err = fmt.Errorf("Cannot Read Current Volume Position: %v", e)
  145. return
  146. }
  147. if version == Version3 {
  148. tsOffset := NeedleHeaderSize + size + NeedleChecksumSize
  149. util.Uint64toBytes(dataSlice[tsOffset:tsOffset+TimestampSize], appendAtNs)
  150. }
  151. if err == nil {
  152. _, err = w.WriteAt(dataSlice, int64(offset))
  153. }
  154. return
  155. }