Skip to content

Commit

Permalink
✅ ♻️ Implement zstd test (#676)
Browse files Browse the repository at this point in the history
* implement zstd test

* add comment

* remove unused code and fix e2e test
  • Loading branch information
kevindiu authored Sep 10, 2020
1 parent dc3563d commit b7bc99a
Show file tree
Hide file tree
Showing 3 changed files with 663 additions and 321 deletions.
13 changes: 13 additions & 0 deletions internal/compress/zstd.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ type zstdCompressor struct {
zstd zstd.Zstd
}

// NewZstd returns the zstd compressor object or any initialization error.
func NewZstd(opts ...ZstdOption) (Compressor, error) {
c := &zstdCompressor{
zstd: zstd.New(),
Expand All @@ -45,6 +46,8 @@ func NewZstd(opts ...ZstdOption) (Compressor, error) {
return c, nil
}

// CompressVector compresses the data given and returns the compressed data.
// If CompressVector fails, it will return an error.
func (z *zstdCompressor) CompressVector(vector []float32) ([]byte, error) {
gob, err := z.gobc.CompressVector(vector)
if err != nil {
Expand All @@ -70,6 +73,8 @@ func (z *zstdCompressor) CompressVector(vector []float32) ([]byte, error) {
return buf.Bytes(), nil
}

// DecompressVector decompresses the compressed data and returns the data.
// If decompress fails, it will return an error.
func (z *zstdCompressor) DecompressVector(bs []byte) ([]float32, error) {
buf := new(bytes.Buffer)
zr, err := z.zstd.NewReader(bytes.NewReader(bs))
Expand All @@ -91,6 +96,7 @@ func (z *zstdCompressor) DecompressVector(bs []byte) ([]float32, error) {
return vec, nil
}

// Reader returns io.ReadCloser implementation.
func (z *zstdCompressor) Reader(src io.ReadCloser) (io.ReadCloser, error) {
r, err := z.zstd.NewReader(src)
if err != nil {
Expand All @@ -103,6 +109,7 @@ func (z *zstdCompressor) Reader(src io.ReadCloser) (io.ReadCloser, error) {
}, nil
}

// Writer returns io.WriteCloser implementation.
func (z *zstdCompressor) Writer(dst io.WriteCloser) (io.WriteCloser, error) {
w, err := z.zstd.NewWriter(dst, z.eoptions...)
if err != nil {
Expand All @@ -120,10 +127,13 @@ type zstdReader struct {
r io.Reader
}

// Read returns the number of bytes for read p (0 <= n <= len(p)).
// If any errors occurs, it will return an error.
func (z *zstdReader) Read(p []byte) (n int, err error) {
return z.r.Read(p)
}

// Close closes the reader.
func (z *zstdReader) Close() error {
return z.src.Close()
}
Expand All @@ -133,10 +143,13 @@ type zstdWriter struct {
w io.WriteCloser
}

// Write returns the number of bytes written from p (0 <= n <= len(p)).
// If any errors occurs, it will return an error.
func (z *zstdWriter) Write(p []byte) (n int, err error) {
return z.w.Write(p)
}

// Close closes the writer.
func (z *zstdWriter) Close() (err error) {
err = z.w.Close()
if err != nil {
Expand Down
17 changes: 11 additions & 6 deletions internal/compress/zstd/zstd.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,13 @@ import (
"github.com/klauspost/compress/zstd"
)

// EOption is type alias of zstd.EOption.
type EOption = zstd.EOption
type (
// EOption is type alias of zstd.EOption.
EOption = zstd.EOption

// DOption is type alias of zstd.DOption.
DOption = zstd.DOption
)

// Encoder represents an interface for Encoder of zstd.
type Encoder interface {
Expand All @@ -39,8 +44,8 @@ type Decoder interface {

// Zstd is an interface to create Writer and Reader implementation.
type Zstd interface {
NewWriter(w io.Writer, opts ...zstd.EOption) (Encoder, error)
NewReader(r io.Reader, opts ...zstd.DOption) (Decoder, error)
NewWriter(w io.Writer, opts ...EOption) (Encoder, error)
NewReader(r io.Reader, opts ...DOption) (Decoder, error)
}

type compress struct{}
Expand All @@ -51,11 +56,11 @@ func New() Zstd {
}

// NewWriter returns Encoder implementation.
func (*compress) NewWriter(w io.Writer, opts ...zstd.EOption) (Encoder, error) {
func (*compress) NewWriter(w io.Writer, opts ...EOption) (Encoder, error) {
return zstd.NewWriter(w, opts...)
}

// NewReader returns Decoder implementation.
func (*compress) NewReader(r io.Reader, opts ...zstd.DOption) (Decoder, error) {
func (*compress) NewReader(r io.Reader, opts ...DOption) (Decoder, error) {
return zstd.NewReader(r, opts...)
}
Loading

0 comments on commit b7bc99a

Please sign in to comment.