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 ipfs#7198 so we can use sessions
everywhere.
  • Loading branch information
Stebalien authored and Walter Beegle committed Jun 8, 2020
1 parent 2417324 commit 6ab1862
Showing 1 changed file with 14 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

0 comments on commit 6ab1862

Please sign in to comment.