aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--storage/index.go71
-rw-r--r--storage/local_storage.go44
2 files changed, 78 insertions, 37 deletions
diff --git a/storage/index.go b/storage/index.go
new file mode 100644
index 0000000..f34abab
--- /dev/null
+++ b/storage/index.go
@@ -0,0 +1,71 @@
+package storage
+
+import (
+ "bufio"
+ "code.laria.me/petrific/objects"
+ "fmt"
+ "io"
+ "strings"
+)
+
+type Index map[objects.ObjectType]map[string]struct{}
+
+func NewIndex() Index {
+ idx := make(Index)
+ idx.Init()
+ return idx
+}
+
+func (idx Index) Init() {
+ for _, t := range objects.AllObjectTypes {
+ idx[t] = make(map[string]struct{})
+ }
+}
+
+func (idx Index) Set(id objects.ObjectId, typ objects.ObjectType) {
+ idx[typ][id.String()] = struct{}{}
+}
+
+func (idx Index) List(typ objects.ObjectType) []objects.ObjectId {
+ ids := make([]objects.ObjectId, 0, len(idx[typ]))
+ for id := range idx[typ] {
+ ids = append(ids, objects.MustParseObjectId(id))
+ }
+
+ return ids
+}
+
+func (idx Index) Save(w io.Writer) error {
+ for t, objs := range idx {
+ for id := range objs {
+ if _, err := fmt.Fprintf(w, "%s %s\n", t, id); err != nil {
+ return err
+ }
+ }
+ }
+ return nil
+}
+
+func (idx Index) Load(r io.Reader) error {
+ scan := bufio.NewScanner(r)
+ for scan.Scan() {
+ line := scan.Text()
+
+ parts := strings.SplitN(strings.TrimSpace(line), " ", 2)
+ if len(parts) == 2 {
+ id, err := objects.ParseObjectId(parts[1])
+ if err != nil {
+ return err
+ }
+
+ typ := objects.ObjectType(parts[0])
+
+ if _, ok := idx[typ]; !ok {
+ return fmt.Errorf("Failed loading index: Unknown ObjectType %s", typ)
+ }
+
+ idx[typ][id.String()] = struct{}{}
+ }
+ }
+ return scan.Err()
+}
diff --git a/storage/local_storage.go b/storage/local_storage.go
index 18a62f6..c080564 100644
--- a/storage/local_storage.go
+++ b/storage/local_storage.go
@@ -1,7 +1,6 @@
package storage
import (
- "bufio"
"bytes"
"code.laria.me/petrific/config"
"code.laria.me/petrific/objects"
@@ -22,7 +21,7 @@ func objectDir(id objects.ObjectId) string {
type LocalStorage struct {
Path string
- index map[objects.ObjectType]map[string]struct{}
+ index Index
}
func LocalStorageFromConfig(conf config.Config, name string) (Storage, error) {
@@ -36,10 +35,7 @@ func LocalStorageFromConfig(conf config.Config, name string) (Storage, error) {
func OpenLocalStorage(path string) (l LocalStorage, err error) {
l.Path = path
- l.index = make(map[objects.ObjectType]map[string]struct{})
- for _, t := range objects.AllObjectTypes {
- l.index[t] = make(map[string]struct{})
- }
+ l.index = NewIndex()
if fi, err := os.Stat(path); os.IsNotExist(err) {
if err := os.MkdirAll(path, 0755); err != nil {
@@ -61,22 +57,7 @@ func OpenLocalStorage(path string) (l LocalStorage, err error) {
if err == nil {
defer f.Close()
-
- scan := bufio.NewScanner(f)
- for scan.Scan() {
- line := scan.Text()
-
- parts := strings.SplitN(strings.TrimSpace(line), " ", 2)
- if len(parts) == 2 {
- id, err := objects.ParseObjectId(parts[1])
- if err != nil {
- return l, err
- }
-
- l.index[objects.ObjectType(parts[0])][id.String()] = struct{}{}
- }
- }
- err = scan.Err()
+ err = l.index.Load(f)
}
return
@@ -130,31 +111,20 @@ func (l LocalStorage) Set(id objects.ObjectId, typ objects.ObjectType, raw []byt
defer f.Close()
_, err = f.Write(raw)
- l.index[typ][id.String()] = struct{}{}
+ l.index.Set(id, typ)
return err
}
func (l LocalStorage) List(typ objects.ObjectType) ([]objects.ObjectId, error) {
- ids := make([]objects.ObjectId, 0, len(l.index[typ]))
- for id := range l.index[typ] {
- ids = append(ids, objects.MustParseObjectId(id))
- }
- return ids, nil
+ return l.index.List(typ), nil
}
-func (l LocalStorage) Close() (outerr error) {
+func (l LocalStorage) Close() error {
f, err := os.Create(joinPath(l.Path, "index"))
if err != nil {
return err
}
defer f.Close()
- for t, objs := range l.index {
- for id := range objs {
- if _, err := fmt.Fprintf(f, "%s %s\n", t, id); err != nil {
- return err
- }
- }
- }
- return nil
+ return l.index.Save(f)
}