Bitcoin Core  31.0.0
P2P Digital Currency
sqlite.h
Go to the documentation of this file.
1 // Copyright (c) 2020-present 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 #include <semaphore>
12 
13 struct bilingual_str;
14 
15 struct sqlite3_stmt;
16 struct sqlite3;
17 
18 namespace wallet {
19 class SQLiteDatabase;
20 
23 {
24 public:
25  sqlite3_stmt* m_cursor_stmt{nullptr};
26  // Copies of the prefix things for the prefix cursor.
27  // Prevents SQLite from accessing temp variables for the prefix things.
28  std::vector<std::byte> m_prefix_range_start;
29  std::vector<std::byte> m_prefix_range_end;
30 
31  explicit SQLiteCursor() = default;
32  explicit SQLiteCursor(std::vector<std::byte> start_range, std::vector<std::byte> end_range)
33  : m_prefix_range_start(std::move(start_range)),
34  m_prefix_range_end(std::move(end_range))
35  {}
36  ~SQLiteCursor() override;
37 
38  Status Next(DataStream& key, DataStream& value) override;
39 };
40 
44 {
45 public:
46  virtual ~SQliteExecHandler() = default;
47  virtual int Exec(SQLiteDatabase& database, const std::string& statement);
48 };
49 
51 class SQLiteBatch : public DatabaseBatch
52 {
53 private:
55  std::unique_ptr<SQliteExecHandler> m_exec_handler{std::make_unique<SQliteExecHandler>()};
56 
57  sqlite3_stmt* m_read_stmt{nullptr};
58  sqlite3_stmt* m_insert_stmt{nullptr};
59  sqlite3_stmt* m_overwrite_stmt{nullptr};
60  sqlite3_stmt* m_delete_stmt{nullptr};
61  sqlite3_stmt* m_delete_prefix_stmt{nullptr};
62 
73  bool m_txn{false};
74 
75  void SetupSQLStatements();
76  bool ExecStatement(sqlite3_stmt* stmt, std::span<const std::byte> blob);
77 
78  bool ReadKey(DataStream&& key, DataStream& value) override;
79  bool WriteKey(DataStream&& key, DataStream&& value, bool overwrite = true) override;
80  bool EraseKey(DataStream&& key) override;
81  bool HasKey(DataStream&& key) override;
82  bool ErasePrefix(std::span<const std::byte> prefix) override;
83 
84 public:
85  explicit SQLiteBatch(SQLiteDatabase& database);
86  ~SQLiteBatch() override { Close(); }
87 
88  void SetExecHandler(std::unique_ptr<SQliteExecHandler>&& handler) { m_exec_handler = std::move(handler); }
89 
90  void Close() override;
91 
92  std::unique_ptr<DatabaseCursor> GetNewCursor() override;
93  std::unique_ptr<DatabaseCursor> GetNewPrefixCursor(std::span<const std::byte> prefix) override;
94  bool TxnBegin() override;
95  bool TxnCommit() override;
96  bool TxnAbort() override;
97  bool HasActiveTxn() override { return m_txn; }
98 };
99 
103 {
104 private:
105  const bool m_mock{false};
106 
108 
109  const std::string m_file_path;
110 
118  static int g_sqlite_count GUARDED_BY(g_sqlite_mutex);
119 
121 
122 public:
123  SQLiteDatabase() = delete;
124 
126  SQLiteDatabase(const fs::path& dir_path, const fs::path& file_path, const DatabaseOptions& options, bool mock = false);
127 
128  ~SQLiteDatabase();
129 
130  // Batches must acquire this semaphore on writing, and release when done writing.
131  // This ensures that only one batch is modifying the database at a time.
132  std::binary_semaphore m_write_semaphore;
133 
134  bool Verify(bilingual_str& error);
135 
137  void Open() override;
138 
140  void Close() override;
141 
143  bool Rewrite() override;
144 
147  bool Backup(const std::string& dest) const override;
148 
149  std::string Filename() override { return m_file_path; }
151  std::vector<fs::path> Files() override
152  {
153  std::vector<fs::path> files;
154  files.emplace_back(m_dir_path / fs::PathFromString(m_file_path));
155  files.emplace_back(m_dir_path / fs::PathFromString(m_file_path + "-journal"));
156  return files;
157  }
158  std::string Format() override { return "sqlite"; }
159 
161  std::unique_ptr<DatabaseBatch> MakeBatch() override;
162 
164  bool HasActiveTxn();
165 
166  sqlite3* m_db{nullptr};
168 };
169 
170 std::unique_ptr<SQLiteDatabase> MakeSQLiteDatabase(const fs::path& path, const DatabaseOptions& options, DatabaseStatus& status, bilingual_str& error);
171 
172 std::string SQLiteDatabaseVersion();
173 } // namespace wallet
174 
175 #endif // BITCOIN_WALLET_SQLITE_H
void Cleanup() noexcept EXCLUSIVE_LOCKS_REQUIRED(!g_sqlite_mutex)
Definition: sqlite.cpp:172
Bilingual messages:
Definition: translation.h:24
static Mutex g_sqlite_mutex
This mutex protects SQLite initialization and shutdown.
Definition: sqlite.h:117
std::unique_ptr< SQLiteDatabase > MakeSQLiteDatabase(const fs::path &path, const DatabaseOptions &options, DatabaseStatus &status, bilingual_str &error)
Definition: sqlite.cpp:691
void SetExecHandler(std::unique_ptr< SQliteExecHandler > &&handler)
Definition: sqlite.h:88
bool TxnCommit() override
Definition: sqlite.cpp:663
const char * prefix
Definition: rest.cpp:1141
const std::string m_file_path
Definition: sqlite.h:109
void SetupSQLStatements()
Definition: sqlite.cpp:146
bool m_txn
Whether this batch has started a database transaction and whether it owns SQLiteDatabase::m_write_sem...
Definition: sqlite.h:73
~SQLiteBatch() override
Definition: sqlite.h:86
SQLiteDatabase & m_database
Definition: sqlite.h:54
bool HasKey(DataStream &&key) override
Definition: sqlite.cpp:547
std::unique_ptr< DatabaseBatch > MakeBatch() override
Make a SQLiteBatch connected to this database.
Definition: sqlite.cpp:390
std::string SQLiteDatabaseVersion()
Definition: sqlite.cpp:709
Definition: common.h:29
sqlite3_stmt * m_overwrite_stmt
Definition: sqlite.h:59
std::string Format() override
Definition: sqlite.h:158
sqlite3_stmt * m_insert_stmt
Definition: sqlite.h:58
bool(* handler)(const std::any &context, HTTPRequest *req, const std::string &strReq)
Definition: rest.cpp:1142
std::unique_ptr< DatabaseCursor > GetNewCursor() override
Definition: sqlite.cpp:591
RAII class that provides access to a WalletDatabase.
Definition: db.h:50
virtual int Exec(SQLiteDatabase &database, const std::string &statement)
Definition: sqlite.cpp:385
std::unique_ptr< SQliteExecHandler > m_exec_handler
Definition: sqlite.h:55
bool Verify(bilingual_str &error)
Definition: sqlite.cpp:187
static int g_sqlite_count GUARDED_BY(g_sqlite_mutex)
std::binary_semaphore m_write_semaphore
Definition: sqlite.h:132
const bool m_mock
Definition: sqlite.h:105
bool ErasePrefix(std::span< const std::byte > prefix) override
Definition: sqlite.cpp:542
bool TxnAbort() override
Definition: sqlite.cpp:677
std::vector< std::byte > m_prefix_range_end
Definition: sqlite.h:29
Double ended buffer combining vector and stream-like interfaces.
Definition: streams.h:132
void Open() override
Open the database if it is not already opened.
Definition: sqlite.cpp:244
bool HasActiveTxn() override
Definition: sqlite.h:97
bool WriteKey(DataStream &&key, DataStream &&value, bool overwrite=true) override
Definition: sqlite.cpp:480
bool Rewrite() override
Rewrite the entire database on disk.
Definition: sqlite.cpp:336
bool HasActiveTxn()
Return true if there is an on-going txn in this connection.
Definition: sqlite.cpp:379
bool EraseKey(DataStream &&key) override
Definition: sqlite.cpp:537
bool ExecStatement(sqlite3_stmt *stmt, std::span< const std::byte > blob)
Definition: sqlite.cpp:513
void Close() override
Definition: sqlite.cpp:405
std::vector< std::byte > m_prefix_range_start
Definition: sqlite.h:28
SQLiteBatch(SQLiteDatabase &database)
Definition: sqlite.cpp:396
Status Next(DataStream &key, DataStream &value) override
Definition: sqlite.cpp:560
RAII class that provides a database cursor.
Definition: sqlite.h:22
DatabaseStatus
Definition: db.h:186
~SQLiteCursor() override
Definition: sqlite.cpp:580
#define EXCLUSIVE_LOCKS_REQUIRED(...)
Definition: threadsafety.h:51
std::unique_ptr< DatabaseCursor > GetNewPrefixCursor(std::span< const std::byte > prefix) override
Definition: sqlite.cpp:606
std::string Filename() override
Return path to main database file for logs and error messages.
Definition: sqlite.h:149
sqlite3_stmt * m_cursor_stmt
Definition: sqlite.h:25
bool ReadKey(DataStream &&key, DataStream &value) override
Definition: sqlite.cpp:454
SQLiteCursor(std::vector< std::byte > start_range, std::vector< std::byte > end_range)
Definition: sqlite.h:32
Class responsible for executing SQL statements in SQLite databases.
Definition: sqlite.h:43
bool TxnBegin() override
Definition: sqlite.cpp:648
virtual ~SQliteExecHandler()=default
An instance of this class represents one SQLite3 database.
Definition: sqlite.h:102
RAII class that provides access to a WalletDatabase.
Definition: sqlite.h:51
const fs::path m_dir_path
Definition: sqlite.h:107
sqlite3_stmt * m_delete_stmt
Definition: sqlite.h:60
static path PathFromString(const std::string &string)
Convert byte string to path object.
Definition: fs.h:180
sqlite3_stmt * m_delete_prefix_stmt
Definition: sqlite.h:61
Path class wrapper to block calls to the fs::path(std::string) implicit constructor and the fs::path:...
Definition: fs.h:33
bool Backup(const std::string &dest) const override
Back up the entire database to a file.
Definition: sqlite.cpp:343
An instance of this class represents one database.
Definition: db.h:129
sqlite3_stmt * m_read_stmt
Definition: sqlite.h:57
void Close() override
Close the database.
Definition: sqlite.cpp:370
std::vector< fs::path > Files() override
Return paths to all database created files.
Definition: sqlite.h:151