Skip to content

Commit 8725c12

Browse files
committed
refactor scylla: optimize statements and cql builder
1 parent 6be55ed commit 8725c12

27 files changed

+830
-459
lines changed

mongo/src/storages/mongo/stats.cpp

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -187,9 +187,7 @@ void OperationStopwatch::Account(ErrorType error_type) noexcept {
187187
UASSERT(ms >= 0);
188188
stats_item->timings.GetCurrentCounter().Account(ms);
189189
stats_item->timings_sum += utils::statistics::Rate{static_cast<utils::statistics::Rate::ValueType>(ms)};
190-
} catch (const std::exception&) {
191-
// ignore
192-
}
190+
} catch (const std::exception&) {}
193191
}
194192

195193
ConnectionWaitStopwatch::ConnectionWaitStopwatch(std::shared_ptr<PoolConnectStatistics> stats_ptr)
@@ -201,9 +199,7 @@ ConnectionWaitStopwatch::~ConnectionWaitStopwatch() {
201199
try {
202200
++stats_ptr_->requested;
203201
stats_ptr_->request_timings_agg.GetCurrentCounter().Account(GetMilliseconds(scope_time_.Reset()));
204-
} catch (const std::exception&) {
205-
// ignore
206-
}
202+
} catch (const std::exception&) {}
207203
}
208204

209205
ConnectionThrottleStopwatch::ConnectionThrottleStopwatch(std::shared_ptr<PoolConnectStatistics> stats_ptr)
@@ -225,9 +221,7 @@ void ConnectionThrottleStopwatch::Stop() noexcept {
225221

226222
try {
227223
stats_ptr->queue_wait_timings_agg.GetCurrentCounter().Account(GetMilliseconds(scope_time_.Reset()));
228-
} catch (const std::exception&) {
229-
// ignore
230-
}
224+
} catch (const std::exception&) {}
231225
}
232226

233227
} // namespace storages::mongo::stats

scripts/scylla/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ docker exec -it scylla-dev-1 bash
3939
/opt/userver/scripts/scylla/entrypoint.sh
4040
```
4141

42-
This builds and runs the `samples/scylla_service` sample.
42+
This builds current userver and runs the `samples/scylla_service` sample.
4343

4444
---
4545

scylla/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# userver: Scylla Driver
Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
#pragma once
22

3-
/// @file userver/storages/scylla.hpp
4-
/// @brief Include-all header for ScyllaDB client
5-
63
#include <userver/storages/scylla/component.hpp>
74
#include <userver/storages/scylla/exception.hpp>
85
#include <userver/storages/scylla/session.hpp>
@@ -13,7 +10,6 @@
1310

1411
USERVER_NAMESPACE_BEGIN
1512

16-
// ScyllaDB client
1713
namespace storages::scylla {}
1814

19-
USERVER_NAMESPACE_END
15+
USERVER_NAMESPACE_END

scylla/include/userver/storages/scylla/component.hpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,10 @@ class Scylla : public ComponentBase {
2727
std::string dbalias_;
2828
storages::scylla::SessionPtr session_;
2929

30-
3130
utils::statistics::Entry statistics_entry_;
3231
concurrent::AsyncEventSubscriberScope secdist_subscriber_;
3332
};
3433

35-
} // namespace components
34+
}
3635

37-
USERVER_NAMESPACE_END
36+
USERVER_NAMESPACE_END

scylla/include/userver/storages/scylla/exception.hpp

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
#pragma once
22

3-
/// @file userver/storages/scylla/exception.hpp
4-
/// @brief ScyllaDB-specific exceptions
5-
63
#include <chrono>
74

85
#include <userver/utils/traceful_exception.hpp>
@@ -66,6 +63,19 @@ class PoolOverloadException : public ScyllaException {
6663
using ScyllaException::ScyllaException;
6764
};
6865

69-
} // namespace storages::scylla
66+
class CancelledException : public ScyllaException {
67+
public:
68+
struct ByDeadlinePropagation final {};
69+
70+
using ScyllaException::ScyllaException;
71+
explicit CancelledException(ByDeadlinePropagation);
72+
73+
bool IsByDeadlinePropagation() const noexcept { return by_deadline_propagation_; }
74+
75+
private:
76+
bool by_deadline_propagation_{false};
77+
};
78+
79+
}
7080

71-
USERVER_NAMESPACE_END
81+
USERVER_NAMESPACE_END

scylla/include/userver/storages/scylla/operations.hpp

Lines changed: 191 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ USERVER_NAMESPACE_BEGIN
1111

1212
namespace storages::scylla::impl::driver {
1313
class DriverTableImpl;
14-
} // namespace storages::scylla::impl::driver
14+
}
1515

1616
namespace storages::scylla::operations {
1717

@@ -83,6 +83,194 @@ class SelectOne {
8383
utils::FastPimpl<Impl, kSize, kAlignment, false> impl_;
8484
};
8585

86-
} // namespace storages::scylla::operations
86+
class DeleteOne {
87+
public:
88+
DeleteOne();
89+
~DeleteOne();
90+
91+
DeleteOne(const DeleteOne&);
92+
DeleteOne(DeleteOne&&) noexcept;
93+
DeleteOne& operator=(const DeleteOne&);
94+
DeleteOne& operator=(DeleteOne&&) noexcept;
95+
96+
void WhereString(std::string column_name, std::string value);
97+
void WhereInt32(std::string column_name, int32_t value);
98+
void WhereInt64(std::string column_name, int64_t value);
99+
void WhereBool(std::string column_name, bool value);
100+
void WhereFloat(std::string column_name, float value);
101+
void WhereDouble(std::string column_name, double value);
102+
103+
private:
104+
friend class storages::scylla::impl::driver::DriverTableImpl;
105+
106+
struct Condition {
107+
std::string column_name;
108+
std::variant<std::string, int32_t, int64_t, bool, float, double> value;
109+
};
110+
111+
class Impl;
112+
static constexpr size_t kSize = 64;
113+
static constexpr size_t kAlignment = 8;
114+
utils::FastPimpl<Impl, kSize, kAlignment, false> impl_;
115+
};
116+
117+
class SelectMany {
118+
public:
119+
using Value = std::variant<std::string, int32_t, int64_t, bool, float, double>;
120+
using Row = std::vector<std::pair<std::string, Value>>;
121+
using ResultSet = std::vector<Row>;
122+
123+
SelectMany();
124+
~SelectMany();
125+
126+
SelectMany(const SelectMany&);
127+
SelectMany(SelectMany&&) noexcept;
128+
SelectMany& operator=(const SelectMany&);
129+
SelectMany& operator=(SelectMany&&) noexcept;
130+
131+
void AddColumn(std::string column_name);
132+
void AddAllColumns();
133+
134+
void WhereString(std::string column_name, std::string value);
135+
void WhereInt32(std::string column_name, int32_t value);
136+
void WhereInt64(std::string column_name, int64_t value);
137+
void WhereBool(std::string column_name, bool value);
138+
void WhereFloat(std::string column_name, float value);
139+
void WhereDouble(std::string column_name, double value);
140+
141+
void SetLimit(size_t limit);
142+
143+
private:
144+
friend class storages::scylla::impl::driver::DriverTableImpl;
145+
146+
struct Condition {
147+
std::string column_name;
148+
std::variant<std::string, int32_t, int64_t, bool, float, double> value;
149+
};
150+
151+
class Impl;
152+
static constexpr size_t kSize = 128;
153+
static constexpr size_t kAlignment = 8;
154+
utils::FastPimpl<Impl, kSize, kAlignment, false> impl_;
155+
};
156+
157+
class UpdateOne {
158+
public:
159+
UpdateOne();
160+
~UpdateOne();
161+
162+
UpdateOne(const UpdateOne&);
163+
UpdateOne(UpdateOne&&) noexcept;
164+
UpdateOne& operator=(const UpdateOne&);
165+
UpdateOne& operator=(UpdateOne&&) noexcept;
166+
167+
void SetString(std::string column_name, std::string value);
168+
void SetInt32(std::string column_name, int32_t value);
169+
void SetInt64(std::string column_name, int64_t value);
170+
void SetBool(std::string column_name, bool value);
171+
void SetFloat(std::string column_name, float value);
172+
void SetDouble(std::string column_name, double value);
173+
174+
void WhereString(std::string column_name, std::string value);
175+
void WhereInt32(std::string column_name, int32_t value);
176+
void WhereInt64(std::string column_name, int64_t value);
177+
void WhereBool(std::string column_name, bool value);
178+
void WhereFloat(std::string column_name, float value);
179+
void WhereDouble(std::string column_name, double value);
180+
181+
private:
182+
friend class storages::scylla::impl::driver::DriverTableImpl;
183+
184+
struct Assignment {
185+
std::string column_name;
186+
std::variant<std::string, int32_t, int64_t, bool, float, double> value;
187+
};
188+
struct Condition {
189+
std::string column_name;
190+
std::variant<std::string, int32_t, int64_t, bool, float, double> value;
191+
};
192+
193+
class Impl;
194+
static constexpr size_t kSize = 128;
195+
static constexpr size_t kAlignment = 8;
196+
utils::FastPimpl<Impl, kSize, kAlignment, false> impl_;
197+
};
198+
199+
class Count {
200+
public:
201+
Count();
202+
~Count();
203+
204+
Count(const Count&);
205+
Count(Count&&) noexcept;
206+
Count& operator=(const Count&);
207+
Count& operator=(Count&&) noexcept;
208+
209+
void WhereString(std::string column_name, std::string value);
210+
void WhereInt32(std::string column_name, int32_t value);
211+
void WhereInt64(std::string column_name, int64_t value);
212+
void WhereBool(std::string column_name, bool value);
213+
void WhereFloat(std::string column_name, float value);
214+
void WhereDouble(std::string column_name, double value);
215+
216+
private:
217+
friend class storages::scylla::impl::driver::DriverTableImpl;
218+
219+
struct Condition {
220+
std::string column_name;
221+
std::variant<std::string, int32_t, int64_t, bool, float, double> value;
222+
};
223+
224+
class Impl;
225+
static constexpr size_t kSize = 64;
226+
static constexpr size_t kAlignment = 8;
227+
utils::FastPimpl<Impl, kSize, kAlignment, false> impl_;
228+
};
229+
230+
class InsertMany {
231+
public:
232+
InsertMany();
233+
~InsertMany();
234+
235+
InsertMany(const InsertMany&);
236+
InsertMany(InsertMany&&) noexcept;
237+
InsertMany& operator=(const InsertMany&);
238+
InsertMany& operator=(InsertMany&&) noexcept;
239+
240+
void NextRow();
241+
242+
void BindString(std::string column_name, std::string value);
243+
void BindInt32(std::string column_name, int32_t value);
244+
void BindInt64(std::string column_name, int64_t value);
245+
void BindBool(std::string column_name, bool value);
246+
void BindFloat(std::string column_name, float value);
247+
void BindDouble(std::string column_name, double value);
248+
249+
private:
250+
friend class storages::scylla::impl::driver::DriverTableImpl;
251+
252+
struct Binding {
253+
std::string column_name;
254+
std::variant<std::string, int32_t, int64_t, bool, float, double> value;
255+
};
256+
257+
class Impl;
258+
static constexpr size_t kSize = 64;
259+
static constexpr size_t kAlignment = 8;
260+
utils::FastPimpl<Impl, kSize, kAlignment, false> impl_;
261+
};
262+
263+
class Truncate {
264+
public:
265+
Truncate() = default;
266+
~Truncate() = default;
267+
268+
Truncate(const Truncate&) = default;
269+
Truncate(Truncate&&) noexcept = default;
270+
Truncate& operator=(const Truncate&) = default;
271+
Truncate& operator=(Truncate&&) noexcept = default;
272+
};
273+
274+
}
87275

88-
USERVER_NAMESPACE_END
276+
USERVER_NAMESPACE_END

scylla/include/userver/storages/scylla/session.hpp

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -21,37 +21,22 @@ namespace impl {
2121
class SessionImpl;
2222
}
2323

24-
/// @brief ScyllaDB client session.
25-
///
26-
/// Use constructor only for tests, in production the session should be
27-
/// retrieved from @ref userver_components "the components" via
28-
/// components::Scylla::GetSession().
2924
class Session {
3025
public:
3126
Session(Session&&) noexcept;
3227
Session& operator=(Session&&) noexcept;
3328
~Session();
3429

35-
/// Checks whether a table exists in the keyspace
3630
bool HasTable(utils::zstring_view name) const;
3731

3832
Table GetTable(std::string table_name) const;
3933

40-
// PreparedStatement Prepare(std::string query) const;
41-
42-
/// Execute a CQL query
43-
// ResultSet Execute(const Statement& statement) const;
44-
45-
/// Drops the associated keyspace if it exists
4634
void DropKeyspace();
4735

48-
/// Get a list of all table names in the associated keyspace
4936
std::vector<std::string> ListTableNames() const;
5037

51-
/// @throws storages::scylla::ScyllaException if failed to connect
5238
void Ping();
5339

54-
/// @cond
5540
explicit Session(
5641
std::string id,
5742
const std::string& hosts,
@@ -66,14 +51,13 @@ class Session {
6651
void SetSessionSettings(const SessionSettings& session_settings);
6752

6853
void SetContactPoints(const std::string& contact_points);
69-
/// @endcond
7054

7155
private:
7256
std::shared_ptr<impl::SessionImpl> impl_;
7357
};
7458

7559
using SessionPtr = std::shared_ptr<Session>;
7660

77-
} // namespace storages::scylla
61+
}
7862

79-
USERVER_NAMESPACE_END
63+
USERVER_NAMESPACE_END

0 commit comments

Comments
 (0)