utils.go 3.68 KB
Newer Older
1 2 3 4
package dagutils

import (
	"errors"
Jeromy's avatar
Jeromy committed
5
	"strings"
6 7 8 9 10 11

	context "github.com/ipfs/go-ipfs/Godeps/_workspace/src/golang.org/x/net/context"

	dag "github.com/ipfs/go-ipfs/merkledag"
)

Jeromy's avatar
Jeromy committed
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
type Editor struct {
	root *dag.Node
	ds   dag.DAGService
}

func NewDagEditor(ds dag.DAGService, root *dag.Node) *Editor {
	return &Editor{
		root: root,
		ds:   ds,
	}
}

func (e *Editor) GetNode() *dag.Node {
	return e.root.Copy()
}

28 29
func (e *Editor) GetDagService() dag.DAGService {
	return e.ds
Jeromy's avatar
Jeromy committed
30 31
}

32
func addLink(ctx context.Context, ds dag.DAGService, root *dag.Node, childname string, childnd *dag.Node) (*dag.Node, error) {
33 34 35 36
	if childname == "" {
		return nil, errors.New("cannot create link with no name!")
	}

37 38
	// ensure that the node we are adding is in the dagservice
	_, err := ds.Add(childnd)
39 40 41 42
	if err != nil {
		return nil, err
	}

Jeromy's avatar
Jeromy committed
43 44
	_ = ds.Remove(root)

45 46 47
	// ensure no link with that name already exists
	_ = root.RemoveNodeLink(childname) // ignore error, only option is ErrNotFound

48
	if err := root.AddNodeLinkClean(childname, childnd); err != nil {
49 50 51
		return nil, err
	}

52
	if _, err := ds.Add(root); err != nil {
53 54 55 56 57
		return nil, err
	}
	return root, nil
}

58
func (e *Editor) InsertNodeAtPath(ctx context.Context, path string, toinsert *dag.Node, create func() *dag.Node) error {
Jeromy's avatar
Jeromy committed
59 60 61 62 63 64 65 66 67
	splpath := strings.Split(path, "/")
	nd, err := insertNodeAtPath(ctx, e.ds, e.root, splpath, toinsert, create)
	if err != nil {
		return err
	}
	e.root = nd
	return nil
}

68
func insertNodeAtPath(ctx context.Context, ds dag.DAGService, root *dag.Node, path []string, toinsert *dag.Node, create func() *dag.Node) (*dag.Node, error) {
69
	if len(path) == 1 {
Jeromy's avatar
Jeromy committed
70
		return addLink(ctx, ds, root, path[0], toinsert)
71 72 73 74 75
	}

	nd, err := root.GetLinkedNode(ctx, ds, path[0])
	if err != nil {
		// if 'create' is true, we create directories on the way down as needed
Jeromy's avatar
Jeromy committed
76 77
		if err == dag.ErrNotFound && create != nil {
			nd = create()
78 79 80 81 82
		} else {
			return nil, err
		}
	}

Jeromy's avatar
Jeromy committed
83
	ndprime, err := insertNodeAtPath(ctx, ds, nd, path[1:], toinsert, create)
84 85 86 87
	if err != nil {
		return nil, err
	}

Jeromy's avatar
Jeromy committed
88 89
	_ = ds.Remove(root)

90 91 92 93 94 95 96 97 98 99 100 101 102 103
	_ = root.RemoveNodeLink(path[0])
	err = root.AddNodeLinkClean(path[0], ndprime)
	if err != nil {
		return nil, err
	}

	_, err = ds.Add(root)
	if err != nil {
		return nil, err
	}

	return root, nil
}

Jeromy's avatar
Jeromy committed
104 105 106 107 108 109 110 111 112 113 114
func (e *Editor) RmLink(ctx context.Context, path string) error {
	splpath := strings.Split(path, "/")
	nd, err := rmLink(ctx, e.ds, e.root, splpath)
	if err != nil {
		return err
	}
	e.root = nd
	return nil
}

func rmLink(ctx context.Context, ds dag.DAGService, root *dag.Node, path []string) (*dag.Node, error) {
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
	if len(path) == 1 {
		// base case, remove node in question
		err := root.RemoveNodeLink(path[0])
		if err != nil {
			return nil, err
		}

		_, err = ds.Add(root)
		if err != nil {
			return nil, err
		}

		return root, nil
	}

	nd, err := root.GetLinkedNode(ctx, ds, path[0])
	if err != nil {
		return nil, err
	}

Jeromy's avatar
Jeromy committed
135
	nnode, err := rmLink(ctx, ds, nd, path[1:])
136 137 138 139
	if err != nil {
		return nil, err
	}

Jeromy's avatar
Jeromy committed
140 141
	_ = ds.Remove(root)

142 143 144 145 146 147 148 149 150 151 152 153 154
	_ = root.RemoveNodeLink(path[0])
	err = root.AddNodeLinkClean(path[0], nnode)
	if err != nil {
		return nil, err
	}

	_, err = ds.Add(root)
	if err != nil {
		return nil, err
	}

	return root, nil
}
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183

func (e *Editor) WriteOutputTo(ds dag.DAGService) error {
	return copyDag(e.GetNode(), e.ds, ds)
}

func copyDag(nd *dag.Node, from, to dag.DAGService) error {
	_, err := to.Add(nd)
	if err != nil {
		return err
	}

	for _, lnk := range nd.Links {
		child, err := lnk.GetNode(context.Background(), from)
		if err != nil {
			if err == dag.ErrNotFound {
				// not found means we didnt modify it, and it should
				// already be in the target datastore
				continue
			}
			return err
		}

		err = copyDag(child, from, to)
		if err != nil {
			return err
		}
	}
	return nil
}