Skip to content
This repository has been archived by the owner on Jun 20, 2023. It is now read-only.

fix: don't dag.Get in ResolveToLastNode when not needed #1

Merged
merged 1 commit into from
Aug 6, 2018
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
14 changes: 9 additions & 5 deletions resolver/resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
}
}

Expand Down
36 changes: 36 additions & 0 deletions resolver/resolver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()))
}
}