Clio  develop
The XRP Ledger API server.
Loading...
Searching...
No Matches
KeyspaceSchema.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 KeyspaceSchema : public Schema<SettingsProvider> {
25public:
26 using Schema::Schema;
27
31 struct KeyspaceStatements : public Schema<SettingsProvider>::Statements {
32 using Schema<SettingsProvider>::Statements::Statements;
33
34 //
35 // Insert queries
36 //
41 return handle_.get().prepare(
42 fmt::format(
43 R"(
44 INSERT INTO {} (is_latest, sequence) VALUES (?, ?) IF NOT EXISTS
45 )",
46 qualifiedTableName(settingsProvider_.get(), "ledger_range")
47 )
48 );
49 }();
50
51 //
52 // Update (and "delete") queries
53 //
58 return handle_.get().prepare(
59 fmt::format(
60 R"(
61 UPDATE {}
62 SET sequence = ?
63 WHERE is_latest = ?
64 IF sequence = ?
65 )",
66 qualifiedTableName(settingsProvider_.get(), "ledger_range")
67 )
68 );
69 }();
70
75 return handle_.get().prepare(
76 fmt::format(
77 R"(
78 SELECT sequence
79 FROM {}
80 WHERE is_latest in (True, False)
81 )",
82 qualifiedTableName(settingsProvider_.get(), "ledger_range")
83 )
84 );
85 }();
86
87 //
88 // Select queries
89 //
94 return handle_.get().prepare(
95 fmt::format(
96 R"(
97 SELECT token_id
98 FROM {}
99 WHERE issuer = ?
100 AND taxon > ?
101 ORDER BY taxon ASC, token_id ASC
102 LIMIT ?
103 )",
104 qualifiedTableName(settingsProvider_.get(), "issuer_nf_tokens_v2")
105 )
106 );
107 }();
108 };
109
115 void
116 prepareStatements(Handle const& handle) override
117 {
118 LOG(log_.info()) << "Preparing aws keyspace statements";
119 statements_ = std::make_unique<KeyspaceStatements>(settingsProvider_, handle);
120 LOG(log_.info()) << "Finished preparing statements";
121 }
122
128 std::unique_ptr<KeyspaceStatements> const&
130 {
131 return statements_;
132 }
133
134private:
135 std::unique_ptr<KeyspaceStatements> statements_{nullptr};
136};
137
138} // namespace data::cassandra
Represents a handle to the cassandra database cluster.
Definition Handle.hpp:27
Manages the DB schema and provides access to prepared statements.
Definition KeyspaceSchema.hpp:24
Schema(SettingsProviderType const &settingsProvider)
Shared Schema's between all Schema classes (Cassandra and Keyspace).
Definition Schema.hpp:56
std::unique_ptr< KeyspaceStatements > const & operator->() const
Provides access to statements.
Definition KeyspaceSchema.hpp:129
void prepareStatements(Handle const &handle) override
Recreates the prepared statements, including the Keyspace-specific ones.
Definition KeyspaceSchema.hpp:116
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 Keyspace Schema object.
Definition KeyspaceSchema.hpp:31
PreparedStatement insertLedgerRange
Insert a ledger_range row (min or max sequence) if it does not exist.
Definition KeyspaceSchema.hpp:40
PreparedStatement selectNFTsAfterTaxonKeyspaces
Select a page of NFT IDs for an issuer with taxon above a given one.
Definition KeyspaceSchema.hpp:93
PreparedStatement updateLedgerRange
Conditionally update a ledger_range sequence (LWT on the previous value).
Definition KeyspaceSchema.hpp:57
PreparedStatement selectLedgerRange
Select both the min and max sequences from ledger_range.
Definition KeyspaceSchema.hpp:74