mount: shortcut when there is only one chunk

This commit is contained in:
Chris Lu 2020-10-20 23:48:29 -07:00
parent f64252023e
commit c31b254248
3 changed files with 28 additions and 5 deletions

View file

@ -3,6 +3,8 @@ package filesys
import (
"bytes"
"io"
"github.com/chrislusf/seaweedfs/weed/util"
)
type IntervalNode struct {
@ -200,10 +202,13 @@ func (c *ContinuousIntervals) ReadDataAt(data []byte, startOffset int64) (maxSto
func (l *IntervalLinkedList) ToReader() io.Reader {
var readers []io.Reader
t := l.Head
readers = append(readers, bytes.NewReader(t.Data))
readers = append(readers, util.NewBytesReader(t.Data))
for t.Next != nil {
t = t.Next
readers = append(readers, bytes.NewReader(t.Data))
}
if len(readers) == 1 {
return readers[0]
}
return io.MultiReader(readers...)
}

View file

@ -76,10 +76,15 @@ func Upload(uploadUrl string, filename string, cipher bool, reader io.Reader, is
}
func doUpload(uploadUrl string, filename string, cipher bool, reader io.Reader, isInputCompressed bool, mtype string, pairMap map[string]string, jwt security.EncodedJwt) (uploadResult *UploadResult, err error, data []byte) {
data, err = ioutil.ReadAll(reader)
if err != nil {
err = fmt.Errorf("read input: %v", err)
return
bytesReader, ok := reader.(*util.BytesReader)
if ok {
data = bytesReader.Bytes
} else {
data, err = ioutil.ReadAll(reader)
if err != nil {
err = fmt.Errorf("read input: %v", err)
return
}
}
uploadResult, uploadErr := retriedUploadData(uploadUrl, filename, cipher, data, isInputCompressed, mtype, pairMap, jwt)
return uploadResult, uploadErr, data

View file

@ -1,6 +1,7 @@
package util
import (
"bytes"
"crypto/md5"
"crypto/rand"
"encoding/base64"
@ -148,3 +149,15 @@ func RandomBytes(byteCount int) []byte {
rand.Read(buf)
return buf
}
type BytesReader struct {
Bytes []byte
*bytes.Reader
}
func NewBytesReader(b []byte) *BytesReader {
return &BytesReader{
Bytes: b,
Reader: bytes.NewReader(b),
}
}