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.

277 lines
6.6 KiB

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