Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

zstd: Allow partial reads #169

Merged
merged 1 commit into from
Oct 10, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 20 additions & 5 deletions zstd/decoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,9 @@ func (d *Decoder) Read(p []byte) (int, error) {
if d.current.err != nil {
break
}
d.nextBlock()
if !d.nextBlock(n == 0) {
return n, nil
}
}
}
if len(d.current.b) > 0 {
Expand Down Expand Up @@ -252,7 +254,7 @@ func (d *Decoder) WriteTo(w io.Writer) (int64, error) {
if d.current.err != nil {
break
}
d.nextBlock()
d.nextBlock(true)
}
err := d.current.err
if err != nil {
Expand Down Expand Up @@ -329,7 +331,10 @@ func (d *Decoder) DecodeAll(input, dst []byte) ([]byte, error) {

// nextBlock returns the next block.
// If an error occurs d.err will be set.
func (d *Decoder) nextBlock() {
// Optionally the function can block for new output.
// If non-blocking mode is used the returned boolean will be false
// if no data was available without blocking.
func (d *Decoder) nextBlock(blocking bool) (ok bool) {
if d.current.d != nil {
if debug {
printf("re-adding current decoder %p", d.current.d)
Expand All @@ -339,12 +344,22 @@ func (d *Decoder) nextBlock() {
}
if d.current.err != nil {
// Keep error state.
return
return blocking
}

if blocking {
d.current.decodeOutput = <-d.current.output
} else {
select {
case d.current.decodeOutput = <-d.current.output:
default:
return false
}
}
d.current.decodeOutput = <-d.current.output
if debug {
println("got", len(d.current.b), "bytes, error:", d.current.err)
}
return true
}

// Close will release all resources.
Expand Down
78 changes: 78 additions & 0 deletions zstd/decoder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"bytes"
"encoding/binary"
"encoding/hex"
"fmt"
"io"
"io/ioutil"
"log"
Expand Down Expand Up @@ -263,6 +264,83 @@ func TestNewDecoderSmallFile(t *testing.T) {
t.Logf("Decoded %d bytes with %f.2 MB/s", n, mbpersec)
}

type readAndBlock struct {
buf []byte
unblock chan struct{}
}

func (r *readAndBlock) Read(p []byte) (int, error) {
n := copy(p, r.buf)
if n == 0 {
<-r.unblock
return 0, io.EOF
}
r.buf = r.buf[n:]
return n, nil
}

func TestNewDecoderFlushed(t *testing.T) {
if testing.Short() {
t.SkipNow()
}
file := "testdata/z000028.zst"
payload, err := ioutil.ReadFile(file)
if err != nil {
t.Fatal(err)
}
payload = append(payload, payload...) //2x
payload = append(payload, payload...) //4x
payload = append(payload, payload...) //8x
rng := rand.New(rand.NewSource(0x1337))
runs := 100
if testing.Short() {
runs = 5
}
enc, err := NewWriter(nil, WithWindowSize(128<<10))
if err != nil {
t.Fatal(err)
}
defer enc.Close()
for i := 0; i < runs; i++ {
wantSize := rng.Intn(len(payload)-1) + 1
t.Run(fmt.Sprint("size-", wantSize), func(t *testing.T) {
var encoded bytes.Buffer
enc.Reset(&encoded)
_, err := enc.Write(payload[:wantSize])
if err != nil {
t.Fatal(err)
}
err = enc.Flush()
if err != nil {
t.Fatal(err)
}

// We must be able to read back up until the flush...
r := readAndBlock{
buf: encoded.Bytes(),
unblock: make(chan struct{}),
}
defer timeout(5 * time.Second)()
dec, err := NewReader(&r)
if err != nil {
t.Fatal(err)
}
defer dec.Close()
defer close(r.unblock)
readBack := 0
dst := make([]byte, 1024)
for readBack < wantSize {
// Read until we have enough.
n, err := dec.Read(dst)
if err != nil {
t.Fatal(err)
}
readBack += n
}
})
}
}

func TestDecoderRegression(t *testing.T) {
defer timeout(60 * time.Second)()
data, err := ioutil.ReadFile("testdata/regression.zip")
Expand Down