Clio  develop
The XRP Ledger API server.
Loading...
Searching...
No Matches
KeyspaceSchema.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 KeyspaceSchema : public Schema<SettingsProvider> {
42public:
43 using Schema::Schema;
44
50 struct KeyspaceStatements : public Schema<SettingsProvider>::Statements {
51 using Schema<SettingsProvider>::Statements::Statements;
52
53 //
54 // Insert queries
55 //
56 PreparedStatement insertLedgerRange = [this]() {
57 return handle_.get().prepare(
58 fmt::format(
59 R"(
60 INSERT INTO {} (is_latest, sequence) VALUES (?, ?) IF NOT EXISTS
61 )",
62 qualifiedTableName(settingsProvider_.get(), "ledger_range")
63 )
64 );
65 }();
66
67 //
68 // Update (and "delete") queries
69 //
70 PreparedStatement updateLedgerRange = [this]() {
71 return handle_.get().prepare(
72 fmt::format(
73 R"(
74 UPDATE {}
75 SET sequence = ?
76 WHERE is_latest = ?
77 IF sequence = ?
78 )",
79 qualifiedTableName(settingsProvider_.get(), "ledger_range")
80 )
81 );
82 }();
83
84 PreparedStatement selectLedgerRange = [this]() {
85 return handle_.get().prepare(
86 fmt::format(
87 R"(
88 SELECT sequence
89 FROM {}
90 WHERE is_latest in (True, False)
91 )",
92 qualifiedTableName(settingsProvider_.get(), "ledger_range")
93 )
94 );
95 }();
96
97 //
98 // Select queries
99 //
100 PreparedStatement selectNFTsAfterTaxonKeyspaces = [this]() {
101 return handle_.get().prepare(
102 fmt::format(
103 R"(
104 SELECT token_id
105 FROM {}
106 WHERE issuer = ?
107 AND taxon > ?
108 ORDER BY taxon ASC, token_id ASC
109 LIMIT ?
110 )",
111 qualifiedTableName(settingsProvider_.get(), "issuer_nf_tokens_v2")
112 )
113 );
114 }();
115 };
116
117 void
118 prepareStatements(Handle const& handle) override
119 {
120 LOG(log_.info()) << "Preparing aws keyspace statements";
121 statements_ = std::make_unique<KeyspaceStatements>(settingsProvider_, handle);
122 LOG(log_.info()) << "Finished preparing statements";
123 }
124
130 std::unique_ptr<KeyspaceStatements> const&
132 {
133 return statements_;
134 }
135
136private:
137 std::unique_ptr<KeyspaceStatements> statements_{nullptr};
138};
139
140} // namespace data::cassandra
Represents a handle to the cassandra database cluster.
Definition Handle.hpp:46
Manages the DB schema and provides access to prepared statements.
Definition KeyspaceSchema.hpp:41
Schema(SettingsProviderType const &settingsProvider)
Shared Schema's between all Schema classes (Cassandra and Keyspace)
Definition Schema.hpp:68
std::unique_ptr< KeyspaceStatements > const & operator->() const
Provides access to statements.
Definition KeyspaceSchema.hpp:131
void prepareStatements(Handle const &handle) override
Recreates the prepared statements.
Definition KeyspaceSchema.hpp:118
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 Keyspace Schema object.
Definition KeyspaceSchema.hpp:50