frostfs-node/pkg/local_object_storage/blobovnicza/put.go
Leonard Lyubich 5e2ca0d04b [#791] blobovnicza: Implement method to iterate over object addresses
In previous implementation `Blobovnicza.Iterate` op decoded object data only
and passed it to the handler. There is a need to iterate over all addresses
of the stored objects.

Add `DecodeAddresses` and `WithoutData` methods of `IteratePrm` type. Add
`Address` method to `IterationElement` type. Make `Iterate` to decode object
addresses if `DecodeAddress` was called and not read the data if
`WithoutData` was called. Implement `IterateAddresses` helper function to
simplify the code.

Signed-off-by: Leonard Lyubich <leonard@nspcc.ru>
2021-10-05 10:51:10 +03:00

93 lines
2.1 KiB
Go

package blobovnicza
import (
"errors"
"fmt"
objectSDK "github.com/nspcc-dev/neofs-api-go/pkg/object"
"go.etcd.io/bbolt"
)
// PutPrm groups the parameters of Put operation.
type PutPrm struct {
addr *objectSDK.Address
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.
func (p *PutPrm) SetAddress(addr *objectSDK.Address) {
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.
func (b *Blobovnicza) Put(prm *PutPrm) (*PutRes, error) {
addr := prm.addr
if addr == nil {
return nil, errNilAddress
}
err := b.boltDB.Batch(func(tx *bbolt.Tx) error {
if b.full() {
return ErrFull
}
// calculate size
sz := uint64(len(prm.objData))
// 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)
return fmt.Errorf("(%T) bucket for size %d not created", b, sz)
}
// save the object in bucket
if err := buck.Put(addressKey(addr), prm.objData); err != nil {
return fmt.Errorf("(%T) could not save object in bucket: %w", b, err)
}
// increase fullness counter
b.incSize(sz)
return nil
})
return nil, err
}
func addressKey(addr *objectSDK.Address) []byte {
return []byte(addr.String())
}
func addressFromKey(dst *objectSDK.Address, data []byte) error {
return dst.Parse(string(data))
}