From 64a517afed3bce08320715702836bfe93f21a8ff Mon Sep 17 00:00:00 2001 From: Steven Allen Date: Fri, 26 Oct 2018 12:57:32 -0700 Subject: [PATCH 1/3] simplify dag add License: MIT Signed-off-by: Steven Allen --- core/commands/dag/dag.go | 97 +++++++++++++++------------------------- 1 file changed, 37 insertions(+), 60 deletions(-) diff --git a/core/commands/dag/dag.go b/core/commands/dag/dag.go index ac61db563..0dae28bec 100644 --- a/core/commands/dag/dag.go +++ b/core/commands/dag/dag.go @@ -87,81 +87,58 @@ into an object of the specified format. } } - outChan := make(chan interface{}, 8) + cids := cid.NewSet() + b := ipld.NewBatch(req.Context, nd.DAG) - addAllAndPin := func(f files.File) error { - cids := cid.NewSet() - b := ipld.NewBatch(req.Context, nd.DAG) - - for { - file, err := f.NextFile() - if err == io.EOF { - // Finished the list of files. - break - } else if err != nil { - return err - } - - nds, err := coredag.ParseInputs(ienc, format, file, mhType, -1) - if err != nil { - return err - } - if len(nds) == 0 { - return fmt.Errorf("no node returned from ParseInputs") - } - - for _, nd := range nds { - err := b.Add(nd) - if err != nil { - return err - } - } - - cid := nds[0].Cid() - cids.Add(cid) - - select { - case outChan <- &OutputObject{Cid: cid}: - case <-req.Context.Done(): - return nil - } - } - - if err := b.Commit(); err != nil { + for { + file, err := req.Files.NextFile() + if err == io.EOF { + // Finished the list of files. + break + } else if err != nil { return err } - if dopin { - defer nd.Blockstore.PinLock().Unlock() + nds, err := coredag.ParseInputs(ienc, format, file, mhType, -1) + if err != nil { + return err + } + if len(nds) == 0 { + return fmt.Errorf("no node returned from ParseInputs") + } - cids.ForEach(func(c cid.Cid) error { - nd.Pinning.PinWithMode(c, pin.Recursive) - return nil - }) - - err := nd.Pinning.Flush() + for _, nd := range nds { + err := b.Add(nd) if err != nil { return err } } - return nil + cid := nds[0].Cid() + cids.Add(cid) + if err := res.Emit(&OutputObject{Cid: cid}); err != nil { + return err + } } - errC := make(chan error) - go func() { - var err error - defer func() { errC <- err }() - defer close(outChan) - err = addAllAndPin(req.Files) - }() - - err = res.Emit(outChan) - if err != nil { + if err := b.Commit(); err != nil { return err } - return <-errC + if dopin { + defer nd.Blockstore.PinLock().Unlock() + + cids.ForEach(func(c cid.Cid) error { + nd.Pinning.PinWithMode(c, pin.Recursive) + return nil + }) + + err := nd.Pinning.Flush() + if err != nil { + return err + } + } + return nil }, Type: OutputObject{}, Encoders: cmds.EncoderMap{ From b8d7d25569fd59911ea876b25c500c69ba56d2c8 Mon Sep 17 00:00:00 2001 From: Steven Allen Date: Fri, 26 Oct 2018 12:58:25 -0700 Subject: [PATCH 2/3] remove dead code License: MIT Signed-off-by: Steven Allen --- core/commands/dag/dag.go | 15 --------------- 1 file changed, 15 deletions(-) diff --git a/core/commands/dag/dag.go b/core/commands/dag/dag.go index 0dae28bec..4d627f5c9 100644 --- a/core/commands/dag/dag.go +++ b/core/commands/dag/dag.go @@ -6,7 +6,6 @@ import ( "math" "github.com/ipfs/go-ipfs/core/commands/cmdenv" - "github.com/ipfs/go-ipfs/core/commands/e" "github.com/ipfs/go-ipfs/core/coredag" "github.com/ipfs/go-ipfs/pin" @@ -237,17 +236,3 @@ var DagResolveCmd = &cmds.Command{ }, Type: ResolveOutput{}, } - -// copy+pasted from ../commands.go -func unwrapOutput(i interface{}) (interface{}, error) { - var ( - ch <-chan interface{} - ok bool - ) - - if ch, ok = i.(<-chan interface{}); !ok { - return nil, e.TypeErr(ch, i) - } - - return <-ch, nil -} From a0c0355dd59f8b92f4f88e6cf6d7195d8d9976ec Mon Sep 17 00:00:00 2001 From: Steven Allen Date: Fri, 26 Oct 2018 13:02:08 -0700 Subject: [PATCH 3/3] fix pin-lock in dag put License: MIT Signed-off-by: Steven Allen --- core/commands/dag/dag.go | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/core/commands/dag/dag.go b/core/commands/dag/dag.go index 4d627f5c9..7f3b970be 100644 --- a/core/commands/dag/dag.go +++ b/core/commands/dag/dag.go @@ -13,7 +13,6 @@ import ( mh "gx/ipfs/QmPnFwZ2JXKnXgMw8CdBPxn7FWh6LLdjUjxV1fKHuJnkr8/go-multihash" path "gx/ipfs/QmRKuTyCzg7HFBcV1YUhzStroGtJSb8iWgyxfsDCwFhWTS/go-path" cmds "gx/ipfs/QmSXUokcP4TJpFfqozT69AVAYRtzXVMUjzQVkYX41R9Svs/go-ipfs-cmds" - files "gx/ipfs/QmZMWMvWMVKCbHetJ4RgndbuEF1io2UpUxwQwtNjtYPzSC/go-ipfs-files" ipld "gx/ipfs/QmdDXJs4axxefSPgK6Y1QhpJWKuDPnGJiqgq4uncb4rFHL/go-ipld-format" cmdkit "gx/ipfs/Qmde5VP1qUkyQXKCfmEUA7bP64V2HAptbJ7phuPp7jXWwg/go-ipfs-cmdkit" ) @@ -89,6 +88,10 @@ into an object of the specified format. cids := cid.NewSet() b := ipld.NewBatch(req.Context, nd.DAG) + if dopin { + defer nd.Blockstore.PinLock().Unlock() + } + for { file, err := req.Files.NextFile() if err == io.EOF { @@ -125,8 +128,6 @@ into an object of the specified format. } if dopin { - defer nd.Blockstore.PinLock().Unlock() - cids.ForEach(func(c cid.Cid) error { nd.Pinning.PinWithMode(c, pin.Recursive) return nil