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.

201 lines
5.0 KiB

7 years ago
6 years ago
7 years ago
7 years ago
6 years ago
7 years ago
7 years ago
6 years ago
6 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
7 years ago
5 years ago
6 years ago
6 years ago
4 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
5 years ago
7 years ago
  1. package filer
  2. import (
  3. "bytes"
  4. "fmt"
  5. "github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
  6. "github.com/chrislusf/seaweedfs/weed/util"
  7. "github.com/chrislusf/seaweedfs/weed/wdclient"
  8. "math"
  9. )
  10. func TotalSize(chunks []*filer_pb.FileChunk) (size uint64) {
  11. for _, c := range chunks {
  12. t := uint64(c.Offset + int64(c.Size))
  13. if size < t {
  14. size = t
  15. }
  16. }
  17. return
  18. }
  19. func FileSize(entry *filer_pb.Entry) (size uint64) {
  20. return maxUint64(TotalSize(entry.Chunks), entry.Attributes.FileSize)
  21. }
  22. func ETag(entry *filer_pb.Entry) (etag string) {
  23. if entry.Attributes == nil || entry.Attributes.Md5 == nil {
  24. return ETagChunks(entry.Chunks)
  25. }
  26. return fmt.Sprintf("%x", entry.Attributes.Md5)
  27. }
  28. func ETagEntry(entry *Entry) (etag string) {
  29. if entry.Attr.Md5 == nil {
  30. return ETagChunks(entry.Chunks)
  31. }
  32. return fmt.Sprintf("%x", entry.Attr.Md5)
  33. }
  34. func ETagChunks(chunks []*filer_pb.FileChunk) (etag string) {
  35. if len(chunks) == 1 {
  36. return fmt.Sprintf("%x", util.Base64Md5ToBytes(chunks[0].ETag))
  37. }
  38. md5_digests := [][]byte{}
  39. for _, c := range chunks {
  40. md5_digests = append(md5_digests, util.Base64Md5ToBytes(c.ETag))
  41. }
  42. return fmt.Sprintf("%x-%d", util.Md5(bytes.Join(md5_digests, nil)), len(chunks))
  43. }
  44. func CompactFileChunks(lookupFileIdFn wdclient.LookupFileIdFunctionType, chunks []*filer_pb.FileChunk) (compacted, garbage []*filer_pb.FileChunk) {
  45. visibles, _ := NonOverlappingVisibleIntervals(lookupFileIdFn, chunks, 0, math.MaxInt64)
  46. fileIds := make(map[string]bool)
  47. for _, interval := range visibles {
  48. fileIds[interval.fileId] = true
  49. }
  50. for _, chunk := range chunks {
  51. if _, found := fileIds[chunk.GetFileIdString()]; found {
  52. compacted = append(compacted, chunk)
  53. } else {
  54. garbage = append(garbage, chunk)
  55. }
  56. }
  57. return
  58. }
  59. func MinusChunks(lookupFileIdFn wdclient.LookupFileIdFunctionType, as, bs []*filer_pb.FileChunk) (delta []*filer_pb.FileChunk, err error) {
  60. aData, aMeta, aErr := ResolveChunkManifest(lookupFileIdFn, as, 0, math.MaxInt64)
  61. if aErr != nil {
  62. return nil, aErr
  63. }
  64. bData, bMeta, bErr := ResolveChunkManifest(lookupFileIdFn, bs, 0, math.MaxInt64)
  65. if bErr != nil {
  66. return nil, bErr
  67. }
  68. delta = append(delta, DoMinusChunks(aData, bData)...)
  69. delta = append(delta, DoMinusChunks(aMeta, bMeta)...)
  70. return
  71. }
  72. func DoMinusChunks(as, bs []*filer_pb.FileChunk) (delta []*filer_pb.FileChunk) {
  73. fileIds := make(map[string]bool)
  74. for _, interval := range bs {
  75. fileIds[interval.GetFileIdString()] = true
  76. }
  77. for _, chunk := range as {
  78. if _, found := fileIds[chunk.GetFileIdString()]; !found {
  79. delta = append(delta, chunk)
  80. }
  81. }
  82. return
  83. }
  84. type ChunkView struct {
  85. FileId string
  86. Offset int64
  87. Size uint64
  88. LogicOffset int64 // actual offset in the file, for the data specified via [offset, offset+size) in current chunk
  89. ChunkSize uint64
  90. CipherKey []byte
  91. IsGzipped bool
  92. }
  93. func (cv *ChunkView) IsFullChunk() bool {
  94. return cv.Size == cv.ChunkSize
  95. }
  96. func ViewFromChunks(lookupFileIdFn wdclient.LookupFileIdFunctionType, chunks []*filer_pb.FileChunk, offset int64, size int64) (views []*ChunkView) {
  97. visibles, _ := NonOverlappingVisibleIntervals(lookupFileIdFn, chunks, offset, offset+size)
  98. return ViewFromVisibleIntervals(visibles, offset, size)
  99. }
  100. func ViewFromVisibleIntervals(visibles []VisibleInterval, offset int64, size int64) (views []*ChunkView) {
  101. stop := offset + size
  102. if size == math.MaxInt64 {
  103. stop = math.MaxInt64
  104. }
  105. if stop < offset {
  106. stop = math.MaxInt64
  107. }
  108. for _, chunk := range visibles {
  109. chunkStart, chunkStop := max(offset, chunk.start), min(stop, chunk.stop)
  110. if chunkStart < chunkStop {
  111. views = append(views, &ChunkView{
  112. FileId: chunk.fileId,
  113. Offset: chunkStart - chunk.start + chunk.chunkOffset,
  114. Size: uint64(chunkStop - chunkStart),
  115. LogicOffset: chunkStart,
  116. ChunkSize: chunk.chunkSize,
  117. CipherKey: chunk.cipherKey,
  118. IsGzipped: chunk.isGzipped,
  119. })
  120. }
  121. }
  122. return views
  123. }
  124. func logPrintf(name string, visibles []VisibleInterval) {
  125. /*
  126. glog.V(0).Infof("%s len %d", name, len(visibles))
  127. for _, v := range visibles {
  128. glog.V(0).Infof("%s: [%d,%d) %s %d", name, v.start, v.stop, v.fileId, v.chunkOffset)
  129. }
  130. */
  131. }
  132. // NonOverlappingVisibleIntervals translates the file chunk into VisibleInterval in memory
  133. // If the file chunk content is a chunk manifest
  134. func NonOverlappingVisibleIntervals(lookupFileIdFn wdclient.LookupFileIdFunctionType, chunks []*filer_pb.FileChunk, startOffset int64, stopOffset int64) (visibles []VisibleInterval, err error) {
  135. chunks, _, err = ResolveChunkManifest(lookupFileIdFn, chunks, startOffset, stopOffset)
  136. visibles = readResolvedChunks(chunks)
  137. return
  138. }
  139. // find non-overlapping visible intervals
  140. // visible interval map to one file chunk
  141. type VisibleInterval struct {
  142. start int64
  143. stop int64
  144. modifiedTime int64
  145. fileId string
  146. chunkOffset int64
  147. chunkSize uint64
  148. cipherKey []byte
  149. isGzipped bool
  150. }
  151. func min(x, y int64) int64 {
  152. if x <= y {
  153. return x
  154. }
  155. return y
  156. }
  157. func max(x, y int64) int64 {
  158. if x <= y {
  159. return y
  160. }
  161. return x
  162. }