rippled
Loading...
Searching...
No Matches
DatabaseCon.h
1//------------------------------------------------------------------------------
2/*
3 This file is part of rippled: https://github.com/ripple/rippled
4 Copyright (c) 2012, 2013 Ripple Labs Inc.
5
6 Permission to use, copy, modify, and/or distribute this software for any
7 purpose with or without fee is hereby granted, provided that the above
8 copyright notice and this permission notice appear in all copies.
9
10 THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17*/
18//==============================================================================
19
20#ifndef RIPPLE_APP_DATA_DATABASECON_H_INCLUDED
21#define RIPPLE_APP_DATA_DATABASECON_H_INCLUDED
22
23#include <xrpld/app/main/DBInit.h>
24#include <xrpld/core/Config.h>
25#include <xrpld/core/SociDB.h>
26#include <xrpld/perflog/PerfLog.h>
27
28#include <boost/filesystem/path.hpp>
29
30#include <mutex>
31#include <optional>
32#include <string>
33
34namespace soci {
35class session;
36}
37
38namespace ripple {
39
41{
42public:
44
45private:
48
49public:
55 : session_(std::move(rhs.session_)), lock_(std::move(rhs.lock_))
56 {
57 }
61 operator=(LockedSociSession const& rhs) = delete;
62
63 soci::session*
65 {
66 return session_.get();
67 }
68 soci::session&
70 {
71 return *session_;
72 }
73 soci::session*
75 {
76 return session_.get();
77 }
78 explicit
79 operator bool() const
80 {
81 return bool(session_);
82 }
83};
84
86{
87public:
88 struct Setup
89 {
90 explicit Setup() = default;
91
93 bool standAlone = false;
94 boost::filesystem::path dataDir;
95 // Indicates whether or not to return the `globalPragma`
96 // from commonPragma()
97 bool useGlobalPragma = false;
98
101 {
102 XRPL_ASSERT(
104 "ripple::DatabaseCon::Setup::commonPragma : consistent global "
105 "pragma");
107 : nullptr;
108 }
109
113 };
114
120
121 template <std::size_t N, std::size_t M>
123 Setup const& setup,
124 std::string const& dbName,
125 std::array<std::string, N> const& pragma,
126 std::array<char const*, M> const& initSQL,
127 beast::Journal journal)
128 // Use temporary files or regular DB files?
129 : DatabaseCon(
130 setup.standAlone && setup.startUp != Config::LOAD &&
131 setup.startUp != Config::LOAD_FILE &&
132 setup.startUp != Config::REPLAY
133 ? ""
134 : (setup.dataDir / dbName),
135 setup.commonPragma(),
136 pragma,
137 initSQL,
138 journal)
139 {
140 }
141
142 // Use this constructor to setup checkpointing
143 template <std::size_t N, std::size_t M>
145 Setup const& setup,
146 std::string const& dbName,
147 std::array<std::string, N> const& pragma,
148 std::array<char const*, M> const& initSQL,
149 CheckpointerSetup const& checkpointerSetup,
150 beast::Journal journal)
151 : DatabaseCon(setup, dbName, pragma, initSQL, journal)
152 {
153 setupCheckpointing(checkpointerSetup.jobQueue, *checkpointerSetup.logs);
154 }
155
156 template <std::size_t N, std::size_t M>
158 boost::filesystem::path const& dataDir,
159 std::string const& dbName,
160 std::array<std::string, N> const& pragma,
161 std::array<char const*, M> const& initSQL,
162 beast::Journal journal)
163 : DatabaseCon(dataDir / dbName, nullptr, pragma, initSQL, journal)
164 {
165 }
166
167 // Use this constructor to setup checkpointing
168 template <std::size_t N, std::size_t M>
170 boost::filesystem::path const& dataDir,
171 std::string const& dbName,
172 std::array<std::string, N> const& pragma,
173 std::array<char const*, M> const& initSQL,
174 CheckpointerSetup const& checkpointerSetup,
175 beast::Journal journal)
176 : DatabaseCon(dataDir, dbName, pragma, initSQL, journal)
177 {
178 setupCheckpointing(checkpointerSetup.jobQueue, *checkpointerSetup.logs);
179 }
180
181 ~DatabaseCon();
182
183 soci::session&
185 {
186 return *session_;
187 }
188
191 {
192 using namespace std::chrono_literals;
194 [&]() { return LockedSociSession(session_, lock_); },
195 "checkoutDb",
196 10ms,
197 j_);
198
199 return session;
200 }
201
202private:
203 void
205
206 template <std::size_t N, std::size_t M>
208 boost::filesystem::path const& pPath,
209 std::vector<std::string> const* commonPragma,
210 std::array<std::string, N> const& pragma,
211 std::array<char const*, M> const& initSQL,
212 beast::Journal journal)
213 : session_(std::make_shared<soci::session>()), j_(journal)
214 {
215 open(*session_, "sqlite", pPath.string());
216
217 for (auto const& p : pragma)
218 {
219 soci::statement st = session_->prepare << p;
220 st.execute(true);
221 }
222
223 if (commonPragma)
224 {
225 for (auto const& p : *commonPragma)
226 {
227 soci::statement st = session_->prepare << p;
228 st.execute(true);
229 }
230 }
231
232 for (auto const& sql : initSQL)
233 {
234 soci::statement st = session_->prepare << sql;
235 st.execute(true);
236 }
237 }
238
240
241 // checkpointer may outlive the DatabaseCon when the checkpointer jobQueue
242 // callback locks a weak pointer and the DatabaseCon is then destroyed. In
243 // this case, the checkpointer needs to make sure it doesn't use an already
244 // destroyed session. Thus this class keeps a shared_ptr to the session (so
245 // the checkpointer can keep a weak_ptr) and the checkpointer is a
246 // shared_ptr in this class. session_ will never be null.
249
251};
252
253// Return the checkpointer from its id. If the checkpointer no longer exists, an
254// nullptr is returned
257
260 Config const& c,
262
263} // namespace ripple
264
265#endif
A generic endpoint for log messages.
Definition Journal.h:60
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:58
LockedSociSession & operator=(LockedSociSession const &rhs)=delete
std::unique_lock< mutex > lock_
Definition DatabaseCon.h:47
soci::session * get()
Definition DatabaseCon.h:64
LockedSociSession(LockedSociSession &&rhs) noexcept
Definition DatabaseCon.h:54
soci::session & operator*()
Definition DatabaseCon.h:69
LockedSociSession(LockedSociSession const &rhs)=delete
LockedSociSession(std::shared_ptr< soci::session > it, mutex &m)
Definition DatabaseCon.h:50
std::shared_ptr< soci::session > session_
Definition DatabaseCon.h:46
soci::session * operator->()
Definition DatabaseCon.h:74
Manages partitions for logging.
Definition Log.h:52
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:187
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:25
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:94
static std::unique_ptr< std::vector< std::string > const > globalPragma
std::vector< std::string > const * commonPragma() const
std::array< std::string, 1 > lgrPragma
Config::StartUpType startUp
Definition DatabaseCon.h:92
std::array< std::string, 4 > txPragma