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>
28#include <xrpl/basics/ByteUtilities.h>
29#include <xrpl/basics/contract.h>
31#include <boost/filesystem.hpp>
32#include <soci/sqlite3/soci-sqlite3.h>
50 Throw<std::runtime_error>(
51 "Sqlite databases must specify a dir and a name. Name: " + name +
54 boost::filesystem::path file(dir);
55 if (is_directory(file))
63 auto const& section = config.
section(
"sqdb");
64 auto const backendName =
get(section,
"backend",
"sqlite");
66 if (backendName !=
"sqlite")
67 Throw<std::runtime_error>(
"Unsupported soci backend: " + backendName);
69 auto const path = config.
legacy(
"database_path");
71 dbName ==
"validators" || dbName ==
"peerfinder" ?
".sqlite" :
".db";
82 :
DBConfig(detail::getSociInit(config, dbName))
110 if (beName ==
"sqlite")
111 s.open(soci::sqlite3, connectionString);
113 Throw<std::runtime_error>(
"Unsupported soci backend: " + beName);
116static sqlite_api::sqlite3*
119 sqlite_api::sqlite3* result =
nullptr;
120 auto be = s.get_backend();
121 if (
auto b =
dynamic_cast<soci::sqlite3_session_backend*
>(be))
125 Throw<std::logic_error>(
"Didn't get a database connection.");
134 Throw<std::logic_error>(
"No connection found.");
135 return static_cast<size_t>(
136 sqlite_api::sqlite3_memory_used() /
kilobytes(1));
145 int cur = 0, hiw = 0;
146 sqlite_api::sqlite3_db_status(
147 conn, SQLITE_DBSTATUS_CACHE_USED, &cur, &hiw, 0);
150 Throw<std::logic_error>(
"");
157 to.
resize(from.get_len());
160 from.read(0,
reinterpret_cast<char*
>(&to[0]), from.get_len());
175 to.write(0,
reinterpret_cast<char const*
>(&from[0]), from.
size());
184 to.write(0, from.
data(), from.
size());
200class WALCheckpointer :
public Checkpointer
209 , session_(
std::move(session))
211 , j_(logs.journal(
"WALCheckpointer"))
216 sqlite_api::sqlite3_wal_hook(
217 conn, &sqliteWALHook,
reinterpret_cast<void*
>(id_));
224 if (
auto p = session_.lock())
237 ~WALCheckpointer()
override =
default;
250 if (!jobQueue_.addJob(
259 if (auto self = wp.lock())
269 checkpoint()
override
276 int log = 0, ckpt = 0;
277 int ret = sqlite3_wal_checkpoint_v2(
278 conn,
nullptr, SQLITE_CHECKPOINT_PASSIVE, &log, &ckpt);
280 auto fname = sqlite3_db_filename(conn,
"main");
281 if (ret != SQLITE_OK)
283 auto jm = (ret == SQLITE_LOCKED) ? j_.
trace() : j_.
warn();
284 JLOG(jm) <<
"WAL(" << fname <<
"): error " << ret;
288 JLOG(j_.
trace()) <<
"WAL(" << fname <<
"): frames=" <<
log
289 <<
", written=" << ckpt;
305 bool running_ =
false;
311 sqlite_api::sqlite3* conn,
317 if (
auto checkpointer =
320 checkpointer->schedule();
324 sqlite_api::sqlite3_wal_hook(conn,
nullptr,
nullptr);
340 return std::make_shared<WALCheckpointer>(
341 id, std::move(session), queue, logs);
346#if defined(__clang__)
347#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)