filechunk_group.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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. 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. chunkCache: chunkCache,
  19. sections: make(map[SectionIndex]*FileChunkSection),
  20. readerCache: NewReaderCache(32, chunkCache, lookupFn),
  21. }
  22. err := group.SetChunks(chunks)
  23. return group, err
  24. }
  25. func (group *ChunkGroup) AddChunk(chunk *filer_pb.FileChunk) error {
  26. group.sectionsLock.Lock()
  27. defer group.sectionsLock.Unlock()
  28. sectionIndexStart, sectionIndexStop := SectionIndex(chunk.Offset/SectionSize), SectionIndex((chunk.Offset+int64(chunk.Size))/SectionSize)
  29. for si := sectionIndexStart; si < sectionIndexStop+1; si++ {
  30. section, found := group.sections[si]
  31. if !found {
  32. section = NewFileChunkSection(si)
  33. group.sections[si] = section
  34. }
  35. section.addChunk(chunk)
  36. }
  37. return nil
  38. }
  39. func (group *ChunkGroup) ReadDataAt(fileSize int64, buff []byte, offset int64) (n int, tsNs int64, err error) {
  40. group.sectionsLock.RLock()
  41. defer group.sectionsLock.RUnlock()
  42. sectionIndexStart, sectionIndexStop := SectionIndex(offset/SectionSize), SectionIndex((offset+int64(len(buff)))/SectionSize)
  43. for si := sectionIndexStart; si < sectionIndexStop+1; si++ {
  44. section, found := group.sections[si]
  45. rangeStart, rangeStop := max(offset, int64(si*SectionSize)), min(offset+int64(len(buff)), int64((si+1)*SectionSize))
  46. if !found {
  47. for i := rangeStart; i < rangeStop; i++ {
  48. buff[i-offset] = 0
  49. }
  50. continue
  51. }
  52. xn, xTsNs, xErr := section.readDataAt(group, fileSize, buff[rangeStart-offset:rangeStop-offset], rangeStart)
  53. if xErr != nil {
  54. err = xErr
  55. }
  56. n += xn
  57. tsNs = max(tsNs, xTsNs)
  58. }
  59. return
  60. }
  61. func (group *ChunkGroup) SetChunks(chunks []*filer_pb.FileChunk) error {
  62. group.sectionsLock.RLock()
  63. defer group.sectionsLock.RUnlock()
  64. var dataChunks []*filer_pb.FileChunk
  65. for _, chunk := range chunks {
  66. if !chunk.IsChunkManifest {
  67. dataChunks = append(dataChunks, chunk)
  68. continue
  69. }
  70. resolvedChunks, err := ResolveOneChunkManifest(group.lookupFn, chunk)
  71. if err != nil {
  72. return err
  73. }
  74. dataChunks = append(dataChunks, resolvedChunks...)
  75. }
  76. sections := make(map[SectionIndex]*FileChunkSection)
  77. for _, chunk := range dataChunks {
  78. sectionIndexStart, sectionIndexStop := SectionIndex(chunk.Offset/SectionSize), SectionIndex((chunk.Offset+int64(chunk.Size))/SectionSize)
  79. for si := sectionIndexStart; si < sectionIndexStop+1; si++ {
  80. section, found := sections[si]
  81. if !found {
  82. section = NewFileChunkSection(si)
  83. sections[si] = section
  84. }
  85. section.chunks = append(section.chunks, chunk)
  86. }
  87. }
  88. group.sections = sections
  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. }