mirror of
https://github.com/ipfs/kubo.git
synced 2025-06-27 16:07:42 +08:00
path/resolver.go: simplify ResolveLinks()
License: MIT Signed-off-by: Mildred Ki'Lya <mildred-pub.git@mildred.fr>
This commit is contained in:
@ -90,21 +90,6 @@ func (l *Link) GetNode(ctx context.Context, serv DAGService) (*Node, error) {
|
||||
return serv.Get(ctx, key.Key(l.Hash))
|
||||
}
|
||||
|
||||
// GetNodeAndCache return the MDAG Node that the link points to and store a
|
||||
// pointer to that node along with the link to speed up further retrivals. A
|
||||
// timeout is to be specified to avoid taking too much time.
|
||||
func (l *Link) GetNodeAndCache(ctx context.Context, serv DAGService) (*Node, error) {
|
||||
if l.node == nil {
|
||||
nd, err := serv.Get(ctx, key.Key(l.Hash))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
l.node = nd
|
||||
}
|
||||
|
||||
return l.node, nil
|
||||
}
|
||||
|
||||
// AddNodeLink adds a link to another node.
|
||||
func (n *Node) AddNodeLink(name string, that *Node) error {
|
||||
n.encoded = nil
|
||||
|
@ -111,33 +111,20 @@ func (s *Resolver) ResolveLinks(ctx context.Context, ndd *merkledag.Node, names
|
||||
// for each of the path components
|
||||
for _, name := range names {
|
||||
|
||||
var nlink *merkledag.Link
|
||||
// for each of the links in nd, the current object
|
||||
for _, link := range nd.Links {
|
||||
if link.Name == name {
|
||||
nlink = link
|
||||
break
|
||||
}
|
||||
}
|
||||
var cancel context.CancelFunc
|
||||
ctx, cancel = context.WithTimeout(ctx, time.Minute)
|
||||
defer cancel()
|
||||
|
||||
if nlink == nil || len(nlink.Hash) == 0 {
|
||||
nextnode, err := nd.GetLinkedNode(ctx, s.DAG, name)
|
||||
if err == merkledag.ErrLinkNotFound {
|
||||
n, _ := nd.Multihash()
|
||||
return result, ErrNoLink{Name: name, Node: n}
|
||||
} else if err != nil {
|
||||
return append(result, nextnode), err
|
||||
}
|
||||
|
||||
if nlink.GetCachedNode() == nil {
|
||||
var cancel context.CancelFunc
|
||||
ctx, cancel = context.WithTimeout(ctx, time.Minute)
|
||||
defer cancel()
|
||||
}
|
||||
|
||||
var err error
|
||||
nd, err = nlink.GetNodeAndCache(ctx, s.DAG)
|
||||
if err != nil {
|
||||
return append(result, nd), err
|
||||
}
|
||||
|
||||
result = append(result, nd)
|
||||
nd = nextnode
|
||||
result = append(result, nextnode)
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
Reference in New Issue
Block a user