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

Commit b999913

Browse files
committed
fix: don't dag.Get in ResolveToLastNode when not needed
1 parent cadcad1 commit b999913

File tree

2 files changed

+24
-5
lines changed

2 files changed

+24
-5
lines changed

resolver/resolver.go

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -55,14 +55,18 @@ func NewBasicResolver(ds ipld.DAGService) *Resolver {
5555
}
5656
}
5757

58-
// ResolveToLastNode walks the given path and returns the ipld.Node
59-
// referenced by the last element in it.
60-
func (r *Resolver) ResolveToLastNode(ctx context.Context, fpath path.Path) (ipld.Node, []string, error) {
58+
// ResolveToLastNode walks the given path and returns the cid of the last node
59+
// referenced by the path
60+
func (r *Resolver) ResolveToLastNode(ctx context.Context, fpath path.Path) (*cid.Cid, []string, error) {
6161
c, p, err := path.SplitAbsPath(fpath)
6262
if err != nil {
6363
return nil, nil, err
6464
}
6565

66+
if len(p) == 0 {
67+
return c, nil, nil
68+
}
69+
6670
nd, err := r.DAG.Get(ctx, c)
6771
if err != nil {
6872
return nil, nil, err
@@ -91,7 +95,7 @@ func (r *Resolver) ResolveToLastNode(ctx context.Context, fpath path.Path) (ipld
9195
}
9296

9397
if len(p) == 0 {
94-
return nd, nil, nil
98+
return nd.Cid(), nil, nil
9599
}
96100

97101
// Confirm the path exists within the object
@@ -107,7 +111,7 @@ func (r *Resolver) ResolveToLastNode(ctx context.Context, fpath path.Path) (ipld
107111
case *ipld.Link:
108112
return nil, nil, errors.New("inconsistent ResolveOnce / nd.Resolve")
109113
default:
110-
return nd, p, nil
114+
return nd.Cid(), p, nil
111115
}
112116
}
113117

resolver/resolver_test.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,4 +69,19 @@ func TestRecurivePathResolution(t *testing.T) {
6969
"recursive path resolution failed for %s: %s != %s",
7070
p.String(), key.String(), cKey.String()))
7171
}
72+
73+
rCid, rest, err := resolver.ResolveToLastNode(ctx, p)
74+
if err != nil {
75+
t.Fatal(err)
76+
}
77+
78+
if len(rest) != 0 {
79+
t.Error("expected rest to be empty")
80+
}
81+
82+
if rCid.String() != cKey.String() {
83+
t.Fatal(fmt.Errorf(
84+
"ResolveToLastNode failed for %s: %s != %s",
85+
p.String(), rCid.String(), cKey.String()))
86+
}
7287
}

0 commit comments

Comments
 (0)