merkledag.go 4.43 KB
Newer Older
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
1 2 3
package merkledag

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

	"code.google.com/p/go.net/context"
Chas Leichner's avatar
Chas Leichner committed
8

9
	mh "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multihash"
10
	blocks "github.com/jbenet/go-ipfs/blocks"
11
	bserv "github.com/jbenet/go-ipfs/blockservice"
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
12
	u "github.com/jbenet/go-ipfs/util"
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
13 14
)

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
15
var log = u.Logger("merkledag")
Jeromy's avatar
Jeromy committed
16

Juan Batiz-Benet's avatar
go lint  
Juan Batiz-Benet committed
17 18 19
// NodeMap maps u.Keys to Nodes.
// We cannot use []byte/Multihash for keys :(
// so have to convert Multihash bytes to string (u.Key)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
20 21
type NodeMap map[u.Key]*Node

Juan Batiz-Benet's avatar
go lint  
Juan Batiz-Benet committed
22
// Node represents a node in the IPFS Merkle DAG.
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
23
// nodes have opaque data and a set of navigable links.
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
24
type Node struct {
Juan Batiz-Benet's avatar
gofmt  
Juan Batiz-Benet committed
25 26
	Links []*Link
	Data  []byte
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
27 28 29

	// cache encoded/marshaled value
	encoded []byte
30 31

	cached mh.Multihash
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
32 33
}

Juan Batiz-Benet's avatar
go lint  
Juan Batiz-Benet committed
34
// Link represents an IPFS Merkle DAG Link between Nodes.
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
35
type Link struct {
Juan Batiz-Benet's avatar
gofmt  
Juan Batiz-Benet committed
36 37
	// utf string name. should be unique per object
	Name string // utf8
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
38

Juan Batiz-Benet's avatar
gofmt  
Juan Batiz-Benet committed
39 40
	// cumulative size of target object
	Size uint64
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
41

Juan Batiz-Benet's avatar
gofmt  
Juan Batiz-Benet committed
42 43
	// multihash of the target object
	Hash mh.Multihash
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
44 45 46

	// a ptr to the actual node for graph manipulation
	Node *Node
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
47 48
}

Jeromy's avatar
Jeromy committed
49 50
func MakeLink(n *Node) (*Link, error) {
	s, err := n.Size()
51
	if err != nil {
Jeromy's avatar
Jeromy committed
52
		return nil, err
53 54
	}

Jeromy's avatar
Jeromy committed
55
	h, err := n.Multihash()
56
	if err != nil {
Jeromy's avatar
Jeromy committed
57
		return nil, err
58
	}
Jeromy's avatar
Jeromy committed
59
	return &Link{
60 61
		Size: s,
		Hash: h,
Jeromy's avatar
Jeromy committed
62
	}, nil
63 64
}

65 66 67 68 69 70 71 72
func (l *Link) GetNode(serv *DAGService) (*Node, error) {
	if l.Node != nil {
		return l.Node, nil
	}

	return serv.Get(u.Key(l.Hash))
}

Jeromy's avatar
Jeromy committed
73 74 75
// AddNodeLink adds a link to another node.
func (n *Node) AddNodeLink(name string, that *Node) error {
	lnk, err := MakeLink(that)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
76 77 78
	if err != nil {
		return err
	}
Jeromy's avatar
Jeromy committed
79 80 81 82 83 84
	lnk.Name = name
	lnk.Node = that

	n.Links = append(n.Links, lnk)
	return nil
}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
85

Jeromy's avatar
Jeromy committed
86 87 88 89
// AddNodeLink adds a link to another node. without keeping a reference to
// the child node
func (n *Node) AddNodeLinkClean(name string, that *Node) error {
	lnk, err := MakeLink(that)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
90 91 92
	if err != nil {
		return err
	}
Jeromy's avatar
Jeromy committed
93
	lnk.Name = name
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
94

Jeromy's avatar
Jeromy committed
95
	n.Links = append(n.Links, lnk)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
96 97
	return nil
}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
98

99 100 101 102 103 104 105 106 107 108
func (n *Node) RemoveNodeLink(name string) error {
	for i, l := range n.Links {
		if l.Name == name {
			n.Links = append(n.Links[:i], n.Links[i+1:]...)
			return nil
		}
	}
	return u.ErrNotFound
}

109 110
// Copy returns a copy of the node.
// NOTE: does not make copies of Node objects in the links.
Jeromy's avatar
Jeromy committed
111 112 113 114 115 116 117 118 119 120
func (n *Node) Copy() *Node {
	nnode := new(Node)
	nnode.Data = make([]byte, len(n.Data))
	copy(nnode.Data, n.Data)

	nnode.Links = make([]*Link, len(n.Links))
	copy(nnode.Links, n.Links)
	return nnode
}

Juan Batiz-Benet's avatar
go lint  
Juan Batiz-Benet committed
121 122
// Size returns the total size of the data addressed by node,
// including the total sizes of references.
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
123
func (n *Node) Size() (uint64, error) {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
124
	b, err := n.Encoded(false)
Juan Batiz-Benet's avatar
gofmt  
Juan Batiz-Benet committed
125 126 127 128
	if err != nil {
		return 0, err
	}

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
129
	s := uint64(len(b))
Juan Batiz-Benet's avatar
gofmt  
Juan Batiz-Benet committed
130 131 132 133
	for _, l := range n.Links {
		s += l.Size
	}
	return s, nil
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
134
}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
135

Juan Batiz-Benet's avatar
go lint  
Juan Batiz-Benet committed
136
// Multihash hashes the encoded data of this node.
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
137
func (n *Node) Multihash() (mh.Multihash, error) {
138
	// Note: Encoded generates the hash and puts it in n.cached.
139
	_, err := n.Encoded(false)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
140 141 142 143
	if err != nil {
		return nil, err
	}

144
	return n.cached, nil
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
145
}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
146

Juan Batiz-Benet's avatar
go lint  
Juan Batiz-Benet committed
147
// Key returns the Multihash as a key, for maps.
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
148 149 150 151
func (n *Node) Key() (u.Key, error) {
	h, err := n.Multihash()
	return u.Key(h), err
}
152

Juan Batiz-Benet's avatar
go lint  
Juan Batiz-Benet committed
153 154 155
// DAGService is an IPFS Merkle DAG service.
// - the root is virtual (like a forest)
// - stores nodes' data in a BlockService
156 157
// TODO: should cache Nodes that are in memory, and be
//       able to free some of them when vm pressure is high
158
type DAGService struct {
159
	Blocks *bserv.BlockService
160 161
}

162 163 164
// Add adds a node to the DAGService, storing the block in the BlockService
func (n *DAGService) Add(nd *Node) (u.Key, error) {
	k, _ := nd.Key()
165
	log.Debug("DagService Add [%s]", k)
166 167 168 169 170 171 172 173 174
	if n == nil {
		return "", fmt.Errorf("DAGService is nil")
	}

	d, err := nd.Encoded(false)
	if err != nil {
		return "", err
	}

175 176 177
	b := new(blocks.Block)
	b.Data = d
	b.Multihash, err = nd.Multihash()
178 179 180 181 182 183 184
	if err != nil {
		return "", err
	}

	return n.Blocks.AddBlock(b)
}

185 186 187
func (n *DAGService) AddRecursive(nd *Node) error {
	_, err := n.Add(nd)
	if err != nil {
Jeromy's avatar
Jeromy committed
188
		log.Info("AddRecursive Error: %s\n", err)
189 190 191 192
		return err
	}

	for _, link := range nd.Links {
193 194 195 196 197
		if link.Node != nil {
			err := n.AddRecursive(link.Node)
			if err != nil {
				return err
			}
198 199 200 201 202 203
		}
	}

	return nil
}

Juan Batiz-Benet's avatar
go lint  
Juan Batiz-Benet committed
204
// Get retrieves a node from the DAGService, fetching the block in the BlockService
205 206 207 208 209
func (n *DAGService) Get(k u.Key) (*Node, error) {
	if n == nil {
		return nil, fmt.Errorf("DAGService is nil")
	}

Jeromy's avatar
Jeromy committed
210 211
	ctx, _ := context.WithTimeout(context.TODO(), time.Second*5)
	b, err := n.Blocks.GetBlock(ctx, k)
212 213 214 215 216 217
	if err != nil {
		return nil, err
	}

	return Decoded(b.Data)
}