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.

350 lines
10 KiB

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