2015-02-08 05:54:21 -05:00
|
|
|
/*
|
|
|
|
* Mini Object Storage, (C) 2015 Minio, Inc.
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2015-01-21 15:44:09 -05:00
|
|
|
package inmemory
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"crypto/sha256"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
2015-01-27 16:11:15 -05:00
|
|
|
"sort"
|
2015-01-21 15:44:09 -05:00
|
|
|
"strings"
|
2015-02-18 18:33:19 -05:00
|
|
|
"sync"
|
2015-01-21 15:44:09 -05:00
|
|
|
"time"
|
|
|
|
|
|
|
|
mstorage "github.com/minio-io/minio/pkg/storage"
|
2015-02-15 20:03:27 -05:00
|
|
|
"github.com/minio-io/minio/pkg/utils/policy"
|
2015-01-21 15:44:09 -05:00
|
|
|
)
|
|
|
|
|
2015-01-21 20:15:05 -05:00
|
|
|
type storage struct {
|
2015-01-21 15:44:09 -05:00
|
|
|
bucketdata map[string]storedBucket
|
|
|
|
objectdata map[string]storedObject
|
2015-02-18 18:33:19 -05:00
|
|
|
lock *sync.RWMutex
|
2015-01-21 15:44:09 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
type storedBucket struct {
|
|
|
|
metadata mstorage.BucketMetadata
|
|
|
|
// owner string // TODO
|
|
|
|
// id string // TODO
|
|
|
|
}
|
|
|
|
|
|
|
|
type storedObject struct {
|
|
|
|
metadata mstorage.ObjectMetadata
|
|
|
|
data []byte
|
|
|
|
}
|
|
|
|
|
2015-03-03 04:25:45 -05:00
|
|
|
// Start inmemory object server
|
|
|
|
func Start() (chan<- string, <-chan error, *storage) {
|
|
|
|
ctrlChannel := make(chan string)
|
|
|
|
errorChannel := make(chan error)
|
|
|
|
go start(ctrlChannel, errorChannel)
|
|
|
|
return ctrlChannel, errorChannel, &storage{
|
|
|
|
bucketdata: make(map[string]storedBucket),
|
|
|
|
objectdata: make(map[string]storedObject),
|
|
|
|
lock: new(sync.RWMutex),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func start(ctrlChannel <-chan string, errorChannel chan<- error) {
|
|
|
|
close(errorChannel)
|
|
|
|
}
|
|
|
|
|
|
|
|
// GET object from memory buffer
|
2015-01-21 20:15:05 -05:00
|
|
|
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 {
|
2015-01-25 20:41:59 -05:00
|
|
|
return 0, mstorage.ObjectNotFound{Bucket: bucket, Object: object}
|
2015-01-21 15:44:09 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-03 04:25:45 -05:00
|
|
|
// Not implemented
|
2015-02-15 20:03:27 -05:00
|
|
|
func (storage *storage) StoreBucketPolicy(bucket string, policy interface{}) error {
|
|
|
|
return mstorage.ApiNotImplemented{Api: "PutBucketPolicy"}
|
|
|
|
}
|
|
|
|
|
2015-03-03 04:25:45 -05:00
|
|
|
// Not implemented
|
2015-02-15 20:03:27 -05:00
|
|
|
func (storage *storage) GetBucketPolicy(bucket string) (interface{}, error) {
|
|
|
|
return policy.BucketPolicy{}, mstorage.ApiNotImplemented{Api: "GetBucketPolicy"}
|
|
|
|
}
|
|
|
|
|
2015-03-03 04:25:45 -05:00
|
|
|
// PUT object to memory buffer
|
2015-02-04 20:32:40 -05:00
|
|
|
func (storage *storage) StoreObject(bucket, key, contentType string, data io.Reader) error {
|
2015-02-18 18:33:19 -05:00
|
|
|
storage.lock.Lock()
|
|
|
|
defer storage.lock.Unlock()
|
|
|
|
|
2015-01-21 15:44:09 -05:00
|
|
|
objectKey := bucket + ":" + key
|
2015-01-24 18:35:01 -05:00
|
|
|
|
|
|
|
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}
|
|
|
|
}
|
2015-02-04 20:32:40 -05:00
|
|
|
|
|
|
|
if contentType == "" {
|
|
|
|
contentType = "application/octet-stream"
|
|
|
|
}
|
|
|
|
|
|
|
|
contentType = strings.TrimSpace(contentType)
|
|
|
|
|
2015-01-21 15:44:09 -05:00
|
|
|
var bytesBuffer bytes.Buffer
|
2015-01-24 18:35:01 -05:00
|
|
|
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{
|
2015-02-04 20:32:40 -05:00
|
|
|
Bucket: bucket,
|
|
|
|
Key: key,
|
|
|
|
|
|
|
|
ContentType: contentType,
|
|
|
|
Created: time.Now(),
|
|
|
|
ETag: etag,
|
|
|
|
Size: int64(size),
|
2015-01-21 15:44:09 -05:00
|
|
|
}
|
|
|
|
newObject.data = bytesBuffer.Bytes()
|
|
|
|
}
|
|
|
|
storage.objectdata[objectKey] = newObject
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-03-03 04:25:45 -05:00
|
|
|
// Create Bucket in memory
|
2015-01-21 20:15:05 -05:00
|
|
|
func (storage *storage) StoreBucket(bucketName string) error {
|
2015-02-18 18:33:19 -05:00
|
|
|
storage.lock.Lock()
|
|
|
|
defer storage.lock.Unlock()
|
2015-01-21 18:22:15 -05:00
|
|
|
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}
|
|
|
|
}
|
2015-01-24 18:35:01 -05:00
|
|
|
|
|
|
|
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-24 18:35:01 -05:00
|
|
|
|
2015-01-21 15:44:09 -05:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-03-03 04:25:45 -05:00
|
|
|
// List objects in memory
|
2015-02-26 20:23:42 -05:00
|
|
|
func (storage *storage) ListObjects(bucket string, resources mstorage.BucketResourcesMetadata) ([]mstorage.ObjectMetadata, mstorage.BucketResourcesMetadata, error) {
|
2015-02-01 13:48:31 -05:00
|
|
|
if _, ok := storage.bucketdata[bucket]; ok == false {
|
2015-02-26 20:23:42 -05:00
|
|
|
return []mstorage.ObjectMetadata{}, mstorage.BucketResourcesMetadata{IsTruncated: false}, mstorage.BucketNotFound{Bucket: bucket}
|
2015-02-01 13:48:31 -05:00
|
|
|
}
|
2015-01-21 15:44:09 -05:00
|
|
|
var results []mstorage.ObjectMetadata
|
2015-01-27 16:11:15 -05:00
|
|
|
var keys []string
|
|
|
|
for key := range storage.objectdata {
|
2015-02-26 20:23:42 -05:00
|
|
|
if strings.HasPrefix(key, bucket+":"+resources.Prefix) {
|
2015-01-27 16:11:15 -05:00
|
|
|
keys = append(keys, key)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
sort.Strings(keys)
|
|
|
|
for _, key := range keys {
|
2015-02-26 20:23:42 -05:00
|
|
|
if len(results) == resources.Maxkeys {
|
|
|
|
return results, mstorage.BucketResourcesMetadata{IsTruncated: true}, nil
|
2015-01-25 16:32:39 -05:00
|
|
|
}
|
2015-01-27 16:11:15 -05:00
|
|
|
object := storage.objectdata[key]
|
2015-01-24 18:35:01 -05:00
|
|
|
if bucket == object.metadata.Bucket {
|
2015-02-26 20:23:42 -05:00
|
|
|
if strings.HasPrefix(key, bucket+":"+resources.Prefix) {
|
2015-01-24 18:35:01 -05:00
|
|
|
results = append(results, object.metadata)
|
|
|
|
}
|
2015-01-21 15:44:09 -05:00
|
|
|
}
|
|
|
|
}
|
2015-02-26 20:23:42 -05:00
|
|
|
return results, resources, nil
|
2015-01-21 15:44:09 -05:00
|
|
|
}
|
|
|
|
|
2015-02-01 03:16:25 -05:00
|
|
|
type ByBucketName []mstorage.BucketMetadata
|
|
|
|
|
2015-03-03 04:25:45 -05:00
|
|
|
// Len of bucket name
|
|
|
|
func (b ByBucketName) Len() int { return len(b) }
|
|
|
|
|
|
|
|
// Swap bucket i, j
|
|
|
|
func (b ByBucketName) Swap(i, j int) { b[i], b[j] = b[j], b[i] }
|
|
|
|
|
|
|
|
// Less
|
2015-02-01 03:16:25 -05:00
|
|
|
func (b ByBucketName) Less(i, j int) bool { return b[i].Name < b[j].Name }
|
|
|
|
|
2015-03-03 04:25:45 -05:00
|
|
|
// List buckets
|
2015-02-15 20:03:27 -05:00
|
|
|
func (storage *storage) ListBuckets() ([]mstorage.BucketMetadata, error) {
|
2015-01-21 15:44:09 -05:00
|
|
|
var results []mstorage.BucketMetadata
|
2015-02-15 20:03:27 -05:00
|
|
|
for _, bucket := range storage.bucketdata {
|
|
|
|
results = append(results, bucket.metadata)
|
2015-01-21 15:44:09 -05:00
|
|
|
}
|
2015-02-01 03:16:25 -05:00
|
|
|
sort.Sort(ByBucketName(results))
|
2015-01-25 18:35:08 -05:00
|
|
|
return results, nil
|
2015-01-21 15:44:09 -05:00
|
|
|
}
|
|
|
|
|
2015-03-03 04:25:45 -05:00
|
|
|
// HEAD object
|
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 {
|
2015-01-25 20:41:59 -05:00
|
|
|
return mstorage.ObjectMetadata{}, mstorage.ObjectNotFound{Bucket: bucket, Object: key}
|
2015-01-22 17:24:18 -05:00
|
|
|
}
|
2015-01-21 18:02:08 -05:00
|
|
|
}
|