|
|
@ -217,19 +217,19 @@ func (b bucket) ReadObject(objectName string) (reader io.ReadCloser, size int64, |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// WriteObject - write a new object into bucket
|
|
|
|
// WriteObject - write a new object into bucket
|
|
|
|
func (b bucket) WriteObject(objectName string, objectData io.Reader, expectedMD5Sum string, metadata map[string]string) (string, error) { |
|
|
|
func (b bucket) WriteObject(objectName string, objectData io.Reader, expectedMD5Sum string, metadata map[string]string) (ObjectMetadata, error) { |
|
|
|
b.lock.Lock() |
|
|
|
b.lock.Lock() |
|
|
|
defer b.lock.Unlock() |
|
|
|
defer b.lock.Unlock() |
|
|
|
if objectName == "" || objectData == nil { |
|
|
|
if objectName == "" || objectData == nil { |
|
|
|
return "", iodine.New(InvalidArgument{}, nil) |
|
|
|
return ObjectMetadata{}, iodine.New(InvalidArgument{}, nil) |
|
|
|
} |
|
|
|
} |
|
|
|
writers, err := b.getDiskWriters(normalizeObjectName(objectName), "data") |
|
|
|
writers, err := b.getDiskWriters(normalizeObjectName(objectName), "data") |
|
|
|
if err != nil { |
|
|
|
if err != nil { |
|
|
|
return "", iodine.New(err, nil) |
|
|
|
return ObjectMetadata{}, iodine.New(err, nil) |
|
|
|
} |
|
|
|
} |
|
|
|
sumMD5 := md5.New() |
|
|
|
sumMD5 := md5.New() |
|
|
|
sum512 := sha512.New() |
|
|
|
sum512 := sha512.New() |
|
|
|
objMetadata := new(ObjectMetadata) |
|
|
|
objMetadata := ObjectMetadata{} |
|
|
|
objMetadata.Version = objectMetadataVersion |
|
|
|
objMetadata.Version = objectMetadataVersion |
|
|
|
objMetadata.Created = time.Now().UTC() |
|
|
|
objMetadata.Created = time.Now().UTC() |
|
|
|
// if total writers are only '1' do not compute erasure
|
|
|
|
// if total writers are only '1' do not compute erasure
|
|
|
@ -238,19 +238,19 @@ func (b bucket) WriteObject(objectName string, objectData io.Reader, expectedMD5 |
|
|
|
mw := io.MultiWriter(writers[0], sumMD5, sum512) |
|
|
|
mw := io.MultiWriter(writers[0], sumMD5, sum512) |
|
|
|
totalLength, err := io.Copy(mw, objectData) |
|
|
|
totalLength, err := io.Copy(mw, objectData) |
|
|
|
if err != nil { |
|
|
|
if err != nil { |
|
|
|
return "", iodine.New(err, nil) |
|
|
|
return ObjectMetadata{}, iodine.New(err, nil) |
|
|
|
} |
|
|
|
} |
|
|
|
objMetadata.Size = totalLength |
|
|
|
objMetadata.Size = totalLength |
|
|
|
case false: |
|
|
|
case false: |
|
|
|
// calculate data and parity dictated by total number of writers
|
|
|
|
// calculate data and parity dictated by total number of writers
|
|
|
|
k, m, err := b.getDataAndParity(len(writers)) |
|
|
|
k, m, err := b.getDataAndParity(len(writers)) |
|
|
|
if err != nil { |
|
|
|
if err != nil { |
|
|
|
return "", iodine.New(err, nil) |
|
|
|
return ObjectMetadata{}, iodine.New(err, nil) |
|
|
|
} |
|
|
|
} |
|
|
|
// encoded data with k, m and write
|
|
|
|
// encoded data with k, m and write
|
|
|
|
chunkCount, totalLength, err := b.writeEncodedData(k, m, writers, objectData, sumMD5, sum512) |
|
|
|
chunkCount, totalLength, err := b.writeEncodedData(k, m, writers, objectData, sumMD5, sum512) |
|
|
|
if err != nil { |
|
|
|
if err != nil { |
|
|
|
return "", iodine.New(err, nil) |
|
|
|
return ObjectMetadata{}, iodine.New(err, nil) |
|
|
|
} |
|
|
|
} |
|
|
|
/// donutMetadata section
|
|
|
|
/// donutMetadata section
|
|
|
|
objMetadata.BlockSize = blockSize |
|
|
|
objMetadata.BlockSize = blockSize |
|
|
@ -271,20 +271,19 @@ func (b bucket) WriteObject(objectName string, objectData io.Reader, expectedMD5 |
|
|
|
// Verify if the written object is equal to what is expected, only if it is requested as such
|
|
|
|
// Verify if the written object is equal to what is expected, only if it is requested as such
|
|
|
|
if strings.TrimSpace(expectedMD5Sum) != "" { |
|
|
|
if strings.TrimSpace(expectedMD5Sum) != "" { |
|
|
|
if err := b.isMD5SumEqual(strings.TrimSpace(expectedMD5Sum), objMetadata.MD5Sum); err != nil { |
|
|
|
if err := b.isMD5SumEqual(strings.TrimSpace(expectedMD5Sum), objMetadata.MD5Sum); err != nil { |
|
|
|
return "", iodine.New(err, nil) |
|
|
|
return ObjectMetadata{}, iodine.New(err, nil) |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
objMetadata.Metadata = metadata |
|
|
|
objMetadata.Metadata = metadata |
|
|
|
// write object specific metadata
|
|
|
|
// write object specific metadata
|
|
|
|
if err := b.writeObjectMetadata(normalizeObjectName(objectName), objMetadata); err != nil { |
|
|
|
if err := b.writeObjectMetadata(normalizeObjectName(objectName), objMetadata); err != nil { |
|
|
|
return "", iodine.New(err, nil) |
|
|
|
return ObjectMetadata{}, iodine.New(err, nil) |
|
|
|
} |
|
|
|
} |
|
|
|
// close all writers, when control flow reaches here
|
|
|
|
// close all writers, when control flow reaches here
|
|
|
|
for _, writer := range writers { |
|
|
|
for _, writer := range writers { |
|
|
|
writer.Close() |
|
|
|
writer.Close() |
|
|
|
} |
|
|
|
} |
|
|
|
return objMetadata.MD5Sum, nil |
|
|
|
return objMetadata, nil |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// isMD5SumEqual - returns error if md5sum mismatches, other its `nil`
|
|
|
|
// isMD5SumEqual - returns error if md5sum mismatches, other its `nil`
|
|
|
@ -307,8 +306,8 @@ func (b bucket) isMD5SumEqual(expectedMD5Sum, actualMD5Sum string) error { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// writeObjectMetadata - write additional object metadata
|
|
|
|
// writeObjectMetadata - write additional object metadata
|
|
|
|
func (b bucket) writeObjectMetadata(objectName string, objMetadata *ObjectMetadata) error { |
|
|
|
func (b bucket) writeObjectMetadata(objectName string, objMetadata ObjectMetadata) error { |
|
|
|
if objMetadata == nil { |
|
|
|
if objMetadata.Object == "" { |
|
|
|
return iodine.New(InvalidArgument{}, nil) |
|
|
|
return iodine.New(InvalidArgument{}, nil) |
|
|
|
} |
|
|
|
} |
|
|
|
objMetadataWriters, err := b.getDiskWriters(objectName, objectMetadataConfig) |
|
|
|
objMetadataWriters, err := b.getDiskWriters(objectName, objectMetadataConfig) |
|
|
@ -320,7 +319,7 @@ func (b bucket) writeObjectMetadata(objectName string, objMetadata *ObjectMetada |
|
|
|
} |
|
|
|
} |
|
|
|
for _, objMetadataWriter := range objMetadataWriters { |
|
|
|
for _, objMetadataWriter := range objMetadataWriters { |
|
|
|
jenc := json.NewEncoder(objMetadataWriter) |
|
|
|
jenc := json.NewEncoder(objMetadataWriter) |
|
|
|
if err := jenc.Encode(objMetadata); err != nil { |
|
|
|
if err := jenc.Encode(&objMetadata); err != nil { |
|
|
|
return iodine.New(err, nil) |
|
|
|
return iodine.New(err, nil) |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|