Bitcoin Core  26.99.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 
42 {
43 public:
44  virtual ~SQliteExecHandler() {}
45  virtual int Exec(SQLiteDatabase& database, const std::string& statement);
46 };
47 
49 class SQLiteBatch : public DatabaseBatch
50 {
51 private:
53  std::unique_ptr<SQliteExecHandler> m_exec_handler{std::make_unique<SQliteExecHandler>()};
54 
55  sqlite3_stmt* m_read_stmt{nullptr};
56  sqlite3_stmt* m_insert_stmt{nullptr};
57  sqlite3_stmt* m_overwrite_stmt{nullptr};
58  sqlite3_stmt* m_delete_stmt{nullptr};
59  sqlite3_stmt* m_delete_prefix_stmt{nullptr};
60 
71  bool m_txn{false};
72 
73  void SetupSQLStatements();
74  bool ExecStatement(sqlite3_stmt* stmt, Span<const std::byte> blob);
75 
76  bool ReadKey(DataStream&& key, DataStream& value) override;
77  bool WriteKey(DataStream&& key, DataStream&& value, bool overwrite = true) override;
78  bool EraseKey(DataStream&& key) override;
79  bool HasKey(DataStream&& key) override;
81 
82 public:
83  explicit SQLiteBatch(SQLiteDatabase& database);
84  ~SQLiteBatch() override { Close(); }
85 
86  void SetExecHandler(std::unique_ptr<SQliteExecHandler>&& handler) { m_exec_handler = std::move(handler); }
87 
88  /* No-op. See comment on SQLiteDatabase::Flush */
89  void Flush() override {}
90 
91  void Close() override;
92 
93  std::unique_ptr<DatabaseCursor> GetNewCursor() override;
94  std::unique_ptr<DatabaseCursor> GetNewPrefixCursor(Span<const std::byte> prefix) override;
95  bool TxnBegin() override;
96  bool TxnCommit() override;
97  bool TxnAbort() override;
98 };
99 
103 {
104 private:
105  const bool m_mock{false};
106 
107  const std::string m_dir_path;
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.
133 
134  bool Verify(bilingual_str& error);
135 
137  void Open() override;
138 
140  void Close() override;
141 
142  /* These functions are unused */
143  void AddRef() override { assert(false); }
144  void RemoveRef() override { assert(false); }
145 
147  bool Rewrite(const char* skip = nullptr) override;
148 
151  bool Backup(const std::string& dest) const override;
152 
161  void Flush() override {}
162  bool PeriodicFlush() override { return false; }
163  void ReloadDbEnv() override {}
164 
165  void IncrementUpdateCounter() override { ++nUpdateCounter; }
166 
167  std::string Filename() override { return m_file_path; }
168  std::string Format() override { return "sqlite"; }
169 
171  std::unique_ptr<DatabaseBatch> MakeBatch(bool flush_on_close = true) override;
172 
174  bool HasActiveTxn();
175 
176  sqlite3* m_db{nullptr};
178 };
179 
180 std::unique_ptr<SQLiteDatabase> MakeSQLiteDatabase(const fs::path& path, const DatabaseOptions& options, DatabaseStatus& status, bilingual_str& error);
181 
182 std::string SQLiteDatabaseVersion();
183 } // namespace wallet
184 
185 #endif // BITCOIN_WALLET_SQLITE_H
An implementation of a semaphore.
Definition: sync.h:308
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 WalletDatabase.
Definition: db.h:45
RAII class that provides access to a WalletDatabase.
Definition: sqlite.h:50
bool ReadKey(DataStream &&key, DataStream &value) override
Definition: sqlite.cpp:459
bool TxnCommit() override
Definition: sqlite.cpp:668
SQLiteBatch(SQLiteDatabase &database)
Definition: sqlite.cpp:401
std::unique_ptr< SQliteExecHandler > m_exec_handler
Definition: sqlite.h:53
bool HasKey(DataStream &&key) override
Definition: sqlite.cpp:552
bool m_txn
Whether this batch has started a database transaction and whether it owns SQLiteDatabase::m_write_sem...
Definition: sqlite.h:71
std::unique_ptr< DatabaseCursor > GetNewCursor() override
Definition: sqlite.cpp:596
bool ErasePrefix(Span< const std::byte > prefix) override
Definition: sqlite.cpp:547
bool EraseKey(DataStream &&key) override
Definition: sqlite.cpp:542
sqlite3_stmt * m_delete_stmt
Definition: sqlite.h:58
bool TxnBegin() override
Definition: sqlite.cpp:653
~SQLiteBatch() override
Definition: sqlite.h:84
sqlite3_stmt * m_read_stmt
Definition: sqlite.h:55
void Close() override
Definition: sqlite.cpp:410
sqlite3_stmt * m_overwrite_stmt
Definition: sqlite.h:57
bool ExecStatement(sqlite3_stmt *stmt, Span< const std::byte > blob)
Definition: sqlite.cpp:518
std::unique_ptr< DatabaseCursor > GetNewPrefixCursor(Span< const std::byte > prefix) override
Definition: sqlite.cpp:611
void Flush() override
Definition: sqlite.h:89
void SetExecHandler(std::unique_ptr< SQliteExecHandler > &&handler)
Definition: sqlite.h:86
sqlite3_stmt * m_delete_prefix_stmt
Definition: sqlite.h:59
void SetupSQLStatements()
Definition: sqlite.cpp:151
sqlite3_stmt * m_insert_stmt
Definition: sqlite.h:56
SQLiteDatabase & m_database
Definition: sqlite.h:52
bool TxnAbort() override
Definition: sqlite.cpp:682
bool WriteKey(DataStream &&key, DataStream &&value, bool overwrite=true) override
Definition: sqlite.cpp:485
RAII class that provides a database cursor.
Definition: sqlite.h:21
std::vector< std::byte > m_prefix_range_start
Definition: sqlite.h:26
~SQLiteCursor() override
Definition: sqlite.cpp:585
Status Next(DataStream &key, DataStream &value) override
Definition: sqlite.cpp:565
sqlite3_stmt * m_cursor_stmt
Definition: sqlite.h:23
SQLiteCursor(std::vector< std::byte > start_range, std::vector< std::byte > end_range)
Definition: sqlite.h:30
std::vector< std::byte > m_prefix_range_end
Definition: sqlite.h:27
An instance of this class represents one SQLite3 database.
Definition: sqlite.h:103
static Mutex g_sqlite_mutex
This mutex protects SQLite initialization and shutdown.
Definition: sqlite.h:117
void Flush() override
No-ops.
Definition: sqlite.h:161
std::string Format() override
Definition: sqlite.h:168
CSemaphore m_write_semaphore
Definition: sqlite.h:132
static int g_sqlite_count GUARDED_BY(g_sqlite_mutex)
bool PeriodicFlush() override
Definition: sqlite.h:162
void IncrementUpdateCounter() override
Definition: sqlite.h:165
void Open() override
Open the database if it is not already opened.
Definition: sqlite.cpp:249
std::string Filename() override
Return path to main database file for logs and error messages.
Definition: sqlite.h:167
bool Rewrite(const char *skip=nullptr) override
Rewrite the entire database on disk.
Definition: sqlite.cpp:341
void Cleanup() noexcept EXCLUSIVE_LOCKS_REQUIRED(!g_sqlite_mutex)
Definition: sqlite.cpp:177
const bool m_mock
Definition: sqlite.h:105
void Close() override
Close the database.
Definition: sqlite.cpp:375
void ReloadDbEnv() override
Definition: sqlite.h:163
bool Backup(const std::string &dest) const override
Back up the entire database to a file.
Definition: sqlite.cpp:348
const std::string m_dir_path
Definition: sqlite.h:107
void AddRef() override
Indicate the a new database user has began using the database.
Definition: sqlite.h:143
const std::string m_file_path
Definition: sqlite.h:109
std::unique_ptr< DatabaseBatch > MakeBatch(bool flush_on_close=true) override
Make a SQLiteBatch connected to this database.
Definition: sqlite.cpp:395
bool Verify(bilingual_str &error)
Definition: sqlite.cpp:192
void RemoveRef() override
Indicate that database user has stopped using the database and that it could be flushed or closed.
Definition: sqlite.h:144
bool HasActiveTxn()
Return true if there is an on-going txn in this connection.
Definition: sqlite.cpp:384
Class responsible for executing SQL statements in SQLite databases.
Definition: sqlite.h:42
virtual int Exec(SQLiteDatabase &database, const std::string &statement)
Definition: sqlite.cpp:390
virtual ~SQliteExecHandler()
Definition: sqlite.h:44
An instance of this class represents one database.
Definition: db.h:124
std::atomic< unsigned int > nUpdateCounter
Definition: db.h:168
bool error(const char *fmt, const Args &... args)
Definition: logging.h:267
Filesystem operations and types.
std::unique_ptr< SQLiteDatabase > MakeSQLiteDatabase(const fs::path &path, const DatabaseOptions &options, DatabaseStatus &status, bilingual_str &error)
Definition: sqlite.cpp:696
std::string SQLiteDatabaseVersion()
Definition: sqlite.cpp:714
DatabaseStatus
Definition: db.h:196
const char * prefix
Definition: rest.cpp:1005
bool(* handler)(const std::any &context, HTTPRequest *req, const std::string &strReq)
Definition: rest.cpp:1006
Bilingual messages:
Definition: translation.h:18
#define EXCLUSIVE_LOCKS_REQUIRED(...)
Definition: threadsafety.h:49
assert(!tx.IsCoinBase())