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.

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