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.

276 lines
6.5 KiB

7 years ago
7 years ago
5 years ago
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
7 years ago
6 years ago
7 years ago
7 years ago
6 years ago
6 years ago
6 years ago
5 years ago
7 years ago
6 years ago
5 years ago
7 years ago
6 years ago
7 years ago
6 years ago
6 years ago
6 years ago
7 years ago
7 years ago
6 years ago
7 years ago
6 years ago
7 years ago
6 years ago
7 years ago
7 years ago
5 years ago
7 years ago
5 years ago
5 years ago
7 years ago
  1. package filer2
  2. import (
  3. "fmt"
  4. "hash/fnv"
  5. "math"
  6. "sort"
  7. "sync"
  8. "github.com/chrislusf/seaweedfs/weed/glog"
  9. "github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
  10. )
  11. func TotalSize(chunks []*filer_pb.FileChunk) (size uint64) {
  12. for _, c := range chunks {
  13. t := uint64(c.Offset + int64(c.Size))
  14. if size < t {
  15. size = t
  16. }
  17. }
  18. return
  19. }
  20. func FileSize(entry *filer_pb.Entry) (size uint64) {
  21. return maxUint64(TotalSize(entry.Chunks), entry.Attributes.FileSize)
  22. }
  23. func ETag(entry *filer_pb.Entry) (etag string) {
  24. if entry.Attributes == nil || entry.Attributes.Md5 == nil {
  25. return ETagChunks(entry.Chunks)
  26. }
  27. return fmt.Sprintf("%x", entry.Attributes.Md5)
  28. }
  29. func ETagEntry(entry *Entry) (etag string) {
  30. if entry.Attr.Md5 == nil {
  31. return ETagChunks(entry.Chunks)
  32. }
  33. return fmt.Sprintf("%x", entry.Attr.Md5)
  34. }
  35. func ETagChunks(chunks []*filer_pb.FileChunk) (etag string) {
  36. if len(chunks) == 1 {
  37. return chunks[0].ETag
  38. }
  39. h := fnv.New32a()
  40. for _, c := range chunks {
  41. h.Write([]byte(c.ETag))
  42. }
  43. return fmt.Sprintf("%x", h.Sum32())
  44. }
  45. func CompactFileChunks(lookupFileIdFn LookupFileIdFunctionType, chunks []*filer_pb.FileChunk) (compacted, garbage []*filer_pb.FileChunk) {
  46. visibles, _ := NonOverlappingVisibleIntervals(lookupFileIdFn, chunks)
  47. fileIds := make(map[string]bool)
  48. for _, interval := range visibles {
  49. fileIds[interval.fileId] = true
  50. }
  51. for _, chunk := range chunks {
  52. if _, found := fileIds[chunk.GetFileIdString()]; found {
  53. compacted = append(compacted, chunk)
  54. } else {
  55. garbage = append(garbage, chunk)
  56. }
  57. }
  58. return
  59. }
  60. func MinusChunks(lookupFileIdFn LookupFileIdFunctionType, as, bs []*filer_pb.FileChunk) (delta []*filer_pb.FileChunk, err error) {
  61. aData, aMeta, aErr := ResolveChunkManifest(lookupFileIdFn, as)
  62. if aErr != nil {
  63. return nil, aErr
  64. }
  65. bData, bMeta, bErr := ResolveChunkManifest(lookupFileIdFn, bs)
  66. if bErr != nil {
  67. return nil, bErr
  68. }
  69. delta = append(delta, DoMinusChunks(aData, bData)...)
  70. delta = append(delta, DoMinusChunks(aMeta, bMeta)...)
  71. return
  72. }
  73. func DoMinusChunks(as, bs []*filer_pb.FileChunk) (delta []*filer_pb.FileChunk) {
  74. fileIds := make(map[string]bool)
  75. for _, interval := range bs {
  76. fileIds[interval.GetFileIdString()] = true
  77. }
  78. for _, chunk := range as {
  79. if _, found := fileIds[chunk.GetFileIdString()]; !found {
  80. delta = append(delta, chunk)
  81. }
  82. }
  83. return
  84. }
  85. type ChunkView struct {
  86. FileId string
  87. Offset int64
  88. Size uint64
  89. LogicOffset int64
  90. ChunkSize uint64
  91. CipherKey []byte
  92. IsGzipped bool
  93. }
  94. func (cv *ChunkView) IsFullChunk() bool {
  95. return cv.Size == cv.ChunkSize
  96. }
  97. func ViewFromChunks(lookupFileIdFn LookupFileIdFunctionType, chunks []*filer_pb.FileChunk, offset int64, size int64) (views []*ChunkView) {
  98. visibles, _ := NonOverlappingVisibleIntervals(lookupFileIdFn, chunks)
  99. return ViewFromVisibleIntervals(visibles, offset, size)
  100. }
  101. func ViewFromVisibleIntervals(visibles []VisibleInterval, offset int64, size int64) (views []*ChunkView) {
  102. stop := offset + size
  103. if size == math.MaxInt64 {
  104. stop = math.MaxInt64
  105. }
  106. if stop < offset {
  107. stop = math.MaxInt64
  108. }
  109. for _, chunk := range visibles {
  110. chunkStart, chunkStop := max(offset, chunk.start), min(stop, chunk.stop)
  111. if chunkStart < chunkStop {
  112. views = append(views, &ChunkView{
  113. FileId: chunk.fileId,
  114. Offset: chunkStart-chunk.start,
  115. Size: uint64(chunkStop - chunkStart),
  116. LogicOffset: chunkStart,
  117. ChunkSize: chunk.chunkSize,
  118. CipherKey: chunk.cipherKey,
  119. IsGzipped: chunk.isGzipped,
  120. })
  121. }
  122. }
  123. return views
  124. }
  125. func logPrintf(name string, visibles []VisibleInterval) {
  126. /*
  127. log.Printf("%s len %d", name, len(visibles))
  128. for _, v := range visibles {
  129. log.Printf("%s: => %+v", name, v)
  130. }
  131. */
  132. }
  133. var bufPool = sync.Pool{
  134. New: func() interface{} {
  135. return new(VisibleInterval)
  136. },
  137. }
  138. func MergeIntoVisibles(visibles, newVisibles []VisibleInterval, chunk *filer_pb.FileChunk) []VisibleInterval {
  139. newV := newVisibleInterval(chunk.Offset, chunk.Offset+int64(chunk.Size), chunk.GetFileIdString(), chunk.Mtime, chunk.Size, chunk.CipherKey, chunk.IsCompressed)
  140. length := len(visibles)
  141. if length == 0 {
  142. return append(visibles, newV)
  143. }
  144. last := visibles[length-1]
  145. if last.stop <= chunk.Offset {
  146. return append(visibles, newV)
  147. }
  148. logPrintf(" before", visibles)
  149. for _, v := range visibles {
  150. if v.start < chunk.Offset && chunk.Offset < v.stop {
  151. newVisibles = append(newVisibles, newVisibleInterval(v.start, chunk.Offset, v.fileId, v.modifiedTime, chunk.Size, v.cipherKey, v.isGzipped))
  152. }
  153. chunkStop := chunk.Offset + int64(chunk.Size)
  154. if v.start < chunkStop && chunkStop < v.stop {
  155. newVisibles = append(newVisibles, newVisibleInterval(chunkStop, v.stop, v.fileId, v.modifiedTime, chunk.Size, v.cipherKey, v.isGzipped))
  156. }
  157. if chunkStop <= v.start || v.stop <= chunk.Offset {
  158. newVisibles = append(newVisibles, v)
  159. }
  160. }
  161. newVisibles = append(newVisibles, newV)
  162. logPrintf(" append", newVisibles)
  163. for i := len(newVisibles) - 1; i >= 0; i-- {
  164. if i > 0 && newV.start < newVisibles[i-1].start {
  165. newVisibles[i] = newVisibles[i-1]
  166. } else {
  167. newVisibles[i] = newV
  168. break
  169. }
  170. }
  171. logPrintf(" sorted", newVisibles)
  172. return newVisibles
  173. }
  174. // NonOverlappingVisibleIntervals translates the file chunk into VisibleInterval in memory
  175. // If the file chunk content is a chunk manifest
  176. func NonOverlappingVisibleIntervals(lookupFileIdFn LookupFileIdFunctionType, chunks []*filer_pb.FileChunk) (visibles []VisibleInterval, err error) {
  177. chunks, _, err = ResolveChunkManifest(lookupFileIdFn, chunks)
  178. sort.Slice(chunks, func(i, j int) bool {
  179. return chunks[i].Mtime < chunks[j].Mtime
  180. })
  181. var newVisibles []VisibleInterval
  182. for _, chunk := range chunks {
  183. newVisibles = MergeIntoVisibles(visibles, newVisibles, chunk)
  184. t := visibles[:0]
  185. visibles = newVisibles
  186. newVisibles = t
  187. logPrintf("add", visibles)
  188. }
  189. return
  190. }
  191. // find non-overlapping visible intervals
  192. // visible interval map to one file chunk
  193. type VisibleInterval struct {
  194. start int64
  195. stop int64
  196. modifiedTime int64
  197. fileId string
  198. chunkSize uint64
  199. cipherKey []byte
  200. isGzipped bool
  201. }
  202. func newVisibleInterval(start, stop int64, fileId string, modifiedTime int64, chunkSize uint64, cipherKey []byte, isGzipped bool) VisibleInterval {
  203. return VisibleInterval{
  204. start: start,
  205. stop: stop,
  206. fileId: fileId,
  207. modifiedTime: modifiedTime,
  208. chunkSize: chunkSize,
  209. cipherKey: cipherKey,
  210. isGzipped: isGzipped,
  211. }
  212. }
  213. func min(x, y int64) int64 {
  214. if x <= y {
  215. return x
  216. }
  217. return y
  218. }
  219. func max(x, y int64) int64 {
  220. if x <= y {
  221. return y
  222. }
  223. return x
  224. }