From 9745704f5fc1c6d59e0c73716e243d1dbbe6f0c2 Mon Sep 17 00:00:00 2001 From: Stephen Whitmore <stephen.whitmore@gmail.com> Date: Sun, 24 Jan 2016 23:31:05 -0800 Subject: [PATCH] Slightly optimizes core.ResolveToKey(). This is done by skipping the step of resolving the final segment in the path to a DAG node; instead preferring to look at the second-to-last segmenet's links. License: MIT Signed-off-by: Stephen Whitmore <noffle@ipfs.io> --- core/pathresolver.go | 16 +++++++++++----- test/sharness/t0081-repo-pinning.sh | 2 ++ 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/core/pathresolver.go b/core/pathresolver.go index 515899969..fce67b11d 100644 --- a/core/pathresolver.go +++ b/core/pathresolver.go @@ -71,16 +71,22 @@ func ResolveToKey(ctx context.Context, n *IpfsNode, p path.Path) (key.Key, error return key.B58KeyDecode(p.Segments()[1]), nil } - // Fall back onto regular dagnode resolution. - dagnode, err := Resolve(ctx, n, p) + // Fall back onto regular dagnode resolution. Retrieve the second-to-last + // segment of the path and resolve its link to the last segment. + head, tail, err := p.PopLastSegment() + if err != nil { + return key.Key(""), err + } + dagnode, err := Resolve(ctx, n, head) if err != nil { return key.Key(""), err } - // Extract and return the node's key. - k, err := dagnode.Key() + // Extract and return the key of the link to the target dag node. + link, err := dagnode.GetNodeLink(tail) if err != nil { return key.Key(""), err } - return k, nil + + return key.Key(link.Hash), nil } diff --git a/test/sharness/t0081-repo-pinning.sh b/test/sharness/t0081-repo-pinning.sh index be0e2e7ea..ca5db950c 100755 --- a/test/sharness/t0081-repo-pinning.sh +++ b/test/sharness/t0081-repo-pinning.sh @@ -283,6 +283,8 @@ FICTIONAL_HASH="QmXV4f9v8a56MxWKBhP3ETsz4EaafudU1cKfPaaJnenc48" test_launch_ipfs_daemon test_expect_success "test unpinning a hash that's not pinned" " test_expect_code 1 ipfs pin rm $FICTIONAL_HASH --timeout=5s + test_expect_code 1 ipfs pin rm $FICTIONAL_HASH/a --timeout=5s + test_expect_code 1 ipfs pin rm $FICTIONAL_HASH/a/b --timeout=5s " test_kill_ipfs_daemon -- GitLab