Commit 55f6c9ab authored by Juan Batiz-Benet's avatar Juan Batiz-Benet

abstracted merkledag service

parent eb9b75fb
package merkledag
import (
"fmt"
blocks "github.com/jbenet/go-ipfs/blocks"
u "github.com/jbenet/go-ipfs/util"
mh "github.com/jbenet/go-multihash"
)
......@@ -79,3 +81,41 @@ 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
type DAGService struct {
Blocks *blocks.BlockService
}
func (n *DAGService) Put(nd *Node) (u.Key, error) {
if n == nil {
return "", fmt.Errorf("DAGService is nil")
}
d, err := nd.Encoded(false)
if err != nil {
return "", err
}
b, err := blocks.NewBlock(d)
if err != nil {
return "", err
}
return n.Blocks.AddBlock(b)
}
func (n *DAGService) Get(k u.Key) (*Node, error) {
if n == nil {
return nil, fmt.Errorf("DAGService is nil")
}
b, err := n.Blocks.GetBlock(k)
if err != nil {
return nil, err
}
return Decoded(b.Data)
}
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