Skip to content

Commit

Permalink
merkledag
Browse files Browse the repository at this point in the history
  • Loading branch information
jbenet committed Jul 4, 2014
0 parents commit 63e4477
Show file tree
Hide file tree
Showing 3 changed files with 137 additions and 0 deletions.
61 changes: 61 additions & 0 deletions coding.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
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
}

36 changes: 36 additions & 0 deletions merkledag.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
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
}
}
40 changes: 40 additions & 0 deletions merkledag.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
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;
}

0 comments on commit 63e4477

Please sign in to comment.