[#915] blobovnicza: Re-use Bolt transaction in syncFullnessCounter

Make `syncFullnessCounter` to accept `bbolt.Tx` argument of Bolt
transaction within which counter should be synchronized. Pass
corresponding transaction during `Init`.

Signed-off-by: Leonard Lyubich <leonard@nspcc.ru>
This commit is contained in:
Leonard Lyubich 2021-12-08 13:45:23 +03:00 committed by LeL
parent feb0a65efb
commit 2920c5203b
2 changed files with 15 additions and 20 deletions

View file

@ -65,7 +65,7 @@ func (b *Blobovnicza) Init() error {
b.log.Debug("bucket already exists, initializing state")
return true, b.syncFullnessCounter()
return true, b.syncFullnessCounter(tx)
}
if err != nil {

View file

@ -52,29 +52,24 @@ func (b *Blobovnicza) full() bool {
return b.filled.Load() >= b.fullSizeLimit
}
func (b *Blobovnicza) syncFullnessCounter() error {
err := b.boltDB.View(func(tx *bbolt.Tx) error {
sz := uint64(0)
func (b *Blobovnicza) syncFullnessCounter(tx *bbolt.Tx) error {
sz := uint64(0)
if err := b.iterateBucketKeys(func(lower, upper uint64, key []byte) (bool, error) {
buck := tx.Bucket(key)
if buck == nil {
return false, fmt.Errorf("bucket not found %s", stringifyBounds(lower, upper))
}
sz += uint64(buck.Stats().KeyN) * (upper - lower)
return false, nil
}); err != nil {
return err
if err := b.iterateBucketKeys(func(lower, upper uint64, key []byte) (bool, error) {
buck := tx.Bucket(key)
if buck == nil {
return false, fmt.Errorf("bucket not found %s", stringifyBounds(lower, upper))
}
b.filled.Store(sz)
sz += uint64(buck.Stats().KeyN) * (upper - lower)
return nil
})
if err != nil {
return fmt.Errorf("could not sync fullness counter: %w", err)
return false, nil
}); err != nil {
return err
}
b.filled.Store(sz)
return nil
}