filechunk_group.go 4.4 KB

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