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.

398 lines
11 KiB

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