coding.go 2.35 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
	mh "gx/ipfs/QmYf7ng2hG5XBtJA3tN34DQ2GUN5HNksEw1rLDkmr6vGku/go-multihash"
8

9
	pb "github.com/ipfs/go-ipfs/merkledag/pb"
10
	u "gx/ipfs/QmZNVWh8LLjAavuQ2JXuFmuYH3C11xo988vSgp7UQrTRj1/go-ipfs-util"
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 *Node) unmarshal(encoded []byte) error {
19
	var pbn pb.PBNode
Juan Batiz-Benet's avatar
gofmt  
Juan Batiz-Benet committed
20 21 22 23 24 25 26 27 28 29
	if err := pbn.Unmarshal(encoded); err != nil {
		return fmt.Errorf("Unmarshal failed. %v", err)
	}

	pbnl := pbn.GetLinks()
	n.Links = make([]*Link, len(pbnl))
	for i, l := range pbnl {
		n.Links[i] = &Link{Name: l.GetName(), Size: l.GetTsize()}
		h, err := mh.Cast(l.GetHash())
		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 33
		}
		n.Links[i].Hash = h
	}
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()
	return nil
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
38 39
}

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

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

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

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

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

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

Juan Batiz-Benet's avatar
go lint  
Juan Batiz-Benet committed
87
// Decoded decodes raw data and returns a new Node instance.
88
func DecodeProtobuf(encoded []byte) (*Node, error) {
89
	n := new(Node)
90
	err := n.unmarshal(encoded)
91 92 93 94
	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
95
}