notea-de-me/server/tree.go

236 lines
4.8 KiB
Go

package main
import (
"encoding/json"
"io/ioutil"
"os"
"path"
yaml "gopkg.in/yaml.v2"
)
type Branch struct {
Leaf Leaf
Branches map[ID]Branch
}
func (branch Branch) IsZero() bool {
return branch.Leaf == (Leaf{}) && len(branch.Branches) == 0
}
func (branch Branch) ForEach(foo func(ID, Leaf) error) error {
return branch.forEach(NewID(""), foo)
}
func (branch Branch) forEach(preid ID, foo func(ID, Leaf) error) error {
if err := foo(preid, branch.Leaf); err != nil {
return err
}
for id, child := range branch.Branches {
if err := child.forEach(id, foo); err != nil {
return err
}
}
return nil
}
type Leaf struct {
Meta struct {
Title string
Deleted bool
}
Content string
}
func (base Leaf) Merge(updated Leaf) Leaf {
if updated.Meta.Title != "" {
base.Meta.Title = updated.Meta.Title
}
if base.Meta.Title == "" {
base.Meta.Title = "Untitled"
}
base.Meta.Deleted = updated.Meta.Deleted
base.Content = updated.Content
return base
}
type Tree struct {
root string
}
func NewTree(root string) Tree {
return Tree{root: root}
}
func (tree Tree) WithRoot(root string) Tree {
tree.root = root
return tree
}
func (tree Tree) GetRootMeta() (Branch, error) {
if meta, ok := tree.getCachedRootMeta(); ok {
return meta, nil
}
got, err := tree.getRoot(NewID(""), false, false)
if err != nil {
return Branch{}, err
}
tree.cacheRootMeta(got)
return got, err
}
func (tree Tree) GetRoot() (Branch, error) {
if root, ok := tree.getCachedRoot(); ok {
return root, nil
}
got, err := tree.getRoot(NewID(""), true, false)
if err != nil {
return Branch{}, err
}
tree.cacheRoot(got)
return got, err
}
func (tree Tree) getCachedRoot() (Branch, bool) {
return tree.getCachedFrom("root.json")
}
func (tree Tree) getCachedRootMeta() (Branch, bool) {
return tree.getCachedFrom("root_meta.json")
}
func (tree Tree) getCachedFrom(name string) (Branch, bool) {
b, err := ioutil.ReadFile(path.Join(tree.root, name))
if err != nil {
return Branch{}, false
}
var branch Branch
err = json.Unmarshal(b, &branch)
return branch, err == nil
}
func (tree Tree) cacheRoot(branch Branch) {
tree.cacheRootFrom("root.json", branch)
}
func (tree Tree) cacheRootMeta(branch Branch) {
tree.cacheRootFrom("root_meta.json", branch)
}
func (tree Tree) cacheRootFrom(name string, branch Branch) {
b, err := json.Marshal(branch)
if err != nil {
return
}
ensureAndWrite(path.Join(tree.root, name), b)
}
func (tree Tree) cacheClear() {
os.Remove(path.Join(path.Join(tree.root, "root.json")))
os.Remove(path.Join(path.Join(tree.root, "root_meta.json")))
}
func (tree Tree) getRoot(pid ID, withContent, withDeleted bool) (Branch, error) {
m := Branch{Branches: map[ID]Branch{}}
entries, err := os.ReadDir(tree.root)
if os.IsNotExist(err) {
return m, nil
}
if err != nil {
return Branch{}, err
}
for _, entry := range entries {
if entry.Name() == "data.yaml" {
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
}
if !withContent {
m.Leaf.Content = ""
}
if m.Leaf.Meta.Deleted && !withDeleted {
return m, nil
}
} else if entry.IsDir() {
subtree := tree.WithRoot(path.Join(tree.root, entry.Name()))
if branch, err := subtree.getRoot(pid.Push(entry.Name()), withContent, withDeleted); err != nil {
return Branch{}, err
} else if !branch.IsZero() && (!branch.Leaf.Meta.Deleted || withDeleted) {
m.Branches[pid.Push(entry.Name())] = branch
}
}
}
return m, nil
}
func peekLeaf(all bool, path string) ([]byte, error) {
return ioutil.ReadFile(path)
}
func (tree Tree) toDir(id ID) string {
return path.Dir(tree.toData(id))
}
func (tree Tree) toData(id ID) string {
return path.Join(tree.root, string(id), "data.yaml")
}
func (tree Tree) Put(id ID, input Leaf) error {
tree.cacheClear()
if _, err := os.Stat(tree.toData(id)); os.IsNotExist(err) {
b, err := yaml.Marshal(Leaf{})
if err != nil {
return err
}
if err := ensureAndWrite(tree.toData(id), b); err != nil {
return err
}
}
old, err := tree.Get(id)
if err != nil {
return err
}
b, err := yaml.Marshal(old.Merge(input))
if err != nil {
return err
}
if err := ensureAndWrite(tree.toData(id), b); err != nil {
return err
}
return nil
}
func (tree Tree) Del(id ID) error {
tree.cacheClear()
got, err := tree.Get(id)
if os.IsNotExist(err) {
return nil
}
if err != nil {
return err
}
if got.Meta.Deleted {
return nil
}
got.Meta.Deleted = true
return tree.Put(id, got)
}
func (tree Tree) HardDel(id ID) error {
tree.cacheClear()
os.RemoveAll(tree.toDir(id))
return nil
}
func (tree Tree) Get(id ID) (Leaf, error) {
f, err := os.Open(tree.toData(id))
if err != nil {
return Leaf{}, err
}
defer f.Close()
var got Leaf
err = yaml.NewDecoder(f).Decode(&got)
return got, err
}