filechunks.go 6.5 KB

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