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