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.

227 lines
5.1 KiB

7 years ago
6 years ago
5 years ago
7 years ago
6 years ago
7 years ago
7 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
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
7 years ago
6 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
7 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/pb/filer_pb"
  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 ETag(chunks []*filer_pb.FileChunk) (etag string) {
  20. if len(chunks) == 1 {
  21. return chunks[0].ETag
  22. }
  23. h := fnv.New32a()
  24. for _, c := range chunks {
  25. h.Write([]byte(c.ETag))
  26. }
  27. return fmt.Sprintf("%x", h.Sum32())
  28. }
  29. func CompactFileChunks(chunks []*filer_pb.FileChunk) (compacted, garbage []*filer_pb.FileChunk) {
  30. visibles := NonOverlappingVisibleIntervals(chunks)
  31. fileIds := make(map[string]bool)
  32. for _, interval := range visibles {
  33. fileIds[interval.fileId] = true
  34. }
  35. for _, chunk := range chunks {
  36. if _, found := fileIds[chunk.GetFileIdString()]; found {
  37. compacted = append(compacted, chunk)
  38. } else {
  39. garbage = append(garbage, chunk)
  40. }
  41. }
  42. return
  43. }
  44. func MinusChunks(as, bs []*filer_pb.FileChunk) (delta []*filer_pb.FileChunk) {
  45. fileIds := make(map[string]bool)
  46. for _, interval := range bs {
  47. fileIds[interval.GetFileIdString()] = true
  48. }
  49. for _, chunk := range as {
  50. if _, found := fileIds[chunk.GetFileIdString()]; !found {
  51. delta = append(delta, chunk)
  52. }
  53. }
  54. return
  55. }
  56. type ChunkView struct {
  57. FileId string
  58. Offset int64
  59. Size uint64
  60. LogicOffset int64
  61. IsFullChunk bool
  62. CipherKey []byte
  63. IsGzipped bool
  64. }
  65. func ViewFromChunks(chunks []*filer_pb.FileChunk, offset int64, size int64) (views []*ChunkView) {
  66. visibles := NonOverlappingVisibleIntervals(chunks)
  67. return ViewFromVisibleIntervals(visibles, offset, size)
  68. }
  69. func ViewFromVisibleIntervals(visibles []VisibleInterval, offset int64, size int64) (views []*ChunkView) {
  70. stop := offset + size
  71. if size == math.MaxInt64 {
  72. stop = math.MaxInt64
  73. }
  74. if stop < offset {
  75. stop = math.MaxInt64
  76. }
  77. for _, chunk := range visibles {
  78. if chunk.start <= offset && offset < chunk.stop && offset < stop {
  79. isFullChunk := chunk.isFullChunk && chunk.start == offset && chunk.stop <= stop
  80. views = append(views, &ChunkView{
  81. FileId: chunk.fileId,
  82. Offset: offset - chunk.start, // offset is the data starting location in this file id
  83. Size: uint64(min(chunk.stop, stop) - offset),
  84. LogicOffset: offset,
  85. IsFullChunk: isFullChunk,
  86. CipherKey: chunk.cipherKey,
  87. IsGzipped: chunk.isGzipped,
  88. })
  89. offset = min(chunk.stop, stop)
  90. }
  91. }
  92. return views
  93. }
  94. func logPrintf(name string, visibles []VisibleInterval) {
  95. /*
  96. log.Printf("%s len %d", name, len(visibles))
  97. for _, v := range visibles {
  98. log.Printf("%s: => %+v", name, v)
  99. }
  100. */
  101. }
  102. var bufPool = sync.Pool{
  103. New: func() interface{} {
  104. return new(VisibleInterval)
  105. },
  106. }
  107. func MergeIntoVisibles(visibles, newVisibles []VisibleInterval, chunk *filer_pb.FileChunk) []VisibleInterval {
  108. newV := newVisibleInterval(chunk.Offset, chunk.Offset+int64(chunk.Size), chunk.GetFileIdString(), chunk.Mtime, true, chunk.CipherKey, chunk.IsGzipped)
  109. length := len(visibles)
  110. if length == 0 {
  111. return append(visibles, newV)
  112. }
  113. last := visibles[length-1]
  114. if last.stop <= chunk.Offset {
  115. return append(visibles, newV)
  116. }
  117. logPrintf(" before", visibles)
  118. for _, v := range visibles {
  119. if v.start < chunk.Offset && chunk.Offset < v.stop {
  120. newVisibles = append(newVisibles, newVisibleInterval(v.start, chunk.Offset, v.fileId, v.modifiedTime, false, v.cipherKey, v.isGzipped))
  121. }
  122. chunkStop := chunk.Offset + int64(chunk.Size)
  123. if v.start < chunkStop && chunkStop < v.stop {
  124. newVisibles = append(newVisibles, newVisibleInterval(chunkStop, v.stop, v.fileId, v.modifiedTime, false, v.cipherKey, v.isGzipped))
  125. }
  126. if chunkStop <= v.start || v.stop <= chunk.Offset {
  127. newVisibles = append(newVisibles, v)
  128. }
  129. }
  130. newVisibles = append(newVisibles, newV)
  131. logPrintf(" append", newVisibles)
  132. for i := len(newVisibles) - 1; i >= 0; i-- {
  133. if i > 0 && newV.start < newVisibles[i-1].start {
  134. newVisibles[i] = newVisibles[i-1]
  135. } else {
  136. newVisibles[i] = newV
  137. break
  138. }
  139. }
  140. logPrintf(" sorted", newVisibles)
  141. return newVisibles
  142. }
  143. func NonOverlappingVisibleIntervals(chunks []*filer_pb.FileChunk) (visibles []VisibleInterval) {
  144. sort.Slice(chunks, func(i, j int) bool {
  145. return chunks[i].Mtime < chunks[j].Mtime
  146. })
  147. var newVisibles []VisibleInterval
  148. for _, chunk := range chunks {
  149. newVisibles = MergeIntoVisibles(visibles, newVisibles, chunk)
  150. t := visibles[:0]
  151. visibles = newVisibles
  152. newVisibles = t
  153. logPrintf("add", visibles)
  154. }
  155. return
  156. }
  157. // find non-overlapping visible intervals
  158. // visible interval map to one file chunk
  159. type VisibleInterval struct {
  160. start int64
  161. stop int64
  162. modifiedTime int64
  163. fileId string
  164. isFullChunk bool
  165. cipherKey []byte
  166. isGzipped bool
  167. }
  168. func newVisibleInterval(start, stop int64, fileId string, modifiedTime int64, isFullChunk bool, cipherKey []byte, isGzipped bool) VisibleInterval {
  169. return VisibleInterval{
  170. start: start,
  171. stop: stop,
  172. fileId: fileId,
  173. modifiedTime: modifiedTime,
  174. isFullChunk: isFullChunk,
  175. cipherKey: cipherKey,
  176. isGzipped: isGzipped,
  177. }
  178. }
  179. func min(x, y int64) int64 {
  180. if x <= y {
  181. return x
  182. }
  183. return y
  184. }