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.

355 lines
10 KiB

12 years ago
12 years ago
12 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
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. "math"
  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, 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. 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:NeedleEntrySize]); 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[NeedleEntrySize : NeedleEntrySize+size]
  176. case Version2, Version3:
  177. n.readNeedleDataVersion2(bytes[NeedleEntrySize : NeedleEntrySize+int(n.Size)])
  178. }
  179. if size == 0 {
  180. return nil
  181. }
  182. checksum := util.BytesToUint32(bytes[NeedleEntrySize+size : NeedleEntrySize+size+NeedleChecksumSize])
  183. newChecksum := NewCRC(n.Data)
  184. if checksum != newChecksum.Value() {
  185. return errors.New("CRC error! Data On Disk Corrupted")
  186. }
  187. n.Checksum = newChecksum
  188. if version == Version3 {
  189. tsOffset := NeedleEntrySize + size + NeedleChecksumSize
  190. n.AppendAtNs = util.BytesToUint64(bytes[tsOffset : tsOffset+TimestampSize])
  191. }
  192. return nil
  193. }
  194. func (n *Needle) ParseNeedleHeader(bytes []byte) {
  195. n.Cookie = BytesToCookie(bytes[0:CookieSize])
  196. n.Id = BytesToNeedleId(bytes[CookieSize : CookieSize+NeedleIdSize])
  197. n.Size = util.BytesToUint32(bytes[CookieSize+NeedleIdSize : NeedleEntrySize])
  198. }
  199. func (n *Needle) readNeedleDataVersion2(bytes []byte) {
  200. index, lenBytes := 0, len(bytes)
  201. if index < lenBytes {
  202. n.DataSize = util.BytesToUint32(bytes[index : index+4])
  203. index = index + 4
  204. if int(n.DataSize)+index > lenBytes {
  205. // this if clause is due to bug #87 and #93, fixed in v0.69
  206. // remove this clause later
  207. return
  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. n.Name = bytes[index : index+int(n.NameSize)]
  218. index = index + int(n.NameSize)
  219. }
  220. if index < lenBytes && n.HasMime() {
  221. n.MimeSize = uint8(bytes[index])
  222. index = index + 1
  223. n.Mime = bytes[index : index+int(n.MimeSize)]
  224. index = index + int(n.MimeSize)
  225. }
  226. if index < lenBytes && n.HasLastModifiedDate() {
  227. n.LastModified = util.BytesToUint64(bytes[index : index+LastModifiedBytesLength])
  228. index = index + LastModifiedBytesLength
  229. }
  230. if index < lenBytes && n.HasTtl() {
  231. n.Ttl = LoadTTLFromBytes(bytes[index : index+TtlBytesLength])
  232. index = index + TtlBytesLength
  233. }
  234. if index < lenBytes && n.HasPairs() {
  235. n.PairsSize = util.BytesToUint16(bytes[index : index+2])
  236. index += 2
  237. end := index + int(n.PairsSize)
  238. n.Pairs = bytes[index:end]
  239. index = end
  240. }
  241. }
  242. func ReadNeedleHeader(r *os.File, version Version, offset int64) (n *Needle, bodyLength int64, err error) {
  243. n = new(Needle)
  244. if version == Version1 || version == Version2 || version == Version3 {
  245. bytes := make([]byte, NeedleEntrySize)
  246. var count int
  247. count, err = r.ReadAt(bytes, offset)
  248. if count <= 0 || err != nil {
  249. return nil, 0, err
  250. }
  251. n.ParseNeedleHeader(bytes)
  252. bodyLength = NeedleBodyLength(n.Size, version)
  253. }
  254. return
  255. }
  256. func PaddingLength(needleSize uint32, version Version) uint32 {
  257. if version == Version3 {
  258. // this is same value as version2, but just listed here for clarity
  259. return NeedlePaddingSize - ((NeedleEntrySize + needleSize + NeedleChecksumSize + TimestampSize) % NeedlePaddingSize)
  260. }
  261. return NeedlePaddingSize - ((NeedleEntrySize + needleSize + NeedleChecksumSize) % NeedlePaddingSize)
  262. }
  263. func NeedleBodyLength(needleSize uint32, version Version) int64 {
  264. if version == Version3 {
  265. return int64(needleSize) + NeedleChecksumSize + TimestampSize + int64(PaddingLength(needleSize, version))
  266. }
  267. return int64(needleSize) + NeedleChecksumSize + int64(PaddingLength(needleSize, version))
  268. }
  269. //n should be a needle already read the header
  270. //the input stream will read until next file entry
  271. func (n *Needle) ReadNeedleBody(r *os.File, version Version, offset int64, bodyLength int64) (err error) {
  272. if bodyLength <= 0 {
  273. return nil
  274. }
  275. switch version {
  276. case Version1:
  277. bytes := make([]byte, bodyLength)
  278. if _, err = r.ReadAt(bytes, offset); err != nil {
  279. return
  280. }
  281. n.Data = bytes[:n.Size]
  282. n.Checksum = NewCRC(n.Data)
  283. case Version2, Version3:
  284. bytes := make([]byte, bodyLength)
  285. if _, err = r.ReadAt(bytes, offset); err != nil {
  286. return
  287. }
  288. n.readNeedleDataVersion2(bytes[0:n.Size])
  289. n.Checksum = NewCRC(n.Data)
  290. default:
  291. err = fmt.Errorf("Unsupported Version! (%d)", version)
  292. }
  293. return
  294. }
  295. func (n *Needle) IsGzipped() bool {
  296. return n.Flags&FlagGzip > 0
  297. }
  298. func (n *Needle) SetGzipped() {
  299. n.Flags = n.Flags | FlagGzip
  300. }
  301. func (n *Needle) HasName() bool {
  302. return n.Flags&FlagHasName > 0
  303. }
  304. func (n *Needle) SetHasName() {
  305. n.Flags = n.Flags | FlagHasName
  306. }
  307. func (n *Needle) HasMime() bool {
  308. return n.Flags&FlagHasMime > 0
  309. }
  310. func (n *Needle) SetHasMime() {
  311. n.Flags = n.Flags | FlagHasMime
  312. }
  313. func (n *Needle) HasLastModifiedDate() bool {
  314. return n.Flags&FlagHasLastModifiedDate > 0
  315. }
  316. func (n *Needle) SetHasLastModifiedDate() {
  317. n.Flags = n.Flags | FlagHasLastModifiedDate
  318. }
  319. func (n *Needle) HasTtl() bool {
  320. return n.Flags&FlagHasTtl > 0
  321. }
  322. func (n *Needle) SetHasTtl() {
  323. n.Flags = n.Flags | FlagHasTtl
  324. }
  325. func (n *Needle) IsChunkedManifest() bool {
  326. return n.Flags&FlagIsChunkManifest > 0
  327. }
  328. func (n *Needle) SetIsChunkManifest() {
  329. n.Flags = n.Flags | FlagIsChunkManifest
  330. }
  331. func (n *Needle) HasPairs() bool {
  332. return n.Flags&FlagHasPairs != 0
  333. }
  334. func (n *Needle) SetHasPairs() {
  335. n.Flags = n.Flags | FlagHasPairs
  336. }