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

import (
4
	"context"
Jeromy's avatar
Jeromy committed
5
	"fmt"
Jeromy's avatar
Jeromy committed
6

Jeromy's avatar
Jeromy committed
7
	node "gx/ipfs/QmRSU5EqqWVZSNdbU51yXmVoF1uNw3JgTNB6RaiL7DZM16/go-ipld-node"
8
	mh "gx/ipfs/QmYDds3421prZgqKbLpEK7T9Aa2eVdQ7o3YarX1LVLdP2J/go-multihash"
Jeromy's avatar
Jeromy committed
9
	cid "gx/ipfs/QmcTcsTvfaeEBRFo1TkFgT8sRmgi1n1LTZpecfVP8fzpGD/go-cid"
10 11
)

Jeromy's avatar
Jeromy committed
12
var ErrNotProtobuf = fmt.Errorf("expected protobuf dag node")
13 14
var ErrLinkNotFound = fmt.Errorf("no link by that name")

15 16
// Node represents a node in the IPFS Merkle DAG.
// nodes have opaque data and a set of navigable links.
17
type ProtoNode struct {
Jeromy's avatar
Jeromy committed
18
	links []*node.Link
19
	data  []byte
20 21 22 23

	// cache encoded/marshaled value
	encoded []byte

Jeromy's avatar
Jeromy committed
24
	cached *cid.Cid
25 26 27

	// prefix specifies cid version and hashing function
	prefix cid.Prefix
28 29
}

Jeromy's avatar
Jeromy committed
30
type LinkSlice []*node.Link
31 32 33 34 35

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 }

36 37
func NodeWithData(d []byte) *ProtoNode {
	return &ProtoNode{data: d}
38 39
}

40
// AddNodeLink adds a link to another node.
41
func (n *ProtoNode) AddNodeLink(name string, that node.Node) error {
42
	n.encoded = nil
43

Jeromy's avatar
Jeromy committed
44
	lnk, err := node.MakeLink(that)
45 46 47 48
	if err != nil {
		return err
	}

Jeromy's avatar
Jeromy committed
49 50
	lnk.Name = name

51 52
	n.AddRawLink(name, lnk)

53 54 55
	return nil
}

56
// AddNodeLinkClean adds a link to another node. without keeping a reference to
57
// the child node
Jeromy's avatar
Jeromy committed
58
func (n *ProtoNode) AddNodeLinkClean(name string, that node.Node) error {
59
	n.encoded = nil
Jeromy's avatar
Jeromy committed
60
	lnk, err := node.MakeLink(that)
61 62 63
	if err != nil {
		return err
	}
64 65 66 67 68 69
	n.AddRawLink(name, lnk)

	return nil
}

// AddRawLink adds a copy of a link to this node
Jeromy's avatar
Jeromy committed
70
func (n *ProtoNode) AddRawLink(name string, l *node.Link) error {
71
	n.encoded = nil
Jeromy's avatar
Jeromy committed
72
	n.links = append(n.links, &node.Link{
73 74
		Name: name,
		Size: l.Size,
75
		Cid:  l.Cid,
76
	})
77 78 79 80 81

	return nil
}

// Remove a link on this node by the given name
82
func (n *ProtoNode) RemoveNodeLink(name string) error {
83
	n.encoded = nil
Jeromy's avatar
Jeromy committed
84
	good := make([]*node.Link, 0, len(n.links))
Jeromy's avatar
Jeromy committed
85 86
	var found bool

87
	for _, l := range n.links {
Jeromy's avatar
Jeromy committed
88 89 90 91
		if l.Name != name {
			good = append(good, l)
		} else {
			found = true
92 93
		}
	}
94
	n.links = good
Jeromy's avatar
Jeromy committed
95 96 97 98 99 100

	if !found {
		return ErrNotFound
	}

	return nil
101 102
}

103
// Return a copy of the link with given name
Jeromy's avatar
Jeromy committed
104
func (n *ProtoNode) GetNodeLink(name string) (*node.Link, error) {
105
	for _, l := range n.links {
106
		if l.Name == name {
Jeromy's avatar
Jeromy committed
107
			return &node.Link{
108 109
				Name: l.Name,
				Size: l.Size,
110
				Cid:  l.Cid,
111 112 113
			}, nil
		}
	}
114
	return nil, ErrLinkNotFound
115 116
}

117 118 119 120 121 122 123 124 125 126 127 128 129 130
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
}

Jeromy's avatar
Jeromy committed
131
func (n *ProtoNode) GetLinkedNode(ctx context.Context, ds DAGService, name string) (node.Node, error) {
132 133 134 135 136 137 138 139
	lnk, err := n.GetNodeLink(name)
	if err != nil {
		return nil, err
	}

	return lnk.GetNode(ctx, ds)
}

140
// Copy returns a copy of the node.
141
// NOTE: Does not make copies of Node objects in the links.
142
func (n *ProtoNode) Copy() node.Node {
143
	nnode := new(ProtoNode)
144 145 146
	if len(n.data) > 0 {
		nnode.data = make([]byte, len(n.data))
		copy(nnode.data, n.data)
Jeromy's avatar
Jeromy committed
147
	}
148

149
	if len(n.links) > 0 {
Jeromy's avatar
Jeromy committed
150
		nnode.links = make([]*node.Link, len(n.links))
151
		copy(nnode.links, n.links)
Jeromy's avatar
Jeromy committed
152
	}
153 154 155
	return nnode
}

156
func (n *ProtoNode) RawData() []byte {
Jeromy's avatar
Jeromy committed
157 158 159 160
	out, _ := n.EncodeProtobuf(false)
	return out
}

161
func (n *ProtoNode) Data() []byte {
162 163 164
	return n.data
}

165
func (n *ProtoNode) SetData(d []byte) {
166 167 168 169 170
	n.encoded = nil
	n.cached = nil
	n.data = d
}

171 172
// 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.
173
func (n *ProtoNode) UpdateNodeLink(name string, that *ProtoNode) (*ProtoNode, error) {
174
	newnode := n.Copy().(*ProtoNode)
175 176 177 178 179 180
	err := newnode.RemoveNodeLink(name)
	err = nil // ignore error
	err = newnode.AddNodeLink(name, that)
	return newnode, err
}

181 182
// Size returns the total size of the data addressed by node,
// including the total sizes of references.
183
func (n *ProtoNode) Size() (uint64, error) {
184
	b, err := n.EncodeProtobuf(false)
185 186 187 188 189
	if err != nil {
		return 0, err
	}

	s := uint64(len(b))
190
	for _, l := range n.links {
191 192 193 194 195 196
		s += l.Size
	}
	return s, nil
}

// Stat returns statistics on the node.
Jeromy's avatar
Jeromy committed
197
func (n *ProtoNode) Stat() (*node.NodeStat, error) {
198
	enc, err := n.EncodeProtobuf(false)
199
	if err != nil {
200
		return nil, err
201 202 203 204
	}

	cumSize, err := n.Size()
	if err != nil {
205
		return nil, err
206 207
	}

Jeromy's avatar
Jeromy committed
208
	return &node.NodeStat{
Jeromy's avatar
Jeromy committed
209
		Hash:           n.Cid().String(),
210
		NumLinks:       len(n.links),
211
		BlockSize:      len(enc),
212 213
		LinksSize:      len(enc) - len(n.data), // includes framing.
		DataSize:       len(n.data),
214 215 216 217
		CumulativeSize: int(cumSize),
	}, nil
}

218
func (n *ProtoNode) Loggable() map[string]interface{} {
Jeromy's avatar
Jeromy committed
219 220 221 222 223
	return map[string]interface{}{
		"node": n.String(),
	}
}

224
func (n *ProtoNode) Cid() *cid.Cid {
Jeromy's avatar
Jeromy committed
225 226 227 228 229
	h := n.Multihash()

	return cid.NewCidV0(h)
}

230
func (n *ProtoNode) String() string {
Jeromy's avatar
Jeromy committed
231 232 233
	return n.Cid().String()
}

234
// Multihash hashes the encoded data of this node.
235
func (n *ProtoNode) Multihash() mh.Multihash {
236
	// NOTE: EncodeProtobuf generates the hash and puts it in n.cached.
237
	_, err := n.EncodeProtobuf(false)
238
	if err != nil {
Jeromy's avatar
Jeromy committed
239 240
		// Note: no possibility exists for an error to be returned through here
		panic(err)
241 242
	}

Jeromy's avatar
Jeromy committed
243
	return n.cached.Hash()
244
}
245

Jeromy's avatar
Jeromy committed
246
func (n *ProtoNode) Links() []*node.Link {
247 248 249
	return n.links
}

Jeromy's avatar
Jeromy committed
250
func (n *ProtoNode) SetLinks(links []*node.Link) {
251 252 253
	n.links = links
}

254 255 256 257 258
func (n *ProtoNode) Resolve(path []string) (interface{}, []string, error) {
	return n.ResolveLink(path)
}

func (n *ProtoNode) ResolveLink(path []string) (*node.Link, []string, error) {
259 260 261 262 263 264 265 266 267 268 269 270
	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
}

271 272 273 274 275 276 277
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
	}

278 279 280 281 282 283
	out := make([]string, 0, len(n.links))
	for _, lnk := range n.links {
		out = append(out, lnk.Name)
	}
	return out
}