Commit dcdb1e32 authored by Juan Batiz-Benet's avatar Juan Batiz-Benet

coding with protobuf

parent 75719112
......@@ -10,18 +10,19 @@ import (
func (n *Node) Unmarshal(encoded []byte) error {
var pbn PBNode
if err := pbn.Unmarshal(encoded), err != nil {
if err := pbn.Unmarshal(encoded); err != nil {
return fmt.Errorf("Unmarshal failed. %v", err)
}
pbnl := pbn.Links()
pbnl := pbn.GetLinks()
n.Links = make([]*Link, len(pbnl))
for i, l := range(pbnl) {
n.Links[i] = &Link{Name: l.GetName(), Size: l.GetSize()}
n.Links[i].Hash, err := mh.Cast(l.GetHash())
n.Links[i] = &Link{Name: l.GetName(), Size: l.GetTsize()}
h, err := mh.Cast(l.GetHash())
if err != nil {
return fmt.Errorf("Link hash is not valid multihash. %v", err)
}
n.Links[i].Hash = h
}
n.Data = pbn.GetData()
......@@ -30,7 +31,7 @@ func (n *Node) Unmarshal(encoded []byte) error {
func (n *Node) MarshalTo(encoded []byte) error {
pbn := n.getPBNode()
if err := pbn.MarshalTo(encoded), err != nil {
if _, err := pbn.MarshalTo(encoded); err != nil {
return fmt.Errorf("Marshal failed. %v", err)
}
return nil
......@@ -50,12 +51,12 @@ func (n *Node) getPBNode() *PBNode {
pbn.Links = make([]*PBLink, len(n.Links))
for i, l := range(n.Links) {
pbn.Links[i] = &PBLink{}
n.Links[i].Name = &l.Name
n.Links[i].Size = l.Size
n.Links[i].Hash = &[]byte(l.Hash)
pbn.Links[i].Name = &l.Name
pbn.Links[i].Tsize = &l.Size
pbn.Links[i].Hash = []byte(l.Hash)
}
pbn.Data = &n.Data
return pbn, nil
pbn.Data = n.Data
return pbn
}
......@@ -6,14 +6,14 @@ import (
// A node in the IPFS Merkle DAG.
// nodes have opaque data and a set of navigable links.
type Node {
type Node struct {
Links []*Link
Data []byte
}
// An IPFS Merkle DAG Link
type Link {
type Link struct {
// utf string name. should be unique per object
Name string // utf8
......@@ -28,9 +28,15 @@ type Link {
type EncodedNode []byte
func (n *Node) Size() uint64 {
uint64 s = len(n.Encode())
func (n *Node) Size() (uint64, error) {
d, err := n.Marshal()
if err != nil {
return 0, err
}
s := uint64(len(d))
for _, l := range(n.Links) {
s += l.Size
}
return s, nil
}
......@@ -25,7 +25,7 @@ message PBLink {
optional string Name = 2;
// cumulative size of target object
optional uint64 Size = 3;
optional uint64 Tsize = 3;
}
// An IPFS MerkleDAG Node
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment