-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 63e4477
Showing
3 changed files
with
137 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |