merkledag.go 2.05 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
Juan Batiz-Benet committed
10 11 12 13
// can't use []byte/Multihash for keys :(
// so have to convert Multihash bytes to string
type NodeMap map[u.Key]*Node

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

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

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

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

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

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

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
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
57

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
58
func (n *Node) Size() (uint64, error) {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
59
	b, err := n.Encoded(false)
Juan Batiz-Benet's avatar
gofmt  
Juan Batiz-Benet committed
60 61 62 63
	if err != nil {
		return 0, err
	}

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

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
77
	return u.Hash(b)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
78
}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
79 80 81 82 83

func (n *Node) Key() (u.Key, error) {
	h, err := n.Multihash()
	return u.Key(h), err
}
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121

// An IPFS Merkle DAG service.
// the root is virtual (like a forest)
// stores nodes' data in a blockService
type DAGService struct {
	Blocks *blocks.BlockService
}

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

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