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

import (
	"errors"

6
	context "context"
George Antoniadis's avatar
George Antoniadis committed
7 8
	ds "gx/ipfs/QmbzuUusHqaLLoNTDEVLcSF6vZDHZDLPC7p4bztRvvkXxU/go-datastore"
	syncds "gx/ipfs/QmbzuUusHqaLLoNTDEVLcSF6vZDHZDLPC7p4bztRvvkXxU/go-datastore/sync"
9

10 11 12
	bstore "github.com/ipfs/go-ipfs/blocks/blockstore"
	bserv "github.com/ipfs/go-ipfs/blockservice"
	offline "github.com/ipfs/go-ipfs/exchange/offline"
13
	dag "github.com/ipfs/go-ipfs/merkledag"
rht's avatar
rht committed
14
	path "github.com/ipfs/go-ipfs/path"
15 16
)

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

	// 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
34 35
}

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

	return root, nil
}

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

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

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

		return root, nil
	}

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

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

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

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

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

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

	return root, nil
}
189

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

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

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

213 214 215 216 217 218
		childpb, ok := child.(*dag.ProtoNode)
		if !ok {
			return dag.ErrNotProtobuf
		}

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