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.

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