minio/pkg/storage/storage.go

60 lines
1.4 KiB
Go
Raw Normal View History

package storage
import (
"bytes"
"errors"
"io"
)
2015-01-18 19:54:45 -05:00
type Storage struct {
data map[string][]byte
}
type GenericError struct {
bucket string
path string
}
type ObjectNotFound GenericError
func (self ObjectNotFound) Error() string {
return "Not Found: " + self.bucket + "#" + self.path
}
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
if val, ok := storage.data[key]; ok {
objectBuffer := bytes.NewBuffer(val)
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
}
}
func (storage *Storage) StoreObject(bucket string, object string, data io.Reader) {
key := bucket + ":" + object
var bytesBuffer bytes.Buffer
if _, ok := io.Copy(&bytesBuffer, data); ok == nil {
storage.data[key] = bytesBuffer.Bytes()
}
}
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{
data: make(map[string][]byte),
}
}
func start(ctrlChannel <-chan string, errorChannel chan<- error) {
errorChannel <- errors.New("STORAGE MSG")
errorChannel <- errors.New("STORAGE MSG")
errorChannel <- errors.New("STORAGE MSG")
close(errorChannel)
}