Clio  develop
The XRP Ledger API server.
Loading...
Searching...
No Matches
Handle.hpp
1#pragma once
2
3#include "data/cassandra/Error.hpp"
4#include "data/cassandra/Types.hpp"
5#include "data/cassandra/impl/Batch.hpp"
6#include "data/cassandra/impl/Cluster.hpp"
7#include "data/cassandra/impl/Future.hpp"
8#include "data/cassandra/impl/ManagedObject.hpp"
9#include "data/cassandra/impl/Result.hpp"
10#include "data/cassandra/impl/Session.hpp"
11#include "data/cassandra/impl/Statement.hpp"
12
13#include <cassandra.h>
14
15#include <functional>
16#include <string_view>
17#include <vector>
18
22namespace data::cassandra {
23
27class Handle {
28 impl::Cluster cluster_;
29 impl::Session session_;
30
31public:
39
45 explicit Handle(Settings clusterSettings = Settings::defaultSettings());
46
52 explicit Handle(std::string_view contactPoints);
53
57 ~Handle();
58
62 Handle(Handle&&) = default;
63
69 [[nodiscard]] FutureType
70 asyncConnect() const;
71
79 [[nodiscard]] MaybeErrorType
80 connect() const;
81
88 [[nodiscard]] FutureType
89 asyncConnect(std::string_view keyspace) const;
90
99 [[nodiscard]] MaybeErrorType
100 connect(std::string_view keyspace) const;
101
107 [[nodiscard]] FutureType
108 asyncDisconnect() const;
109
117 [[nodiscard]] [[maybe_unused]] MaybeErrorType
118 disconnect() const;
119
126 [[nodiscard]] FutureType
127 asyncReconnect(std::string_view keyspace) const;
128
137 [[nodiscard]] MaybeErrorType
138 reconnect(std::string_view keyspace) const;
139
148 template <typename... Args>
149 [[nodiscard]] FutureType
150 asyncExecute(std::string_view query, Args&&... args) const
151 {
152 auto const statement = StatementType{query, std::forward<Args>(args)...};
153 return cass_session_execute(session_, statement);
154 }
155
166 template <typename... Args>
167 [[nodiscard]] [[maybe_unused]] ResultOrErrorType
168 execute(std::string_view query, Args&&... args) const
169 {
170 return asyncExecute<Args...>(query, std::forward<Args>(args)...).get();
171 }
172
183 [[nodiscard]] std::vector<FutureType>
184 asyncExecuteEach(std::vector<StatementType> const& statements) const;
185
194 [[nodiscard]] [[maybe_unused]] MaybeErrorType
195 executeEach(std::vector<StatementType> const& statements) const;
196
205 template <typename... Args>
206 [[nodiscard]] FutureType
207 asyncExecute(PreparedStatementType const& statement, Args&&... args) const
208 {
209 auto const bound = statement.bind<Args...>(std::forward<Args>(args)...);
210 return cass_session_execute(session_, bound);
211 }
212
223 template <typename... Args>
224 [[maybe_unused]] ResultOrErrorType
225 execute(PreparedStatementType const& statement, Args&&... args) const
226 {
227 return asyncExecute<Args...>(statement, std::forward<Args>(args)...).get();
228 }
229
236 [[nodiscard]] FutureType
237 asyncExecute(StatementType const& statement) const;
238
246 [[nodiscard]] FutureWithCallbackType
247 asyncExecute(StatementType const& statement, std::function<void(ResultOrErrorType)>&& cb) const;
248
257 [[nodiscard]] [[maybe_unused]] ResultOrErrorType
258 execute(StatementType const& statement) const;
259
266 [[nodiscard]] FutureType
267 asyncExecute(std::vector<StatementType> const& statements) const;
268
277 [[nodiscard]] [[maybe_unused]] MaybeErrorType
278 execute(std::vector<StatementType> const& statements) const;
279
288 [[nodiscard]] FutureWithCallbackType
290 std::vector<StatementType> const& statements,
291 std::function<void(ResultOrErrorType)>&& cb
292 ) const;
293
301 [[nodiscard]] PreparedStatementType
302 prepare(std::string_view query) const;
303};
304
316template <typename... Types>
317[[nodiscard]] impl::ResultExtractor<Types...>
319{
320 return {result};
321}
322
323} // namespace data::cassandra
PreparedStatement PreparedStatementType
A prepared statement.
Definition Handle.hpp:37
FutureWithCallback FutureWithCallbackType
Future that invokes a callback.
Definition Handle.hpp:34
MaybeErrorType connect() const
Synchronous version of the above.
Definition Handle.cpp:37
MaybeErrorType reconnect(std::string_view keyspace) const
Synchronous version of the above.
Definition Handle.cpp:78
MaybeErrorType disconnect() const
Synchronous version of the above.
Definition Handle.cpp:61
FutureType asyncReconnect(std::string_view keyspace) const
Reconnect to the specified keyspace asynchronously.
Definition Handle.cpp:67
ResultOrError ResultOrErrorType
A query result or an error.
Definition Handle.hpp:32
Handle(Handle &&)=default
Move is supported.
FutureType asyncDisconnect() const
Disconnect from the cluster asynchronously.
Definition Handle.cpp:55
std::vector< FutureType > asyncExecuteEach(std::vector< StatementType > const &statements) const
Execute each of the statements asynchronously.
Definition Handle.cpp:84
MaybeErrorType executeEach(std::vector< StatementType > const &statements) const
Synchronous version of the above.
Definition Handle.cpp:94
ResultOrErrorType execute(PreparedStatementType const &statement, Args &&... args) const
Synchronous version of the above.
Definition Handle.hpp:225
Statement StatementType
A simple or bound statement.
Definition Handle.hpp:36
FutureType asyncExecute(std::string_view query, Args &&... args) const
Execute a simple query with optional args asynchronously.
Definition Handle.hpp:150
ResultOrErrorType execute(std::string_view query, Args &&... args) const
Synchronous version of the above.
Definition Handle.hpp:168
Result ResultType
A query result.
Definition Handle.hpp:38
Future FutureType
Future of an asynchronous query.
Definition Handle.hpp:35
FutureType asyncConnect() const
Connect to the cluster asynchronously.
Definition Handle.cpp:31
~Handle()
Disconnects gracefully if possible.
Definition Handle.cpp:25
Handle(Settings clusterSettings=Settings::defaultSettings())
Construct a new handle from a Settings object.
Definition Handle.cpp:16
MaybeError MaybeErrorType
Success or an error.
Definition Handle.hpp:33
PreparedStatementType prepare(std::string_view query) const
Prepare a statement.
Definition Handle.cpp:149
FutureType asyncExecute(PreparedStatementType const &statement, Args &&... args) const
Execute a prepared statement with optional args asynchronously.
Definition Handle.hpp:207
Definition Cluster.hpp:157
Statement bind(Args &&... args) const
Bind the given arguments and produce a ready to execute Statement.
Definition Statement.hpp:174
Definition Session.hpp:9
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
std::expected< void, CassandraError > MaybeError
Success or a database error.
Definition Types.hpp:65
impl::Future Future
Future of an asynchronous query.
Definition Types.hpp:24
impl::Statement Statement
A simple or bound statement.
Definition Types.hpp:27
impl::PreparedStatement PreparedStatement
A prepared statement.
Definition Types.hpp:28
std::expected< Result, CassandraError > ResultOrError
A query result or an error.
Definition Types.hpp:66
impl::Settings Settings
All Cassandra backend settings.
Definition Types.hpp:23
impl::Result Result
Result of a query.
Definition Types.hpp:26
impl::FutureWithCallback FutureWithCallback
Future that invokes a callback.
Definition Types.hpp:25
static Settings defaultSettings()
Returns the default settings.
Definition Cluster.hpp:151