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