From f1223b46df326f150ed2e11c6aef6aa781b270d3 Mon Sep 17 00:00:00 2001 From: Evgenii Stratonikov Date: Mon, 21 Mar 2022 16:16:08 +0300 Subject: [PATCH] [#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 --- pkg/local_object_storage/blobovnicza/put.go | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/pkg/local_object_storage/blobovnicza/put.go b/pkg/local_object_storage/blobovnicza/put.go index c86043720..30a3c7bd6 100644 --- a/pkg/local_object_storage/blobovnicza/put.go +++ b/pkg/local_object_storage/blobovnicza/put.go @@ -55,16 +55,16 @@ func (b *Blobovnicza) Put(prm *PutPrm) (*PutRes, error) { return nil, errNilAddress } + sz := uint64(len(prm.objData)) + bucketName := bucketForSize(sz) + key := addressKey(addr) + 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)) + buck := tx.Bucket(bucketName) if buck == nil { // expected to happen: // - before initialization step (incorrect usage by design) @@ -73,15 +73,15 @@ func (b *Blobovnicza) Put(prm *PutPrm) (*PutRes, error) { } // 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) } - // increase fullness counter - b.incSize(sz) - return nil }) + if err == nil { + b.incSize(sz) + } return nil, err }