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.9 KiB

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