2020-11-17 16:29:00 +00:00
|
|
|
package blobstor
|
|
|
|
|
|
|
|
import (
|
2021-05-18 08:12:51 +00:00
|
|
|
"fmt"
|
2022-01-10 12:46:01 +00:00
|
|
|
"strings"
|
2021-05-18 08:12:51 +00:00
|
|
|
|
2022-01-10 12:46:01 +00:00
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/core/object"
|
2021-09-06 14:28:07 +00:00
|
|
|
storagelog "github.com/nspcc-dev/neofs-node/pkg/local_object_storage/internal/log"
|
2021-11-10 07:08:33 +00:00
|
|
|
objectSDK "github.com/nspcc-dev/neofs-sdk-go/object"
|
2020-11-17 16:29:00 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// PutPrm groups the parameters of Put operation.
|
|
|
|
type PutPrm struct {
|
2020-11-24 14:24:07 +00:00
|
|
|
rwObject
|
2020-11-17 16:29:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// PutRes groups resulting values of Put operation.
|
2020-11-24 14:24:07 +00:00
|
|
|
type PutRes struct {
|
|
|
|
roBlobovniczaID
|
2020-11-17 16:29:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Put saves the object in BLOB storage.
|
|
|
|
//
|
2020-11-24 14:24:07 +00:00
|
|
|
// If object is "big", BlobStor saves the object in shallow dir.
|
|
|
|
// Otherwise, BlobStor saves the object in blobonicza. In this
|
|
|
|
// case the identifier of blobovnicza is returned.
|
|
|
|
//
|
2020-11-17 16:29:00 +00:00
|
|
|
// Returns any error encountered that
|
|
|
|
// did not allow to completely save the object.
|
|
|
|
func (b *BlobStor) Put(prm *PutPrm) (*PutRes, error) {
|
|
|
|
// marshal object
|
|
|
|
data, err := prm.obj.Marshal()
|
|
|
|
if err != nil {
|
2021-05-18 08:12:51 +00:00
|
|
|
return nil, fmt.Errorf("could not marshal the object: %w", err)
|
2020-11-17 16:29:00 +00:00
|
|
|
}
|
|
|
|
|
2022-01-11 11:33:04 +00:00
|
|
|
return b.PutRaw(prm.obj.Address(), data, b.NeedsCompression(prm.obj))
|
2022-01-10 12:46:01 +00:00
|
|
|
}
|
|
|
|
|
2022-01-11 11:33:04 +00:00
|
|
|
// NeedsCompression returns true if object should be compressed.
|
|
|
|
// For object to be compressed 2 conditions must hold:
|
|
|
|
// 1. Compression is enabled in settings.
|
|
|
|
// 2. Object MIME Content-Type is allowed for compression.
|
|
|
|
func (b *BlobStor) NeedsCompression(obj *object.Object) bool {
|
2022-01-10 12:46:01 +00:00
|
|
|
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
|
2021-04-19 10:00:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// PutRaw saves already marshaled object in BLOB storage.
|
2022-01-10 12:46:01 +00:00
|
|
|
func (b *BlobStor) PutRaw(addr *objectSDK.Address, data []byte, compress bool) (*PutRes, error) {
|
2020-12-02 09:58:42 +00:00
|
|
|
big := b.isBig(data)
|
2020-11-17 16:29:00 +00:00
|
|
|
|
2022-01-10 12:46:01 +00:00
|
|
|
if compress {
|
|
|
|
data = b.compressor(data)
|
|
|
|
}
|
2020-12-02 09:58:42 +00:00
|
|
|
|
|
|
|
if big {
|
2020-11-26 07:44:34 +00:00
|
|
|
// save object in shallow dir
|
2021-09-06 14:28:07 +00:00
|
|
|
err := b.fsTree.Put(addr, data)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
storagelog.Write(b.log, storagelog.AddressField(addr), storagelog.OpField("fstree PUT"))
|
|
|
|
|
|
|
|
return new(PutRes), nil
|
2021-01-11 15:21:06 +00:00
|
|
|
}
|
2020-11-26 07:44:34 +00:00
|
|
|
|
2021-01-11 15:21:06 +00:00
|
|
|
// save object in blobovnicza
|
2021-04-19 10:00:56 +00:00
|
|
|
res, err := b.blobovniczas.put(addr, data)
|
2021-01-11 15:21:06 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2020-11-26 07:44:34 +00:00
|
|
|
}
|
2021-01-11 15:21:06 +00:00
|
|
|
|
|
|
|
return &PutRes{
|
|
|
|
roBlobovniczaID: roBlobovniczaID{
|
|
|
|
blobovniczaID: res,
|
|
|
|
},
|
|
|
|
}, nil
|
2020-11-17 16:29:00 +00:00
|
|
|
}
|
|
|
|
|
2020-11-26 07:44:34 +00:00
|
|
|
// checks if object is "big".
|
2020-11-26 14:17:24 +00:00
|
|
|
func (b *BlobStor) isBig(data []byte) bool {
|
|
|
|
return uint64(len(data)) > b.smallSizeLimit
|
2020-11-26 07:44:34 +00:00
|
|
|
}
|