Browse Source

refactor

pull/2252/head
Chris Lu 3 years ago
parent
commit
ecb234f75a
  1. 22
      weed/filer/stream.go

22
weed/filer/stream.go

@ -89,6 +89,7 @@ func ReadAll(masterClient *wdclient.MasterClient, chunks []*filer_pb.FileChunk)
// ---------------- ChunkStreamReader ---------------------------------- // ---------------- ChunkStreamReader ----------------------------------
type ChunkStreamReader struct { type ChunkStreamReader struct {
chunkViews []*ChunkView chunkViews []*ChunkView
totalSize int64
logicOffset int64 logicOffset int64
buffer []byte buffer []byte
bufferOffset int64 bufferOffset int64
@ -107,9 +108,15 @@ func NewChunkStreamReaderFromFiler(masterClient *wdclient.MasterClient, chunks [
chunkViews := ViewFromChunks(lookupFileIdFn, chunks, 0, math.MaxInt64) chunkViews := ViewFromChunks(lookupFileIdFn, chunks, 0, math.MaxInt64)
var totalSize int64
for _, chunk := range chunkViews {
totalSize += int64(chunk.Size)
}
return &ChunkStreamReader{ return &ChunkStreamReader{
chunkViews: chunkViews, chunkViews: chunkViews,
lookupFileId: lookupFileIdFn, lookupFileId: lookupFileIdFn,
totalSize: totalSize,
} }
} }
@ -119,9 +126,15 @@ func NewChunkStreamReader(filerClient filer_pb.FilerClient, chunks []*filer_pb.F
chunkViews := ViewFromChunks(lookupFileIdFn, chunks, 0, math.MaxInt64) chunkViews := ViewFromChunks(lookupFileIdFn, chunks, 0, math.MaxInt64)
var totalSize int64
for _, chunk := range chunkViews {
totalSize += int64(chunk.Size)
}
return &ChunkStreamReader{ return &ChunkStreamReader{
chunkViews: chunkViews, chunkViews: chunkViews,
lookupFileId: lookupFileIdFn, lookupFileId: lookupFileIdFn,
totalSize: totalSize,
} }
} }
@ -148,20 +161,15 @@ func (c *ChunkStreamReader) isBufferEmpty() bool {
func (c *ChunkStreamReader) Seek(offset int64, whence int) (int64, error) { func (c *ChunkStreamReader) Seek(offset int64, whence int) (int64, error) {
var totalSize int64
for _, chunk := range c.chunkViews {
totalSize += int64(chunk.Size)
}
var err error var err error
switch whence { switch whence {
case io.SeekStart: case io.SeekStart:
case io.SeekCurrent: case io.SeekCurrent:
offset += c.bufferOffset + int64(c.bufferPos) offset += c.bufferOffset + int64(c.bufferPos)
case io.SeekEnd: case io.SeekEnd:
offset = totalSize + offset
offset = c.totalSize + offset
} }
if offset > totalSize {
if offset > c.totalSize {
err = io.ErrUnexpectedEOF err = io.ErrUnexpectedEOF
} }

Loading…
Cancel
Save