Bitcoin Core  26.1.0
P2P Digital Currency
sqlite.h
Go to the documentation of this file.
1 // Copyright (c) 2020-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_SQLITE_H
6 #define BITCOIN_WALLET_SQLITE_H
7 
8 #include <sync.h>
9 #include <wallet/db.h>
10 
11 struct bilingual_str;
12 
13 struct sqlite3_stmt;
14 struct sqlite3;
15 
16 namespace wallet {
17 class SQLiteDatabase;
18 
21 {
22 public:
23  sqlite3_stmt* m_cursor_stmt{nullptr};
24  // Copies of the prefix things for the prefix cursor.
25  // Prevents SQLite from accessing temp variables for the prefix things.
26  std::vector<std::byte> m_prefix_range_start;
27  std::vector<std::byte> m_prefix_range_end;
28 
29  explicit SQLiteCursor() {}
30  explicit SQLiteCursor(std::vector<std::byte> start_range, std::vector<std::byte> end_range)
31  : m_prefix_range_start(std::move(start_range)),
32  m_prefix_range_end(std::move(end_range))
33  {}
34  ~SQLiteCursor() override;
35 
36  Status Next(DataStream& key, DataStream& value) override;
37 };
38 
40 class SQLiteBatch : public DatabaseBatch
41 {
42 private:
44 
45  sqlite3_stmt* m_read_stmt{nullptr};
46  sqlite3_stmt* m_insert_stmt{nullptr};
47  sqlite3_stmt* m_overwrite_stmt{nullptr};
48  sqlite3_stmt* m_delete_stmt{nullptr};
49  sqlite3_stmt* m_delete_prefix_stmt{nullptr};
50 
51  void SetupSQLStatements();
52  bool ExecStatement(sqlite3_stmt* stmt, Span<const std::byte> blob);
53 
54  bool ReadKey(DataStream&& key, DataStream& value) override;
55  bool WriteKey(DataStream&& key, DataStream&& value, bool overwrite = true) override;
56  bool EraseKey(DataStream&& key) override;
57  bool HasKey(DataStream&& key) override;
59 
60 public:
61  explicit SQLiteBatch(SQLiteDatabase& database);
62  ~SQLiteBatch() override { Close(); }
63 
64  /* No-op. See comment on SQLiteDatabase::Flush */
65  void Flush() override {}
66 
67  void Close() override;
68 
69  std::unique_ptr<DatabaseCursor> GetNewCursor() override;
70  std::unique_ptr<DatabaseCursor> GetNewPrefixCursor(Span<const std::byte> prefix) override;
71  bool TxnBegin() override;
72  bool TxnCommit() override;
73  bool TxnAbort() override;
74 };
75 
79 {
80 private:
81  const bool m_mock{false};
82 
83  const std::string m_dir_path;
84 
85  const std::string m_file_path;
86 
94  static int g_sqlite_count GUARDED_BY(g_sqlite_mutex);
95 
97 
98 public:
99  SQLiteDatabase() = delete;
100 
102  SQLiteDatabase(const fs::path& dir_path, const fs::path& file_path, const DatabaseOptions& options, bool mock = false);
103 
104  ~SQLiteDatabase();
105 
106  bool Verify(bilingual_str& error);
107 
109  void Open() override;
110 
112  void Close() override;
113 
114  /* These functions are unused */
115  void AddRef() override { assert(false); }
116  void RemoveRef() override { assert(false); }
117 
119  bool Rewrite(const char* skip = nullptr) override;
120 
123  bool Backup(const std::string& dest) const override;
124 
133  void Flush() override {}
134  bool PeriodicFlush() override { return false; }
135  void ReloadDbEnv() override {}
136 
137  void IncrementUpdateCounter() override { ++nUpdateCounter; }
138 
139  std::string Filename() override { return m_file_path; }
140  std::string Format() override { return "sqlite"; }
141 
143  std::unique_ptr<DatabaseBatch> MakeBatch(bool flush_on_close = true) override;
144 
145  sqlite3* m_db{nullptr};
147 };
148 
149 std::unique_ptr<SQLiteDatabase> MakeSQLiteDatabase(const fs::path& path, const DatabaseOptions& options, DatabaseStatus& status, bilingual_str& error);
150 
151 std::string SQLiteDatabaseVersion();
152 } // namespace wallet
153 
154 #endif // BITCOIN_WALLET_SQLITE_H
assert(!tx.IsCoinBase())
void Cleanup() noexcept EXCLUSIVE_LOCKS_REQUIRED(!g_sqlite_mutex)
Definition: sqlite.cpp:173
Bilingual messages:
Definition: translation.h:18
static Mutex g_sqlite_mutex
This mutex protects SQLite initialization and shutdown.
Definition: sqlite.h:93
std::unique_ptr< SQLiteDatabase > MakeSQLiteDatabase(const fs::path &path, const DatabaseOptions &options, DatabaseStatus &status, bilingual_str &error)
Definition: sqlite.cpp:637
void IncrementUpdateCounter() override
Definition: sqlite.h:137
bool TxnCommit() override
Definition: sqlite.cpp:617
const char * prefix
Definition: rest.cpp:1004
const std::string m_file_path
Definition: sqlite.h:85
void SetupSQLStatements()
Definition: sqlite.cpp:147
~SQLiteBatch() override
Definition: sqlite.h:62
SQLiteDatabase & m_database
Definition: sqlite.h:43
bool HasKey(DataStream &&key) override
Definition: sqlite.cpp:506
std::string SQLiteDatabaseVersion()
Definition: sqlite.cpp:655
sqlite3_stmt * m_overwrite_stmt
Definition: sqlite.h:47
std::string Format() override
Definition: sqlite.h:140
sqlite3_stmt * m_insert_stmt
Definition: sqlite.h:46
std::atomic< unsigned int > nUpdateCounter
Definition: db.h:169
std::unique_ptr< DatabaseCursor > GetNewCursor() override
Definition: sqlite.cpp:550
RAII class that provides access to a WalletDatabase.
Definition: db.h:45
Filesystem operations and types.
bool Verify(bilingual_str &error)
Definition: sqlite.cpp:188
static int g_sqlite_count GUARDED_BY(g_sqlite_mutex)
const bool m_mock
Definition: sqlite.h:81
bool Rewrite(const char *skip=nullptr) override
Rewrite the entire database on disk.
Definition: sqlite.cpp:337
bool TxnAbort() override
Definition: sqlite.cpp:627
std::vector< std::byte > m_prefix_range_end
Definition: sqlite.h:27
Double ended buffer combining vector and stream-like interfaces.
Definition: streams.h:192
void Flush() override
Definition: sqlite.h:65
void Open() override
Open the database if it is not already opened.
Definition: sqlite.cpp:245
bool WriteKey(DataStream &&key, DataStream &&value, bool overwrite=true) override
Definition: sqlite.cpp:451
std::unique_ptr< DatabaseCursor > GetNewPrefixCursor(Span< const std::byte > prefix) override
Definition: sqlite.cpp:565
bool EraseKey(DataStream &&key) override
Definition: sqlite.cpp:496
void Close() override
Definition: sqlite.cpp:395
std::vector< std::byte > m_prefix_range_start
Definition: sqlite.h:26
std::unique_ptr< DatabaseBatch > MakeBatch(bool flush_on_close=true) override
Make a SQLiteBatch connected to this database.
Definition: sqlite.cpp:380
SQLiteBatch(SQLiteDatabase &database)
Definition: sqlite.cpp:386
Status Next(DataStream &key, DataStream &value) override
Definition: sqlite.cpp:519
RAII class that provides a database cursor.
Definition: sqlite.h:20
DatabaseStatus
Definition: db.h:197
~SQLiteCursor() override
Definition: sqlite.cpp:539
#define EXCLUSIVE_LOCKS_REQUIRED(...)
Definition: threadsafety.h:49
const std::string m_dir_path
Definition: sqlite.h:83
std::string Filename() override
Return path to main database file for logs and error messages.
Definition: sqlite.h:139
sqlite3_stmt * m_cursor_stmt
Definition: sqlite.h:23
bool ReadKey(DataStream &&key, DataStream &value) override
Definition: sqlite.cpp:425
bool error(const char *fmt, const Args &... args)
Definition: logging.h:262
SQLiteCursor(std::vector< std::byte > start_range, std::vector< std::byte > end_range)
Definition: sqlite.h:30
bool TxnBegin() override
Definition: sqlite.cpp:607
bool ExecStatement(sqlite3_stmt *stmt, Span< const std::byte > blob)
Definition: sqlite.cpp:478
void AddRef() override
Indicate the a new database user has began using the database.
Definition: sqlite.h:115
bool ErasePrefix(Span< const std::byte > prefix) override
Definition: sqlite.cpp:501
An instance of this class represents one SQLite3 database.
Definition: sqlite.h:78
RAII class that provides access to a WalletDatabase.
Definition: sqlite.h:40
sqlite3_stmt * m_delete_stmt
Definition: sqlite.h:48
void RemoveRef() override
Indicate that database user has stopped using the database and that it could be flushed or closed...
Definition: sqlite.h:116
sqlite3_stmt * m_delete_prefix_stmt
Definition: sqlite.h:49
void Flush() override
No-ops.
Definition: sqlite.h:133
Path class wrapper to block calls to the fs::path(std::string) implicit constructor and the fs::path:...
Definition: fs.h:30
bool Backup(const std::string &dest) const override
Back up the entire database to a file.
Definition: sqlite.cpp:344
void ReloadDbEnv() override
Definition: sqlite.h:135
An instance of this class represents one database.
Definition: db.h:124
sqlite3_stmt * m_read_stmt
Definition: sqlite.h:45
void Close() override
Close the database.
Definition: sqlite.cpp:371
bool PeriodicFlush() override
Definition: sqlite.h:134