Golint cleanup pkg/storage

This commit is contained in:
Harshavardhana 2015-03-05 20:32:04 -08:00
parent 256faddab5
commit 76e601b26b
7 changed files with 89 additions and 72 deletions

View File

@ -43,10 +43,10 @@ func (s *MySuite) TestCauchyDecode(c *C) {
chunks[9] = nil chunks[9] = nil
chunks[13] = nil chunks[13] = nil
recovered_data, err := e.Decode(chunks, length) recoveredData, err := e.Decode(chunks, length)
c.Assert(err, IsNil) c.Assert(err, IsNil)
if !bytes.Equal(data, recovered_data) { if !bytes.Equal(data, recoveredData) {
c.Fatalf("Recovered data mismatches with original data") c.Fatalf("Recovered data mismatches with original data")
} }
} }

View File

@ -38,10 +38,10 @@ func (s *MySuite) TestVanderMondeDecode(c *C) {
chunks[9] = nil chunks[9] = nil
chunks[13] = nil chunks[13] = nil
recovered_data, err := e.Decode(chunks, length) recoveredData, err := e.Decode(chunks, length)
c.Assert(err, IsNil) c.Assert(err, IsNil)
if !bytes.Equal(recovered_data, data) { if !bytes.Equal(recoveredData, data) {
c.Fatalf("Recovered data mismatches with original data") c.Fatalf("Recovered data mismatches with original data")
} }
} }

View File

@ -38,6 +38,7 @@ type storage struct {
lock *sync.Mutex lock *sync.Mutex
} }
// SerializedMetadata - carries content type
type SerializedMetadata struct { type SerializedMetadata struct {
ContentType string ContentType string
} }
@ -237,9 +238,8 @@ func (storage *storage) CopyObjectToWriter(w io.Writer, bucket string, object st
{ {
if os.IsNotExist(err) { if os.IsNotExist(err) {
return 0, mstorage.ObjectNotFound{Bucket: bucket, Object: object} return 0, mstorage.ObjectNotFound{Bucket: bucket, Object: object}
} else {
return 0, mstorage.EmbedError(bucket, object, err)
} }
return 0, mstorage.EmbedError(bucket, object, err)
} }
} }
file, err := os.Open(objectPath) file, err := os.Open(objectPath)
@ -311,20 +311,20 @@ func (storage *storage) GetObjectMetadata(bucket string, object string) (mstorag
return metadata, nil return metadata, nil
} }
type Path struct { type bucketDir struct {
files map[string]os.FileInfo files map[string]os.FileInfo
root string root string
} }
func (p *Path) getAllFiles(path string, fl os.FileInfo, err error) error { func (p *bucketDir) getAllFiles(object string, fl os.FileInfo, err error) error {
if err != nil { if err != nil {
return err return err
} }
if fl.Mode().IsRegular() { if fl.Mode().IsRegular() {
if strings.HasSuffix(path, "$metadata") { if strings.HasSuffix(object, "$metadata") {
return nil return nil
} }
_p := strings.Split(path, p.root+"/") _p := strings.Split(object, p.root+"/")
if len(_p) > 1 { if len(_p) > 1 {
p.files[_p[1]] = fl p.files[_p[1]] = fl
} }
@ -332,8 +332,8 @@ func (p *Path) getAllFiles(path string, fl os.FileInfo, err error) error {
return nil return nil
} }
func delimiter(path, delimiter string) string { func delimiter(object, delimiter string) string {
readBuffer := bytes.NewBufferString(path) readBuffer := bytes.NewBufferString(object)
reader := bufio.NewReader(readBuffer) reader := bufio.NewReader(readBuffer)
stringReader := strings.NewReader(delimiter) stringReader := strings.NewReader(delimiter)
delimited, _ := stringReader.ReadByte() delimited, _ := stringReader.ReadByte()
@ -341,20 +341,20 @@ func delimiter(path, delimiter string) string {
return delimitedStr return delimitedStr
} }
type ByObjectKey []mstorage.ObjectMetadata type byObjectKey []mstorage.ObjectMetadata
// Len // Len
func (b ByObjectKey) Len() int { return len(b) } func (b byObjectKey) Len() int { return len(b) }
// Swap // Swap
func (b ByObjectKey) Swap(i, j int) { b[i], b[j] = b[j], b[i] } func (b byObjectKey) Swap(i, j int) { b[i], b[j] = b[j], b[i] }
// Less // Less
func (b ByObjectKey) Less(i, j int) bool { return b[i].Key < b[j].Key } func (b byObjectKey) Less(i, j int) bool { return b[i].Key < b[j].Key }
// GET bucket (list objects) // GET bucket (list objects)
func (storage *storage) ListObjects(bucket string, resources mstorage.BucketResourcesMetadata) ([]mstorage.ObjectMetadata, mstorage.BucketResourcesMetadata, error) { func (storage *storage) ListObjects(bucket string, resources mstorage.BucketResourcesMetadata) ([]mstorage.ObjectMetadata, mstorage.BucketResourcesMetadata, error) {
p := Path{} p := bucketDir{}
p.files = make(map[string]os.FileInfo) p.files = make(map[string]os.FileInfo)
if mstorage.IsValidBucket(bucket) == false { if mstorage.IsValidBucket(bucket) == false {
@ -450,7 +450,7 @@ func (storage *storage) ListObjects(bucket string, resources mstorage.BucketReso
} }
ret: ret:
sort.Sort(ByObjectKey(metadataList)) sort.Sort(byObjectKey(metadataList))
return metadataList, resources, nil return metadataList, resources, nil
} }

View File

@ -1,5 +1,5 @@
/* /*
* Mini Object Storage, (C) 2015 Minio, Inc. * Minio Object Storage, (C) 2015 Minio, Inc.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -72,9 +72,8 @@ func (storage *storage) CopyObjectToWriter(w io.Writer, bucket string, object st
objectBuffer := bytes.NewBuffer(val.data) objectBuffer := bytes.NewBuffer(val.data)
written, err := io.Copy(w, objectBuffer) written, err := io.Copy(w, objectBuffer)
return written, err return written, err
} else {
return 0, mstorage.ObjectNotFound{Bucket: bucket, Object: object}
} }
return 0, mstorage.ObjectNotFound{Bucket: bucket, Object: object}
} }
// Not implemented // Not implemented
@ -176,16 +175,16 @@ func (storage *storage) ListObjects(bucket string, resources mstorage.BucketReso
return results, resources, nil return results, resources, nil
} }
type ByBucketName []mstorage.BucketMetadata type byBucketName []mstorage.BucketMetadata
// Len of bucket name // Len of bucket name
func (b ByBucketName) Len() int { return len(b) } func (b byBucketName) Len() int { return len(b) }
// Swap bucket i, j // Swap bucket i, j
func (b ByBucketName) Swap(i, j int) { b[i], b[j] = b[j], b[i] } func (b byBucketName) Swap(i, j int) { b[i], b[j] = b[j], b[i] }
// Less // Less
func (b ByBucketName) Less(i, j int) bool { return b[i].Name < b[j].Name } func (b byBucketName) Less(i, j int) bool { return b[i].Name < b[j].Name }
// List buckets // List buckets
func (storage *storage) ListBuckets() ([]mstorage.BucketMetadata, error) { func (storage *storage) ListBuckets() ([]mstorage.BucketMetadata, error) {
@ -193,7 +192,7 @@ func (storage *storage) ListBuckets() ([]mstorage.BucketMetadata, error) {
for _, bucket := range storage.bucketdata { for _, bucket := range storage.bucketdata {
results = append(results, bucket.metadata) results = append(results, bucket.metadata)
} }
sort.Sort(ByBucketName(results)) sort.Sort(byBucketName(results))
return results, nil return results, nil
} }
@ -203,7 +202,6 @@ func (storage *storage) GetObjectMetadata(bucket, key string) (mstorage.ObjectMe
if object, ok := storage.objectdata[objectKey]; ok == true { if object, ok := storage.objectdata[objectKey]; ok == true {
return object.metadata, nil return object.metadata, nil
} else {
return mstorage.ObjectMetadata{}, mstorage.ObjectNotFound{Bucket: bucket, Object: key}
} }
return mstorage.ObjectMetadata{}, mstorage.ObjectNotFound{Bucket: bucket, Object: key}
} }

View File

@ -23,6 +23,7 @@ import (
"unicode/utf8" "unicode/utf8"
) )
// Storage - generic API interface
type Storage interface { type Storage interface {
// Bucket Operations // Bucket Operations
ListBuckets() ([]BucketMetadata, error) ListBuckets() ([]BucketMetadata, error)
@ -37,11 +38,13 @@ type Storage interface {
StoreObject(bucket string, key string, contentType string, data io.Reader) error StoreObject(bucket string, key string, contentType string, data io.Reader) error
} }
// BucketMetadata - name and create date
type BucketMetadata struct { type BucketMetadata struct {
Name string Name string
Created time.Time Created time.Time
} }
// ObjectMetadata - object key and its relevant metadata
type ObjectMetadata struct { type ObjectMetadata struct {
Bucket string Bucket string
Key string Key string
@ -52,7 +55,7 @@ type ObjectMetadata struct {
Size int64 Size int64
} }
// Various types of bucket resources // BucketResourcesMetadata - various types of bucket resources
type BucketResourcesMetadata struct { type BucketResourcesMetadata struct {
Prefix string Prefix string
Marker string Marker string
@ -67,7 +70,8 @@ type BucketResourcesMetadata struct {
Notification string Notification string
} }
// Verify Bucket name in accordance with http://docs.aws.amazon.com/AmazonS3/latest/dev/UsingBucket.html // IsValidBucket - verify bucket name in accordance with
// - http://docs.aws.amazon.com/AmazonS3/latest/dev/UsingBucket.html
func IsValidBucket(bucket string) bool { func IsValidBucket(bucket string) bool {
if len(bucket) < 3 || len(bucket) > 63 { if len(bucket) < 3 || len(bucket) > 63 {
return false return false
@ -83,7 +87,8 @@ func IsValidBucket(bucket string) bool {
return match return match
} }
// Verify Object name in accordance with http://docs.aws.amazon.com/AmazonS3/latest/dev/UsingMetadata.html // IsValidObject - verify object name in accordance with
// - http://docs.aws.amazon.com/AmazonS3/latest/dev/UsingMetadata.html
func IsValidObject(object string) bool { func IsValidObject(object string) bool {
if len(object) > 1024 || len(object) == 0 { if len(object) > 1024 || len(object) == 0 {
return false return false

View File

@ -24,7 +24,7 @@ import (
. "gopkg.in/check.v1" . "gopkg.in/check.v1"
) )
// API test suite // APITestSuite - collection of API tests
func APITestSuite(c *C, create func() Storage) { func APITestSuite(c *C, create func() Storage) {
testCreateBucket(c, create) testCreateBucket(c, create)
testMultipleObjectCreation(c, create) testMultipleObjectCreation(c, create)

View File

@ -16,62 +16,76 @@
package storage package storage
// BackendError - generic disk backend error
type BackendError struct { type BackendError struct {
Path string Path string
} }
type GenericError struct { // BackendCorrupted - path has corrupted data
Bucket string type BackendCorrupted BackendError
Path string
// APINotImplemented - generic API not implemented error
type APINotImplemented struct {
API string
} }
type ObjectExists struct { // GenericBucketError - generic bucket error
Bucket string
Key string
}
type ApiNotImplemented struct {
Api string
}
type ObjectNotFound GenericObjectError
type GenericBucketError struct { type GenericBucketError struct {
Bucket string Bucket string
} }
// GenericObjectError - generic object error
type GenericObjectError struct { type GenericObjectError struct {
Bucket string Bucket string
Object string Object string
} }
// ImplementationError - generic implementation error
type ImplementationError struct { type ImplementationError struct {
Bucket string Bucket string
Object string Object string
Err error Err error
} }
type BackendCorrupted BackendError /// Bucket related errors
// BucketPolicyNotFound - missing bucket policy
type BucketPolicyNotFound GenericBucketError type BucketPolicyNotFound GenericBucketError
// BucketNameInvalid - bucketname provided is invalid
type BucketNameInvalid GenericBucketError type BucketNameInvalid GenericBucketError
// BucketExists - bucket already exists
type BucketExists GenericBucketError type BucketExists GenericBucketError
// BucketNotFound - requested bucket not found
type BucketNotFound GenericBucketError type BucketNotFound GenericBucketError
/// Object related errors
// ObjectNotFound - requested object not found
type ObjectNotFound GenericObjectError
// ObjectExists - object already exists
type ObjectExists GenericObjectError
// ObjectNameInvalid - object name provided is invalid
type ObjectNameInvalid GenericObjectError type ObjectNameInvalid GenericObjectError
// Return string an error formatted as the given text // Return string an error formatted as the given text
func (self ImplementationError) Error() string { func (e ImplementationError) Error() string {
error := "" error := ""
if self.Bucket != "" { if e.Bucket != "" {
error = error + "Bucket: " + self.Bucket + " " error = error + "Bucket: " + e.Bucket + " "
} }
if self.Object != "" { if e.Object != "" {
error = error + "Object: " + self.Object + " " error = error + "Object: " + e.Object + " "
} }
error = error + "Error: " + self.Err.Error() error = error + "Error: " + e.Err.Error()
return error return error
} }
// Wrapper function for error object // EmbedError - wrapper function for error object
func EmbedError(bucket, object string, err error) ImplementationError { func EmbedError(bucket, object string, err error) ImplementationError {
return ImplementationError{ return ImplementationError{
Bucket: bucket, Bucket: bucket,
@ -81,46 +95,46 @@ func EmbedError(bucket, object string, err error) ImplementationError {
} }
// Return string an error formatted as the given text // Return string an error formatted as the given text
func (self BucketPolicyNotFound) Error() string { func (e BucketPolicyNotFound) Error() string {
return "Bucket policy not found for: " + self.Bucket return "Bucket policy not found for: " + e.Bucket
} }
// Return string an error formatted as the given text // Return string an error formatted as the given text
func (self ObjectNotFound) Error() string { func (e ObjectNotFound) Error() string {
return "Object not Found: " + self.Bucket + "#" + self.Object return "Object not Found: " + e.Bucket + "#" + e.Object
} }
// Return string an error formatted as the given text // Return string an error formatted as the given text
func (self ApiNotImplemented) Error() string { func (e APINotImplemented) Error() string {
return "Api not implemented: " + self.Api return "Api not implemented: " + e.Api
} }
// Return string an error formatted as the given text // Return string an error formatted as the given text
func (self ObjectExists) Error() string { func (e ObjectExists) Error() string {
return "Object exists: " + self.Bucket + "#" + self.Key return "Object exists: " + e.Bucket + "#" + e.Object
} }
// Return string an error formatted as the given text // Return string an error formatted as the given text
func (self BucketNameInvalid) Error() string { func (e BucketNameInvalid) Error() string {
return "Bucket name invalid: " + self.Bucket return "Bucket name invalid: " + e.Bucket
} }
// Return string an error formatted as the given text // Return string an error formatted as the given text
func (self BucketExists) Error() string { func (e BucketExists) Error() string {
return "Bucket exists: " + self.Bucket return "Bucket exists: " + e.Bucket
} }
// Return string an error formatted as the given text // Return string an error formatted as the given text
func (self BucketNotFound) Error() string { func (e BucketNotFound) Error() string {
return "Bucket not Found: " + self.Bucket return "Bucket not Found: " + e.Bucket
} }
// Return string an error formatted as the given text // Return string an error formatted as the given text
func (self ObjectNameInvalid) Error() string { func (e ObjectNameInvalid) Error() string {
return "Object name invalid: " + self.Bucket + "#" + self.Object return "Object name invalid: " + e.Bucket + "#" + e.Object
} }
// Return string an error formatted as the given text // Return string an error formatted as the given text
func (self BackendCorrupted) Error() string { func (e BackendCorrupted) Error() string {
return "Backend corrupted: " + self.Path return "Backend corrupted: " + e.Path
} }