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

import (
4
	"fmt"
Chas Leichner's avatar
Chas Leichner committed
5

Jeromy's avatar
Jeromy committed
6 7
	proto "github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/goprotobuf/proto"
	logging "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/op/go-logging"
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
)

15
var log = logging.MustGetLogger("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
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
30 31
}

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

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

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

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

Juan Batiz-Benet's avatar
go lint  
Juan Batiz-Benet committed
47
// AddNodeLink adds a link to another node.
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
func (n *Node) AddNodeLink(name string, that *Node) error {
	s, err := that.Size()
	if err != nil {
		return err
	}

	h, err := that.Multihash()
	if err != nil {
		return err
	}

	n.Links = append(n.Links, &Link{
		Name: name,
		Size: s,
		Hash: h,
63
		Node: that,
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
64 65 66
	})
	return nil
}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
67

68 69 70 71 72 73 74 75 76 77
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
}

Jeromy's avatar
Jeromy committed
78 79 80 81 82 83 84 85 86 87
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
88 89
// 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
90
func (n *Node) Size() (uint64, error) {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
91
	b, err := n.Encoded(false)
Juan Batiz-Benet's avatar
gofmt  
Juan Batiz-Benet committed
92 93 94 95
	if err != nil {
		return 0, err
	}

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
96
	s := uint64(len(b))
Juan Batiz-Benet's avatar
gofmt  
Juan Batiz-Benet committed
97 98 99 100
	for _, l := range n.Links {
		s += l.Size
	}
	return s, nil
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
101
}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
102

Juan Batiz-Benet's avatar
go lint  
Juan Batiz-Benet committed
103
// Multihash hashes the encoded data of this node.
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
104 105 106 107 108 109
func (n *Node) Multihash() (mh.Multihash, error) {
	b, err := n.Encoded(false)
	if err != nil {
		return nil, err
	}

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
110
	return u.Hash(b)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
111
}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
112

Juan Batiz-Benet's avatar
go lint  
Juan Batiz-Benet committed
113
// Key returns the Multihash as a key, for maps.
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
114 115 116 117
func (n *Node) Key() (u.Key, error) {
	h, err := n.Multihash()
	return u.Key(h), err
}
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
// Recursively update all hash links and size values in the tree
func (n *Node) Update() error {
	log.Debug("node update")
	for _, l := range n.Links {
		if l.Node != nil {
			err := l.Node.Update()
			if err != nil {
				return err
			}
			nhash, err := l.Node.Multihash()
			if err != nil {
				return err
			}
			l.Hash = nhash
			size, err := l.Node.Size()
			if err != nil {
				return err
			}
			l.Size = size
		}
	}
	_, err := n.Encoded(true)
	return err
}

Juan Batiz-Benet's avatar
go lint  
Juan Batiz-Benet committed
144 145 146
// DAGService is an IPFS Merkle DAG service.
// - the root is virtual (like a forest)
// - stores nodes' data in a BlockService
147 148
// TODO: should cache Nodes that are in memory, and be
//       able to free some of them when vm pressure is high
149
type DAGService struct {
150
	Blocks *bserv.BlockService
151 152
}

153 154 155
// 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()
Jeromy's avatar
Jeromy committed
156
	log.Debug("DagService Add [%s]\n", k.Pretty())
157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173
	if n == nil {
		return "", fmt.Errorf("DAGService is nil")
	}

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

	b, err := blocks.NewBlock(d)
	if err != nil {
		return "", err
	}

	return n.Blocks.AddBlock(b)
}

174 175 176
func (n *DAGService) AddRecursive(nd *Node) error {
	_, err := n.Add(nd)
	if err != nil {
Jeromy's avatar
Jeromy committed
177
		log.Info("AddRecursive Error: %s\n", err)
178 179 180 181
		return err
	}

	for _, link := range nd.Links {
182 183 184 185 186
		if link.Node != nil {
			err := n.AddRecursive(link.Node)
			if err != nil {
				return err
			}
187 188 189 190 191 192
		}
	}

	return nil
}

Juan Batiz-Benet's avatar
go lint  
Juan Batiz-Benet committed
193
// Get retrieves a node from the DAGService, fetching the block in the BlockService
194 195 196 197 198 199 200 201 202 203 204 205
func (n *DAGService) Get(k u.Key) (*Node, error) {
	if n == nil {
		return nil, fmt.Errorf("DAGService is nil")
	}

	b, err := n.Blocks.GetBlock(k)
	if err != nil {
		return nil, err
	}

	return Decoded(b.Data)
}
206

207
func FilePBData(data []byte) []byte {
208 209 210
	pbfile := new(PBData)
	typ := PBData_File
	pbfile.Type = &typ
211
	pbfile.Data = data
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

	data, err := proto.Marshal(pbfile)
	if err != nil {
		//this really shouldnt happen, i promise
		panic(err)
	}
	return data
}

func FolderPBData() []byte {
	pbfile := new(PBData)
	typ := PBData_Directory
	pbfile.Type = &typ

	data, err := proto.Marshal(pbfile)
	if err != nil {
		//this really shouldnt happen, i promise
		panic(err)
	}
	return data
}

func WrapData(b []byte) []byte {
	pbdata := new(PBData)
	typ := PBData_Raw
	pbdata.Data = b
	pbdata.Type = &typ

	out, err := proto.Marshal(pbdata)
	if err != nil {
		// This shouldnt happen. seriously.
		panic(err)
	}

	return out
}