Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
What's new
10
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Open sidebar
dms3
go-merkledag
Commits
7744373c
Commit
7744373c
authored
Oct 08, 2014
by
Jeromy
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
add more comments!
parent
372dd226
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
16 additions
and
3 deletions
+16
-3
dagreader.go
dagreader.go
+16
-3
No files found.
dagreader.go
View file @
7744373c
...
@@ -20,6 +20,8 @@ type DagReader struct {
...
@@ -20,6 +20,8 @@ type DagReader struct {
buf
*
bytes
.
Buffer
buf
*
bytes
.
Buffer
}
}
// NewDagReader creates a new reader object that reads the data represented by the given
// node, using the passed in DAGService for data retreival
func
NewDagReader
(
n
*
Node
,
serv
*
DAGService
)
(
io
.
Reader
,
error
)
{
func
NewDagReader
(
n
*
Node
,
serv
*
DAGService
)
(
io
.
Reader
,
error
)
{
pb
:=
new
(
ft
.
PBData
)
pb
:=
new
(
ft
.
PBData
)
err
:=
proto
.
Unmarshal
(
n
.
Data
,
pb
)
err
:=
proto
.
Unmarshal
(
n
.
Data
,
pb
)
...
@@ -29,6 +31,7 @@ func NewDagReader(n *Node, serv *DAGService) (io.Reader, error) {
...
@@ -29,6 +31,7 @@ func NewDagReader(n *Node, serv *DAGService) (io.Reader, error) {
switch
pb
.
GetType
()
{
switch
pb
.
GetType
()
{
case
ft
.
PBData_Directory
:
case
ft
.
PBData_Directory
:
// Dont allow reading directories
return
nil
,
ErrIsDir
return
nil
,
ErrIsDir
case
ft
.
PBData_File
:
case
ft
.
PBData_File
:
return
&
DagReader
{
return
&
DagReader
{
...
@@ -37,12 +40,15 @@ func NewDagReader(n *Node, serv *DAGService) (io.Reader, error) {
...
@@ -37,12 +40,15 @@ func NewDagReader(n *Node, serv *DAGService) (io.Reader, error) {
buf
:
bytes
.
NewBuffer
(
pb
.
GetData
()),
buf
:
bytes
.
NewBuffer
(
pb
.
GetData
()),
},
nil
},
nil
case
ft
.
PBData_Raw
:
case
ft
.
PBData_Raw
:
// Raw block will just be a single level, return a byte buffer
return
bytes
.
NewBuffer
(
pb
.
GetData
()),
nil
return
bytes
.
NewBuffer
(
pb
.
GetData
()),
nil
default
:
default
:
panic
(
"Unrecognized node type!"
)
return
nil
,
ft
.
ErrUnrecognizedType
}
}
}
}
// Follows the next link in line and loads it from the DAGService,
// setting the next buffer to read from
func
(
dr
*
DagReader
)
precalcNextBuf
()
error
{
func
(
dr
*
DagReader
)
precalcNextBuf
()
error
{
if
dr
.
position
>=
len
(
dr
.
node
.
Links
)
{
if
dr
.
position
>=
len
(
dr
.
node
.
Links
)
{
return
io
.
EOF
return
io
.
EOF
...
@@ -65,7 +71,7 @@ func (dr *DagReader) precalcNextBuf() error {
...
@@ -65,7 +71,7 @@ func (dr *DagReader) precalcNextBuf() error {
switch
pb
.
GetType
()
{
switch
pb
.
GetType
()
{
case
ft
.
PBData_Directory
:
case
ft
.
PBData_Directory
:
panic
(
"Why is there a directory under a file?"
)
return
ft
.
ErrInvalidDirLocation
case
ft
.
PBData_File
:
case
ft
.
PBData_File
:
//TODO: this *should* work, needs testing first
//TODO: this *should* work, needs testing first
//return NewDagReader(nxt, dr.serv)
//return NewDagReader(nxt, dr.serv)
...
@@ -74,11 +80,12 @@ func (dr *DagReader) precalcNextBuf() error {
...
@@ -74,11 +80,12 @@ func (dr *DagReader) precalcNextBuf() error {
dr
.
buf
=
bytes
.
NewBuffer
(
pb
.
GetData
())
dr
.
buf
=
bytes
.
NewBuffer
(
pb
.
GetData
())
return
nil
return
nil
default
:
default
:
panic
(
"Unrecognized node type!"
)
return
ft
.
ErrUnrecognizedType
}
}
}
}
func
(
dr
*
DagReader
)
Read
(
b
[]
byte
)
(
int
,
error
)
{
func
(
dr
*
DagReader
)
Read
(
b
[]
byte
)
(
int
,
error
)
{
// If no cached buffer, load one
if
dr
.
buf
==
nil
{
if
dr
.
buf
==
nil
{
err
:=
dr
.
precalcNextBuf
()
err
:=
dr
.
precalcNextBuf
()
if
err
!=
nil
{
if
err
!=
nil
{
...
@@ -87,16 +94,22 @@ func (dr *DagReader) Read(b []byte) (int, error) {
...
@@ -87,16 +94,22 @@ func (dr *DagReader) Read(b []byte) (int, error) {
}
}
total
:=
0
total
:=
0
for
{
for
{
// Attempt to fill bytes from cached buffer
n
,
err
:=
dr
.
buf
.
Read
(
b
[
total
:
])
n
,
err
:=
dr
.
buf
.
Read
(
b
[
total
:
])
total
+=
n
total
+=
n
if
err
!=
nil
{
if
err
!=
nil
{
// EOF is expected
if
err
!=
io
.
EOF
{
if
err
!=
io
.
EOF
{
return
total
,
err
return
total
,
err
}
}
}
}
// If weve read enough bytes, return
if
total
==
len
(
b
)
{
if
total
==
len
(
b
)
{
return
total
,
nil
return
total
,
nil
}
}
// Otherwise, load up the next block
err
=
dr
.
precalcNextBuf
()
err
=
dr
.
precalcNextBuf
()
if
err
!=
nil
{
if
err
!=
nil
{
return
total
,
err
return
total
,
err
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment