2
2
mirror of https://github.com/octoleo/restic.git synced 2024-05-29 15:10:49 +00:00
restic/src/restic/tree.go

105 lines
2.1 KiB
Go
Raw Normal View History

2014-12-05 20:45:49 +00:00
package restic
2014-04-21 21:25:31 +00:00
import (
2014-08-11 20:47:24 +00:00
"fmt"
2014-11-30 21:34:21 +00:00
"sort"
2014-09-18 20:41:24 +00:00
"github.com/pkg/errors"
"restic/debug"
2014-04-21 21:25:31 +00:00
)
type Tree struct {
Nodes []*Node `json:"nodes"`
}
2014-04-21 21:25:31 +00:00
2014-11-30 21:34:21 +00:00
var (
ErrNodeNotFound = errors.New("named node not found")
ErrNodeAlreadyInTree = errors.New("node already present")
)
func NewTree() *Tree {
return &Tree{
Nodes: []*Node{},
}
}
func (t Tree) String() string {
return fmt.Sprintf("Tree<%d nodes>", len(t.Nodes))
}
2014-11-30 21:34:21 +00:00
2015-10-26 19:49:01 +00:00
type TreeLoader interface {
2016-08-31 18:58:57 +00:00
LoadJSONPack(BlobType, ID, interface{}) error
2015-10-26 19:49:01 +00:00
}
2016-08-31 18:29:54 +00:00
func LoadTree(repo TreeLoader, id ID) (*Tree, error) {
tree := &Tree{}
2016-08-31 18:58:57 +00:00
err := repo.LoadJSONPack(TreeBlob, id, tree)
2014-11-30 21:34:21 +00:00
if err != nil {
return nil, err
}
return tree, nil
}
// Equals returns true if t and other have exactly the same nodes.
func (t Tree) Equals(other *Tree) bool {
if len(t.Nodes) != len(other.Nodes) {
2015-01-14 21:08:48 +00:00
debug.Log("Tree.Equals", "tree.Equals(): trees have different number of nodes")
return false
}
for i := 0; i < len(t.Nodes); i++ {
if !t.Nodes[i].Equals(*other.Nodes[i]) {
2015-01-14 21:08:48 +00:00
debug.Log("Tree.Equals", "tree.Equals(): node %d is different:", i)
debug.Log("Tree.Equals", " %#v", t.Nodes[i])
debug.Log("Tree.Equals", " %#v", other.Nodes[i])
return false
}
}
return true
2014-11-30 21:34:21 +00:00
}
func (t *Tree) Insert(node *Node) error {
pos, _, err := t.binarySearch(node.Name)
2014-11-30 21:34:21 +00:00
if err == nil {
return ErrNodeAlreadyInTree
}
2015-02-15 13:44:54 +00:00
// https://code.google.com/p/go-wiki/wiki/SliceTricks
t.Nodes = append(t.Nodes, &Node{})
copy(t.Nodes[pos+1:], t.Nodes[pos:])
t.Nodes[pos] = node
2014-11-30 21:34:21 +00:00
return nil
}
func (t Tree) binarySearch(name string) (int, *Node, error) {
pos := sort.Search(len(t.Nodes), func(i int) bool {
return t.Nodes[i].Name >= name
2014-11-30 21:34:21 +00:00
})
if pos < len(t.Nodes) && t.Nodes[pos].Name == name {
return pos, t.Nodes[pos], nil
2014-11-30 21:34:21 +00:00
}
return pos, nil, ErrNodeNotFound
}
func (t Tree) Find(name string) (*Node, error) {
_, node, err := t.binarySearch(name)
2014-11-30 21:34:21 +00:00
return node, err
}
// Subtrees returns a slice of all subtree IDs of the tree.
2016-08-31 18:29:54 +00:00
func (t Tree) Subtrees() (trees IDs) {
for _, node := range t.Nodes {
2016-09-01 19:20:03 +00:00
if node.Type == "dir" && node.Subtree != nil {
trees = append(trees, *node.Subtree)
}
}
return trees
}