@cryptotaxi247 / kubo / commits / 9cf046dd9

coding with protobuf

Juan Batiz-Benet committed Jul 4, 2014 at 15:29 UTC 9cf046dd9d99a3d12a5bf4f3fbcbacf0437271e4
3 files changed +22 -15
merkledag/coding.go
+11 -10
@@ -10,18 +10,19 @@ import (
10
11 func (n *Node) Unmarshal(encoded []byte) error {
12 var pbn PBNode
13 - if err := pbn.Unmarshal(encoded), err != nil {
13 + if err := pbn.Unmarshal(encoded); err != nil {
14 return fmt.Errorf("Unmarshal failed. %v", err)
15 }
16
17 - pbnl := pbn.Links()
17 + pbnl := pbn.GetLinks()
18 n.Links = make([]*Link, len(pbnl))
19 for i, l := range(pbnl) {
20 - n.Links[i] = &Link{Name: l.GetName(), Size: l.GetSize()}
21 - n.Links[i].Hash, err := mh.Cast(l.GetHash())
20 + n.Links[i] = &Link{Name: l.GetName(), Size: l.GetTsize()}
21 + h, err := mh.Cast(l.GetHash())
22 if err != nil {
23 return fmt.Errorf("Link hash is not valid multihash. %v", err)
24 }
25 + n.Links[i].Hash = h
26 }
27
28 n.Data = pbn.GetData()
@@ -30,7 +31,7 @@ func (n *Node) Unmarshal(encoded []byte) error {
31
32 func (n *Node) MarshalTo(encoded []byte) error {
33 pbn := n.getPBNode()
33 - if err := pbn.MarshalTo(encoded), err != nil {
34 + if _, err := pbn.MarshalTo(encoded); err != nil {
35 return fmt.Errorf("Marshal failed. %v", err)
36 }
37 return nil
@@ -50,12 +51,12 @@ func (n *Node) getPBNode() *PBNode {
51 pbn.Links = make([]*PBLink, len(n.Links))
52 for i, l := range(n.Links) {
53 pbn.Links[i] = &PBLink{}
53 - n.Links[i].Name = &l.Name
54 - n.Links[i].Size = l.Size
55 - n.Links[i].Hash = &[]byte(l.Hash)
54 + pbn.Links[i].Name = &l.Name
55 + pbn.Links[i].Tsize = &l.Size
56 + pbn.Links[i].Hash = []byte(l.Hash)
57 }
58
58 - pbn.Data = &n.Data
59 - return pbn, nil
59 + pbn.Data = n.Data
60 + return pbn
61 }
62
merkledag/merkledag.go
+10 -4
@@ -6,14 +6,14 @@ import (
6
7 // A node in the IPFS Merkle DAG.
8 // nodes have opaque data and a set of navigable links.
9 -type Node {
9 +type Node struct {
10 Links []*Link
11 Data []byte
12 }
13
14
15 // An IPFS Merkle DAG Link
16 -type Link {
16 +type Link struct {
17 // utf string name. should be unique per object
18 Name string // utf8
19
@@ -28,9 +28,15 @@ type Link {
28 type EncodedNode []byte
29
30
31 -func (n *Node) Size() uint64 {
32 - uint64 s = len(n.Encode())
31 +func (n *Node) Size() (uint64, error) {
32 + d, err := n.Marshal()
33 + if err != nil {
34 + return 0, err
35 + }
36 +
37 + s := uint64(len(d))
38 for _, l := range(n.Links) {
39 s += l.Size
40 }
41 + return s, nil
42 }
merkledag/node.proto
+1 -1
@@ -25,7 +25,7 @@ message PBLink {
25 optional string Name = 2;
26
27 // cumulative size of target object
28 - optional uint64 Size = 3;
28 + optional uint64 Tsize = 3;
29 }
30
31 // An IPFS MerkleDAG Node