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

import (
Juan Batiz-Benet's avatar
gofmt  
Juan Batiz-Benet committed
4
	"fmt"
5
	"sort"
6

7
	pb "github.com/ipfs/go-ipfs/merkledag/pb"
Jeromy's avatar
Jeromy committed
8

Jeromy's avatar
Jeromy committed
9 10
	node "gx/ipfs/QmRSU5EqqWVZSNdbU51yXmVoF1uNw3JgTNB6RaiL7DZM16/go-ipld-node"
	cid "gx/ipfs/QmcTcsTvfaeEBRFo1TkFgT8sRmgi1n1LTZpecfVP8fzpGD/go-cid"
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
11 12 13 14 15
)

// for now, we use a PBNode intermediate thing.
// because native go objects are nice.

16
// unmarshal decodes raw data into a *Node instance.
Juan Batiz-Benet's avatar
go lint  
Juan Batiz-Benet committed
17
// The conversion uses an intermediate PBNode.
18
func (n *ProtoNode) unmarshal(encoded []byte) error {
19
	var pbn pb.PBNode
Juan Batiz-Benet's avatar
gofmt  
Juan Batiz-Benet committed
20 21 22 23 24
	if err := pbn.Unmarshal(encoded); err != nil {
		return fmt.Errorf("Unmarshal failed. %v", err)
	}

	pbnl := pbn.GetLinks()
Jeromy's avatar
Jeromy committed
25
	n.links = make([]*node.Link, len(pbnl))
Juan Batiz-Benet's avatar
gofmt  
Juan Batiz-Benet committed
26
	for i, l := range pbnl {
Jeromy's avatar
Jeromy committed
27
		n.links[i] = &node.Link{Name: l.GetName(), Size: l.GetTsize()}
28
		c, err := cid.Cast(l.GetHash())
Juan Batiz-Benet's avatar
gofmt  
Juan Batiz-Benet committed
29
		if err != nil {
30
			return fmt.Errorf("Link hash #%d is not valid multihash. %v", i, err)
Juan Batiz-Benet's avatar
gofmt  
Juan Batiz-Benet committed
31
		}
32
		n.links[i].Cid = c
Juan Batiz-Benet's avatar
gofmt  
Juan Batiz-Benet committed
33
	}
34
	sort.Stable(LinkSlice(n.links)) // keep links sorted
Juan Batiz-Benet's avatar
gofmt  
Juan Batiz-Benet committed
35

36 37
	n.data = pbn.GetData()
	n.encoded = encoded
Juan Batiz-Benet's avatar
gofmt  
Juan Batiz-Benet committed
38
	return nil
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
39 40
}

Juan Batiz-Benet's avatar
go lint  
Juan Batiz-Benet committed
41 42
// Marshal encodes a *Node instance into a new byte slice.
// The conversion uses an intermediate PBNode.
43
func (n *ProtoNode) Marshal() ([]byte, error) {
Juan Batiz-Benet's avatar
gofmt  
Juan Batiz-Benet committed
44 45 46 47 48 49
	pbn := n.getPBNode()
	data, err := pbn.Marshal()
	if err != nil {
		return data, fmt.Errorf("Marshal failed. %v", err)
	}
	return data, nil
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
50 51
}

52
func (n *ProtoNode) getPBNode() *pb.PBNode {
53
	pbn := &pb.PBNode{}
54 55
	if len(n.links) > 0 {
		pbn.Links = make([]*pb.PBLink, len(n.links))
Jeromy's avatar
Jeromy committed
56
	}
57

58 59
	sort.Stable(LinkSlice(n.links)) // keep links sorted
	for i, l := range n.links {
60
		pbn.Links[i] = &pb.PBLink{}
Juan Batiz-Benet's avatar
gofmt  
Juan Batiz-Benet committed
61 62
		pbn.Links[i].Name = &l.Name
		pbn.Links[i].Tsize = &l.Size
63
		pbn.Links[i].Hash = l.Cid.Bytes()
Juan Batiz-Benet's avatar
gofmt  
Juan Batiz-Benet committed
64 65
	}

66 67
	if len(n.data) > 0 {
		pbn.Data = n.data
Jeromy's avatar
Jeromy committed
68
	}
Juan Batiz-Benet's avatar
gofmt  
Juan Batiz-Benet committed
69
	return pbn
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
70
}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
71

72
// EncodeProtobuf returns the encoded raw data version of a Node instance.
Juan Batiz-Benet's avatar
go lint  
Juan Batiz-Benet committed
73
// It may use a cached encoded version, unless the force flag is given.
74 75
func (n *ProtoNode) EncodeProtobuf(force bool) ([]byte, error) {
	sort.Stable(LinkSlice(n.links)) // keep links sorted
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
76
	if n.encoded == nil || force {
77
		n.cached = nil
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
78 79 80
		var err error
		n.encoded, err = n.Marshal()
		if err != nil {
81
			return nil, err
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
82
		}
83 84 85
	}

	if n.cached == nil {
86 87
		if n.Prefix.Codec == 0 { // unset
			n.Prefix = defaultCidPrefix
88
		}
89
		c, err := n.Prefix.Sum(n.encoded)
90 91 92 93 94
		if err != nil {
			return nil, err
		}

		n.cached = c
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
95 96 97 98
	}

	return n.encoded, nil
}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
99

Juan Batiz-Benet's avatar
go lint  
Juan Batiz-Benet committed
100
// Decoded decodes raw data and returns a new Node instance.
101 102
func DecodeProtobuf(encoded []byte) (*ProtoNode, error) {
	n := new(ProtoNode)
103
	err := n.unmarshal(encoded)
104 105 106 107
	if err != nil {
		return nil, fmt.Errorf("incorrectly formatted merkledag node: %s", err)
	}
	return n, nil
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
108
}