mirror of
https://github.com/ipfs/kubo.git
synced 2025-06-28 00:39:31 +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))
|
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.
|
// AddNodeLink adds a link to another node.
|
||||||
func (n *Node) AddNodeLink(name string, that *Node) error {
|
func (n *Node) AddNodeLink(name string, that *Node) error {
|
||||||
n.encoded = nil
|
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 each of the path components
|
||||||
for _, name := range names {
|
for _, name := range names {
|
||||||
|
|
||||||
var nlink *merkledag.Link
|
var cancel context.CancelFunc
|
||||||
// for each of the links in nd, the current object
|
ctx, cancel = context.WithTimeout(ctx, time.Minute)
|
||||||
for _, link := range nd.Links {
|
defer cancel()
|
||||||
if link.Name == name {
|
|
||||||
nlink = link
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if nlink == nil || len(nlink.Hash) == 0 {
|
nextnode, err := nd.GetLinkedNode(ctx, s.DAG, name)
|
||||||
|
if err == merkledag.ErrLinkNotFound {
|
||||||
n, _ := nd.Multihash()
|
n, _ := nd.Multihash()
|
||||||
return result, ErrNoLink{Name: name, Node: n}
|
return result, ErrNoLink{Name: name, Node: n}
|
||||||
|
} else if err != nil {
|
||||||
|
return append(result, nextnode), err
|
||||||
}
|
}
|
||||||
|
|
||||||
if nlink.GetCachedNode() == nil {
|
nd = nextnode
|
||||||
var cancel context.CancelFunc
|
result = append(result, nextnode)
|
||||||
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)
|
|
||||||
}
|
}
|
||||||
return result, nil
|
return result, nil
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user