[#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:
parent
35ad6f188e
commit
f1223b46df
1 changed files with 9 additions and 9 deletions
|
@ -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
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue