coding.go 2.49 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
	cid "gx/ipfs/QmXUuRadqDq5BuFWzVU6VuKaSjTcNm1gNCtLvvP1TJCW4z/go-cid"
10 11
	mh "gx/ipfs/QmYDds3421prZgqKbLpEK7T9Aa2eVdQ7o3YarX1LVLdP2J/go-multihash"
	u "gx/ipfs/Qmb912gdngC1UWwTkhuW8knyRbcWeu5kqkxBpveLmW8bSr/go-ipfs-util"
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 *Node) unmarshal(encoded []byte) error {
20
	var pbn pb.PBNode
Juan Batiz-Benet's avatar
gofmt  
Juan Batiz-Benet committed
21 22 23 24 25 26 27 28 29 30
	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 {
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 34
		}
		n.Links[i].Hash = h
	}
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.
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
44
func (n *Node) 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 54
func (n *Node) getPBNode() *pb.PBNode {
	pbn := &pb.PBNode{}
Jeromy's avatar
Jeromy committed
55 56 57
	if len(n.Links) > 0 {
		pbn.Links = make([]*pb.PBLink, len(n.Links))
	}
58 59

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

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
func (n *Node) EncodeProtobuf(force bool) ([]byte, error) {
76
	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
func DecodeProtobuf(encoded []byte) (*Node, error) {
95
	n := new(Node)
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
}