func insertNodeAtPath(ctx context.Context, ds dag.DAGService, root *dag.Node, path []string, toinsert key.Key, create func() *dag.Node) (*dag.Node, error) { if len(path) == 1 { return addLink(ctx, ds, root, path[0], toinsert) } nd, err := root.GetLinkedNode(ctx, ds, path[0]) if err != nil { // if 'create' is true, we create directories on the way down as needed if err == dag.ErrNotFound && create != nil { nd = create() } else { return nil, err } } ndprime, err := insertNodeAtPath(ctx, ds, nd, path[1:], toinsert, create) if err != nil { return nil, err } _ = 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 }
func (e *Editor) rmLink(ctx context.Context, root *dag.Node, path []string) (*dag.Node, error) { if len(path) == 1 { // base case, remove node in question err := root.RemoveNodeLink(path[0]) if err != nil { return nil, err } _, err = e.tmp.Add(root) if err != nil { return nil, err } return root, nil } // search for node in both tmp dagstore and source dagstore nd, err := root.GetLinkedNode(ctx, e.tmp, path[0]) if err == dag.ErrNotFound { nd, err = root.GetLinkedNode(ctx, e.src, path[0]) } if err != nil { return nil, err } nnode, err := e.rmLink(ctx, nd, path[1:]) if err != nil { return nil, err } _ = e.tmp.Remove(root) _ = root.RemoveNodeLink(path[0]) err = root.AddNodeLinkClean(path[0], nnode) if err != nil { return nil, err } _, err = e.tmp.Add(root) if err != nil { return nil, err } return root, nil }
func (e *Editor) insertNodeAtPath(ctx context.Context, root *dag.Node, path []string, toinsert *dag.Node, create func() *dag.Node) (*dag.Node, error) { if len(path) == 1 { return addLink(ctx, e.tmp, root, path[0], toinsert) } nd, err := root.GetLinkedNode(ctx, e.tmp, path[0]) if err != nil { // if 'create' is true, we create directories on the way down as needed if err == dag.ErrLinkNotFound && create != nil { nd = create() err = nil // no longer an error case } else if err == dag.ErrNotFound { // try finding it in our source dagstore nd, err = root.GetLinkedNode(ctx, e.src, path[0]) } // if we receive an ErrNotFound, then our second 'GetLinkedNode' call // also fails, we want to error out if err != nil { return nil, err } } ndprime, err := e.insertNodeAtPath(ctx, nd, path[1:], toinsert, create) if err != nil { return nil, err } _ = e.tmp.Remove(root) _ = root.RemoveNodeLink(path[0]) err = root.AddNodeLinkClean(path[0], ndprime) if err != nil { return nil, err } _, err = e.tmp.Add(root) if err != nil { return nil, err } return root, nil }
func rmLink(ctx context.Context, ds dag.DAGService, root *dag.Node, path []string) (*dag.Node, error) { 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 } nnode, err := rmLink(ctx, ds, nd, path[1:]) if err != nil { return nil, err } _ = 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 }
//GetLinkedNode returns node named name from merkledag n. func (m *Self) GetLinkedNode(n *merkledag.Node, name string) (*merkledag.Node, error) { log.Println("getting name", name) nn, err := n.GetLinkedNode(m.ctx, m.ipfsNode.DAG, name) log.If(err) return nn, err }