From c31b2542489ea4cddffbf1efedbdb867fb6cdb2f Mon Sep 17 00:00:00 2001 From: Chris Lu Date: Tue, 20 Oct 2020 23:48:29 -0700 Subject: [PATCH] mount: shortcut when there is only one chunk --- weed/filesys/dirty_page_interval.go | 7 ++++++- weed/operation/upload_content.go | 13 +++++++++---- weed/util/bytes.go | 13 +++++++++++++ 3 files changed, 28 insertions(+), 5 deletions(-) diff --git a/weed/filesys/dirty_page_interval.go b/weed/filesys/dirty_page_interval.go index afa2755ed..f143fe3e4 100644 --- a/weed/filesys/dirty_page_interval.go +++ b/weed/filesys/dirty_page_interval.go @@ -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...) } diff --git a/weed/operation/upload_content.go b/weed/operation/upload_content.go index e9002d09d..9eb05f6e1 100644 --- a/weed/operation/upload_content.go +++ b/weed/operation/upload_content.go @@ -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 diff --git a/weed/util/bytes.go b/weed/util/bytes.go index 67e6876fa..c2a4df108 100644 --- a/weed/util/bytes.go +++ b/weed/util/bytes.go @@ -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), + } +}