-
Notifications
You must be signed in to change notification settings - Fork 0
/
node.go
329 lines (267 loc) · 6.74 KB
/
node.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
package merkledag
import (
"context"
"encoding/json"
"fmt"
node "gx/ipfs/QmRSU5EqqWVZSNdbU51yXmVoF1uNw3JgTNB6RaiL7DZM16/go-ipld-node"
mh "gx/ipfs/QmYDds3421prZgqKbLpEK7T9Aa2eVdQ7o3YarX1LVLdP2J/go-multihash"
cid "gx/ipfs/QmcTcsTvfaeEBRFo1TkFgT8sRmgi1n1LTZpecfVP8fzpGD/go-cid"
)
var ErrNotProtobuf = fmt.Errorf("expected protobuf dag node")
var ErrLinkNotFound = fmt.Errorf("no link by that name")
// Node represents a node in the IPFS Merkle DAG.
// nodes have opaque data and a set of navigable links.
type ProtoNode struct {
links []*node.Link
data []byte
// cache encoded/marshaled value
encoded []byte
cached *cid.Cid
// Prefix specifies cid version and hashing function
Prefix cid.Prefix
}
var defaultCidPrefix = cid.Prefix{
Codec: cid.DagProtobuf,
MhLength: -1,
MhType: mh.SHA2_256,
Version: 0,
}
type LinkSlice []*node.Link
func (ls LinkSlice) Len() int { return len(ls) }
func (ls LinkSlice) Swap(a, b int) { ls[a], ls[b] = ls[b], ls[a] }
func (ls LinkSlice) Less(a, b int) bool { return ls[a].Name < ls[b].Name }
func NodeWithData(d []byte) *ProtoNode {
return &ProtoNode{data: d}
}
// AddNodeLink adds a link to another node.
func (n *ProtoNode) AddNodeLink(name string, that node.Node) error {
n.encoded = nil
lnk, err := node.MakeLink(that)
if err != nil {
return err
}
lnk.Name = name
n.AddRawLink(name, lnk)
return nil
}
// AddNodeLinkClean adds a link to another node. without keeping a reference to
// the child node
func (n *ProtoNode) AddNodeLinkClean(name string, that node.Node) error {
n.encoded = nil
lnk, err := node.MakeLink(that)
if err != nil {
return err
}
n.AddRawLink(name, lnk)
return nil
}
// AddRawLink adds a copy of a link to this node
func (n *ProtoNode) AddRawLink(name string, l *node.Link) error {
n.encoded = nil
n.links = append(n.links, &node.Link{
Name: name,
Size: l.Size,
Cid: l.Cid,
})
return nil
}
// Remove a link on this node by the given name
func (n *ProtoNode) RemoveNodeLink(name string) error {
n.encoded = nil
good := make([]*node.Link, 0, len(n.links))
var found bool
for _, l := range n.links {
if l.Name != name {
good = append(good, l)
} else {
found = true
}
}
n.links = good
if !found {
return ErrNotFound
}
return nil
}
// Return a copy of the link with given name
func (n *ProtoNode) GetNodeLink(name string) (*node.Link, error) {
for _, l := range n.links {
if l.Name == name {
return &node.Link{
Name: l.Name,
Size: l.Size,
Cid: l.Cid,
}, nil
}
}
return nil, ErrLinkNotFound
}
func (n *ProtoNode) GetLinkedProtoNode(ctx context.Context, ds DAGService, name string) (*ProtoNode, error) {
nd, err := n.GetLinkedNode(ctx, ds, name)
if err != nil {
return nil, err
}
pbnd, ok := nd.(*ProtoNode)
if !ok {
return nil, ErrNotProtobuf
}
return pbnd, nil
}
func (n *ProtoNode) GetLinkedNode(ctx context.Context, ds DAGService, name string) (node.Node, error) {
lnk, err := n.GetNodeLink(name)
if err != nil {
return nil, err
}
return lnk.GetNode(ctx, ds)
}
// Copy returns a copy of the node.
// NOTE: Does not make copies of Node objects in the links.
func (n *ProtoNode) Copy() node.Node {
nnode := new(ProtoNode)
if len(n.data) > 0 {
nnode.data = make([]byte, len(n.data))
copy(nnode.data, n.data)
}
if len(n.links) > 0 {
nnode.links = make([]*node.Link, len(n.links))
copy(nnode.links, n.links)
}
return nnode
}
func (n *ProtoNode) RawData() []byte {
out, _ := n.EncodeProtobuf(false)
return out
}
func (n *ProtoNode) Data() []byte {
return n.data
}
func (n *ProtoNode) SetData(d []byte) {
n.encoded = nil
n.cached = nil
n.data = d
}
// UpdateNodeLink return a copy of the node with the link name set to point to
// that. If a link of the same name existed, it is removed.
func (n *ProtoNode) UpdateNodeLink(name string, that *ProtoNode) (*ProtoNode, error) {
newnode := n.Copy().(*ProtoNode)
err := newnode.RemoveNodeLink(name)
err = nil // ignore error
err = newnode.AddNodeLink(name, that)
return newnode, err
}
// Size returns the total size of the data addressed by node,
// including the total sizes of references.
func (n *ProtoNode) Size() (uint64, error) {
b, err := n.EncodeProtobuf(false)
if err != nil {
return 0, err
}
s := uint64(len(b))
for _, l := range n.links {
s += l.Size
}
return s, nil
}
// Stat returns statistics on the node.
func (n *ProtoNode) Stat() (*node.NodeStat, error) {
enc, err := n.EncodeProtobuf(false)
if err != nil {
return nil, err
}
cumSize, err := n.Size()
if err != nil {
return nil, err
}
return &node.NodeStat{
Hash: n.Cid().String(),
NumLinks: len(n.links),
BlockSize: len(enc),
LinksSize: len(enc) - len(n.data), // includes framing.
DataSize: len(n.data),
CumulativeSize: int(cumSize),
}, nil
}
func (n *ProtoNode) Loggable() map[string]interface{} {
return map[string]interface{}{
"node": n.String(),
}
}
func (n *ProtoNode) UnmarshalJSON(b []byte) error {
s := struct {
Data []byte `json:"data"`
Links []*node.Link `json:"links"`
}{}
err := json.Unmarshal(b, &s)
if err != nil {
return err
}
n.data = s.Data
n.links = s.Links
return nil
}
func (n *ProtoNode) MarshalJSON() ([]byte, error) {
out := map[string]interface{}{
"data": n.data,
"links": n.links,
}
return json.Marshal(out)
}
func (n *ProtoNode) Cid() *cid.Cid {
if n.encoded != nil && n.cached != nil {
return n.cached
}
if n.Prefix.Codec == 0 {
n.Prefix = defaultCidPrefix
}
c, err := n.Prefix.Sum(n.RawData())
if err != nil {
// programmer error
panic(err)
}
n.cached = c
return c
}
func (n *ProtoNode) String() string {
return n.Cid().String()
}
// Multihash hashes the encoded data of this node.
func (n *ProtoNode) Multihash() mh.Multihash {
// NOTE: EncodeProtobuf generates the hash and puts it in n.cached.
_, err := n.EncodeProtobuf(false)
if err != nil {
// Note: no possibility exists for an error to be returned through here
panic(err)
}
return n.cached.Hash()
}
func (n *ProtoNode) Links() []*node.Link {
return n.links
}
func (n *ProtoNode) SetLinks(links []*node.Link) {
n.links = links
}
func (n *ProtoNode) Resolve(path []string) (interface{}, []string, error) {
return n.ResolveLink(path)
}
func (n *ProtoNode) ResolveLink(path []string) (*node.Link, []string, error) {
if len(path) == 0 {
return nil, nil, fmt.Errorf("end of path, no more links to resolve")
}
lnk, err := n.GetNodeLink(path[0])
if err != nil {
return nil, nil, err
}
return lnk, path[1:], nil
}
func (n *ProtoNode) Tree(p string, depth int) []string {
// ProtoNodes are only ever one path deep, anything below that results in
// nothing
if p != "" {
return nil
}
out := make([]string, 0, len(n.links))
for _, lnk := range n.links {
out = append(out, lnk.Name)
}
return out
}