[#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") b.log.Debug("bucket already exists, initializing state")
return true, b.syncFullnessCounter() return true, b.syncFullnessCounter(tx)
} }
if err != nil { if err != nil {

View file

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