2020-11-26 14:26:53 +00:00
|
|
|
package blobovnicza
|
|
|
|
|
|
|
|
import (
|
2021-05-18 08:12:51 +00:00
|
|
|
"fmt"
|
|
|
|
|
2023-03-07 13:38:26 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/local_object_storage/util/logicerr"
|
|
|
|
oid "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/object/id"
|
2020-11-26 14:26:53 +00:00
|
|
|
"go.etcd.io/bbolt"
|
|
|
|
)
|
|
|
|
|
|
|
|
// PutPrm groups the parameters of Put operation.
|
|
|
|
type PutPrm struct {
|
2022-05-31 17:00:41 +00:00
|
|
|
addr oid.Address
|
2020-11-26 14:26:53 +00:00
|
|
|
|
|
|
|
objData []byte
|
|
|
|
}
|
|
|
|
|
2022-04-21 11:28:05 +00:00
|
|
|
// PutRes groups the resulting values of Put operation.
|
2020-11-26 14:26:53 +00:00
|
|
|
type PutRes struct {
|
|
|
|
}
|
|
|
|
|
2022-04-21 11:28:05 +00:00
|
|
|
// ErrFull is returned when trying to save an
|
2020-11-26 14:26:53 +00:00
|
|
|
// object to a filled blobovnicza.
|
2022-11-09 10:59:24 +00:00
|
|
|
var ErrFull = logicerr.New("blobovnicza is full")
|
2020-11-26 14:26:53 +00:00
|
|
|
|
2022-04-21 11:28:05 +00:00
|
|
|
// SetAddress sets the address of the saving object.
|
2022-05-31 17:00:41 +00:00
|
|
|
func (p *PutPrm) SetAddress(addr oid.Address) {
|
2020-11-26 14:26:53 +00:00
|
|
|
p.addr = addr
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetMarshaledObject sets binary representation of the object.
|
|
|
|
func (p *PutPrm) SetMarshaledObject(data []byte) {
|
|
|
|
p.objData = data
|
|
|
|
}
|
|
|
|
|
2022-04-21 11:28:05 +00:00
|
|
|
// Put saves an object in Blobovnicza.
|
2020-11-26 14:26:53 +00:00
|
|
|
//
|
|
|
|
// If binary representation of the object is not set,
|
|
|
|
// it is calculated via Marshal method.
|
|
|
|
//
|
|
|
|
// The size of the object MUST BE less that or equal to
|
|
|
|
// the size specified in WithObjectSizeLimit option.
|
|
|
|
//
|
|
|
|
// Returns any error encountered that
|
|
|
|
// did not allow to completely save the object.
|
|
|
|
//
|
|
|
|
// Returns ErrFull if blobovnicza is filled.
|
2021-09-14 14:44:07 +00:00
|
|
|
//
|
|
|
|
// Should not be called in read-only configuration.
|
2022-05-31 12:22:32 +00:00
|
|
|
func (b *Blobovnicza) Put(prm PutPrm) (PutRes, error) {
|
2022-03-21 13:16:08 +00:00
|
|
|
sz := uint64(len(prm.objData))
|
|
|
|
bucketName := bucketForSize(sz)
|
2022-05-31 17:00:41 +00:00
|
|
|
key := addressKey(prm.addr)
|
2022-03-21 13:16:08 +00:00
|
|
|
|
2021-04-09 09:18:38 +00:00
|
|
|
err := b.boltDB.Batch(func(tx *bbolt.Tx) error {
|
2020-11-26 14:26:53 +00:00
|
|
|
if b.full() {
|
|
|
|
return ErrFull
|
|
|
|
}
|
|
|
|
|
2022-03-21 13:16:08 +00:00
|
|
|
buck := tx.Bucket(bucketName)
|
2020-11-26 14:26:53 +00:00
|
|
|
if buck == nil {
|
|
|
|
// expected to happen:
|
|
|
|
// - before initialization step (incorrect usage by design)
|
|
|
|
// - if DB is corrupted (in future this case should be handled)
|
2022-11-09 10:59:24 +00:00
|
|
|
return logicerr.Wrap(fmt.Errorf("(%T) bucket for size %d not created", b, sz))
|
2020-11-26 14:26:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// save the object in bucket
|
2022-03-21 13:16:08 +00:00
|
|
|
if err := buck.Put(key, prm.objData); err != nil {
|
2021-05-18 08:12:51 +00:00
|
|
|
return fmt.Errorf("(%T) could not save object in bucket: %w", b, err)
|
2020-11-26 14:26:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
2022-03-21 13:16:08 +00:00
|
|
|
if err == nil {
|
|
|
|
b.incSize(sz)
|
|
|
|
}
|
2020-11-26 14:26:53 +00:00
|
|
|
|
2022-05-31 12:22:32 +00:00
|
|
|
return PutRes{}, err
|
2020-11-26 14:26:53 +00:00
|
|
|
}
|
|
|
|
|
2022-05-31 17:00:41 +00:00
|
|
|
func addressKey(addr oid.Address) []byte {
|
|
|
|
return []byte(addr.EncodeToString())
|
2020-11-26 14:26:53 +00:00
|
|
|
}
|
2021-09-14 14:14:25 +00:00
|
|
|
|
2022-05-31 17:00:41 +00:00
|
|
|
func addressFromKey(dst *oid.Address, data []byte) error {
|
|
|
|
return dst.DecodeString(string(data))
|
2021-09-14 14:14:25 +00:00
|
|
|
}
|