Commit 6db5ce44 authored by Jeromy's avatar Jeromy

rework dagreader to have a dagservice for node resolution

parent 24257b36
......@@ -5,20 +5,22 @@ import (
"errors"
"io"
"code.google.com/p/goprotobuf/proto"
proto "code.google.com/p/goprotobuf/proto"
u "github.com/jbenet/go-ipfs/util"
)
var ErrIsDir = errors.New("this dag node is a directory.")
// DagReader provides a way to easily read the data contained in a dag.
type DagReader struct {
serv *DAGService
node *Node
position int
buf *bytes.Buffer
thisData []byte
}
func NewDagReader(n *Node) (io.Reader, error) {
func NewDagReader(n *Node, serv *DAGService) (io.Reader, error) {
pb := new(PBData)
err := proto.Unmarshal(n.Data, pb)
if err != nil {
......@@ -31,6 +33,7 @@ func NewDagReader(n *Node) (io.Reader, error) {
return &DagReader{
node: n,
thisData: pb.GetData(),
serv: serv,
}, nil
case PBData_Raw:
return bytes.NewBuffer(pb.GetData()), nil
......@@ -46,8 +49,11 @@ func (dr *DagReader) precalcNextBuf() error {
nxtLink := dr.node.Links[dr.position]
nxt := nxtLink.Node
if nxt == nil {
//TODO: should use dagservice or something to get needed block
return errors.New("Link to nil node! Tree not fully expanded!")
nxtNode, err := dr.serv.Get(u.Key(nxtLink.Hash))
if err != nil {
return err
}
nxt = nxtNode
}
pb := new(PBData)
err := proto.Unmarshal(nxt.Data, pb)
......@@ -96,3 +102,29 @@ func (dr *DagReader) Read(b []byte) (int, error) {
}
}
}
/*
func (dr *DagReader) Seek(offset int64, whence int) (int64, error) {
switch whence {
case os.SEEK_SET:
for i := 0; i < len(dr.node.Links); i++ {
nsize := dr.node.Links[i].Size - 8
if offset > nsize {
offset -= nsize
} else {
break
}
}
dr.position = i
err := dr.precalcNextBuf()
if err != nil {
return 0, err
}
case os.SEEK_CUR:
case os.SEEK_END:
default:
return 0, errors.New("invalid whence")
}
return 0, nil
}
*/
......@@ -96,6 +96,8 @@ func (n *Node) Key() (u.Key, error) {
// DAGService is an IPFS Merkle DAG service.
// - the root is virtual (like a forest)
// - stores nodes' data in a BlockService
// TODO: should cache Nodes that are in memory, and be
// able to free some of them when vm pressure is high
type DAGService struct {
Blocks *bserv.BlockService
}
......
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