mirror of
https://github.com/minio/minio.git
synced 2024-12-27 15:45:55 -05:00
Merge pull request #467 from fkautz/pr_out_better_locking_in_memory_driver
This commit is contained in:
commit
b464ddbf31
@ -71,6 +71,8 @@ func start(ctrlChannel <-chan string, errorChannel chan<- error) {
|
|||||||
|
|
||||||
// GetObject - GET object from memory buffer
|
// GetObject - GET object from memory buffer
|
||||||
func (memory memoryDriver) GetObject(w io.Writer, bucket string, object string) (int64, error) {
|
func (memory memoryDriver) GetObject(w io.Writer, bucket string, object string) (int64, error) {
|
||||||
|
memory.lock.RLock()
|
||||||
|
defer memory.lock.RUnlock()
|
||||||
if _, ok := memory.bucketdata[bucket]; ok == false {
|
if _, ok := memory.bucketdata[bucket]; ok == false {
|
||||||
return 0, drivers.BucketNotFound{Bucket: bucket}
|
return 0, drivers.BucketNotFound{Bucket: bucket}
|
||||||
}
|
}
|
||||||
@ -86,6 +88,8 @@ func (memory memoryDriver) GetObject(w io.Writer, bucket string, object string)
|
|||||||
|
|
||||||
// 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) {
|
||||||
|
memory.lock.RLock()
|
||||||
|
defer memory.lock.RUnlock()
|
||||||
var sourceBuffer bytes.Buffer
|
var sourceBuffer bytes.Buffer
|
||||||
if _, err := memory.GetObject(&sourceBuffer, bucket, object); err != nil {
|
if _, err := memory.GetObject(&sourceBuffer, bucket, object); err != nil {
|
||||||
return 0, err
|
return 0, err
|
||||||
@ -98,6 +102,8 @@ func (memory memoryDriver) GetPartialObject(w io.Writer, bucket, object string,
|
|||||||
|
|
||||||
// GetBucketMetadata -
|
// GetBucketMetadata -
|
||||||
func (memory memoryDriver) GetBucketMetadata(bucket string) (drivers.BucketMetadata, error) {
|
func (memory memoryDriver) GetBucketMetadata(bucket string) (drivers.BucketMetadata, error) {
|
||||||
|
memory.lock.RLock()
|
||||||
|
defer memory.lock.RUnlock()
|
||||||
if _, ok := memory.bucketdata[bucket]; ok == false {
|
if _, ok := memory.bucketdata[bucket]; ok == false {
|
||||||
return drivers.BucketMetadata{}, drivers.BucketNotFound{Bucket: bucket}
|
return drivers.BucketMetadata{}, drivers.BucketNotFound{Bucket: bucket}
|
||||||
}
|
}
|
||||||
@ -116,16 +122,18 @@ func (memory memoryDriver) GetBucketPolicy(bucket string) (drivers.BucketPolicy,
|
|||||||
|
|
||||||
// CreateObject - PUT object to memory buffer
|
// CreateObject - PUT object to memory buffer
|
||||||
func (memory memoryDriver) CreateObject(bucket, key, contentType, md5sum string, data io.Reader) error {
|
func (memory memoryDriver) CreateObject(bucket, key, contentType, md5sum string, data io.Reader) error {
|
||||||
memory.lock.Lock()
|
memory.lock.RLock()
|
||||||
defer memory.lock.Unlock()
|
|
||||||
|
|
||||||
if _, ok := memory.bucketdata[bucket]; ok == false {
|
if _, ok := memory.bucketdata[bucket]; ok == false {
|
||||||
|
memory.lock.RUnlock()
|
||||||
return drivers.BucketNotFound{Bucket: bucket}
|
return drivers.BucketNotFound{Bucket: bucket}
|
||||||
}
|
}
|
||||||
|
|
||||||
if _, ok := memory.objectdata[key]; ok == true {
|
if _, ok := memory.objectdata[key]; ok == true {
|
||||||
|
memory.lock.RUnlock()
|
||||||
return drivers.ObjectExists{Bucket: bucket, Object: key}
|
return drivers.ObjectExists{Bucket: bucket, Object: key}
|
||||||
}
|
}
|
||||||
|
memory.lock.RUnlock()
|
||||||
|
|
||||||
if contentType == "" {
|
if contentType == "" {
|
||||||
contentType = "application/octet-stream"
|
contentType = "application/octet-stream"
|
||||||
@ -150,26 +158,41 @@ func (memory memoryDriver) CreateObject(bucket, key, contentType, md5sum string,
|
|||||||
}
|
}
|
||||||
newObject.data = bytesBuffer.Bytes()
|
newObject.data = bytesBuffer.Bytes()
|
||||||
}
|
}
|
||||||
|
memory.lock.Lock()
|
||||||
|
if _, ok := memory.bucketdata[bucket]; ok == false {
|
||||||
|
memory.lock.Unlock()
|
||||||
|
return drivers.BucketNotFound{Bucket: bucket}
|
||||||
|
}
|
||||||
|
|
||||||
|
if _, ok := memory.objectdata[key]; ok == true {
|
||||||
|
memory.lock.Unlock()
|
||||||
|
return drivers.ObjectExists{Bucket: bucket, Object: key}
|
||||||
|
}
|
||||||
memory.objectdata[key] = newObject
|
memory.objectdata[key] = newObject
|
||||||
|
memory.lock.Unlock()
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// CreateBucket - create bucket in memory
|
// CreateBucket - create bucket in memory
|
||||||
func (memory memoryDriver) CreateBucket(bucketName string) error {
|
func (memory memoryDriver) CreateBucket(bucketName string) error {
|
||||||
memory.lock.Lock()
|
memory.lock.RLock()
|
||||||
defer memory.lock.Unlock()
|
|
||||||
if !drivers.IsValidBucket(bucketName) {
|
if !drivers.IsValidBucket(bucketName) {
|
||||||
|
memory.lock.RUnlock()
|
||||||
return drivers.BucketNameInvalid{Bucket: bucketName}
|
return drivers.BucketNameInvalid{Bucket: bucketName}
|
||||||
}
|
}
|
||||||
|
|
||||||
if _, ok := memory.bucketdata[bucketName]; ok == true {
|
if _, ok := memory.bucketdata[bucketName]; ok == true {
|
||||||
|
memory.lock.RUnlock()
|
||||||
return drivers.BucketExists{Bucket: bucketName}
|
return drivers.BucketExists{Bucket: bucketName}
|
||||||
}
|
}
|
||||||
|
memory.lock.RUnlock()
|
||||||
|
|
||||||
var newBucket = storedBucket{}
|
var newBucket = storedBucket{}
|
||||||
newBucket.metadata = drivers.BucketMetadata{}
|
newBucket.metadata = drivers.BucketMetadata{}
|
||||||
newBucket.metadata.Name = bucketName
|
newBucket.metadata.Name = bucketName
|
||||||
newBucket.metadata.Created = time.Now()
|
newBucket.metadata.Created = time.Now()
|
||||||
|
memory.lock.Lock()
|
||||||
|
defer memory.lock.Unlock()
|
||||||
memory.bucketdata[bucketName] = newBucket
|
memory.bucketdata[bucketName] = newBucket
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
@ -208,6 +231,8 @@ func (memory memoryDriver) filterDelimiterPrefix(keys []string, key, delimitedNa
|
|||||||
|
|
||||||
// ListObjects - list objects from memory
|
// ListObjects - list objects from memory
|
||||||
func (memory memoryDriver) ListObjects(bucket string, resources drivers.BucketResourcesMetadata) ([]drivers.ObjectMetadata, drivers.BucketResourcesMetadata, error) {
|
func (memory memoryDriver) ListObjects(bucket string, resources drivers.BucketResourcesMetadata) ([]drivers.ObjectMetadata, drivers.BucketResourcesMetadata, error) {
|
||||||
|
memory.lock.RLock()
|
||||||
|
defer memory.lock.RUnlock()
|
||||||
if _, ok := memory.bucketdata[bucket]; ok == false {
|
if _, ok := memory.bucketdata[bucket]; ok == false {
|
||||||
return []drivers.ObjectMetadata{}, drivers.BucketResourcesMetadata{IsTruncated: false}, drivers.BucketNotFound{Bucket: bucket}
|
return []drivers.ObjectMetadata{}, drivers.BucketResourcesMetadata{IsTruncated: false}, drivers.BucketNotFound{Bucket: bucket}
|
||||||
}
|
}
|
||||||
@ -266,6 +291,8 @@ func (b ByBucketName) Less(i, j int) bool { return b[i].Name < b[j].Name }
|
|||||||
|
|
||||||
// ListBuckets - List buckets from memory
|
// ListBuckets - List buckets from memory
|
||||||
func (memory memoryDriver) ListBuckets() ([]drivers.BucketMetadata, error) {
|
func (memory memoryDriver) ListBuckets() ([]drivers.BucketMetadata, error) {
|
||||||
|
memory.lock.RLock()
|
||||||
|
defer memory.lock.RUnlock()
|
||||||
var results []drivers.BucketMetadata
|
var results []drivers.BucketMetadata
|
||||||
for _, bucket := range memory.bucketdata {
|
for _, bucket := range memory.bucketdata {
|
||||||
results = append(results, bucket.metadata)
|
results = append(results, bucket.metadata)
|
||||||
@ -276,6 +303,8 @@ func (memory memoryDriver) ListBuckets() ([]drivers.BucketMetadata, error) {
|
|||||||
|
|
||||||
// GetObjectMetadata - get object metadata from memory
|
// GetObjectMetadata - get object metadata from memory
|
||||||
func (memory memoryDriver) GetObjectMetadata(bucket, key, prefix string) (drivers.ObjectMetadata, error) {
|
func (memory memoryDriver) GetObjectMetadata(bucket, key, prefix string) (drivers.ObjectMetadata, error) {
|
||||||
|
memory.lock.RLock()
|
||||||
|
defer memory.lock.RUnlock()
|
||||||
// check if bucket exists
|
// check if bucket exists
|
||||||
if _, ok := memory.bucketdata[bucket]; ok == false {
|
if _, ok := memory.bucketdata[bucket]; ok == false {
|
||||||
return drivers.ObjectMetadata{}, drivers.BucketNotFound{Bucket: bucket}
|
return drivers.ObjectMetadata{}, drivers.BucketNotFound{Bucket: bucket}
|
||||||
|
Loading…
Reference in New Issue
Block a user