needle_write.go 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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. }
  128. return offset, size, actualSize, err
  129. }
  130. func WriteNeedleBlob(w backend.BackendStorageFile, dataSlice []byte, size Size, appendAtNs uint64, version Version) (offset uint64, err error) {
  131. if end, _, e := w.GetStat(); e == nil {
  132. defer func(w backend.BackendStorageFile, off int64) {
  133. if err != nil {
  134. if te := w.Truncate(end); te != nil {
  135. glog.V(0).Infof("Failed to truncate %s back to %d with error: %v", w.Name(), end, te)
  136. }
  137. }
  138. }(w, end)
  139. offset = uint64(end)
  140. } else {
  141. err = fmt.Errorf("Cannot Read Current Volume Position: %v", e)
  142. return
  143. }
  144. if version == Version3 {
  145. tsOffset := NeedleHeaderSize + size + NeedleChecksumSize
  146. util.Uint64toBytes(dataSlice[tsOffset:tsOffset+TimestampSize], appendAtNs)
  147. }
  148. if err == nil {
  149. _, err = w.WriteAt(dataSlice, int64(offset))
  150. }
  151. return
  152. }