2020-11-17 16:29:00 +00:00
|
|
|
package blobstor
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"path"
|
|
|
|
|
|
|
|
objectSDK "github.com/nspcc-dev/neofs-api-go/pkg/object"
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
)
|
|
|
|
|
|
|
|
// 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 {
|
|
|
|
return nil, errors.Wrap(err, "could not marshal the object")
|
|
|
|
}
|
|
|
|
|
2020-11-26 14:17:24 +00:00
|
|
|
if b.isBig(data) {
|
|
|
|
// compress object data
|
|
|
|
data = b.compressor(data)
|
2020-11-17 16:29:00 +00:00
|
|
|
|
2020-11-26 07:44:34 +00:00
|
|
|
// save object in shallow dir
|
2020-12-01 13:33:29 +00:00
|
|
|
return new(PutRes), b.fsTree.put(prm.obj.Address(), data)
|
2020-11-26 07:44:34 +00:00
|
|
|
} else {
|
|
|
|
// save object in blobovnicza
|
2020-12-01 15:44:02 +00:00
|
|
|
res, err := b.blobovniczas.put(prm.obj.Address(), data)
|
2020-12-01 13:33:29 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-11-26 07:44:34 +00:00
|
|
|
|
2020-12-01 13:33:29 +00:00
|
|
|
return &PutRes{
|
|
|
|
roBlobovniczaID: roBlobovniczaID{
|
|
|
|
blobovniczaID: res,
|
|
|
|
},
|
|
|
|
}, nil
|
2020-11-26 07:44:34 +00:00
|
|
|
}
|
2020-11-17 16:29:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (t *fsTree) put(addr *objectSDK.Address, data []byte) error {
|
|
|
|
p := t.treePath(addr)
|
|
|
|
|
2020-11-19 13:52:37 +00:00
|
|
|
if err := os.MkdirAll(path.Dir(p), t.Permissions); err != nil {
|
2020-11-17 16:29:00 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-11-19 13:52:37 +00:00
|
|
|
return ioutil.WriteFile(p, data, t.Permissions)
|
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
|
|
|
}
|