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

import (
4 5
	"fmt"
	blocks "github.com/jbenet/go-ipfs/blocks"
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
6
	u "github.com/jbenet/go-ipfs/util"
Juan Batiz-Benet's avatar
gofmt  
Juan Batiz-Benet committed
7
	mh "github.com/jbenet/go-multihash"
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
8 9
)

Juan Batiz-Benet's avatar
go lint  
Juan Batiz-Benet committed
10 11 12
// 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
13 14
type NodeMap map[u.Key]*Node

Juan Batiz-Benet's avatar
go lint  
Juan Batiz-Benet committed
15
// Node represents a node in the IPFS Merkle DAG.
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
16
// nodes have opaque data and a set of navigable links.
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
17
type Node struct {
Juan Batiz-Benet's avatar
gofmt  
Juan Batiz-Benet committed
18 19
	Links []*Link
	Data  []byte
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
20 21 22

	// cache encoded/marshaled value
	encoded []byte
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
23 24
}

Juan Batiz-Benet's avatar
go lint  
Juan Batiz-Benet committed
25
// Link represents an IPFS Merkle DAG Link between Nodes.
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
26
type Link struct {
Juan Batiz-Benet's avatar
gofmt  
Juan Batiz-Benet committed
27 28
	// utf string name. should be unique per object
	Name string // utf8
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
29

Juan Batiz-Benet's avatar
gofmt  
Juan Batiz-Benet committed
30 31
	// cumulative size of target object
	Size uint64
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
32

Juan Batiz-Benet's avatar
gofmt  
Juan Batiz-Benet committed
33 34
	// multihash of the target object
	Hash mh.Multihash
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
35 36 37

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

Juan Batiz-Benet's avatar
go lint  
Juan Batiz-Benet committed
40
// AddNodeLink adds a link to another node.
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
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,
	})
	return nil
}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
59

Juan Batiz-Benet's avatar
go lint  
Juan Batiz-Benet committed
60 61
// 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
62
func (n *Node) Size() (uint64, error) {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
63
	b, err := n.Encoded(false)
Juan Batiz-Benet's avatar
gofmt  
Juan Batiz-Benet committed
64 65 66 67
	if err != nil {
		return 0, err
	}

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
68
	s := uint64(len(b))
Juan Batiz-Benet's avatar
gofmt  
Juan Batiz-Benet committed
69 70 71 72
	for _, l := range n.Links {
		s += l.Size
	}
	return s, nil
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
73
}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
74

Juan Batiz-Benet's avatar
go lint  
Juan Batiz-Benet committed
75
// Multihash hashes the encoded data of this node.
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
76 77 78 79 80 81
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
82
	return u.Hash(b)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
83
}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
84

Juan Batiz-Benet's avatar
go lint  
Juan Batiz-Benet committed
85
// Key returns the Multihash as a key, for maps.
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
86 87 88 89
func (n *Node) Key() (u.Key, error) {
	h, err := n.Multihash()
	return u.Key(h), err
}
90

Juan Batiz-Benet's avatar
go lint  
Juan Batiz-Benet committed
91 92 93
// DAGService is an IPFS Merkle DAG service.
// - the root is virtual (like a forest)
// - stores nodes' data in a BlockService
94 95 96 97
type DAGService struct {
	Blocks *blocks.BlockService
}

Juan Batiz-Benet's avatar
go lint  
Juan Batiz-Benet committed
98
// Put adds a node to the DAGService, storing the block in the BlockService
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
func (n *DAGService) Put(nd *Node) (u.Key, error) {
	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)
}

Juan Batiz-Benet's avatar
go lint  
Juan Batiz-Benet committed
117
// Get retrieves a node from the DAGService, fetching the block in the BlockService
118 119 120 121 122 123 124 125 126 127 128 129
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)
}