2020-11-26 14:26:53 +00:00
|
|
|
package blobovnicza
|
|
|
|
|
|
|
|
import (
|
2021-05-18 08:12:51 +00:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
|
2022-01-26 12:11:13 +00:00
|
|
|
addressSDK "github.com/nspcc-dev/neofs-sdk-go/object/address"
|
2020-11-26 14:26:53 +00:00
|
|
|
"go.etcd.io/bbolt"
|
|
|
|
)
|
|
|
|
|
|
|
|
// PutPrm groups the parameters of Put operation.
|
|
|
|
type PutPrm struct {
|
2022-01-26 12:11:13 +00:00
|
|
|
addr *addressSDK.Address
|
2020-11-26 14:26:53 +00:00
|
|
|
|
|
|
|
objData []byte
|
|
|
|
}
|
|
|
|
|
|
|
|
// PutRes groups resulting values of Put operation.
|
|
|
|
type PutRes struct {
|
|
|
|
}
|
|
|
|
|
|
|
|
// ErrFull is returned returned when trying to save an
|
|
|
|
// object to a filled blobovnicza.
|
|
|
|
var ErrFull = errors.New("blobovnicza is full")
|
|
|
|
|
|
|
|
var errNilAddress = errors.New("object address is nil")
|
|
|
|
|
|
|
|
// SetAddress sets address of saving object.
|
2022-01-26 12:11:13 +00:00
|
|
|
func (p *PutPrm) SetAddress(addr *addressSDK.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
|
|
|
|
}
|
|
|
|
|
|
|
|
// Put saves object in Blobovnicza.
|
|
|
|
//
|
|
|
|
// 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.
|
2020-11-26 14:26:53 +00:00
|
|
|
func (b *Blobovnicza) Put(prm *PutPrm) (*PutRes, error) {
|
|
|
|
addr := prm.addr
|
|
|
|
if addr == nil {
|
2020-12-02 09:58:42 +00:00
|
|
|
return nil, errNilAddress
|
2020-11-26 14:26:53 +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
|
|
|
|
}
|
|
|
|
|
|
|
|
// calculate size
|
2020-12-02 09:58:42 +00:00
|
|
|
sz := uint64(len(prm.objData))
|
2020-11-26 14:26:53 +00:00
|
|
|
|
|
|
|
// get bucket for size
|
|
|
|
buck := tx.Bucket(bucketForSize(sz))
|
|
|
|
if buck == nil {
|
|
|
|
// expected to happen:
|
|
|
|
// - before initialization step (incorrect usage by design)
|
|
|
|
// - if DB is corrupted (in future this case should be handled)
|
2021-05-18 08:12:51 +00:00
|
|
|
return fmt.Errorf("(%T) bucket for size %d not created", b, sz)
|
2020-11-26 14:26:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// save the object in bucket
|
2020-12-02 09:58:42 +00:00
|
|
|
if err := buck.Put(addressKey(addr), 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
|
|
|
}
|
|
|
|
|
|
|
|
// increase fullness counter
|
|
|
|
b.incSize(sz)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-01-26 12:11:13 +00:00
|
|
|
func addressKey(addr *addressSDK.Address) []byte {
|
2020-11-26 14:26:53 +00:00
|
|
|
return []byte(addr.String())
|
|
|
|
}
|
2021-09-14 14:14:25 +00:00
|
|
|
|
2022-01-26 12:11:13 +00:00
|
|
|
func addressFromKey(dst *addressSDK.Address, data []byte) error {
|
2021-09-14 14:14:25 +00:00
|
|
|
return dst.Parse(string(data))
|
|
|
|
}
|