Skip to content

Commit

Permalink
fix: use bitswap sessions for ipfs refs
Browse files Browse the repository at this point in the history
This isn't perfect (we only use sessions after resolving the root cid) but it's
better than what we have. The real solution is #7198 so we can use sessions
everywhere.
  • Loading branch information
Stebalien committed May 29, 2020
1 parent 765c456 commit 62f61c5
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 8 deletions.
22 changes: 14 additions & 8 deletions core/commands/refs.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
cidenc "github.com/ipfs/go-cidutil/cidenc"
cmds "github.com/ipfs/go-ipfs-cmds"
ipld "github.com/ipfs/go-ipld-format"
merkledag "github.com/ipfs/go-merkledag"
iface "github.com/ipfs/interface-go-ipfs-core"
path "github.com/ipfs/interface-go-ipfs-core/path"
)
Expand Down Expand Up @@ -102,14 +103,15 @@ NOTE: List all references recursively by using the flag '-r'.
format = "<src> -> <dst>"
}

// TODO: use session for resolving as well.
objs, err := objectsForPaths(ctx, api, req.Arguments)
if err != nil {
return err
}

rw := RefWriter{
res: res,
DAG: api.Dag(),
DAG: merkledag.NewSession(ctx, api.Dag()),
Ctx: ctx,
Unique: unique,
PrintFmt: format,
Expand Down Expand Up @@ -164,16 +166,16 @@ Displays the hashes of all local objects.
Type: RefWrapper{},
}

func objectsForPaths(ctx context.Context, n iface.CoreAPI, paths []string) ([]ipld.Node, error) {
objects := make([]ipld.Node, len(paths))
func objectsForPaths(ctx context.Context, n iface.CoreAPI, paths []string) ([]cid.Cid, error) {
roots := make([]cid.Cid, len(paths))
for i, sp := range paths {
o, err := n.ResolveNode(ctx, path.New(sp))
o, err := n.ResolvePath(ctx, path.New(sp))
if err != nil {
return nil, err
}
objects[i] = o
roots[i] = o.Cid()
}
return objects, nil
return roots, nil
}

type RefWrapper struct {
Expand All @@ -183,7 +185,7 @@ type RefWrapper struct {

type RefWriter struct {
res cmds.ResponseEmitter
DAG ipld.DAGService
DAG ipld.NodeGetter
Ctx context.Context

Unique bool
Expand All @@ -194,7 +196,11 @@ type RefWriter struct {
}

// WriteRefs writes refs of the given object to the underlying writer.
func (rw *RefWriter) WriteRefs(n ipld.Node, enc cidenc.Encoder) (int, error) {
func (rw *RefWriter) WriteRefs(c cid.Cid, enc cidenc.Encoder) (int, error) {
n, err := rw.DAG.Get(rw.Ctx, c)
if err != nil {
return 0, err
}
return rw.writeRefsRecursive(n, 0, enc)
}

Expand Down
10 changes: 10 additions & 0 deletions core/coreapi/dag.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
cid "github.com/ipfs/go-cid"
"github.com/ipfs/go-ipfs-pinner"
ipld "github.com/ipfs/go-ipld-format"
dag "github.com/ipfs/go-merkledag"
)

type dagAPI struct {
Expand Down Expand Up @@ -50,3 +51,12 @@ func (adder *pinningAdder) AddMany(ctx context.Context, nds []ipld.Node) error {
func (api *dagAPI) Pinning() ipld.NodeAdder {
return (*pinningAdder)(api.core)
}

func (api *dagAPI) Session(ctx context.Context) ipld.NodeGetter {
return dag.NewSession(ctx, api.DAGService)
}

var (
_ ipld.DAGService = (*dagAPI)(nil)
_ dag.SessionMaker = (*dagAPI)(nil)
)

0 comments on commit 62f61c5

Please sign in to comment.