2014-11-29 17:42:22 -05:00
|
|
|
package storage
|
2015-01-18 16:31:22 -05:00
|
|
|
|
2015-01-18 19:10:48 -05:00
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"errors"
|
|
|
|
"io"
|
|
|
|
)
|
2015-01-18 16:31:22 -05:00
|
|
|
|
2015-01-18 19:54:45 -05:00
|
|
|
type Storage struct {
|
|
|
|
data map[string][]byte
|
|
|
|
}
|
2015-01-18 19:10:48 -05:00
|
|
|
|
2015-01-19 20:39:20 -05:00
|
|
|
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) {
|
2015-01-18 19:10:48 -05:00
|
|
|
// 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)
|
2015-01-19 20:39:20 -05:00
|
|
|
written, err := io.Copy(w, objectBuffer)
|
|
|
|
return written, err
|
2015-01-18 19:54:45 -05:00
|
|
|
} else {
|
2015-01-19 20:39:20 -05:00
|
|
|
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()
|
|
|
|
}
|
2015-01-18 19:10:48 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func Start() (chan<- string, <-chan error, *Storage) {
|
2015-01-18 16:31:22 -05:00
|
|
|
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),
|
|
|
|
}
|
2015-01-18 16:31:22 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
}
|