2020-11-17 16:29:00 +00:00
|
|
|
package blobstor
|
|
|
|
|
|
|
|
import (
|
2021-05-18 08:12:51 +00:00
|
|
|
"fmt"
|
2022-04-28 10:19:03 +00:00
|
|
|
"os"
|
2021-05-18 08:12:51 +00:00
|
|
|
|
2022-04-28 10:19:03 +00:00
|
|
|
"github.com/klauspost/compress/zstd"
|
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"
|
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) {
|
|
|
|
prm.Address = object.AddressOf(prm.Object)
|
|
|
|
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-06 13:41:35 +00:00
|
|
|
return b.PutRaw(prm, b.NeedsCompression(prm.Object))
|
2022-01-10 12:46:01 +00:00
|
|
|
}
|
|
|
|
|
2022-04-21 11:28:05 +00:00
|
|
|
// NeedsCompression returns true if the object should be compressed.
|
|
|
|
// For an object to be compressed 2 conditions must hold:
|
2022-01-11 11:33:04 +00:00
|
|
|
// 1. Compression is enabled in settings.
|
|
|
|
// 2. Object MIME Content-Type is allowed for compression.
|
2022-03-03 14:19:05 +00:00
|
|
|
func (b *BlobStor) NeedsCompression(obj *objectSDK.Object) bool {
|
2022-07-05 13:47:39 +00:00
|
|
|
return b.cfg.CConfig.NeedsCompression(obj)
|
2021-04-19 10:00:56 +00:00
|
|
|
}
|
|
|
|
|
2022-04-21 11:28:05 +00:00
|
|
|
// PutRaw saves an already marshaled object in BLOB storage.
|
2022-07-06 13:41:35 +00:00
|
|
|
func (b *BlobStor) PutRaw(prm common.PutPrm, compress bool) (common.PutRes, error) {
|
|
|
|
big := b.isBig(prm.RawData)
|
2020-11-17 16:29:00 +00:00
|
|
|
|
2020-12-02 09:58:42 +00:00
|
|
|
if big {
|
2022-04-28 10:19:03 +00:00
|
|
|
var err error
|
|
|
|
if compress {
|
2022-07-06 13:41:35 +00:00
|
|
|
err = b.fsTree.PutStream(prm.Address, func(f *os.File) error {
|
2022-04-28 10:19:03 +00:00
|
|
|
enc, _ := zstd.NewWriter(f) // nil error if no options are provided
|
2022-07-06 13:41:35 +00:00
|
|
|
if _, err := enc.Write(prm.RawData); err != nil {
|
2022-04-28 10:19:03 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
return enc.Close()
|
|
|
|
})
|
|
|
|
} else {
|
2022-07-06 13:41:35 +00:00
|
|
|
_, err = b.fsTree.Put(prm)
|
2022-04-28 10:19:03 +00:00
|
|
|
}
|
2021-09-06 14:28:07 +00:00
|
|
|
if err != nil {
|
2022-07-06 13:41:35 +00:00
|
|
|
return common.PutRes{}, err
|
2021-09-06 14:28:07 +00:00
|
|
|
}
|
|
|
|
|
2022-07-06 13:41:35 +00:00
|
|
|
storagelog.Write(b.log, storagelog.AddressField(prm.Address), storagelog.OpField("fstree PUT"))
|
2021-09-06 14:28:07 +00:00
|
|
|
|
2022-07-06 13:41:35 +00:00
|
|
|
return common.PutRes{}, nil
|
2021-01-11 15:21:06 +00:00
|
|
|
}
|
2020-11-26 07:44:34 +00:00
|
|
|
|
2022-04-28 10:19:03 +00:00
|
|
|
if compress {
|
2022-07-06 13:41:35 +00:00
|
|
|
prm.RawData = b.CConfig.Compress(prm.RawData)
|
2022-04-28 10:19:03 +00:00
|
|
|
}
|
|
|
|
|
2021-01-11 15:21:06 +00:00
|
|
|
// save object in blobovnicza
|
2022-07-06 13:41:35 +00:00
|
|
|
return b.blobovniczas.Put(prm)
|
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
|
|
|
}
|