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)
109 auto const txID{item.first->getTransactionID()};
111 auto const txMeta{std::make_shared<TxMeta>(
112 txID, ledger->
seq(), *item.second)};
114 session <<
"DELETE FROM AccountTransactions "
115 "WHERE TransID = :txID;",
118 auto const& accounts = txMeta->getAffectedAccounts();
119 if (!accounts.empty())
122 auto const s{boost::str(
123 boost::format(
"('%s','%s',%s,%s)") % sTxID %
"%s" %
126 sql.
reserve((accounts.size() + 1) * 128);
128 "INSERT INTO AccountTransactions "
129 "(TransID, Account, LedgerSeq, TxnSeq) VALUES ";
130 sql += boost::algorithm::join(
132 boost::adaptors::transformed(
143 <<
"shard " << index <<
" account transaction: " << sql;
150 <<
"shard " << index <<
" transaction in ledger "
151 << sSeq <<
" affects no accounts";
158 item.first->getMetaSQL(
171 auto& session{lgrsession};
172 soci::transaction tr(session);
179 session <<
"DELETE FROM Ledgers "
180 "WHERE LedgerSeq = :seq;",
181 soci::use(ledgerSeq);
182 session <<
"INSERT OR REPLACE INTO Ledgers ("
183 "LedgerHash, LedgerSeq, PrevHash, TotalCoins, ClosingTime,"
184 "PrevClosingTime, CloseTimeRes, CloseFlags, AccountSetHash,"
187 ":ledgerHash, :ledgerSeq, :prevHash, :totalCoins,"
188 ":closingTime, :prevClosingTime, :closeTimeRes,"
189 ":closeFlags, :accountSetHash, :transSetHash);",
190 soci::use(sHash), soci::use(ledgerSeq), soci::use(sParentHash),
210 return std::make_unique<DatabaseCon>(
221 session <<
"INSERT INTO Shard (ShardIndex) "
222 "VALUES (:shardIndex);",
231 boost::optional<std::uint32_t> resIndex;
232 soci::blob sociBlob(session);
233 soci::indicator blobPresent;
235 session <<
"SELECT ShardIndex, StoredLedgerSeqs "
237 "WHERE ShardIndex = :index;",
238 soci::into(resIndex), soci::into(sociBlob, blobPresent),
241 if (!resIndex || index != resIndex)
244 if (blobPresent != soci::i_ok)
258 boost::optional<std::uint32_t> resIndex;
259 boost::optional<std::string> sHash0;
260 soci::blob sociBlob(session);
261 soci::indicator blobPresent;
263 session <<
"SELECT ShardIndex, LastLedgerHash, StoredLedgerSeqs "
265 "WHERE ShardIndex = :index;",
266 soci::into(resIndex), soci::into(sHash0),
267 soci::into(sociBlob, blobPresent), soci::use(index);
272 if (!resIndex || index != resIndex)
273 return {
false, {{}, {}}};
275 if (blobPresent != soci::i_ok)
276 return {
true, {{}, sHash}};
281 return {
true, {s, sHash}};
286 soci::session& session,
292 soci::blob sociBlob(session);
298 if (ledger->
info().
seq == lastSeq)
301 session <<
"UPDATE Shard "
302 "SET LastLedgerHash = :lastLedgerHash,"
303 "StoredLedgerSeqs = :storedLedgerSeqs "
304 "WHERE ShardIndex = :shardIndex;",
305 soci::use(sHash), soci::use(sociBlob), soci::use(index);
309 session <<
"UPDATE Shard "
310 "SET StoredLedgerSeqs = :storedLedgerSeqs "
311 "WHERE ShardIndex = :shardIndex;",
312 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