[#1060] blobstor: allow to disable compression based on content-type

For some data compression makes little sense, as it is already compressed.
This commit allows to leave such data unchanged based on `Content-Type`
attribute. Currently exact, prefix and suffix matching are supported.

Signed-off-by: Evgenii Stratonikov <evgeniy@nspcc.ru>
This commit is contained in:
Evgenii Stratonikov 2022-01-10 15:46:01 +03:00 committed by Alex Vanin
parent 0f1eb743af
commit 0d969d7a06
11 changed files with 133 additions and 8 deletions

View file

@ -2,7 +2,9 @@ package blobstor
import (
"fmt"
"strings"
"github.com/nspcc-dev/neofs-node/pkg/core/object"
storagelog "github.com/nspcc-dev/neofs-node/pkg/local_object_storage/internal/log"
objectSDK "github.com/nspcc-dev/neofs-sdk-go/object"
)
@ -32,15 +34,43 @@ func (b *BlobStor) Put(prm *PutPrm) (*PutRes, error) {
return nil, fmt.Errorf("could not marshal the object: %w", err)
}
return b.PutRaw(prm.obj.Address(), data)
return b.PutRaw(prm.obj.Address(), data, b.needsCompression(prm.obj))
}
func (b *BlobStor) needsCompression(obj *object.Object) bool {
if !b.compressionEnabled || len(b.uncompressableContentTypes) == 0 {
return b.compressionEnabled
}
for _, attr := range obj.Attributes() {
if attr.Key() == objectSDK.AttributeContentType {
for _, value := range b.uncompressableContentTypes {
match := false
switch {
case len(value) > 0 && value[len(value)-1] == '*':
match = strings.HasPrefix(attr.Value(), value[:len(value)-1])
case len(value) > 0 && value[0] == '*':
match = strings.HasSuffix(attr.Value(), value[1:])
default:
match = attr.Value() == value
}
if match {
return false
}
}
}
}
return b.compressionEnabled
}
// PutRaw saves already marshaled object in BLOB storage.
func (b *BlobStor) PutRaw(addr *objectSDK.Address, data []byte) (*PutRes, error) {
func (b *BlobStor) PutRaw(addr *objectSDK.Address, data []byte, compress bool) (*PutRes, error) {
big := b.isBig(data)
// compress object data
data = b.compressor(data)
if compress {
data = b.compressor(data)
}
if big {
// save object in shallow dir