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.

378 lines
11 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
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. err = n.readNeedleDataVersion2(bytes[NeedleEntrySize : NeedleEntrySize+int(n.Size)])
  178. }
  179. if size == 0 || err != nil {
  180. return err
  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) (err error) {
  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. return fmt.Errorf("index out of range %d", 1)
  206. }
  207. n.Data = bytes[index : index+int(n.DataSize)]
  208. index = index + int(n.DataSize)
  209. n.Flags = bytes[index]
  210. index = index + 1
  211. }
  212. if index < lenBytes && n.HasName() {
  213. n.NameSize = uint8(bytes[index])
  214. index = index + 1
  215. if int(n.NameSize)+index > lenBytes {
  216. return fmt.Errorf("index out of range %d", 2)
  217. }
  218. n.Name = bytes[index : index+int(n.NameSize)]
  219. index = index + int(n.NameSize)
  220. }
  221. if index < lenBytes && n.HasMime() {
  222. n.MimeSize = uint8(bytes[index])
  223. index = index + 1
  224. if int(n.MimeSize)+index > lenBytes {
  225. return fmt.Errorf("index out of range %d", 3)
  226. }
  227. n.Mime = bytes[index : index+int(n.MimeSize)]
  228. index = index + int(n.MimeSize)
  229. }
  230. if index < lenBytes && n.HasLastModifiedDate() {
  231. if LastModifiedBytesLength+index > lenBytes {
  232. return fmt.Errorf("index out of range %d", 4)
  233. }
  234. n.LastModified = util.BytesToUint64(bytes[index : index+LastModifiedBytesLength])
  235. index = index + LastModifiedBytesLength
  236. }
  237. if index < lenBytes && n.HasTtl() {
  238. if TtlBytesLength+index > lenBytes {
  239. return fmt.Errorf("index out of range %d", 5)
  240. }
  241. n.Ttl = LoadTTLFromBytes(bytes[index : index+TtlBytesLength])
  242. index = index + TtlBytesLength
  243. }
  244. if index < lenBytes && n.HasPairs() {
  245. if 2+index > lenBytes {
  246. return fmt.Errorf("index out of range %d", 6)
  247. }
  248. n.PairsSize = util.BytesToUint16(bytes[index : index+2])
  249. index += 2
  250. if int(n.PairsSize)+index > lenBytes {
  251. return fmt.Errorf("index out of range %d", 7)
  252. }
  253. end := index + int(n.PairsSize)
  254. n.Pairs = bytes[index:end]
  255. index = end
  256. }
  257. return nil
  258. }
  259. func ReadNeedleHeader(r *os.File, version Version, offset int64) (n *Needle, bodyLength int64, err error) {
  260. n = new(Needle)
  261. if version == Version1 || version == Version2 || version == Version3 {
  262. bytes := make([]byte, NeedleEntrySize)
  263. var count int
  264. count, err = r.ReadAt(bytes, offset)
  265. if count <= 0 || err != nil {
  266. return nil, 0, err
  267. }
  268. n.ParseNeedleHeader(bytes)
  269. bodyLength = NeedleBodyLength(n.Size, version)
  270. }
  271. return
  272. }
  273. func PaddingLength(needleSize uint32, version Version) uint32 {
  274. if version == Version3 {
  275. // this is same value as version2, but just listed here for clarity
  276. return NeedlePaddingSize - ((NeedleEntrySize + needleSize + NeedleChecksumSize + TimestampSize) % NeedlePaddingSize)
  277. }
  278. return NeedlePaddingSize - ((NeedleEntrySize + needleSize + NeedleChecksumSize) % NeedlePaddingSize)
  279. }
  280. func NeedleBodyLength(needleSize uint32, version Version) int64 {
  281. if version == Version3 {
  282. return int64(needleSize) + NeedleChecksumSize + TimestampSize + int64(PaddingLength(needleSize, version))
  283. }
  284. return int64(needleSize) + NeedleChecksumSize + int64(PaddingLength(needleSize, version))
  285. }
  286. //n should be a needle already read the header
  287. //the input stream will read until next file entry
  288. func (n *Needle) ReadNeedleBody(r *os.File, version Version, offset int64, bodyLength int64) (err error) {
  289. if bodyLength <= 0 {
  290. return nil
  291. }
  292. switch version {
  293. case Version1:
  294. bytes := make([]byte, bodyLength)
  295. if _, err = r.ReadAt(bytes, offset); err != nil {
  296. return
  297. }
  298. n.Data = bytes[:n.Size]
  299. n.Checksum = NewCRC(n.Data)
  300. case Version2, Version3:
  301. bytes := make([]byte, bodyLength)
  302. if _, err = r.ReadAt(bytes, offset); err != nil {
  303. return
  304. }
  305. err = n.readNeedleDataVersion2(bytes[0:n.Size])
  306. n.Checksum = NewCRC(n.Data)
  307. if version == Version3 {
  308. tsOffset := n.Size + NeedleChecksumSize
  309. n.AppendAtNs = util.BytesToUint64(bytes[tsOffset : tsOffset+TimestampSize])
  310. }
  311. default:
  312. err = fmt.Errorf("Unsupported Version! (%d)", version)
  313. }
  314. return
  315. }
  316. func (n *Needle) IsGzipped() bool {
  317. return n.Flags&FlagGzip > 0
  318. }
  319. func (n *Needle) SetGzipped() {
  320. n.Flags = n.Flags | FlagGzip
  321. }
  322. func (n *Needle) HasName() bool {
  323. return n.Flags&FlagHasName > 0
  324. }
  325. func (n *Needle) SetHasName() {
  326. n.Flags = n.Flags | FlagHasName
  327. }
  328. func (n *Needle) HasMime() bool {
  329. return n.Flags&FlagHasMime > 0
  330. }
  331. func (n *Needle) SetHasMime() {
  332. n.Flags = n.Flags | FlagHasMime
  333. }
  334. func (n *Needle) HasLastModifiedDate() bool {
  335. return n.Flags&FlagHasLastModifiedDate > 0
  336. }
  337. func (n *Needle) SetHasLastModifiedDate() {
  338. n.Flags = n.Flags | FlagHasLastModifiedDate
  339. }
  340. func (n *Needle) HasTtl() bool {
  341. return n.Flags&FlagHasTtl > 0
  342. }
  343. func (n *Needle) SetHasTtl() {
  344. n.Flags = n.Flags | FlagHasTtl
  345. }
  346. func (n *Needle) IsChunkedManifest() bool {
  347. return n.Flags&FlagIsChunkManifest > 0
  348. }
  349. func (n *Needle) SetIsChunkManifest() {
  350. n.Flags = n.Flags | FlagIsChunkManifest
  351. }
  352. func (n *Needle) HasPairs() bool {
  353. return n.Flags&FlagHasPairs != 0
  354. }
  355. func (n *Needle) SetHasPairs() {
  356. n.Flags = n.Flags | FlagHasPairs
  357. }