coding.go 2.53 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
	node "gx/ipfs/QmRSU5EqqWVZSNdbU51yXmVoF1uNw3JgTNB6RaiL7DZM16/go-ipld-node"
10
	u "gx/ipfs/Qmb912gdngC1UWwTkhuW8knyRbcWeu5kqkxBpveLmW8bSr/go-ipfs-util"
Jeromy's avatar
Jeromy committed
11
	cid "gx/ipfs/QmcTcsTvfaeEBRFo1TkFgT8sRmgi1n1LTZpecfVP8fzpGD/go-cid"
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
12 13 14 15 16
)

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

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

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

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

Juan Batiz-Benet's avatar
go lint  
Juan Batiz-Benet committed
42 43
// Marshal encodes a *Node instance into a new byte slice.
// The conversion uses an intermediate PBNode.
44
func (n *ProtoNode) Marshal() ([]byte, error) {
Juan Batiz-Benet's avatar
gofmt  
Juan Batiz-Benet committed
45 46 47 48 49 50
	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
51 52
}

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

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

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

73
// EncodeProtobuf returns the encoded raw data version of a Node instance.
Juan Batiz-Benet's avatar
go lint  
Juan Batiz-Benet committed
74
// It may use a cached encoded version, unless the force flag is given.
75 76
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
77
	if n.encoded == nil || force {
78
		n.cached = nil
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
79 80 81
		var err error
		n.encoded, err = n.Marshal()
		if err != nil {
82
			return nil, err
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
83
		}
84 85 86
	}

	if n.cached == nil {
Jeromy's avatar
Jeromy committed
87
		n.cached = cid.NewCidV0(u.Hash(n.encoded))
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
88 89 90 91
	}

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

Juan Batiz-Benet's avatar
go lint  
Juan Batiz-Benet committed
93
// Decoded decodes raw data and returns a new Node instance.
94 95
func DecodeProtobuf(encoded []byte) (*ProtoNode, error) {
	n := new(ProtoNode)
96
	err := n.unmarshal(encoded)
97 98 99 100
	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
101
}