Clio develop
The XRP Ledger API server.
Loading...
Searching...
No Matches
Handle.hpp
1//------------------------------------------------------------------------------
2/*
3 This file is part of clio: https://github.com/XRPLF/clio
4 Copyright (c) 2023, 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/Error.hpp"
23#include "data/cassandra/Types.hpp"
24#include "data/cassandra/impl/Batch.hpp"
25#include "data/cassandra/impl/Cluster.hpp"
26#include "data/cassandra/impl/Future.hpp"
27#include "data/cassandra/impl/ManagedObject.hpp"
28#include "data/cassandra/impl/Result.hpp"
29#include "data/cassandra/impl/Session.hpp"
30#include "data/cassandra/impl/Statement.hpp"
31
32#include <cassandra.h>
33
34#include <functional>
35#include <string_view>
36#include <vector>
37
41namespace data::cassandra {
42
46class Handle {
47 impl::Cluster cluster_;
48 impl::Session session_;
49
50public:
51 using ResultOrErrorType = ResultOrError;
52 using MaybeErrorType = MaybeError;
54 using FutureType = Future;
57 using ResultType = Result;
58
64 explicit Handle(Settings clusterSettings = Settings::defaultSettings());
65
71 explicit Handle(std::string_view contactPoints);
72
76 ~Handle();
77
81 Handle(Handle&&) = default;
82
88 [[nodiscard]] FutureType
89 asyncConnect() const;
90
98 [[nodiscard]] MaybeErrorType
99 connect() const;
100
107 [[nodiscard]] FutureType
108 asyncConnect(std::string_view keyspace) const;
109
118 [[nodiscard]] MaybeErrorType
119 connect(std::string_view keyspace) const;
120
126 [[nodiscard]] FutureType
127 asyncDisconnect() const;
128
136 [[maybe_unused]] MaybeErrorType
137 disconnect() const;
138
145 [[nodiscard]] FutureType
146 asyncReconnect(std::string_view keyspace) const;
147
156 [[nodiscard]] MaybeErrorType
157 reconnect(std::string_view keyspace) const;
158
166 template <typename... Args>
167 [[nodiscard]] FutureType
168 asyncExecute(std::string_view query, Args&&... args) const
169 {
170 auto statement = StatementType{query, std::forward<Args>(args)...};
171 return cass_session_execute(session_, statement);
172 }
173
183 template <typename... Args>
184 [[maybe_unused]] ResultOrErrorType
185 execute(std::string_view query, Args&&... args) const
186 {
187 return asyncExecute<Args...>(query, std::forward<Args>(args)...).get();
188 }
189
200 [[nodiscard]] std::vector<FutureType>
201 asyncExecuteEach(std::vector<StatementType> const& statements) const;
202
211 [[maybe_unused]] MaybeErrorType
212 executeEach(std::vector<StatementType> const& statements) const;
213
221 template <typename... Args>
222 [[nodiscard]] FutureType
223 asyncExecute(PreparedStatementType const& statement, Args&&... args) const
224 {
225 auto bound = statement.bind<Args...>(std::forward<Args>(args)...);
226 return cass_session_execute(session_, bound);
227 }
228
238 template <typename... Args>
239 [[maybe_unused]] ResultOrErrorType
240 execute(PreparedStatementType const& statement, Args&&... args) const
241 {
242 return asyncExecute<Args...>(statement, std::forward<Args>(args)...).get();
243 }
244
251 [[nodiscard]] FutureType
252 asyncExecute(StatementType const& statement) const;
253
261 [[nodiscard]] FutureWithCallbackType
262 asyncExecute(StatementType const& statement, std::function<void(ResultOrErrorType)>&& cb) const;
263
272 [[maybe_unused]] ResultOrErrorType
273 execute(StatementType const& statement) const;
274
281 [[nodiscard]] FutureType
282 asyncExecute(std::vector<StatementType> const& statements) const;
283
292 [[maybe_unused]] MaybeErrorType
293 execute(std::vector<StatementType> const& statements) const;
294
302 [[nodiscard]] FutureWithCallbackType
303 asyncExecute(std::vector<StatementType> const& statements, std::function<void(ResultOrErrorType)>&& cb) const;
304
312 [[nodiscard]] PreparedStatementType
313 prepare(std::string_view query) const;
314};
315
327template <typename... Types>
328[[nodiscard]] impl::ResultExtractor<Types...>
330{
331 return {result};
332}
333
334} // namespace data::cassandra
Represents a handle to the cassandra database cluster.
Definition Handle.hpp:46
MaybeErrorType connect() const
Synchonous version of the above.
Definition Handle.cpp:55
MaybeErrorType reconnect(std::string_view keyspace) const
Synchonous version of the above.
Definition Handle.cpp:93
MaybeErrorType disconnect() const
Synchonous version of the above.
Definition Handle.cpp:79
FutureType asyncReconnect(std::string_view keyspace) const
Reconnect to the the specified keyspace asynchronously.
Definition Handle.cpp:85
Handle(Handle &&)=default
Move is supported.
FutureType asyncDisconnect() const
Disconnect from the cluster asynchronously.
Definition Handle.cpp:73
std::vector< FutureType > asyncExecuteEach(std::vector< StatementType > const &statements) const
Execute each of the statements asynchronously.
Definition Handle.cpp:99
MaybeErrorType executeEach(std::vector< StatementType > const &statements) const
Synchonous version of the above.
Definition Handle.cpp:109
ResultOrErrorType execute(PreparedStatementType const &statement, Args &&... args) const
Synchonous version of the above.
Definition Handle.hpp:240
FutureType asyncExecute(std::string_view query, Args &&... args) const
Execute a simple query with optional args asynchronously.
Definition Handle.hpp:168
ResultOrErrorType execute(std::string_view query, Args &&... args) const
Synchonous version of the above.
Definition Handle.hpp:185
FutureType asyncConnect() const
Connect to the cluster asynchronously.
Definition Handle.cpp:49
~Handle()
Disconnects gracefully if possible.
Definition Handle.cpp:43
Handle(Settings clusterSettings=Settings::defaultSettings())
Construct a new handle from a Settings object.
Definition Handle.cpp:35
PreparedStatementType prepare(std::string_view query) const
Prepare a statement.
Definition Handle.cpp:156
FutureType asyncExecute(PreparedStatementType const &statement, Args &&... args) const
Execute a prepared statement with optional args asynchronously.
Definition Handle.hpp:223
Definition Cluster.hpp:124
Represents a prepared statement on the DB side.
Definition Statement.hpp:155
Statement bind(Args &&... args) const
Bind the given arguments and produce a ready to execute Statement.
Definition Statement.hpp:171
Definition Session.hpp:28
Definition Statement.hpp:45
This namespace implements a wrapper for the Cassandra C++ driver.
Definition Concepts.hpp:37
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:329
Definition Future.hpp:32
Definition Result.hpp:110
Bundles all cassandra settings in one place.
Definition Cluster.hpp:43
static Settings defaultSettings()
Returns the default settings.
Definition Cluster.hpp:118