diff --git a/server/tree.go b/server/tree.go index e1d2b8b..274e5f4 100644 --- a/server/tree.go +++ b/server/tree.go @@ -1,8 +1,8 @@ package main import ( - "io" "io/ioutil" + "log" "os" "path" @@ -53,8 +53,9 @@ func (base Leaf) Merge(updated Leaf) Leaf { } type Tree struct { - root string - cachedRoot Branch + root string + cachedRoot Branch + cachedMetaRoot Branch } func NewTree(root string) Tree { @@ -64,11 +65,19 @@ func NewTree(root string) Tree { func (tree Tree) WithRoot(root string) Tree { tree.root = root tree.cachedRoot = Branch{} + tree.cachedMetaRoot = Branch{} return tree } func (tree Tree) GetRootMeta() (Branch, error) { - return tree.getRoot(NewID(""), false, false) + if !tree.cachedMetaRoot.IsZero() { + return tree.cachedMetaRoot, nil + } + got, err := tree.getRoot(NewID(""), false, false) + if err == nil { + tree.cachedMetaRoot = got + } + return got, err } func (tree Tree) GetRoot() (Branch, error) { @@ -93,7 +102,8 @@ func (tree Tree) getRoot(pid ID, withContent, withDeleted bool) (Branch, error) } for _, entry := range entries { if entry.Name() == "data.yaml" { - if b, err := peekFile(withContent, path.Join(tree.root, entry.Name())); err != nil { + log.Printf("reading %s", tree.root) + if b, err := peekLeaf(withContent, path.Join(tree.root, entry.Name())); err != nil { return Branch{}, err } else if err := yaml.Unmarshal(b, &m.Leaf); err != nil { return Branch{}, err @@ -116,17 +126,11 @@ func (tree Tree) getRoot(pid ID, withContent, withDeleted bool) (Branch, error) return m, nil } -func peekFile(all bool, path string) ([]byte, error) { - if !all { +func peekLeaf(all bool, path string) ([]byte, error) { + if all { return ioutil.ReadFile(path) } - f, err := os.Open(path) - if err != nil { - return nil, err - } - defer f.Close() - r := io.LimitReader(f, 1024) - return ioutil.ReadAll(r) + return ioutil.ReadFile(path) } func (tree Tree) toDir(id ID) string { @@ -159,6 +163,7 @@ func (tree Tree) Put(id ID, input Leaf) error { return err } tree.cachedRoot = Branch{} + tree.cachedMetaRoot = Branch{} return nil } @@ -180,6 +185,7 @@ func (tree Tree) Del(id ID) error { func (tree Tree) HardDel(id ID) error { os.RemoveAll(tree.toDir(id)) tree.cachedRoot = Branch{} + tree.cachedMetaRoot = Branch{} return nil }