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.

410 lines
12 KiB

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