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.

214 lines
4.4 KiB

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