node.go 8.63 KB
Newer Older
1 2 3
package merkledag

import (
4
	"context"
5
	"encoding/json"
Jeromy's avatar
Jeromy committed
6
	"fmt"
Jeromy's avatar
Jeromy committed
7

8
	mh "gx/ipfs/QmPnFwZ2JXKnXgMw8CdBPxn7FWh6LLdjUjxV1fKHuJnkr8/go-multihash"
Steven Allen's avatar
Steven Allen committed
9 10
	cid "gx/ipfs/QmYVNvtQkeZ6AKSwDrjQTs432QtL6umrrK41EBq3cu7iSP/go-cid"
	ipld "gx/ipfs/QmZtNq8dArGfnpCZfx2pUNY7UcjGhVp5qqwQ4hH6mpTMRQ/go-ipld-format"
11 12
)

13 14 15 16 17
// Common errors
var (
	ErrNotProtobuf  = fmt.Errorf("expected protobuf dag node")
	ErrLinkNotFound = fmt.Errorf("no link by that name")
)
18

19
// ProtoNode represents a node in the IPFS Merkle DAG.
20
// nodes have opaque data and a set of navigable links.
21
type ProtoNode struct {
22
	links []*ipld.Link
23
	data  []byte
24 25 26 27

	// cache encoded/marshaled value
	encoded []byte

Jeromy's avatar
Jeromy committed
28
	cached *cid.Cid
29

30 31 32 33
	// Prefix specifies cid version and hashing function
	Prefix cid.Prefix
}

34
var v0CidPrefix = cid.Prefix{
35 36 37 38
	Codec:    cid.DagProtobuf,
	MhLength: -1,
	MhType:   mh.SHA2_256,
	Version:  0,
39 40
}

41 42 43 44 45 46 47
var v1CidPrefix = cid.Prefix{
	Codec:    cid.DagProtobuf,
	MhLength: -1,
	MhType:   mh.SHA2_256,
	Version:  1,
}

Kevin Atkinson's avatar
Kevin Atkinson committed
48
// V0CidPrefix returns a prefix for CIDv0
49
func V0CidPrefix() cid.Prefix { return v0CidPrefix }
Kevin Atkinson's avatar
Kevin Atkinson committed
50 51

// V1CidPrefix returns a prefix for CIDv1 with the default settings
52 53
func V1CidPrefix() cid.Prefix { return v1CidPrefix }

Kevin Atkinson's avatar
Kevin Atkinson committed
54
// PrefixForCidVersion returns the Protobuf prefix for a given CID version
55 56 57 58 59 60 61 62 63 64 65
func PrefixForCidVersion(version int) (cid.Prefix, error) {
	switch version {
	case 0:
		return v0CidPrefix, nil
	case 1:
		return v1CidPrefix, nil
	default:
		return cid.Prefix{}, fmt.Errorf("unknown CID version: %d", version)
	}
}

Kevin Atkinson's avatar
Kevin Atkinson committed
66
// SetPrefix sets the CID prefix if it is non nil, if prefix is nil then
67 68 69 70 71 72 73 74 75 76
// it resets it the default value
func (n *ProtoNode) SetPrefix(prefix *cid.Prefix) {
	if prefix == nil {
		n.Prefix = v0CidPrefix
	} else {
		n.Prefix = *prefix
		n.Prefix.Codec = cid.DagProtobuf
		n.encoded = nil
		n.cached = nil
	}
77 78
}

79
// LinkSlice is a slice of ipld.Links
80
type LinkSlice []*ipld.Link
81 82 83 84 85

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 }

86
// NodeWithData builds a new Protonode with the given data.
87 88
func NodeWithData(d []byte) *ProtoNode {
	return &ProtoNode{data: d}
89 90
}

91
// AddNodeLink adds a link to another node.
92
func (n *ProtoNode) AddNodeLink(name string, that ipld.Node) error {
93
	n.encoded = nil
94

95
	lnk, err := ipld.MakeLink(that)
96 97 98 99
	if err != nil {
		return err
	}

Jeromy's avatar
Jeromy committed
100 101
	lnk.Name = name

102 103
	n.AddRawLink(name, lnk)

104 105 106
	return nil
}

107
// AddRawLink adds a copy of a link to this node
108
func (n *ProtoNode) AddRawLink(name string, l *ipld.Link) error {
109
	n.encoded = nil
110
	n.links = append(n.links, &ipld.Link{
111 112
		Name: name,
		Size: l.Size,
113
		Cid:  l.Cid,
114
	})
115 116 117 118

	return nil
}

Steven Allen's avatar
Steven Allen committed
119
// RemoveNodeLink removes a link on this node by the given name.
120
func (n *ProtoNode) RemoveNodeLink(name string) error {
121
	n.encoded = nil
122
	good := make([]*ipld.Link, 0, len(n.links))
Jeromy's avatar
Jeromy committed
123 124
	var found bool

125
	for _, l := range n.links {
Jeromy's avatar
Jeromy committed
126 127 128 129
		if l.Name != name {
			good = append(good, l)
		} else {
			found = true
130 131
		}
	}
132
	n.links = good
Jeromy's avatar
Jeromy committed
133 134

	if !found {
135
		return ipld.ErrNotFound
Jeromy's avatar
Jeromy committed
136 137 138
	}

	return nil
139 140
}

Steven Allen's avatar
Steven Allen committed
141
// GetNodeLink returns a copy of the link with the given name.
142
func (n *ProtoNode) GetNodeLink(name string) (*ipld.Link, error) {
143
	for _, l := range n.links {
144
		if l.Name == name {
145
			return &ipld.Link{
146 147
				Name: l.Name,
				Size: l.Size,
148
				Cid:  l.Cid,
149 150 151
			}, nil
		}
	}
152
	return nil, ErrLinkNotFound
153 154
}

Steven Allen's avatar
Steven Allen committed
155
// GetLinkedProtoNode returns a copy of the ProtoNode with the given name.
156
func (n *ProtoNode) GetLinkedProtoNode(ctx context.Context, ds ipld.DAGService, name string) (*ProtoNode, error) {
157 158 159 160 161 162 163 164 165 166 167 168 169
	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
}

Steven Allen's avatar
Steven Allen committed
170
// GetLinkedNode returns a copy of the IPLD Node with the given name.
171
func (n *ProtoNode) GetLinkedNode(ctx context.Context, ds ipld.DAGService, name string) (ipld.Node, error) {
172 173 174 175 176 177 178 179
	lnk, err := n.GetNodeLink(name)
	if err != nil {
		return nil, err
	}

	return lnk.GetNode(ctx, ds)
}

180
// Copy returns a copy of the node.
181
// NOTE: Does not make copies of Node objects in the links.
182
func (n *ProtoNode) Copy() ipld.Node {
183
	nnode := new(ProtoNode)
184 185 186
	if len(n.data) > 0 {
		nnode.data = make([]byte, len(n.data))
		copy(nnode.data, n.data)
Jeromy's avatar
Jeromy committed
187
	}
188

189
	if len(n.links) > 0 {
190
		nnode.links = make([]*ipld.Link, len(n.links))
191
		copy(nnode.links, n.links)
Jeromy's avatar
Jeromy committed
192
	}
193 194 195

	nnode.Prefix = n.Prefix

196 197 198
	return nnode
}

199
// RawData returns the protobuf-encoded version of the node.
200
func (n *ProtoNode) RawData() []byte {
Jeromy's avatar
Jeromy committed
201 202 203 204
	out, _ := n.EncodeProtobuf(false)
	return out
}

205
// Data returns the data stored by this node.
206
func (n *ProtoNode) Data() []byte {
207 208 209
	return n.data
}

210
// SetData stores data in this nodes.
211
func (n *ProtoNode) SetData(d []byte) {
212 213 214 215 216
	n.encoded = nil
	n.cached = nil
	n.data = d
}

217 218
// 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.
219
func (n *ProtoNode) UpdateNodeLink(name string, that *ProtoNode) (*ProtoNode, error) {
220
	newnode := n.Copy().(*ProtoNode)
221 222
	_ = newnode.RemoveNodeLink(name) // ignore error
	err := newnode.AddNodeLink(name, that)
223 224 225
	return newnode, err
}

226 227
// Size returns the total size of the data addressed by node,
// including the total sizes of references.
228
func (n *ProtoNode) Size() (uint64, error) {
229
	b, err := n.EncodeProtobuf(false)
230 231 232 233 234
	if err != nil {
		return 0, err
	}

	s := uint64(len(b))
235
	for _, l := range n.links {
236 237 238 239 240 241
		s += l.Size
	}
	return s, nil
}

// Stat returns statistics on the node.
242
func (n *ProtoNode) Stat() (*ipld.NodeStat, error) {
243
	enc, err := n.EncodeProtobuf(false)
244
	if err != nil {
245
		return nil, err
246 247 248 249
	}

	cumSize, err := n.Size()
	if err != nil {
250
		return nil, err
251 252
	}

253
	return &ipld.NodeStat{
Jeromy's avatar
Jeromy committed
254
		Hash:           n.Cid().String(),
255
		NumLinks:       len(n.links),
256
		BlockSize:      len(enc),
257 258
		LinksSize:      len(enc) - len(n.data), // includes framing.
		DataSize:       len(n.data),
259 260 261 262
		CumulativeSize: int(cumSize),
	}, nil
}

263
// Loggable implements the ipfs/go-log.Loggable interface.
264
func (n *ProtoNode) Loggable() map[string]interface{} {
Jeromy's avatar
Jeromy committed
265 266 267 268 269
	return map[string]interface{}{
		"node": n.String(),
	}
}

270
// UnmarshalJSON reads the node fields from a JSON-encoded byte slice.
Jeromy's avatar
Jeromy committed
271 272 273
func (n *ProtoNode) UnmarshalJSON(b []byte) error {
	s := struct {
		Data  []byte       `json:"data"`
274
		Links []*ipld.Link `json:"links"`
Jeromy's avatar
Jeromy committed
275 276 277 278 279 280 281 282 283 284 285 286
	}{}

	err := json.Unmarshal(b, &s)
	if err != nil {
		return err
	}

	n.data = s.Data
	n.links = s.Links
	return nil
}

287
// MarshalJSON returns a JSON representation of the node.
288 289 290 291 292 293 294 295 296
func (n *ProtoNode) MarshalJSON() ([]byte, error) {
	out := map[string]interface{}{
		"data":  n.data,
		"links": n.links,
	}

	return json.Marshal(out)
}

297 298
// Cid returns the node's Cid, calculated according to its prefix
// and raw data contents.
299
func (n *ProtoNode) Cid() *cid.Cid {
300 301 302 303 304
	if n.encoded != nil && n.cached != nil {
		return n.cached
	}

	if n.Prefix.Codec == 0 {
305
		n.SetPrefix(nil)
306 307 308 309 310
	}

	c, err := n.Prefix.Sum(n.RawData())
	if err != nil {
		// programmer error
311
		err = fmt.Errorf("invalid CID of length %d: %x: %v", len(n.RawData()), n.RawData(), err)
312 313
		panic(err)
	}
Jeromy's avatar
Jeromy committed
314

315 316
	n.cached = c
	return c
Jeromy's avatar
Jeromy committed
317 318
}

319
// String prints the node's Cid.
320
func (n *ProtoNode) String() string {
Jeromy's avatar
Jeromy committed
321 322 323
	return n.Cid().String()
}

324
// Multihash hashes the encoded data of this node.
325
func (n *ProtoNode) Multihash() mh.Multihash {
326
	// NOTE: EncodeProtobuf generates the hash and puts it in n.cached.
327
	_, err := n.EncodeProtobuf(false)
328
	if err != nil {
Jeromy's avatar
Jeromy committed
329 330
		// Note: no possibility exists for an error to be returned through here
		panic(err)
331 332
	}

Jeromy's avatar
Jeromy committed
333
	return n.cached.Hash()
334
}
335

336
// Links returns the node links.
337
func (n *ProtoNode) Links() []*ipld.Link {
338 339 340
	return n.links
}

341
// SetLinks replaces the node links with the given ones.
342
func (n *ProtoNode) SetLinks(links []*ipld.Link) {
343 344 345
	n.links = links
}

346
// Resolve is an alias for ResolveLink.
347 348 349 350
func (n *ProtoNode) Resolve(path []string) (interface{}, []string, error) {
	return n.ResolveLink(path)
}

351 352 353
// ResolveLink consumes the first element of the path and obtains the link
// corresponding to it from the node. It returns the link
// and the path without the consumed element.
354
func (n *ProtoNode) ResolveLink(path []string) (*ipld.Link, []string, error) {
355 356 357 358 359 360 361 362 363 364 365 366
	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
}

367 368 369
// Tree returns the link names of the ProtoNode.
// ProtoNodes are only ever one path deep, so anything different than an empty
// string for p results in nothing. The depth parameter is ignored.
370 371 372 373 374
func (n *ProtoNode) Tree(p string, depth int) []string {
	if p != "" {
		return nil
	}

375 376 377 378 379 380
	out := make([]string, 0, len(n.links))
	for _, lnk := range n.links {
		out = append(out, lnk.Name)
	}
	return out
}