frostfs-node/pkg/local_object_storage/blobstor/compress.go
Evgenii Stratonikov dd678cd976 [#868] blobstor: allow to decompress objects on-the-fly
We should be able to read whatever we have written earlier.
Compression setting applies only to the new objects.

Signed-off-by: Evgenii Stratonikov <evgeniy@nspcc.ru>
2021-10-08 16:01:45 +03:00

39 lines
847 B
Go

package blobstor
import (
"github.com/klauspost/compress/zstd"
)
// zstdFrameMagic contains first 4 bytes of any compressed object
// https://github.com/klauspost/compress/blob/master/zstd/framedec.go#L58 .
var zstdFrameMagic = []byte{0x28, 0xb5, 0x2f, 0xfd}
func noOpCompressor(data []byte) []byte {
return data
}
func noOpDecompressor(data []byte) ([]byte, error) {
return data, nil
}
func zstdCompressor() (func([]byte) []byte, error) {
enc, err := zstd.NewWriter(nil)
if err != nil {
return nil, err
}
return func(data []byte) []byte {
return enc.EncodeAll(data, make([]byte, 0, len(data)))
}, nil
}
func zstdDecompressor() (func([]byte) ([]byte, error), error) {
dec, err := zstd.NewReader(nil)
if err != nil {
return nil, err
}
return func(data []byte) ([]byte, error) {
return dec.DecodeAll(data, nil)
}, nil
}