[#1715] compression: Decouple Config and Compressor

Refactoring.

Change-Id: Ide2e1378f30c39045d4bacd13a902331bd4f764f
Signed-off-by: Dmitrii Stepanov <d.stepanov@yadro.com>
This commit is contained in:
Dmitrii Stepanov 2025-04-14 16:18:08 +03:00
parent 98308d0cad
commit 8c746a914a
12 changed files with 49 additions and 41 deletions

View file

@ -19,6 +19,13 @@ const (
LevelSmallestSize Level = "smallest_size"
)
type Compressor struct {
Config
encoder *zstd.Encoder
decoder *zstd.Decoder
}
// Config represents common compression-related configuration.
type Config struct {
Enabled bool
@ -27,9 +34,6 @@ type Config struct {
UseCompressEstimation bool
CompressEstimationThreshold float64
encoder *zstd.Encoder
decoder *zstd.Decoder
}
// zstdFrameMagic contains first 4 bytes of any compressed object
@ -37,7 +41,7 @@ type Config struct {
var zstdFrameMagic = []byte{0x28, 0xb5, 0x2f, 0xfd}
// Init initializes compression routines.
func (c *Config) Init() error {
func (c *Compressor) Init() error {
var err error
if c.Enabled {
@ -84,7 +88,7 @@ func (c *Config) NeedsCompression(obj *objectSDK.Object) bool {
// Decompress decompresses data if it starts with the magic
// and returns data untouched otherwise.
func (c *Config) Decompress(data []byte) ([]byte, error) {
func (c *Compressor) Decompress(data []byte) ([]byte, error) {
if len(data) < 4 || !bytes.Equal(data[:4], zstdFrameMagic) {
return data, nil
}
@ -93,7 +97,7 @@ func (c *Config) Decompress(data []byte) ([]byte, error) {
// Compress compresses data if compression is enabled
// and returns data untouched otherwise.
func (c *Config) Compress(data []byte) []byte {
func (c *Compressor) Compress(data []byte) []byte {
if c == nil || !c.Enabled {
return data
}
@ -107,7 +111,7 @@ func (c *Config) Compress(data []byte) []byte {
return c.compress(data)
}
func (c *Config) compress(data []byte) []byte {
func (c *Compressor) compress(data []byte) []byte {
maxSize := c.encoder.MaxEncodedSize(len(data))
compressed := c.encoder.EncodeAll(data, make([]byte, 0, maxSize))
if len(data) < len(compressed) {
@ -117,7 +121,7 @@ func (c *Config) compress(data []byte) []byte {
}
// Close closes encoder and decoder, returns any error occurred.
func (c *Config) Close() error {
func (c *Compressor) Close() error {
var err error
if c.encoder != nil {
err = c.encoder.Close()
@ -135,7 +139,7 @@ func (c *Config) HasValidCompressionLevel() bool {
c.Level == LevelSmallestSize
}
func (c *Config) compressionLevel() zstd.EncoderLevel {
func (c *Compressor) compressionLevel() zstd.EncoderLevel {
switch c.Level {
case LevelDefault, LevelOptimal:
return zstd.SpeedDefault