-
-
Notifications
You must be signed in to change notification settings - Fork 3k
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
dag: Support multiple files in put #4254
Merged
Merged
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,12 +8,15 @@ import ( | |
"strings" | ||
|
||
cmds "github.com/ipfs/go-ipfs/commands" | ||
files "github.com/ipfs/go-ipfs/commands/files" | ||
coredag "github.com/ipfs/go-ipfs/core/coredag" | ||
path "github.com/ipfs/go-ipfs/path" | ||
pin "github.com/ipfs/go-ipfs/pin" | ||
|
||
cid "gx/ipfs/QmNp85zy9RLrQ5oQD4hPyS39ezrrXpcaa7R4Y9kxdWQLLQ/go-cid" | ||
u "gx/ipfs/QmSU6eubNdhXjFBJBSksTp8kv8YRub8mGAPv8tVJHmL2EU/go-ipfs-util" | ||
mh "gx/ipfs/QmU9a9NV9RdPNwZQDYd5uKsm6N6LJLSvLbywDDYFbaaC6P/go-multihash" | ||
"reflect" | ||
) | ||
|
||
var DagCmd = &cmds.Command{ | ||
|
@@ -53,7 +56,7 @@ into an object of the specified format. | |
`, | ||
}, | ||
Arguments: []cmds.Argument{ | ||
cmds.FileArg("object data", true, false, "The object to put").EnableStdin(), | ||
cmds.FileArg("object data", true, true, "The object to put").EnableStdin(), | ||
}, | ||
Options: []cmds.Option{ | ||
cmds.StringOption("format", "f", "Format that the object will be added as.").Default("cbor"), | ||
|
@@ -68,12 +71,6 @@ into an object of the specified format. | |
return | ||
} | ||
|
||
fi, err := req.Files().NextFile() | ||
if err != nil { | ||
res.SetError(err, cmds.ErrNormal) | ||
return | ||
} | ||
|
||
ienc, _, _ := req.Option("input-enc").String() | ||
format, _, _ := req.Option("format").String() | ||
hash, _, err := req.Option("hash").String() | ||
|
@@ -100,52 +97,86 @@ into an object of the specified format. | |
defer n.Blockstore.PinLock().Unlock() | ||
} | ||
|
||
nds, err := coredag.ParseInputs(ienc, format, fi, mhType, -1) | ||
if err != nil { | ||
res.SetError(err, cmds.ErrNormal) | ||
return | ||
} | ||
if len(nds) == 0 { | ||
res.SetError(fmt.Errorf("no node returned from ParseInputs"), cmds.ErrNormal) | ||
return | ||
} | ||
outChan := make(chan interface{}, 8) | ||
res.SetOutput((<-chan interface{})(outChan)) | ||
|
||
b := n.DAG.Batch() | ||
for _, nd := range nds { | ||
_, err := b.Add(nd) | ||
if err != nil { | ||
res.SetError(err, cmds.ErrNormal) | ||
return | ||
addAllAndPin := func(f files.File) error { | ||
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") | ||
} | ||
|
||
b := n.DAG.Batch() | ||
for _, nd := range nds { | ||
_, err := b.Add(nd) | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
|
||
if err := b.Commit(); err != nil { | ||
return err | ||
} | ||
|
||
root := nds[0].Cid() | ||
if dopin { | ||
n.Pinning.PinWithMode(root, pin.Recursive) | ||
|
||
err := n.Pinning.Flush() | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
|
||
outChan <- &OutputObject{Cid: root} | ||
} | ||
} | ||
|
||
if err := b.Commit(); err != nil { | ||
res.SetError(err, cmds.ErrNormal) | ||
return | ||
return nil | ||
} | ||
|
||
root := nds[0].Cid() | ||
if dopin { | ||
n.Pinning.PinWithMode(root, pin.Recursive) | ||
|
||
err := n.Pinning.Flush() | ||
if err != nil { | ||
go func() { | ||
defer close(outChan) | ||
if err := addAllAndPin(req.Files()); err != nil { | ||
res.SetError(err, cmds.ErrNormal) | ||
return | ||
} | ||
} | ||
|
||
res.SetOutput(&OutputObject{Cid: root}) | ||
}() | ||
}, | ||
Type: OutputObject{}, | ||
Marshalers: cmds.MarshalerMap{ | ||
cmds.Text: func(res cmds.Response) (io.Reader, error) { | ||
oobj, ok := res.Output().(*OutputObject) | ||
outChan, ok := res.Output().(<-chan interface{}) | ||
if !ok { | ||
return nil, fmt.Errorf("expected a different object in marshaler") | ||
fmt.Println(reflect.TypeOf(res.Output())) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Leftovers from debugging. |
||
return nil, u.ErrCast() | ||
} | ||
|
||
marshal := func(v interface{}) (io.Reader, error) { | ||
obj, ok := v.(*OutputObject) | ||
if !ok { | ||
return nil, u.ErrCast() | ||
} | ||
|
||
return strings.NewReader(obj.Cid.String() + "\n"), nil | ||
} | ||
|
||
return strings.NewReader(oobj.Cid.String()), nil | ||
return &cmds.ChannelMarshaler{ | ||
Channel: outChan, | ||
Marshaler: marshal, | ||
Res: res, | ||
}, nil | ||
}, | ||
}, | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -153,7 +153,7 @@ test_dag_cmd() { | |
' | ||
|
||
test_expect_success "dag put with dag-pb works output looks good" ' | ||
printf $HASH > dag_put_exp && | ||
echo $HASH > dag_put_exp && | ||
test_cmp dag_put_exp dag_put_out | ||
' | ||
|
||
|
@@ -163,7 +163,20 @@ test_dag_cmd() { | |
' | ||
|
||
test_expect_success "dag put with dag-pb works output looks good" ' | ||
printf $HASH > dag_put_exp && | ||
echo $HASH > dag_put_exp && | ||
test_cmp dag_put_exp dag_put_out | ||
' | ||
|
||
test_expect_success "dag put multiple files" ' | ||
printf {\"foo\":\"bar\"} > a.json && | ||
printf {\"foo\":\"baz\"} > b.json && | ||
ipfs dag put a.json b.json > dag_put_out | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Awesome! Will test this week how many files it can get at once w/o choking. Thanks a lot for adding this! |
||
' | ||
|
||
test_expect_success "dag put multiple files output looks good" ' | ||
echo zdpuAoKMEvka7gKGSjF9B3of1F5gE5MyMMywxTC13wCmouQrf > dag_put_exp && | ||
echo zdpuAogmDEvpvGjMFsNTGDEU1JMYe6v69oxR8nG81EurmGHMj >> dag_put_exp && | ||
|
||
test_cmp dag_put_exp dag_put_out | ||
' | ||
|
||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Given that you add the blocks/pin in a go routine, this needs to go somewhere else (i.e., in the go routine). Also, if you want to improve this slightly, it's probably best not to hold this lock while waiting on the client (either receiving or sending data).