Skip to content

Commit e8222b7

Browse files
committed
Use shared schema as base
1 parent 1222f67 commit e8222b7

File tree

5 files changed

+368
-1306
lines changed

5 files changed

+368
-1306
lines changed

src/data/CassandraBackend.hpp

Lines changed: 13 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -70,12 +70,12 @@ template <
7070
class BasicCassandraBackend : public CassandraBackendFamily<
7171
SettingsProviderType,
7272
ExecutionStrategyType,
73-
Schema<SettingsProviderType>,
73+
CassandraSchema<SettingsProviderType>,
7474
FetchLedgerCacheType> {
7575
using DefaultCassandraFamily = CassandraBackendFamily<
7676
SettingsProviderType,
7777
ExecutionStrategyType,
78-
Schema<SettingsProviderType>,
78+
CassandraSchema<SettingsProviderType>,
7979
FetchLedgerCacheType>;
8080

8181
// protected because CassandraMigrationBackend inherits from this class
@@ -106,7 +106,7 @@ class BasicCassandraBackend : public CassandraBackendFamily<
106106
executor_.writeSync(schema_->updateLedgerRange, ledgerSequence_, false, ledgerSequence_);
107107
}
108108

109-
if (not executeSyncUpdate(schema_->updateLedgerRange.bind(ledgerSequence_, true, ledgerSequence_ - 1))) {
109+
if (not this->executeSyncUpdate(schema_->updateLedgerRange.bind(ledgerSequence_, true, ledgerSequence_ - 1))) {
110110
LOG(log_.warn()) << "Update failed for ledger " << ledgerSequence_;
111111
return false;
112112
}
@@ -171,9 +171,10 @@ class BasicCassandraBackend : public CassandraBackendFamily<
171171
selectNFTStatements.reserve(nftIDs.size());
172172

173173
std::transform(
174-
std::cbegin(nftIDs), std::cend(nftIDs), std::back_inserter(selectNFTStatements), [&](auto const& nftID) {
175-
return schema_->selectNFT.bind(nftID, ledgerSequence);
176-
}
174+
std::cbegin(nftIDs),
175+
std::cend(nftIDs),
176+
std::back_inserter(selectNFTStatements),
177+
[&](auto const& nftID) { return schema_->selectNFT.bind(nftID, ledgerSequence); }
177178
);
178179

179180
auto const nftInfos = executor_.readEach(yield, selectNFTStatements);
@@ -182,9 +183,10 @@ class BasicCassandraBackend : public CassandraBackendFamily<
182183
selectNFTURIStatements.reserve(nftIDs.size());
183184

184185
std::transform(
185-
std::cbegin(nftIDs), std::cend(nftIDs), std::back_inserter(selectNFTURIStatements), [&](auto const& nftID) {
186-
return schema_->selectNFTURI.bind(nftID, ledgerSequence);
187-
}
186+
std::cbegin(nftIDs),
187+
std::cend(nftIDs),
188+
std::back_inserter(selectNFTURIStatements),
189+
[&](auto const& nftID) { return schema_->selectNFTURI.bind(nftID, ledgerSequence); }
188190
);
189191

190192
auto const nftUris = executor_.readEach(yield, selectNFTURIStatements);
@@ -202,12 +204,8 @@ class BasicCassandraBackend : public CassandraBackendFamily<
202204
}
203205

204206
std::vector<ripple::uint256>
205-
fetchAccountRoots(
206-
std::uint32_t number,
207-
std::uint32_t pageSize,
208-
std::uint32_t seq,
209-
boost::asio::yield_context yield
210-
) const override
207+
fetchAccountRoots(std::uint32_t number, std::uint32_t pageSize, std::uint32_t seq, boost::asio::yield_context yield)
208+
const override
211209
{
212210
std::vector<ripple::uint256> liveAccounts;
213211
std::optional<ripple::AccountID> lastItem;
@@ -248,31 +246,6 @@ class BasicCassandraBackend : public CassandraBackendFamily<
248246

249247
return liveAccounts;
250248
}
251-
252-
private:
253-
bool
254-
executeSyncUpdate(Statement statement)
255-
{
256-
auto const res = executor_.writeSync(statement);
257-
auto maybeSuccess = res->template get<bool>();
258-
if (not maybeSuccess) {
259-
LOG(log_.error()) << "executeSyncUpdate - error getting result - no row";
260-
return false;
261-
}
262-
263-
if (not maybeSuccess.value()) {
264-
LOG(log_.warn()) << "Update failed. Checking if DB state is what we expect";
265-
266-
// error may indicate that another writer wrote something.
267-
// in this case let's just compare the current state of things
268-
// against what we were trying to write in the first place and
269-
// use that as the source of truth for the result.
270-
auto rng = this->hardFetchLedgerRangeNoThrow();
271-
return rng && rng->maxSequence == ledgerSequence_;
272-
}
273-
274-
return true;
275-
}
276249
};
277250

278251
using CassandraBackend = BasicCassandraBackend<SettingsProvider, impl::DefaultExecutionStrategy<>>;

src/data/KeyspaceBackend.hpp

Lines changed: 13 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
#include "data/cassandra/SettingsProvider.hpp"
2828
#include "data/cassandra/Types.hpp"
2929
#include "data/cassandra/impl/ExecutionStrategy.hpp"
30+
#include "util/Assert.hpp"
3031
#include "util/log/Logger.hpp"
3132

3233
#include <boost/asio/spawn.hpp>
@@ -48,6 +49,7 @@
4849
#include <iterator>
4950
#include <optional>
5051
#include <stdexcept>
52+
#include <utility>
5153
#include <vector>
5254

5355
namespace data::cassandra {
@@ -104,7 +106,7 @@ class BasicKeyspaceBackend : public CassandraBackendFamily<
104106
executor_.writeSync(schema_->insertLedgerRange, true, ledgerSequence_);
105107
}
106108

107-
if (not executeSyncUpdate(schema_->updateLedgerRange.bind(ledgerSequence_, true, ledgerSequence_ - 1))) {
109+
if (not this->executeSyncUpdate(schema_->updateLedgerRange.bind(ledgerSequence_, true, ledgerSequence_ - 1))) {
108110
log_.warn() << "Update failed for ledger " << ledgerSequence_;
109111
return false;
110112
}
@@ -180,35 +182,11 @@ class BasicKeyspaceBackend : public CassandraBackendFamily<
180182
[[maybe_unused]] boost::asio::yield_context yield
181183
) const override
182184
{
183-
LOG(log_.error()) << "Fetching account roots is not supported by the Keyspaces backend.";
184-
throw std::runtime_error("Fetching account roots is not supported by the Keyspaces backend.");
185+
ASSERT(false, "Fetching account roots is not supported by the Keyspaces backend.");
186+
std::unreachable();
185187
}
186188

187189
private:
188-
bool
189-
executeSyncUpdate(Statement statement)
190-
{
191-
auto const res = executor_.writeSync(statement);
192-
auto maybeSuccess = res->template get<bool>();
193-
if (not maybeSuccess) {
194-
LOG(log_.error()) << "executeSyncUpdate - error getting result - no row";
195-
return false;
196-
}
197-
198-
if (not maybeSuccess.value()) {
199-
LOG(log_.warn()) << "Update failed. Checking if DB state is what we expect";
200-
201-
// error may indicate that another writer wrote something.
202-
// in this case let's just compare the current state of things
203-
// against what we were trying to write in the first place and
204-
// use that as the source of truth for the result.
205-
auto rng = this->hardFetchLedgerRangeNoThrow();
206-
return rng && rng->maxSequence == ledgerSequence_;
207-
}
208-
209-
return true;
210-
}
211-
212190
std::vector<ripple::uint256>
213191
fetchNFTIDsByTaxon(
214192
ripple::AccountID const& issuer,
@@ -295,17 +273,19 @@ class BasicKeyspaceBackend : public CassandraBackendFamily<
295273
std::vector<Statement> selectNFTStatements;
296274
selectNFTStatements.reserve(nftIDs.size());
297275
std::transform(
298-
std::cbegin(nftIDs), std::cend(nftIDs), std::back_inserter(selectNFTStatements), [&](auto const& nftID) {
299-
return schema_->selectNFT.bind(nftID, ledgerSequence);
300-
}
276+
std::cbegin(nftIDs),
277+
std::cend(nftIDs),
278+
std::back_inserter(selectNFTStatements),
279+
[&](auto const& nftID) { return schema_->selectNFT.bind(nftID, ledgerSequence); }
301280
);
302281

303282
std::vector<Statement> selectNFTURIStatements;
304283
selectNFTURIStatements.reserve(nftIDs.size());
305284
std::transform(
306-
std::cbegin(nftIDs), std::cend(nftIDs), std::back_inserter(selectNFTURIStatements), [&](auto const& nftID) {
307-
return schema_->selectNFTURI.bind(nftID, ledgerSequence);
308-
}
285+
std::cbegin(nftIDs),
286+
std::cend(nftIDs),
287+
std::back_inserter(selectNFTURIStatements),
288+
[&](auto const& nftID) { return schema_->selectNFTURI.bind(nftID, ledgerSequence); }
309289
);
310290

311291
auto const nftInfos = executor_.readEach(yield, selectNFTStatements);

src/data/cassandra/CassandraBackendFamily.hpp

Lines changed: 23 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -343,10 +343,8 @@ class CassandraBackendFamily : public BackendInterface {
343343
}
344344

345345
std::vector<ripple::uint256>
346-
fetchAllTransactionHashesInLedger(
347-
std::uint32_t const ledgerSequence,
348-
boost::asio::yield_context yield
349-
) const override
346+
fetchAllTransactionHashesInLedger(std::uint32_t const ledgerSequence, boost::asio::yield_context yield)
347+
const override
350348
{
351349
auto start = std::chrono::system_clock::now();
352350
auto const res = executor_.read(yield, schema_->selectAllTransactionHashesInLedger, ledgerSequence);
@@ -376,11 +374,8 @@ class CassandraBackendFamily : public BackendInterface {
376374
}
377375

378376
std::optional<NFT>
379-
fetchNFT(
380-
ripple::uint256 const& tokenID,
381-
std::uint32_t const ledgerSequence,
382-
boost::asio::yield_context yield
383-
) const override
377+
fetchNFT(ripple::uint256 const& tokenID, std::uint32_t const ledgerSequence, boost::asio::yield_context yield)
378+
const override
384379
{
385380
auto const res = executor_.read(yield, schema_->selectNFT, tokenID, ledgerSequence);
386381
if (not res)
@@ -525,11 +520,8 @@ class CassandraBackendFamily : public BackendInterface {
525520
}
526521

527522
std::optional<Blob>
528-
doFetchLedgerObject(
529-
ripple::uint256 const& key,
530-
std::uint32_t const sequence,
531-
boost::asio::yield_context yield
532-
) const override
523+
doFetchLedgerObject(ripple::uint256 const& key, std::uint32_t const sequence, boost::asio::yield_context yield)
524+
const override
533525
{
534526
LOG(log_.debug()) << "Fetching ledger object for seq " << sequence << ", key = " << ripple::to_string(key);
535527
if (auto const res = executor_.read(yield, schema_->selectObject, key, sequence); res) {
@@ -547,11 +539,8 @@ class CassandraBackendFamily : public BackendInterface {
547539
}
548540

549541
std::optional<std::uint32_t>
550-
doFetchLedgerObjectSeq(
551-
ripple::uint256 const& key,
552-
std::uint32_t const sequence,
553-
boost::asio::yield_context yield
554-
) const override
542+
doFetchLedgerObjectSeq(ripple::uint256 const& key, std::uint32_t const sequence, boost::asio::yield_context yield)
543+
const override
555544
{
556545
LOG(log_.debug()) << "Fetching ledger object for seq " << sequence << ", key = " << ripple::to_string(key);
557546
if (auto const res = executor_.read(yield, schema_->selectObject, key, sequence); res) {
@@ -585,11 +574,8 @@ class CassandraBackendFamily : public BackendInterface {
585574
}
586575

587576
std::optional<ripple::uint256>
588-
doFetchSuccessorKey(
589-
ripple::uint256 key,
590-
std::uint32_t const ledgerSequence,
591-
boost::asio::yield_context yield
592-
) const override
577+
doFetchSuccessorKey(ripple::uint256 key, std::uint32_t const ledgerSequence, boost::asio::yield_context yield)
578+
const override
593579
{
594580
if (auto const res = executor_.read(yield, schema_->selectSuccessor, key, ledgerSequence); res) {
595581
if (auto const result = res->template get<ripple::uint256>(); result) {
@@ -622,9 +608,10 @@ class CassandraBackendFamily : public BackendInterface {
622608
auto const timeDiff = util::timed([this, yield, &results, &hashes, &statements]() {
623609
// TODO: seems like a job for "hash IN (list of hashes)" instead?
624610
std::transform(
625-
std::cbegin(hashes), std::cend(hashes), std::back_inserter(statements), [this](auto const& hash) {
626-
return schema_->selectTransaction.bind(hash);
627-
}
611+
std::cbegin(hashes),
612+
std::cend(hashes),
613+
std::back_inserter(statements),
614+
[this](auto const& hash) { return schema_->selectTransaction.bind(hash); }
628615
);
629616

630617
auto const entries = executor_.readEach(yield, statements);
@@ -668,14 +655,18 @@ class CassandraBackendFamily : public BackendInterface {
668655

669656
// TODO: seems like a job for "key IN (list of keys)" instead?
670657
std::transform(
671-
std::cbegin(keys), std::cend(keys), std::back_inserter(statements), [this, &sequence](auto const& key) {
672-
return schema_->selectObject.bind(key, sequence);
673-
}
658+
std::cbegin(keys),
659+
std::cend(keys),
660+
std::back_inserter(statements),
661+
[this, &sequence](auto const& key) { return schema_->selectObject.bind(key, sequence); }
674662
);
675663

676664
auto const entries = executor_.readEach(yield, statements);
677665
std::transform(
678-
std::cbegin(entries), std::cend(entries), std::back_inserter(results), [](auto const& res) -> Blob {
666+
std::cbegin(entries),
667+
std::cend(entries),
668+
std::back_inserter(results),
669+
[](auto const& res) -> Blob {
679670
if (auto const maybeValue = res.template get<Blob>(); maybeValue)
680671
return *maybeValue;
681672

@@ -940,7 +931,7 @@ class CassandraBackendFamily : public BackendInterface {
940931
return executor_.stats();
941932
}
942933

943-
private:
934+
protected:
944935
bool
945936
executeSyncUpdate(Statement statement)
946937
{

0 commit comments

Comments
 (0)