minio/pkg/storage/inmemory/inmemory.go

137 lines
3.5 KiB
Go
Raw Normal View History

2015-01-21 15:44:09 -05:00
package inmemory
import (
"bytes"
"crypto/sha256"
"fmt"
"io"
"strings"
"time"
mstorage "github.com/minio-io/minio/pkg/storage"
)
type storage struct {
2015-01-21 15:44:09 -05:00
bucketdata map[string]storedBucket
objectdata map[string]storedObject
}
type storedBucket struct {
metadata mstorage.BucketMetadata
// owner string // TODO
// id string // TODO
}
type storedObject struct {
metadata mstorage.ObjectMetadata
data []byte
}
func (storage *storage) CopyObjectToWriter(w io.Writer, bucket string, object string) (int64, error) {
2015-01-21 15:44:09 -05:00
// TODO synchronize access
// get object
key := bucket + ":" + object
if val, ok := storage.objectdata[key]; ok {
objectBuffer := bytes.NewBuffer(val.data)
written, err := io.Copy(w, objectBuffer)
return written, err
} else {
return 0, mstorage.ObjectNotFound{Bucket: bucket, Path: object}
}
}
func (storage *storage) StoreObject(bucket string, key string, data io.Reader) error {
2015-01-21 15:44:09 -05:00
objectKey := bucket + ":" + key
if _, ok := storage.bucketdata[bucket]; ok == false {
return mstorage.BucketNotFound{Bucket: bucket}
}
2015-01-21 15:44:09 -05:00
if _, ok := storage.objectdata[objectKey]; ok == true {
return mstorage.ObjectExists{Bucket: bucket, Key: key}
}
var bytesBuffer bytes.Buffer
var newObject = storedObject{}
2015-01-21 15:44:09 -05:00
if _, ok := io.Copy(&bytesBuffer, data); ok == nil {
size := bytesBuffer.Len()
etag := fmt.Sprintf("%x", sha256.Sum256(bytesBuffer.Bytes()))
newObject.metadata = mstorage.ObjectMetadata{
Bucket: bucket,
2015-01-21 20:12:47 -05:00
Key: key,
Created: time.Now(),
Size: size,
ETag: etag,
2015-01-21 15:44:09 -05:00
}
newObject.data = bytesBuffer.Bytes()
}
storage.objectdata[objectKey] = newObject
return nil
}
func (storage *storage) StoreBucket(bucketName string) error {
if !mstorage.IsValidBucket(bucketName) {
2015-01-21 15:44:09 -05:00
return mstorage.BucketNameInvalid{Bucket: bucketName}
}
if _, ok := storage.bucketdata[bucketName]; ok == true {
return mstorage.BucketExists{Bucket: bucketName}
}
var newBucket = storedBucket{}
newBucket.metadata = mstorage.BucketMetadata{}
newBucket.metadata.Name = bucketName
newBucket.metadata.Created = time.Now()
2015-01-21 15:44:09 -05:00
storage.bucketdata[bucketName] = newBucket
2015-01-21 15:44:09 -05:00
return nil
}
2015-01-25 16:32:39 -05:00
func (storage *storage) ListObjects(bucket, prefix string, count int) ([]mstorage.ObjectMetadata, bool) {
2015-01-21 15:44:09 -05:00
// TODO prefix and count handling
var results []mstorage.ObjectMetadata
for key, object := range storage.objectdata {
2015-01-25 16:32:39 -05:00
if len(results) == count {
return results, true
}
if bucket == object.metadata.Bucket {
if strings.HasPrefix(key, bucket+":") {
results = append(results, object.metadata)
}
2015-01-21 15:44:09 -05:00
}
}
2015-01-25 16:32:39 -05:00
return results, false
2015-01-21 15:44:09 -05:00
}
func (storage *storage) ListBuckets(prefix string) []mstorage.BucketMetadata {
2015-01-21 15:44:09 -05:00
// TODO prefix handling
var results []mstorage.BucketMetadata
for _, bucket := range storage.bucketdata {
results = append(results, bucket.metadata)
}
return results
}
func Start() (chan<- string, <-chan error, *storage) {
2015-01-21 15:44:09 -05:00
ctrlChannel := make(chan string)
errorChannel := make(chan error)
go start(ctrlChannel, errorChannel)
return ctrlChannel, errorChannel, &storage{
2015-01-21 15:44:09 -05:00
bucketdata: make(map[string]storedBucket),
objectdata: make(map[string]storedObject),
}
}
func start(ctrlChannel <-chan string, errorChannel chan<- error) {
close(errorChannel)
}
2015-01-22 17:24:18 -05:00
func (storage *storage) GetObjectMetadata(bucket, key string) (mstorage.ObjectMetadata, error) {
2015-01-21 18:02:08 -05:00
objectKey := bucket + ":" + key
2015-01-22 17:24:18 -05:00
if object, ok := storage.objectdata[objectKey]; ok == true {
return object.metadata, nil
} else {
return mstorage.ObjectMetadata{}, mstorage.ObjectNotFound{Bucket: bucket, Path: key}
}
2015-01-21 18:02:08 -05:00
}