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.

414 lines
12 KiB

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