Skip to content

Commit

Permalink
Avoid partly init'ing Adder by passing all needed components to NewAd…
Browse files Browse the repository at this point in the history
…der.

License: MIT
Signed-off-by: Kevin Atkinson <[email protected]>
  • Loading branch information
kevina committed May 31, 2016
1 parent 57bac4f commit f5fb3c9
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 47 deletions.
19 changes: 6 additions & 13 deletions core/commands/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,34 +164,27 @@ You can now refer to the added file in a gateway, like so:
outChan := make(chan interface{}, 8)
res.SetOutput((<-chan interface{})(outChan))

fileAdder, err := coreunix.NewAdder(req.Context(), nil, outChan)
if err != nil {
res.SetError(err, cmds.ErrNormal)
return
}

fileAdder.Pinning = n.Pinning
var fileAdder *coreunix.Adder
if nocopy || link {
fs, ok := n.Repo.SubDatastore(fsrepo.RepoFilestore).(*filestore.Datastore)
if !ok {
res.SetError(errors.New("Could not extract filestore"), cmds.ErrNormal)
return
}
fileAdder.Blockstore = filestore_support.NewBlockstore(n.Blockstore, n.Repo.Datastore(), fs)
blockService := bserv.New(fileAdder.Blockstore, n.Exchange)
blockstore := filestore_support.NewBlockstore(n.Blockstore, n.Repo.Datastore(), fs)
blockService := bserv.New(blockstore, n.Exchange)
dagService := dag.NewDAGService(blockService)
dagService.NodeToBlock = filestore_support.NodeToBlock{}
fileAdder.DAG = dagService
fileAdder, err = coreunix.NewAdder(req.Context(), n.Pinning, blockstore, dagService)
} else {
fileAdder.Blockstore = n.Blockstore
fileAdder.DAG = n.DAG
fileAdder, err = coreunix.NewAdder(req.Context(), n.Pinning, n.Blockstore, n.DAG)
}
err = fileAdder.FinishInit()
if err != nil {
res.SetError(err, cmds.ErrNormal)
return
}

fileAdder.Out = outChan
fileAdder.Chunker = chunker
fileAdder.Progress = progress
fileAdder.Hidden = hidden
Expand Down
53 changes: 20 additions & 33 deletions core/coreunix/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,48 +67,35 @@ type AddedObject struct {
Bytes int64 `json:",omitempty"`
}

func NewAdder(ctx context.Context, n *core.IpfsNode, out chan interface{}) (*Adder, error) {
adder := &Adder{
func NewAdder(ctx context.Context, p pin.Pinner, bs bstore.GCBlockstore, ds dag.DAGService) (*Adder, error) {
mr, err := mfs.NewRoot(ctx, ds, newDirNode(), nil)
if err != nil {
return nil, err
}

return &Adder{
mr: mr,
ctx: ctx,
out: out,
Pinning: p,
Blockstore: bs,
DAG: ds,
Progress: false,
Hidden: true,
Pin: true,
Trickle: false,
Wrap: false,
Chunker: "",
}


if n != nil {
adder.Pinning = n.Pinning
adder.Blockstore = n.Blockstore
adder.DAG = n.DAG
err := adder.FinishInit()
if err != nil {
return nil, err
}
}

return adder, nil
}
}, nil

func (adder *Adder) FinishInit() error {
mr, err := mfs.NewRoot(adder.ctx, adder.DAG, newDirNode(), nil)
if err != nil {
return err
}
adder.mr = mr
return nil
}

// Internal structure for holding the switches passed to the `add` call
type Adder struct {
ctx context.Context
ctx context.Context
Pinning pin.Pinner
Blockstore bstore.GCBlockstore
DAG dag.DAGService
out chan interface{}
Out chan interface{}
Progress bool
Hidden bool
Pin bool
Expand Down Expand Up @@ -252,15 +239,15 @@ func (adder *Adder) outputDirs(path string, fs mfs.FSNode) error {
}
}

return outputDagnode(adder.out, path, nd)
return outputDagnode(adder.Out, path, nd)
}

// Add builds a merkledag from the a reader, pinning all objects to the local
// datastore. Returns a key representing the root node.
func Add(n *core.IpfsNode, r io.Reader) (string, error) {
defer n.Blockstore.PinLock().Unlock()

fileAdder, err := NewAdder(n.Context(), n, nil)
fileAdder, err := NewAdder(n.Context(), n.Pinning, n.Blockstore, n.DAG)
if err != nil {
return "", err
}
Expand Down Expand Up @@ -292,7 +279,7 @@ func AddR(n *core.IpfsNode, root string) (key string, err error) {
}
defer f.Close()

fileAdder, err := NewAdder(n.Context(), n, nil)
fileAdder, err := NewAdder(n.Context(), n.Pinning, n.Blockstore, n.DAG)
if err != nil {
return "", err
}
Expand Down Expand Up @@ -321,7 +308,7 @@ func AddR(n *core.IpfsNode, root string) (key string, err error) {
// the directory, and and error if any.
func AddWrapped(n *core.IpfsNode, r io.Reader, filename string) (string, *dag.Node, error) {
file := files.NewReaderFile(filename, filename, ioutil.NopCloser(r), nil)
fileAdder, err := NewAdder(n.Context(), n, nil)
fileAdder, err := NewAdder(n.Context(), n.Pinning, n.Blockstore, n.DAG)
if err != nil {
return "", nil, err
}
Expand Down Expand Up @@ -370,7 +357,7 @@ func (adder *Adder) addNode(node *dag.Node, path string) error {
}

if !adder.Silent {
return outputDagnode(adder.out, path, node)
return outputDagnode(adder.Out, path, node)
}
return nil
}
Expand Down Expand Up @@ -416,7 +403,7 @@ func (adder *Adder) addFile(file files.File) error {
// progress updates to the client (over the output channel)
var reader io.Reader = file
if adder.Progress {
rdr := &progressReader{file: file, out: adder.out}
rdr := &progressReader{file: file, out: adder.Out}
if fi, ok := file.(files.FileInfo); ok {
reader = &progressReader2{rdr, fi}
} else {
Expand Down
3 changes: 2 additions & 1 deletion core/coreunix/add_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@ func TestAddGCLive(t *testing.T) {

errs := make(chan error)
out := make(chan interface{})
adder, err := NewAdder(context.Background(), node, out)
adder, err := NewAdder(context.Background(), node.Pinning, node.Blockstore, node.DAG)
adder.Out = out
if err != nil {
t.Fatal(err)
}
Expand Down

1 comment on commit f5fb3c9

@whyrusleeping
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 This LGTM!

Please sign in to comment.