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.

189 lines
4.9 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. "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 func(fileId string) (targetUrl string, err error)
  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 (c *ChunkStreamReader) Read(p []byte) (n int, err error) {
  79. for n < len(p) {
  80. if c.isBufferEmpty() {
  81. if c.chunkIndex >= len(c.chunkViews) {
  82. return n, io.EOF
  83. }
  84. chunkView := c.chunkViews[c.chunkIndex]
  85. c.fetchChunkToBuffer(chunkView)
  86. c.chunkIndex++
  87. }
  88. t := copy(p[n:], c.buffer[c.bufferPos:])
  89. c.bufferPos += t
  90. n += t
  91. }
  92. return
  93. }
  94. func (c *ChunkStreamReader) isBufferEmpty() bool {
  95. return len(c.buffer) <= c.bufferPos
  96. }
  97. func (c *ChunkStreamReader) Seek(offset int64, whence int) (int64, error) {
  98. var totalSize int64
  99. for _, chunk := range c.chunkViews {
  100. totalSize += int64(chunk.Size)
  101. }
  102. var err error
  103. switch whence {
  104. case io.SeekStart:
  105. case io.SeekCurrent:
  106. offset += c.bufferOffset + int64(c.bufferPos)
  107. case io.SeekEnd:
  108. offset = totalSize + offset
  109. }
  110. if offset > totalSize {
  111. err = io.ErrUnexpectedEOF
  112. }
  113. for i, chunk := range c.chunkViews {
  114. if chunk.LogicOffset <= offset && offset < chunk.LogicOffset+int64(chunk.Size) {
  115. if c.isBufferEmpty() || c.bufferOffset != chunk.LogicOffset {
  116. c.fetchChunkToBuffer(chunk)
  117. c.chunkIndex = i + 1
  118. break
  119. }
  120. }
  121. }
  122. c.bufferPos = int(offset - c.bufferOffset)
  123. return offset, err
  124. }
  125. func (c *ChunkStreamReader) fetchChunkToBuffer(chunkView *ChunkView) error {
  126. urlString, err := c.lookupFileId(chunkView.FileId)
  127. if err != nil {
  128. glog.V(1).Infof("operation LookupFileId %s failed, err: %v", chunkView.FileId, err)
  129. return err
  130. }
  131. var buffer bytes.Buffer
  132. err = util.ReadUrlAsStream(urlString, chunkView.CipherKey, chunkView.IsGzipped, chunkView.IsFullChunk(), chunkView.Offset, int(chunkView.Size), func(data []byte) {
  133. buffer.Write(data)
  134. })
  135. if err != nil {
  136. glog.V(1).Infof("read %s failed, err: %v", chunkView.FileId, err)
  137. return err
  138. }
  139. c.buffer = buffer.Bytes()
  140. c.bufferPos = 0
  141. c.bufferOffset = chunkView.LogicOffset
  142. // glog.V(0).Infof("read %s [%d,%d)", chunkView.FileId, chunkView.LogicOffset, chunkView.LogicOffset+int64(chunkView.Size))
  143. return nil
  144. }
  145. func (c *ChunkStreamReader) Close() {
  146. // TODO try to release and reuse buffer
  147. }
  148. func VolumeId(fileId string) string {
  149. lastCommaIndex := strings.LastIndex(fileId, ",")
  150. if lastCommaIndex > 0 {
  151. return fileId[:lastCommaIndex]
  152. }
  153. return fileId
  154. }