-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathsecondary_index_list.cpp
335 lines (283 loc) · 9.75 KB
/
secondary_index_list.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
#include "secondary_index.h"
#include <util/charset/utf8.h>
using namespace NLastGetopt;
using namespace NYdb;
using namespace NYdb::NTable;
using namespace NYdb::NStatusHelpers;
////////////////////////////////////////////////////////////////////////////////
static void ParseSeries(std::vector<TSeries>& results, TResultSetParser&& parser) {
results.clear();
while (parser.TryNextRow()) {
auto& series = results.emplace_back();
series.SeriesId = *parser.ColumnParser(0).GetOptionalUint64();
series.Title = *parser.ColumnParser(1).GetOptionalUtf8();
series.SeriesInfo = *parser.ColumnParser(2).GetOptionalUtf8();
series.ReleaseDate = TInstant::Days(*parser.ColumnParser(3).GetOptionalUint32());
series.Views = *parser.ColumnParser(4).GetOptionalUint64();
}
}
static TStatus ListByViews(
std::vector<TSeries>& results,
TSession& session,
const std::string& prefix,
uint64_t limit,
uint64_t lastSeriesId,
uint64_t lastViews)
{
auto queryText = std::format(R"(
--!syntax_v1
PRAGMA TablePathPrefix("{}");
DECLARE $limit AS Uint64;
DECLARE $lastSeriesId AS Uint64;
DECLARE $lastViews AS Uint64;
-- Simulate a DESC index by inverting views using max(uint64)-views
$maxUint64 = 0xffffffffffffffff;
$lastRevViews = $maxUint64 - $lastViews;
$filterRaw = (
SELECT rev_views, series_id
FROM series_rev_views
WHERE rev_views = $lastRevViews AND series_id > $lastSeriesId
ORDER BY rev_views, series_id
LIMIT $limit
UNION ALL
SELECT rev_views, series_id
FROM series_rev_views
WHERE rev_views > $lastRevViews
ORDER BY rev_views, series_id
LIMIT $limit
);
-- $filterRaw may have more than $limit rows
$filter = (
SELECT rev_views, series_id
FROM $filterRaw
ORDER BY rev_views, series_id
LIMIT $limit
);
SELECT t2.series_id AS series_id, title, series_info, release_date, views
FROM $filter AS t1
INNER JOIN series AS t2 USING (series_id)
ORDER BY views DESC, series_id ASC;
)", prefix);
auto prepareResult = session.PrepareDataQuery(queryText).ExtractValueSync();
if (!prepareResult.IsSuccess()) {
return prepareResult;
}
auto query = prepareResult.GetQuery();
auto params = query.GetParamsBuilder()
.AddParam("$limit")
.Uint64(limit)
.Build()
.AddParam("$lastSeriesId")
.Uint64(lastSeriesId)
.Build()
.AddParam("$lastViews")
.Uint64(lastViews)
.Build()
.Build();
auto result = query.Execute(
TTxControl::BeginTx(TTxSettings::SerializableRW()).CommitTx(),
std::move(params)).ExtractValueSync();
if (result.IsSuccess()) {
ParseSeries(results, result.GetResultSetParser(0));
}
return result;
}
static TStatus ListByViews(
std::vector<TSeries>& results,
TSession& session,
const std::string& prefix,
uint64_t limit)
{
auto queryText = std::format(R"(
--!syntax_v1
PRAGMA TablePathPrefix("{}");
DECLARE $limit AS Uint64;
$filter = (
SELECT rev_views, series_id
FROM series_rev_views
ORDER BY rev_views, series_id
LIMIT $limit
);
SELECT t2.series_id AS series_id, title, series_info, release_date, views
FROM $filter AS t1
INNER JOIN series AS t2 USING (series_id)
ORDER BY views DESC, series_id ASC;
)", prefix);
auto prepareResult = session.PrepareDataQuery(queryText).ExtractValueSync();
if (!prepareResult.IsSuccess()) {
return prepareResult;
}
auto query = prepareResult.GetQuery();
auto params = query.GetParamsBuilder()
.AddParam("$limit")
.Uint64(limit)
.Build()
.Build();
auto result = query.Execute(
TTxControl::BeginTx(TTxSettings::SerializableRW()).CommitTx(),
std::move(params)).ExtractValueSync();
if (result.IsSuccess()) {
ParseSeries(results, result.GetResultSetParser(0));
}
return result;
}
static TStatus ListById(
std::vector<TSeries>& results,
TSession& session,
const std::string& prefix,
uint64_t limit,
uint64_t lastSeriesId)
{
auto queryText = std::format(R"(
--!syntax_v1
PRAGMA TablePathPrefix("{}");
DECLARE $limit AS Uint64;
DECLARE $lastSeriesId AS Uint64;
SELECT series_id, title, series_info, release_date, views
FROM series
WHERE series_id > $lastSeriesId
ORDER BY series_id
LIMIT $limit;
)", prefix);
auto prepareResult = session.PrepareDataQuery(queryText).ExtractValueSync();
if (!prepareResult.IsSuccess()) {
return prepareResult;
}
auto query = prepareResult.GetQuery();
auto params = query.GetParamsBuilder()
.AddParam("$limit")
.Uint64(limit)
.Build()
.AddParam("$lastSeriesId")
.Uint64(lastSeriesId)
.Build()
.Build();
auto result = query.Execute(
TTxControl::BeginTx(TTxSettings::SerializableRW()).CommitTx(),
std::move(params)).ExtractValueSync();
if (result.IsSuccess()) {
ParseSeries(results, result.GetResultSetParser(0));
}
return result;
}
static TStatus ListById(
std::vector<TSeries>& results,
TSession& session,
const std::string& prefix,
uint64_t limit)
{
auto queryText = std::format(R"(
--!syntax_v1
PRAGMA TablePathPrefix("{}");
DECLARE $limit AS Uint64;
SELECT series_id, title, series_info, release_date, views
FROM series
ORDER BY series_id
LIMIT $limit;
)", prefix);
auto prepareResult = session.PrepareDataQuery(queryText).ExtractValueSync();
if (!prepareResult.IsSuccess()) {
return prepareResult;
}
auto query = prepareResult.GetQuery();
auto params = query.GetParamsBuilder()
.AddParam("$limit")
.Uint64(limit)
.Build()
.Build();
auto result = query.Execute(
TTxControl::BeginTx(TTxSettings::SerializableRW()).CommitTx(),
std::move(params)).ExtractValueSync();
if (result.IsSuccess()) {
ParseSeries(results, result.GetResultSetParser(0));
}
return result;
}
int RunListSeries(TDriver& driver, const std::string& prefix, int argc, char** argv) {
TOpts opts = TOpts::Default();
bool byViews = false;
uint64_t limit = 10;
uint64_t lastSeriesId = -1;
uint64_t lastViews = -1;
opts.AddLongOption("by-views", "Sort by views").StoreTrue(&byViews);
opts.AddLongOption("limit", "Maximum number of rows").Optional().RequiredArgument("NUM")
.StoreResult(&limit);
opts.AddLongOption("last-id", "Resume from this last series id").Optional().RequiredArgument("NUM")
.StoreResult(&lastSeriesId);
opts.AddLongOption("last-views", "Resume from this last series views").Optional().RequiredArgument("NUM")
.StoreResult(&lastViews);
TOptsParseResult res(&opts, argc, argv);
std::vector<TSeries> results;
TTableClient client(driver);
ThrowOnError(client.RetryOperationSync([&](TSession session) -> TStatus {
if (byViews) {
if (res.Has("last-id") && res.Has("last-views")) {
return ListByViews(results, session, prefix, limit, lastSeriesId, lastViews);
} else {
return ListByViews(results, session, prefix, limit);
}
} else {
if (res.Has("last-id")) {
return ListById(results, session, prefix, limit, lastSeriesId);
} else {
return ListById(results, session, prefix, limit);
}
}
}));
size_t rows = results.size() + 1;
std::vector<std::string> columns[5];
for (size_t i = 0; i < 5; ++i) {
columns[i].reserve(rows);
}
columns[0].push_back("series_id");
columns[1].push_back("title");
columns[2].push_back("series_info");
columns[3].push_back("release_date");
columns[4].push_back("views");
for (const auto& result : results) {
columns[0].push_back(TStringBuilder() << result.SeriesId);
columns[1].push_back(TStringBuilder() << result.Title);
columns[2].push_back(TStringBuilder() << result.SeriesInfo);
columns[3].push_back(TStringBuilder() << result.ReleaseDate.FormatGmTime("%Y-%m-%d"));
columns[4].push_back(TStringBuilder() << result.Views);
}
size_t widths[5] = { 0 };
for (size_t i = 0; i < 5; ++i) {
for (const auto& value : columns[i]) {
widths[i] = Max(widths[i], GetNumberOfUTF8Chars(value) + 2);
}
}
auto printLine = [&]() {
std::cout << '+';
for (size_t i = 0; i < 5; ++i) {
for (size_t k = 0; k < widths[i]; ++k) {
std::cout << '-';
}
std::cout << '+';
}
std::cout << std::endl;
};
auto printRow = [&](size_t row) {
std::cout << '|';
for (size_t i = 0; i < 5; ++i) {
std::cout << ' ' << columns[i][row];
size_t printed = 1 + GetNumberOfUTF8Chars(columns[i][row]);
while (printed < widths[i]) {
std::cout << ' ';
++printed;
}
std::cout << '|';
}
std::cout << std::endl;
};
printLine();
printRow(0);
printLine();
if (rows > 1) {
for (size_t row = 1; row < rows; ++row) {
printRow(row);
}
printLine();
}
return 0;
}