github.com/wfusion/gofusion@v1.1.14/common/utils/compress/types.go (about)

     1  package compress
     2  
     3  import (
     4  	"errors"
     5  	"io"
     6  
     7  	"github.com/klauspost/compress/flate"
     8  	"github.com/klauspost/compress/gzip"
     9  	"github.com/klauspost/compress/s2"
    10  	"github.com/klauspost/compress/zlib"
    11  	"github.com/klauspost/compress/zstd"
    12  
    13  	"github.com/wfusion/gofusion/common/utils"
    14  )
    15  
    16  const (
    17  	RndSeed int64 = 9009760027768254931
    18  )
    19  
    20  var (
    21  	ErrUnknownAlgorithm = errors.New("unknown compress algorithm")
    22  
    23  	encoderPools = map[Algorithm]utils.Poolable[*writerPoolSealer]{
    24  		AlgorithmZSTD: utils.NewPool(func() *writerPoolSealer {
    25  			return &writerPoolSealer{utils.Must(zstd.NewWriter(nil))}
    26  		}),
    27  		AlgorithmZLib: utils.NewPool(func() *writerPoolSealer {
    28  			return &writerPoolSealer{zlib.NewWriter(nil)}
    29  		}),
    30  		AlgorithmS2: utils.NewPool(func() *writerPoolSealer {
    31  			return &writerPoolSealer{s2.NewWriter(nil)}
    32  		}),
    33  		AlgorithmGZip: utils.NewPool(func() *writerPoolSealer {
    34  			return &writerPoolSealer{gzip.NewWriter(nil)}
    35  		}),
    36  		AlgorithmDeflate: utils.NewPool(func() *writerPoolSealer {
    37  			return &writerPoolSealer{utils.Must(flate.NewWriter(nil, flate.DefaultCompression))}
    38  		}),
    39  	}
    40  
    41  	decoderPools = map[Algorithm]utils.Poolable[*readerPoolSealer]{
    42  		AlgorithmZSTD: utils.NewPool(func() *readerPoolSealer {
    43  			return &readerPoolSealer{utils.Must(zstd.NewReader(nil))}
    44  		}),
    45  		AlgorithmZLib: utils.NewPool(func() *readerPoolSealer {
    46  			return &readerPoolSealer{new(zlibDecodable)} // init when call reset method
    47  		}),
    48  		AlgorithmS2: utils.NewPool(func() *readerPoolSealer {
    49  			return &readerPoolSealer{&s2Decodable{s2.NewReader(nil)}}
    50  		}),
    51  		AlgorithmGZip: utils.NewPool(func() *readerPoolSealer {
    52  			return &readerPoolSealer{new(gzipDecodable)} // init when call reset method
    53  		}),
    54  		AlgorithmDeflate: utils.NewPool(func() *readerPoolSealer {
    55  			return &readerPoolSealer{new(deflateDecodable)} // init when call reset method
    56  		}),
    57  	}
    58  
    59  	eofErrs = []error{io.EOF, io.ErrUnexpectedEOF}
    60  )
    61  
    62  type readerPoolSealer struct{ decodable }
    63  
    64  func (r *readerPoolSealer) Reset(obj any) error {
    65  	return r.decodable.Reset(obj.(io.Reader))
    66  }
    67  
    68  type writerPoolSealer struct{ encodable }
    69  
    70  func (w *writerPoolSealer) Reset(obj any) {
    71  	w.encodable.Reset(obj.(io.Writer))
    72  }