Skip to content

Commit 4aebd83

Browse files
committed
db: Change DatabaseCursor::Next to return status enum
Next()'s result is a tri-state - failed, more to go, complete. Replace the way that this is returned with an enum with values FAIL, MORE, and DONE rather than with two booleans.
1 parent d79e8dc commit 4aebd83

File tree

11 files changed

+49
-55
lines changed

11 files changed

+49
-55
lines changed

src/wallet/bdb.cpp

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -481,11 +481,10 @@ bool BerkeleyDatabase::Rewrite(const char* pszSkip)
481481
while (fSuccess) {
482482
CDataStream ssKey(SER_DISK, CLIENT_VERSION);
483483
CDataStream ssValue(SER_DISK, CLIENT_VERSION);
484-
bool complete;
485-
bool ret1 = cursor->Next(ssKey, ssValue, complete);
486-
if (complete) {
484+
DatabaseCursor::Status ret1 = cursor->Next(ssKey, ssValue);
485+
if (ret1 == DatabaseCursor::Status::DONE) {
487486
break;
488-
} else if (!ret1) {
487+
} else if (ret1 == DatabaseCursor::Status::FAIL) {
489488
fSuccess = false;
490489
break;
491490
}
@@ -668,21 +667,19 @@ BerkeleyCursor::BerkeleyCursor(BerkeleyDatabase& database)
668667
}
669668
}
670669

671-
bool BerkeleyCursor::Next(CDataStream& ssKey, CDataStream& ssValue, bool& complete)
670+
DatabaseCursor::Status BerkeleyCursor::Next(CDataStream& ssKey, CDataStream& ssValue)
672671
{
673-
complete = false;
674-
if (m_cursor == nullptr) return false;
672+
if (m_cursor == nullptr) return Status::FAIL;
675673
// Read at cursor
676674
SafeDbt datKey;
677675
SafeDbt datValue;
678676
int ret = m_cursor->get(datKey, datValue, DB_NEXT);
679677
if (ret == DB_NOTFOUND) {
680-
complete = true;
678+
return Status::DONE;
679+
}
680+
if (ret != 0 || datKey.get_data() == nullptr || datValue.get_data() == nullptr) {
681+
return Status::FAIL;
681682
}
682-
if (ret != 0)
683-
return false;
684-
else if (datKey.get_data() == nullptr || datValue.get_data() == nullptr)
685-
return false;
686683

687684
// Convert to streams
688685
ssKey.SetType(SER_DISK);
@@ -691,7 +688,7 @@ bool BerkeleyCursor::Next(CDataStream& ssKey, CDataStream& ssValue, bool& comple
691688
ssValue.SetType(SER_DISK);
692689
ssValue.clear();
693690
ssValue.write({AsBytePtr(datValue.get_data()), datValue.get_size()});
694-
return true;
691+
return Status::MORE;
695692
}
696693

697694
BerkeleyCursor::~BerkeleyCursor()

src/wallet/bdb.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ class BerkeleyCursor : public DatabaseCursor
194194
explicit BerkeleyCursor(BerkeleyDatabase& database);
195195
~BerkeleyCursor() override;
196196

197-
bool Next(CDataStream& key, CDataStream& value, bool& complete) override;
197+
Status Next(CDataStream& key, CDataStream& value) override;
198198
};
199199

200200
/** RAII class that provides access to a Berkeley database */

src/wallet/db.h

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,14 @@ class DatabaseCursor
3131
DatabaseCursor(const DatabaseCursor&) = delete;
3232
DatabaseCursor& operator=(const DatabaseCursor&) = delete;
3333

34-
virtual bool Next(CDataStream& key, CDataStream& value, bool& complete) { return false; }
34+
enum class Status
35+
{
36+
FAIL,
37+
MORE,
38+
DONE,
39+
};
40+
41+
virtual Status Next(CDataStream& key, CDataStream& value) { return Status::FAIL; }
3542
};
3643

3744
/** RAII class that provides access to a WalletDatabase */
@@ -168,7 +175,7 @@ class WalletDatabase
168175

169176
class DummyCursor : public DatabaseCursor
170177
{
171-
bool Next(CDataStream& key, CDataStream& value, bool& complete) override { return false; }
178+
Status Next(CDataStream& key, CDataStream& value) override { return Status::FAIL; }
172179
};
173180

174181
/** RAII class that provides access to a DummyDatabase. Never fails. */

src/wallet/dump.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -69,13 +69,13 @@ bool DumpWallet(const ArgsManager& args, CWallet& wallet, bilingual_str& error)
6969
while (true) {
7070
CDataStream ss_key(SER_DISK, CLIENT_VERSION);
7171
CDataStream ss_value(SER_DISK, CLIENT_VERSION);
72-
bool complete;
73-
ret = cursor->Next(ss_key, ss_value, complete);
74-
if (complete) {
72+
DatabaseCursor::Status status = cursor->Next(ss_key, ss_value);
73+
if (status == DatabaseCursor::Status::DONE) {
7574
ret = true;
7675
break;
77-
} else if (!ret) {
76+
} else if (status == DatabaseCursor::Status::FAIL) {
7877
error = _("Error reading next record from wallet database");
78+
ret = false;
7979
break;
8080
}
8181
std::string key_str = HexStr(ss_key);

src/wallet/sqlite.cpp

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -470,18 +470,15 @@ bool SQLiteBatch::HasKey(CDataStream&& key)
470470
return res == SQLITE_ROW;
471471
}
472472

473-
bool SQLiteCursor::Next(CDataStream& key, CDataStream& value, bool& complete)
473+
DatabaseCursor::Status SQLiteCursor::Next(CDataStream& key, CDataStream& value)
474474
{
475-
complete = false;
476-
477475
int res = sqlite3_step(m_cursor_stmt);
478476
if (res == SQLITE_DONE) {
479-
complete = true;
480-
return true;
477+
return Status::DONE;
481478
}
482479
if (res != SQLITE_ROW) {
483480
LogPrintf("%s: Unable to execute cursor step: %s\n", __func__, sqlite3_errstr(res));
484-
return false;
481+
return Status::FAIL;
485482
}
486483

487484
// Leftmost column in result is index 0
@@ -491,7 +488,7 @@ bool SQLiteCursor::Next(CDataStream& key, CDataStream& value, bool& complete)
491488
const std::byte* value_data{AsBytePtr(sqlite3_column_blob(m_cursor_stmt, 1))};
492489
size_t value_data_size(sqlite3_column_bytes(m_cursor_stmt, 1));
493490
value.write({value_data, value_data_size});
494-
return true;
491+
return Status::MORE;
495492
}
496493

497494
SQLiteCursor::~SQLiteCursor()

src/wallet/sqlite.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ class SQLiteCursor : public DatabaseCursor
2222
explicit SQLiteCursor() {}
2323
~SQLiteCursor() override;
2424

25-
bool Next(CDataStream& key, CDataStream& value, bool& complete) override;
25+
Status Next(CDataStream& key, CDataStream& value) override;
2626
};
2727

2828
/** RAII class that provides access to a WalletDatabase */

src/wallet/test/util.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,9 @@ std::unique_ptr<WalletDatabase> DuplicateMockDatabase(WalletDatabase& database,
5959
while (true) {
6060
CDataStream key(SER_DISK, CLIENT_VERSION);
6161
CDataStream value(SER_DISK, CLIENT_VERSION);
62-
bool complete;
63-
cursor->Next(key, value, complete);
64-
if (complete) break;
62+
DatabaseCursor::Status status = cursor->Next(key, value);
63+
assert(status != DatabaseCursor::Status::FAIL);
64+
if (status == DatabaseCursor::Status::DONE) break;
6565
new_batch->Write(key, value);
6666
}
6767

src/wallet/test/wallet_tests.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -870,7 +870,7 @@ BOOST_FIXTURE_TEST_CASE(ZapSelectTx, TestChain100Setup)
870870
class FailCursor : public DatabaseCursor
871871
{
872872
public:
873-
bool Next(CDataStream& key, CDataStream& value, bool& complete) override { return false; }
873+
Status Next(CDataStream& key, CDataStream& value) override { return Status::FAIL; }
874874
};
875875

876876
/** RAII class that provides access to a FailDatabase. Which fails if needed. */

src/wallet/test/walletload_tests.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,9 @@ bool HasAnyRecordOfType(WalletDatabase& db, const std::string& key)
6060
while (true) {
6161
CDataStream ssKey(SER_DISK, CLIENT_VERSION);
6262
CDataStream ssValue(SER_DISK, CLIENT_VERSION);
63-
bool complete;
64-
BOOST_CHECK(cursor->Next(ssKey, ssValue, complete));
65-
if (complete) break;
63+
DatabaseCursor::Status status = cursor->Next(ssKey, ssValue);
64+
assert(status != DatabaseCursor::Status::FAIL);
65+
if (status == DatabaseCursor::Status::DONE) break;
6666
std::string type;
6767
ssKey >> type;
6868
if (type == key) return true;

src/wallet/wallet.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3776,12 +3776,12 @@ bool CWallet::MigrateToSQLite(bilingual_str& error)
37763776
error = _("Error: Unable to begin reading all records in the database");
37773777
return false;
37783778
}
3779-
bool complete = false;
3779+
DatabaseCursor::Status status = DatabaseCursor::Status::FAIL;
37803780
while (true) {
37813781
CDataStream ss_key(SER_DISK, CLIENT_VERSION);
37823782
CDataStream ss_value(SER_DISK, CLIENT_VERSION);
3783-
bool ret = cursor->Next(ss_key, ss_value, complete);
3784-
if (complete || !ret) {
3783+
status = cursor->Next(ss_key, ss_value);
3784+
if (status != DatabaseCursor::Status::MORE) {
37853785
break;
37863786
}
37873787
SerializeData key(ss_key.begin(), ss_key.end());
@@ -3790,7 +3790,7 @@ bool CWallet::MigrateToSQLite(bilingual_str& error)
37903790
}
37913791
cursor.reset();
37923792
batch.reset();
3793-
if (!complete) {
3793+
if (status != DatabaseCursor::Status::DONE) {
37943794
error = _("Error: Unable to read all records in the database");
37953795
return false;
37963796
}

0 commit comments

Comments
 (0)