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-dms3-chunker
Commits
03118146
Unverified
Commit
03118146
authored
Mar 26, 2020
by
Peter Rabbitson
Committed by
GitHub
Mar 26, 2020
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #22 from ipfs/fix/buzhash-empty-block
fix: don't return an empty block at the end
parents
72733e3f
b4e4e73e
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
42 additions
and
21 deletions
+42
-21
buzhash.go
buzhash.go
+9
-2
buzhash_test.go
buzhash_test.go
+33
-19
No files found.
buzhash.go
View file @
03118146
...
...
@@ -40,9 +40,16 @@ func (b *Buzhash) NextBytes() ([]byte, error) {
n
,
err
:=
io
.
ReadFull
(
b
.
r
,
b
.
buf
[
b
.
n
:
])
if
err
!=
nil
{
if
err
==
io
.
ErrUnexpectedEOF
||
err
==
io
.
EOF
{
if
b
.
n
+
n
<
buzMin
{
buffered
:=
b
.
n
+
n
if
buffered
<
buzMin
{
b
.
err
=
io
.
EOF
res
:=
make
([]
byte
,
b
.
n
+
n
)
// Read nothing? Don't return an empty block.
if
buffered
==
0
{
pool
.
Put
(
b
.
buf
)
b
.
buf
=
nil
return
nil
,
b
.
err
}
res
:=
make
([]
byte
,
buffered
)
copy
(
res
,
b
.
buf
)
pool
.
Put
(
b
.
buf
)
...
...
buzhash_test.go
View file @
03118146
...
...
@@ -2,7 +2,6 @@ package chunk
import
(
"bytes"
"fmt"
"io"
"testing"
...
...
@@ -11,33 +10,48 @@ import (
func
TestBuzhashChunking
(
t
*
testing
.
T
)
{
data
:=
make
([]
byte
,
1024
*
1024
*
16
)
util
.
NewTimeSeededRand
()
.
Read
(
data
)
r
:=
NewBuzhash
(
bytes
.
NewReader
(
data
))
chunkCount
:=
0
rounds
:=
100
var
chunks
[][]
byte
for
i
:=
0
;
i
<
rounds
;
i
++
{
util
.
NewTimeSeededRand
()
.
Read
(
data
)
for
{
chunk
,
err
:=
r
.
NextBytes
()
if
err
!=
nil
{
if
err
==
io
.
EOF
{
break
r
:=
NewBuzhash
(
bytes
.
NewReader
(
data
))
var
chunks
[][]
byte
for
{
chunk
,
err
:=
r
.
NextBytes
()
if
err
!=
nil
{
if
err
==
io
.
EOF
{
break
}
t
.
Fatal
(
err
)
}
t
.
Fatal
(
err
)
chunks
=
append
(
chunks
,
chunk
)
}
chunkCount
+=
len
(
chunks
)
chunks
=
append
(
chunks
,
chunk
)
}
for
i
,
chunk
:=
range
chunks
{
if
len
(
chunk
)
==
0
{
t
.
Fatalf
(
"chunk %d/%d is empty"
,
i
+
1
,
len
(
chunks
))
}
}
t
.
Logf
(
"average block size: %d
\n
"
,
len
(
data
)
/
len
(
chunks
))
for
i
,
chunk
:=
range
chunks
[
:
len
(
chunks
)
-
1
]
{
if
len
(
chunk
)
<
buzMin
{
t
.
Fatalf
(
"chunk %d/%d is less than the minimum size"
,
i
+
1
,
len
(
chunks
))
}
}
unchunked
:=
bytes
.
Join
(
chunks
,
nil
)
if
!
bytes
.
Equal
(
unchunked
,
data
)
{
fmt
.
Printf
(
"%d %d
\n
"
,
len
(
unchunked
),
len
(
data
))
//ioutil.WriteFile("./incorrect", unchunked, 0777)
//ioutil.WriteFile("./correct", data, 0777)
t
.
Fatal
(
"data was chunked incorrectly"
)
unchunked
:=
bytes
.
Join
(
chunks
,
nil
)
if
!
bytes
.
Equal
(
unchunked
,
data
)
{
t
.
Fatal
(
"data was chunked incorrectly"
)
}
}
t
.
Logf
(
"average block size: %d
\n
"
,
len
(
data
)
*
rounds
/
chunkCount
)
}
func
TestBuzhashChunkReuse
(
t
*
testing
.
T
)
{
...
...
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