Skip to content

Commit

Permalink
TxDb: Walk method - to reuse cursors (erigontech#1548)
Browse files Browse the repository at this point in the history
  • Loading branch information
AskAlexSharov authored and sam committed Apr 5, 2021
1 parent 161a554 commit f65163e
Showing 1 changed file with 14 additions and 2 deletions.
16 changes: 14 additions & 2 deletions ethdb/tx_db.go
Original file line number Diff line number Diff line change
Expand Up @@ -222,8 +222,20 @@ func (m *TxDb) IdealBatchSize() int {

func (m *TxDb) Walk(bucket string, startkey []byte, fixedbits int, walker func([]byte, []byte) (bool, error)) error {
m.panicOnEmptyDB()
c := m.tx.Cursor(bucket) // create new cursor, then call other methods of TxDb inside MultiWalk callback will not affect this cursor
defer c.Close()
// get cursor out of pool, then calls txDb.Put/Get/Delete on same bucket inside Walk callback - will not affect state of Walk
c, ok := m.cursors[bucket]
if ok {
delete(m.cursors, bucket)
} else {
c = m.tx.Cursor(bucket)
}
defer func() { // put cursor back to pool if can
if _, ok = m.cursors[bucket]; ok {
c.Close()
} else {
m.cursors[bucket] = c
}
}()
return Walk(c, startkey, fixedbits, walker)
}

Expand Down

0 comments on commit f65163e

Please sign in to comment.