Commit 63e44775 authored by Juan Batiz-Benet's avatar Juan Batiz-Benet

merkledag

parents
package merkledag
import (
"fmt"
mh "github.com/jbenet/go-multihash"
)
// for now, we use a PBNode intermediate thing.
// because native go objects are nice.
func (n *Node) Unmarshal(encoded []byte) error {
var pbn PBNode
if err := pbn.Unmarshal(encoded), err != nil {
return fmt.Errorf("Unmarshal failed. %v", err)
}
pbnl := pbn.Links()
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())
if err != nil {
return fmt.Errorf("Link hash is not valid multihash. %v", err)
}
}
n.Data = pbn.GetData()
return nil
}
func (n *Node) MarshalTo(encoded []byte) error {
pbn := n.getPBNode()
if err := pbn.MarshalTo(encoded), err != nil {
return fmt.Errorf("Marshal failed. %v", err)
}
return nil
}
func (n *Node) Marshal() ([]byte, error) {
pbn := n.getPBNode()
data, err := pbn.Marshal()
if err != nil {
return data, fmt.Errorf("Marshal failed. %v", err)
}
return data, nil
}
func (n *Node) getPBNode() *PBNode {
pbn := &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.Data = &n.Data
return pbn, nil
}
package merkledag
import (
mh "github.com/jbenet/go-multihash"
)
// A node in the IPFS Merkle DAG.
// nodes have opaque data and a set of navigable links.
type Node {
Links []*Link
Data []byte
}
// An IPFS Merkle DAG Link
type Link {
// utf string name. should be unique per object
Name string // utf8
// cumulative size of target object
Size uint64
// multihash of the target object
Hash mh.Multihash
}
type EncodedNode []byte
func (n *Node) Size() uint64 {
uint64 s = len(n.Encode())
for _, l := range(n.Links) {
s += l.Size
}
}
package merkledag
import "code.google.com/p/gogoprotobuf/gogoproto/gogo.proto";
option (gogoproto.gostring_all) = true;
option (gogoproto.equal_all) = true;
option (gogoproto.verbose_equal_all) = true;
option (gogoproto.goproto_stringer_all) = false;
option (gogoproto.stringer_all) = true;
option (gogoproto.populate_all) = true;
option (gogoproto.testgen_all) = true;
option (gogoproto.benchgen_all) = true;
option (gogoproto.marshaler_all) = true;
option (gogoproto.sizer_all) = true;
option (gogoproto.unmarshaler_all) = true;
// An IPFS MerkleDAG Node
message PBNode {
// refs to other objects
repeated Link Links = 2;
// opaque user data
optional bytes Data = 1;
}
// An IPFS MerkleDAG Link
message PBLink {
// multihash of the target object
optional bytes Hash = 1;
// utf string name. should be unique per object
optional string Name = 2;
// cumulative size of target object
optional uint64 Size = 3;
}
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