[#378] metabase: Add the ability to break the loop in IterateOverGraveyard

Make `DB.IterateOverGraveyard` to immediately return nil if GraveHandler
returns ErrInterruptIterator.

Signed-off-by: Leonard Lyubich <leonard@nspcc.ru>
This commit is contained in:
Leonard Lyubich 2021-02-19 11:19:28 +03:00 committed by Alex Vanin
parent ff442e9559
commit f5f416268f

View file

@ -30,7 +30,8 @@ type GraveHandler func(*Grave) error
// IterateOverGraveyard iterates over all graves in DB. // IterateOverGraveyard iterates over all graves in DB.
// //
// Returns errors of h directly. // If h returns ErrInterruptIterator, nil returns immediately.
// Returns other errors of h directly.
func (db *DB) IterateOverGraveyard(h GraveHandler) error { func (db *DB) IterateOverGraveyard(h GraveHandler) error {
return db.boltDB.View(func(tx *bbolt.Tx) error { return db.boltDB.View(func(tx *bbolt.Tx) error {
return db.iterateOverGraveyard(tx, h) return db.iterateOverGraveyard(tx, h)
@ -45,7 +46,7 @@ func (db *DB) iterateOverGraveyard(tx *bbolt.Tx, h GraveHandler) error {
} }
// iterate over all graves // iterate over all graves
return bktGraveyard.ForEach(func(k, v []byte) error { err := bktGraveyard.ForEach(func(k, v []byte) error {
// parse Grave // parse Grave
g, err := graveFromKV(k, v) g, err := graveFromKV(k, v)
if err != nil { if err != nil {
@ -55,6 +56,12 @@ func (db *DB) iterateOverGraveyard(tx *bbolt.Tx, h GraveHandler) error {
// handler Grave // handler Grave
return h(g) return h(g)
}) })
if errors.Is(err, ErrInterruptIterator) {
err = nil
}
return err
} }
func graveFromKV(k, v []byte) (*Grave, error) { func graveFromKV(k, v []byte) (*Grave, error) {