Commit 6db5ce44 authored by Jeromy's avatar Jeromy

rework dagreader to have a dagservice for node resolution

parent 24257b36
...@@ -5,20 +5,22 @@ import ( ...@@ -5,20 +5,22 @@ import (
"errors" "errors"
"io" "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.") var ErrIsDir = errors.New("this dag node is a directory.")
// DagReader provides a way to easily read the data contained in a dag. // DagReader provides a way to easily read the data contained in a dag.
type DagReader struct { type DagReader struct {
serv *DAGService
node *Node node *Node
position int position int
buf *bytes.Buffer buf *bytes.Buffer
thisData []byte thisData []byte
} }
func NewDagReader(n *Node) (io.Reader, error) { func NewDagReader(n *Node, serv *DAGService) (io.Reader, error) {
pb := new(PBData) pb := new(PBData)
err := proto.Unmarshal(n.Data, pb) err := proto.Unmarshal(n.Data, pb)
if err != nil { if err != nil {
...@@ -31,6 +33,7 @@ func NewDagReader(n *Node) (io.Reader, error) { ...@@ -31,6 +33,7 @@ func NewDagReader(n *Node) (io.Reader, error) {
return &DagReader{ return &DagReader{
node: n, node: n,
thisData: pb.GetData(), thisData: pb.GetData(),
serv: serv,
}, nil }, nil
case PBData_Raw: case PBData_Raw:
return bytes.NewBuffer(pb.GetData()), nil return bytes.NewBuffer(pb.GetData()), nil
...@@ -46,8 +49,11 @@ func (dr *DagReader) precalcNextBuf() error { ...@@ -46,8 +49,11 @@ func (dr *DagReader) precalcNextBuf() error {
nxtLink := dr.node.Links[dr.position] nxtLink := dr.node.Links[dr.position]
nxt := nxtLink.Node nxt := nxtLink.Node
if nxt == nil { if nxt == nil {
//TODO: should use dagservice or something to get needed block nxtNode, err := dr.serv.Get(u.Key(nxtLink.Hash))
return errors.New("Link to nil node! Tree not fully expanded!") if err != nil {
return err
}
nxt = nxtNode
} }
pb := new(PBData) pb := new(PBData)
err := proto.Unmarshal(nxt.Data, pb) err := proto.Unmarshal(nxt.Data, pb)
...@@ -96,3 +102,29 @@ func (dr *DagReader) Read(b []byte) (int, error) { ...@@ -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) { ...@@ -96,6 +96,8 @@ func (n *Node) Key() (u.Key, error) {
// DAGService is 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
// TODO: should cache Nodes that are in memory, and be
// able to free some of them when vm pressure is high
type DAGService struct { type DAGService struct {
Blocks *bserv.BlockService 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