minio/pkg/storage/storage.go

173 lines
4.0 KiB
Go
Raw Normal View History

2015-01-21 02:16:06 -05:00
/*
* Mini Object Storage, (C) 2014 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.
*/
package storage
import (
"bytes"
"io"
2015-01-20 21:39:30 -05:00
"log"
"strings"
"time"
)
2015-01-18 19:54:45 -05:00
type Storage struct {
2015-01-21 03:50:23 -05:00
bucketdata map[string]storedBucket
objectdata map[string]storedObject
2015-01-18 19:54:45 -05:00
}
2015-01-20 21:39:30 -05:00
type storedObject struct {
metadata ObjectMetadata
data []byte
}
2015-01-21 03:50:23 -05:00
type storedBucket struct {
metadata BucketMetadata
// owner string // TODO
// id string // TODO
}
type BucketMetadata struct {
Name string
Created int64
}
2015-01-20 21:39:30 -05:00
type ObjectMetadata struct {
Key string
SecCreated int64
Size int
}
2015-01-20 19:08:14 -05:00
2015-01-21 03:50:23 -05:00
func isValidBucket(bucket string) bool {
l := len(bucket)
if l < 3 || l > 63 {
return false
}
valid := false
prev := byte('.')
for i := 0; i < len(bucket); i++ {
c := bucket[i]
switch {
default:
return false
case 'a' <= c && c <= 'z':
valid = true
case '0' <= c && c <= '9':
// Is allowed, but bucketname can't be just numbers.
// Therefore, don't set valid to true
case c == '-':
if prev == '.' {
return false
}
case c == '.':
if prev == '.' || prev == '-' {
return false
}
}
prev = c
}
if prev == '-' || prev == '.' {
return false
}
return valid
}
func (storage *Storage) CopyObjectToWriter(w io.Writer, bucket string, object string) (int64, error) {
// TODO synchronize access
// get object
2015-01-18 19:54:45 -05:00
key := bucket + ":" + object
2015-01-21 03:50:23 -05:00
if val, ok := storage.objectdata[key]; ok {
2015-01-20 21:39:30 -05:00
objectBuffer := bytes.NewBuffer(val.data)
written, err := io.Copy(w, objectBuffer)
return written, err
2015-01-18 19:54:45 -05:00
} else {
return 0, ObjectNotFound{bucket: bucket, path: object}
2015-01-18 19:54:45 -05:00
}
}
2015-01-20 22:13:57 -05:00
func (storage *Storage) StoreObject(bucket string, key string, data io.Reader) error {
2015-01-20 21:39:30 -05:00
objectKey := bucket + ":" + key
2015-01-21 03:50:23 -05:00
if _, ok := storage.objectdata[objectKey]; ok == true {
2015-01-20 22:13:57 -05:00
return ObjectExists{bucket: bucket, key: key}
}
2015-01-18 19:54:45 -05:00
var bytesBuffer bytes.Buffer
2015-01-20 21:39:30 -05:00
newObject := storedObject{}
2015-01-18 19:54:45 -05:00
if _, ok := io.Copy(&bytesBuffer, data); ok == nil {
2015-01-20 21:39:30 -05:00
newObject.metadata = ObjectMetadata{
Key: key,
SecCreated: time.Now().Unix(),
Size: len(bytesBuffer.Bytes()),
}
newObject.data = bytesBuffer.Bytes()
2015-01-18 19:54:45 -05:00
}
2015-01-21 03:50:23 -05:00
storage.objectdata[objectKey] = newObject
return nil
}
func (storage *Storage) StoreBucket(bucketName string) error {
if !isValidBucket(bucketName) {
return BucketNameInvalid{bucket: bucketName}
}
if _, ok := storage.bucketdata[bucketName]; ok == true {
return BucketExists{bucket: bucketName}
}
newBucket := storedBucket{}
newBucket.metadata = BucketMetadata{
Name: bucketName,
Created: time.Now().Unix(),
}
log.Println(bucketName)
storage.bucketdata[bucketName] = newBucket
2015-01-20 22:13:57 -05:00
return nil
}
2015-01-20 19:08:14 -05:00
func (storage *Storage) ListObjects(bucket, prefix string, count int) []ObjectMetadata {
2015-01-21 03:50:23 -05:00
// TODO prefix and count handling
2015-01-20 21:39:30 -05:00
var results []ObjectMetadata
2015-01-21 03:50:23 -05:00
for key, object := range storage.objectdata {
2015-01-20 21:39:30 -05:00
if strings.HasPrefix(key, bucket+":") {
results = append(results, object.metadata)
}
}
return results
2015-01-20 19:08:14 -05:00
}
2015-01-21 03:50:23 -05:00
func (storage *Storage) ListBuckets(prefix string) []BucketMetadata {
// TODO prefix handling
var results []BucketMetadata
for _, bucket := range storage.bucketdata {
results = append(results, bucket.metadata)
}
return results
}
func Start() (chan<- string, <-chan error, *Storage) {
ctrlChannel := make(chan string)
errorChannel := make(chan error)
go start(ctrlChannel, errorChannel)
2015-01-19 13:30:40 -05:00
return ctrlChannel, errorChannel, &Storage{
2015-01-21 03:50:23 -05:00
bucketdata: make(map[string]storedBucket),
objectdata: make(map[string]storedObject),
2015-01-19 13:30:40 -05:00
}
}
func start(ctrlChannel <-chan string, errorChannel chan<- error) {
close(errorChannel)
}