2020-11-17 12:23:15 +00:00
|
|
|
package shard
|
|
|
|
|
|
|
|
import (
|
2021-05-18 08:12:51 +00:00
|
|
|
"fmt"
|
|
|
|
|
2020-11-17 17:39:43 +00:00
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/local_object_storage/blobstor"
|
2020-12-08 09:56:14 +00:00
|
|
|
meta "github.com/nspcc-dev/neofs-node/pkg/local_object_storage/metabase"
|
2022-03-03 14:19:05 +00:00
|
|
|
"github.com/nspcc-dev/neofs-sdk-go/object"
|
2021-04-06 10:56:06 +00:00
|
|
|
"go.uber.org/zap"
|
2020-11-17 12:23:15 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// PutPrm groups the parameters of Put operation.
|
|
|
|
type PutPrm struct {
|
|
|
|
obj *object.Object
|
|
|
|
}
|
|
|
|
|
2022-04-21 11:28:05 +00:00
|
|
|
// PutRes groups the resulting values of Put operation.
|
2020-11-17 12:23:15 +00:00
|
|
|
type PutRes struct{}
|
|
|
|
|
|
|
|
// WithObject is a Put option to set object to save.
|
2022-05-20 18:08:59 +00:00
|
|
|
func (p *PutPrm) WithObject(obj *object.Object) {
|
2020-11-17 12:23:15 +00:00
|
|
|
if p != nil {
|
|
|
|
p.obj = obj
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Put saves the object in shard.
|
|
|
|
//
|
|
|
|
// Returns any error encountered that
|
|
|
|
// did not allow to completely save the object.
|
2021-12-27 11:04:07 +00:00
|
|
|
//
|
|
|
|
// Returns ErrReadOnlyMode error if shard is in "read-only" mode.
|
2022-05-20 18:08:59 +00:00
|
|
|
func (s *Shard) Put(prm PutPrm) (*PutRes, error) {
|
2022-03-17 11:55:25 +00:00
|
|
|
if s.GetMode() != ModeReadWrite {
|
2021-12-27 11:04:07 +00:00
|
|
|
return nil, ErrReadOnlyMode
|
|
|
|
}
|
|
|
|
|
2020-12-01 07:46:52 +00:00
|
|
|
putPrm := new(blobstor.PutPrm) // form Put parameters
|
|
|
|
putPrm.SetObject(prm.obj)
|
2020-11-17 17:39:43 +00:00
|
|
|
|
2020-12-01 07:46:52 +00:00
|
|
|
// exist check are not performed there, these checks should be executed
|
|
|
|
// ahead of `Put` by storage engine
|
2021-04-06 10:56:06 +00:00
|
|
|
if s.hasWriteCache() {
|
|
|
|
err := s.writeCache.Put(prm.obj)
|
|
|
|
if err == nil {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
s.log.Debug("can't put message to writeCache, trying to blobStor",
|
|
|
|
zap.String("err", err.Error()))
|
|
|
|
}
|
2020-11-17 17:39:43 +00:00
|
|
|
|
2020-12-01 07:46:52 +00:00
|
|
|
var (
|
|
|
|
err error
|
|
|
|
res *blobstor.PutRes
|
|
|
|
)
|
|
|
|
|
|
|
|
// res == nil if there is no writeCache or writeCache.Put has been failed
|
2021-04-06 10:56:06 +00:00
|
|
|
if res, err = s.blobStor.Put(putPrm); err != nil {
|
2021-05-18 08:12:51 +00:00
|
|
|
return nil, fmt.Errorf("could not put object to BLOB storage: %w", err)
|
2020-11-17 17:39:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// put to metabase
|
2020-12-08 09:56:14 +00:00
|
|
|
if err := meta.Put(s.metaBase, prm.obj, res.BlobovniczaID()); err != nil {
|
2020-11-17 17:39:43 +00:00
|
|
|
// may we need to handle this case in a special way
|
|
|
|
// since the object has been successfully written to BlobStor
|
2021-05-18 08:12:51 +00:00
|
|
|
return nil, fmt.Errorf("could not put object to metabase: %w", err)
|
2020-11-17 17:39:43 +00:00
|
|
|
}
|
2020-11-17 12:23:15 +00:00
|
|
|
|
|
|
|
return nil, nil
|
|
|
|
}
|