Skip to content

Commit

Permalink
Merge pull request #1965 from ipfs/fix/add-mem-4
Browse files Browse the repository at this point in the history
Fix/add mem 4
  • Loading branch information
whyrusleeping committed Nov 19, 2015
2 parents 09d34db + 02e14c4 commit 5f8cfc9
Show file tree
Hide file tree
Showing 10 changed files with 133 additions and 72 deletions.
16 changes: 9 additions & 7 deletions core/commands/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,13 +149,15 @@ remains to be implemented.
return err
}

if !hash {
// copy intermediary nodes from editor to our actual dagservice
err := fileAdder.WriteOutputTo(n.DAG)
if err != nil {
log.Error("WRITE OUT: ", err)
return err
}
if hash {
return nil
}

// copy intermediary nodes from editor to our actual dagservice
_, err := fileAdder.Finalize(n.DAG)
if err != nil {
log.Error("WRITE OUT: ", err)
return err
}

return fileAdder.PinRoot()
Expand Down
14 changes: 10 additions & 4 deletions core/commands/object.go
Original file line number Diff line number Diff line change
Expand Up @@ -599,14 +599,17 @@ func rmLinkCaller(req cmds.Request, root *dag.Node) (key.Key, error) {

path := req.Arguments()[2]

e := dagutils.NewDagEditor(nd.DAG, root)
e := dagutils.NewDagEditor(root, nd.DAG)

err = e.RmLink(req.Context(), path)
if err != nil {
return "", err
}

nnode := e.GetNode()
nnode, err := e.Finalize(nd.DAG)
if err != nil {
return "", err
}

return nnode.Key()
}
Expand Down Expand Up @@ -636,7 +639,7 @@ func addLinkCaller(req cmds.Request, root *dag.Node) (key.Key, error) {
}
}

e := dagutils.NewDagEditor(nd.DAG, root)
e := dagutils.NewDagEditor(root, nd.DAG)

childnd, err := nd.DAG.Get(req.Context(), childk)
if err != nil {
Expand All @@ -648,7 +651,10 @@ func addLinkCaller(req cmds.Request, root *dag.Node) (key.Key, error) {
return "", err
}

nnode := e.GetNode()
nnode, err := e.Finalize(nd.DAG)
if err != nil {
return "", err
}

return nnode.Key()
}
Expand Down
10 changes: 8 additions & 2 deletions core/corehttp/gateway_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -320,14 +320,20 @@ func (i *gatewayHandler) putHandler(w http.ResponseWriter, r *http.Request) {
return
}

e := dagutils.NewDagEditor(i.node.DAG, rnode)
e := dagutils.NewDagEditor(rnode, i.node.DAG)
err = e.InsertNodeAtPath(ctx, newPath, newnode, uio.NewEmptyDirectory)
if err != nil {
webError(w, "putHandler: InsertNodeAtPath failed", err, http.StatusInternalServerError)
return
}

newkey, err = e.GetNode().Key()
nnode, err := e.Finalize(i.node.DAG)
if err != nil {
webError(w, "putHandler: could not get node", err, http.StatusInternalServerError)
return
}

newkey, err = nnode.Key()
if err != nil {
webError(w, "putHandler: could not get key of edited node", err, http.StatusInternalServerError)
return
Expand Down
38 changes: 19 additions & 19 deletions core/coreunix/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import (

"github.com/ipfs/go-ipfs/commands/files"
core "github.com/ipfs/go-ipfs/core"
merkledag "github.com/ipfs/go-ipfs/merkledag"
dag "github.com/ipfs/go-ipfs/merkledag"
unixfs "github.com/ipfs/go-ipfs/unixfs"
logging "github.com/ipfs/go-ipfs/vendor/QmQg1J6vikuXF9oDvm4wpdeAUvvkVEKW1EYDw9HhTMnP2b/go-log"
)
Expand Down Expand Up @@ -63,7 +63,7 @@ type AddedObject struct {
}

func NewAdder(ctx context.Context, n *core.IpfsNode, out chan interface{}) *Adder {
e := dagutils.NewDagEditor(NewMemoryDagService(), newDirNode())
e := dagutils.NewDagEditor(newDirNode(), nil)
return &Adder{
ctx: ctx,
node: n,
Expand All @@ -90,11 +90,11 @@ type Adder struct {
Trickle bool
Wrap bool
Chunker string
root *merkledag.Node
root *dag.Node
}

// Perform the actual add & pin locally, outputting results to reader
func (params Adder) add(reader io.Reader) (*merkledag.Node, error) {
func (params Adder) add(reader io.Reader) (*dag.Node, error) {
chnk, err := chunk.FromString(reader, params.Chunker)
if err != nil {
return nil, err
Expand All @@ -112,7 +112,7 @@ func (params Adder) add(reader io.Reader) (*merkledag.Node, error) {
)
}

func (params *Adder) RootNode() (*merkledag.Node, error) {
func (params *Adder) RootNode() (*dag.Node, error) {
// for memoizing
if params.root != nil {
return params.root, nil
Expand Down Expand Up @@ -153,8 +153,8 @@ func (params *Adder) PinRoot() error {
return params.node.Pinning.Flush()
}

func (params *Adder) WriteOutputTo(DAG merkledag.DAGService) error {
return params.editor.WriteOutputTo(DAG)
func (params *Adder) Finalize(DAG dag.DAGService) (*dag.Node, error) {
return params.editor.Finalize(DAG)
}

// Add builds a merkledag from the a reader, pinning all objects to the local
Expand Down Expand Up @@ -212,7 +212,7 @@ func AddR(n *core.IpfsNode, root string) (key string, err error) {
// to preserve the filename.
// Returns the path of the added file ("<dir hash>/filename"), the DAG node of
// the directory, and and error if any.
func AddWrapped(n *core.IpfsNode, r io.Reader, filename string) (string, *merkledag.Node, error) {
func AddWrapped(n *core.IpfsNode, r io.Reader, filename string) (string, *dag.Node, error) {
file := files.NewReaderFile(filename, filename, ioutil.NopCloser(r), nil)
dir := files.NewSliceFile("", "", []files.File{file})
fileAdder := NewAdder(n.Context(), n, nil)
Expand All @@ -230,7 +230,7 @@ func AddWrapped(n *core.IpfsNode, r io.Reader, filename string) (string, *merkle
return gopath.Join(k.String(), filename), dagnode, nil
}

func (params *Adder) addNode(node *merkledag.Node, path string) error {
func (params *Adder) addNode(node *dag.Node, path string) error {
// patch it into the root
if path == "" {
key, err := node.Key()
Expand All @@ -249,7 +249,7 @@ func (params *Adder) addNode(node *merkledag.Node, path string) error {
}

// Add the given file while respecting the params.
func (params *Adder) AddFile(file files.File) (*merkledag.Node, error) {
func (params *Adder) AddFile(file files.File) (*dag.Node, error) {
switch {
case files.IsHidden(file) && !params.Hidden:
log.Debugf("%s is hidden, skipping", file.FileName())
Expand All @@ -265,7 +265,7 @@ func (params *Adder) AddFile(file files.File) (*merkledag.Node, error) {
return nil, err
}

dagnode := &merkledag.Node{Data: sdata}
dagnode := &dag.Node{Data: sdata}
_, err = params.node.DAG.Add(dagnode)
if err != nil {
return nil, err
Expand Down Expand Up @@ -294,7 +294,7 @@ func (params *Adder) AddFile(file files.File) (*merkledag.Node, error) {
return dagnode, err
}

func (params *Adder) addDir(dir files.File) (*merkledag.Node, error) {
func (params *Adder) addDir(dir files.File) (*dag.Node, error) {
tree := newDirNode()
log.Infof("adding directory: %s", dir.FileName())

Expand All @@ -317,7 +317,7 @@ func (params *Adder) addDir(dir files.File) (*merkledag.Node, error) {

_, name := gopath.Split(file.FileName())

if err := tree.AddNodeLink(name, node); err != nil {
if err := tree.AddNodeLinkClean(name, node); err != nil {
return nil, err
}
}
Expand All @@ -334,7 +334,7 @@ func (params *Adder) addDir(dir files.File) (*merkledag.Node, error) {
}

// outputDagnode sends dagnode info over the output channel
func outputDagnode(out chan interface{}, name string, dn *merkledag.Node) error {
func outputDagnode(out chan interface{}, name string, dn *dag.Node) error {
if out == nil {
return nil
}
Expand All @@ -352,20 +352,20 @@ func outputDagnode(out chan interface{}, name string, dn *merkledag.Node) error
return nil
}

func NewMemoryDagService() merkledag.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 merkledag.NewDAGService(bsrv)
return dag.NewDAGService(bsrv)
}

// TODO: generalize this to more than unix-fs nodes.
func newDirNode() *merkledag.Node {
return &merkledag.Node{Data: unixfs.FolderPBData()}
func newDirNode() *dag.Node {
return &dag.Node{Data: unixfs.FolderPBData()}
}

// from core/commands/object.go
func getOutput(dagnode *merkledag.Node) (*Object, error) {
func getOutput(dagnode *dag.Node) (*Object, error) {
key, err := dagnode.Key()
if err != nil {
return nil, err
Expand Down
13 changes: 11 additions & 2 deletions merkledag/merkledag.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ type DAGService interface {
AddRecursive(*Node) error
Get(context.Context, key.Key) (*Node, error)
Remove(*Node) error
RemoveRecursive(*Node) error

// GetDAG returns, in order, all the single leve child
// nodes of the passed in node.
Expand Down Expand Up @@ -107,10 +108,10 @@ func (n *dagService) Get(ctx context.Context, k key.Key) (*Node, error) {
}

// Remove deletes the given node and all of its children from the BlockService
func (n *dagService) Remove(nd *Node) error {
func (n *dagService) RemoveRecursive(nd *Node) error {
for _, l := range nd.Links {
if l.Node != nil {
n.Remove(l.Node)
n.RemoveRecursive(l.Node)
}
}
k, err := nd.Key()
Expand All @@ -120,6 +121,14 @@ func (n *dagService) Remove(nd *Node) error {
return n.Blocks.DeleteBlock(k)
}

func (n *dagService) Remove(nd *Node) error {
k, err := nd.Key()
if err != nil {
return err
}
return n.Blocks.DeleteBlock(k)
}

// FetchGraph fetches all nodes that are children of the given node
func FetchGraph(ctx context.Context, root *Node, serv DAGService) error {
return EnumerateChildrenAsync(ctx, serv, root, key.NewKeySet())
Expand Down
4 changes: 3 additions & 1 deletion merkledag/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import (
key "github.com/ipfs/go-ipfs/blocks/key"
)

var ErrLinkNotFound = fmt.Errorf("no link by that name")

// Node represents a node in the IPFS Merkle DAG.
// nodes have opaque data and a set of navigable links.
type Node struct {
Expand Down Expand Up @@ -160,7 +162,7 @@ func (n *Node) GetNodeLink(name string) (*Link, error) {
}, nil
}
}
return nil, ErrNotFound
return nil, ErrLinkNotFound
}

func (n *Node) GetLinkedNode(ctx context.Context, ds DAGService, name string) (*Node, error) {
Expand Down
5 changes: 3 additions & 2 deletions merkledag/utils/diff.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func (c *Change) String() string {
}

func ApplyChange(ctx context.Context, ds dag.DAGService, nd *dag.Node, cs []*Change) (*dag.Node, error) {
e := NewDagEditor(ds, nd)
e := NewDagEditor(nd, ds)
for _, c := range cs {
switch c.Type {
case Add:
Expand Down Expand Up @@ -71,7 +71,8 @@ func ApplyChange(ctx context.Context, ds dag.DAGService, nd *dag.Node, cs []*Cha
}
}
}
return e.GetNode(), nil

return e.Finalize(ds)
}

func Diff(ctx context.Context, ds dag.DAGService, a, b *dag.Node) []*Change {
Expand Down
Loading

0 comments on commit 5f8cfc9

Please sign in to comment.