needle_read_write.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  1. package needle
  2. import (
  3. "errors"
  4. "fmt"
  5. "io"
  6. "math"
  7. "github.com/chrislusf/seaweedfs/weed/glog"
  8. "github.com/chrislusf/seaweedfs/weed/storage/backend"
  9. . "github.com/chrislusf/seaweedfs/weed/storage/types"
  10. "github.com/chrislusf/seaweedfs/weed/util"
  11. )
  12. const (
  13. FlagIsCompressed = 0x01
  14. FlagHasName = 0x02
  15. FlagHasMime = 0x04
  16. FlagHasLastModifiedDate = 0x08
  17. FlagHasTtl = 0x10
  18. FlagHasPairs = 0x20
  19. FlagIsChunkManifest = 0x80
  20. LastModifiedBytesLength = 5
  21. TtlBytesLength = 2
  22. )
  23. func (n *Needle) DiskSize(version Version) int64 {
  24. return GetActualSize(n.Size, version)
  25. }
  26. func (n *Needle) prepareWriteBuffer(version Version) ([]byte, uint32, int64, error) {
  27. writeBytes := make([]byte, 0)
  28. switch version {
  29. case Version1:
  30. header := make([]byte, NeedleHeaderSize)
  31. CookieToBytes(header[0:CookieSize], n.Cookie)
  32. NeedleIdToBytes(header[CookieSize:CookieSize+NeedleIdSize], n.Id)
  33. n.Size = uint32(len(n.Data))
  34. util.Uint32toBytes(header[CookieSize+NeedleIdSize:CookieSize+NeedleIdSize+SizeSize], n.Size)
  35. size := n.Size
  36. actualSize := NeedleHeaderSize + int64(n.Size)
  37. writeBytes = append(writeBytes, header...)
  38. writeBytes = append(writeBytes, n.Data...)
  39. padding := PaddingLength(n.Size, version)
  40. util.Uint32toBytes(header[0:NeedleChecksumSize], n.Checksum.Value())
  41. writeBytes = append(writeBytes, header[0:NeedleChecksumSize+padding]...)
  42. return writeBytes, size, actualSize, nil
  43. case Version2, Version3:
  44. header := make([]byte, NeedleHeaderSize+TimestampSize) // adding timestamp to reuse it and avoid extra allocation
  45. CookieToBytes(header[0:CookieSize], n.Cookie)
  46. NeedleIdToBytes(header[CookieSize:CookieSize+NeedleIdSize], n.Id)
  47. if len(n.Name) >= math.MaxUint8 {
  48. n.NameSize = math.MaxUint8
  49. } else {
  50. n.NameSize = uint8(len(n.Name))
  51. }
  52. n.DataSize, n.MimeSize = uint32(len(n.Data)), uint8(len(n.Mime))
  53. if n.DataSize > 0 {
  54. n.Size = 4 + n.DataSize + 1
  55. if n.HasName() {
  56. n.Size = n.Size + 1 + uint32(n.NameSize)
  57. }
  58. if n.HasMime() {
  59. n.Size = n.Size + 1 + uint32(n.MimeSize)
  60. }
  61. if n.HasLastModifiedDate() {
  62. n.Size = n.Size + LastModifiedBytesLength
  63. }
  64. if n.HasTtl() {
  65. n.Size = n.Size + TtlBytesLength
  66. }
  67. if n.HasPairs() {
  68. n.Size += 2 + uint32(n.PairsSize)
  69. }
  70. } else {
  71. n.Size = 0
  72. }
  73. util.Uint32toBytes(header[CookieSize+NeedleIdSize:CookieSize+NeedleIdSize+SizeSize], n.Size)
  74. writeBytes = append(writeBytes, header[0:NeedleHeaderSize]...)
  75. if n.DataSize > 0 {
  76. util.Uint32toBytes(header[0:4], n.DataSize)
  77. writeBytes = append(writeBytes, header[0:4]...)
  78. writeBytes = append(writeBytes, n.Data...)
  79. util.Uint8toBytes(header[0:1], n.Flags)
  80. writeBytes = append(writeBytes, header[0:1]...)
  81. if n.HasName() {
  82. util.Uint8toBytes(header[0:1], n.NameSize)
  83. writeBytes = append(writeBytes, header[0:1]...)
  84. writeBytes = append(writeBytes, n.Name[:n.NameSize]...)
  85. }
  86. if n.HasMime() {
  87. util.Uint8toBytes(header[0:1], n.MimeSize)
  88. writeBytes = append(writeBytes, header[0:1]...)
  89. writeBytes = append(writeBytes, n.Mime...)
  90. }
  91. if n.HasLastModifiedDate() {
  92. util.Uint64toBytes(header[0:8], n.LastModified)
  93. writeBytes = append(writeBytes, header[8-LastModifiedBytesLength:8]...)
  94. }
  95. if n.HasTtl() && n.Ttl != nil {
  96. n.Ttl.ToBytes(header[0:TtlBytesLength])
  97. writeBytes = append(writeBytes, header[0:TtlBytesLength]...)
  98. }
  99. if n.HasPairs() {
  100. util.Uint16toBytes(header[0:2], n.PairsSize)
  101. writeBytes = append(writeBytes, header[0:2]...)
  102. writeBytes = append(writeBytes, n.Pairs...)
  103. }
  104. }
  105. padding := PaddingLength(n.Size, version)
  106. util.Uint32toBytes(header[0:NeedleChecksumSize], n.Checksum.Value())
  107. if version == Version2 {
  108. writeBytes = append(writeBytes, header[0:NeedleChecksumSize+padding]...)
  109. } else {
  110. // version3
  111. util.Uint64toBytes(header[NeedleChecksumSize:NeedleChecksumSize+TimestampSize], n.AppendAtNs)
  112. writeBytes = append(writeBytes, header[0:NeedleChecksumSize+TimestampSize+padding]...)
  113. }
  114. return writeBytes, n.DataSize, GetActualSize(n.Size, version), nil
  115. }
  116. return writeBytes, 0, 0, fmt.Errorf("Unsupported Version! (%d)", version)
  117. }
  118. func (n *Needle) Append(w backend.BackendStorageFile, version Version) (offset uint64, size uint32, actualSize int64, err error) {
  119. if end, _, e := w.GetStat(); e == nil {
  120. defer func(w backend.BackendStorageFile, off int64) {
  121. if err != nil {
  122. if te := w.Truncate(end); te != nil {
  123. glog.V(0).Infof("Failed to truncate %s back to %d with error: %v", w.Name(), end, te)
  124. }
  125. }
  126. }(w, end)
  127. offset = uint64(end)
  128. } else {
  129. err = fmt.Errorf("Cannot Read Current Volume Position: %v", e)
  130. return
  131. }
  132. if offset >= MaxPossibleVolumeSize {
  133. err = fmt.Errorf("Volume Size %d Exeededs %d", offset, MaxPossibleVolumeSize)
  134. return
  135. }
  136. bytesToWrite, size, actualSize, err := n.prepareWriteBuffer(version)
  137. if err == nil {
  138. _, err = w.WriteAt(bytesToWrite, int64(offset))
  139. }
  140. return offset, size, actualSize, err
  141. }
  142. func ReadNeedleBlob(r backend.BackendStorageFile, offset int64, size uint32, version Version) (dataSlice []byte, err error) {
  143. dataSize := GetActualSize(size, version)
  144. dataSlice = make([]byte, int(dataSize))
  145. _, err = r.ReadAt(dataSlice, offset)
  146. return dataSlice, err
  147. }
  148. // ReadBytes hydrates the needle from the bytes buffer, with only n.Id is set.
  149. func (n *Needle) ReadBytes(bytes []byte, offset int64, size uint32, version Version) (err error) {
  150. n.ParseNeedleHeader(bytes)
  151. if n.Size != size {
  152. return fmt.Errorf("entry not found: offset %d found id %x size %d, expected size %d", offset, n.Id, n.Size, size)
  153. }
  154. switch version {
  155. case Version1:
  156. n.Data = bytes[NeedleHeaderSize : NeedleHeaderSize+size]
  157. case Version2, Version3:
  158. err = n.readNeedleDataVersion2(bytes[NeedleHeaderSize : NeedleHeaderSize+int(n.Size)])
  159. }
  160. if err != nil && err != io.EOF {
  161. return err
  162. }
  163. if size > 0 {
  164. checksum := util.BytesToUint32(bytes[NeedleHeaderSize+size : NeedleHeaderSize+size+NeedleChecksumSize])
  165. newChecksum := NewCRC(n.Data)
  166. if checksum != newChecksum.Value() {
  167. return errors.New("CRC error! Data On Disk Corrupted")
  168. }
  169. n.Checksum = newChecksum
  170. }
  171. if version == Version3 {
  172. tsOffset := NeedleHeaderSize + size + NeedleChecksumSize
  173. n.AppendAtNs = util.BytesToUint64(bytes[tsOffset : tsOffset+TimestampSize])
  174. }
  175. return nil
  176. }
  177. // ReadData hydrates the needle from the file, with only n.Id is set.
  178. func (n *Needle) ReadData(r backend.BackendStorageFile, offset int64, size uint32, version Version) (err error) {
  179. bytes, err := ReadNeedleBlob(r, offset, size, version)
  180. if err != nil {
  181. return err
  182. }
  183. return n.ReadBytes(bytes, offset, size, version)
  184. }
  185. func (n *Needle) ParseNeedleHeader(bytes []byte) {
  186. n.Cookie = BytesToCookie(bytes[0:CookieSize])
  187. n.Id = BytesToNeedleId(bytes[CookieSize : CookieSize+NeedleIdSize])
  188. n.Size = util.BytesToUint32(bytes[CookieSize+NeedleIdSize : NeedleHeaderSize])
  189. }
  190. func (n *Needle) readNeedleDataVersion2(bytes []byte) (err error) {
  191. index, lenBytes := 0, len(bytes)
  192. if index < lenBytes {
  193. n.DataSize = util.BytesToUint32(bytes[index : index+4])
  194. index = index + 4
  195. if int(n.DataSize)+index > lenBytes {
  196. return fmt.Errorf("index out of range %d", 1)
  197. }
  198. n.Data = bytes[index : index+int(n.DataSize)]
  199. index = index + int(n.DataSize)
  200. n.Flags = bytes[index]
  201. index = index + 1
  202. }
  203. if index < lenBytes && n.HasName() {
  204. n.NameSize = uint8(bytes[index])
  205. index = index + 1
  206. if int(n.NameSize)+index > lenBytes {
  207. return fmt.Errorf("index out of range %d", 2)
  208. }
  209. n.Name = bytes[index : index+int(n.NameSize)]
  210. index = index + int(n.NameSize)
  211. }
  212. if index < lenBytes && n.HasMime() {
  213. n.MimeSize = uint8(bytes[index])
  214. index = index + 1
  215. if int(n.MimeSize)+index > lenBytes {
  216. return fmt.Errorf("index out of range %d", 3)
  217. }
  218. n.Mime = bytes[index : index+int(n.MimeSize)]
  219. index = index + int(n.MimeSize)
  220. }
  221. if index < lenBytes && n.HasLastModifiedDate() {
  222. if LastModifiedBytesLength+index > lenBytes {
  223. return fmt.Errorf("index out of range %d", 4)
  224. }
  225. n.LastModified = util.BytesToUint64(bytes[index : index+LastModifiedBytesLength])
  226. index = index + LastModifiedBytesLength
  227. }
  228. if index < lenBytes && n.HasTtl() {
  229. if TtlBytesLength+index > lenBytes {
  230. return fmt.Errorf("index out of range %d", 5)
  231. }
  232. n.Ttl = LoadTTLFromBytes(bytes[index : index+TtlBytesLength])
  233. index = index + TtlBytesLength
  234. }
  235. if index < lenBytes && n.HasPairs() {
  236. if 2+index > lenBytes {
  237. return fmt.Errorf("index out of range %d", 6)
  238. }
  239. n.PairsSize = util.BytesToUint16(bytes[index : index+2])
  240. index += 2
  241. if int(n.PairsSize)+index > lenBytes {
  242. return fmt.Errorf("index out of range %d", 7)
  243. }
  244. end := index + int(n.PairsSize)
  245. n.Pairs = bytes[index:end]
  246. index = end
  247. }
  248. return nil
  249. }
  250. func ReadNeedleHeader(r backend.BackendStorageFile, version Version, offset int64) (n *Needle, bytes []byte, bodyLength int64, err error) {
  251. n = new(Needle)
  252. if version == Version1 || version == Version2 || version == Version3 {
  253. bytes = make([]byte, NeedleHeaderSize)
  254. var count int
  255. count, err = r.ReadAt(bytes, offset)
  256. if count <= 0 || err != nil {
  257. return nil, bytes, 0, err
  258. }
  259. n.ParseNeedleHeader(bytes)
  260. bodyLength = NeedleBodyLength(n.Size, version)
  261. }
  262. return
  263. }
  264. func PaddingLength(needleSize uint32, version Version) uint32 {
  265. if version == Version3 {
  266. // this is same value as version2, but just listed here for clarity
  267. return NeedlePaddingSize - ((NeedleHeaderSize + needleSize + NeedleChecksumSize + TimestampSize) % NeedlePaddingSize)
  268. }
  269. return NeedlePaddingSize - ((NeedleHeaderSize + needleSize + NeedleChecksumSize) % NeedlePaddingSize)
  270. }
  271. func NeedleBodyLength(needleSize uint32, version Version) int64 {
  272. if version == Version3 {
  273. return int64(needleSize) + NeedleChecksumSize + TimestampSize + int64(PaddingLength(needleSize, version))
  274. }
  275. return int64(needleSize) + NeedleChecksumSize + int64(PaddingLength(needleSize, version))
  276. }
  277. //n should be a needle already read the header
  278. //the input stream will read until next file entry
  279. func (n *Needle) ReadNeedleBody(r backend.BackendStorageFile, version Version, offset int64, bodyLength int64) (bytes []byte, err error) {
  280. if bodyLength <= 0 {
  281. return nil, nil
  282. }
  283. bytes = make([]byte, bodyLength)
  284. if _, err = r.ReadAt(bytes, offset); err != nil {
  285. return
  286. }
  287. err = n.ReadNeedleBodyBytes(bytes, version)
  288. return
  289. }
  290. func (n *Needle) ReadNeedleBodyBytes(needleBody []byte, version Version) (err error) {
  291. if len(needleBody) <= 0 {
  292. return nil
  293. }
  294. switch version {
  295. case Version1:
  296. n.Data = needleBody[:n.Size]
  297. n.Checksum = NewCRC(n.Data)
  298. case Version2, Version3:
  299. err = n.readNeedleDataVersion2(needleBody[0:n.Size])
  300. n.Checksum = NewCRC(n.Data)
  301. if version == Version3 {
  302. tsOffset := n.Size + NeedleChecksumSize
  303. n.AppendAtNs = util.BytesToUint64(needleBody[tsOffset : tsOffset+TimestampSize])
  304. }
  305. default:
  306. err = fmt.Errorf("unsupported version %d!", version)
  307. }
  308. return
  309. }
  310. func (n *Needle) IsCompressed() bool {
  311. return n.Flags&FlagIsCompressed > 0
  312. }
  313. func (n *Needle) SetIsCompressed() {
  314. n.Flags = n.Flags | FlagIsCompressed
  315. }
  316. func (n *Needle) HasName() bool {
  317. return n.Flags&FlagHasName > 0
  318. }
  319. func (n *Needle) SetHasName() {
  320. n.Flags = n.Flags | FlagHasName
  321. }
  322. func (n *Needle) HasMime() bool {
  323. return n.Flags&FlagHasMime > 0
  324. }
  325. func (n *Needle) SetHasMime() {
  326. n.Flags = n.Flags | FlagHasMime
  327. }
  328. func (n *Needle) HasLastModifiedDate() bool {
  329. return n.Flags&FlagHasLastModifiedDate > 0
  330. }
  331. func (n *Needle) SetHasLastModifiedDate() {
  332. n.Flags = n.Flags | FlagHasLastModifiedDate
  333. }
  334. func (n *Needle) HasTtl() bool {
  335. return n.Flags&FlagHasTtl > 0
  336. }
  337. func (n *Needle) SetHasTtl() {
  338. n.Flags = n.Flags | FlagHasTtl
  339. }
  340. func (n *Needle) IsChunkedManifest() bool {
  341. return n.Flags&FlagIsChunkManifest > 0
  342. }
  343. func (n *Needle) SetIsChunkManifest() {
  344. n.Flags = n.Flags | FlagIsChunkManifest
  345. }
  346. func (n *Needle) HasPairs() bool {
  347. return n.Flags&FlagHasPairs != 0
  348. }
  349. func (n *Needle) SetHasPairs() {
  350. n.Flags = n.Flags | FlagHasPairs
  351. }
  352. func GetActualSize(size uint32, version Version) int64 {
  353. return NeedleHeaderSize + NeedleBodyLength(size, version)
  354. }