Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix/add mem 4 #1965

Merged
merged 3 commits into from
Nov 19, 2015
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 9 additions & 7 deletions core/commands/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,13 +141,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 @@ -150,8 +150,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 @@ -209,7 +209,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 @@ -227,7 +227,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 @@ -246,7 +246,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 @@ -262,7 +262,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 @@ -291,7 +291,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 @@ -314,7 +314,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 @@ -331,7 +331,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 @@ -349,20 +349,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