Skip to content

Commit d79e8dc

Browse files
committed
wallet: Have cursor users use DatabaseCursor directly
Instead of having the DatabaseBatch manage the cursor, having the consumer handle it directly
1 parent 7a198bb commit d79e8dc

File tree

7 files changed

+29
-43
lines changed

7 files changed

+29
-43
lines changed

src/wallet/bdb.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -399,7 +399,6 @@ void BerkeleyBatch::Close()
399399
activeTxn->abort();
400400
activeTxn = nullptr;
401401
pdb = nullptr;
402-
CloseCursor();
403402

404403
if (fFlushOnClose)
405404
Flush();
@@ -477,12 +476,13 @@ bool BerkeleyDatabase::Rewrite(const char* pszSkip)
477476
fSuccess = false;
478477
}
479478

480-
if (db.StartCursor()) {
479+
std::unique_ptr<DatabaseCursor> cursor = db.GetNewCursor();
480+
if (cursor) {
481481
while (fSuccess) {
482482
CDataStream ssKey(SER_DISK, CLIENT_VERSION);
483483
CDataStream ssValue(SER_DISK, CLIENT_VERSION);
484484
bool complete;
485-
bool ret1 = db.ReadAtCursor(ssKey, ssValue, complete);
485+
bool ret1 = cursor->Next(ssKey, ssValue, complete);
486486
if (complete) {
487487
break;
488488
} else if (!ret1) {
@@ -503,7 +503,7 @@ bool BerkeleyDatabase::Rewrite(const char* pszSkip)
503503
if (ret2 > 0)
504504
fSuccess = false;
505505
}
506-
db.CloseCursor();
506+
cursor.reset();
507507
}
508508
if (fSuccess) {
509509
db.Close();

src/wallet/db.h

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,6 @@ class DatabaseCursor
3838
class DatabaseBatch
3939
{
4040
private:
41-
std::unique_ptr<DatabaseCursor> m_cursor;
42-
4341
virtual bool ReadKey(CDataStream&& key, CDataStream& value) = 0;
4442
virtual bool WriteKey(CDataStream&& key, CDataStream&& value, bool overwrite=true) = 0;
4543
virtual bool EraseKey(CDataStream&& key) = 0;
@@ -107,20 +105,6 @@ class DatabaseBatch
107105
}
108106

109107
virtual std::unique_ptr<DatabaseCursor> GetNewCursor() = 0;
110-
bool StartCursor()
111-
{
112-
m_cursor = GetNewCursor();
113-
return m_cursor != nullptr;
114-
}
115-
bool ReadAtCursor(CDataStream& ssKey, CDataStream& ssValue, bool& complete)
116-
{
117-
if (!m_cursor) return false;
118-
return m_cursor->Next(ssKey, ssValue, complete);
119-
}
120-
void CloseCursor()
121-
{
122-
m_cursor.reset();
123-
}
124108
virtual bool TxnBegin() = 0;
125109
virtual bool TxnCommit() = 0;
126110
virtual bool TxnAbort() = 0;

src/wallet/dump.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,8 @@ bool DumpWallet(const ArgsManager& args, CWallet& wallet, bilingual_str& error)
4747
std::unique_ptr<DatabaseBatch> batch = db.MakeBatch();
4848

4949
bool ret = true;
50-
if (!batch->StartCursor()) {
50+
std::unique_ptr<DatabaseCursor> cursor = batch->GetNewCursor();
51+
if (!cursor) {
5152
error = _("Error: Couldn't create cursor into database");
5253
ret = false;
5354
}
@@ -69,7 +70,7 @@ bool DumpWallet(const ArgsManager& args, CWallet& wallet, bilingual_str& error)
6970
CDataStream ss_key(SER_DISK, CLIENT_VERSION);
7071
CDataStream ss_value(SER_DISK, CLIENT_VERSION);
7172
bool complete;
72-
ret = batch->ReadAtCursor(ss_key, ss_value, complete);
73+
ret = cursor->Next(ss_key, ss_value, complete);
7374
if (complete) {
7475
ret = true;
7576
break;
@@ -85,7 +86,7 @@ bool DumpWallet(const ArgsManager& args, CWallet& wallet, bilingual_str& error)
8586
}
8687
}
8788

88-
batch->CloseCursor();
89+
cursor.reset();
8990
batch.reset();
9091

9192
// Close the wallet after we're done with it. The caller won't be doing this

src/wallet/test/util.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ std::unique_ptr<WalletDatabase> DuplicateMockDatabase(WalletDatabase& database,
5050

5151
// Get a cursor to the original database
5252
auto batch = database.MakeBatch();
53-
batch->StartCursor();
53+
std::unique_ptr<wallet::DatabaseCursor> cursor = batch->GetNewCursor();
5454

5555
// Get a batch for the new database
5656
auto new_batch = new_database->MakeBatch();
@@ -60,7 +60,7 @@ std::unique_ptr<WalletDatabase> DuplicateMockDatabase(WalletDatabase& database,
6060
CDataStream key(SER_DISK, CLIENT_VERSION);
6161
CDataStream value(SER_DISK, CLIENT_VERSION);
6262
bool complete;
63-
batch->ReadAtCursor(key, value, complete);
63+
cursor->Next(key, value, complete);
6464
if (complete) break;
6565
new_batch->Write(key, value);
6666
}

src/wallet/test/walletload_tests.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,12 +54,14 @@ BOOST_FIXTURE_TEST_CASE(wallet_load_unknown_descriptor, TestingSetup)
5454
bool HasAnyRecordOfType(WalletDatabase& db, const std::string& key)
5555
{
5656
std::unique_ptr<DatabaseBatch> batch = db.MakeBatch(false);
57-
BOOST_CHECK(batch->StartCursor());
57+
BOOST_CHECK(batch);
58+
std::unique_ptr<DatabaseCursor> cursor = batch->GetNewCursor();
59+
BOOST_CHECK(cursor);
5860
while (true) {
5961
CDataStream ssKey(SER_DISK, CLIENT_VERSION);
6062
CDataStream ssValue(SER_DISK, CLIENT_VERSION);
6163
bool complete;
62-
BOOST_CHECK(batch->ReadAtCursor(ssKey, ssValue, complete));
64+
BOOST_CHECK(cursor->Next(ssKey, ssValue, complete));
6365
if (complete) break;
6466
std::string type;
6567
ssKey >> type;

src/wallet/wallet.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3770,24 +3770,25 @@ bool CWallet::MigrateToSQLite(bilingual_str& error)
37703770

37713771
// Get all of the records for DB type migration
37723772
std::unique_ptr<DatabaseBatch> batch = m_database->MakeBatch();
3773+
std::unique_ptr<DatabaseCursor> cursor = batch->GetNewCursor();
37733774
std::vector<std::pair<SerializeData, SerializeData>> records;
3774-
if (!batch->StartCursor()) {
3775+
if (!cursor) {
37753776
error = _("Error: Unable to begin reading all records in the database");
37763777
return false;
37773778
}
37783779
bool complete = false;
37793780
while (true) {
37803781
CDataStream ss_key(SER_DISK, CLIENT_VERSION);
37813782
CDataStream ss_value(SER_DISK, CLIENT_VERSION);
3782-
bool ret = batch->ReadAtCursor(ss_key, ss_value, complete);
3783-
if (!ret) {
3783+
bool ret = cursor->Next(ss_key, ss_value, complete);
3784+
if (complete || !ret) {
37843785
break;
37853786
}
37863787
SerializeData key(ss_key.begin(), ss_key.end());
37873788
SerializeData value(ss_value.begin(), ss_value.end());
37883789
records.emplace_back(key, value);
37893790
}
3790-
batch->CloseCursor();
3791+
cursor.reset();
37913792
batch.reset();
37923793
if (!complete) {
37933794
error = _("Error: Unable to read all records in the database");

src/wallet/walletdb.cpp

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -812,7 +812,8 @@ DBErrors WalletBatch::LoadWallet(CWallet* pwallet)
812812
#endif
813813

814814
// Get cursor
815-
if (!m_batch->StartCursor())
815+
std::unique_ptr<DatabaseCursor> cursor = m_batch->GetNewCursor();
816+
if (!cursor)
816817
{
817818
pwallet->WalletLogPrintf("Error getting wallet database cursor\n");
818819
return DBErrors::CORRUPT;
@@ -824,13 +825,13 @@ DBErrors WalletBatch::LoadWallet(CWallet* pwallet)
824825
CDataStream ssKey(SER_DISK, CLIENT_VERSION);
825826
CDataStream ssValue(SER_DISK, CLIENT_VERSION);
826827
bool complete;
827-
bool ret = m_batch->ReadAtCursor(ssKey, ssValue, complete);
828+
bool ret = cursor->Next(ssKey, ssValue, complete);
828829
if (complete) {
829830
break;
830831
}
831832
else if (!ret)
832833
{
833-
m_batch->CloseCursor();
834+
cursor.reset();
834835
pwallet->WalletLogPrintf("Error reading next record from wallet database\n");
835836
return DBErrors::CORRUPT;
836837
}
@@ -878,7 +879,6 @@ DBErrors WalletBatch::LoadWallet(CWallet* pwallet)
878879
} catch (...) {
879880
result = DBErrors::CORRUPT;
880881
}
881-
m_batch->CloseCursor();
882882

883883
// Set the active ScriptPubKeyMans
884884
for (auto spk_man_pair : wss.m_active_external_spks) {
@@ -986,7 +986,8 @@ DBErrors WalletBatch::FindWalletTx(std::vector<uint256>& vTxHash, std::list<CWal
986986
}
987987

988988
// Get cursor
989-
if (!m_batch->StartCursor())
989+
std::unique_ptr<DatabaseCursor> cursor = m_batch->GetNewCursor();
990+
if (!cursor)
990991
{
991992
LogPrintf("Error getting wallet database cursor\n");
992993
return DBErrors::CORRUPT;
@@ -998,11 +999,10 @@ DBErrors WalletBatch::FindWalletTx(std::vector<uint256>& vTxHash, std::list<CWal
998999
CDataStream ssKey(SER_DISK, CLIENT_VERSION);
9991000
CDataStream ssValue(SER_DISK, CLIENT_VERSION);
10001001
bool complete;
1001-
bool ret = m_batch->ReadAtCursor(ssKey, ssValue, complete);
1002+
bool ret = cursor->Next(ssKey, ssValue, complete);
10021003
if (complete) {
10031004
break;
10041005
} else if (!ret) {
1005-
m_batch->CloseCursor();
10061006
LogPrintf("Error reading next record from wallet database\n");
10071007
return DBErrors::CORRUPT;
10081008
}
@@ -1020,7 +1020,6 @@ DBErrors WalletBatch::FindWalletTx(std::vector<uint256>& vTxHash, std::list<CWal
10201020
} catch (...) {
10211021
result = DBErrors::CORRUPT;
10221022
}
1023-
m_batch->CloseCursor();
10241023

10251024
return result;
10261025
}
@@ -1114,7 +1113,8 @@ bool WalletBatch::WriteWalletFlags(const uint64_t flags)
11141113
bool WalletBatch::EraseRecords(const std::unordered_set<std::string>& types)
11151114
{
11161115
// Get cursor
1117-
if (!m_batch->StartCursor())
1116+
std::unique_ptr<DatabaseCursor> cursor = m_batch->GetNewCursor();
1117+
if (!cursor)
11181118
{
11191119
return false;
11201120
}
@@ -1126,13 +1126,12 @@ bool WalletBatch::EraseRecords(const std::unordered_set<std::string>& types)
11261126
CDataStream key(SER_DISK, CLIENT_VERSION);
11271127
CDataStream value(SER_DISK, CLIENT_VERSION);
11281128
bool complete;
1129-
bool ret = m_batch->ReadAtCursor(key, value, complete);
1129+
bool ret = cursor->Next(key, value, complete);
11301130
if (complete) {
11311131
break;
11321132
}
11331133
else if (!ret)
11341134
{
1135-
m_batch->CloseCursor();
11361135
return false;
11371136
}
11381137

@@ -1146,7 +1145,6 @@ bool WalletBatch::EraseRecords(const std::unordered_set<std::string>& types)
11461145
m_batch->Erase(key_data);
11471146
}
11481147
}
1149-
m_batch->CloseCursor();
11501148
return true;
11511149
}
11521150

0 commit comments

Comments
 (0)