2#pragma clang diagnostic push
3#pragma clang diagnostic ignored "-Wdeprecated"
6#include <xrpl/basics/ByteUtilities.h>
7#include <xrpl/basics/contract.h>
8#include <xrpl/rdb/DatabaseCon.h>
9#include <xrpl/rdb/SociDB.h>
11#include <boost/filesystem.hpp>
13#include <soci/sqlite3/soci-sqlite3.h>
28 Throw<std::runtime_error>(
"Sqlite databases must specify a dir and a name. Name: " + name +
" Dir: " + dir);
30 boost::filesystem::path file(dir);
31 if (is_directory(file))
39 auto const& section = config.
section(
"sqdb");
40 auto const backendName =
get(section,
"backend",
"sqlite");
42 if (backendName !=
"sqlite")
43 Throw<std::runtime_error>(
"Unsupported soci backend: " + backendName);
45 auto const path = config.
legacy(
"database_path");
46 auto const ext = dbName ==
"validators" || dbName ==
"peerfinder" ?
".sqlite" :
".db";
81 if (beName ==
"sqlite")
82 s.open(soci::sqlite3, connectionString);
84 Throw<std::runtime_error>(
"Unsupported soci backend: " + beName);
87static sqlite_api::sqlite3*
90 sqlite_api::sqlite3* result =
nullptr;
91 auto be = s.get_backend();
92 if (
auto b =
dynamic_cast<soci::sqlite3_session_backend*
>(be))
96 Throw<std::logic_error>(
"Didn't get a database connection.");
105 Throw<std::logic_error>(
"No connection found.");
106 return static_cast<size_t>(sqlite_api::sqlite3_memory_used() /
kilobytes(1));
115 int cur = 0, hiw = 0;
116 sqlite_api::sqlite3_db_status(conn, SQLITE_DBSTATUS_CACHE_USED, &cur, &hiw, 0);
119 Throw<std::logic_error>(
"");
126 to.
resize(from.get_len());
129 from.read(0,
reinterpret_cast<char*
>(&to[0]), from.get_len());
144 to.write(0,
reinterpret_cast<char const*
>(&from[0]), from.
size());
153 to.write(0, from.
data(), from.
size());
169class WALCheckpointer :
public Checkpointer
173 : id_(id), session_(
std::move(session)), jobQueue_(q), j_(logs.journal(
"WALCheckpointer"))
178 sqlite_api::sqlite3_wal_hook(conn, &sqliteWALHook,
reinterpret_cast<void*
>(id_));
185 if (
auto p = session_.lock())
198 ~WALCheckpointer()
override =
default;
211 if (!jobQueue_.addJob(
220 if (auto self = wp.lock())
230 checkpoint()
override
237 int log = 0, ckpt = 0;
238 int ret = sqlite3_wal_checkpoint_v2(conn,
nullptr, SQLITE_CHECKPOINT_PASSIVE, &log, &ckpt);
240 auto fname = sqlite3_db_filename(conn,
"main");
241 if (ret != SQLITE_OK)
243 auto jm = (ret == SQLITE_LOCKED) ? j_.
trace() : j_.
warn();
244 JLOG(jm) <<
"WAL(" << fname <<
"): error " << ret;
248 JLOG(j_.
trace()) <<
"WAL(" << fname <<
"): frames=" <<
log <<
", written=" << ckpt;
264 bool running_ =
false;
268 sqliteWALHook(
void* cpId, sqlite_api::sqlite3* conn,
char const* dbName,
int walSize)
274 checkpointer->schedule();
278 sqlite_api::sqlite3_wal_hook(conn,
nullptr,
nullptr);
295#if defined(__clang__)
296#pragma clang diagnostic pop
A generic endpoint for log messages.
Stream trace() const
Severity stream access functions.
Holds unparsed configuration information.
void legacy(std::string const §ion, std::string value)
Set a value that is not a key/value pair.
Section & section(std::string const &name)
Returns the section with the given name.
DBConfig is used when a client wants to delay opening a soci::session after parsing the config parame...
void open(soci::session &s) const
std::string connectionString() const
std::string connectionString_
DBConfig(std::string const &dbPath)
A pool of threads to perform work.
Manages partitions for logging.
std::string getSociSqliteInit(std::string const &name, std::string const &dir, std::string const &ext)
std::string getSociInit(BasicConfig const &config, std::string const &dbName)
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
static auto checkpointPageCount
T get(Section const §ion, std::string const &name, T const &defaultValue=T{})
Retrieve a key/value pair from a section.
std::uint32_t getKBUsedDB(soci::session &s)
static sqlite_api::sqlite3 * getConnection(soci::session &s)
std::uint32_t getKBUsedAll(soci::session &s)
@ open
We haven't closed our ledger yet, but others might have.
std::shared_ptr< Checkpointer > makeCheckpointer(std::uintptr_t id, std::weak_ptr< soci::session >, JobQueue &, Logs &)
Returns a new checkpointer which makes checkpoints of a soci database every checkpointPageCount pages...
constexpr auto kilobytes(T value) noexcept
void convert(soci::blob &from, std::vector< std::uint8_t > &to)
std::shared_ptr< Checkpointer > checkpointerFromId(std::uintptr_t id)