Clio  develop
The XRP Ledger API server.
Loading...
Searching...
No Matches
CassandraBackend.hpp
1#pragma once
2
3#include "data/LedgerHeaderCache.hpp"
4#include "data/Types.hpp"
5#include "data/cassandra/CassandraBackendFamily.hpp"
6#include "data/cassandra/CassandraSchema.hpp"
7#include "data/cassandra/Concepts.hpp"
8#include "data/cassandra/Handle.hpp"
9#include "data/cassandra/SettingsProvider.hpp"
10#include "data/cassandra/Types.hpp"
11#include "data/cassandra/impl/ExecutionStrategy.hpp"
12#include "util/log/Logger.hpp"
13
14#include <boost/asio/spawn.hpp>
15#include <boost/json/object.hpp>
16#include <boost/uuid/string_generator.hpp>
17#include <boost/uuid/uuid.hpp>
18#include <cassandra.h>
19#include <fmt/format.h>
20#include <xrpl/basics/Blob.h>
21#include <xrpl/basics/base_uint.h>
22#include <xrpl/basics/strHex.h>
23#include <xrpl/protocol/AccountID.h>
24#include <xrpl/protocol/Indexes.h>
25#include <xrpl/protocol/LedgerHeader.h>
26#include <xrpl/protocol/nft.h>
27
28#include <algorithm>
29#include <cstddef>
30#include <cstdint>
31#include <iterator>
32#include <optional>
33#include <string>
34#include <tuple>
35#include <vector>
36
37namespace data::cassandra {
38
46template <
47 SomeSettingsProvider SettingsProviderType,
48 SomeExecutionStrategy ExecutionStrategyType,
49 typename FetchLedgerCacheType = FetchLedgerCache>
51 SettingsProviderType,
52 ExecutionStrategyType,
53 CassandraSchema<SettingsProviderType>,
54 FetchLedgerCacheType> {
55 using DefaultCassandraFamily = CassandraBackendFamily<
56 SettingsProviderType,
57 ExecutionStrategyType,
59 FetchLedgerCacheType>;
60
61 // protected because CassandraMigrationBackend inherits from this class
62protected:
63 using DefaultCassandraFamily::executor_;
64 using DefaultCassandraFamily::ledgerSequence_;
65 using DefaultCassandraFamily::log_;
66 using DefaultCassandraFamily::range_;
67 using DefaultCassandraFamily::schema_;
68
69public:
73 using DefaultCassandraFamily::DefaultCassandraFamily;
74
79
88 bool
89 doFinishWrites() override
90 {
92
93 if (!range_) {
94 executor_.writeSync(
95 schema_->updateLedgerRange, ledgerSequence_, false, ledgerSequence_
96 );
97 }
98
99 if (not this->executeSyncUpdate(
100 schema_->updateLedgerRange.bind(ledgerSequence_, true, ledgerSequence_ - 1)
101 )) {
102 LOG(log_.warn()) << "Update failed for ledger " << ledgerSequence_;
103 return false;
104 }
105
106 LOG(log_.info()) << "Committed ledger " << ledgerSequence_;
107 return true;
108 }
109
121 [[nodiscard]] NFTsAndCursor
123 xrpl::AccountID const& issuer,
124 std::optional<std::uint32_t> const& taxon,
125 std::uint32_t const ledgerSequence,
126 std::uint32_t const limit,
127 std::optional<xrpl::uint256> const& cursorIn,
128 boost::asio::yield_context yield
129 ) const override
130 {
131 NFTsAndCursor ret;
132
133 Statement const idQueryStatement = [&taxon, &issuer, &cursorIn, &limit, this] {
134 if (taxon.has_value()) {
135 auto r = schema_->selectNFTIDsByIssuerTaxon.bind(issuer);
136 r.bindAt(1, *taxon);
137 r.bindAt(2, cursorIn.value_or(xrpl::uint256(0)));
138 r.bindAt(3, Limit{limit});
139 return r;
140 }
141
142 auto r = schema_->selectNFTIDsByIssuer.bind(issuer);
143 r.bindAt(
144 1,
145 std::make_tuple(
146 cursorIn.has_value() ? xrpl::nft::toUInt32(xrpl::nft::getTaxon(*cursorIn)) : 0,
147 cursorIn.value_or(xrpl::uint256(0))
148 )
149 );
150 r.bindAt(2, Limit{limit});
151 return r;
152 }();
153
154 // Query for all the NFTs issued by the account, potentially filtered by the taxon
155 auto const res = executor_.read(yield, idQueryStatement);
156
157 auto const& idQueryResults = res.value();
158 if (not idQueryResults.hasRows()) {
159 LOG(log_.debug()) << "No rows returned";
160 return {};
161 }
162
163 std::vector<xrpl::uint256> nftIDs;
164 for (auto const [nftID] : extract<xrpl::uint256>(idQueryResults))
165 nftIDs.push_back(nftID);
166
167 if (nftIDs.empty())
168 return ret;
169
170 if (nftIDs.size() == limit)
171 ret.cursor = nftIDs.back();
172
173 std::vector<Statement> selectNFTStatements;
174 selectNFTStatements.reserve(nftIDs.size());
175
176 std::transform(
177 std::cbegin(nftIDs),
178 std::cend(nftIDs),
179 std::back_inserter(selectNFTStatements),
180 [&](auto const& nftID) { return schema_->selectNFT.bind(nftID, ledgerSequence); }
181 );
182
183 auto const nftInfos = executor_.readEach(yield, selectNFTStatements);
184
185 std::vector<Statement> selectNFTURIStatements;
186 selectNFTURIStatements.reserve(nftIDs.size());
187
188 std::transform(
189 std::cbegin(nftIDs),
190 std::cend(nftIDs),
191 std::back_inserter(selectNFTURIStatements),
192 [&](auto const& nftID) { return schema_->selectNFTURI.bind(nftID, ledgerSequence); }
193 );
194
195 auto const nftUris = executor_.readEach(yield, selectNFTURIStatements);
196
197 for (auto i = 0u; i < nftIDs.size(); i++) {
198 if (auto const maybeRow = nftInfos[i].template get<uint32_t, xrpl::AccountID, bool>();
199 maybeRow.has_value()) {
200 auto [seq, owner, isBurned] = *maybeRow;
201 NFT nft(nftIDs[i], seq, owner, isBurned);
202 if (auto const maybeUri = nftUris[i].template get<xrpl::Blob>();
203 maybeUri.has_value())
204 nft.uri = *maybeUri;
205 ret.nfts.push_back(nft);
206 }
207 }
208 return ret;
209 }
210
221 [[nodiscard]] std::vector<xrpl::uint256>
223 std::uint32_t number,
224 std::uint32_t pageSize,
225 std::uint32_t seq,
226 boost::asio::yield_context yield
227 ) const override
228 {
229 std::vector<xrpl::uint256> liveAccounts;
230 std::optional<xrpl::AccountID> lastItem;
231
232 while (liveAccounts.size() < number) {
233 Statement const statement = lastItem
234 ? schema_->selectAccountFromToken.bind(*lastItem, Limit{pageSize})
235 : schema_->selectAccountFromBeginning.bind(Limit{pageSize});
236
237 auto const res = executor_.read(yield, statement);
238 if (res) {
239 auto const& results = res.value();
240 if (not results.hasRows()) {
241 LOG(log_.debug()) << "No rows returned";
242 break;
243 }
244 // The results should not contain duplicates, we just filter out deleted accounts
245 std::vector<xrpl::uint256> fullAccounts;
246 for (auto [account] : extract<xrpl::AccountID>(results)) {
247 fullAccounts.push_back(xrpl::keylet::account(account).key);
248 lastItem = account;
249 }
250 auto const objs = this->doFetchLedgerObjects(fullAccounts, seq, yield);
251
252 for (auto i = 0u; i < fullAccounts.size(); i++) {
253 if (not objs[i].empty()) {
254 if (liveAccounts.size() < number) {
255 liveAccounts.push_back(fullAccounts[i]);
256 } else {
257 break;
258 }
259 }
260 }
261 } else {
262 LOG(log_.error()) << "Could not fetch account from account_tx: " << res.error();
263 break;
264 }
265 }
266
267 return liveAccounts;
268 }
269};
270
275
276} // namespace data::cassandra
Implements CassandraBackendFamily for Cassandra/ScyllaDB.
Definition CassandraBackend.hpp:54
std::vector< xrpl::uint256 > fetchAccountRoots(std::uint32_t number, std::uint32_t pageSize, std::uint32_t seq, boost::asio::yield_context yield) const override
Fetch the specified number of account root object indexes by page; the accounts need to exist for seq...
Definition CassandraBackend.hpp:222
NFTsAndCursor fetchNFTsByIssuer(xrpl::AccountID const &issuer, std::optional< std::uint32_t > const &taxon, std::uint32_t const ledgerSequence, std::uint32_t const limit, std::optional< xrpl::uint256 > const &cursorIn, boost::asio::yield_context yield) const override
Fetches all NFTs issued by a given address.
Definition CassandraBackend.hpp:122
BasicCassandraBackend(BasicCassandraBackend &&)=delete
Move constructor is deleted because handle_ is shared by reference with executor.
bool doFinishWrites() override
Wait for pending writes and commit the ledger range update for the current ledger.
Definition CassandraBackend.hpp:89
CassandraBackendFamily(SettingsProviderType settingsProvider, data::LedgerCacheInterface &cache, bool readOnly)
Definition CassandraBackendFamily.hpp:98
std::vector< Blob > doFetchLedgerObjects(std::vector< xrpl::uint256 > const &keys, std::uint32_t const sequence, boost::asio::yield_context yield) const override
Definition CassandraBackendFamily.hpp:906
Manages the DB schema and provides access to prepared statements.
Definition CassandraSchema.hpp:24
The requirements of an execution strategy.
Definition Concepts.hpp:39
The requirements of a settings provider.
Definition Concepts.hpp:26
This namespace implements a wrapper for the Cassandra C++ driver.
Definition CassandraBackendFamily.hpp:50
impl::ResultExtractor< Types... > extract(Handle::ResultType const &result)
Extracts the results into series of std::tuple<Types...> by creating a simple wrapper with an STL inp...
Definition Handle.hpp:318
impl::Statement Statement
A simple or bound statement.
Definition Types.hpp:27
BasicCassandraBackend< SettingsProvider, impl::DefaultExecutionStrategy<> > CassandraBackend
The Cassandra backend with the default settings provider and execution strategy.
Definition CassandraBackend.hpp:274
Represents a NFToken.
Definition Types.hpp:164
Blob uri
URI of the token; empty if not set.
Definition Types.hpp:168
Represents a bundle of NFTs with a cursor to the next page.
Definition Types.hpp:230
std::vector< NFT > nfts
NFTs in this page.
Definition Types.hpp:231
std::optional< xrpl::uint256 > cursor
Token ID to continue from; nullopt if no more pages.
Definition Types.hpp:232
A strong type wrapper for int32_t.
Definition Types.hpp:38