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.

393 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. func (n *Needle) ReadData(r *os.File, offset int64, size uint32, version Version) (err error) {
  165. bytes, err := ReadNeedleBlob(r, offset, size, version)
  166. if err != nil {
  167. return err
  168. }
  169. n.ParseNeedleHeader(bytes)
  170. if n.Size != size {
  171. return fmt.Errorf("File Entry Not Found. offset %d, Needle id %d expected size %d Memory %d", offset, n.Id, n.Size, size)
  172. }
  173. switch version {
  174. case Version1:
  175. n.Data = bytes[NeedleHeaderSize : NeedleHeaderSize+size]
  176. case Version2, Version3:
  177. err = n.readNeedleDataVersion2(bytes[NeedleHeaderSize : NeedleHeaderSize+int(n.Size)])
  178. }
  179. if err != nil && err != io.EOF{
  180. return err
  181. }
  182. if size > 0 {
  183. checksum := util.BytesToUint32(bytes[NeedleHeaderSize+size : NeedleHeaderSize+size+NeedleChecksumSize])
  184. newChecksum := NewCRC(n.Data)
  185. if checksum != newChecksum.Value() {
  186. return errors.New("CRC error! Data On Disk Corrupted")
  187. }
  188. n.Checksum = newChecksum
  189. }
  190. if version == Version3 {
  191. tsOffset := NeedleHeaderSize + size + NeedleChecksumSize
  192. n.AppendAtNs = util.BytesToUint64(bytes[tsOffset : tsOffset+TimestampSize])
  193. }
  194. return nil
  195. }
  196. func (n *Needle) ParseNeedleHeader(bytes []byte) {
  197. n.Cookie = BytesToCookie(bytes[0:CookieSize])
  198. n.Id = BytesToNeedleId(bytes[CookieSize : CookieSize+NeedleIdSize])
  199. n.Size = util.BytesToUint32(bytes[CookieSize+NeedleIdSize : NeedleHeaderSize])
  200. }
  201. func (n *Needle) readNeedleDataVersion2(bytes []byte) (err error) {
  202. index, lenBytes := 0, len(bytes)
  203. if index < lenBytes {
  204. n.DataSize = util.BytesToUint32(bytes[index : index+4])
  205. index = index + 4
  206. if int(n.DataSize)+index > lenBytes {
  207. return fmt.Errorf("index out of range %d", 1)
  208. }
  209. n.Data = bytes[index : index+int(n.DataSize)]
  210. index = index + int(n.DataSize)
  211. n.Flags = bytes[index]
  212. index = index + 1
  213. }
  214. if index < lenBytes && n.HasName() {
  215. n.NameSize = uint8(bytes[index])
  216. index = index + 1
  217. if int(n.NameSize)+index > lenBytes {
  218. return fmt.Errorf("index out of range %d", 2)
  219. }
  220. n.Name = bytes[index : index+int(n.NameSize)]
  221. index = index + int(n.NameSize)
  222. }
  223. if index < lenBytes && n.HasMime() {
  224. n.MimeSize = uint8(bytes[index])
  225. index = index + 1
  226. if int(n.MimeSize)+index > lenBytes {
  227. return fmt.Errorf("index out of range %d", 3)
  228. }
  229. n.Mime = bytes[index : index+int(n.MimeSize)]
  230. index = index + int(n.MimeSize)
  231. }
  232. if index < lenBytes && n.HasLastModifiedDate() {
  233. if LastModifiedBytesLength+index > lenBytes {
  234. return fmt.Errorf("index out of range %d", 4)
  235. }
  236. n.LastModified = util.BytesToUint64(bytes[index : index+LastModifiedBytesLength])
  237. index = index + LastModifiedBytesLength
  238. }
  239. if index < lenBytes && n.HasTtl() {
  240. if TtlBytesLength+index > lenBytes {
  241. return fmt.Errorf("index out of range %d", 5)
  242. }
  243. n.Ttl = LoadTTLFromBytes(bytes[index : index+TtlBytesLength])
  244. index = index + TtlBytesLength
  245. }
  246. if index < lenBytes && n.HasPairs() {
  247. if 2+index > lenBytes {
  248. return fmt.Errorf("index out of range %d", 6)
  249. }
  250. n.PairsSize = util.BytesToUint16(bytes[index : index+2])
  251. index += 2
  252. if int(n.PairsSize)+index > lenBytes {
  253. return fmt.Errorf("index out of range %d", 7)
  254. }
  255. end := index + int(n.PairsSize)
  256. n.Pairs = bytes[index:end]
  257. index = end
  258. }
  259. return nil
  260. }
  261. func ReadNeedleHeader(r *os.File, version Version, offset int64) (n *Needle, bytes []byte, bodyLength int64, err error) {
  262. n = new(Needle)
  263. if version == Version1 || version == Version2 || version == Version3 {
  264. bytes = make([]byte, NeedleHeaderSize)
  265. var count int
  266. count, err = r.ReadAt(bytes, offset)
  267. if count <= 0 || err != nil {
  268. return nil, bytes, 0, err
  269. }
  270. n.ParseNeedleHeader(bytes)
  271. bodyLength = NeedleBodyLength(n.Size, version)
  272. }
  273. return
  274. }
  275. func PaddingLength(needleSize uint32, version Version) uint32 {
  276. if version == Version3 {
  277. // this is same value as version2, but just listed here for clarity
  278. return NeedlePaddingSize - ((NeedleHeaderSize + needleSize + NeedleChecksumSize + TimestampSize) % NeedlePaddingSize)
  279. }
  280. return NeedlePaddingSize - ((NeedleHeaderSize + needleSize + NeedleChecksumSize) % NeedlePaddingSize)
  281. }
  282. func NeedleBodyLength(needleSize uint32, version Version) int64 {
  283. if version == Version3 {
  284. return int64(needleSize) + NeedleChecksumSize + TimestampSize + int64(PaddingLength(needleSize, version))
  285. }
  286. return int64(needleSize) + NeedleChecksumSize + int64(PaddingLength(needleSize, version))
  287. }
  288. //n should be a needle already read the header
  289. //the input stream will read until next file entry
  290. func (n *Needle) ReadNeedleBody(r *os.File, version Version, offset int64, bodyLength int64) (bytes []byte, err error) {
  291. if bodyLength <= 0 {
  292. return nil, nil
  293. }
  294. bytes = make([]byte, bodyLength)
  295. if _, err = r.ReadAt(bytes, offset); err != nil {
  296. return
  297. }
  298. err = n.ReadNeedleBodyBytes(bytes, version)
  299. return
  300. }
  301. func (n *Needle) ReadNeedleBodyBytes(needleBody []byte, version Version) (err error) {
  302. if len(needleBody) <= 0 {
  303. return nil
  304. }
  305. switch version {
  306. case Version1:
  307. n.Data = needleBody[:n.Size]
  308. n.Checksum = NewCRC(n.Data)
  309. case Version2, Version3:
  310. err = n.readNeedleDataVersion2(needleBody[0:n.Size])
  311. n.Checksum = NewCRC(n.Data)
  312. if version == Version3 {
  313. tsOffset := n.Size + NeedleChecksumSize
  314. n.AppendAtNs = util.BytesToUint64(needleBody[tsOffset : tsOffset+TimestampSize])
  315. }
  316. default:
  317. err = fmt.Errorf("unsupported version %d!", version)
  318. }
  319. return
  320. }
  321. func (n *Needle) IsGzipped() bool {
  322. return n.Flags&FlagGzip > 0
  323. }
  324. func (n *Needle) SetGzipped() {
  325. n.Flags = n.Flags | FlagGzip
  326. }
  327. func (n *Needle) HasName() bool {
  328. return n.Flags&FlagHasName > 0
  329. }
  330. func (n *Needle) SetHasName() {
  331. n.Flags = n.Flags | FlagHasName
  332. }
  333. func (n *Needle) HasMime() bool {
  334. return n.Flags&FlagHasMime > 0
  335. }
  336. func (n *Needle) SetHasMime() {
  337. n.Flags = n.Flags | FlagHasMime
  338. }
  339. func (n *Needle) HasLastModifiedDate() bool {
  340. return n.Flags&FlagHasLastModifiedDate > 0
  341. }
  342. func (n *Needle) SetHasLastModifiedDate() {
  343. n.Flags = n.Flags | FlagHasLastModifiedDate
  344. }
  345. func (n *Needle) HasTtl() bool {
  346. return n.Flags&FlagHasTtl > 0
  347. }
  348. func (n *Needle) SetHasTtl() {
  349. n.Flags = n.Flags | FlagHasTtl
  350. }
  351. func (n *Needle) IsChunkedManifest() bool {
  352. return n.Flags&FlagIsChunkManifest > 0
  353. }
  354. func (n *Needle) SetIsChunkManifest() {
  355. n.Flags = n.Flags | FlagIsChunkManifest
  356. }
  357. func (n *Needle) HasPairs() bool {
  358. return n.Flags&FlagHasPairs != 0
  359. }
  360. func (n *Needle) SetHasPairs() {
  361. n.Flags = n.Flags | FlagHasPairs
  362. }
  363. func getActualSize(size uint32, version Version) int64 {
  364. return NeedleHeaderSize + NeedleBodyLength(size, version)
  365. }