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

coding with protobuf

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