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.

152 lines
4.3 KiB

  1. package filer
  2. import (
  3. "github.com/seaweedfs/seaweedfs/weed/pb/filer_pb"
  4. "github.com/seaweedfs/seaweedfs/weed/util/chunk_cache"
  5. "github.com/seaweedfs/seaweedfs/weed/wdclient"
  6. "sync"
  7. )
  8. type ChunkGroup struct {
  9. lookupFn wdclient.LookupFileIdFunctionType
  10. chunkCache chunk_cache.ChunkCache
  11. manifestChunks []*filer_pb.FileChunk
  12. sections map[SectionIndex]*FileChunkSection
  13. sectionsLock sync.RWMutex
  14. }
  15. func NewChunkGroup(lookupFn wdclient.LookupFileIdFunctionType, chunkCache chunk_cache.ChunkCache, chunks []*filer_pb.FileChunk) (*ChunkGroup, error) {
  16. group := &ChunkGroup{
  17. lookupFn: lookupFn,
  18. chunkCache: chunkCache,
  19. sections: make(map[SectionIndex]*FileChunkSection),
  20. }
  21. err := group.SetChunks(chunks)
  22. return group, err
  23. }
  24. func (group *ChunkGroup) AddChunk(chunk *filer_pb.FileChunk) error {
  25. group.sectionsLock.Lock()
  26. defer group.sectionsLock.Unlock()
  27. sectionIndexStart, sectionIndexStop := SectionIndex(chunk.Offset/SectionSize), SectionIndex((chunk.Offset+int64(chunk.Size))/SectionSize)
  28. for si := sectionIndexStart; si < sectionIndexStop+1; si++ {
  29. section, found := group.sections[si]
  30. if !found {
  31. section = &FileChunkSection{
  32. sectionIndex: si,
  33. }
  34. group.sections[si] = section
  35. }
  36. section.addChunk(chunk)
  37. }
  38. return nil
  39. }
  40. func (group *ChunkGroup) ReadDataAt(fileSize int64, buff []byte, offset int64) (n int, tsNs int64, err error) {
  41. group.sectionsLock.RLock()
  42. defer group.sectionsLock.RUnlock()
  43. sectionIndexStart, sectionIndexStop := SectionIndex(offset/SectionSize), SectionIndex((offset+int64(len(buff)))/SectionSize)
  44. for si := sectionIndexStart; si < sectionIndexStop+1; si++ {
  45. section, found := group.sections[si]
  46. rangeStart, rangeStop := max(offset, int64(si*SectionSize)), min(offset+int64(len(buff)), int64((si+1)*SectionSize))
  47. if !found {
  48. for i := rangeStart; i < rangeStop; i++ {
  49. buff[i-offset] = 0
  50. }
  51. continue
  52. }
  53. xn, xTsNs, xErr := section.readDataAt(group, fileSize, buff[rangeStart-offset:rangeStop-offset], rangeStart)
  54. if xErr != nil {
  55. err = xErr
  56. }
  57. n += xn
  58. tsNs = max(tsNs, xTsNs)
  59. }
  60. return
  61. }
  62. func (group *ChunkGroup) SetChunks(chunks []*filer_pb.FileChunk) error {
  63. var dataChunks []*filer_pb.FileChunk
  64. for _, chunk := range chunks {
  65. if !chunk.IsChunkManifest {
  66. dataChunks = append(dataChunks, chunk)
  67. continue
  68. }
  69. resolvedChunks, err := ResolveOneChunkManifest(group.lookupFn, chunk)
  70. if err != nil {
  71. return err
  72. }
  73. group.manifestChunks = append(group.manifestChunks, chunk)
  74. dataChunks = append(dataChunks, resolvedChunks...)
  75. }
  76. for _, chunk := range dataChunks {
  77. sectionIndexStart, sectionIndexStop := SectionIndex(chunk.Offset/SectionSize), SectionIndex((chunk.Offset+int64(chunk.Size))/SectionSize)
  78. for si := sectionIndexStart; si < sectionIndexStop+1; si++ {
  79. section, found := group.sections[si]
  80. if !found {
  81. section = &FileChunkSection{
  82. sectionIndex: si,
  83. }
  84. group.sections[si] = section
  85. }
  86. section.chunks = append(section.chunks, chunk)
  87. }
  88. }
  89. return nil
  90. }
  91. const (
  92. // see weedfs_file_lseek.go
  93. SEEK_DATA uint32 = 3 // seek to next data after the offset
  94. // SEEK_HOLE uint32 = 4 // seek to next hole after the offset
  95. )
  96. // FIXME: needa tests
  97. func (group *ChunkGroup) SearchChunks(offset, fileSize int64, whence uint32) (found bool, out int64) {
  98. group.sectionsLock.RLock()
  99. defer group.sectionsLock.RUnlock()
  100. return group.doSearchChunks(offset, fileSize, whence)
  101. }
  102. func (group *ChunkGroup) doSearchChunks(offset, fileSize int64, whence uint32) (found bool, out int64) {
  103. sectionIndex, maxSectionIndex := SectionIndex(offset/SectionSize), SectionIndex(fileSize/SectionSize)
  104. if whence == SEEK_DATA {
  105. for si := sectionIndex; si < maxSectionIndex+1; si++ {
  106. section, foundSection := group.sections[si]
  107. if !foundSection {
  108. continue
  109. }
  110. sectionStart := section.DataStartOffset(group, offset, fileSize)
  111. if sectionStart == -1 {
  112. continue
  113. }
  114. return true, sectionStart
  115. }
  116. return false, 0
  117. } else {
  118. // whence == SEEK_HOLE
  119. for si := sectionIndex; si < maxSectionIndex; si++ {
  120. section, foundSection := group.sections[si]
  121. if !foundSection {
  122. return true, offset
  123. }
  124. holeStart := section.NextStopOffset(group, offset, fileSize)
  125. if holeStart%SectionSize == 0 {
  126. continue
  127. }
  128. return true, holeStart
  129. }
  130. return true, fileSize
  131. }
  132. }