Unverified Commit 34251f0e authored by Whyrusleeping's avatar Whyrusleeping Committed by GitHub

Merge pull request #1 from ipfs/fix/resolve-last-cid

fix: don't dag.Get in ResolveToLastNode when not needed
parents ca783f9d 29e9e4c0
......@@ -55,14 +55,18 @@ func NewBasicResolver(ds ipld.DAGService) *Resolver {
}
}
// ResolveToLastNode walks the given path and returns the ipld.Node
// referenced by the last element in it.
func (r *Resolver) ResolveToLastNode(ctx context.Context, fpath path.Path) (ipld.Node, []string, error) {
// ResolveToLastNode walks the given path and returns the cid of the last node
// referenced by the path
func (r *Resolver) ResolveToLastNode(ctx context.Context, fpath path.Path) (*cid.Cid, []string, error) {
c, p, err := path.SplitAbsPath(fpath)
if err != nil {
return nil, nil, err
}
if len(p) == 0 {
return c, nil, nil
}
nd, err := r.DAG.Get(ctx, c)
if err != nil {
return nil, nil, err
......@@ -91,7 +95,7 @@ func (r *Resolver) ResolveToLastNode(ctx context.Context, fpath path.Path) (ipld
}
if len(p) == 0 {
return nd, nil, nil
return nd.Cid(), nil, nil
}
// Confirm the path exists within the object
......@@ -107,7 +111,7 @@ func (r *Resolver) ResolveToLastNode(ctx context.Context, fpath path.Path) (ipld
case *ipld.Link:
return nil, nil, errors.New("inconsistent ResolveOnce / nd.Resolve")
default:
return nd, p, nil
return nd.Cid(), p, nil
}
}
......
......@@ -69,4 +69,40 @@ func TestRecurivePathResolution(t *testing.T) {
"recursive path resolution failed for %s: %s != %s",
p.String(), key.String(), cKey.String()))
}
rCid, rest, err := resolver.ResolveToLastNode(ctx, p)
if err != nil {
t.Fatal(err)
}
if len(rest) != 0 {
t.Error("expected rest to be empty")
}
if rCid.String() != cKey.String() {
t.Fatal(fmt.Errorf(
"ResolveToLastNode failed for %s: %s != %s",
p.String(), rCid.String(), cKey.String()))
}
p2, err := path.FromSegments("/ipfs/", aKey.String())
if err != nil {
t.Fatal(err)
}
rCid, rest, err = resolver.ResolveToLastNode(ctx, p2)
if err != nil {
t.Fatal(err)
}
if len(rest) != 0 {
t.Error("expected rest to be empty")
}
if rCid.String() != aKey.String() {
t.Fatal(fmt.Errorf(
"ResolveToLastNode failed for %s: %s != %s",
p.String(), rCid.String(), cKey.String()))
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment