1#include <xrpld/app/rdb/PeerFinder.h>
3#include <xrpld/peerfinder/detail/Store.h>
5#include <xrpl/basics/Log.h>
6#include <xrpl/basics/contract.h>
7#include <xrpl/beast/net/IPEndpoint.h>
8#include <xrpl/beast/utility/Journal.h>
9#include <xrpl/config/BasicConfig.h>
10#include <xrpl/rdb/SociDB.h>
12#include <boost/optional/optional.hpp>
15#include <soci/session.h>
16#include <soci/statement.h>
17#include <soci/transaction.h>
30 DBConfig const sociConfig(config,
"peerfinder");
31 sociConfig.
open(session);
35 soci::transaction tr(session);
36 session <<
"PRAGMA encoding=\"UTF-8\";";
38 session <<
"CREATE TABLE IF NOT EXISTS SchemaVersion ( "
39 " name TEXT PRIMARY KEY, "
43 session <<
"CREATE TABLE IF NOT EXISTS PeerFinder_BootstrapCache ( "
44 " id INTEGER PRIMARY KEY AUTOINCREMENT, "
45 " address TEXT UNIQUE NOT NULL, "
49 session <<
"CREATE INDEX IF NOT EXISTS "
50 " PeerFinder_BootstrapCache_Index ON "
51 "PeerFinder_BootstrapCache "
62 soci::transaction tr(session);
67 boost::optional<int> vO;
70 "FROM SchemaVersion WHERE "
71 " name = 'PeerFinder';",
74 version = vO.value_or(0);
76 JLOG(j.
info()) <<
"Opened version " << version <<
" database";
80 if (version < currentSchemaVersion)
82 JLOG(j.
info()) <<
"Updating database to version " << currentSchemaVersion;
84 else if (version > currentSchemaVersion)
96 session <<
"CREATE TABLE IF NOT EXISTS "
97 "PeerFinder_BootstrapCache_Next ( "
98 " id INTEGER PRIMARY KEY AUTOINCREMENT, "
99 " address TEXT UNIQUE NOT NULL, "
103 session <<
"CREATE INDEX IF NOT EXISTS "
104 " PeerFinder_BootstrapCache_Next_Index ON "
105 " PeerFinder_BootstrapCache_Next "
109 session <<
"SELECT COUNT(*) FROM PeerFinder_BootstrapCache;", soci::into(count);
118 (session.prepare <<
"SELECT "
121 "FROM PeerFinder_BootstrapCache;",
123 soci::into(valence));
130 if (!isUnspecified(entry.endpoint))
132 entry.valence = valence;
137 JLOG(j.
error()) <<
"Bad address string '" << s <<
"' in Bootcache table";
149 for (
auto iter(list.
cbegin()); iter != list.
cend(); ++iter)
155 session <<
"INSERT INTO PeerFinder_BootstrapCache_Next ( "
161 soci::use(s), soci::use(valence);
164 session <<
"DROP TABLE IF EXISTS PeerFinder_BootstrapCache;";
166 session <<
"DROP INDEX IF EXISTS PeerFinder_BootstrapCache_Index;";
168 session <<
"ALTER TABLE PeerFinder_BootstrapCache_Next "
169 " RENAME TO PeerFinder_BootstrapCache;";
171 session <<
"CREATE INDEX IF NOT EXISTS "
172 " PeerFinder_BootstrapCache_Index ON "
173 "PeerFinder_BootstrapCache "
185 session <<
"DROP TABLE IF EXISTS LegacyEndpoints;";
187 session <<
"DROP TABLE IF EXISTS PeerFinderLegacyEndpoints;";
189 session <<
"DROP TABLE IF EXISTS PeerFinder_LegacyEndpoints;";
191 session <<
"DROP TABLE IF EXISTS PeerFinder_LegacyEndpoints_Index;";
195 int const v(currentSchemaVersion);
196 session <<
"INSERT OR REPLACE INTO SchemaVersion ("
200 " 'PeerFinder', :version "
214 (session.prepare <<
"SELECT "
217 "FROM PeerFinder_BootstrapCache;",
219 soci::into(valence));
231 soci::transaction tr(session);
232 session <<
"DELETE FROM PeerFinder_BootstrapCache;";
241 for (
auto const& e : v)
247 session <<
"INSERT INTO PeerFinder_BootstrapCache ( "
253 soci::use(s), soci::use(valence);
static Endpoint fromString(std::string const &s)
A generic endpoint for log messages.
Holds unparsed configuration information.
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
T emplace_back(T... args)
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
void readPeerFinderDB(soci::session &session, std::function< void(std::string const &, int)> const &func)
readPeerFinderDB Reads all entries from the peer finder database and invokes the given callback for e...
void updatePeerFinderDB(soci::session &session, int currentSchemaVersion, beast::Journal j)
updatePeerFinderDB Updates the peer finder database to a new version.
void initPeerFinderDB(soci::session &session, BasicConfig const &config, beast::Journal j)
initPeerFinderDB Opens a session with the peer finder database.
std::string to_string(BaseUInt< Bits, Tag > const &a)
void savePeerFinderDB(soci::session &session, std::vector< PeerFinder::Store::Entry > const &v)
savePeerFinderDB Saves a new entry to the peer finder database.
XRPL_NO_SANITIZE_ADDRESS void Throw(Args &&... args)