Commit 030291dc authored by Juan Batiz-Benet's avatar Juan Batiz-Benet

NewDagReader: return DagReader for more functions

Since the DagReader has lots of useful information (like sizes etc)
it's good to be able to use it.
parent 767e9462
...@@ -54,7 +54,7 @@ type ReadSeekCloser interface { ...@@ -54,7 +54,7 @@ type ReadSeekCloser interface {
// NewDagReader creates a new reader object that reads the data represented by the given // NewDagReader creates a new reader object that reads the data represented by the given
// node, using the passed in DAGService for data retreival // node, using the passed in DAGService for data retreival
func NewDagReader(ctx context.Context, n *mdag.Node, serv mdag.DAGService) (ReadSeekCloser, error) { func NewDagReader(ctx context.Context, n *mdag.Node, serv mdag.DAGService) (*DagReader, error) {
pb := new(ftpb.Data) pb := new(ftpb.Data)
err := proto.Unmarshal(n.Data, pb) err := proto.Unmarshal(n.Data, pb)
if err != nil { if err != nil {
...@@ -65,6 +65,8 @@ func NewDagReader(ctx context.Context, n *mdag.Node, serv mdag.DAGService) (Read ...@@ -65,6 +65,8 @@ func NewDagReader(ctx context.Context, n *mdag.Node, serv mdag.DAGService) (Read
case ftpb.Data_Directory: case ftpb.Data_Directory:
// Dont allow reading directories // Dont allow reading directories
return nil, ErrIsDir return nil, ErrIsDir
case ftpb.Data_Raw:
fallthrough
case ftpb.Data_File: case ftpb.Data_File:
fctx, cancel := context.WithCancel(ctx) fctx, cancel := context.WithCancel(ctx)
promises := serv.GetDAG(fctx, n) promises := serv.GetDAG(fctx, n)
...@@ -77,9 +79,6 @@ func NewDagReader(ctx context.Context, n *mdag.Node, serv mdag.DAGService) (Read ...@@ -77,9 +79,6 @@ func NewDagReader(ctx context.Context, n *mdag.Node, serv mdag.DAGService) (Read
cancel: cancel, cancel: cancel,
pbdata: pb, pbdata: pb,
}, nil }, nil
case ftpb.Data_Raw:
// Raw block will just be a single level, return a byte buffer
return NewRSNCFromBytes(pb.GetData()), nil
default: default:
return nil, ft.ErrUnrecognizedType return nil, ft.ErrUnrecognizedType
} }
...@@ -123,6 +122,11 @@ func (dr *DagReader) precalcNextBuf() error { ...@@ -123,6 +122,11 @@ func (dr *DagReader) precalcNextBuf() error {
} }
} }
// Size return the total length of the data from the DAG structured file.
func (dr *DagReader) Size() int64 {
return int64(dr.pbdata.GetFilesize())
}
// Read reads data from the DAG structured file // Read reads data from the DAG structured file
func (dr *DagReader) Read(b []byte) (int, error) { func (dr *DagReader) Read(b []byte) (int, error) {
// If no cached buffer, load one // If no cached buffer, load one
......
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