aboutsummaryrefslogtreecommitdiff
path: root/storage/storage.go
diff options
context:
space:
mode:
Diffstat (limited to 'storage/storage.go')
-rw-r--r--storage/storage.go37
1 files changed, 37 insertions, 0 deletions
diff --git a/storage/storage.go b/storage/storage.go
new file mode 100644
index 0000000..424661c
--- /dev/null
+++ b/storage/storage.go
@@ -0,0 +1,37 @@
+package storage
+
+import (
+ "bytes"
+ "code.laria.me/petrific/objects"
+ "errors"
+)
+
+var (
+ ObjectNotFound = errors.New("Object not found")
+)
+
+type Storage interface {
+ Get(id objects.ObjectId) ([]byte, error)
+ Has(id objects.ObjectId) (bool, error)
+ Set(id objects.ObjectId, typ objects.ObjectType, raw []byte) error
+ List(typ objects.ObjectType) ([]objects.ObjectId, error)
+}
+
+func SetObject(s Storage, o objects.RawObject) (id objects.ObjectId, err error) {
+ buf := new(bytes.Buffer)
+
+ id, err = o.SerializeAndId(buf, objects.OIdAlgoDefault)
+ if err != nil {
+ return
+ }
+
+ ok, err := s.Has(id)
+ if err != nil {
+ return
+ }
+
+ if !ok {
+ err = s.Set(id, o.Type, buf.Bytes())
+ }
+ return
+}