Remove usage of merkledag.Link.Node pointer outside of merkledag
This prepares for inclusion of IPLD where the Node pointer won't be there. License: MIT Signed-off-by: Mildred Ki'Lya <mildred-pub.git@mildred.fr>
Mildred Ki'Lya committed
Oct 26, 2015 at 17:13 UTC
5808fe2edd800834efcf793dd2ad649456e75171
4 files changed
+34
-20
core/commands/ls.go
+3
-2
@@ -77,12 +77,13 @@ Displays the links an IPFS or IPNS object(s) contains, with the following format
77
Links: make([]LsLink, len(dagnode.Links)),
78
}
79
for j, link := range dagnode.Links {
80
- link.Node, err = link.GetNode(req.Context(), node.DAG)
80
+ var linkNode *merkledag.Node
81
+ linkNode, err = link.GetNode(req.Context(), node.DAG)
82
if err != nil {
83
res.SetError(err, cmds.ErrNormal)
84
return
85
}
85
- d, err := unixfs.FromBytes(link.Node.Data)
86
+ d, err := unixfs.FromBytes(linkNode.Data)
87
if err != nil {
88
res.SetError(err, cmds.ErrNormal)
89
return
core/commands/unixfs/ls.go
+4
-2
@@ -12,6 +12,7 @@ import (
12
path "github.com/ipfs/go-ipfs/path"
13
unixfs "github.com/ipfs/go-ipfs/unixfs"
14
unixfspb "github.com/ipfs/go-ipfs/unixfs/pb"
15
+ merkledag "github.com/ipfs/go-ipfs/merkledag"
16
)
17
18
type LsLink struct {
@@ -104,12 +105,13 @@ size is the IPFS link size.
105
links := make([]LsLink, len(merkleNode.Links))
106
output.Objects[hash].Links = links
107
for i, link := range merkleNode.Links {
107
- link.Node, err = link.GetNode(ctx, node.DAG)
108
+ var linkNode *merkledag.Node
109
+ linkNode, err = link.GetNode(ctx, node.DAG)
110
if err != nil {
111
res.SetError(err, cmds.ErrNormal)
112
return
113
}
112
- d, err := unixfs.FromBytes(link.Node.Data)
114
+ d, err := unixfs.FromBytes(linkNode.Data)
115
if err != nil {
116
res.SetError(err, cmds.ErrNormal)
117
return
merkledag/node.go
+21
@@ -2,6 +2,7 @@ package merkledag
2
3
import (
4
"fmt"
5
+ "time"
6
7
"gx/ipfs/QmZy2y8t9zQH2a1b8q2ZSLKp17ATuJoCNxxyMFG5qFExpt/go-net/context"
8
@@ -85,6 +86,26 @@ func (l *Link) GetNode(ctx context.Context, serv DAGService) (*Node, error) {
86
return serv.Get(ctx, key.Key(l.Hash))
87
}
88
89
+// GetNodeAndCache return the MDAG Node that the link points to and store a
90
+// pointer to that node along with the link to speed up further retrivals. A
91
+// timeout is to be specified to avoid taking too much time.
92
+func (l *Link) GetNodeAndCache(ctx context.Context, serv DAGService, timeout time.Duration) (*Node, error) {
93
+ if l.Node == nil {
94
+ if timeout != 0 {
95
+ var cancel context.CancelFunc
96
+ ctx, cancel = context.WithTimeout(ctx, time.Minute)
97
+ defer cancel()
98
+ }
99
+ nd, err := serv.Get(ctx, key.Key(l.Hash))
100
+ if err != nil {
101
+ return nil, err
102
+ }
103
+ l.Node = nd
104
+ }
105
+
106
+ return l.Node, nil
107
+}
108
+
109
// AddNodeLink adds a link to another node.
110
func (n *Node) AddNodeLink(name string, that *Node) error {
111
n.encoded = nil
path/resolver.go
+6
-16
@@ -111,37 +111,27 @@ 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 next key.Key
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 {
119
- next = key.Key(link.Hash)
118
nlink = link
119
break
120
}
121
}
122
125
- if next == "" {
123
+ if nlink == nil || len(nlink.Hash) == 0 {
124
n, _ := nd.Multihash()
125
return result, ErrNoLink{Name: name, Node: n}
126
}
127
130
- if nlink.Node == nil {
131
- // fetch object for link and assign to nd
132
- ctx, cancel := context.WithTimeout(ctx, time.Minute)
133
- defer cancel()
134
- var err error
135
- nd, err = s.DAG.Get(ctx, next)
136
- if err != nil {
137
- return append(result, nd), err
138
- }
139
- nlink.Node = nd
140
- } else {
141
- nd = nlink.Node
128
+ var err error
129
+ nd, err = nlink.GetNodeAndCache(ctx, s.DAG, time.Minute)
130
+ if err != nil {
131
+ return append(result, nd), err
132
}
133
144
- result = append(result, nlink.Node)
134
+ result = append(result, nd)
135
}
136
return result, nil
137
}