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.

204 lines
5.4 KiB

4 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
  1. package filer
  2. import (
  3. "bytes"
  4. "io"
  5. "math"
  6. "strings"
  7. "github.com/chrislusf/seaweedfs/weed/glog"
  8. "github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
  9. "github.com/chrislusf/seaweedfs/weed/util"
  10. "github.com/chrislusf/seaweedfs/weed/wdclient"
  11. )
  12. func StreamContent(masterClient *wdclient.MasterClient, w io.Writer, chunks []*filer_pb.FileChunk, offset int64, size int64) error {
  13. // fmt.Printf("start to stream content for chunks: %+v\n", chunks)
  14. chunkViews := ViewFromChunks(masterClient.LookupFileId, chunks, offset, size)
  15. fileId2Url := make(map[string]string)
  16. for _, chunkView := range chunkViews {
  17. urlString, err := masterClient.LookupFileId(chunkView.FileId)
  18. if err != nil {
  19. glog.V(1).Infof("operation LookupFileId %s failed, err: %v", chunkView.FileId, err)
  20. return err
  21. }
  22. fileId2Url[chunkView.FileId] = urlString
  23. }
  24. for _, chunkView := range chunkViews {
  25. urlString := fileId2Url[chunkView.FileId]
  26. err := util.ReadUrlAsStream(urlString+"?readDeleted=true", chunkView.CipherKey, chunkView.IsGzipped, chunkView.IsFullChunk(), chunkView.Offset, int(chunkView.Size), func(data []byte) {
  27. w.Write(data)
  28. })
  29. if err != nil {
  30. glog.V(1).Infof("read %s failed, err: %v", chunkView.FileId, err)
  31. return err
  32. }
  33. }
  34. return nil
  35. }
  36. // ---------------- ReadAllReader ----------------------------------
  37. func ReadAll(masterClient *wdclient.MasterClient, chunks []*filer_pb.FileChunk) ([]byte, error) {
  38. buffer := bytes.Buffer{}
  39. lookupFileIdFn := func(fileId string) (targetUrl string, err error) {
  40. return masterClient.LookupFileId(fileId)
  41. }
  42. chunkViews := ViewFromChunks(lookupFileIdFn, chunks, 0, math.MaxInt64)
  43. for _, chunkView := range chunkViews {
  44. urlString, err := lookupFileIdFn(chunkView.FileId)
  45. if err != nil {
  46. glog.V(1).Infof("operation LookupFileId %s failed, err: %v", chunkView.FileId, err)
  47. return nil, err
  48. }
  49. err = util.ReadUrlAsStream(urlString+"?readDeleted=true", chunkView.CipherKey, chunkView.IsGzipped, chunkView.IsFullChunk(), chunkView.Offset, int(chunkView.Size), func(data []byte) {
  50. buffer.Write(data)
  51. })
  52. if err != nil {
  53. glog.V(1).Infof("read %s failed, err: %v", chunkView.FileId, err)
  54. return nil, err
  55. }
  56. }
  57. return buffer.Bytes(), nil
  58. }
  59. // ---------------- ChunkStreamReader ----------------------------------
  60. type ChunkStreamReader struct {
  61. chunkViews []*ChunkView
  62. logicOffset int64
  63. buffer []byte
  64. bufferOffset int64
  65. bufferPos int
  66. chunkIndex int
  67. lookupFileId LookupFileIdFunctionType
  68. }
  69. var _ = io.ReadSeeker(&ChunkStreamReader{})
  70. func NewChunkStreamReaderFromFiler(masterClient *wdclient.MasterClient, chunks []*filer_pb.FileChunk) *ChunkStreamReader {
  71. lookupFileIdFn := func(fileId string) (targetUrl string, err error) {
  72. return masterClient.LookupFileId(fileId)
  73. }
  74. chunkViews := ViewFromChunks(lookupFileIdFn, chunks, 0, math.MaxInt64)
  75. return &ChunkStreamReader{
  76. chunkViews: chunkViews,
  77. lookupFileId: lookupFileIdFn,
  78. }
  79. }
  80. func NewChunkStreamReader(filerClient filer_pb.FilerClient, chunks []*filer_pb.FileChunk) *ChunkStreamReader {
  81. lookupFileIdFn := LookupFn(filerClient)
  82. chunkViews := ViewFromChunks(lookupFileIdFn, chunks, 0, math.MaxInt64)
  83. return &ChunkStreamReader{
  84. chunkViews: chunkViews,
  85. lookupFileId: lookupFileIdFn,
  86. }
  87. }
  88. func (c *ChunkStreamReader) Read(p []byte) (n int, err error) {
  89. for n < len(p) {
  90. if c.isBufferEmpty() {
  91. if c.chunkIndex >= len(c.chunkViews) {
  92. return n, io.EOF
  93. }
  94. chunkView := c.chunkViews[c.chunkIndex]
  95. c.fetchChunkToBuffer(chunkView)
  96. c.chunkIndex++
  97. }
  98. t := copy(p[n:], c.buffer[c.bufferPos:])
  99. c.bufferPos += t
  100. n += t
  101. }
  102. return
  103. }
  104. func (c *ChunkStreamReader) isBufferEmpty() bool {
  105. return len(c.buffer) <= c.bufferPos
  106. }
  107. func (c *ChunkStreamReader) Seek(offset int64, whence int) (int64, error) {
  108. var totalSize int64
  109. for _, chunk := range c.chunkViews {
  110. totalSize += int64(chunk.Size)
  111. }
  112. var err error
  113. switch whence {
  114. case io.SeekStart:
  115. case io.SeekCurrent:
  116. offset += c.bufferOffset + int64(c.bufferPos)
  117. case io.SeekEnd:
  118. offset = totalSize + offset
  119. }
  120. if offset > totalSize {
  121. err = io.ErrUnexpectedEOF
  122. }
  123. for i, chunk := range c.chunkViews {
  124. if chunk.LogicOffset <= offset && offset < chunk.LogicOffset+int64(chunk.Size) {
  125. if c.isBufferEmpty() || c.bufferOffset != chunk.LogicOffset {
  126. c.fetchChunkToBuffer(chunk)
  127. c.chunkIndex = i + 1
  128. break
  129. }
  130. }
  131. }
  132. c.bufferPos = int(offset - c.bufferOffset)
  133. return offset, err
  134. }
  135. func (c *ChunkStreamReader) fetchChunkToBuffer(chunkView *ChunkView) error {
  136. urlString, err := c.lookupFileId(chunkView.FileId)
  137. if err != nil {
  138. glog.V(1).Infof("operation LookupFileId %s failed, err: %v", chunkView.FileId, err)
  139. return err
  140. }
  141. var buffer bytes.Buffer
  142. err = util.ReadUrlAsStream(urlString+"?readDeleted=true", chunkView.CipherKey, chunkView.IsGzipped, chunkView.IsFullChunk(), chunkView.Offset, int(chunkView.Size), func(data []byte) {
  143. buffer.Write(data)
  144. })
  145. if err != nil {
  146. glog.V(1).Infof("read %s failed, err: %v", chunkView.FileId, err)
  147. return err
  148. }
  149. c.buffer = buffer.Bytes()
  150. c.bufferPos = 0
  151. c.bufferOffset = chunkView.LogicOffset
  152. // glog.V(0).Infof("read %s [%d,%d)", chunkView.FileId, chunkView.LogicOffset, chunkView.LogicOffset+int64(chunkView.Size))
  153. return nil
  154. }
  155. func (c *ChunkStreamReader) Close() {
  156. // TODO try to release and reuse buffer
  157. }
  158. func VolumeId(fileId string) string {
  159. lastCommaIndex := strings.LastIndex(fileId, ",")
  160. if lastCommaIndex > 0 {
  161. return fileId[:lastCommaIndex]
  162. }
  163. return fileId
  164. }