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.

205 lines
5.3 KiB

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