forked from scylladb/scylladb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommands.cc
245 lines (222 loc) · 9.85 KB
/
commands.cc
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
/*
* Copyright (C) 2019 pengjian.uestc @ gmail.com
*/
/*
* SPDX-License-Identifier: LicenseRef-ScyllaDB-Source-Available-1.0
*/
#include "redis/commands.hh"
#include <seastar/core/shared_ptr.hh>
#include "redis/request.hh"
#include "redis/reply.hh"
#include "service_permit.hh"
#include "redis/options.hh"
#include "redis/query_utils.hh"
#include "redis/mutation_utils.hh"
#include "redis/lolwut.hh"
#include "redis/keyspace_utils.hh"
namespace redis {
namespace commands {
future<redis_message> get(service::storage_proxy& proxy, request& req, redis::redis_options& options, service_permit permit) {
if (req.arguments_size() != 1) {
throw wrong_arguments_exception(1, req.arguments_size(), req._command);
}
return redis::read_strings(proxy, options, req._args[0], permit).then([] (auto result) {
if (result->has_result()) {
return redis_message::make_strings_result(std::move(result->result()));
}
// return nil string if key does not exist
return redis_message::nil();
});
}
future<redis_message> exists(service::storage_proxy& proxy, request& req, redis::redis_options& options, service_permit permit) {
if (req.arguments_size() < 1) {
throw wrong_arguments_exception(1, req.arguments_size(), req._command);
}
return do_with(size_t(0), [&proxy, &options, permit, &req] (size_t& count) {
return seastar::do_for_each(req._args, [&proxy, &options, permit, &count] (auto& key) {
return redis::read_strings(proxy, options, key, permit).then([&count] (lw_shared_ptr<strings_result> result) {
if (result->has_result()) {
count++;
}
});
}).then([&count] () {
return redis_message::number(count);
});
});
}
future<redis_message> ttl(service::storage_proxy& proxy, request& req, redis::redis_options& options, service_permit permit) {
if (req.arguments_size() != 1) {
throw wrong_arguments_exception(1, req.arguments_size(), req._command);
}
return redis::read_strings(proxy, options, req._args[0], permit).then([] (auto result) {
if (result->has_result()) {
if (result->has_ttl()) {
return redis_message::number(result->ttl().count());
}else{
return redis_message::number(-1);
}
}
return redis_message::number(-2);
});
}
future<redis_message> strlen(service::storage_proxy& proxy, request& req, redis::redis_options& options, service_permit permit) {
if (req.arguments_size() != 1) {
throw wrong_arguments_exception(1, req.arguments_size(), req._command);
}
return redis::read_strings(proxy, options, req._args[0], permit).then([] (auto result) {
if (result->has_result()) {
return redis_message::number(result->result().length());
}
// return 0 string if key does not exist
return redis_message::zero();
});
}
future<redis_message> hgetall(service::storage_proxy& proxy, request& req, redis::redis_options& options, service_permit permit) {
if (req.arguments_size() != 1) {
throw wrong_number_of_arguments_exception(req._command);
}
return redis::read_hashes(proxy, options, req._args[0], permit).then([] (auto result) {
if (!result->empty()) {
return redis_message::make_list_result(*result);
}
// return nil string if key does not exist
return redis_message::nil();
});
}
future<redis_message> hget(service::storage_proxy& proxy, request& req, redis::redis_options& options, service_permit permit) {
if (req.arguments_size() != 2) {
throw wrong_arguments_exception(2, req.arguments_size(), req._command);
}
auto field = std::move(req._args[1]);
return redis::read_hashes(proxy, options, req._args[0], field, permit).then([field] (auto result) {
if (!result->empty()) {
return redis_message::make_strings_result(std::move(result->at(field)));
}
// return nil string if key does not exist
return redis_message::nil();
});
}
future<redis_message> hset(service::storage_proxy& proxy, request& req, redis::redis_options& options, service_permit permit) {
if (req.arguments_size() != 3) {
throw wrong_number_of_arguments_exception(req._command);
}
return redis::write_hashes(proxy, options, std::move(req._args[0]), std::move(req._args[1]), std::move(req._args[2]), 0, permit).then([] {
return redis_message::one();
});
}
future<redis_message> hdel(service::storage_proxy& proxy, request& req, redis::redis_options& options, service_permit permit) {
if (req.arguments_size() < 2) {
throw wrong_number_of_arguments_exception(req._command);
}
//FIXME: We should return the count of the actually deleted fields.
auto fields = std::vector<bytes>(req._args.begin() + 1, req._args.end());
auto size = fields.size();
return redis::delete_fields(proxy, options, std::move(req._args[0]), std::move(fields), permit).then([size] {
return redis_message::number(size);
});
}
future<redis_message> hexists(service::storage_proxy& proxy, request& req, redis::redis_options& options, service_permit permit) {
if (req.arguments_size() != 2) {
throw wrong_arguments_exception(2, req.arguments_size(), req._command);
}
return redis::read_hashes(proxy, options, req._args[0], req._args[1], permit).then([] (auto result) {
return redis_message::number(result->empty() ? 0 : 1);
});
}
future<redis_message> set(service::storage_proxy& proxy, request& req, redis::redis_options& options, service_permit permit) {
if (req.arguments_size() != 2 && req.arguments_size() != 4) {
throw invalid_arguments_exception(req._command);
}
long ttl = 0;
if (req.arguments_size() == 4) {
bytes opt;
opt.resize(req._args[2].size());
std::transform(req._args[2].begin(), req._args[2].end(), opt.begin(), ::tolower);
if (opt == "ex") {
try {
ttl = std::stol(std::string(reinterpret_cast<const char*>(req._args[3].data()), req._args[3].size()));
}
catch (...) {
throw invalid_arguments_exception(req._command);
}
}
}
return redis::write_strings(proxy, options, std::move(req._args[0]), std::move(req._args[1]), ttl, permit).then([] {
return redis_message::ok();
});
}
future<redis_message> setex(service::storage_proxy& proxy, request& req, redis::redis_options& options, service_permit permit) {
if (req.arguments_size() != 3) {
throw wrong_arguments_exception(3, req.arguments_size(), req._command);
}
long ttl = std::stol(std::string(reinterpret_cast<const char*>(req._args[1].data()), req._args[1].size()));
return redis::write_strings(proxy, options, std::move(req._args[0]), std::move(req._args[2]), ttl, permit).then([] {
return redis_message::ok();
});
}
future<redis_message> del(service::storage_proxy& proxy, request& req, redis::redis_options& options, service_permit permit) {
if (req.arguments_size() == 0) {
throw wrong_number_of_arguments_exception(req._command);
}
//FIXME: We should return the count of the actually deleted keys.
auto keys = req._args;
auto size = keys.size();
return redis::delete_objects(proxy, options, std::move(keys), permit).then([size] {
return redis_message::number(size);
});
}
future<redis_message> select(service::storage_proxy&, request& req, redis::redis_options& options, service_permit) {
if (req.arguments_size() != 1) {
throw wrong_arguments_exception(1, req.arguments_size(), req._command);
}
long index = -1;
try {
index = std::stol(std::string(reinterpret_cast<const char*>(req._args[0].data()), req._args[0].size()));
}
catch (...) {
throw invalid_db_index_exception();
}
if (index < 0 || static_cast<size_t>(index) >= options.get_total_redis_db_count()) {
throw invalid_db_index_exception();
}
options.set_keyspace_name(fmt::format("REDIS_{}", static_cast<size_t>(index)));
return redis_message::ok();
}
future<redis_message> unknown(service::storage_proxy&, request& req, redis::redis_options&, service_permit) {
return redis_message::unknown(std::move(req._command));
}
future<redis_message> ping(service::storage_proxy&, request& req, redis::redis_options&, service_permit) {
return redis_message::pong();
}
future<redis_message> echo(service::storage_proxy&, request& req, redis::redis_options&, service_permit) {
if (req.arguments_size() != 1) {
throw wrong_arguments_exception(1, req.arguments_size(), req._command);
}
return redis_message::make_strings_result(req._args[0]);
}
future<redis_message> lolwut(service::storage_proxy&, request& req, redis::redis_options& options, service_permit) {
int cols = 66;
int squares_per_row = 8;
int squares_per_col = 12;
try {
if (req.arguments_size() >= 1) {
cols = std::stoi(std::string(reinterpret_cast<const char*>(req._args[0].data()), req._args[0].size()));
cols = std::clamp(cols, 1, 1000);
}
if (req.arguments_size() >= 2) {
squares_per_row = std::stoi(std::string(reinterpret_cast<const char*>(req._args[1].data()), req._args[1].size()));
squares_per_row = std::clamp(squares_per_row, 1, 200);
}
if (req.arguments_size() >= 3) {
squares_per_col = std::stoi(std::string(reinterpret_cast<const char*>(req._args[2].data()), req._args[2].size()));
squares_per_col = std::clamp(squares_per_col, 1, 200);
}
} catch (...) {
throw wrong_arguments_exception(1, req.arguments_size(), req._command);
}
return redis::lolwut5(cols, squares_per_row, squares_per_col).then([] (auto result) {
return redis_message::make_strings_result(std::move(result));
});
}
}
}