node.go 8.71 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

Jeromy's avatar
Jeromy committed
8 9 10
	cid "github.com/ipfs/go-cid"
	ipld "github.com/ipfs/go-ipld-format"
	mh "github.com/multiformats/go-multihash"
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
	// builder specifies cid version and hashing function
	builder cid.Builder
32 33
}

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)
	}
}

66 67
// CidBuilder returns the CID Builder for this ProtoNode, it is never nil
func (n *ProtoNode) CidBuilder() cid.Builder {
68 69 70 71 72 73
	if n.builder == nil {
		n.builder = v0CidPrefix
	}
	return n.builder
}

74 75 76 77
// SetCidBuilder sets the CID builder if it is non nil, if nil then it
// is reset to the default value
func (n *ProtoNode) SetCidBuilder(builder cid.Builder) {
	if builder == nil {
78
		n.builder = v0CidPrefix
79
	} else {
80
		n.builder = builder.WithCodec(cid.DagProtobuf)
81 82 83
		n.encoded = nil
		n.cached = nil
	}
84 85
}

86
// LinkSlice is a slice of ipld.Links
87
type LinkSlice []*ipld.Link
88 89 90 91 92

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 }

93
// NodeWithData builds a new Protonode with the given data.
94 95
func NodeWithData(d []byte) *ProtoNode {
	return &ProtoNode{data: d}
96 97
}

98
// AddNodeLink adds a link to another node.
99
func (n *ProtoNode) AddNodeLink(name string, that ipld.Node) error {
100
	n.encoded = nil
101

102
	lnk, err := ipld.MakeLink(that)
103 104 105 106
	if err != nil {
		return err
	}

Jeromy's avatar
Jeromy committed
107 108
	lnk.Name = name

109 110
	n.AddRawLink(name, lnk)

111 112 113
	return nil
}

114
// AddRawLink adds a copy of a link to this node
115
func (n *ProtoNode) AddRawLink(name string, l *ipld.Link) error {
116
	n.encoded = nil
117
	n.links = append(n.links, &ipld.Link{
118 119
		Name: name,
		Size: l.Size,
120
		Cid:  l.Cid,
121
	})
122 123 124 125

	return nil
}

Steven Allen's avatar
Steven Allen committed
126
// RemoveNodeLink removes a link on this node by the given name.
127
func (n *ProtoNode) RemoveNodeLink(name string) error {
128
	n.encoded = nil
Jeromy's avatar
Jeromy committed
129

130 131 132 133 134 135 136 137
	ref := &n.links
	filterPos := 0
	found := false

	for i := 0; i < len(*ref); i++ {
		if v := (*ref)[i]; v.Name != name {
			(*ref)[filterPos] = v
			filterPos++
Jeromy's avatar
Jeromy committed
138 139
		} else {
			found = true
140 141
		}
	}
Jeromy's avatar
Jeromy committed
142 143

	if !found {
144
		return ipld.ErrNotFound
Jeromy's avatar
Jeromy committed
145 146
	}

147 148
	n.links = (*ref)[:filterPos]

Jeromy's avatar
Jeromy committed
149
	return nil
150 151
}

Steven Allen's avatar
Steven Allen committed
152
// GetNodeLink returns a copy of the link with the given name.
153
func (n *ProtoNode) GetNodeLink(name string) (*ipld.Link, error) {
154
	for _, l := range n.links {
155
		if l.Name == name {
156
			return &ipld.Link{
157 158
				Name: l.Name,
				Size: l.Size,
159
				Cid:  l.Cid,
160 161 162
			}, nil
		}
	}
163
	return nil, ErrLinkNotFound
164 165
}

Steven Allen's avatar
Steven Allen committed
166
// GetLinkedProtoNode returns a copy of the ProtoNode with the given name.
167
func (n *ProtoNode) GetLinkedProtoNode(ctx context.Context, ds ipld.DAGService, name string) (*ProtoNode, error) {
168 169 170 171 172 173 174 175 176 177 178 179 180
	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
181
// GetLinkedNode returns a copy of the IPLD Node with the given name.
182
func (n *ProtoNode) GetLinkedNode(ctx context.Context, ds ipld.DAGService, name string) (ipld.Node, error) {
183 184 185 186 187 188 189 190
	lnk, err := n.GetNodeLink(name)
	if err != nil {
		return nil, err
	}

	return lnk.GetNode(ctx, ds)
}

191
// Copy returns a copy of the node.
192
// NOTE: Does not make copies of Node objects in the links.
193
func (n *ProtoNode) Copy() ipld.Node {
194
	nnode := new(ProtoNode)
195 196 197
	if len(n.data) > 0 {
		nnode.data = make([]byte, len(n.data))
		copy(nnode.data, n.data)
Jeromy's avatar
Jeromy committed
198
	}
199

200
	if len(n.links) > 0 {
201
		nnode.links = make([]*ipld.Link, len(n.links))
202
		copy(nnode.links, n.links)
Jeromy's avatar
Jeromy committed
203
	}
204

205
	nnode.builder = n.builder
206

207 208 209
	return nnode
}

210
// RawData returns the protobuf-encoded version of the node.
211
func (n *ProtoNode) RawData() []byte {
Jeromy's avatar
Jeromy committed
212 213 214 215
	out, _ := n.EncodeProtobuf(false)
	return out
}

216
// Data returns the data stored by this node.
217
func (n *ProtoNode) Data() []byte {
218 219 220
	return n.data
}

221
// SetData stores data in this nodes.
222
func (n *ProtoNode) SetData(d []byte) {
223 224 225 226 227
	n.encoded = nil
	n.cached = nil
	n.data = d
}

228 229
// 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.
230
func (n *ProtoNode) UpdateNodeLink(name string, that *ProtoNode) (*ProtoNode, error) {
231
	newnode := n.Copy().(*ProtoNode)
232 233
	_ = newnode.RemoveNodeLink(name) // ignore error
	err := newnode.AddNodeLink(name, that)
234 235 236
	return newnode, err
}

237 238
// Size returns the total size of the data addressed by node,
// including the total sizes of references.
239
func (n *ProtoNode) Size() (uint64, error) {
240
	b, err := n.EncodeProtobuf(false)
241 242 243 244 245
	if err != nil {
		return 0, err
	}

	s := uint64(len(b))
246
	for _, l := range n.links {
247 248 249 250 251 252
		s += l.Size
	}
	return s, nil
}

// Stat returns statistics on the node.
253
func (n *ProtoNode) Stat() (*ipld.NodeStat, error) {
254
	enc, err := n.EncodeProtobuf(false)
255
	if err != nil {
256
		return nil, err
257 258 259 260
	}

	cumSize, err := n.Size()
	if err != nil {
261
		return nil, err
262 263
	}

264
	return &ipld.NodeStat{
Jeromy's avatar
Jeromy committed
265
		Hash:           n.Cid().String(),
266
		NumLinks:       len(n.links),
267
		BlockSize:      len(enc),
268 269
		LinksSize:      len(enc) - len(n.data), // includes framing.
		DataSize:       len(n.data),
270 271 272 273
		CumulativeSize: int(cumSize),
	}, nil
}

274
// Loggable implements the ipfs/go-log.Loggable interface.
275
func (n *ProtoNode) Loggable() map[string]interface{} {
Jeromy's avatar
Jeromy committed
276 277 278 279 280
	return map[string]interface{}{
		"node": n.String(),
	}
}

281
// UnmarshalJSON reads the node fields from a JSON-encoded byte slice.
Jeromy's avatar
Jeromy committed
282 283 284
func (n *ProtoNode) UnmarshalJSON(b []byte) error {
	s := struct {
		Data  []byte       `json:"data"`
285
		Links []*ipld.Link `json:"links"`
Jeromy's avatar
Jeromy committed
286 287 288 289 290 291 292 293 294 295 296 297
	}{}

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

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

298
// MarshalJSON returns a JSON representation of the node.
299 300 301 302 303 304 305 306 307
func (n *ProtoNode) MarshalJSON() ([]byte, error) {
	out := map[string]interface{}{
		"data":  n.data,
		"links": n.links,
	}

	return json.Marshal(out)
}

308 309
// Cid returns the node's Cid, calculated according to its prefix
// and raw data contents.
310
func (n *ProtoNode) Cid() *cid.Cid {
311 312 313 314
	if n.encoded != nil && n.cached != nil {
		return n.cached
	}

315
	c, err := n.builder.Sum(n.RawData())
316 317
	if err != nil {
		// programmer error
318
		err = fmt.Errorf("invalid CID of length %d: %x: %v", len(n.RawData()), n.RawData(), err)
319 320
		panic(err)
	}
Jeromy's avatar
Jeromy committed
321

322 323
	n.cached = c
	return c
Jeromy's avatar
Jeromy committed
324 325
}

326
// String prints the node's Cid.
327
func (n *ProtoNode) String() string {
Jeromy's avatar
Jeromy committed
328 329 330
	return n.Cid().String()
}

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

Jeromy's avatar
Jeromy committed
340
	return n.cached.Hash()
341
}
342

343
// Links returns the node links.
344
func (n *ProtoNode) Links() []*ipld.Link {
345 346 347
	return n.links
}

348
// SetLinks replaces the node links with the given ones.
349
func (n *ProtoNode) SetLinks(links []*ipld.Link) {
350 351 352
	n.links = links
}

353
// Resolve is an alias for ResolveLink.
354 355 356 357
func (n *ProtoNode) Resolve(path []string) (interface{}, []string, error) {
	return n.ResolveLink(path)
}

358 359 360
// 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.
361
func (n *ProtoNode) ResolveLink(path []string) (*ipld.Link, []string, error) {
362 363 364 365 366 367 368 369 370 371 372 373
	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
}

374 375 376
// 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.
377 378 379 380 381
func (n *ProtoNode) Tree(p string, depth int) []string {
	if p != "" {
		return nil
	}

382 383 384 385 386 387
	out := make([]string, 0, len(n.links))
	for _, lnk := range n.links {
		out = append(out, lnk.Name)
	}
	return out
}