rippled
Loading...
Searching...
No Matches
DatabaseCon.h
1#ifndef XRPL_APP_DATA_DATABASECON_H_INCLUDED
2#define XRPL_APP_DATA_DATABASECON_H_INCLUDED
3
4#include <xrpld/app/main/DBInit.h>
5#include <xrpld/core/Config.h>
6#include <xrpld/core/SociDB.h>
7#include <xrpld/perflog/PerfLog.h>
8
9#include <boost/filesystem/path.hpp>
10
11#include <mutex>
12#include <optional>
13#include <string>
14
15namespace soci {
16class session;
17}
18
19namespace ripple {
20
22{
23public:
25
26private:
29
30public:
36 : session_(std::move(rhs.session_)), lock_(std::move(rhs.lock_))
37 {
38 }
42 operator=(LockedSociSession const& rhs) = delete;
43
44 soci::session*
46 {
47 return session_.get();
48 }
49 soci::session&
51 {
52 return *session_;
53 }
54 soci::session*
56 {
57 return session_.get();
58 }
59 explicit
60 operator bool() const
61 {
62 return bool(session_);
63 }
64};
65
67{
68public:
69 struct Setup
70 {
71 explicit Setup() = default;
72
74 bool standAlone = false;
75 boost::filesystem::path dataDir;
76 // Indicates whether or not to return the `globalPragma`
77 // from commonPragma()
78 bool useGlobalPragma = false;
79
82 {
83 XRPL_ASSERT(
85 "ripple::DatabaseCon::Setup::commonPragma : consistent global "
86 "pragma");
88 : nullptr;
89 }
90
94 };
95
101
102 template <std::size_t N, std::size_t M>
104 Setup const& setup,
105 std::string const& dbName,
106 std::array<std::string, N> const& pragma,
107 std::array<char const*, M> const& initSQL,
108 beast::Journal journal)
109 // Use temporary files or regular DB files?
110 : DatabaseCon(
111 setup.standAlone && setup.startUp != Config::LOAD &&
112 setup.startUp != Config::LOAD_FILE &&
113 setup.startUp != Config::REPLAY
114 ? ""
115 : (setup.dataDir / dbName),
116 setup.commonPragma(),
117 pragma,
118 initSQL,
119 journal)
120 {
121 }
122
123 // Use this constructor to setup checkpointing
124 template <std::size_t N, std::size_t M>
126 Setup const& setup,
127 std::string const& dbName,
128 std::array<std::string, N> const& pragma,
129 std::array<char const*, M> const& initSQL,
130 CheckpointerSetup const& checkpointerSetup,
131 beast::Journal journal)
132 : DatabaseCon(setup, dbName, pragma, initSQL, journal)
133 {
134 setupCheckpointing(checkpointerSetup.jobQueue, *checkpointerSetup.logs);
135 }
136
137 template <std::size_t N, std::size_t M>
139 boost::filesystem::path const& dataDir,
140 std::string const& dbName,
141 std::array<std::string, N> const& pragma,
142 std::array<char const*, M> const& initSQL,
143 beast::Journal journal)
144 : DatabaseCon(dataDir / dbName, nullptr, pragma, initSQL, journal)
145 {
146 }
147
148 // Use this constructor to setup checkpointing
149 template <std::size_t N, std::size_t M>
151 boost::filesystem::path const& dataDir,
152 std::string const& dbName,
153 std::array<std::string, N> const& pragma,
154 std::array<char const*, M> const& initSQL,
155 CheckpointerSetup const& checkpointerSetup,
156 beast::Journal journal)
157 : DatabaseCon(dataDir, dbName, pragma, initSQL, journal)
158 {
159 setupCheckpointing(checkpointerSetup.jobQueue, *checkpointerSetup.logs);
160 }
161
162 ~DatabaseCon();
163
164 soci::session&
166 {
167 return *session_;
168 }
169
172 {
173 using namespace std::chrono_literals;
175 [&]() { return LockedSociSession(session_, lock_); },
176 "checkoutDb",
177 10ms,
178 j_);
179
180 return session;
181 }
182
183private:
184 void
186
187 template <std::size_t N, std::size_t M>
189 boost::filesystem::path const& pPath,
190 std::vector<std::string> const* commonPragma,
191 std::array<std::string, N> const& pragma,
192 std::array<char const*, M> const& initSQL,
193 beast::Journal journal)
194 : session_(std::make_shared<soci::session>()), j_(journal)
195 {
196 open(*session_, "sqlite", pPath.string());
197
198 for (auto const& p : pragma)
199 {
200 soci::statement st = session_->prepare << p;
201 st.execute(true);
202 }
203
204 if (commonPragma)
205 {
206 for (auto const& p : *commonPragma)
207 {
208 soci::statement st = session_->prepare << p;
209 st.execute(true);
210 }
211 }
212
213 for (auto const& sql : initSQL)
214 {
215 soci::statement st = session_->prepare << sql;
216 st.execute(true);
217 }
218 }
219
221
222 // checkpointer may outlive the DatabaseCon when the checkpointer jobQueue
223 // callback locks a weak pointer and the DatabaseCon is then destroyed. In
224 // this case, the checkpointer needs to make sure it doesn't use an already
225 // destroyed session. Thus this class keeps a shared_ptr to the session (so
226 // the checkpointer can keep a weak_ptr) and the checkpointer is a
227 // shared_ptr in this class. session_ will never be null.
230
232};
233
234// Return the checkpointer from its id. If the checkpointer no longer exists, an
235// nullptr is returned
238
241 Config const& c,
243
244} // namespace ripple
245
246#endif
A generic endpoint for log messages.
Definition Journal.h:41
void setupCheckpointing(JobQueue *, Logs &)
beast::Journal const j_
LockedSociSession checkoutDb()
DatabaseCon(boost::filesystem::path const &dataDir, std::string const &dbName, std::array< std::string, N > const &pragma, std::array< char const *, M > const &initSQL, CheckpointerSetup const &checkpointerSetup, beast::Journal journal)
DatabaseCon(Setup const &setup, std::string const &dbName, std::array< std::string, N > const &pragma, std::array< char const *, M > const &initSQL, CheckpointerSetup const &checkpointerSetup, beast::Journal journal)
LockedSociSession::mutex lock_
DatabaseCon(Setup const &setup, std::string const &dbName, std::array< std::string, N > const &pragma, std::array< char const *, M > const &initSQL, beast::Journal journal)
std::shared_ptr< soci::session > const session_
DatabaseCon(boost::filesystem::path const &pPath, std::vector< std::string > const *commonPragma, std::array< std::string, N > const &pragma, std::array< char const *, M > const &initSQL, beast::Journal journal)
std::shared_ptr< Checkpointer > checkpointer_
soci::session & getSession()
DatabaseCon(boost::filesystem::path const &dataDir, std::string const &dbName, std::array< std::string, N > const &pragma, std::array< char const *, M > const &initSQL, beast::Journal journal)
A pool of threads to perform work.
Definition JobQueue.h:39
LockedSociSession & operator=(LockedSociSession const &rhs)=delete
std::unique_lock< mutex > lock_
Definition DatabaseCon.h:28
soci::session * get()
Definition DatabaseCon.h:45
LockedSociSession(LockedSociSession &&rhs) noexcept
Definition DatabaseCon.h:35
soci::session & operator*()
Definition DatabaseCon.h:50
LockedSociSession(LockedSociSession const &rhs)=delete
LockedSociSession(std::shared_ptr< soci::session > it, mutex &m)
Definition DatabaseCon.h:31
std::shared_ptr< soci::session > session_
Definition DatabaseCon.h:27
soci::session * operator->()
Definition DatabaseCon.h:55
Manages partitions for logging.
Definition Log.h:33
T get(T... args)
T is_same_v
auto measureDurationAndLog(Func &&func, std::string const &actionDescription, std::chrono::duration< Rep, Period > maxDelay, beast::Journal const &journal)
Definition PerfLog.h:168
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:6
DatabaseCon::Setup setup_DatabaseCon(Config const &c, std::optional< beast::Journal > j=std::nullopt)
@ open
We haven't closed our ledger yet, but others might have.
std::shared_ptr< Checkpointer > checkpointerFromId(std::uintptr_t id)
STL namespace.
boost::filesystem::path dataDir
Definition DatabaseCon.h:75
static std::unique_ptr< std::vector< std::string > const > globalPragma
Definition DatabaseCon.h:91
std::vector< std::string > const * commonPragma() const
Definition DatabaseCon.h:81
std::array< std::string, 1 > lgrPragma
Definition DatabaseCon.h:93
Config::StartUpType startUp
Definition DatabaseCon.h:73
std::array< std::string, 4 > txPragma
Definition DatabaseCon.h:92