filechunk_group.go 4.4 KB

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