coding.go 2.6 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

9 10
	cid "gx/ipfs/QmTprEaAA2A9bst5XH7exuyi5KzNMK3SEDNN8rBDnKWcUS/go-cid"
	node "gx/ipfs/QmYNyRZJBUYPNrLszFmrBrPJbsBh2vMsefz5gnDpB5M1P6/go-ipld-format"
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
Jeromy's avatar
Jeromy committed
63 64 65
		if l.Cid != nil {
			pbn.Links[i].Hash = l.Cid.Bytes()
		}
Juan Batiz-Benet's avatar
gofmt  
Juan Batiz-Benet committed
66 67
	}

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

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

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

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

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

Juan Batiz-Benet's avatar
go lint  
Juan Batiz-Benet committed
102
// Decoded decodes raw data and returns a new Node instance.
103 104
func DecodeProtobuf(encoded []byte) (*ProtoNode, error) {
	n := new(ProtoNode)
105
	err := n.unmarshal(encoded)
106 107 108 109
	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
110
}