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

import (
4
	"context"
5 6
	"errors"

7
	bserv "github.com/ipfs/go-ipfs/blockservice"
8
	dag "github.com/ipfs/go-ipfs/merkledag"
rht's avatar
rht committed
9
	path "github.com/ipfs/go-ipfs/path"
10

Steven Allen's avatar
Steven Allen committed
11
	offline "gx/ipfs/QmS6mo1dPpHdYsVkm27BRZDLxpKBCiJKUH8fHX15XFfMez/go-ipfs-exchange-offline"
Steven Allen's avatar
Steven Allen committed
12
	ipld "gx/ipfs/QmZtNq8dArGfnpCZfx2pUNY7UcjGhVp5qqwQ4hH6mpTMRQ/go-ipld-format"
Steven Allen's avatar
Steven Allen committed
13
	bstore "gx/ipfs/QmadMhXJLHMFjpRmh85XjpmVDkEtQpNYEZNRpWRvYVLrvb/go-ipfs-blockstore"
Steven Allen's avatar
Steven Allen committed
14 15
	ds "gx/ipfs/QmeiCcJfDW1GJnWUArudsv5rQsihpi4oyddPhdqo3CfX6i/go-datastore"
	syncds "gx/ipfs/QmeiCcJfDW1GJnWUArudsv5rQsihpi4oyddPhdqo3CfX6i/go-datastore/sync"
16 17
)

18 19
// Editor represents a ProtoNode tree editor and provides methods to
// modify it.
Jeromy's avatar
Jeromy committed
20
type Editor struct {
21
	root *dag.ProtoNode
22 23 24

	// tmp is a temporary in memory (for now) dagstore for all of the
	// intermediary nodes to be stored in
25
	tmp ipld.DAGService
26 27 28

	// 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)
29
	src ipld.DAGService
30 31
}

Steven Allen's avatar
Steven Allen committed
32
// NewMemoryDagService returns a new, thread-safe in-memory DAGService.
33
func NewMemoryDagService() ipld.DAGService {
34 35 36 37
	// 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
38 39
}

Steven Allen's avatar
Steven Allen committed
40 41 42 43
// NewDagEditor returns an ProtoNode editor.
//
// * root is the node to be modified
// * source is the dagstore to pull nodes from (optional)
44
func NewDagEditor(root *dag.ProtoNode, source ipld.DAGService) *Editor {
Jeromy's avatar
Jeromy committed
45 46
	return &Editor{
		root: root,
47 48
		tmp:  NewMemoryDagService(),
		src:  source,
Jeromy's avatar
Jeromy committed
49 50 51
	}
}

Steven Allen's avatar
Steven Allen committed
52
// GetNode returns the a copy of the root node being edited.
53
func (e *Editor) GetNode() *dag.ProtoNode {
54
	return e.root.Copy().(*dag.ProtoNode)
Jeromy's avatar
Jeromy committed
55 56
}

Steven Allen's avatar
Steven Allen committed
57
// GetDagService returns the DAGService used by this editor.
58
func (e *Editor) GetDagService() ipld.DAGService {
59
	return e.tmp
Jeromy's avatar
Jeromy committed
60 61
}

62
func addLink(ctx context.Context, ds ipld.DAGService, root *dag.ProtoNode, childname string, childnd ipld.Node) (*dag.ProtoNode, error) {
63
	if childname == "" {
Steven Allen's avatar
Steven Allen committed
64
		return nil, errors.New("cannot create link with no name")
65 66
	}

67
	// ensure that the node we are adding is in the dagservice
68
	err := ds.Add(ctx, childnd)
69 70 71 72
	if err != nil {
		return nil, err
	}

73
	_ = ds.Remove(ctx, root.Cid())
Jeromy's avatar
Jeromy committed
74

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

Hector Sanjuan's avatar
Hector Sanjuan committed
78
	if err := root.AddNodeLink(childname, childnd); err != nil {
79 80 81
		return nil, err
	}

82
	if err := ds.Add(ctx, root); err != nil {
83 84 85 86 87
		return nil, err
	}
	return root, nil
}

88
// InsertNodeAtPath inserts a new node in the tree and replaces the current root with the new one.
89
func (e *Editor) InsertNodeAtPath(ctx context.Context, pth string, toinsert ipld.Node, create func() *dag.ProtoNode) error {
rht's avatar
rht committed
90
	splpath := path.SplitList(pth)
91
	nd, err := e.insertNodeAtPath(ctx, e.root, splpath, toinsert, create)
Jeromy's avatar
Jeromy committed
92 93 94 95 96 97 98
	if err != nil {
		return err
	}
	e.root = nd
	return nil
}

99
func (e *Editor) insertNodeAtPath(ctx context.Context, root *dag.ProtoNode, path []string, toinsert ipld.Node, create func() *dag.ProtoNode) (*dag.ProtoNode, error) {
100
	if len(path) == 1 {
101
		return addLink(ctx, e.tmp, root, path[0], toinsert)
102 103
	}

104
	nd, err := root.GetLinkedProtoNode(ctx, e.tmp, path[0])
105 106
	if err != nil {
		// if 'create' is true, we create directories on the way down as needed
107
		if err == dag.ErrLinkNotFound && create != nil {
Jeromy's avatar
Jeromy committed
108
			nd = create()
109
			err = nil // no longer an error case
110
		} else if err == ipld.ErrNotFound {
111
			// try finding it in our source dagstore
112
			nd, err = root.GetLinkedProtoNode(ctx, e.src, path[0])
113 114
		}

115 116
		// if we receive an ErrNotFound, then our second 'GetLinkedNode' call
		// also fails, we want to error out
117
		if err != nil {
118 119 120 121
			return nil, err
		}
	}

122
	ndprime, err := e.insertNodeAtPath(ctx, nd, path[1:], toinsert, create)
123 124 125 126
	if err != nil {
		return nil, err
	}

127
	_ = e.tmp.Remove(ctx, root.Cid())
Jeromy's avatar
Jeromy committed
128

129
	_ = root.RemoveNodeLink(path[0])
Hector Sanjuan's avatar
Hector Sanjuan committed
130
	err = root.AddNodeLink(path[0], ndprime)
131 132 133 134
	if err != nil {
		return nil, err
	}

135
	err = e.tmp.Add(ctx, root)
136 137 138 139 140 141 142
	if err != nil {
		return nil, err
	}

	return root, nil
}

143 144
// RmLink removes the link with the given name and updates the root node of
// the editor.
rht's avatar
rht committed
145 146
func (e *Editor) RmLink(ctx context.Context, pth string) error {
	splpath := path.SplitList(pth)
147
	nd, err := e.rmLink(ctx, e.root, splpath)
Jeromy's avatar
Jeromy committed
148 149 150 151 152 153 154
	if err != nil {
		return err
	}
	e.root = nd
	return nil
}

155
func (e *Editor) rmLink(ctx context.Context, root *dag.ProtoNode, path []string) (*dag.ProtoNode, error) {
156 157 158 159 160 161 162
	if len(path) == 1 {
		// base case, remove node in question
		err := root.RemoveNodeLink(path[0])
		if err != nil {
			return nil, err
		}

163
		err = e.tmp.Add(ctx, root)
164 165 166 167 168 169 170
		if err != nil {
			return nil, err
		}

		return root, nil
	}

171
	// search for node in both tmp dagstore and source dagstore
172
	nd, err := root.GetLinkedProtoNode(ctx, e.tmp, path[0])
173
	if err == ipld.ErrNotFound {
174
		nd, err = root.GetLinkedProtoNode(ctx, e.src, path[0])
175 176
	}

177 178 179 180
	if err != nil {
		return nil, err
	}

181
	nnode, err := e.rmLink(ctx, nd, path[1:])
182 183 184 185
	if err != nil {
		return nil, err
	}

186
	e.tmp.Remove(ctx, root.Cid())
Jeromy's avatar
Jeromy committed
187

188
	_ = root.RemoveNodeLink(path[0])
Hector Sanjuan's avatar
Hector Sanjuan committed
189
	err = root.AddNodeLink(path[0], nnode)
190 191 192 193
	if err != nil {
		return nil, err
	}

194
	err = e.tmp.Add(ctx, root)
195 196 197 198 199 200
	if err != nil {
		return nil, err
	}

	return root, nil
}
201

Steven Allen's avatar
Steven Allen committed
202 203
// Finalize writes the new DAG to the given DAGService and returns the modified
// root node.
204
func (e *Editor) Finalize(ctx context.Context, ds ipld.DAGService) (*dag.ProtoNode, error) {
205
	nd := e.GetNode()
206
	err := copyDag(ctx, nd, e.tmp, ds)
207
	return nd, err
208 209
}

210
func copyDag(ctx context.Context, nd ipld.Node, from, to ipld.DAGService) error {
211 212
	// TODO(#4609): make this batch.
	err := to.Add(ctx, nd)
213 214 215 216
	if err != nil {
		return err
	}

217
	for _, lnk := range nd.Links() {
218
		child, err := lnk.GetNode(ctx, from)
219
		if err != nil {
220
			if err == ipld.ErrNotFound {
221 222 223 224 225 226 227
				// not found means we didnt modify it, and it should
				// already be in the target datastore
				continue
			}
			return err
		}

228
		err = copyDag(ctx, child, from, to)
229 230 231 232 233 234
		if err != nil {
			return err
		}
	}
	return nil
}