2015-03-14 14:34:26 -04:00
|
|
|
/*
|
2015-03-19 17:35:50 -04:00
|
|
|
* Minimalist Object Storage, (C) 2015 Minio, Inc.
|
2015-03-14 14:34:26 -04: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.
|
|
|
|
*/
|
|
|
|
|
2015-03-23 23:40:21 -04:00
|
|
|
package donut
|
2015-03-14 14:34:26 -04:00
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
2015-03-22 22:55:15 -04:00
|
|
|
"io"
|
2015-03-23 23:06:15 -04:00
|
|
|
"sort"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
"time"
|
2015-03-23 23:40:21 -04:00
|
|
|
|
2015-03-25 03:25:27 -04:00
|
|
|
"github.com/minio-io/iodine"
|
2015-03-23 23:40:21 -04:00
|
|
|
"github.com/minio-io/minio/pkg/drivers"
|
|
|
|
"github.com/minio-io/minio/pkg/storage/donut"
|
2015-03-28 15:43:26 -04:00
|
|
|
"github.com/minio-io/minio/pkg/utils/log"
|
2015-03-14 14:34:26 -04:00
|
|
|
)
|
|
|
|
|
2015-03-23 23:40:21 -04:00
|
|
|
// donutDriver - creates a new single disk drivers driver using donut
|
|
|
|
type donutDriver struct {
|
2015-03-23 23:06:15 -04:00
|
|
|
donut donut.Donut
|
|
|
|
}
|
2015-03-14 14:34:26 -04:00
|
|
|
|
2015-03-16 21:20:01 -04:00
|
|
|
const (
|
|
|
|
blockSize = 10 * 1024 * 1024
|
|
|
|
)
|
|
|
|
|
2015-03-14 14:34:26 -04:00
|
|
|
// Start a single disk subsystem
|
2015-03-23 23:40:21 -04:00
|
|
|
func Start(path string) (chan<- string, <-chan error, drivers.Driver) {
|
2015-03-14 14:34:26 -04:00
|
|
|
ctrlChannel := make(chan string)
|
|
|
|
errorChannel := make(chan error)
|
2015-03-23 23:40:21 -04:00
|
|
|
s := new(donutDriver)
|
2015-03-23 23:06:15 -04:00
|
|
|
|
|
|
|
// TODO donut driver should be passed in as Start param and driven by config
|
2015-03-25 22:12:39 -04:00
|
|
|
var err error
|
2015-03-25 03:25:27 -04:00
|
|
|
s.donut, err = donut.NewDonut(path)
|
2015-03-26 18:55:06 -04:00
|
|
|
err = iodine.New(err, map[string]string{"path": path})
|
2015-03-25 03:25:27 -04:00
|
|
|
if err != nil {
|
2015-03-28 18:58:48 -04:00
|
|
|
log.Error.Println(err)
|
2015-03-25 03:25:27 -04:00
|
|
|
}
|
2015-03-23 23:06:15 -04:00
|
|
|
|
2015-03-14 14:34:26 -04:00
|
|
|
go start(ctrlChannel, errorChannel, s)
|
|
|
|
return ctrlChannel, errorChannel, s
|
|
|
|
}
|
|
|
|
|
2015-03-23 23:40:21 -04:00
|
|
|
func start(ctrlChannel <-chan string, errorChannel chan<- error, s *donutDriver) {
|
2015-03-14 14:34:26 -04:00
|
|
|
close(errorChannel)
|
|
|
|
}
|
|
|
|
|
|
|
|
// ListBuckets returns a list of buckets
|
2015-03-23 23:40:21 -04:00
|
|
|
func (d donutDriver) ListBuckets() (results []drivers.BucketMetadata, err error) {
|
|
|
|
buckets, err := d.donut.ListBuckets()
|
2015-03-23 23:06:15 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
for _, bucket := range buckets {
|
2015-03-23 23:40:21 -04:00
|
|
|
result := drivers.BucketMetadata{
|
2015-03-23 23:06:15 -04:00
|
|
|
Name: bucket,
|
|
|
|
// TODO Add real created date
|
|
|
|
Created: time.Now(),
|
|
|
|
}
|
|
|
|
results = append(results, result)
|
|
|
|
}
|
|
|
|
return results, nil
|
2015-03-14 14:34:26 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// CreateBucket creates a new bucket
|
2015-03-23 23:40:21 -04:00
|
|
|
func (d donutDriver) CreateBucket(bucket string) error {
|
|
|
|
return d.donut.CreateBucket(bucket)
|
2015-03-17 02:41:16 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetBucketMetadata retrieves an bucket's metadata
|
2015-03-23 23:40:21 -04:00
|
|
|
func (d donutDriver) GetBucketMetadata(bucket string) (drivers.BucketMetadata, error) {
|
|
|
|
return drivers.BucketMetadata{}, errors.New("Not Implemented")
|
2015-03-14 14:34:26 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// CreateBucketPolicy sets a bucket's access policy
|
2015-03-23 23:40:21 -04:00
|
|
|
func (d donutDriver) CreateBucketPolicy(bucket string, p drivers.BucketPolicy) error {
|
2015-03-14 14:34:26 -04:00
|
|
|
return errors.New("Not Implemented")
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetBucketPolicy returns a bucket's access policy
|
2015-03-23 23:40:21 -04:00
|
|
|
func (d donutDriver) GetBucketPolicy(bucket string) (drivers.BucketPolicy, error) {
|
|
|
|
return drivers.BucketPolicy{}, errors.New("Not Implemented")
|
2015-03-14 14:34:26 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetObject retrieves an object and writes it to a writer
|
2015-03-23 23:40:21 -04:00
|
|
|
func (d donutDriver) GetObject(target io.Writer, bucket, key string) (int64, error) {
|
|
|
|
reader, err := d.donut.GetObjectReader(bucket, key)
|
2015-03-23 23:06:15 -04:00
|
|
|
if err != nil {
|
2015-03-23 23:40:21 -04:00
|
|
|
return 0, drivers.ObjectNotFound{
|
2015-03-23 23:06:15 -04:00
|
|
|
Bucket: bucket,
|
|
|
|
Object: key,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return io.Copy(target, reader)
|
2015-03-14 14:34:26 -04:00
|
|
|
}
|
|
|
|
|
2015-03-23 23:40:21 -04:00
|
|
|
// GetPartialObject retrieves an object range and writes it to a writer
|
|
|
|
func (d donutDriver) GetPartialObject(w io.Writer, bucket, object string, start, length int64) (int64, error) {
|
2015-03-14 14:34:26 -04:00
|
|
|
return 0, errors.New("Not Implemented")
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetObjectMetadata retrieves an object's metadata
|
2015-03-23 23:40:21 -04:00
|
|
|
func (d donutDriver) GetObjectMetadata(bucket, key string, prefix string) (drivers.ObjectMetadata, error) {
|
|
|
|
metadata, err := d.donut.GetObjectMetadata(bucket, key)
|
2015-03-23 23:06:15 -04:00
|
|
|
created, err := time.Parse(time.RFC3339Nano, metadata["sys.created"])
|
|
|
|
if err != nil {
|
2015-03-23 23:40:21 -04:00
|
|
|
return drivers.ObjectMetadata{}, err
|
2015-03-23 23:06:15 -04:00
|
|
|
}
|
|
|
|
size, err := strconv.ParseInt(metadata["sys.size"], 10, 64)
|
|
|
|
if err != nil {
|
2015-03-23 23:40:21 -04:00
|
|
|
return drivers.ObjectMetadata{}, err
|
2015-03-23 23:06:15 -04:00
|
|
|
}
|
2015-03-23 23:40:21 -04:00
|
|
|
objectMetadata := drivers.ObjectMetadata{
|
2015-03-23 23:06:15 -04:00
|
|
|
Bucket: bucket,
|
|
|
|
Key: key,
|
|
|
|
|
|
|
|
ContentType: metadata["contentType"],
|
|
|
|
Created: created,
|
|
|
|
Md5: metadata["sys.md5"],
|
|
|
|
Size: size,
|
|
|
|
}
|
|
|
|
return objectMetadata, nil
|
2015-03-14 14:34:26 -04:00
|
|
|
}
|
|
|
|
|
2015-03-23 23:40:21 -04:00
|
|
|
// ListObjects - returns list of objects
|
|
|
|
func (d donutDriver) ListObjects(bucket string, resources drivers.BucketResourcesMetadata) ([]drivers.ObjectMetadata, drivers.BucketResourcesMetadata, error) {
|
2015-03-23 23:06:15 -04:00
|
|
|
// TODO Fix IsPrefixSet && IsDelimiterSet and use them
|
2015-03-23 23:40:21 -04:00
|
|
|
objects, err := d.donut.ListObjects(bucket)
|
2015-03-23 23:06:15 -04:00
|
|
|
if err != nil {
|
2015-03-23 23:40:21 -04:00
|
|
|
return nil, drivers.BucketResourcesMetadata{}, err
|
2015-03-23 23:06:15 -04:00
|
|
|
}
|
|
|
|
sort.Strings(objects)
|
|
|
|
if resources.Prefix != "" {
|
|
|
|
objects = filterPrefix(objects, resources.Prefix)
|
|
|
|
objects = removePrefix(objects, resources.Prefix)
|
|
|
|
}
|
|
|
|
if resources.Maxkeys <= 0 || resources.Maxkeys > 1000 {
|
|
|
|
resources.Maxkeys = 1000
|
|
|
|
}
|
|
|
|
|
|
|
|
var actualObjects []string
|
|
|
|
var commonPrefixes []string
|
|
|
|
if strings.TrimSpace(resources.Delimiter) != "" {
|
|
|
|
actualObjects = filterDelimited(objects, resources.Delimiter)
|
|
|
|
commonPrefixes = filterNotDelimited(objects, resources.Delimiter)
|
|
|
|
commonPrefixes = extractDir(commonPrefixes, resources.Delimiter)
|
|
|
|
commonPrefixes = uniqueObjects(commonPrefixes)
|
|
|
|
resources.CommonPrefixes = commonPrefixes
|
|
|
|
} else {
|
|
|
|
actualObjects = objects
|
|
|
|
}
|
|
|
|
|
2015-03-23 23:40:21 -04:00
|
|
|
var results []drivers.ObjectMetadata
|
2015-03-23 23:06:15 -04:00
|
|
|
for _, object := range actualObjects {
|
|
|
|
if len(results) >= resources.Maxkeys {
|
|
|
|
resources.IsTruncated = true
|
|
|
|
break
|
|
|
|
}
|
2015-03-23 23:40:21 -04:00
|
|
|
metadata, err := d.GetObjectMetadata(bucket, resources.Prefix+object, "")
|
2015-03-23 23:06:15 -04:00
|
|
|
if err != nil {
|
2015-03-23 23:40:21 -04:00
|
|
|
return nil, drivers.BucketResourcesMetadata{}, err
|
2015-03-23 23:06:15 -04:00
|
|
|
}
|
|
|
|
results = append(results, metadata)
|
|
|
|
}
|
|
|
|
return results, resources, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func filterPrefix(objects []string, prefix string) []string {
|
|
|
|
var results []string
|
|
|
|
for _, object := range objects {
|
|
|
|
if strings.HasPrefix(object, prefix) {
|
|
|
|
results = append(results, object)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return results
|
|
|
|
}
|
|
|
|
|
|
|
|
func removePrefix(objects []string, prefix string) []string {
|
|
|
|
var results []string
|
|
|
|
for _, object := range objects {
|
|
|
|
results = append(results, strings.TrimPrefix(object, prefix))
|
|
|
|
}
|
|
|
|
return results
|
|
|
|
}
|
|
|
|
|
|
|
|
func filterDelimited(objects []string, delim string) []string {
|
|
|
|
var results []string
|
|
|
|
for _, object := range objects {
|
|
|
|
if !strings.Contains(object, delim) {
|
|
|
|
results = append(results, object)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return results
|
|
|
|
}
|
|
|
|
func filterNotDelimited(objects []string, delim string) []string {
|
|
|
|
var results []string
|
|
|
|
for _, object := range objects {
|
|
|
|
if strings.Contains(object, delim) {
|
|
|
|
results = append(results, object)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return results
|
|
|
|
}
|
|
|
|
|
|
|
|
func extractDir(objects []string, delim string) []string {
|
|
|
|
var results []string
|
|
|
|
for _, object := range objects {
|
|
|
|
parts := strings.Split(object, delim)
|
|
|
|
results = append(results, parts[0]+"/")
|
|
|
|
}
|
|
|
|
return results
|
|
|
|
}
|
|
|
|
|
|
|
|
func uniqueObjects(objects []string) []string {
|
|
|
|
objectMap := make(map[string]string)
|
|
|
|
for _, v := range objects {
|
|
|
|
objectMap[v] = v
|
|
|
|
}
|
|
|
|
var results []string
|
|
|
|
for k := range objectMap {
|
|
|
|
results = append(results, k)
|
|
|
|
}
|
|
|
|
sort.Strings(results)
|
|
|
|
return results
|
2015-03-14 14:34:26 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// CreateObject creates a new object
|
2015-03-23 23:40:21 -04:00
|
|
|
func (d donutDriver) CreateObject(bucketKey, objectKey, contentType, expectedMd5sum string, reader io.Reader) error {
|
|
|
|
writer, err := d.donut.GetObjectWriter(bucketKey, objectKey)
|
2015-03-23 23:06:15 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if _, err := io.Copy(writer, reader); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if contentType == "" {
|
|
|
|
contentType = "application/octet-stream"
|
|
|
|
}
|
|
|
|
contentType = strings.TrimSpace(contentType)
|
|
|
|
metadata := make(map[string]string)
|
|
|
|
metadata["bucket"] = bucketKey
|
|
|
|
metadata["object"] = objectKey
|
|
|
|
metadata["contentType"] = contentType
|
|
|
|
if err = writer.SetMetadata(metadata); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return writer.Close()
|
2015-03-16 20:40:59 -04:00
|
|
|
}
|