20 #include <ripple/app/rdb/UnitaryShard.h>
21 #include <ripple/basics/StringUtilities.h>
22 #include <boost/format.hpp>
23 #include <boost/range/adaptor/transformed.hpp>
32 auto tx{std::make_unique<DatabaseCon>(
34 tx->getSession() << boost::str(
35 boost::format(
"PRAGMA cache_size=-%d;") %
38 auto lgr{std::make_unique<DatabaseCon>(
40 lgr->getSession() << boost::str(
41 boost::format(
"PRAGMA cache_size=-%d;") %
44 return {std::move(lgr), std::move(tx)};
54 auto tx{std::make_unique<DatabaseCon>(
56 tx->getSession() << boost::str(
57 boost::format(
"PRAGMA cache_size=-%d;") %
61 auto lgr{std::make_unique<DatabaseCon>(
63 lgr->getSession() << boost::str(
64 boost::format(
"PRAGMA cache_size=-%d;") %
67 return {std::move(lgr), std::move(tx)};
72 soci::session& txsession,
73 soci::session& lgrsession,
79 auto const ledgerSeq{ledger->
info().
seq};
83 auto& session{txsession};
84 soci::transaction tr(session);
86 session <<
"DELETE FROM Transactions "
87 "WHERE LedgerSeq = :seq;",
89 session <<
"DELETE FROM AccountTransactions "
90 "WHERE LedgerSeq = :seq;",
99 <<
"shard " << index <<
" has an invalid transaction map"
100 <<
" on sequence " << sSeq;
104 for (
auto const& item : ledger->
txs)
106 if (stop.
load(std::memory_order_relaxed))
110 item.first->getTransactionID(),
114 auto const sTxID =
to_string(txMeta.getTxID());
116 session <<
"DELETE FROM AccountTransactions "
117 "WHERE TransID = :txID;",
120 auto const& accounts = txMeta.getAffectedAccounts();
121 if (!accounts.empty())
124 auto const s{boost::str(
125 boost::format(
"('%s','%s',%s,%s)") % sTxID %
"%s" %
128 sql.
reserve((accounts.size() + 1) * 128);
130 "INSERT INTO AccountTransactions "
131 "(TransID, Account, LedgerSeq, TxnSeq) VALUES ";
132 sql += boost::algorithm::join(
134 boost::adaptors::transformed(
145 <<
"shard " << index <<
" account transaction: " << sql;
152 <<
"shard " << index <<
" transaction in ledger "
153 << sSeq <<
" affects no accounts";
160 item.first->getMetaSQL(
173 auto& session{lgrsession};
174 soci::transaction tr(session);
181 session <<
"DELETE FROM Ledgers "
182 "WHERE LedgerSeq = :seq;",
183 soci::use(ledgerSeq);
184 session <<
"INSERT OR REPLACE INTO Ledgers ("
185 "LedgerHash, LedgerSeq, PrevHash, TotalCoins, ClosingTime,"
186 "PrevClosingTime, CloseTimeRes, CloseFlags, AccountSetHash,"
189 ":ledgerHash, :ledgerSeq, :prevHash, :totalCoins,"
190 ":closingTime, :prevClosingTime, :closeTimeRes,"
191 ":closeFlags, :accountSetHash, :transSetHash);",
192 soci::use(sHash), soci::use(ledgerSeq), soci::use(sParentHash),
212 return std::make_unique<DatabaseCon>(
223 session <<
"INSERT INTO Shard (ShardIndex) "
224 "VALUES (:shardIndex);",
233 boost::optional<std::uint32_t> resIndex;
234 soci::blob sociBlob(session);
235 soci::indicator blobPresent;
237 session <<
"SELECT ShardIndex, StoredLedgerSeqs "
239 "WHERE ShardIndex = :index;",
240 soci::into(resIndex), soci::into(sociBlob, blobPresent),
243 if (!resIndex || index != resIndex)
246 if (blobPresent != soci::i_ok)
260 boost::optional<std::uint32_t> resIndex;
261 boost::optional<std::string> sHash0;
262 soci::blob sociBlob(session);
263 soci::indicator blobPresent;
265 session <<
"SELECT ShardIndex, LastLedgerHash, StoredLedgerSeqs "
267 "WHERE ShardIndex = :index;",
268 soci::into(resIndex), soci::into(sHash0),
269 soci::into(sociBlob, blobPresent), soci::use(index);
274 if (!resIndex || index != resIndex)
275 return {
false, {{}, {}}};
277 if (blobPresent != soci::i_ok)
278 return {
true, {{}, sHash}};
283 return {
true, {s, sHash}};
288 soci::session& session,
294 soci::blob sociBlob(session);
300 if (ledger->
info().
seq == lastSeq)
303 session <<
"UPDATE Shard "
304 "SET LastLedgerHash = :lastLedgerHash,"
305 "StoredLedgerSeqs = :storedLedgerSeqs "
306 "WHERE ShardIndex = :shardIndex;",
307 soci::use(sHash), soci::use(sociBlob), soci::use(index);
311 session <<
"UPDATE Shard "
312 "SET StoredLedgerSeqs = :storedLedgerSeqs "
313 "WHERE ShardIndex = :shardIndex;",
314 soci::use(sociBlob), soci::use(index);
constexpr std::array< char const *, 1 > AcquireShardDBPragma
constexpr auto AcquireShardDBName
std::unique_ptr< DatabaseCon > makeAcquireDB(DatabaseCon::Setup const &setup, DatabaseCon::CheckpointerSetup const &checkpointerSetup)
makeAcquireDB Opens the shard acquire database and returns its descriptor.
constexpr std::array< char const *, 4 > TxDBPragma
Stream trace() const
Severity stream access functions.
void convert(soci::blob &from, std::vector< std::uint8_t > &to)
std::pair< bool, AcquireShardSeqsHash > selectAcquireDBLedgerSeqsHash(soci::session &session, std::uint32_t index)
selectAcquireDBLedgerSeqsHash Returns the set of acquired ledger sequences and the last ledger hash f...
void insertAcquireDBIndex(soci::session &session, std::uint32_t index)
insertAcquireDBIndex Adds a new shard index to the shard acquire database.
std::string toBase58(AccountID const &v)
Convert AccountID to base58 checked string.
constexpr auto kilobytes(T value) noexcept
constexpr std::array< char const *, 1 > AcquireShardDBInit
static std::string const & getMetaSQLInsertReplaceHeader()
constexpr std::array< char const *, 5 > LgrDBInit
NetClock::time_point closeTime
Integers of any length that is a multiple of 32-bits.
void updateAcquireDB(soci::session &session, std::shared_ptr< Ledger const > const &ledger, std::uint32_t index, std::uint32_t lastSeq, std::optional< std::string > const &seqs)
updateAcquireDB Updates information in the acquire DB.
LedgerInfo const & info() const override
Returns information about the ledger.
T time_since_epoch(T... args)
bool isPseudoTx(STObject const &tx)
Check whether a transaction is a pseudo-transaction.
int getValueFor(SizedItem item, std::optional< std::size_t > node=std::nullopt) const
Retrieve the default value for the item at the specified node size.
constexpr std::array< char const *, 1 > LgrDBPragma
A generic endpoint for log messages.
constexpr std::array< char const *, 2 > FinalShardDBPragma
SHAMap const & txMap() const
bool updateLedgerDBs(soci::session &txsession, soci::session &lgrsession, std::shared_ptr< Ledger const > const &ledger, std::uint32_t index, std::atomic< bool > &stop, beast::Journal j)
updateLedgerDBs Saves the given ledger to shard databases.
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
LedgerIndex seq() const
Returns the sequence number of the base ledger.
NetClock::duration closeTimeResolution
DatabasePair makeShardIncompleteLedgerDBs(Config const &config, DatabaseCon::Setup const &setup, DatabaseCon::CheckpointerSetup const &checkpointerSetup)
makeShardIncompleteLedgerDBs Opens shard databases for partially downloaded or unverified shards and ...
std::string to_string(Manifest const &m)
Format the specified manifest to a string for debugging purposes.
std::pair< bool, std::optional< std::string > > selectAcquireDBLedgerSeqs(soci::session &session, std::uint32_t index)
selectAcquireDBLedgerSeqs Returns the set of acquired ledgers for the given shard.
constexpr std::array< char const *, 8 > TxDBInit
std::string sqlBlobLiteral(Blob const &blob)
Format arbitrary binary data as an SQLite "blob literal".
DatabasePair makeShardCompleteLedgerDBs(Config const &config, DatabaseCon::Setup const &setup)
makeShardCompleteLedgerDBs Opens shard databases for verified shards and returns their descriptors.
NetClock::time_point parentCloseTime