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.

438 lines
13 KiB

6 years ago
12 years ago
12 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
4 years ago
4 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. "github.com/chrislusf/seaweedfs/weed/glog"
  6. "github.com/chrislusf/seaweedfs/weed/storage/backend"
  7. . "github.com/chrislusf/seaweedfs/weed/storage/types"
  8. "github.com/chrislusf/seaweedfs/weed/util"
  9. "io"
  10. "math"
  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 WriteNeedleBlob(w backend.BackendStorageFile, dataSlice []byte, size Size, appendAtNs uint64, version Version) (offset uint64, err error) {
  144. if end, _, e := w.GetStat(); e == nil {
  145. defer func(w backend.BackendStorageFile, off int64) {
  146. if err != nil {
  147. if te := w.Truncate(end); te != nil {
  148. glog.V(0).Infof("Failed to truncate %s back to %d with error: %v", w.Name(), end, te)
  149. }
  150. }
  151. }(w, end)
  152. offset = uint64(end)
  153. } else {
  154. err = fmt.Errorf("Cannot Read Current Volume Position: %v", e)
  155. return
  156. }
  157. if version == Version3 {
  158. tsOffset := NeedleHeaderSize + size + NeedleChecksumSize
  159. util.Uint64toBytes(dataSlice[tsOffset:tsOffset+TimestampSize], appendAtNs)
  160. }
  161. if err == nil {
  162. _, err = w.WriteAt(dataSlice, int64(offset))
  163. }
  164. return
  165. }
  166. func ReadNeedleBlob(r backend.BackendStorageFile, offset int64, size Size, version Version) (dataSlice []byte, err error) {
  167. dataSize := GetActualSize(size, version)
  168. dataSlice = make([]byte, int(dataSize))
  169. var n int
  170. n, err = r.ReadAt(dataSlice, offset)
  171. if err != nil && int64(n) == dataSize {
  172. err = nil
  173. }
  174. if err != nil {
  175. fileSize, _, _ := r.GetStat()
  176. println("n", n, "dataSize", dataSize, "offset", offset, "fileSize", fileSize)
  177. }
  178. return dataSlice, err
  179. }
  180. // ReadBytes hydrates the needle from the bytes buffer, with only n.Id is set.
  181. func (n *Needle) ReadBytes(bytes []byte, offset int64, size Size, version Version) (err error) {
  182. n.ParseNeedleHeader(bytes)
  183. if n.Size != size {
  184. // cookie is not always passed in for this API. Use size to do preliminary checking.
  185. if OffsetSize == 4 && offset < int64(MaxPossibleVolumeSize) {
  186. glog.Errorf("entry not found1: offset %d found id %x size %d, expected size %d", offset, n.Id, n.Size, size)
  187. return ErrorSizeMismatch
  188. }
  189. return fmt.Errorf("entry not found: offset %d found id %x size %d, expected size %d", offset, n.Id, n.Size, size)
  190. }
  191. switch version {
  192. case Version1:
  193. n.Data = bytes[NeedleHeaderSize : NeedleHeaderSize+size]
  194. case Version2, Version3:
  195. err = n.readNeedleDataVersion2(bytes[NeedleHeaderSize : NeedleHeaderSize+int(n.Size)])
  196. }
  197. if err != nil && err != io.EOF {
  198. return err
  199. }
  200. if size > 0 {
  201. checksum := util.BytesToUint32(bytes[NeedleHeaderSize+size : NeedleHeaderSize+size+NeedleChecksumSize])
  202. newChecksum := NewCRC(n.Data)
  203. if checksum != newChecksum.Value() {
  204. return errors.New("CRC error! Data On Disk Corrupted")
  205. }
  206. n.Checksum = newChecksum
  207. }
  208. if version == Version3 {
  209. tsOffset := NeedleHeaderSize + size + NeedleChecksumSize
  210. n.AppendAtNs = util.BytesToUint64(bytes[tsOffset : tsOffset+TimestampSize])
  211. }
  212. return nil
  213. }
  214. // ReadData hydrates the needle from the file, with only n.Id is set.
  215. func (n *Needle) ReadData(r backend.BackendStorageFile, offset int64, size Size, version Version) (err error) {
  216. bytes, err := ReadNeedleBlob(r, offset, size, version)
  217. if err != nil {
  218. return err
  219. }
  220. return n.ReadBytes(bytes, offset, size, version)
  221. }
  222. func (n *Needle) ParseNeedleHeader(bytes []byte) {
  223. n.Cookie = BytesToCookie(bytes[0:CookieSize])
  224. n.Id = BytesToNeedleId(bytes[CookieSize : CookieSize+NeedleIdSize])
  225. n.Size = BytesToSize(bytes[CookieSize+NeedleIdSize : NeedleHeaderSize])
  226. }
  227. func (n *Needle) readNeedleDataVersion2(bytes []byte) (err error) {
  228. index, lenBytes := 0, len(bytes)
  229. if index < lenBytes {
  230. n.DataSize = util.BytesToUint32(bytes[index : index+4])
  231. index = index + 4
  232. if int(n.DataSize)+index > lenBytes {
  233. return fmt.Errorf("index out of range %d", 1)
  234. }
  235. n.Data = bytes[index : index+int(n.DataSize)]
  236. index = index + int(n.DataSize)
  237. n.Flags = bytes[index]
  238. index = index + 1
  239. }
  240. if index < lenBytes && n.HasName() {
  241. n.NameSize = uint8(bytes[index])
  242. index = index + 1
  243. if int(n.NameSize)+index > lenBytes {
  244. return fmt.Errorf("index out of range %d", 2)
  245. }
  246. n.Name = bytes[index : index+int(n.NameSize)]
  247. index = index + int(n.NameSize)
  248. }
  249. if index < lenBytes && n.HasMime() {
  250. n.MimeSize = uint8(bytes[index])
  251. index = index + 1
  252. if int(n.MimeSize)+index > lenBytes {
  253. return fmt.Errorf("index out of range %d", 3)
  254. }
  255. n.Mime = bytes[index : index+int(n.MimeSize)]
  256. index = index + int(n.MimeSize)
  257. }
  258. if index < lenBytes && n.HasLastModifiedDate() {
  259. if LastModifiedBytesLength+index > lenBytes {
  260. return fmt.Errorf("index out of range %d", 4)
  261. }
  262. n.LastModified = util.BytesToUint64(bytes[index : index+LastModifiedBytesLength])
  263. index = index + LastModifiedBytesLength
  264. }
  265. if index < lenBytes && n.HasTtl() {
  266. if TtlBytesLength+index > lenBytes {
  267. return fmt.Errorf("index out of range %d", 5)
  268. }
  269. n.Ttl = LoadTTLFromBytes(bytes[index : index+TtlBytesLength])
  270. index = index + TtlBytesLength
  271. }
  272. if index < lenBytes && n.HasPairs() {
  273. if 2+index > lenBytes {
  274. return fmt.Errorf("index out of range %d", 6)
  275. }
  276. n.PairsSize = util.BytesToUint16(bytes[index : index+2])
  277. index += 2
  278. if int(n.PairsSize)+index > lenBytes {
  279. return fmt.Errorf("index out of range %d", 7)
  280. }
  281. end := index + int(n.PairsSize)
  282. n.Pairs = bytes[index:end]
  283. index = end
  284. }
  285. return nil
  286. }
  287. func ReadNeedleHeader(r backend.BackendStorageFile, version Version, offset int64) (n *Needle, bytes []byte, bodyLength int64, err error) {
  288. n = new(Needle)
  289. if version == Version1 || version == Version2 || version == Version3 {
  290. bytes = make([]byte, NeedleHeaderSize)
  291. var count int
  292. count, err = r.ReadAt(bytes, offset)
  293. if count <= 0 || err != nil {
  294. return nil, bytes, 0, err
  295. }
  296. n.ParseNeedleHeader(bytes)
  297. bodyLength = NeedleBodyLength(n.Size, version)
  298. }
  299. return
  300. }
  301. func PaddingLength(needleSize Size, version Version) Size {
  302. if version == Version3 {
  303. // this is same value as version2, but just listed here for clarity
  304. return NeedlePaddingSize - ((NeedleHeaderSize + needleSize + NeedleChecksumSize + TimestampSize) % NeedlePaddingSize)
  305. }
  306. return NeedlePaddingSize - ((NeedleHeaderSize + needleSize + NeedleChecksumSize) % NeedlePaddingSize)
  307. }
  308. func NeedleBodyLength(needleSize Size, version Version) int64 {
  309. if version == Version3 {
  310. return int64(needleSize) + NeedleChecksumSize + TimestampSize + int64(PaddingLength(needleSize, version))
  311. }
  312. return int64(needleSize) + NeedleChecksumSize + int64(PaddingLength(needleSize, version))
  313. }
  314. //n should be a needle already read the header
  315. //the input stream will read until next file entry
  316. func (n *Needle) ReadNeedleBody(r backend.BackendStorageFile, version Version, offset int64, bodyLength int64) (bytes []byte, err error) {
  317. if bodyLength <= 0 {
  318. return nil, nil
  319. }
  320. bytes = make([]byte, bodyLength)
  321. if _, err = r.ReadAt(bytes, offset); err != nil {
  322. return
  323. }
  324. err = n.ReadNeedleBodyBytes(bytes, version)
  325. return
  326. }
  327. func (n *Needle) ReadNeedleBodyBytes(needleBody []byte, version Version) (err error) {
  328. if len(needleBody) <= 0 {
  329. return nil
  330. }
  331. switch version {
  332. case Version1:
  333. n.Data = needleBody[:n.Size]
  334. n.Checksum = NewCRC(n.Data)
  335. case Version2, Version3:
  336. err = n.readNeedleDataVersion2(needleBody[0:n.Size])
  337. n.Checksum = NewCRC(n.Data)
  338. if version == Version3 {
  339. tsOffset := n.Size + NeedleChecksumSize
  340. n.AppendAtNs = util.BytesToUint64(needleBody[tsOffset : tsOffset+TimestampSize])
  341. }
  342. default:
  343. err = fmt.Errorf("unsupported version %d!", version)
  344. }
  345. return
  346. }
  347. func (n *Needle) IsCompressed() bool {
  348. return n.Flags&FlagIsCompressed > 0
  349. }
  350. func (n *Needle) SetIsCompressed() {
  351. n.Flags = n.Flags | FlagIsCompressed
  352. }
  353. func (n *Needle) HasName() bool {
  354. return n.Flags&FlagHasName > 0
  355. }
  356. func (n *Needle) SetHasName() {
  357. n.Flags = n.Flags | FlagHasName
  358. }
  359. func (n *Needle) HasMime() bool {
  360. return n.Flags&FlagHasMime > 0
  361. }
  362. func (n *Needle) SetHasMime() {
  363. n.Flags = n.Flags | FlagHasMime
  364. }
  365. func (n *Needle) HasLastModifiedDate() bool {
  366. return n.Flags&FlagHasLastModifiedDate > 0
  367. }
  368. func (n *Needle) SetHasLastModifiedDate() {
  369. n.Flags = n.Flags | FlagHasLastModifiedDate
  370. }
  371. func (n *Needle) HasTtl() bool {
  372. return n.Flags&FlagHasTtl > 0
  373. }
  374. func (n *Needle) SetHasTtl() {
  375. n.Flags = n.Flags | FlagHasTtl
  376. }
  377. func (n *Needle) IsChunkedManifest() bool {
  378. return n.Flags&FlagIsChunkManifest > 0
  379. }
  380. func (n *Needle) SetIsChunkManifest() {
  381. n.Flags = n.Flags | FlagIsChunkManifest
  382. }
  383. func (n *Needle) HasPairs() bool {
  384. return n.Flags&FlagHasPairs != 0
  385. }
  386. func (n *Needle) SetHasPairs() {
  387. n.Flags = n.Flags | FlagHasPairs
  388. }
  389. func GetActualSize(size Size, version Version) int64 {
  390. return NeedleHeaderSize + NeedleBodyLength(size, version)
  391. }