grr cached root doesnt matter because server.tree called each time
parent
2781114863
commit
b076b6a9cf
|
|
@ -1,8 +1,8 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"os"
|
||||
"path"
|
||||
|
||||
|
|
@ -55,6 +55,7 @@ func (base Leaf) Merge(updated Leaf) Leaf {
|
|||
type Tree struct {
|
||||
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
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue