20 #if defined(__clang__)
21 #pragma clang diagnostic push
22 #pragma clang diagnostic ignored "-Wdeprecated"
25 #include <ripple/basics/ByteUtilities.h>
26 #include <ripple/basics/contract.h>
27 #include <ripple/core/Config.h>
28 #include <ripple/core/ConfigSections.h>
29 #include <ripple/core/DatabaseCon.h>
30 #include <ripple/core/SociDB.h>
31 #include <boost/filesystem.hpp>
33 #include <soci/sqlite3/soci-sqlite3.h>
49 Throw<std::runtime_error>(
50 "Sqlite databases must specify a dir and a name. Name: " + name +
53 boost::filesystem::path file(dir);
54 if (is_directory(file))
62 auto const& section = config.
section(
"sqdb");
63 auto const backendName =
get(section,
"backend",
"sqlite");
65 if (backendName !=
"sqlite")
66 Throw<std::runtime_error>(
"Unsupported soci backend: " + backendName);
68 auto const path = config.
legacy(
"database_path");
70 dbName ==
"validators" || dbName ==
"peerfinder" ?
".sqlite" :
".db";
81 :
DBConfig(detail::getSociInit(config, dbName))
109 if (beName ==
"sqlite")
110 s.open(soci::sqlite3, connectionString);
112 Throw<std::runtime_error>(
"Unsupported soci backend: " + beName);
115 static sqlite_api::sqlite3*
118 sqlite_api::sqlite3* result =
nullptr;
119 auto be = s.get_backend();
120 if (
auto b =
dynamic_cast<soci::sqlite3_session_backend*
>(be))
124 Throw<std::logic_error>(
"Didn't get a database connection.");
133 Throw<std::logic_error>(
"No connection found.");
134 return static_cast<size_t>(
135 sqlite_api::sqlite3_memory_used() /
kilobytes(1));
144 int cur = 0, hiw = 0;
145 sqlite_api::sqlite3_db_status(
146 conn, SQLITE_DBSTATUS_CACHE_USED, &cur, &hiw, 0);
149 Throw<std::logic_error>(
"");
156 to.
resize(from.get_len());
159 from.read(0,
reinterpret_cast<char*
>(&to[0]), from.get_len());
174 to.write(0,
reinterpret_cast<char const*
>(&from[0]), from.
size());
183 to.write(0, from.
data(), from.
size());
199 class WALCheckpointer :
public Checkpointer
208 , session_(
std::move(session))
210 , j_(logs.journal(
"WALCheckpointer"))
215 sqlite_api::sqlite3_wal_hook(
216 conn, &sqliteWALHook,
reinterpret_cast<void*
>(id_));
223 if (
auto p = session_.lock())
236 ~WALCheckpointer()
override =
default;
249 if (!jobQueue_.addJob(
258 if (auto self = wp.lock())
268 checkpoint()
override
275 int log = 0, ckpt = 0;
276 int ret = sqlite3_wal_checkpoint_v2(
277 conn,
nullptr, SQLITE_CHECKPOINT_PASSIVE, &log, &ckpt);
279 auto fname = sqlite3_db_filename(conn,
"main");
280 if (ret != SQLITE_OK)
282 auto jm = (ret == SQLITE_LOCKED) ? j_.
trace() : j_.
warn();
283 JLOG(jm) <<
"WAL(" << fname <<
"): error " << ret;
287 JLOG(j_.
trace()) <<
"WAL(" << fname <<
"): frames=" <<
log
288 <<
", written=" << ckpt;
304 bool running_ =
false;
310 sqlite_api::sqlite3* conn,
316 if (
auto checkpointer =
319 checkpointer->schedule();
323 sqlite_api::sqlite3_wal_hook(conn,
nullptr,
nullptr);
339 return std::make_shared<WALCheckpointer>(
340 id, std::move(session), queue, logs);
345 #if defined(__clang__)
346 #pragma clang diagnostic pop