[#1262] blobovnicza: Make helper in Put function idempotent

`Batch` can execute the function multiple times leading to multiple
increases of a size approximation.

Signed-off-by: Evgenii Stratonikov <evgeniy@nspcc.ru>
This commit is contained in:
Evgenii Stratonikov 2022-03-21 16:16:08 +03:00 committed by Alex Vanin
parent 35ad6f188e
commit f1223b46df

View file

@ -55,16 +55,16 @@ func (b *Blobovnicza) Put(prm *PutPrm) (*PutRes, error) {
return nil, errNilAddress return nil, errNilAddress
} }
sz := uint64(len(prm.objData))
bucketName := bucketForSize(sz)
key := addressKey(addr)
err := b.boltDB.Batch(func(tx *bbolt.Tx) error { err := b.boltDB.Batch(func(tx *bbolt.Tx) error {
if b.full() { if b.full() {
return ErrFull return ErrFull
} }
// calculate size buck := tx.Bucket(bucketName)
sz := uint64(len(prm.objData))
// get bucket for size
buck := tx.Bucket(bucketForSize(sz))
if buck == nil { if buck == nil {
// expected to happen: // expected to happen:
// - before initialization step (incorrect usage by design) // - before initialization step (incorrect usage by design)
@ -73,15 +73,15 @@ func (b *Blobovnicza) Put(prm *PutPrm) (*PutRes, error) {
} }
// save the object in bucket // save the object in bucket
if err := buck.Put(addressKey(addr), prm.objData); err != nil { if err := buck.Put(key, prm.objData); err != nil {
return fmt.Errorf("(%T) could not save object in bucket: %w", b, err) return fmt.Errorf("(%T) could not save object in bucket: %w", b, err)
} }
// increase fullness counter
b.incSize(sz)
return nil return nil
}) })
if err == nil {
b.incSize(sz)
}
return nil, err return nil, err
} }