Adding error support to all commands in storage interface

This commit is contained in:
Frederick F. Kautz IV 2015-01-25 15:35:08 -08:00
parent d37bbfec41
commit 2368e7c936
5 changed files with 36 additions and 17 deletions

View File

@ -86,12 +86,12 @@ func (storage *storage) StoreBucket(bucketName string) error {
return nil return nil
} }
func (storage *storage) ListObjects(bucket, prefix string, count int) ([]mstorage.ObjectMetadata, bool) { func (storage *storage) ListObjects(bucket, prefix string, count int) ([]mstorage.ObjectMetadata, bool, error) {
// TODO prefix and count handling // TODO prefix and count handling
var results []mstorage.ObjectMetadata var results []mstorage.ObjectMetadata
for key, object := range storage.objectdata { for key, object := range storage.objectdata {
if len(results) == count { if len(results) == count {
return results, true return results, true, nil
} }
if bucket == object.metadata.Bucket { if bucket == object.metadata.Bucket {
if strings.HasPrefix(key, bucket+":") { if strings.HasPrefix(key, bucket+":") {
@ -99,16 +99,16 @@ func (storage *storage) ListObjects(bucket, prefix string, count int) ([]mstorag
} }
} }
} }
return results, false return results, false, nil
} }
func (storage *storage) ListBuckets(prefix string) []mstorage.BucketMetadata { func (storage *storage) ListBuckets(prefix string) ([]mstorage.BucketMetadata, error) {
// TODO prefix handling // TODO prefix handling
var results []mstorage.BucketMetadata var results []mstorage.BucketMetadata
for _, bucket := range storage.bucketdata { for _, bucket := range storage.bucketdata {
results = append(results, bucket.metadata) results = append(results, bucket.metadata)
} }
return results return results, nil
} }
func Start() (chan<- string, <-chan error, *storage) { func Start() (chan<- string, <-chan error, *storage) {

View File

@ -24,14 +24,14 @@ import (
type Storage interface { type Storage interface {
// Bucket Operations // Bucket Operations
ListBuckets(prefix string) ([]BucketMetadata, error)
StoreBucket(bucket string) error StoreBucket(bucket string) error
ListBuckets(prefix string) []BucketMetadata
// Object Operations // Object Operations
GetObjectMetadata(bucket string, object string) (ObjectMetadata, error)
CopyObjectToWriter(w io.Writer, bucket string, object string) (int64, error) CopyObjectToWriter(w io.Writer, bucket string, object string) (int64, error)
GetObjectMetadata(bucket string, object string) (ObjectMetadata, error)
ListObjects(bucket, prefix string, count int) ([]ObjectMetadata, bool, error)
StoreObject(bucket string, key string, data io.Reader) error StoreObject(bucket string, key string, data io.Reader) error
ListObjects(bucket, prefix string, count int) ([]ObjectMetadata, bool)
} }
type BucketMetadata struct { type BucketMetadata struct {

View File

@ -61,22 +61,25 @@ func testPaging(c *C, create func() Storage) {
storage := create() storage := create()
storage.StoreBucket("bucket") storage.StoreBucket("bucket")
storage.ListObjects("bucket", "", 1000) storage.ListObjects("bucket", "", 1000)
objects, isTruncated := storage.ListObjects("bucket", "", 1000) objects, isTruncated, err := storage.ListObjects("bucket", "", 1000)
c.Assert(len(objects), Equals, 0) c.Assert(len(objects), Equals, 0)
c.Assert(isTruncated, Equals, false) c.Assert(isTruncated, Equals, false)
c.Assert(err, IsNil)
for i := 1; i <= 1000; i++ { for i := 1; i <= 1000; i++ {
key := "obj" + strconv.Itoa(i) key := "obj" + strconv.Itoa(i)
storage.StoreObject("bucket", key, bytes.NewBufferString(key)) storage.StoreObject("bucket", key, bytes.NewBufferString(key))
objects, isTruncated = storage.ListObjects("bucket", "", 1000) objects, isTruncated, err = storage.ListObjects("bucket", "", 1000)
c.Assert(len(objects), Equals, i) c.Assert(len(objects), Equals, i)
c.Assert(isTruncated, Equals, false) c.Assert(isTruncated, Equals, false)
c.Assert(err, IsNil)
} }
for i := 1001; i <= 2000; i++ { for i := 1001; i <= 2000; i++ {
key := "obj" + strconv.Itoa(i) key := "obj" + strconv.Itoa(i)
storage.StoreObject("bucket", key, bytes.NewBufferString(key)) storage.StoreObject("bucket", key, bytes.NewBufferString(key))
objects, isTruncated = storage.ListObjects("bucket", "", 1000) objects, isTruncated, err = storage.ListObjects("bucket", "", 1000)
c.Assert(len(objects), Equals, 1000) c.Assert(len(objects), Equals, 1000)
c.Assert(isTruncated, Equals, true) c.Assert(isTruncated, Equals, true)
c.Assert(err, IsNil)
} }
} }

View File

@ -153,7 +153,12 @@ func (server *minioApi) listBucketsHandler(w http.ResponseWriter, req *http.Requ
contentType = jsonType contentType = jsonType
} }
} }
buckets := server.storage.ListBuckets(prefix) buckets, err := server.storage.ListBuckets(prefix)
if err != nil {
log.Println(err)
w.WriteHeader(http.StatusInternalServerError)
return
}
response := generateBucketsListResult(buckets) response := generateBucketsListResult(buckets)
var bytesBuffer bytes.Buffer var bytesBuffer bytes.Buffer
@ -189,7 +194,12 @@ func (server *minioApi) listObjectsHandler(w http.ResponseWriter, req *http.Requ
} }
} }
objects, isTruncated := server.storage.ListObjects(bucket, prefix, 1000) objects, isTruncated, err := server.storage.ListObjects(bucket, prefix, 1000)
if err != nil {
log.Println(err)
w.WriteHeader(http.StatusInternalServerError)
return
}
response := generateObjectsListResult(bucket, objects, isTruncated) response := generateObjectsListResult(bucket, objects, isTruncated)
var bytesBuffer bytes.Buffer var bytesBuffer bytes.Buffer

View File

@ -214,8 +214,9 @@ func (s *MySuite) TestPutBucket(c *C) {
testServer := httptest.NewServer(httpHandler) testServer := httptest.NewServer(httpHandler)
defer testServer.Close() defer testServer.Close()
buckets := storage.ListBuckets("bucket") buckets, err := storage.ListBuckets("bucket")
c.Assert(len(buckets), Equals, 0) c.Assert(len(buckets), Equals, 0)
c.Assert(err, IsNil)
request, err := http.NewRequest("PUT", testServer.URL+"/bucket/", bytes.NewBufferString("")) request, err := http.NewRequest("PUT", testServer.URL+"/bucket/", bytes.NewBufferString(""))
c.Assert(err, IsNil) c.Assert(err, IsNil)
@ -226,8 +227,9 @@ func (s *MySuite) TestPutBucket(c *C) {
c.Assert(response.StatusCode, Equals, http.StatusOK) c.Assert(response.StatusCode, Equals, http.StatusOK)
// check bucket exists // check bucket exists
buckets = storage.ListBuckets("bucket") buckets, err = storage.ListBuckets("bucket")
c.Assert(len(buckets), Equals, 1) c.Assert(len(buckets), Equals, 1)
c.Assert(err, IsNil)
c.Assert(buckets[0].Name, Equals, "bucket") c.Assert(buckets[0].Name, Equals, "bucket")
} }
@ -237,8 +239,10 @@ func (s *MySuite) TestPutObject(c *C) {
testServer := httptest.NewServer(httpHandler) testServer := httptest.NewServer(httpHandler)
defer testServer.Close() defer testServer.Close()
objects, _ := storage.ListObjects("bucket", "", 1000) objects, isTruncated, err := storage.ListObjects("bucket", "", 1000)
c.Assert(len(objects), Equals, 0) c.Assert(len(objects), Equals, 0)
c.Assert(isTruncated, Equals, false)
c.Assert(err, IsNil)
date1 := time.Now() date1 := time.Now()
@ -260,8 +264,10 @@ func (s *MySuite) TestPutObject(c *C) {
date2 := time.Now() date2 := time.Now()
objects, _ = storage.ListObjects("bucket", "", 1000) objects, isTruncated, err = storage.ListObjects("bucket", "", 1000)
c.Assert(len(objects), Equals, 1) c.Assert(len(objects), Equals, 1)
c.Assert(isTruncated, Equals, false)
c.Assert(err, IsNil)
var writer bytes.Buffer var writer bytes.Buffer