Skip to content

Commit

Permalink
archive: use valyala/gozstd
Browse files Browse the repository at this point in the history
Signed-off-by: Giuseppe Scrivano <[email protected]>
  • Loading branch information
giuseppe committed Jan 10, 2022
1 parent 7ac0e7b commit a3abf19
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 8 deletions.
19 changes: 11 additions & 8 deletions pkg/archive/archive_zstd.go → pkg/archive/archive_zstd_cgo.go
Original file line number Diff line number Diff line change
@@ -1,41 +1,44 @@
// +build linux,cgo

package archive

import (
"io"

"github.com/klauspost/compress/zstd"
zstd "github.com/valyala/gozstd"
)

type wrapperZstdDecoder struct {
decoder *zstd.Decoder
decoder *zstd.Reader
}

func (w *wrapperZstdDecoder) Close() error {
w.decoder.Close()
w.decoder.Release()
return nil
}

func (w *wrapperZstdDecoder) DecodeAll(input, dst []byte) ([]byte, error) {
return w.decoder.DecodeAll(input, dst)
return zstd.Decompress(dst, input)
}

func (w *wrapperZstdDecoder) Read(p []byte) (int, error) {
return w.decoder.Read(p)
}

func (w *wrapperZstdDecoder) Reset(r io.Reader) error {
return w.decoder.Reset(r)
w.decoder.Reset(r, nil)
return nil
}

func (w *wrapperZstdDecoder) WriteTo(wr io.Writer) (int64, error) {
return w.decoder.WriteTo(wr)
}

func zstdReader(buf io.Reader) (io.ReadCloser, error) {
decoder, err := zstd.NewReader(buf)
return &wrapperZstdDecoder{decoder: decoder}, err
decoder := zstd.NewReader(buf)
return &wrapperZstdDecoder{decoder: decoder}, nil
}

func zstdWriter(dest io.Writer) (io.WriteCloser, error) {
return zstd.NewWriter(dest)
return zstd.NewWriter(dest), nil
}
39 changes: 39 additions & 0 deletions pkg/archive/archive_zstd_nocgo.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// +build !linux !cgo

package archive

import (
"fmt"
"io"
)

type wrapperZstdDecoder struct {
}

func (w *wrapperZstdDecoder) Close() error {
return fmt.Errorf("zstd not supported without cgo")
}

func (w *wrapperZstdDecoder) DecodeAll(input, dst []byte) ([]byte, error) {
return nil, fmt.Errorf("zstd not supported without cgo")
}

func (w *wrapperZstdDecoder) Read(p []byte) (int, error) {
return -1, fmt.Errorf("zstd not supported without cgo")
}

func (w *wrapperZstdDecoder) Reset(r io.Reader) error {
return fmt.Errorf("zstd not supported without cgo")
}

func (w *wrapperZstdDecoder) WriteTo(wr io.Writer) (int64, error) {
return -1, fmt.Errorf("zstd not supported without cgo")
}

func zstdReader(buf io.Reader) (io.ReadCloser, error) {
return nil, fmt.Errorf("zstd not supported without cgo")
}

func zstdWriter(dest io.Writer) (io.WriteCloser, error) {
return nil, fmt.Errorf("zstd not supported without cgo")
}

0 comments on commit a3abf19

Please sign in to comment.