aboutsummaryrefslogtreecommitdiff
path: root/storage/memory
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 /storage/memory
parent55053d116a79d681dc10d0e905371533e1a43299 (diff)
downloadpetrific-c04bbf9c82f216891ba44b8b5b621b8020963086.tar.gz
petrific-c04bbf9c82f216891ba44b8b5b621b8020963086.tar.bz2
petrific-c04bbf9c82f216891ba44b8b5b621b8020963086.zip
Restructure package storage
Diffstat (limited to 'storage/memory')
-rw-r--r--storage/memory/memory.go51
1 files changed, 51 insertions, 0 deletions
diff --git a/storage/memory/memory.go b/storage/memory/memory.go
new file mode 100644
index 0000000..456049b
--- /dev/null
+++ b/storage/memory/memory.go
@@ -0,0 +1,51 @@
+package memory
+
+import (
+ "code.laria.me/petrific/config"
+ "code.laria.me/petrific/objects"
+ "code.laria.me/petrific/storage"
+)
+
+type MemoryStorage struct {
+ objects map[string][]byte
+ bytype map[objects.ObjectType][]objects.ObjectId
+}
+
+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.Storage, error) {
+ return NewMemoryStorage(), nil
+}
+
+func (ms MemoryStorage) Get(id objects.ObjectId) ([]byte, error) {
+ b, ok := ms.objects[id.String()]
+ if !ok {
+ return nil, storage.ObjectNotFound
+ }
+ return b, nil
+}
+
+func (ms MemoryStorage) Has(id objects.ObjectId) (bool, error) {
+ _, ok := ms.objects[id.String()]
+ return ok, nil
+}
+
+func (ms MemoryStorage) Set(id objects.ObjectId, typ objects.ObjectType, raw []byte) error {
+ ms.objects[id.String()] = raw
+ ms.bytype[typ] = append(ms.bytype[typ], id)
+
+ return nil
+}
+
+func (ms MemoryStorage) List(typ objects.ObjectType) ([]objects.ObjectId, error) {
+ return ms.bytype[typ], nil
+}
+
+func (MemoryStorage) Close() error {
+ return nil
+}