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

coreapi/unixfs: Use path instead of raw hash in AddEvent #5854

Merged
merged 1 commit into from
Jan 3, 2019
Merged
Show file tree
Hide file tree
Changes from all 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
32 changes: 27 additions & 5 deletions core/commands/add.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package commands

import (
"errors"
"fmt"
"io"
"os"
Expand All @@ -19,6 +20,13 @@ import (
// ErrDepthLimitExceeded indicates that the max depth has been exceeded.
var ErrDepthLimitExceeded = fmt.Errorf("depth limit exceeded")

type AddEvent struct {
Name string
Hash string `json:",omitempty"`
Bytes int64 `json:",omitempty"`
Size string `json:",omitempty"`
}

const (
quietOptionName = "quiet"
quieterOptionName = "quieter"
Expand Down Expand Up @@ -210,9 +218,23 @@ You can now check what blocks have been created by:
_, err = api.Unixfs().Add(req.Context, req.Files, opts...)
}()

err = res.Emit(events)
if err != nil {
return err
for event := range events {
output, ok := event.(*coreiface.AddEvent)
if !ok {
return errors.New("unknown event type")
}

h := ""
if output.Path != nil {
h = output.Path.Cid().String()
}

res.Emit(&AddEvent{
Name: output.Name,
Hash: h,
Bytes: output.Bytes,
Size: output.Size,
})
}

return <-errCh
Expand Down Expand Up @@ -269,7 +291,7 @@ You can now check what blocks have been created by:

break LOOP
}
output := out.(*coreiface.AddEvent)
output := out.(*AddEvent)
if len(output.Hash) > 0 {
lastHash = output.Hash
if quieter {
Expand Down Expand Up @@ -357,5 +379,5 @@ You can now check what blocks have been created by:
}
},
},
Type: coreiface.AddEvent{},
Type: AddEvent{},
}
7 changes: 3 additions & 4 deletions core/commands/tar.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (

"github.com/ipfs/go-ipfs/core"
"github.com/ipfs/go-ipfs/core/commands/cmdenv"
coreiface "github.com/ipfs/go-ipfs/core/coreapi/interface"
tar "github.com/ipfs/go-ipfs/tar"

"gx/ipfs/QmZErC2Ay6WuGi96CPg316PwitdwgLo6RxZRqVjJjRj2MR/go-path"
Expand Down Expand Up @@ -57,14 +56,14 @@ represent it.

c := node.Cid()

return cmds.EmitOnce(res, &coreiface.AddEvent{
return cmds.EmitOnce(res, &AddEvent{
Name: it.Name(),
Hash: c.String(),
})
},
Type: coreiface.AddEvent{},
Type: AddEvent{},
Encoders: cmds.EncoderMap{
cmds.Text: cmds.MakeTypedEncoder(func(req *cmds.Request, w io.Writer, out *coreiface.AddEvent) error {
cmds.Text: cmds.MakeTypedEncoder(func(req *cmds.Request, w io.Writer, out *AddEvent) error {
fmt.Fprintln(w, out.Hash)
return nil
}),
Expand Down
1 change: 1 addition & 0 deletions core/coreapi/interface/path.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ type ResolvedPath interface {
// cidRoot := {"A": {"/": cidA }}
//
// And resolve paths:
//
// * "/ipfs/${cidRoot}"
// * Calling Cid() will return `cidRoot`
// * Calling Root() will return `cidRoot`
Expand Down
7 changes: 3 additions & 4 deletions core/coreapi/interface/unixfs.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,11 @@ import (
ipld "gx/ipfs/QmcKKBwfz6FyQdHR2jsXrrF6XeSBXYL86anmWNewpFpoF5/go-ipld-format"
)

// TODO: ideas on making this more coreapi-ish without breaking the http API?
type AddEvent struct {
Name string
Hash string `json:",omitempty"`
Bytes int64 `json:",omitempty"`
Size string `json:",omitempty"`
Path ResolvedPath `json:",omitempty"`
Bytes int64 `json:",omitempty"`
Size string `json:",omitempty"`
}

// UnixfsAPI is the basic interface to immutable files in IPFS
Expand Down
35 changes: 24 additions & 11 deletions core/coreapi/unixfs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"context"
"encoding/base64"
"fmt"
"gx/ipfs/QmR8BauakNcBa3RbE4nbQu76PDiJgoQgz8AJdhJuiU4TAw/go-cid"
"io"
"io/ioutil"
"math"
Expand Down Expand Up @@ -178,6 +179,14 @@ func TestAdd(t *testing.T) {
t.Error(err)
}

p := func(h string) coreiface.ResolvedPath {
c, err := cid.Parse(h)
if err != nil {
t.Fatal(err)
}
return coreiface.IpfsPath(c)
}

cases := []struct {
name string
data func() files.Node
Expand Down Expand Up @@ -406,7 +415,7 @@ func TestAdd(t *testing.T) {
data: strFile(helloStr),
path: "/ipfs/zb2rhdhmJjJZs9qkhQCpCQ7VREFkqWw3h1r8utjVvQugwHPFd",
events: []coreiface.AddEvent{
{Name: "zb2rhdhmJjJZs9qkhQCpCQ7VREFkqWw3h1r8utjVvQugwHPFd", Hash: "zb2rhdhmJjJZs9qkhQCpCQ7VREFkqWw3h1r8utjVvQugwHPFd", Size: strconv.Itoa(len(helloStr))},
{Name: "zb2rhdhmJjJZs9qkhQCpCQ7VREFkqWw3h1r8utjVvQugwHPFd", Path: p("zb2rhdhmJjJZs9qkhQCpCQ7VREFkqWw3h1r8utjVvQugwHPFd"), Size: strconv.Itoa(len(helloStr))},
},
opts: []options.UnixfsAddOption{options.Unixfs.RawLeaves(true)},
},
Expand All @@ -415,8 +424,8 @@ func TestAdd(t *testing.T) {
data: twoLevelDir(),
path: "/ipfs/QmVG2ZYCkV1S4TK8URA3a4RupBF17A8yAr4FqsRDXVJASr",
events: []coreiface.AddEvent{
{Name: "t/abc", Hash: "QmU7nuGs2djqK99UNsNgEPGh6GV4662p6WtsgccBNGTDxt", Size: "62"},
{Name: "t", Hash: "QmVG2ZYCkV1S4TK8URA3a4RupBF17A8yAr4FqsRDXVJASr", Size: "229"},
{Name: "t/abc", Path: p("QmU7nuGs2djqK99UNsNgEPGh6GV4662p6WtsgccBNGTDxt"), Size: "62"},
{Name: "t", Path: p("QmVG2ZYCkV1S4TK8URA3a4RupBF17A8yAr4FqsRDXVJASr"), Size: "229"},
},
wrap: "t",
opts: []options.UnixfsAddOption{options.Unixfs.Silent(true)},
Expand All @@ -426,11 +435,11 @@ func TestAdd(t *testing.T) {
data: twoLevelDir(),
path: "/ipfs/QmVG2ZYCkV1S4TK8URA3a4RupBF17A8yAr4FqsRDXVJASr",
events: []coreiface.AddEvent{
{Name: "t/abc/def", Hash: "QmNyJpQkU1cEkBwMDhDNFstr42q55mqG5GE5Mgwug4xyGk", Size: "13"},
{Name: "t/bar", Hash: "QmS21GuXiRMvJKHos4ZkEmQDmRBqRaF5tQS2CQCu2ne9sY", Size: "14"},
{Name: "t/foo", Hash: "QmfAjGiVpTN56TXi6SBQtstit5BEw3sijKj1Qkxn6EXKzJ", Size: "14"},
{Name: "t/abc", Hash: "QmU7nuGs2djqK99UNsNgEPGh6GV4662p6WtsgccBNGTDxt", Size: "62"},
{Name: "t", Hash: "QmVG2ZYCkV1S4TK8URA3a4RupBF17A8yAr4FqsRDXVJASr", Size: "229"},
{Name: "t/abc/def", Path: p("QmNyJpQkU1cEkBwMDhDNFstr42q55mqG5GE5Mgwug4xyGk"), Size: "13"},
{Name: "t/bar", Path: p("QmS21GuXiRMvJKHos4ZkEmQDmRBqRaF5tQS2CQCu2ne9sY"), Size: "14"},
{Name: "t/foo", Path: p("QmfAjGiVpTN56TXi6SBQtstit5BEw3sijKj1Qkxn6EXKzJ"), Size: "14"},
{Name: "t/abc", Path: p("QmU7nuGs2djqK99UNsNgEPGh6GV4662p6WtsgccBNGTDxt"), Size: "62"},
{Name: "t", Path: p("QmVG2ZYCkV1S4TK8URA3a4RupBF17A8yAr4FqsRDXVJASr"), Size: "229"},
},
wrap: "t",
},
Expand All @@ -445,7 +454,7 @@ func TestAdd(t *testing.T) {
{Name: "", Bytes: 524288},
{Name: "", Bytes: 786432},
{Name: "", Bytes: 1000000},
{Name: "QmXXNNbwe4zzpdMg62ZXvnX1oU7MwSrQ3vAEtuwFKCm1oD", Hash: "QmXXNNbwe4zzpdMg62ZXvnX1oU7MwSrQ3vAEtuwFKCm1oD", Size: "1000256"},
{Name: "QmXXNNbwe4zzpdMg62ZXvnX1oU7MwSrQ3vAEtuwFKCm1oD", Path: p("QmXXNNbwe4zzpdMg62ZXvnX1oU7MwSrQ3vAEtuwFKCm1oD"), Size: "1000256"},
},
wrap: "",
opts: []options.UnixfsAddOption{options.Unixfs.Progress(true)},
Expand Down Expand Up @@ -497,8 +506,12 @@ func TestAdd(t *testing.T) {
t.Errorf("Event.Name didn't match, %s != %s", expected[0].Name, event.Name)
}

if expected[0].Hash != event.Hash {
t.Errorf("Event.Hash didn't match, %s != %s", expected[0].Hash, event.Hash)
if expected[0].Path != nil && event.Path != nil {
if expected[0].Path.Cid().String() != event.Path.Cid().String() {
t.Errorf("Event.Hash didn't match, %s != %s", expected[0].Path, event.Path)
}
} else if event.Path != expected[0].Path {
t.Errorf("Event.Hash didn't match, %s != %s", expected[0].Path, event.Path)
}
if expected[0].Bytes != event.Bytes {
t.Errorf("Event.Bytes didn't match, %d != %d", expected[0].Bytes, event.Bytes)
Expand Down
24 changes: 5 additions & 19 deletions core/coreunix/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,6 @@ type Link struct {
Size uint64
}

type Object struct {
Hash string
Links []Link
Size string
}

// NewAdder Returns a new Adder used for a file add operation.
func NewAdder(ctx context.Context, p pin.Pinner, bs bstore.GCLocker, ds ipld.DAGService) (*Adder, error) {
bufferedDS := ipld.NewBufferedDAG(ctx, ds)
Expand Down Expand Up @@ -580,7 +574,7 @@ func outputDagnode(out chan<- interface{}, name string, dn ipld.Node) error {
}

out <- &coreiface.AddEvent{
Hash: o.Hash,
Path: o.Path,
Name: name,
Size: o.Size,
}
Expand All @@ -589,24 +583,16 @@ func outputDagnode(out chan<- interface{}, name string, dn ipld.Node) error {
}

// from core/commands/object.go
func getOutput(dagnode ipld.Node) (*Object, error) {
func getOutput(dagnode ipld.Node) (*coreiface.AddEvent, error) {
c := dagnode.Cid()
s, err := dagnode.Size()
if err != nil {
return nil, err
}

output := &Object{
Hash: c.String(),
Size: strconv.FormatUint(s, 10),
Links: make([]Link, len(dagnode.Links())),
}

for i, link := range dagnode.Links() {
output.Links[i] = Link{
Name: link.Name,
Size: link.Size,
}
output := &coreiface.AddEvent{
Path: coreiface.IpfsPath(c),
Size: strconv.FormatUint(s, 10),
}

return output, nil
Expand Down
6 changes: 3 additions & 3 deletions core/coreunix/add_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ func TestAddGCLive(t *testing.T) {
addedHashes := make(map[string]struct{})
select {
case o := <-out:
addedHashes[o.(*coreiface.AddEvent).Hash] = struct{}{}
addedHashes[o.(*coreiface.AddEvent).Path.Cid().String()] = struct{}{}
case <-addDone:
t.Fatal("add shouldnt complete yet")
}
Expand Down Expand Up @@ -128,7 +128,7 @@ func TestAddGCLive(t *testing.T) {

// receive next object from adder
o := <-out
addedHashes[o.(*coreiface.AddEvent).Hash] = struct{}{}
addedHashes[o.(*coreiface.AddEvent).Path.Cid().String()] = struct{}{}

<-gcstarted

Expand All @@ -144,7 +144,7 @@ func TestAddGCLive(t *testing.T) {
var last cid.Cid
for a := range out {
// wait for it to finish
c, err := cid.Decode(a.(*coreiface.AddEvent).Hash)
c, err := cid.Decode(a.(*coreiface.AddEvent).Path.Cid().String())
if err != nil {
t.Fatal(err)
}
Expand Down