Bitcoin Core  29.1.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 
12 namespace wallet {
13 
14 using BerkeleyROData = std::map<SerializeData, SerializeData, std::less<>>;
15 
21 {
22 private:
24 
25 public:
27  BerkeleyRODatabase(const fs::path& filepath, bool open = true) : WalletDatabase(), m_filepath(filepath)
28  {
29  if (open) Open();
30  }
31  ~BerkeleyRODatabase() = default;
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 {
77 private:
79  BerkeleyROData::const_iterator m_cursor;
80  BerkeleyROData::const_iterator m_cursor_end;
81 
82 public:
83  explicit BerkeleyROCursor(const BerkeleyRODatabase& database, Span<const std::byte> prefix = {});
84  ~BerkeleyROCursor() = default;
85 
86  Status Next(DataStream& key, DataStream& value) override;
87 };
88 
91 {
92 private:
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 
103 public:
104  explicit BerkeleyROBatch(const BerkeleyRODatabase& database) : m_database(database) {}
105  ~BerkeleyROBatch() = default;
106 
107  BerkeleyROBatch(const BerkeleyROBatch&) = delete;
108  BerkeleyROBatch& operator=(const BerkeleyROBatch&) = delete;
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 
122 std::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
void RemoveRef() override
Indicate that database user has stopped using the database and that it could be flushed or closed...
Definition: migrate.h:41
bool Backup(const std::string &strDest) const override
Back up the entire database to a file.
Definition: migrate.cpp:707
BerkeleyROData::const_iterator m_cursor_end
Definition: migrate.h:80
std::map< SerializeData, SerializeData, std::less<> > BerkeleyROData
Definition: migrate.h:14
BerkeleyROData::const_iterator m_cursor
Definition: migrate.h:79
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
bool WriteKey(DataStream &&key, DataStream &&value, bool overwrite=true) override
Definition: migrate.h:98
Bilingual messages:
Definition: translation.h:24
bool TxnCommit() override
Definition: migrate.h:116
const char * prefix
Definition: rest.cpp:1009
void Open() override
Open the database if it is not already opened.
Definition: migrate.cpp:524
A class representing a BerkeleyDB file from which we can only read records.
Definition: migrate.h:20
BerkeleyROBatch & operator=(const BerkeleyROBatch &)=delete
RAII class that provides access to a WalletDatabase.
Definition: db.h:50
bool EraseKey(DataStream &&key) override
Definition: migrate.h:99
bool TxnBegin() override
Definition: migrate.h:115
bool ErasePrefix(Span< const std::byte > prefix) override
Definition: migrate.h:101
void Close() override
Flush to the database file and close the database.
Definition: migrate.h:57
void Flush() override
Definition: migrate.h:110
static std::string PathToString(const path &path)
Convert path object to a byte string.
Definition: fs.h:151
void IncrementUpdateCounter() override
Definition: migrate.h:62
Double ended buffer combining vector and stream-like interfaces.
Definition: streams.h:146
bool PeriodicFlush() override
Definition: migrate.h:60
Status Next(DataStream &key, DataStream &value) override
Definition: migrate.cpp:755
std::unique_ptr< DatabaseCursor > GetNewPrefixCursor(Span< const std::byte > prefix) override
Definition: migrate.cpp:766
RAII class that provides access to a BerkeleyRODatabase.
Definition: migrate.h:90
BerkeleyROCursor(const BerkeleyRODatabase &database, Span< const std::byte > prefix={})
Definition: migrate.cpp:749
std::unique_ptr< DatabaseBatch > MakeBatch(bool flush_on_close=true) override
Make a DatabaseBatch connected to this database.
Definition: migrate.cpp:702
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
bool HasKey(DataStream &&key) override
Definition: migrate.cpp:743
BerkeleyROData m_records
Definition: migrate.h:33
void ReloadDbEnv() override
Definition: migrate.h:64
std::string Format() override
Definition: migrate.h:69
DatabaseStatus
Definition: db.h:205
bool ReadKey(DataStream &&key, DataStream &value) override
Definition: migrate.cpp:730
void Close() override
Definition: migrate.h:111
bool HasActiveTxn() override
Definition: migrate.h:118
std::unique_ptr< DatabaseCursor > GetNewCursor() override
Definition: migrate.h:113
BerkeleyROBatch(const BerkeleyRODatabase &database)
Definition: migrate.h:104
BerkeleyRODatabase(const fs::path &filepath, bool open=true)
Create DB handle.
Definition: migrate.h:27
bool TxnAbort() override
Definition: migrate.h:117
std::string Filename() override
Return path to main database file for logs and error messages.
Definition: migrate.h:67
void Flush() override
Make sure all changes are flushed to database file.
Definition: migrate.h:53
void AddRef() override
Indicate the a new database user has began using the database.
Definition: migrate.h:39
const BerkeleyRODatabase & m_database
Definition: migrate.h:78
Path class wrapper to block calls to the fs::path(std::string) implicit constructor and the fs::path:...
Definition: fs.h:32
const fs::path m_filepath
Definition: migrate.h:23
const BerkeleyRODatabase & m_database
Definition: migrate.h:93
An instance of this class represents one database.
Definition: db.h:130