2020-11-17 16:29:00 +00:00
|
|
|
package blobstor
|
|
|
|
|
|
|
|
import (
|
2022-07-08 11:33:49 +00:00
|
|
|
"errors"
|
2021-05-18 08:12:51 +00:00
|
|
|
"fmt"
|
|
|
|
|
2022-01-10 12:46:01 +00:00
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/core/object"
|
2022-07-06 13:41:35 +00:00
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/local_object_storage/blobstor/common"
|
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"
|
2022-07-08 11:33:49 +00:00
|
|
|
"go.uber.org/zap"
|
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.
|
2022-07-06 13:41:35 +00:00
|
|
|
func (b *BlobStor) Put(prm common.PutPrm) (common.PutRes, error) {
|
2022-07-08 07:09:48 +00:00
|
|
|
if prm.Object != nil {
|
|
|
|
prm.Address = object.AddressOf(prm.Object)
|
|
|
|
}
|
2022-07-06 13:41:35 +00:00
|
|
|
if prm.RawData == nil {
|
|
|
|
// marshal object
|
|
|
|
data, err := prm.Object.Marshal()
|
|
|
|
if err != nil {
|
|
|
|
return common.PutRes{}, fmt.Errorf("could not marshal the object: %w", err)
|
|
|
|
}
|
|
|
|
prm.RawData = data
|
2020-11-17 16:29:00 +00:00
|
|
|
}
|
|
|
|
|
2022-07-08 11:33:49 +00:00
|
|
|
for i := range b.storage {
|
2022-07-11 12:34:17 +00:00
|
|
|
if b.storage[i].Policy == nil || b.storage[i].Policy(prm.Object, prm.RawData) {
|
2022-07-08 11:33:49 +00:00
|
|
|
res, err := b.storage[i].Storage.Put(prm)
|
|
|
|
if err == nil {
|
|
|
|
storagelog.Write(b.log,
|
|
|
|
storagelog.AddressField(prm.Address),
|
|
|
|
storagelog.OpField("PUT"),
|
|
|
|
zap.String("type", b.storage[i].Storage.Type()),
|
|
|
|
zap.String("storage ID", string(res.StorageID)))
|
|
|
|
}
|
|
|
|
return res, err
|
2021-09-06 14:28:07 +00:00
|
|
|
}
|
2021-01-11 15:21:06 +00:00
|
|
|
}
|
2020-11-26 07:44:34 +00:00
|
|
|
|
2022-07-08 11:33:49 +00:00
|
|
|
return common.PutRes{}, errors.New("couldn't find a place to store an object")
|
2020-11-17 16:29:00 +00:00
|
|
|
}
|
|
|
|
|
2022-07-08 07:09:48 +00:00
|
|
|
// NeedsCompression returns true if the object should be compressed.
|
|
|
|
// For an 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 *objectSDK.Object) bool {
|
2022-08-19 14:29:53 +00:00
|
|
|
return b.cfg.compression.NeedsCompression(obj)
|
2022-07-08 07:09:48 +00:00
|
|
|
}
|