utils.go 5.29 KB
Newer Older
1 2 3
package dagutils

import (
4
	"context"
5 6
	"errors"

7 8 9
	bstore "github.com/ipfs/go-ipfs/blocks/blockstore"
	bserv "github.com/ipfs/go-ipfs/blockservice"
	offline "github.com/ipfs/go-ipfs/exchange/offline"
10
	dag "github.com/ipfs/go-ipfs/merkledag"
rht's avatar
rht committed
11
	path "github.com/ipfs/go-ipfs/path"
12

13
	node "gx/ipfs/QmPN7cwmpcc4DWXb4KTB9dNAJgjuPY69h3npsMfhRrQL9c/go-ipld-format"
14 15
	ds "gx/ipfs/QmdHG8MAuARdGHxx4rPQASLcvhz24fzjSQq7AJRAQEorq5/go-datastore"
	syncds "gx/ipfs/QmdHG8MAuARdGHxx4rPQASLcvhz24fzjSQq7AJRAQEorq5/go-datastore/sync"
16 17
)

Jeromy's avatar
Jeromy committed
18
type Editor struct {
19
	root *dag.ProtoNode
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34

	// tmp is a temporary in memory (for now) dagstore for all of the
	// intermediary nodes to be stored in
	tmp dag.DAGService

	// src is the dagstore with *all* of the data on it, it is used to pull
	// nodes from for modification (nil is a valid value)
	src dag.DAGService
}

func NewMemoryDagService() dag.DAGService {
	// build mem-datastore for editor's intermediary nodes
	bs := bstore.NewBlockstore(syncds.MutexWrap(ds.NewMapDatastore()))
	bsrv := bserv.New(bs, offline.Exchange(bs))
	return dag.NewDAGService(bsrv)
Jeromy's avatar
Jeromy committed
35 36
}

37
// root is the node to be modified, source is the dagstore to pull nodes from (optional)
38
func NewDagEditor(root *dag.ProtoNode, source dag.DAGService) *Editor {
Jeromy's avatar
Jeromy committed
39 40
	return &Editor{
		root: root,
41 42
		tmp:  NewMemoryDagService(),
		src:  source,
Jeromy's avatar
Jeromy committed
43 44 45
	}
}

46
func (e *Editor) GetNode() *dag.ProtoNode {
47
	return e.root.Copy().(*dag.ProtoNode)
Jeromy's avatar
Jeromy committed
48 49
}

50
func (e *Editor) GetDagService() dag.DAGService {
51
	return e.tmp
Jeromy's avatar
Jeromy committed
52 53
}

54
func addLink(ctx context.Context, ds dag.DAGService, root *dag.ProtoNode, childname string, childnd node.Node) (*dag.ProtoNode, error) {
55 56 57 58
	if childname == "" {
		return nil, errors.New("cannot create link with no name!")
	}

59 60
	// ensure that the node we are adding is in the dagservice
	_, err := ds.Add(childnd)
61 62 63 64
	if err != nil {
		return nil, err
	}

Jeromy's avatar
Jeromy committed
65 66
	_ = ds.Remove(root)

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

70
	if err := root.AddNodeLinkClean(childname, childnd); err != nil {
71 72 73
		return nil, err
	}

74
	if _, err := ds.Add(root); err != nil {
75 76 77 78 79
		return nil, err
	}
	return root, nil
}

80
func (e *Editor) InsertNodeAtPath(ctx context.Context, pth string, toinsert node.Node, create func() *dag.ProtoNode) error {
rht's avatar
rht committed
81
	splpath := path.SplitList(pth)
82
	nd, err := e.insertNodeAtPath(ctx, e.root, splpath, toinsert, create)
Jeromy's avatar
Jeromy committed
83 84 85 86 87 88 89
	if err != nil {
		return err
	}
	e.root = nd
	return nil
}

90
func (e *Editor) insertNodeAtPath(ctx context.Context, root *dag.ProtoNode, path []string, toinsert node.Node, create func() *dag.ProtoNode) (*dag.ProtoNode, error) {
91
	if len(path) == 1 {
92
		return addLink(ctx, e.tmp, root, path[0], toinsert)
93 94
	}

95
	nd, err := root.GetLinkedProtoNode(ctx, e.tmp, path[0])
96 97
	if err != nil {
		// if 'create' is true, we create directories on the way down as needed
98
		if err == dag.ErrLinkNotFound && create != nil {
Jeromy's avatar
Jeromy committed
99
			nd = create()
100 101
			err = nil // no longer an error case
		} else if err == dag.ErrNotFound {
102
			// try finding it in our source dagstore
103
			nd, err = root.GetLinkedProtoNode(ctx, e.src, path[0])
104 105
		}

106 107
		// if we receive an ErrNotFound, then our second 'GetLinkedNode' call
		// also fails, we want to error out
108
		if err != nil {
109 110 111 112
			return nil, err
		}
	}

113
	ndprime, err := e.insertNodeAtPath(ctx, nd, path[1:], toinsert, create)
114 115 116 117
	if err != nil {
		return nil, err
	}

118
	_ = e.tmp.Remove(root)
Jeromy's avatar
Jeromy committed
119

120 121 122 123 124 125
	_ = root.RemoveNodeLink(path[0])
	err = root.AddNodeLinkClean(path[0], ndprime)
	if err != nil {
		return nil, err
	}

126
	_, err = e.tmp.Add(root)
127 128 129 130 131 132 133
	if err != nil {
		return nil, err
	}

	return root, nil
}

rht's avatar
rht committed
134 135
func (e *Editor) RmLink(ctx context.Context, pth string) error {
	splpath := path.SplitList(pth)
136
	nd, err := e.rmLink(ctx, e.root, splpath)
Jeromy's avatar
Jeromy committed
137 138 139 140 141 142 143
	if err != nil {
		return err
	}
	e.root = nd
	return nil
}

144
func (e *Editor) rmLink(ctx context.Context, root *dag.ProtoNode, path []string) (*dag.ProtoNode, error) {
145 146 147 148 149 150 151
	if len(path) == 1 {
		// base case, remove node in question
		err := root.RemoveNodeLink(path[0])
		if err != nil {
			return nil, err
		}

152
		_, err = e.tmp.Add(root)
153 154 155 156 157 158 159
		if err != nil {
			return nil, err
		}

		return root, nil
	}

160
	// search for node in both tmp dagstore and source dagstore
161
	nd, err := root.GetLinkedProtoNode(ctx, e.tmp, path[0])
162
	if err == dag.ErrNotFound {
163
		nd, err = root.GetLinkedProtoNode(ctx, e.src, path[0])
164 165
	}

166 167 168 169
	if err != nil {
		return nil, err
	}

170
	nnode, err := e.rmLink(ctx, nd, path[1:])
171 172 173 174
	if err != nil {
		return nil, err
	}

175
	_ = e.tmp.Remove(root)
Jeromy's avatar
Jeromy committed
176

177 178 179 180 181 182
	_ = root.RemoveNodeLink(path[0])
	err = root.AddNodeLinkClean(path[0], nnode)
	if err != nil {
		return nil, err
	}

183
	_, err = e.tmp.Add(root)
184 185 186 187 188 189
	if err != nil {
		return nil, err
	}

	return root, nil
}
190

191
func (e *Editor) Finalize(ds dag.DAGService) (*dag.ProtoNode, error) {
192 193 194
	nd := e.GetNode()
	err := copyDag(nd, e.tmp, ds)
	return nd, err
195 196
}

197
func copyDag(nd *dag.ProtoNode, from, to dag.DAGService) error {
198 199 200 201 202
	_, err := to.Add(nd)
	if err != nil {
		return err
	}

203
	for _, lnk := range nd.Links() {
204 205 206 207 208 209 210 211 212 213
		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
		}

214
		childpb, ok := child.(*dag.ProtoNode)
215 216 217
		if !ok { // leaf node
			_, err := to.Add(nd)
			return err
218 219 220
		}

		err = copyDag(childpb, from, to)
221 222 223 224 225 226
		if err != nil {
			return err
		}
	}
	return nil
}