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

go lint

link errors left:
- protocol buffers output is not lint-friendly
parent 55f6c9ab
...@@ -8,6 +8,8 @@ import ( ...@@ -8,6 +8,8 @@ import (
// for now, we use a PBNode intermediate thing. // for now, we use a PBNode intermediate thing.
// because native go objects are nice. // because native go objects are nice.
// Unmarshal decodes raw data into a *Node instance.
// The conversion uses an intermediate PBNode.
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 {
...@@ -29,6 +31,8 @@ func (n *Node) Unmarshal(encoded []byte) error { ...@@ -29,6 +31,8 @@ func (n *Node) Unmarshal(encoded []byte) error {
return nil return nil
} }
// MarshalTo encodes a *Node instance into a given byte slice.
// The conversion uses an intermediate PBNode.
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 {
...@@ -37,6 +41,8 @@ func (n *Node) MarshalTo(encoded []byte) error { ...@@ -37,6 +41,8 @@ func (n *Node) MarshalTo(encoded []byte) error {
return nil return nil
} }
// Marshal encodes a *Node instance into a new byte slice.
// The conversion uses an intermediate PBNode.
func (n *Node) Marshal() ([]byte, error) { func (n *Node) Marshal() ([]byte, error) {
pbn := n.getPBNode() pbn := n.getPBNode()
data, err := pbn.Marshal() data, err := pbn.Marshal()
...@@ -60,6 +66,8 @@ func (n *Node) getPBNode() *PBNode { ...@@ -60,6 +66,8 @@ func (n *Node) getPBNode() *PBNode {
return pbn return pbn
} }
// Encoded returns the encoded raw data version of a Node instance.
// It may use a cached encoded version, unless the force flag is given.
func (n *Node) Encoded(force bool) ([]byte, error) { func (n *Node) Encoded(force bool) ([]byte, error) {
if n.encoded == nil || force { if n.encoded == nil || force {
var err error var err error
...@@ -72,6 +80,7 @@ func (n *Node) Encoded(force bool) ([]byte, error) { ...@@ -72,6 +80,7 @@ func (n *Node) Encoded(force bool) ([]byte, error) {
return n.encoded, nil return n.encoded, nil
} }
// Decoded decodes raw data and returns a new Node instance.
func Decoded(encoded []byte) (*Node, error) { func Decoded(encoded []byte) (*Node, error) {
n := &Node{} n := &Node{}
err := n.Unmarshal(encoded) err := n.Unmarshal(encoded)
......
...@@ -7,11 +7,12 @@ import ( ...@@ -7,11 +7,12 @@ import (
mh "github.com/jbenet/go-multihash" mh "github.com/jbenet/go-multihash"
) )
// can't use []byte/Multihash for keys :( // NodeMap maps u.Keys to Nodes.
// so have to convert Multihash bytes to string // We cannot use []byte/Multihash for keys :(
// so have to convert Multihash bytes to string (u.Key)
type NodeMap map[u.Key]*Node type NodeMap map[u.Key]*Node
// A node in the IPFS Merkle DAG. // Node represents 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 struct { type Node struct {
Links []*Link Links []*Link
...@@ -21,7 +22,7 @@ type Node struct { ...@@ -21,7 +22,7 @@ type Node struct {
encoded []byte encoded []byte
} }
// An IPFS Merkle DAG Link // Link represents an IPFS Merkle DAG Link between Nodes.
type Link struct { 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
...@@ -36,6 +37,7 @@ type Link struct { ...@@ -36,6 +37,7 @@ type Link struct {
Node *Node Node *Node
} }
// AddNodeLink adds a link to another node.
func (n *Node) AddNodeLink(name string, that *Node) error { func (n *Node) AddNodeLink(name string, that *Node) error {
s, err := that.Size() s, err := that.Size()
if err != nil { if err != nil {
...@@ -55,6 +57,8 @@ func (n *Node) AddNodeLink(name string, that *Node) error { ...@@ -55,6 +57,8 @@ func (n *Node) AddNodeLink(name string, that *Node) error {
return nil return nil
} }
// Size returns the total size of the data addressed by node,
// including the total sizes of references.
func (n *Node) Size() (uint64, error) { func (n *Node) Size() (uint64, error) {
b, err := n.Encoded(false) b, err := n.Encoded(false)
if err != nil { if err != nil {
...@@ -68,6 +72,7 @@ func (n *Node) Size() (uint64, error) { ...@@ -68,6 +72,7 @@ func (n *Node) Size() (uint64, error) {
return s, nil return s, nil
} }
// Multihash hashes the encoded data of this node.
func (n *Node) Multihash() (mh.Multihash, error) { func (n *Node) Multihash() (mh.Multihash, error) {
b, err := n.Encoded(false) b, err := n.Encoded(false)
if err != nil { if err != nil {
...@@ -77,18 +82,20 @@ func (n *Node) Multihash() (mh.Multihash, error) { ...@@ -77,18 +82,20 @@ func (n *Node) Multihash() (mh.Multihash, error) {
return u.Hash(b) return u.Hash(b)
} }
// Key returns the Multihash as a key, for maps.
func (n *Node) Key() (u.Key, error) { func (n *Node) Key() (u.Key, error) {
h, err := n.Multihash() h, err := n.Multihash()
return u.Key(h), err return u.Key(h), err
} }
// An IPFS Merkle DAG service. // DAGService is an IPFS Merkle DAG service.
// the root is virtual (like a forest) // - the root is virtual (like a forest)
// stores nodes' data in a blockService // - stores nodes' data in a BlockService
type DAGService struct { type DAGService struct {
Blocks *blocks.BlockService Blocks *blocks.BlockService
} }
// Put adds a node to the DAGService, storing the block in the BlockService
func (n *DAGService) Put(nd *Node) (u.Key, error) { func (n *DAGService) Put(nd *Node) (u.Key, error) {
if n == nil { if n == nil {
return "", fmt.Errorf("DAGService is nil") return "", fmt.Errorf("DAGService is nil")
...@@ -107,6 +114,7 @@ func (n *DAGService) Put(nd *Node) (u.Key, error) { ...@@ -107,6 +114,7 @@ func (n *DAGService) Put(nd *Node) (u.Key, error) {
return n.Blocks.AddBlock(b) return n.Blocks.AddBlock(b)
} }
// Get retrieves a node from the DAGService, fetching the block in the BlockService
func (n *DAGService) Get(k u.Key) (*Node, error) { func (n *DAGService) Get(k u.Key) (*Node, error) {
if n == nil { if n == nil {
return nil, fmt.Errorf("DAGService is nil") return nil, fmt.Errorf("DAGService is nil")
......
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