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:
32 using ResultOrErrorType = ResultOrError;
33 using MaybeErrorType = MaybeError;
34 using FutureWithCallbackType = FutureWithCallback;
35 using FutureType = Future;
36 using StatementType = Statement;
37 using PreparedStatementType = PreparedStatement;
38 using ResultType = Result;
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 [[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
147 template <typename... Args>
148 [[nodiscard]] FutureType
149 asyncExecute(std::string_view query, Args&&... args) const
150 {
151 auto statement = StatementType{query, std::forward<Args>(args)...};
152 return cass_session_execute(session_, statement);
153 }
154
164 template <typename... Args>
165 [[maybe_unused]] ResultOrErrorType
166 execute(std::string_view query, Args&&... args) const
167 {
168 return asyncExecute<Args...>(query, std::forward<Args>(args)...).get();
169 }
170
181 [[nodiscard]] std::vector<FutureType>
182 asyncExecuteEach(std::vector<StatementType> const& statements) const;
183
192 [[maybe_unused]] MaybeErrorType
193 executeEach(std::vector<StatementType> const& statements) const;
194
202 template <typename... Args>
203 [[nodiscard]] FutureType
204 asyncExecute(PreparedStatementType const& statement, Args&&... args) const
205 {
206 auto bound = statement.bind<Args...>(std::forward<Args>(args)...);
207 return cass_session_execute(session_, bound);
208 }
209
219 template <typename... Args>
220 [[maybe_unused]] ResultOrErrorType
221 execute(PreparedStatementType const& statement, Args&&... args) const
222 {
223 return asyncExecute<Args...>(statement, std::forward<Args>(args)...).get();
224 }
225
232 [[nodiscard]] FutureType
233 asyncExecute(StatementType const& statement) const;
234
242 [[nodiscard]] FutureWithCallbackType
243 asyncExecute(StatementType const& statement, std::function<void(ResultOrErrorType)>&& cb) const;
244
253 [[maybe_unused]] ResultOrErrorType
254 execute(StatementType const& statement) const;
255
262 [[nodiscard]] FutureType
263 asyncExecute(std::vector<StatementType> const& statements) const;
264
273 [[maybe_unused]] MaybeErrorType
274 execute(std::vector<StatementType> const& statements) const;
275
284 [[nodiscard]] FutureWithCallbackType
286 std::vector<StatementType> const& statements,
287 std::function<void(ResultOrErrorType)>&& cb
288 ) const;
289
297 [[nodiscard]] PreparedStatementType
298 prepare(std::string_view query) const;
299};
300
312template <typename... Types>
313[[nodiscard]] impl::ResultExtractor<Types...>
314extract(Handle::ResultType const& result)
315{
316 return {result};
317}
318
319} // namespace data::cassandra
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
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:221
FutureType asyncExecute(std::string_view query, Args &&... args) const
Execute a simple query with optional args asynchronously.
Definition Handle.hpp:149
ResultOrErrorType execute(std::string_view query, Args &&... args) const
Synchronous version of the above.
Definition Handle.hpp:166
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
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:204
Definition Cluster.hpp:129
Statement bind(Args &&... args) const
Bind the given arguments and produce a ready to execute Statement.
Definition Statement.hpp:175
Definition Session.hpp:9
This namespace implements a wrapper for the Cassandra C++ driver.
Definition CassandraBackendFamily.hpp:47
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:314
static Settings defaultSettings()
Returns the default settings.
Definition Cluster.hpp:123