utils.go 5.63 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

Steven Allen's avatar
Steven Allen committed
13 14
	ds "gx/ipfs/QmPpegoMqhAEqjncrzArm7KVWAkCm78rqL2DPuNjhPrshg/go-datastore"
	syncds "gx/ipfs/QmPpegoMqhAEqjncrzArm7KVWAkCm78rqL2DPuNjhPrshg/go-datastore/sync"
15
	ipld "gx/ipfs/Qme5bWv7wtjUNGsK2BNGVUFPKiuxWrsqrtvYwCLRw8YFES/go-ipld-format"
16 17
)

Jeromy's avatar
Jeromy committed
18
type Editor struct {
19
	root *dag.ProtoNode
20 21 22

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

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

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

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

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

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

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

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

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

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

76
	if err := root.AddNodeLinkClean(childname, childnd); err != nil {
77 78 79
		return nil, err
	}

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

86
func (e *Editor) InsertNodeAtPath(ctx context.Context, pth string, toinsert ipld.Node, create func() *dag.ProtoNode) error {
rht's avatar
rht committed
87
	splpath := path.SplitList(pth)
88
	nd, err := e.insertNodeAtPath(ctx, e.root, splpath, toinsert, create)
Jeromy's avatar
Jeromy committed
89 90 91 92 93 94 95
	if err != nil {
		return err
	}
	e.root = nd
	return nil
}

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

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

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

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

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

126 127 128 129 130 131
	_ = root.RemoveNodeLink(path[0])
	err = root.AddNodeLinkClean(path[0], ndprime)
	if err != nil {
		return nil, err
	}

132
	err = e.tmp.Add(ctx, root)
133 134 135 136 137 138 139
	if err != nil {
		return nil, err
	}

	return root, nil
}

rht's avatar
rht committed
140 141
func (e *Editor) RmLink(ctx context.Context, pth string) error {
	splpath := path.SplitList(pth)
142
	nd, err := e.rmLink(ctx, e.root, splpath)
Jeromy's avatar
Jeromy committed
143 144 145 146 147 148 149
	if err != nil {
		return err
	}
	e.root = nd
	return nil
}

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

158
		err = e.tmp.Add(ctx, root)
159 160 161 162 163 164 165
		if err != nil {
			return nil, err
		}

		return root, nil
	}

166
	// search for node in both tmp dagstore and source dagstore
167
	nd, err := root.GetLinkedProtoNode(ctx, e.tmp, path[0])
168
	if err == ipld.ErrNotFound {
169
		nd, err = root.GetLinkedProtoNode(ctx, e.src, path[0])
170 171
	}

172 173 174 175
	if err != nil {
		return nil, err
	}

176
	nnode, err := e.rmLink(ctx, nd, path[1:])
177 178 179 180
	if err != nil {
		return nil, err
	}

181
	e.tmp.Remove(ctx, root.Cid())
Jeromy's avatar
Jeromy committed
182

183 184 185 186 187 188
	_ = root.RemoveNodeLink(path[0])
	err = root.AddNodeLinkClean(path[0], nnode)
	if err != nil {
		return nil, err
	}

189
	err = e.tmp.Add(ctx, root)
190 191 192 193 194 195
	if err != nil {
		return nil, err
	}

	return root, nil
}
196

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

205
func copyDag(ctx context.Context, nd ipld.Node, from, to ipld.DAGService) error {
206 207
	// TODO(#4609): make this batch.
	err := to.Add(ctx, nd)
208 209 210 211
	if err != nil {
		return err
	}

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

223
		err = copyDag(ctx, child, from, to)
224 225 226 227 228 229
		if err != nil {
			return err
		}
	}
	return nil
}