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.

419 lines
12 KiB

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