mirror of
https://github.com/seaweedfs/seaweedfs.git
synced 2024-01-19 02:48:24 +00:00
refactoring
This commit is contained in:
parent
11372dd548
commit
f251d03673
|
@ -40,8 +40,9 @@ type ChunkManifest struct {
|
||||||
|
|
||||||
// seekable chunked file reader
|
// seekable chunked file reader
|
||||||
type ChunkedFileReader struct {
|
type ChunkedFileReader struct {
|
||||||
Manifest *ChunkManifest
|
totalSize int64
|
||||||
Master string
|
chunkList []*ChunkInfo
|
||||||
|
master string
|
||||||
pos int64
|
pos int64
|
||||||
pr *io.PipeReader
|
pr *io.PipeReader
|
||||||
pw *io.PipeWriter
|
pw *io.PipeWriter
|
||||||
|
@ -125,10 +126,15 @@ func readChunkNeedle(fileUrl string, w io.Writer, offset int64) (written int64,
|
||||||
return io.Copy(w, resp.Body)
|
return io.Copy(w, resp.Body)
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewChunkedFileReader(chunkManifest *ChunkManifest, master string) *ChunkedFileReader {
|
func NewChunkedFileReader(chunkList []*ChunkInfo, master string) *ChunkedFileReader {
|
||||||
|
var totalSize int64
|
||||||
|
for _, chunk := range chunkList {
|
||||||
|
totalSize += chunk.Size
|
||||||
|
}
|
||||||
return &ChunkedFileReader{
|
return &ChunkedFileReader{
|
||||||
Manifest: chunkManifest,
|
totalSize: totalSize,
|
||||||
Master: master,
|
chunkList: chunkList,
|
||||||
|
master: master,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -139,9 +145,9 @@ func (cf *ChunkedFileReader) Seek(offset int64, whence int) (int64, error) {
|
||||||
case 1:
|
case 1:
|
||||||
offset += cf.pos
|
offset += cf.pos
|
||||||
case 2:
|
case 2:
|
||||||
offset = cf.Manifest.Size - offset
|
offset = cf.totalSize - offset
|
||||||
}
|
}
|
||||||
if offset > cf.Manifest.Size {
|
if offset > cf.totalSize {
|
||||||
err = ErrInvalidRange
|
err = ErrInvalidRange
|
||||||
}
|
}
|
||||||
if cf.pos != offset {
|
if cf.pos != offset {
|
||||||
|
@ -152,10 +158,9 @@ func (cf *ChunkedFileReader) Seek(offset int64, whence int) (int64, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (cf *ChunkedFileReader) WriteTo(w io.Writer) (n int64, err error) {
|
func (cf *ChunkedFileReader) WriteTo(w io.Writer) (n int64, err error) {
|
||||||
cm := cf.Manifest
|
|
||||||
chunkIndex := -1
|
chunkIndex := -1
|
||||||
chunkStartOffset := int64(0)
|
chunkStartOffset := int64(0)
|
||||||
for i, ci := range cm.Chunks {
|
for i, ci := range cf.chunkList {
|
||||||
if cf.pos >= ci.Offset && cf.pos < ci.Offset+ci.Size {
|
if cf.pos >= ci.Offset && cf.pos < ci.Offset+ci.Size {
|
||||||
chunkIndex = i
|
chunkIndex = i
|
||||||
chunkStartOffset = cf.pos - ci.Offset
|
chunkStartOffset = cf.pos - ci.Offset
|
||||||
|
@ -165,10 +170,10 @@ func (cf *ChunkedFileReader) WriteTo(w io.Writer) (n int64, err error) {
|
||||||
if chunkIndex < 0 {
|
if chunkIndex < 0 {
|
||||||
return n, ErrInvalidRange
|
return n, ErrInvalidRange
|
||||||
}
|
}
|
||||||
for ; chunkIndex < cm.Chunks.Len(); chunkIndex++ {
|
for ; chunkIndex < len(cf.chunkList); chunkIndex++ {
|
||||||
ci := cm.Chunks[chunkIndex]
|
ci := cf.chunkList[chunkIndex]
|
||||||
// if we need read date from local volume server first?
|
// if we need read date from local volume server first?
|
||||||
fileUrl, lookupError := LookupFileId(cf.Master, ci.Fid)
|
fileUrl, lookupError := LookupFileId(cf.master, ci.Fid)
|
||||||
if lookupError != nil {
|
if lookupError != nil {
|
||||||
return n, lookupError
|
return n, lookupError
|
||||||
}
|
}
|
||||||
|
|
|
@ -187,7 +187,7 @@ func (vs *VolumeServer) tryHandleChunkedFile(n *needle.Needle, fileName string,
|
||||||
|
|
||||||
w.Header().Set("X-File-Store", "chunked")
|
w.Header().Set("X-File-Store", "chunked")
|
||||||
|
|
||||||
chunkedFileReader := operation.NewChunkedFileReader(chunkManifest, vs.GetMaster())
|
chunkedFileReader := operation.NewChunkedFileReader(chunkManifest.Chunks, vs.GetMaster())
|
||||||
defer chunkedFileReader.Close()
|
defer chunkedFileReader.Close()
|
||||||
|
|
||||||
rs := conditionallyResizeImages(chunkedFileReader, ext, r)
|
rs := conditionallyResizeImages(chunkedFileReader, ext, r)
|
||||||
|
|
Loading…
Reference in a new issue