Bitcoin Core 28.99.0
P2P Digital Currency
migrate.h
Go to the documentation of this file.
1// Copyright (c) 2021 The Bitcoin Core developers
2// Distributed under the MIT software license, see the accompanying
3// file COPYING or http://www.opensource.org/licenses/mit-license.php.
4
5#ifndef BITCOIN_WALLET_MIGRATE_H
6#define BITCOIN_WALLET_MIGRATE_H
7
8#include <wallet/db.h>
9
10#include <optional>
11
12namespace wallet {
13
14using BerkeleyROData = std::map<SerializeData, SerializeData, std::less<>>;
15
21{
22private:
24
25public:
27 BerkeleyRODatabase(const fs::path& filepath, bool open = true) : WalletDatabase(), m_filepath(filepath)
28 {
29 if (open) Open();
30 }
32
34
36 void Open() override;
37
39 void AddRef() override {}
41 void RemoveRef() override {}
42
45 bool Rewrite(const char* pszSkip = nullptr) override { return false; }
46
49 bool Backup(const std::string& strDest) const override;
50
53 void Flush() override {}
57 void Close() override {}
58 /* flush the wallet passively (TRY_LOCK)
59 ideal to be called periodically */
60 bool PeriodicFlush() override { return false; }
61
62 void IncrementUpdateCounter() override {}
63
64 void ReloadDbEnv() override {}
65
67 std::string Filename() override { return fs::PathToString(m_filepath); }
68
69 std::string Format() override { return "bdb_ro"; }
70
72 std::unique_ptr<DatabaseBatch> MakeBatch(bool flush_on_close = true) override;
73};
74
76{
77private:
79 BerkeleyROData::const_iterator m_cursor;
80 BerkeleyROData::const_iterator m_cursor_end;
81
82public:
84 ~BerkeleyROCursor() = default;
85
86 Status Next(DataStream& key, DataStream& value) override;
87};
88
91{
92private:
94
95 bool ReadKey(DataStream&& key, DataStream& value) override;
96 // WriteKey returns true since various automatic upgrades for older wallets will expect writing to not fail.
97 // It is okay for this batch type to not actually write anything as those automatic upgrades will occur again after migration.
98 bool WriteKey(DataStream&& key, DataStream&& value, bool overwrite = true) override { return true; }
99 bool EraseKey(DataStream&& key) override { return false; }
100 bool HasKey(DataStream&& key) override;
101 bool ErasePrefix(Span<const std::byte> prefix) override { return false; }
102
103public:
104 explicit BerkeleyROBatch(const BerkeleyRODatabase& database) : m_database(database) {}
105 ~BerkeleyROBatch() = default;
106
109
110 void Flush() override {}
111 void Close() override {}
112
113 std::unique_ptr<DatabaseCursor> GetNewCursor() override { return std::make_unique<BerkeleyROCursor>(m_database); }
114 std::unique_ptr<DatabaseCursor> GetNewPrefixCursor(Span<const std::byte> prefix) override;
115 bool TxnBegin() override { return false; }
116 bool TxnCommit() override { return false; }
117 bool TxnAbort() override { return false; }
118 bool HasActiveTxn() override { return false; }
119};
120
122std::unique_ptr<BerkeleyRODatabase> MakeBerkeleyRODatabase(const fs::path& path, const DatabaseOptions& options, DatabaseStatus& status, bilingual_str& error);
123} // namespace wallet
124
125#endif // BITCOIN_WALLET_MIGRATE_H
Double ended buffer combining vector and stream-like interfaces.
Definition: streams.h:147
Path class wrapper to block calls to the fs::path(std::string) implicit constructor and the fs::path:...
Definition: fs.h:33
RAII class that provides access to a BerkeleyRODatabase.
Definition: migrate.h:91
BerkeleyROBatch(const BerkeleyROBatch &)=delete
void Flush() override
Definition: migrate.h:110
bool ReadKey(DataStream &&key, DataStream &value) override
Definition: migrate.cpp:730
bool ErasePrefix(Span< const std::byte > prefix) override
Definition: migrate.h:101
bool WriteKey(DataStream &&key, DataStream &&value, bool overwrite=true) override
Definition: migrate.h:98
BerkeleyROBatch(const BerkeleyRODatabase &database)
Definition: migrate.h:104
bool HasActiveTxn() override
Definition: migrate.h:118
BerkeleyROBatch & operator=(const BerkeleyROBatch &)=delete
bool TxnCommit() override
Definition: migrate.h:116
bool TxnAbort() override
Definition: migrate.h:117
std::unique_ptr< DatabaseCursor > GetNewPrefixCursor(Span< const std::byte > prefix) override
Definition: migrate.cpp:766
const BerkeleyRODatabase & m_database
Definition: migrate.h:93
std::unique_ptr< DatabaseCursor > GetNewCursor() override
Definition: migrate.h:113
bool EraseKey(DataStream &&key) override
Definition: migrate.h:99
bool HasKey(DataStream &&key) override
Definition: migrate.cpp:743
bool TxnBegin() override
Definition: migrate.h:115
void Close() override
Definition: migrate.h:111
BerkeleyROData::const_iterator m_cursor_end
Definition: migrate.h:80
BerkeleyROCursor(const BerkeleyRODatabase &database, Span< const std::byte > prefix={})
Definition: migrate.cpp:749
BerkeleyROData::const_iterator m_cursor
Definition: migrate.h:79
const BerkeleyRODatabase & m_database
Definition: migrate.h:78
Status Next(DataStream &key, DataStream &value) override
Definition: migrate.cpp:755
A class representing a BerkeleyDB file from which we can only read records.
Definition: migrate.h:21
void AddRef() override
Indicate the a new database user has began using the database.
Definition: migrate.h:39
BerkeleyROData m_records
Definition: migrate.h:33
void Close() override
Flush to the database file and close the database.
Definition: migrate.h:57
void Flush() override
Make sure all changes are flushed to database file.
Definition: migrate.h:53
void Open() override
Open the database if it is not already opened.
Definition: migrate.cpp:524
bool Rewrite(const char *pszSkip=nullptr) override
Rewrite the entire database on disk, with the exception of key pszSkip if non-zero.
Definition: migrate.h:45
const fs::path m_filepath
Definition: migrate.h:23
std::string Format() override
Definition: migrate.h:69
bool PeriodicFlush() override
Definition: migrate.h:60
std::unique_ptr< DatabaseBatch > MakeBatch(bool flush_on_close=true) override
Make a DatabaseBatch connected to this database.
Definition: migrate.cpp:702
void RemoveRef() override
Indicate that database user has stopped using the database and that it could be flushed or closed.
Definition: migrate.h:41
void IncrementUpdateCounter() override
Definition: migrate.h:62
void ReloadDbEnv() override
Definition: migrate.h:64
std::string Filename() override
Return path to main database file for logs and error messages.
Definition: migrate.h:67
bool Backup(const std::string &strDest) const override
Back up the entire database to a file.
Definition: migrate.cpp:707
BerkeleyRODatabase(const fs::path &filepath, bool open=true)
Create DB handle.
Definition: migrate.h:27
RAII class that provides access to a WalletDatabase.
Definition: db.h:51
An instance of this class represents one database.
Definition: db.h:131
static std::string PathToString(const path &path)
Convert path object to a byte string.
Definition: fs.h:151
std::map< SerializeData, SerializeData, std::less<> > BerkeleyROData
Definition: migrate.h:14
std::unique_ptr< BerkeleyRODatabase > MakeBerkeleyRODatabase(const fs::path &path, const DatabaseOptions &options, DatabaseStatus &status, bilingual_str &error)
Return object giving access to Berkeley Read Only database at specified path.
Definition: migrate.cpp:771
DatabaseStatus
Definition: db.h:205
const char * prefix
Definition: rest.cpp:1009
Bilingual messages:
Definition: translation.h:21