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 (
// for now, we use a PBNode intermediate thing.
// 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 {
var pbn PBNode
if err := pbn.Unmarshal(encoded); err != nil {
......@@ -29,6 +31,8 @@ func (n *Node) Unmarshal(encoded []byte) error {
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 {
pbn := n.getPBNode()
if _, err := pbn.MarshalTo(encoded); err != nil {
......@@ -37,6 +41,8 @@ func (n *Node) MarshalTo(encoded []byte) error {
return nil
}
// Marshal encodes a *Node instance into a new byte slice.
// The conversion uses an intermediate PBNode.
func (n *Node) Marshal() ([]byte, error) {
pbn := n.getPBNode()
data, err := pbn.Marshal()
......@@ -60,6 +66,8 @@ func (n *Node) getPBNode() *PBNode {
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) {
if n.encoded == nil || force {
var err error
......@@ -72,6 +80,7 @@ func (n *Node) Encoded(force bool) ([]byte, error) {
return n.encoded, nil
}
// Decoded decodes raw data and returns a new Node instance.
func Decoded(encoded []byte) (*Node, error) {
n := &Node{}
err := n.Unmarshal(encoded)
......
......@@ -7,11 +7,12 @@ import (
mh "github.com/jbenet/go-multihash"
)
// can't use []byte/Multihash for keys :(
// so have to convert Multihash bytes to string
// NodeMap maps u.Keys to Nodes.
// We cannot use []byte/Multihash for keys :(
// so have to convert Multihash bytes to string (u.Key)
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.
type Node struct {
Links []*Link
......@@ -21,7 +22,7 @@ type Node struct {
encoded []byte
}
// An IPFS Merkle DAG Link
// Link represents an IPFS Merkle DAG Link between Nodes.
type Link struct {
// utf string name. should be unique per object
Name string // utf8
......@@ -36,6 +37,7 @@ type Link struct {
Node *Node
}
// AddNodeLink adds a link to another node.
func (n *Node) AddNodeLink(name string, that *Node) error {
s, err := that.Size()
if err != nil {
......@@ -55,6 +57,8 @@ func (n *Node) AddNodeLink(name string, that *Node) error {
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) {
b, err := n.Encoded(false)
if err != nil {
......@@ -68,6 +72,7 @@ func (n *Node) Size() (uint64, error) {
return s, nil
}
// Multihash hashes the encoded data of this node.
func (n *Node) Multihash() (mh.Multihash, error) {
b, err := n.Encoded(false)
if err != nil {
......@@ -77,18 +82,20 @@ func (n *Node) Multihash() (mh.Multihash, error) {
return u.Hash(b)
}
// Key returns the Multihash as a key, for maps.
func (n *Node) Key() (u.Key, error) {
h, err := n.Multihash()
return u.Key(h), err
}
// An IPFS Merkle DAG service.
// the root is virtual (like a forest)
// stores nodes' data in a blockService
// DAGService is an IPFS Merkle DAG service.
// - the root is virtual (like a forest)
// - stores nodes' data in a BlockService
type DAGService struct {
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) {
if n == nil {
return "", fmt.Errorf("DAGService is nil")
......@@ -107,6 +114,7 @@ func (n *DAGService) Put(nd *Node) (u.Key, error) {
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) {
if n == 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