path/resolver.go: simplify ResolveLinks()
License: MIT Signed-off-by: Mildred Ki'Lya <mildred-pub.git@mildred.fr>
Mildred Ki'Lya committed
Feb 24, 2016 at 08:41 UTC
7fc9205709c26a2dfb26997c283380a16874a5df
2 files changed
+9
-37
merkledag/node.go
-15
@@ -90,21 +90,6 @@ func (l *Link) GetNode(ctx context.Context, serv DAGService) (*Node, error) {
90
return serv.Get(ctx, key.Key(l.Hash))
91
}
92
93
-// GetNodeAndCache return the MDAG Node that the link points to and store a
94
-// pointer to that node along with the link to speed up further retrivals. A
95
-// timeout is to be specified to avoid taking too much time.
96
-func (l *Link) GetNodeAndCache(ctx context.Context, serv DAGService) (*Node, error) {
97
- if l.node == nil {
98
- nd, err := serv.Get(ctx, key.Key(l.Hash))
99
- if err != nil {
100
- return nil, err
101
- }
102
- l.node = nd
103
- }
104
-
105
- return l.node, nil
106
-}
107
-
93
// AddNodeLink adds a link to another node.
94
func (n *Node) AddNodeLink(name string, that *Node) error {
95
n.encoded = nil
path/resolver.go
+9
-22
@@ -111,33 +111,20 @@ func (s *Resolver) ResolveLinks(ctx context.Context, ndd *merkledag.Node, names
111
// for each of the path components
112
for _, name := range names {
113
114
- var nlink *merkledag.Link
115
- // for each of the links in nd, the current object
116
- for _, link := range nd.Links {
117
- if link.Name == name {
118
- nlink = link
119
- break
120
- }
121
- }
114
+ var cancel context.CancelFunc
115
+ ctx, cancel = context.WithTimeout(ctx, time.Minute)
116
+ defer cancel()
117
123
- if nlink == nil || len(nlink.Hash) == 0 {
118
+ nextnode, err := nd.GetLinkedNode(ctx, s.DAG, name)
119
+ if err == merkledag.ErrLinkNotFound {
120
n, _ := nd.Multihash()
121
return result, ErrNoLink{Name: name, Node: n}
122
+ } else if err != nil {
123
+ return append(result, nextnode), err
124
}
125
128
- if nlink.GetCachedNode() == nil {
129
- var cancel context.CancelFunc
130
- ctx, cancel = context.WithTimeout(ctx, time.Minute)
131
- defer cancel()
132
- }
133
-
134
- var err error
135
- nd, err = nlink.GetNodeAndCache(ctx, s.DAG)
136
- if err != nil {
137
- return append(result, nd), err
138
- }
139
-
140
- result = append(result, nd)
126
+ nd = nextnode
127
+ result = append(result, nextnode)
128
}
129
return result, nil
130
}