minio/pkg/storage/memory/memory.go

261 lines
8.0 KiB
Go
Raw Normal View History

2015-02-08 05:54:21 -05:00
/*
2015-03-05 23:32:04 -05:00
* Minio Object Storage, (C) 2015 Minio, Inc.
2015-02-08 05:54:21 -05:00
*
* 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.
*/
package memory
2015-01-21 15:44:09 -05:00
import (
"bufio"
2015-01-21 15:44:09 -05:00
"bytes"
"fmt"
"io"
"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"
"crypto/md5"
"encoding/hex"
2015-01-21 15:44:09 -05:00
mstorage "github.com/minio-io/minio/pkg/storage"
)
// Storage - local variables
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
}
// Start memory object server
func Start() (chan<- string, <-chan error, *Storage) {
2015-03-03 04:25:45 -05:00
ctrlChannel := make(chan string)
errorChannel := make(chan error)
go start(ctrlChannel, errorChannel)
return ctrlChannel, errorChannel, &Storage{
2015-03-03 04:25:45 -05:00
bucketdata: make(map[string]storedBucket),
objectdata: make(map[string]storedObject),
lock: new(sync.RWMutex),
}
}
func start(ctrlChannel <-chan string, errorChannel chan<- error) {
close(errorChannel)
}
// GetObject - GET object from memory buffer
func (storage *Storage) GetObject(w io.Writer, bucket string, object string) (int64, error) {
2015-01-21 15:44:09 -05:00
// get object
key := object
2015-01-21 15:44:09 -05:00
if val, ok := storage.objectdata[key]; ok {
objectBuffer := bytes.NewBuffer(val.data)
written, err := io.Copy(w, objectBuffer)
return written, err
}
2015-03-05 23:32:04 -05:00
return 0, mstorage.ObjectNotFound{Bucket: bucket, Object: object}
2015-01-21 15:44:09 -05:00
}
// GetPartialObject - GET object from memory buffer range
func (storage *Storage) GetPartialObject(w io.Writer, bucket, object string, start, end int64) (int64, error) {
return 0, mstorage.APINotImplemented{API: "GetPartialObject"}
2015-03-11 04:01:49 -04:00
}
// CreateBucketPolicy - Not implemented
func (storage *Storage) CreateBucketPolicy(bucket string, policy mstorage.BucketPolicy) error {
2015-03-06 00:07:19 -05:00
return mstorage.APINotImplemented{API: "PutBucketPolicy"}
}
// GetBucketPolicy - Not implemented
2015-03-08 18:57:18 -04:00
func (storage *Storage) GetBucketPolicy(bucket string) (mstorage.BucketPolicy, error) {
return mstorage.BucketPolicy{}, mstorage.APINotImplemented{API: "GetBucketPolicy"}
}
// CreateObject - PUT object to memory buffer
func (storage *Storage) CreateObject(bucket, key, contentType string, data io.Reader) error {
2015-02-18 18:33:19 -05:00
storage.lock.Lock()
defer storage.lock.Unlock()
if _, ok := storage.bucketdata[bucket]; ok == false {
return mstorage.BucketNotFound{Bucket: bucket}
}
if _, ok := storage.objectdata[key]; ok == true {
2015-03-06 00:07:19 -05:00
return mstorage.ObjectExists{Bucket: bucket, Object: key}
2015-01-21 15:44:09 -05:00
}
if contentType == "" {
contentType = "application/octet-stream"
}
contentType = strings.TrimSpace(contentType)
2015-01-21 15:44:09 -05:00
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()
md5SumBytes := md5.Sum(bytesBuffer.Bytes())
md5Sum := hex.EncodeToString(md5SumBytes[:])
2015-01-21 15:44:09 -05:00
newObject.metadata = mstorage.ObjectMetadata{
Bucket: bucket,
Key: key,
ContentType: contentType,
Created: time.Now(),
Md5: md5Sum,
Size: int64(size),
2015-01-21 15:44:09 -05:00
}
newObject.data = bytesBuffer.Bytes()
}
storage.objectdata[key] = newObject
2015-01-21 15:44:09 -05:00
return nil
}
2015-03-11 23:14:40 -04:00
// CreateBucket - create bucket in memory
func (storage *Storage) CreateBucket(bucketName string) error {
2015-02-18 18:33:19 -05:00
storage.lock.Lock()
defer storage.lock.Unlock()
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
}
func delimiter(object, delimiter string) string {
readBuffer := bytes.NewBufferString(object)
reader := bufio.NewReader(readBuffer)
stringReader := strings.NewReader(delimiter)
delimited, _ := stringReader.ReadByte()
delimitedStr, _ := reader.ReadString(delimited)
return delimitedStr
}
func appendUniq(slice []string, i string) []string {
for _, ele := range slice {
if ele == i {
return slice
}
}
return append(slice, i)
}
// ListObjects - list objects from memory
func (storage *Storage) ListObjects(bucket string, resources mstorage.BucketResourcesMetadata) ([]mstorage.ObjectMetadata, mstorage.BucketResourcesMetadata, error) {
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-01-21 15:44:09 -05:00
var results []mstorage.ObjectMetadata
var keys []string
for key := range storage.objectdata {
switch true {
// Prefix absent, delimit object key based on delimiter
case resources.Delimiter != "" && resources.Prefix == "":
delimitedName := delimiter(key, resources.Delimiter)
switch true {
case delimitedName == "" || delimitedName == key:
keys = appendUniq(keys, key)
case delimitedName != "":
resources.CommonPrefixes = appendUniq(resources.CommonPrefixes, delimitedName)
}
// Prefix present, delimit object key with prefix key based on delimiter
case resources.Delimiter != "" && resources.Prefix != "" && strings.HasPrefix(key, resources.Prefix):
trimmedName := strings.TrimPrefix(key, resources.Prefix)
delimitedName := delimiter(trimmedName, resources.Delimiter)
fmt.Println(trimmedName, delimitedName, key, resources.Prefix)
switch true {
case key == resources.Prefix:
keys = appendUniq(keys, key)
// DelimitedName - requires resources.Prefix as it was trimmed off earlier in the flow
case key == resources.Prefix+delimitedName:
keys = appendUniq(keys, key)
case delimitedName != "":
if delimitedName == resources.Delimiter {
resources.CommonPrefixes = appendUniq(resources.CommonPrefixes, resources.Prefix+delimitedName)
} else {
resources.CommonPrefixes = appendUniq(resources.CommonPrefixes, delimitedName)
}
}
// Prefix present, nothing to delimit
case resources.Delimiter == "" && resources.Prefix != "" && strings.HasPrefix(key, resources.Prefix):
keys = appendUniq(keys, key)
// Prefix and delimiter absent
case resources.Prefix == "" && resources.Delimiter == "":
keys = appendUniq(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
}
object := storage.objectdata[key]
if bucket == object.metadata.Bucket {
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-03-14 14:16:26 -04:00
// ByBucketName is a type for sorting bucket metadata by bucket name
type ByBucketName []mstorage.BucketMetadata
2015-02-01 03:16:25 -05:00
2015-03-03 04:25:45 -05:00
// Len of bucket name
func (b ByBucketName) Len() int { return len(b) }
2015-03-03 04:25:45 -05:00
// Swap bucket i, j
func (b ByBucketName) Swap(i, j int) { b[i], b[j] = b[j], b[i] }
2015-03-03 04:25:45 -05:00
// Less
func (b ByBucketName) Less(i, j int) bool { return b[i].Name < b[j].Name }
2015-02-01 03:16:25 -05:00
// ListBuckets - List buckets from memory
func (storage *Storage) ListBuckets() ([]mstorage.BucketMetadata, error) {
2015-01-21 15:44:09 -05:00
var results []mstorage.BucketMetadata
for _, bucket := range storage.bucketdata {
results = append(results, bucket.metadata)
2015-01-21 15:44:09 -05:00
}
sort.Sort(ByBucketName(results))
return results, nil
2015-01-21 15:44:09 -05:00
}
// GetObjectMetadata - get object metadata from memory
func (storage *Storage) GetObjectMetadata(bucket, key, prefix string) (mstorage.ObjectMetadata, error) {
if object, ok := storage.objectdata[key]; ok == true {
2015-01-22 17:24:18 -05:00
return object.metadata, nil
}
2015-03-05 23:32:04 -05:00
return mstorage.ObjectMetadata{}, mstorage.ObjectNotFound{Bucket: bucket, Object: key}
2015-01-21 18:02:08 -05:00
}