Clio  develop
The XRP Ledger API server.
Loading...
Searching...
No Matches
CassandraSchema.hpp
1//------------------------------------------------------------------------------
2/*
3 This file is part of clio: https://github.com/XRPLF/clio
4 Copyright (c) 2025, the clio developers.
5
6 Permission to use, copy, modify, and distribute this software for any
7 purpose with or without fee is hereby granted, provided that the above
8 copyright notice and this permission notice appear in all copies.
9
10 THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17*/
18//==============================================================================
19
20#pragma once
21
22#include "data/cassandra/Concepts.hpp"
23#include "data/cassandra/Handle.hpp"
24#include "data/cassandra/Schema.hpp"
25#include "data/cassandra/SettingsProvider.hpp"
26#include "data/cassandra/Types.hpp"
27#include "util/log/Logger.hpp"
28
29#include <boost/json/string.hpp>
30#include <fmt/compile.h>
31
32#include <functional>
33#include <memory>
34
35namespace data::cassandra {
36
40template <SomeSettingsProvider SettingsProviderType>
41class CassandraSchema : public Schema<SettingsProvider> {
42 using Schema::Schema;
43
44public:
50 struct CassandraStatements : public Schema<SettingsProvider>::Statements {
51 using Schema<SettingsProvider>::Statements::Statements;
52
53 //
54 // Update (and "delete") queries
55 //
56 PreparedStatement updateLedgerRange = [this]() {
57 return handle_.get().prepare(
58 fmt::format(
59 R"(
60 UPDATE {}
61 SET sequence = ?
62 WHERE is_latest = ?
63 IF sequence IN (?, null)
64 )",
65 qualifiedTableName(settingsProvider_.get(), "ledger_range")
66 )
67 );
68 }();
69
70 //
71 // Select queries
72 //
73
74 PreparedStatement selectNFTIDsByIssuer = [this]() {
75 return handle_.get().prepare(
76 fmt::format(
77 R"(
78 SELECT token_id
79 FROM {}
80 WHERE issuer = ?
81 AND (taxon, token_id) > ?
82 ORDER BY taxon ASC, token_id ASC
83 LIMIT ?
84 )",
85 qualifiedTableName(settingsProvider_.get(), "issuer_nf_tokens_v2")
86 )
87 );
88 }();
89
90 PreparedStatement selectAccountFromBeginning = [this]() {
91 return handle_.get().prepare(
92 fmt::format(
93 R"(
94 SELECT account
95 FROM {}
96 WHERE token(account) > 0
97 PER PARTITION LIMIT 1
98 LIMIT ?
99 )",
100 qualifiedTableName(settingsProvider_.get(), "account_tx")
101 )
102 );
103 }();
104
105 PreparedStatement selectAccountFromToken = [this]() {
106 return handle_.get().prepare(
107 fmt::format(
108 R"(
109 SELECT account
110 FROM {}
111 WHERE token(account) > token(?)
112 PER PARTITION LIMIT 1
113 LIMIT ?
114 )",
115 qualifiedTableName(settingsProvider_.get(), "account_tx")
116 )
117 );
118 }();
119
120 PreparedStatement selectLedgerPageKeys = [this]() {
121 return handle_.get().prepare(
122 fmt::format(
123 R"(
124 SELECT key
125 FROM {}
126 WHERE TOKEN(key) >= ?
127 AND sequence <= ?
128 PER PARTITION LIMIT 1
129 LIMIT ?
130 ALLOW FILTERING
131 )",
132 qualifiedTableName(settingsProvider_.get(), "objects")
133 )
134 );
135 }();
136
137 PreparedStatement selectLedgerPage = [this]() {
138 return handle_.get().prepare(
139 fmt::format(
140 R"(
141 SELECT object, key
142 FROM {}
143 WHERE TOKEN(key) >= ?
144 AND sequence <= ?
145 PER PARTITION LIMIT 1
146 LIMIT ?
147 ALLOW FILTERING
148 )",
149 qualifiedTableName(settingsProvider_.get(), "objects")
150 )
151 );
152 }();
153 };
154
155 void
156 prepareStatements(Handle const& handle) override
157 {
158 LOG(log_.info()) << "Preparing cassandra statements";
159 statements_ = std::make_unique<CassandraStatements>(settingsProvider_, handle);
160 LOG(log_.info()) << "Finished preparing statements";
161 }
162
168 std::unique_ptr<CassandraStatements> const&
170 {
171 return statements_;
172 }
173
174private:
175 std::unique_ptr<CassandraStatements> statements_{nullptr};
176};
177
178} // namespace data::cassandra
Manages the DB schema and provides access to prepared statements.
Definition CassandraSchema.hpp:41
std::unique_ptr< CassandraStatements > const & operator->() const
Provides access to statements.
Definition CassandraSchema.hpp:169
void prepareStatements(Handle const &handle) override
Recreates the prepared statements.
Definition CassandraSchema.hpp:156
Represents a handle to the cassandra database cluster.
Definition Handle.hpp:46
Statements(SettingsProviderType const &settingsProvider, Handle const &handle)
Construct a new Statements object.
Definition Schema.hpp:351
Schema(SettingsProviderType const &settingsProvider)
Shared Schema's between all Schema classes (Cassandra and Keyspace)
Definition Schema.hpp:68
Provides settings for BasicCassandraBackend.
Definition SettingsProvider.hpp:35
This namespace implements a wrapper for the Cassandra C++ driver.
Definition CassandraBackendFamily.hpp:66
std::string qualifiedTableName(SettingsProviderType const &provider, std::string_view name)
Returns the table name qualified with the keyspace and table prefix.
Definition Schema.hpp:46
Construct a new Cassandra Schema object.
Definition CassandraSchema.hpp:50