aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLaria Carolin Chabowski <laria@laria.me>2017-09-29 07:57:09 +0200
committerLaria Carolin Chabowski <laria@laria.me>2017-10-03 15:01:38 +0200
commitc04bbf9c82f216891ba44b8b5b621b8020963086 (patch)
treea2e0809ea5e4a0cf2dfd921b6b6c79e48a9d5de7
parent55053d116a79d681dc10d0e905371533e1a43299 (diff)
downloadpetrific-c04bbf9c82f216891ba44b8b5b621b8020963086.tar.gz
petrific-c04bbf9c82f216891ba44b8b5b621b8020963086.tar.bz2
petrific-c04bbf9c82f216891ba44b8b5b621b8020963086.zip
Restructure package storage
-rw-r--r--backup/backup_test.go5
-rw-r--r--backup/restore_test.go6
-rw-r--r--main.go22
-rw-r--r--storage/local/local.go (renamed from storage/local_storage.go)11
-rw-r--r--storage/memory/memory.go (renamed from storage/memory_storage.go)9
-rw-r--r--storage/registry/registry.go58
6 files changed, 75 insertions, 36 deletions
diff --git a/backup/backup_test.go b/backup/backup_test.go
index da14117..a7f7ff9 100644
--- a/backup/backup_test.go
+++ b/backup/backup_test.go
@@ -6,6 +6,7 @@ import (
"code.laria.me/petrific/fs"
"code.laria.me/petrific/objects"
"code.laria.me/petrific/storage"
+ "code.laria.me/petrific/storage/memory"
"testing"
)
@@ -27,7 +28,7 @@ func wantObject(
}
func TestWriteLargeFile(t *testing.T) {
- s := storage.NewMemoryStorage()
+ s := memory.NewMemoryStorage()
id, err := WriteFile(s, bytes.NewReader(content_largefile))
if err != nil {
@@ -61,7 +62,7 @@ func mkfile(t *testing.T, d fs.Dir, name string, exec bool, content []byte) {
}
func TestWriteDir(t *testing.T) {
- s := storage.NewMemoryStorage()
+ s := memory.NewMemoryStorage()
root := fs.NewMemoryFSRoot("root")
diff --git a/backup/restore_test.go b/backup/restore_test.go
index 9e6e525..db66e11 100644
--- a/backup/restore_test.go
+++ b/backup/restore_test.go
@@ -4,7 +4,7 @@ import (
"bytes"
"code.laria.me/petrific/fs"
"code.laria.me/petrific/objects"
- "code.laria.me/petrific/storage"
+ "code.laria.me/petrific/storage/memory"
"io"
"testing"
)
@@ -71,7 +71,7 @@ func wantDir(n int, fx func(*testing.T, fs.Dir)) func(*testing.T, fs.File) {
}
func TestRestoreDir(t *testing.T) {
- s := storage.NewMemoryStorage()
+ s := memory.NewMemoryStorage()
s.Set(objid_emptyfile, objects.OTFile, obj_emptyfile)
s.Set(objid_fooblob, objects.OTBlob, obj_fooblob)
@@ -109,7 +109,7 @@ func TestRestoreDir(t *testing.T) {
}
func TestRestoreLargeFile(t *testing.T) {
- s := storage.NewMemoryStorage()
+ s := memory.NewMemoryStorage()
s.Set(objid_largefile_blob0, objects.OTBlob, obj_largefile_blob0)
s.Set(objid_largefile_blob1, objects.OTBlob, obj_largefile_blob1)
s.Set(objid_largefile, objects.OTFile, obj_largefile)
diff --git a/main.go b/main.go
index 6b2b55e..11202f3 100644
--- a/main.go
+++ b/main.go
@@ -97,27 +97,9 @@ func loadConfig() bool {
storageName = conf.DefaultStorage
}
- storageOptions, ok := conf.Storage[storageName]
- if !ok {
- fmt.Fprintf(os.Stderr, "Storage %s not found\n", storageName)
- return false
- }
-
- var method string
- if err := storageOptions.Get("method", &method); err != nil {
- fmt.Fprintf(os.Stderr, "Failed setting up storage %s: %s\n", storageName, err)
- return false
- }
-
- st, ok := registry.StorageTypes[method]
- if !ok {
- fmt.Fprintf(os.Stderr, "Failed setting up storage %s: Method %s unknown", storageName, method)
- return false
- }
-
- s, err := st(conf, storageName)
+ s, err := registry.LoadStorage(conf, storageName)
if err != nil {
- fmt.Fprintf(os.Stderr, "Failed setting up storage %s: %s\n", storageName, err)
+ fmt.Fprintln(os.Stderr, err)
return false
}
diff --git a/storage/local_storage.go b/storage/local/local.go
index c080564..4869bb2 100644
--- a/storage/local_storage.go
+++ b/storage/local/local.go
@@ -1,9 +1,10 @@
-package storage
+package local
import (
"bytes"
"code.laria.me/petrific/config"
"code.laria.me/petrific/objects"
+ "code.laria.me/petrific/storage"
"encoding/hex"
"fmt"
"io"
@@ -21,10 +22,10 @@ func objectDir(id objects.ObjectId) string {
type LocalStorage struct {
Path string
- index Index
+ index storage.Index
}
-func LocalStorageFromConfig(conf config.Config, name string) (Storage, error) {
+func LocalStorageFromConfig(conf config.Config, name string) (storage.Storage, error) {
var path string
if err := conf.Storage[name].Get("path", &path); err != nil {
return nil, err
@@ -35,7 +36,7 @@ func LocalStorageFromConfig(conf config.Config, name string) (Storage, error) {
func OpenLocalStorage(path string) (l LocalStorage, err error) {
l.Path = path
- l.index = NewIndex()
+ l.index = storage.NewIndex()
if fi, err := os.Stat(path); os.IsNotExist(err) {
if err := os.MkdirAll(path, 0755); err != nil {
@@ -70,7 +71,7 @@ func objectPath(id objects.ObjectId) string {
func (l LocalStorage) Get(id objects.ObjectId) ([]byte, error) {
f, err := os.Open(joinPath(l.Path, objectPath(id)))
if os.IsNotExist(err) {
- return []byte{}, ObjectNotFound
+ return []byte{}, storage.ObjectNotFound
} else if err != nil {
return []byte{}, err
}
diff --git a/storage/memory_storage.go b/storage/memory/memory.go
index b9ed54a..456049b 100644
--- a/storage/memory_storage.go
+++ b/storage/memory/memory.go
@@ -1,8 +1,9 @@
-package storage
+package memory
import (
"code.laria.me/petrific/config"
"code.laria.me/petrific/objects"
+ "code.laria.me/petrific/storage"
)
type MemoryStorage struct {
@@ -10,21 +11,21 @@ type MemoryStorage struct {
bytype map[objects.ObjectType][]objects.ObjectId
}
-func NewMemoryStorage() Storage {
+func NewMemoryStorage() storage.Storage {
return MemoryStorage{
objects: make(map[string][]byte),
bytype: make(map[objects.ObjectType][]objects.ObjectId),
}
}
-func MemoryStorageFromConfig(conf config.Config, name string) (Storage, error) {
+func MemoryStorageFromConfig(conf config.Config, name string) (storage.Storage, error) {
return NewMemoryStorage(), nil
}
func (ms MemoryStorage) Get(id objects.ObjectId) ([]byte, error) {
b, ok := ms.objects[id.String()]
if !ok {
- return nil, ObjectNotFound
+ return nil, storage.ObjectNotFound
}
return b, nil
}
diff --git a/storage/registry/registry.go b/storage/registry/registry.go
index dec7c32..ad66c6a 100644
--- a/storage/registry/registry.go
+++ b/storage/registry/registry.go
@@ -1,13 +1,67 @@
package registry
import (
+ "code.laria.me/petrific/config"
"code.laria.me/petrific/storage"
"code.laria.me/petrific/storage/cloud"
+ "code.laria.me/petrific/storage/local"
+ "code.laria.me/petrific/storage/memory"
+ "errors"
+ "fmt"
)
// List af all available storage types
var StorageTypes = map[string]storage.CreateStorageFromConfig{
- "local": storage.LocalStorageFromConfig,
- "memory": storage.MemoryStorageFromConfig,
+ "local": local.LocalStorageFromConfig,
+ "memory": memory.MemoryStorageFromConfig,
"openstack-swift": cloud.SwiftStorageCreator(),
}
+
+var notFoundErr = errors.New("Storage not found")
+
+type unknownMethodErr string
+
+func (method unknownMethodErr) Error() string {
+ return fmt.Sprintf("Method %s unknown", string(method))
+}
+
+type storageConfErr struct {
+ name string
+ err error
+}
+
+func (e storageConfErr) Error() string {
+ return fmt.Sprintf("Failed setting up storage %s: %s", e.name, e.err.Error())
+}
+
+func loadStorage(conf config.Config, storageName string) (storage.Storage, error) {
+ storageOptions, ok := conf.Storage[storageName]
+ if !ok {
+ return nil, notFoundErr
+ }
+
+ var method string
+ if err := storageOptions.Get("method", &method); err != nil {
+ return nil, err
+ }
+
+ st, ok := StorageTypes[method]
+ if !ok {
+ return nil, unknownMethodErr(method)
+ }
+
+ s, err := st(conf, storageName)
+ if err != nil {
+ return nil, err
+ }
+
+ return s, nil
+}
+
+func LoadStorage(conf config.Config, storageName string) (storage.Storage, error) {
+ s, err := loadStorage(conf, storageName)
+ if err != nil {
+ return nil, storageConfErr{storageName, err}
+ }
+ return s, nil
+}