提交 24e31760 作者: Łukasz Magiera

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

License: MIT
Signed-off-by: 's avatarŁukasz Magiera <magik6k@gmail.com>
上级 5a42289a
package commands package commands
import ( import (
"errors"
"fmt" "fmt"
"io" "io"
"os" "os"
...@@ -19,6 +20,13 @@ import ( ...@@ -19,6 +20,13 @@ import (
// ErrDepthLimitExceeded indicates that the max depth has been exceeded. // ErrDepthLimitExceeded indicates that the max depth has been exceeded.
var ErrDepthLimitExceeded = fmt.Errorf("depth limit 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 ( const (
quietOptionName = "quiet" quietOptionName = "quiet"
quieterOptionName = "quieter" quieterOptionName = "quieter"
...@@ -210,9 +218,23 @@ You can now check what blocks have been created by: ...@@ -210,9 +218,23 @@ You can now check what blocks have been created by:
_, err = api.Unixfs().Add(req.Context, req.Files, opts...) _, err = api.Unixfs().Add(req.Context, req.Files, opts...)
}() }()
err = res.Emit(events) for event := range events {
if err != nil { output, ok := event.(*coreiface.AddEvent)
return err 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 return <-errCh
...@@ -269,7 +291,7 @@ You can now check what blocks have been created by: ...@@ -269,7 +291,7 @@ You can now check what blocks have been created by:
break LOOP break LOOP
} }
output := out.(*coreiface.AddEvent) output := out.(*AddEvent)
if len(output.Hash) > 0 { if len(output.Hash) > 0 {
lastHash = output.Hash lastHash = output.Hash
if quieter { if quieter {
...@@ -357,5 +379,5 @@ You can now check what blocks have been created by: ...@@ -357,5 +379,5 @@ You can now check what blocks have been created by:
} }
}, },
}, },
Type: coreiface.AddEvent{}, Type: AddEvent{},
} }
...@@ -6,7 +6,6 @@ import ( ...@@ -6,7 +6,6 @@ import (
"github.com/ipfs/go-ipfs/core" "github.com/ipfs/go-ipfs/core"
"github.com/ipfs/go-ipfs/core/commands/cmdenv" "github.com/ipfs/go-ipfs/core/commands/cmdenv"
coreiface "github.com/ipfs/go-ipfs/core/coreapi/interface"
tar "github.com/ipfs/go-ipfs/tar" tar "github.com/ipfs/go-ipfs/tar"
"gx/ipfs/QmZErC2Ay6WuGi96CPg316PwitdwgLo6RxZRqVjJjRj2MR/go-path" "gx/ipfs/QmZErC2Ay6WuGi96CPg316PwitdwgLo6RxZRqVjJjRj2MR/go-path"
...@@ -57,14 +56,14 @@ represent it. ...@@ -57,14 +56,14 @@ represent it.
c := node.Cid() c := node.Cid()
return cmds.EmitOnce(res, &coreiface.AddEvent{ return cmds.EmitOnce(res, &AddEvent{
Name: it.Name(), Name: it.Name(),
Hash: c.String(), Hash: c.String(),
}) })
}, },
Type: coreiface.AddEvent{}, Type: AddEvent{},
Encoders: cmds.EncoderMap{ 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) fmt.Fprintln(w, out.Hash)
return nil return nil
}), }),
......
...@@ -46,6 +46,7 @@ type ResolvedPath interface { ...@@ -46,6 +46,7 @@ type ResolvedPath interface {
// cidRoot := {"A": {"/": cidA }} // cidRoot := {"A": {"/": cidA }}
// //
// And resolve paths: // And resolve paths:
//
// * "/ipfs/${cidRoot}" // * "/ipfs/${cidRoot}"
// * Calling Cid() will return `cidRoot` // * Calling Cid() will return `cidRoot`
// * Calling Root() will return `cidRoot` // * Calling Root() will return `cidRoot`
......
...@@ -9,10 +9,9 @@ import ( ...@@ -9,10 +9,9 @@ import (
ipld "gx/ipfs/QmcKKBwfz6FyQdHR2jsXrrF6XeSBXYL86anmWNewpFpoF5/go-ipld-format" ipld "gx/ipfs/QmcKKBwfz6FyQdHR2jsXrrF6XeSBXYL86anmWNewpFpoF5/go-ipld-format"
) )
// TODO: ideas on making this more coreapi-ish without breaking the http API?
type AddEvent struct { type AddEvent struct {
Name string Name string
Hash string `json:",omitempty"` Path ResolvedPath `json:",omitempty"`
Bytes int64 `json:",omitempty"` Bytes int64 `json:",omitempty"`
Size string `json:",omitempty"` Size string `json:",omitempty"`
} }
......
...@@ -5,6 +5,7 @@ import ( ...@@ -5,6 +5,7 @@ import (
"context" "context"
"encoding/base64" "encoding/base64"
"fmt" "fmt"
"gx/ipfs/QmR8BauakNcBa3RbE4nbQu76PDiJgoQgz8AJdhJuiU4TAw/go-cid"
"io" "io"
"io/ioutil" "io/ioutil"
"math" "math"
...@@ -178,6 +179,14 @@ func TestAdd(t *testing.T) { ...@@ -178,6 +179,14 @@ func TestAdd(t *testing.T) {
t.Error(err) 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 { cases := []struct {
name string name string
data func() files.Node data func() files.Node
...@@ -406,7 +415,7 @@ func TestAdd(t *testing.T) { ...@@ -406,7 +415,7 @@ func TestAdd(t *testing.T) {
data: strFile(helloStr), data: strFile(helloStr),
path: "/ipfs/zb2rhdhmJjJZs9qkhQCpCQ7VREFkqWw3h1r8utjVvQugwHPFd", path: "/ipfs/zb2rhdhmJjJZs9qkhQCpCQ7VREFkqWw3h1r8utjVvQugwHPFd",
events: []coreiface.AddEvent{ 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)}, opts: []options.UnixfsAddOption{options.Unixfs.RawLeaves(true)},
}, },
...@@ -415,8 +424,8 @@ func TestAdd(t *testing.T) { ...@@ -415,8 +424,8 @@ func TestAdd(t *testing.T) {
data: twoLevelDir(), data: twoLevelDir(),
path: "/ipfs/QmVG2ZYCkV1S4TK8URA3a4RupBF17A8yAr4FqsRDXVJASr", path: "/ipfs/QmVG2ZYCkV1S4TK8URA3a4RupBF17A8yAr4FqsRDXVJASr",
events: []coreiface.AddEvent{ events: []coreiface.AddEvent{
{Name: "t/abc", Hash: "QmU7nuGs2djqK99UNsNgEPGh6GV4662p6WtsgccBNGTDxt", Size: "62"}, {Name: "t/abc", Path: p("QmU7nuGs2djqK99UNsNgEPGh6GV4662p6WtsgccBNGTDxt"), Size: "62"},
{Name: "t", Hash: "QmVG2ZYCkV1S4TK8URA3a4RupBF17A8yAr4FqsRDXVJASr", Size: "229"}, {Name: "t", Path: p("QmVG2ZYCkV1S4TK8URA3a4RupBF17A8yAr4FqsRDXVJASr"), Size: "229"},
}, },
wrap: "t", wrap: "t",
opts: []options.UnixfsAddOption{options.Unixfs.Silent(true)}, opts: []options.UnixfsAddOption{options.Unixfs.Silent(true)},
...@@ -426,11 +435,11 @@ func TestAdd(t *testing.T) { ...@@ -426,11 +435,11 @@ func TestAdd(t *testing.T) {
data: twoLevelDir(), data: twoLevelDir(),
path: "/ipfs/QmVG2ZYCkV1S4TK8URA3a4RupBF17A8yAr4FqsRDXVJASr", path: "/ipfs/QmVG2ZYCkV1S4TK8URA3a4RupBF17A8yAr4FqsRDXVJASr",
events: []coreiface.AddEvent{ events: []coreiface.AddEvent{
{Name: "t/abc/def", Hash: "QmNyJpQkU1cEkBwMDhDNFstr42q55mqG5GE5Mgwug4xyGk", Size: "13"}, {Name: "t/abc/def", Path: p("QmNyJpQkU1cEkBwMDhDNFstr42q55mqG5GE5Mgwug4xyGk"), Size: "13"},
{Name: "t/bar", Hash: "QmS21GuXiRMvJKHos4ZkEmQDmRBqRaF5tQS2CQCu2ne9sY", Size: "14"}, {Name: "t/bar", Path: p("QmS21GuXiRMvJKHos4ZkEmQDmRBqRaF5tQS2CQCu2ne9sY"), Size: "14"},
{Name: "t/foo", Hash: "QmfAjGiVpTN56TXi6SBQtstit5BEw3sijKj1Qkxn6EXKzJ", Size: "14"}, {Name: "t/foo", Path: p("QmfAjGiVpTN56TXi6SBQtstit5BEw3sijKj1Qkxn6EXKzJ"), Size: "14"},
{Name: "t/abc", Hash: "QmU7nuGs2djqK99UNsNgEPGh6GV4662p6WtsgccBNGTDxt", Size: "62"}, {Name: "t/abc", Path: p("QmU7nuGs2djqK99UNsNgEPGh6GV4662p6WtsgccBNGTDxt"), Size: "62"},
{Name: "t", Hash: "QmVG2ZYCkV1S4TK8URA3a4RupBF17A8yAr4FqsRDXVJASr", Size: "229"}, {Name: "t", Path: p("QmVG2ZYCkV1S4TK8URA3a4RupBF17A8yAr4FqsRDXVJASr"), Size: "229"},
}, },
wrap: "t", wrap: "t",
}, },
...@@ -445,7 +454,7 @@ func TestAdd(t *testing.T) { ...@@ -445,7 +454,7 @@ func TestAdd(t *testing.T) {
{Name: "", Bytes: 524288}, {Name: "", Bytes: 524288},
{Name: "", Bytes: 786432}, {Name: "", Bytes: 786432},
{Name: "", Bytes: 1000000}, {Name: "", Bytes: 1000000},
{Name: "QmXXNNbwe4zzpdMg62ZXvnX1oU7MwSrQ3vAEtuwFKCm1oD", Hash: "QmXXNNbwe4zzpdMg62ZXvnX1oU7MwSrQ3vAEtuwFKCm1oD", Size: "1000256"}, {Name: "QmXXNNbwe4zzpdMg62ZXvnX1oU7MwSrQ3vAEtuwFKCm1oD", Path: p("QmXXNNbwe4zzpdMg62ZXvnX1oU7MwSrQ3vAEtuwFKCm1oD"), Size: "1000256"},
}, },
wrap: "", wrap: "",
opts: []options.UnixfsAddOption{options.Unixfs.Progress(true)}, opts: []options.UnixfsAddOption{options.Unixfs.Progress(true)},
...@@ -497,8 +506,12 @@ func TestAdd(t *testing.T) { ...@@ -497,8 +506,12 @@ func TestAdd(t *testing.T) {
t.Errorf("Event.Name didn't match, %s != %s", expected[0].Name, event.Name) t.Errorf("Event.Name didn't match, %s != %s", expected[0].Name, event.Name)
} }
if expected[0].Hash != event.Hash { if expected[0].Path != nil && event.Path != nil {
t.Errorf("Event.Hash didn't match, %s != %s", expected[0].Hash, event.Hash) 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 { if expected[0].Bytes != event.Bytes {
t.Errorf("Event.Bytes didn't match, %d != %d", expected[0].Bytes, event.Bytes) t.Errorf("Event.Bytes didn't match, %d != %d", expected[0].Bytes, event.Bytes)
......
...@@ -41,12 +41,6 @@ type Link struct { ...@@ -41,12 +41,6 @@ type Link struct {
Size uint64 Size uint64
} }
type Object struct {
Hash string
Links []Link
Size string
}
// NewAdder Returns a new Adder used for a file add operation. // 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) { func NewAdder(ctx context.Context, p pin.Pinner, bs bstore.GCLocker, ds ipld.DAGService) (*Adder, error) {
bufferedDS := ipld.NewBufferedDAG(ctx, ds) bufferedDS := ipld.NewBufferedDAG(ctx, ds)
...@@ -580,7 +574,7 @@ func outputDagnode(out chan<- interface{}, name string, dn ipld.Node) error { ...@@ -580,7 +574,7 @@ func outputDagnode(out chan<- interface{}, name string, dn ipld.Node) error {
} }
out <- &coreiface.AddEvent{ out <- &coreiface.AddEvent{
Hash: o.Hash, Path: o.Path,
Name: name, Name: name,
Size: o.Size, Size: o.Size,
} }
...@@ -589,24 +583,16 @@ func outputDagnode(out chan<- interface{}, name string, dn ipld.Node) error { ...@@ -589,24 +583,16 @@ func outputDagnode(out chan<- interface{}, name string, dn ipld.Node) error {
} }
// from core/commands/object.go // from core/commands/object.go
func getOutput(dagnode ipld.Node) (*Object, error) { func getOutput(dagnode ipld.Node) (*coreiface.AddEvent, error) {
c := dagnode.Cid() c := dagnode.Cid()
s, err := dagnode.Size() s, err := dagnode.Size()
if err != nil { if err != nil {
return nil, err return nil, err
} }
output := &Object{ output := &coreiface.AddEvent{
Hash: c.String(), Path: coreiface.IpfsPath(c),
Size: strconv.FormatUint(s, 10), 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,
}
} }
return output, nil return output, nil
......
...@@ -100,7 +100,7 @@ func TestAddGCLive(t *testing.T) { ...@@ -100,7 +100,7 @@ func TestAddGCLive(t *testing.T) {
addedHashes := make(map[string]struct{}) addedHashes := make(map[string]struct{})
select { select {
case o := <-out: case o := <-out:
addedHashes[o.(*coreiface.AddEvent).Hash] = struct{}{} addedHashes[o.(*coreiface.AddEvent).Path.Cid().String()] = struct{}{}
case <-addDone: case <-addDone:
t.Fatal("add shouldnt complete yet") t.Fatal("add shouldnt complete yet")
} }
...@@ -128,7 +128,7 @@ func TestAddGCLive(t *testing.T) { ...@@ -128,7 +128,7 @@ func TestAddGCLive(t *testing.T) {
// receive next object from adder // receive next object from adder
o := <-out o := <-out
addedHashes[o.(*coreiface.AddEvent).Hash] = struct{}{} addedHashes[o.(*coreiface.AddEvent).Path.Cid().String()] = struct{}{}
<-gcstarted <-gcstarted
...@@ -144,7 +144,7 @@ func TestAddGCLive(t *testing.T) { ...@@ -144,7 +144,7 @@ func TestAddGCLive(t *testing.T) {
var last cid.Cid var last cid.Cid
for a := range out { for a := range out {
// wait for it to finish // 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 { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论