Optimize memory usage in GetPartialObject() for memory driver

master
Harshavardhana 10 years ago
parent c8f31d97a8
commit d63064b8af
  1. 2
      pkg/storage/drivers/donut/donut.go
  2. 36
      pkg/storage/drivers/memory/memory.go

@ -248,13 +248,13 @@ func (d donutDriver) GetPartialObject(w io.Writer, bucketName, objectName string
}, errParams) }, errParams)
} }
reader, size, err := d.donut.GetObject(bucketName, objectName) reader, size, err := d.donut.GetObject(bucketName, objectName)
defer reader.Close()
if err != nil { if err != nil {
return 0, iodine.New(drivers.ObjectNotFound{ return 0, iodine.New(drivers.ObjectNotFound{
Bucket: bucketName, Bucket: bucketName,
Object: objectName, Object: objectName,
}, nil) }, nil)
} }
defer reader.Close()
if start > size || (start+length-1) > size { if start > size || (start+length-1) > size {
return 0, iodine.New(drivers.InvalidRange{ return 0, iodine.New(drivers.InvalidRange{
Start: start, Start: start,

@ -24,10 +24,10 @@ import (
"encoding/hex" "encoding/hex"
"errors" "errors"
"io" "io"
"io/ioutil"
"log" "log"
"runtime/debug" "runtime/debug"
"sort" "sort"
"strconv"
"strings" "strings"
"sync" "sync"
"time" "time"
@ -100,29 +100,43 @@ func (memory *memoryDriver) GetObject(w io.Writer, bucket string, object string)
memory.lock.RUnlock() memory.lock.RUnlock()
return 0, iodine.New(drivers.ObjectNotFound{Bucket: bucket, Object: object}, nil) return 0, iodine.New(drivers.ObjectNotFound{Bucket: bucket, Object: object}, nil)
} }
memory.lock.RUnlock()
written, err := io.Copy(w, bytes.NewBuffer(data)) written, err := io.Copy(w, bytes.NewBuffer(data))
memory.lock.RUnlock()
return written, iodine.New(err, nil) return written, iodine.New(err, nil)
} }
// GetPartialObject - GET object from memory buffer range // GetPartialObject - GET object from memory buffer range
func (memory *memoryDriver) GetPartialObject(w io.Writer, bucket, object string, start, length int64) (int64, error) { func (memory *memoryDriver) GetPartialObject(w io.Writer, bucket, object string, start, length int64) (int64, error) {
errParams := map[string]string{
"bucket": bucket,
"object": object,
"start": strconv.FormatInt(start, 10),
"length": strconv.FormatInt(length, 10),
}
memory.lock.RLock() memory.lock.RLock()
defer memory.lock.RUnlock()
var sourceBuffer bytes.Buffer
if !drivers.IsValidBucket(bucket) { if !drivers.IsValidBucket(bucket) {
return 0, iodine.New(drivers.BucketNameInvalid{Bucket: bucket}, nil) memory.lock.RUnlock()
return 0, iodine.New(drivers.BucketNameInvalid{Bucket: bucket}, errParams)
} }
if !drivers.IsValidObjectName(object) { if !drivers.IsValidObjectName(object) {
return 0, iodine.New(drivers.ObjectNameInvalid{Object: object}, nil) memory.lock.RUnlock()
return 0, iodine.New(drivers.ObjectNameInvalid{Object: object}, errParams)
} }
if _, err := memory.GetObject(&sourceBuffer, bucket, object); err != nil { if start < 0 {
return 0, iodine.New(err, nil) return 0, iodine.New(drivers.InvalidRange{
Start: start,
Length: length,
}, errParams)
} }
if _, err := io.CopyN(ioutil.Discard, &sourceBuffer, start); err != nil { objectKey := bucket + "/" + object
return 0, iodine.New(err, nil) data, ok := memory.objects.Get(objectKey)
if !ok {
memory.lock.RUnlock()
return 0, iodine.New(drivers.ObjectNotFound{Bucket: bucket, Object: object}, errParams)
} }
return io.CopyN(w, &sourceBuffer, length) written, err := io.CopyN(w, bytes.NewBuffer(data[start:]), length)
memory.lock.RUnlock()
return written, iodine.New(err, nil)
} }
// GetBucketMetadata - // GetBucketMetadata -

Loading…
Cancel
Save