Skip to content

Commit 889f059

Browse files
authored
Merge pull request #5 from scunningham/refine_errors
2 parents 4086766 + d97f0e4 commit 889f059

File tree

7 files changed

+54
-23
lines changed

7 files changed

+54
-23
lines changed

internal/pkg/async/reader.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,7 @@ func (r *asyncRdrT) NextBlock(prevBlk *blk.BlkT) (*blk.BlkT, int, error) {
237237
case err != zerr.EndMark:
238238
case r.hasher == nil:
239239
case r.hasher.Done() != r.frameRdr.ContentChecksum():
240-
err = zerr.ErrContentHash
240+
err = zerr.WrapCorrupted(zerr.ErrContentHash)
241241
}
242242

243243
return nextBlk, nRead, err

internal/pkg/sync/reader.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ func (r *syncReaderT) NextBlock(prevBlk *BlkT) (*BlkT, int, error) {
6464
case zerr.EndMark:
6565
if r.hasher != nil {
6666
if r.hasher.Sum32() != r.frameRdr.ContentChecksum() {
67-
err = zerr.ErrContentHash
67+
err = zerr.WrapCorrupted(zerr.ErrContentHash)
6868
}
6969
}
7070
fallthrough

internal/test/rd_test.go

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -720,11 +720,12 @@ func TestContentCRC(t *testing.T) {
720720
}
721721

722722
tests := map[string]struct {
723-
data []byte
724-
sha2 string
725-
munge func([]byte) []byte
726-
err error
727-
opts []plz4.OptT
723+
data []byte
724+
sha2 string
725+
munge func([]byte) []byte
726+
err error
727+
corrupt bool
728+
opts []plz4.OptT
728729
}{
729730
"no_content_hash": {
730731
data: oneFrameNoHash,
@@ -739,24 +740,27 @@ func TestContentCRC(t *testing.T) {
739740
sha2: oneFrameSha2,
740741
},
741742
"munge_one_frame": {
742-
data: oneFrame,
743-
munge: corruptCRC,
744-
err: plz4.ErrContentHash,
743+
data: oneFrame,
744+
munge: corruptCRC,
745+
err: plz4.ErrContentHash,
746+
corrupt: true,
745747
},
746748
"clean_large": {
747749
data: large,
748750
sha2: lsha2,
749751
},
750752
"munged_large": {
751-
data: large,
752-
munge: corruptCRC,
753-
err: plz4.ErrContentHash,
753+
data: large,
754+
munge: corruptCRC,
755+
err: plz4.ErrContentHash,
756+
corrupt: true,
754757
},
755758
"munged_large_sync": {
756-
data: large,
757-
munge: corruptCRC,
758-
err: plz4.ErrContentHash,
759-
opts: []Option{plz4.WithParallel(0)},
759+
data: large,
760+
munge: corruptCRC,
761+
err: plz4.ErrContentHash,
762+
opts: []Option{plz4.WithParallel(0)},
763+
corrupt: true,
760764
},
761765
"large_clipped": {
762766
data: large[:len(large)-4],
@@ -781,6 +785,11 @@ func TestContentCRC(t *testing.T) {
781785
}
782786

783787
if tc.err != nil || err != nil {
788+
789+
if tc.corrupt != plz4.Lz4Corrupted(err) {
790+
t.Errorf("Corrupt mismatch; wanted: %v on '%v'", tc.corrupt, err)
791+
}
792+
784793
if !errors.Is(err, tc.err) {
785794
t.Fatalf("Expected error: %v got %v", tc.err, err)
786795
}

plz4_err.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@ const (
3535
)
3636

3737
// Returns true if 'err' indicates that the read input is corrupted.
38+
//
39+
// Note that a short read is not considered corrupted. In that case
40+
// the returned error will be a join of the error context, and the
41+
// underlying error, either an io.EOF or io.ErrUnexpectedEOF.
3842
func Lz4Corrupted(err error) bool {
3943
return errors.Is(err, ErrCorrupted)
4044
}

plz4_opts.go

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,25 @@ import (
88
"github.com/prequel-dev/plz4/internal/pkg/opts"
99
)
1010

11+
// OptT is a function that sets an option on the processor.
1112
type OptT func(*opts.OptsT)
13+
14+
// WorkerPool is an interface for a worker pool implementation.
1215
type WorkerPool = opts.WorkerPool
16+
17+
// BlockIdxT is a type for block size index.
1318
type BlockIdxT = descriptor.BlockIdxT
19+
20+
// LevelT is a type for compression level.
1421
type LevelT = compress.LevelT
1522

23+
// Progress callback function type.
1624
type CbProgressT = opts.ProgressFuncT
25+
26+
// Skip callback function type.
1727
type CbSkipT = opts.SkipCallbackT
28+
29+
// Dictionary callback function type.
1830
type CbDictT = opts.DictCallbackT
1931

2032
const (
@@ -73,7 +85,7 @@ func WithParallel(n int) OptT {
7385
// This option only applies to the asynchronous case.
7486
// It is ignored in the synchronous case.
7587
//
76-
// Setting the size to -1 forces auto mode, where the processor will automatically
88+
// Setting the pending size to -1 enables auto mode. In auto mode, the processor will automatically
7789
// scale the pending size for maximum speed based on the block size and nParallel.
7890
func WithPendingSize(n int) OptT {
7991
return func(o *opts.OptsT) {
@@ -91,15 +103,15 @@ func WithContentChecksum(enable bool) OptT {
91103
}
92104
}
93105

94-
// Optional worker pool for both compress and decompress mode.
106+
// Optional worker pool for both compress and decompress modes.
95107
func WithWorkerPool(wp WorkerPool) OptT {
96108
return func(o *opts.OptsT) {
97109
o.WorkerPool = wp
98110
}
99111
}
100112

101113
// Processor will emit tuple (src_block_offset, dst_blk_offset) on each
102-
// block boundary. Applies to both compress and decompress mode.
114+
// block boundary. Applies to both compress and decompress modes.
103115
//
104116
// Offsets are relative to the start of the frame.
105117
//
@@ -181,7 +193,7 @@ func WithDictionaryId(id uint32) OptT {
181193

182194
// Read block starting at byte 'offset'.
183195
//
184-
// The offset is the first byte of the block relative to the start of the frame.
196+
// The offset is the first byte of the data block relative to the start of the frame.
185197
func WithReadOffset(offset int64) OptT {
186198
return func(o *opts.OptsT) {
187199
o.ReadOffset = offset

plz4_reader.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ import (
66
"github.com/prequel-dev/plz4/internal/pkg/rdr"
77
)
88

9+
// Reader is an interface for reading LZ4 compressed data.
10+
//
11+
// It implements the io.ReadCloser and the io.WriterTo interfaces.
912
type Reader interface {
1013
// Read decompressed data into 'dst'. Return number bytes read.
1114
Read(dst []byte) (n int, err error)
@@ -19,7 +22,7 @@ type Reader interface {
1922
Close() error
2023
}
2124

22-
// Construct a Reader to decompress the LZ4 frame from 'rdr'.
25+
// Construct a Reader to decompress the LZ4 frame from 'rd'.
2326
//
2427
// Specify optional parameters in 'opts'.
2528
func NewReader(rd io.Reader, opts ...OptT) Reader {

plz4_writer.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ import (
88
"github.com/prequel-dev/plz4/internal/pkg/sync"
99
)
1010

11+
// Writer is an interface for compressing data into an LZ4 frame.
12+
//
13+
// It implements the io.WriteCloser and the io.ReaderFrom interfaces.
1114
type Writer interface {
1215
// Compress 'src' data; return number of bytes written.
1316
// May be used in sequence with ReadFrom.
@@ -31,7 +34,7 @@ type Writer interface {
3134
Close() error
3235
}
3336

34-
// Construct a Writer to compress an LZ4 frame into 'wr'.
37+
// Construct a Writer to compress data into an LZ4 frame written to 'wr'.
3538
//
3639
// Specify optional parameters in 'opts'.
3740
func NewWriter(wr io.Writer, opts ...OptT) Writer {

0 commit comments

Comments
 (0)