From f65163edffaedf10e5ea9f876661801df24dac35 Mon Sep 17 00:00:00 2001 From: Alex Sharov Date: Fri, 12 Mar 2021 16:27:04 +0700 Subject: [PATCH] TxDb: Walk method - to reuse cursors (#1548) --- ethdb/tx_db.go | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/ethdb/tx_db.go b/ethdb/tx_db.go index 4a36f584991..3f41db64433 100644 --- a/ethdb/tx_db.go +++ b/ethdb/tx_db.go @@ -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) }