Skip to content

Commit dd57713

Browse files
committed
Add MakeBerkeleyRODatabase
Implements MakeBerkeleyRODatabase and adds DatabaseFormat::BERKELEY_RO so that MakeDatabase can use BerkeleyRO as the backend database.
1 parent 6e50bee commit dd57713

File tree

4 files changed

+29
-0
lines changed

4 files changed

+29
-0
lines changed

src/wallet/db.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,7 @@ class WalletDatabase
183183
enum class DatabaseFormat {
184184
BERKELEY,
185185
SQLITE,
186+
BERKELEY_RO,
186187
};
187188

188189
struct DatabaseOptions {

src/wallet/migrate.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include <crypto/common.h> // For ReadBE32
66
#include <logging.h>
77
#include <streams.h>
8+
#include <util/translation.h>
89
#include <wallet/migrate.h>
910

1011
#include <optional>
@@ -748,4 +749,18 @@ std::unique_ptr<DatabaseCursor> BerkeleyROBatch::GetNewPrefixCursor(Span<const s
748749
{
749750
return std::make_unique<BerkeleyROCursor>(m_database, prefix);
750751
}
752+
753+
std::unique_ptr<BerkeleyRODatabase> MakeBerkeleyRODatabase(const fs::path& path, const DatabaseOptions& options, DatabaseStatus& status, bilingual_str& error)
754+
{
755+
fs::path data_file = BDBDataFile(path);
756+
try {
757+
std::unique_ptr<BerkeleyRODatabase> db = std::make_unique<BerkeleyRODatabase>(data_file);
758+
status = DatabaseStatus::SUCCESS;
759+
return db;
760+
} catch (const std::runtime_error& e) {
761+
error.original = e.what();
762+
status = DatabaseStatus::FAILED_LOAD;
763+
return nullptr;
764+
}
765+
}
751766
} // namespace wallet

src/wallet/migrate.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,9 @@ class BerkeleyROBatch : public DatabaseBatch
116116
bool TxnCommit() override { return false; }
117117
bool TxnAbort() override { return false; }
118118
};
119+
120+
//! Return object giving access to Berkeley Read Only database at specified path.
121+
std::unique_ptr<BerkeleyRODatabase> MakeBerkeleyRODatabase(const fs::path& path, const DatabaseOptions& options, DatabaseStatus& status, bilingual_str& error);
119122
} // namespace wallet
120123

121124
#endif // BITCOIN_WALLET_MIGRATE_H

src/wallet/walletdb.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#ifdef USE_BDB
2424
#include <wallet/bdb.h>
2525
#endif
26+
#include <wallet/migrate.h>
2627
#ifdef USE_SQLITE
2728
#include <wallet/sqlite.h>
2829
#endif
@@ -1389,6 +1390,11 @@ std::unique_ptr<WalletDatabase> MakeDatabase(const fs::path& path, const Databas
13891390
return nullptr;
13901391
}
13911392

1393+
// If BERKELEY was the format, then change the format from BERKELEY to BERKELEY_RO
1394+
if (format && options.require_format && format == DatabaseFormat::BERKELEY && options.require_format == DatabaseFormat::BERKELEY_RO) {
1395+
format = DatabaseFormat::BERKELEY_RO;
1396+
}
1397+
13921398
// A db already exists so format is set, but options also specifies the format, so make sure they agree
13931399
if (format && options.require_format && format != options.require_format) {
13941400
error = Untranslated(strprintf("Failed to load database path '%s'. Data is not in required format.", fs::PathToString(path)));
@@ -1422,6 +1428,10 @@ std::unique_ptr<WalletDatabase> MakeDatabase(const fs::path& path, const Databas
14221428
}
14231429
}
14241430

1431+
if (format == DatabaseFormat::BERKELEY_RO) {
1432+
return MakeBerkeleyRODatabase(path, options, status, error);
1433+
}
1434+
14251435
#ifdef USE_BDB
14261436
if constexpr (true) {
14271437
return MakeBerkeleyDatabase(path, options, status, error);

0 commit comments

Comments
 (0)