2#pragma clang diagnostic push
3#pragma clang diagnostic ignored "-Wdeprecated"
6#include <xrpld/core/Config.h>
7#include <xrpld/core/DatabaseCon.h>
8#include <xrpld/core/SociDB.h>
10#include <xrpl/basics/ByteUtilities.h>
11#include <xrpl/basics/contract.h>
13#include <boost/filesystem.hpp>
15#include <soci/sqlite3/soci-sqlite3.h>
33 Throw<std::runtime_error>(
34 "Sqlite databases must specify a dir and a name. Name: " + name +
37 boost::filesystem::path file(dir);
38 if (is_directory(file))
46 auto const& section = config.
section(
"sqdb");
47 auto const backendName =
get(section,
"backend",
"sqlite");
49 if (backendName !=
"sqlite")
50 Throw<std::runtime_error>(
"Unsupported soci backend: " + backendName);
52 auto const path = config.
legacy(
"database_path");
54 dbName ==
"validators" || dbName ==
"peerfinder" ?
".sqlite" :
".db";
65 :
DBConfig(detail::getSociInit(config, dbName))
93 if (beName ==
"sqlite")
94 s.open(soci::sqlite3, connectionString);
96 Throw<std::runtime_error>(
"Unsupported soci backend: " + beName);
99static sqlite_api::sqlite3*
102 sqlite_api::sqlite3* result =
nullptr;
103 auto be = s.get_backend();
104 if (
auto b =
dynamic_cast<soci::sqlite3_session_backend*
>(be))
108 Throw<std::logic_error>(
"Didn't get a database connection.");
117 Throw<std::logic_error>(
"No connection found.");
118 return static_cast<size_t>(
119 sqlite_api::sqlite3_memory_used() /
kilobytes(1));
128 int cur = 0, hiw = 0;
129 sqlite_api::sqlite3_db_status(
130 conn, SQLITE_DBSTATUS_CACHE_USED, &cur, &hiw, 0);
133 Throw<std::logic_error>(
"");
140 to.
resize(from.get_len());
143 from.read(0,
reinterpret_cast<char*
>(&to[0]), from.get_len());
158 to.write(0,
reinterpret_cast<char const*
>(&from[0]), from.
size());
167 to.write(0, from.
data(), from.
size());
183class WALCheckpointer :
public Checkpointer
192 , session_(
std::move(session))
194 , j_(logs.journal(
"WALCheckpointer"))
199 sqlite_api::sqlite3_wal_hook(
200 conn, &sqliteWALHook,
reinterpret_cast<void*
>(id_));
207 if (
auto p = session_.lock())
220 ~WALCheckpointer()
override =
default;
233 if (!jobQueue_.addJob(
242 if (auto self = wp.lock())
252 checkpoint()
override
259 int log = 0, ckpt = 0;
260 int ret = sqlite3_wal_checkpoint_v2(
261 conn,
nullptr, SQLITE_CHECKPOINT_PASSIVE, &log, &ckpt);
263 auto fname = sqlite3_db_filename(conn,
"main");
264 if (ret != SQLITE_OK)
266 auto jm = (ret == SQLITE_LOCKED) ? j_.
trace() : j_.
warn();
267 JLOG(jm) <<
"WAL(" << fname <<
"): error " << ret;
271 JLOG(j_.
trace()) <<
"WAL(" << fname <<
"): frames=" <<
log
272 <<
", written=" << ckpt;
288 bool running_ =
false;
294 sqlite_api::sqlite3* conn,
300 if (
auto checkpointer =
303 checkpointer->schedule();
307 sqlite_api::sqlite3_wal_hook(conn,
nullptr,
nullptr);
324 id, std::move(session), queue, logs);
329#if defined(__clang__)
330#pragma clang diagnostic pop
A generic endpoint for log messages.
Stream trace() const
Severity stream access functions.
Holds unparsed configuration information.
Section & section(std::string const &name)
Returns the section with the given name.
void legacy(std::string const §ion, std::string value)
Set a value that is not a key/value pair.
DBConfig is used when a client wants to delay opening a soci::session after parsing the config parame...
std::string connectionString_
DBConfig(std::string const &dbPath)
void open(soci::session &s) const
std::string connectionString() const
A pool of threads to perform work.
Manages partitions for logging.
std::string getSociInit(BasicConfig const &config, std::string const &dbName)
std::string getSociSqliteInit(std::string const &name, std::string const &dir, std::string const &ext)
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
static auto checkpointPageCount
static sqlite_api::sqlite3 * getConnection(soci::session &s)
std::uint32_t getKBUsedAll(soci::session &s)
std::uint32_t getKBUsedDB(soci::session &s)
void convert(soci::blob &from, std::vector< std::uint8_t > &to)
@ open
We haven't closed our ledger yet, but others might have.
constexpr auto kilobytes(T value) noexcept
T get(Section const §ion, std::string const &name, T const &defaultValue=T{})
Retrieve a key/value pair from a section.
std::shared_ptr< Checkpointer > makeCheckpointer(std::uintptr_t id, std::weak_ptr< soci::session > session, JobQueue &queue, Logs &logs)
Returns a new checkpointer which makes checkpoints of a soci database every checkpointPageCount pages...
std::shared_ptr< Checkpointer > checkpointerFromId(std::uintptr_t id)