Clio  develop
The XRP Ledger API server.
Loading...
Searching...
No Matches
CassandraSchema.hpp
1#pragma once
2
3#include "data/cassandra/Concepts.hpp"
4#include "data/cassandra/Handle.hpp"
5#include "data/cassandra/Schema.hpp"
6#include "data/cassandra/SettingsProvider.hpp"
7#include "data/cassandra/Types.hpp"
8#include "util/log/Logger.hpp"
9
10#include <boost/json/string.hpp>
11#include <fmt/compile.h>
12
13#include <functional>
14#include <memory>
15
16namespace data::cassandra {
17
23template <SomeSettingsProvider SettingsProviderType>
24class CassandraSchema : public Schema<SettingsProvider> {
25 using Schema::Schema;
26
27public:
31 struct CassandraStatements : public Schema<SettingsProvider>::Statements {
32 using Schema<SettingsProvider>::Statements::Statements;
33
34 //
35 // Update (and "delete") queries
36 //
41 return handle_.get().prepare(
42 fmt::format(
43 R"(
44 UPDATE {}
45 SET sequence = ?
46 WHERE is_latest = ?
47 IF sequence IN (?, null)
48 )",
49 qualifiedTableName(settingsProvider_.get(), "ledger_range")
50 )
51 );
52 }();
53
54 //
55 // Select queries
56 //
57
62 return handle_.get().prepare(
63 fmt::format(
64 R"(
65 SELECT token_id
66 FROM {}
67 WHERE issuer = ?
68 AND (taxon, token_id) > ?
69 ORDER BY taxon ASC, token_id ASC
70 LIMIT ?
71 )",
72 qualifiedTableName(settingsProvider_.get(), "issuer_nf_tokens_v2")
73 )
74 );
75 }();
76
81 return handle_.get().prepare(
82 fmt::format(
83 R"(
84 SELECT account
85 FROM {}
86 WHERE token(account) > 0
87 PER PARTITION LIMIT 1
88 LIMIT ?
89 )",
90 qualifiedTableName(settingsProvider_.get(), "account_tx")
91 )
92 );
93 }();
94
99 return handle_.get().prepare(
100 fmt::format(
101 R"(
102 SELECT account
103 FROM {}
104 WHERE token(account) > token(?)
105 PER PARTITION LIMIT 1
106 LIMIT ?
107 )",
108 qualifiedTableName(settingsProvider_.get(), "account_tx")
109 )
110 );
111 }();
112
117 return handle_.get().prepare(
118 fmt::format(
119 R"(
120 SELECT key
121 FROM {}
122 WHERE TOKEN(key) >= ?
123 AND sequence <= ?
124 PER PARTITION LIMIT 1
125 LIMIT ?
126 ALLOW FILTERING
127 )",
128 qualifiedTableName(settingsProvider_.get(), "objects")
129 )
130 );
131 }();
132
137 return handle_.get().prepare(
138 fmt::format(
139 R"(
140 SELECT object, key
141 FROM {}
142 WHERE TOKEN(key) >= ?
143 AND sequence <= ?
144 PER PARTITION LIMIT 1
145 LIMIT ?
146 ALLOW FILTERING
147 )",
148 qualifiedTableName(settingsProvider_.get(), "objects")
149 )
150 );
151 }();
152 };
153
159 void
160 prepareStatements(Handle const& handle) override
161 {
162 LOG(log_.info()) << "Preparing cassandra statements";
163 statements_ = std::make_unique<CassandraStatements>(settingsProvider_, handle);
164 LOG(log_.info()) << "Finished preparing statements";
165 }
166
172 std::unique_ptr<CassandraStatements> const&
174 {
175 return statements_;
176 }
177
178private:
179 std::unique_ptr<CassandraStatements> statements_{nullptr};
180};
181
182} // namespace data::cassandra
Manages the DB schema and provides access to prepared statements.
Definition CassandraSchema.hpp:24
std::unique_ptr< CassandraStatements > const & operator->() const
Provides access to statements.
Definition CassandraSchema.hpp:173
void prepareStatements(Handle const &handle) override
Recreates the prepared statements, including the Cassandra-specific ones.
Definition CassandraSchema.hpp:160
Represents a handle to the cassandra database cluster.
Definition Handle.hpp:27
Statements(SettingsProviderType const &settingsProvider, Handle const &handle)
Construct a new Statements object.
Definition Schema.hpp:379
Schema(SettingsProviderType const &settingsProvider)
Shared Schema's between all Schema classes (Cassandra and Keyspace).
Definition Schema.hpp:56
Provides settings for BasicCassandraBackend.
Definition SettingsProvider.hpp:17
This namespace implements a wrapper for the Cassandra C++ driver.
Definition CassandraBackendFamily.hpp:50
std::string qualifiedTableName(SettingsProviderType const &provider, std::string_view name)
Returns the table name qualified with the keyspace and table prefix.
Definition Schema.hpp:27
impl::PreparedStatement PreparedStatement
A prepared statement.
Definition Types.hpp:28
Construct a new Cassandra Schema object.
Definition CassandraSchema.hpp:31
PreparedStatement selectLedgerPageKeys
Select a page of object keys from a token as of a ledger sequence.
Definition CassandraSchema.hpp:116
PreparedStatement updateLedgerRange
Conditionally update a ledger_range sequence (LWT on previous value or null).
Definition CassandraSchema.hpp:40
PreparedStatement selectLedgerPage
Select a page of objects and keys from a token as of a ledger sequence.
Definition CassandraSchema.hpp:136
PreparedStatement selectAccountFromBeginning
Select a page of distinct accounts from the start of account_tx.
Definition CassandraSchema.hpp:80
PreparedStatement selectAccountFromToken
Select a page of distinct accounts in account_tx after a given account.
Definition CassandraSchema.hpp:98
PreparedStatement selectNFTIDsByIssuer
Select a page of NFT IDs for an issuer after a (taxon, token ID) cursor.
Definition CassandraSchema.hpp:61