Adding error support to all commands in storage interface

master
Frederick F. Kautz IV 10 years ago
parent d37bbfec41
commit 2368e7c936
  1. 10
      pkg/storage/inmemory/inmemory.go
  2. 6
      pkg/storage/storage.go
  3. 9
      pkg/storage/storage_api_suite.go
  4. 14
      pkg/webapi/minioapi/minioapi.go
  5. 14
      pkg/webapi/minioapi/minioapi_test.go

@ -86,12 +86,12 @@ func (storage *storage) StoreBucket(bucketName string) error {
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
var results []mstorage.ObjectMetadata
for key, object := range storage.objectdata {
if len(results) == count {
return results, true
return results, true, nil
}
if bucket == object.metadata.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
var results []mstorage.BucketMetadata
for _, bucket := range storage.bucketdata {
results = append(results, bucket.metadata)
}
return results
return results, nil
}
func Start() (chan<- string, <-chan error, *storage) {

@ -24,14 +24,14 @@ import (
type Storage interface {
// Bucket Operations
ListBuckets(prefix string) ([]BucketMetadata, error)
StoreBucket(bucket string) error
ListBuckets(prefix string) []BucketMetadata
// Object Operations
GetObjectMetadata(bucket string, object string) (ObjectMetadata, 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
ListObjects(bucket, prefix string, count int) ([]ObjectMetadata, bool)
}
type BucketMetadata struct {

@ -61,22 +61,25 @@ func testPaging(c *C, create func() Storage) {
storage := create()
storage.StoreBucket("bucket")
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(isTruncated, Equals, false)
c.Assert(err, IsNil)
for i := 1; i <= 1000; i++ {
key := "obj" + strconv.Itoa(i)
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(isTruncated, Equals, false)
c.Assert(err, IsNil)
}
for i := 1001; i <= 2000; i++ {
key := "obj" + strconv.Itoa(i)
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(isTruncated, Equals, true)
c.Assert(err, IsNil)
}
}

@ -153,7 +153,12 @@ func (server *minioApi) listBucketsHandler(w http.ResponseWriter, req *http.Requ
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)
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)
var bytesBuffer bytes.Buffer

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

Loading…
Cancel
Save