21#pragma clang diagnostic push
22#pragma clang diagnostic ignored "-Wdeprecated"
25#include <xrpld/core/Config.h>
26#include <xrpld/core/DatabaseCon.h>
27#include <xrpld/core/SociDB.h>
29#include <xrpl/basics/ByteUtilities.h>
30#include <xrpl/basics/contract.h>
32#include <boost/filesystem.hpp>
34#include <soci/sqlite3/soci-sqlite3.h>
52 Throw<std::runtime_error>(
53 "Sqlite databases must specify a dir and a name. Name: " + name +
56 boost::filesystem::path file(dir);
57 if (is_directory(file))
65 auto const& section = config.
section(
"sqdb");
66 auto const backendName =
get(section,
"backend",
"sqlite");
68 if (backendName !=
"sqlite")
69 Throw<std::runtime_error>(
"Unsupported soci backend: " + backendName);
71 auto const path = config.
legacy(
"database_path");
73 dbName ==
"validators" || dbName ==
"peerfinder" ?
".sqlite" :
".db";
84 :
DBConfig(detail::getSociInit(config, dbName))
112 if (beName ==
"sqlite")
113 s.open(soci::sqlite3, connectionString);
115 Throw<std::runtime_error>(
"Unsupported soci backend: " + beName);
118static sqlite_api::sqlite3*
121 sqlite_api::sqlite3* result =
nullptr;
122 auto be = s.get_backend();
123 if (
auto b =
dynamic_cast<soci::sqlite3_session_backend*
>(be))
127 Throw<std::logic_error>(
"Didn't get a database connection.");
136 Throw<std::logic_error>(
"No connection found.");
137 return static_cast<size_t>(
138 sqlite_api::sqlite3_memory_used() /
kilobytes(1));
147 int cur = 0, hiw = 0;
148 sqlite_api::sqlite3_db_status(
149 conn, SQLITE_DBSTATUS_CACHE_USED, &cur, &hiw, 0);
152 Throw<std::logic_error>(
"");
159 to.
resize(from.get_len());
162 from.read(0,
reinterpret_cast<char*
>(&to[0]), from.get_len());
177 to.write(0,
reinterpret_cast<char const*
>(&from[0]), from.
size());
186 to.write(0, from.
data(), from.
size());
202class WALCheckpointer :
public Checkpointer
211 , session_(
std::move(session))
213 , j_(logs.journal(
"WALCheckpointer"))
218 sqlite_api::sqlite3_wal_hook(
219 conn, &sqliteWALHook,
reinterpret_cast<void*
>(id_));
226 if (
auto p = session_.lock())
239 ~WALCheckpointer()
override =
default;
252 if (!jobQueue_.addJob(
261 if (auto self = wp.lock())
271 checkpoint()
override
278 int log = 0, ckpt = 0;
279 int ret = sqlite3_wal_checkpoint_v2(
280 conn,
nullptr, SQLITE_CHECKPOINT_PASSIVE, &log, &ckpt);
282 auto fname = sqlite3_db_filename(conn,
"main");
283 if (ret != SQLITE_OK)
285 auto jm = (ret == SQLITE_LOCKED) ? j_.
trace() : j_.
warn();
286 JLOG(jm) <<
"WAL(" << fname <<
"): error " << ret;
290 JLOG(j_.
trace()) <<
"WAL(" << fname <<
"): frames=" <<
log
291 <<
", written=" << ckpt;
307 bool running_ =
false;
313 sqlite_api::sqlite3* conn,
319 if (
auto checkpointer =
322 checkpointer->schedule();
326 sqlite_api::sqlite3_wal_hook(conn,
nullptr,
nullptr);
342 return std::make_shared<WALCheckpointer>(
343 id, std::move(session), queue, logs);
348#if defined(__clang__)
349#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)