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.

244 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
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
5 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/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. ChunkSize uint64
  74. CipherKey []byte
  75. IsGzipped bool
  76. }
  77. func (cv *ChunkView) IsFullChunk() bool {
  78. return cv.Size == cv.ChunkSize
  79. }
  80. func ViewFromChunks(chunks []*filer_pb.FileChunk, offset int64, size int64) (views []*ChunkView) {
  81. visibles := NonOverlappingVisibleIntervals(chunks)
  82. return ViewFromVisibleIntervals(visibles, offset, size)
  83. }
  84. func ViewFromVisibleIntervals(visibles []VisibleInterval, offset int64, size int64) (views []*ChunkView) {
  85. stop := offset + size
  86. if size == math.MaxInt64 {
  87. stop = math.MaxInt64
  88. }
  89. if stop < offset {
  90. stop = math.MaxInt64
  91. }
  92. for _, chunk := range visibles {
  93. if chunk.start <= offset && offset < chunk.stop && offset < stop {
  94. views = append(views, &ChunkView{
  95. FileId: chunk.fileId,
  96. Offset: offset - chunk.start, // offset is the data starting location in this file id
  97. Size: uint64(min(chunk.stop, stop) - offset),
  98. LogicOffset: offset,
  99. ChunkSize: chunk.chunkSize,
  100. CipherKey: chunk.cipherKey,
  101. IsGzipped: chunk.isGzipped,
  102. })
  103. offset = min(chunk.stop, stop)
  104. }
  105. }
  106. return views
  107. }
  108. func logPrintf(name string, visibles []VisibleInterval) {
  109. /*
  110. log.Printf("%s len %d", name, len(visibles))
  111. for _, v := range visibles {
  112. log.Printf("%s: => %+v", name, v)
  113. }
  114. */
  115. }
  116. var bufPool = sync.Pool{
  117. New: func() interface{} {
  118. return new(VisibleInterval)
  119. },
  120. }
  121. func MergeIntoVisibles(visibles, newVisibles []VisibleInterval, chunk *filer_pb.FileChunk) []VisibleInterval {
  122. newV := newVisibleInterval(chunk.Offset, chunk.Offset+int64(chunk.Size), chunk.GetFileIdString(), chunk.Mtime, chunk.Size, chunk.CipherKey, chunk.IsGzipped)
  123. length := len(visibles)
  124. if length == 0 {
  125. return append(visibles, newV)
  126. }
  127. last := visibles[length-1]
  128. if last.stop <= chunk.Offset {
  129. return append(visibles, newV)
  130. }
  131. logPrintf(" before", visibles)
  132. for _, v := range visibles {
  133. if v.start < chunk.Offset && chunk.Offset < v.stop {
  134. newVisibles = append(newVisibles, newVisibleInterval(v.start, chunk.Offset, v.fileId, v.modifiedTime, chunk.Size, v.cipherKey, v.isGzipped))
  135. }
  136. chunkStop := chunk.Offset + int64(chunk.Size)
  137. if v.start < chunkStop && chunkStop < v.stop {
  138. newVisibles = append(newVisibles, newVisibleInterval(chunkStop, v.stop, v.fileId, v.modifiedTime, chunk.Size, v.cipherKey, v.isGzipped))
  139. }
  140. if chunkStop <= v.start || v.stop <= chunk.Offset {
  141. newVisibles = append(newVisibles, v)
  142. }
  143. }
  144. newVisibles = append(newVisibles, newV)
  145. logPrintf(" append", newVisibles)
  146. for i := len(newVisibles) - 1; i >= 0; i-- {
  147. if i > 0 && newV.start < newVisibles[i-1].start {
  148. newVisibles[i] = newVisibles[i-1]
  149. } else {
  150. newVisibles[i] = newV
  151. break
  152. }
  153. }
  154. logPrintf(" sorted", newVisibles)
  155. return newVisibles
  156. }
  157. func NonOverlappingVisibleIntervals(chunks []*filer_pb.FileChunk) (visibles []VisibleInterval) {
  158. sort.Slice(chunks, func(i, j int) bool {
  159. return chunks[i].Mtime < chunks[j].Mtime
  160. })
  161. var newVisibles []VisibleInterval
  162. for _, chunk := range chunks {
  163. newVisibles = MergeIntoVisibles(visibles, newVisibles, chunk)
  164. t := visibles[:0]
  165. visibles = newVisibles
  166. newVisibles = t
  167. logPrintf("add", visibles)
  168. }
  169. return
  170. }
  171. // find non-overlapping visible intervals
  172. // visible interval map to one file chunk
  173. type VisibleInterval struct {
  174. start int64
  175. stop int64
  176. modifiedTime int64
  177. fileId string
  178. chunkSize uint64
  179. cipherKey []byte
  180. isGzipped bool
  181. }
  182. func newVisibleInterval(start, stop int64, fileId string, modifiedTime int64, chunkSize uint64, cipherKey []byte, isGzipped bool) VisibleInterval {
  183. return VisibleInterval{
  184. start: start,
  185. stop: stop,
  186. fileId: fileId,
  187. modifiedTime: modifiedTime,
  188. chunkSize: chunkSize,
  189. cipherKey: cipherKey,
  190. isGzipped: isGzipped,
  191. }
  192. }
  193. func min(x, y int64) int64 {
  194. if x <= y {
  195. return x
  196. }
  197. return y
  198. }