forked from fastio/1store
-
Notifications
You must be signed in to change notification settings - Fork 0
/
reply_builder.hh
470 lines (455 loc) · 17.4 KB
/
reply_builder.hh
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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
/*
* Pedis is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* You may obtain a copy of the License at
*
* http://www.gnu.org/licenses
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*
* Copyright (c) 2016-2026, Peng Jian, pstack@163.com. All rights reserved.
*
*/
#pragma once
#include "core/app-template.hh"
#include "core/future-util.hh"
#include "core/timer-set.hh"
#include "core/shared_ptr.hh"
#include "core/stream.hh"
#include "core/memory.hh"
#include "core/units.hh"
#include "core/distributed.hh"
#include "core/vector-data-sink.hh"
#include "core/bitops.hh"
#include "core/slab.hh"
#include "core/align.hh"
#include "net/api.hh"
#include "net/packet-data-source.hh"
#include "cache.hh"
#include "structures/dict_lsa.hh"
#include "structures/sset_lsa.hh"
#include "structures/geo.hh"
#include "keys.hh"
namespace redis {
using scattered_message_ptr = foreign_ptr<lw_shared_ptr<scattered_message<char>>>;
static const bytes msg_crlf {"\r\n"};
static const bytes msg_ok {"+OK\r\n"};
static const bytes msg_pong {"+PONG\r\n"};
static const bytes msg_err = {"-ERR\r\n"};
static const bytes msg_zero = {":0\r\n"};
static const bytes msg_one = {":1\r\n"};
static const bytes msg_neg_one = {":-1\r\n"};
static const bytes msg_neg_two = {":-2\r\n"};
static const bytes msg_null_blik = {"$-1\r\n"};
static const bytes msg_null_multi_bulk = {"*-1\r\n"};
static const bytes msg_empty_multi_bulk = {"*0\r\n"};
static const bytes msg_type_err = {"-WRONGTYPE Operation against a key holding the wrong kind of value\r\n"};
static const bytes msg_nokey_err = {"-ERR no such key\r\n"};
static const bytes msg_nocmd_err = {"-ERR no such command\r\n"};
static const bytes msg_syntax_err = {"-ERR syntax error\r\n"};
static const bytes msg_same_object_err = {"-ERR source and destination objects are the same\r\n"};
static const bytes msg_out_of_range_err = {"-ERR index out of range\r\n"};
static const bytes msg_not_integer_err = {"-ERR ERR hash value is not an integer\r\n" };
static const bytes msg_not_float_err = {"-ERR ERR hash value is not an float\r\n" };
static const bytes msg_str_tag = {"+"};
static const bytes msg_num_tag = {":"};
static const bytes msg_sigle_tag = {"*"};
static const bytes msg_batch_tag = {"$"};
static const bytes msg_not_found = {"+(nil)\r\n"};
static const bytes msg_nil = {"+(nil)\r\n"};
static constexpr const int REDIS_OK = 0;
static constexpr const int REDIS_ERR = 1;
static constexpr const int REDIS_NONE = -1;
static constexpr const int REDIS_WRONG_TYPE = -2;
static const bytes msg_type_string {"+string\r\n"};
static const bytes msg_type_none {"+none\r\n"};
static const bytes msg_type_list {"+list\r\n"};
static const bytes msg_type_hll {"+hyperloglog\r\n"};
static const bytes msg_type_set {"+set\r\n"};
static const bytes msg_type_zset {"+zset\r\n"};
static const bytes msg_type_hash {"+hash\r\n"};
class reply_builder final {
public:
static future<scattered_message_ptr> build(size_t size)
{
auto m = make_lw_shared<scattered_message<char>>();
m->append_static(msg_num_tag);
m->append(to_sstring(size));
m->append_static(msg_crlf);
return make_ready_future<scattered_message_ptr>(foreign_ptr<lw_shared_ptr<scattered_message<char>>>(m));
}
static future<scattered_message_ptr> build(double number)
{
auto m = make_lw_shared<scattered_message<char>>();
auto&& n = to_sstring(number);
m->append_static(msg_batch_tag);
m->append(to_sstring(n.size()));
m->append_static(msg_crlf);
m->append(std::move(n));
m->append_static(msg_crlf);
return make_ready_future<scattered_message_ptr>(foreign_ptr<lw_shared_ptr<scattered_message<char>>>(m));
}
static future<scattered_message_ptr> build(const bytes& message)
{
auto m = make_lw_shared<scattered_message<char>>();
m->append(message);
return make_ready_future<scattered_message_ptr>(foreign_ptr<lw_shared_ptr<scattered_message<char>>>(m));
}
template<bool Key, bool Value>
static future<scattered_message_ptr> build(const cache_entry* e)
{
if (e) {
//build reply
auto m = make_lw_shared<scattered_message<char>>();
if (Key) {
m->append(msg_batch_tag);
m->append(to_sstring(e->key_size()));
m->append_static(msg_crlf);
m->append(sstring{e->key_data(), e->key_size()});
m->append_static(msg_crlf);
}
if (Value) {
m->append_static(msg_batch_tag);
if (e->type_of_integer()) {
auto&& n = to_sstring(e->value_integer());
m->append(to_sstring(n.size()));
m->append_static(msg_crlf);
m->append(n);
m->append_static(msg_crlf);
}
else if (e->type_of_float()) {
auto&& n = to_sstring(e->value_float());
m->append(to_sstring(n.size()));
m->append_static(msg_crlf);
m->append(n);
m->append_static(msg_crlf);
}
else if (e->type_of_bytes()) {
m->append(to_sstring(e->value_bytes_size()));
m->append_static(msg_crlf);
m->append(sstring{e->value_bytes_data(), e->value_bytes_size()});
m->append_static(msg_crlf);
}
else {
m->append_static(msg_type_err);
}
return make_ready_future<scattered_message_ptr>(foreign_ptr<lw_shared_ptr<scattered_message<char>>>(m));
}
}
else {
return reply_builder::build(msg_not_found);
}
}
template<bool Key, bool Value>
static future<scattered_message_ptr> build(const std::vector<const dict_entry*>& entries)
{
if (!entries.empty()) {
//build reply
auto m = make_lw_shared<scattered_message<char>>();
m->append_static(msg_sigle_tag);
if (Key && Value) {
m->append(std::move(to_sstring(entries.size() * 2)));
}
else {
m->append(std::move(to_sstring(entries.size())));
}
m->append_static(msg_crlf);
for (size_t i = 0; i < entries.size(); ++i) {
const auto e = entries[i];
if (Key) {
if (e) {
m->append_static(msg_batch_tag);
m->append(to_sstring(e->key_size()));
m->append_static(msg_crlf);
m->append(sstring{e->key_data(), e->key_size()});
m->append_static(msg_crlf);
}
else {
m->append_static(msg_not_found);
}
}
if (Value) {
if (e) {
m->append_static(msg_batch_tag);
if (e->type_of_integer()) {
auto&& n = to_sstring(e->value_integer());
m->append(to_sstring(n.size()));
m->append_static(msg_crlf);
m->append(n);
m->append_static(msg_crlf);
}
else if (e->type_of_float()) {
auto&& n = to_sstring(e->value_float());
m->append(to_sstring(n.size()));
m->append_static(msg_crlf);
m->append(n);
m->append_static(msg_crlf);
}
else if (e->type_of_bytes()) {
m->append(to_sstring(e->value_bytes_size()));
m->append_static(msg_crlf);
m->append(sstring{e->value_bytes_data(), e->value_bytes_size()});
m->append_static(msg_crlf);
}
else {
m->append_static(msg_type_err);
}
}
else {
m->append_static(msg_not_found);
}
}
}
return make_ready_future<scattered_message_ptr>(foreign_ptr<lw_shared_ptr<scattered_message<char>>>(m));
}
else {
return reply_builder::build(msg_nil);
}
}
template<bool Key, bool Value>
static future<scattered_message_ptr> build(const dict_entry* e)
{
if (e) {
//build reply
auto m = make_lw_shared<scattered_message<char>>();
if (Key) {
m->append(msg_batch_tag);
m->append(to_sstring(e->key_size()));
m->append_static(msg_crlf);
m->append(sstring{e->key_data(), e->key_size()});
m->append_static(msg_crlf);
}
if (Value) {
if (e->type_of_integer()) {
auto&& n = to_sstring(e->value_integer());
m->append(to_sstring(n.size()));
m->append_static(msg_crlf);
m->append(n);
m->append_static(msg_crlf);
}
else if (e->type_of_float()) {
auto&& n = to_sstring(e->value_float());
m->append(to_sstring(n.size()));
m->append_static(msg_crlf);
m->append(n);
m->append_static(msg_crlf);
}
else if (e->type_of_bytes()) {
m->append_static(msg_batch_tag);
m->append(to_sstring(e->value_bytes_size()));
m->append_static(msg_crlf);
m->append(sstring{e->value_bytes_data(), e->value_bytes_size()});
m->append_static(msg_crlf);
}
else {
m->append_static(msg_type_err);
}
return make_ready_future<scattered_message_ptr>(foreign_ptr<lw_shared_ptr<scattered_message<char>>>(m));
}
return make_ready_future<scattered_message_ptr>(foreign_ptr<lw_shared_ptr<scattered_message<char>>>(m));
}
else {
return reply_builder::build(msg_nil);
}
}
static future<scattered_message_ptr> build(const std::vector<const managed_bytes*>& data)
{
auto m = make_lw_shared<scattered_message<char>>();
m->append(msg_sigle_tag);
m->append(to_sstring(data.size()));
m->append_static(msg_crlf);
for (size_t i = 0; i < data.size(); ++i) {
m->append_static(msg_batch_tag);
m->append(to_sstring(data[i]->size()));
m->append_static(msg_crlf);
m->append(sstring{reinterpret_cast<const char*>(data[i]->data()), data[i]->size()});
m->append_static(msg_crlf);
}
return make_ready_future<scattered_message_ptr>(foreign_ptr<lw_shared_ptr<scattered_message<char>>>(m));
}
static future<scattered_message_ptr> build(const managed_bytes& data)
{
auto m = make_lw_shared<scattered_message<char>>();
m->append_static(msg_batch_tag);
m->append(to_sstring(data.size()));
m->append_static(msg_crlf);
m->append(sstring{reinterpret_cast<const char*>(data.data()), data.size()});
m->append_static(msg_crlf);
return make_ready_future<scattered_message_ptr>(foreign_ptr<lw_shared_ptr<scattered_message<char>>>(m));
}
static future<scattered_message_ptr> build(const std::vector<const sset_entry*>& entries, bool with_score)
{
if (!entries.empty()) {
//build reply
auto m = make_lw_shared<scattered_message<char>>();
m->append_static(msg_sigle_tag);
if (with_score) {
m->append(std::move(to_sstring(entries.size() * 2)));
}
else {
m->append(std::move(to_sstring(entries.size())));
}
m->append_static(msg_crlf);
for (size_t i = 0; i < entries.size(); ++i) {
const auto& e = entries[i];
assert(e != nullptr);
m->append_static(msg_batch_tag);
m->append(to_sstring(e->key_size()));
m->append_static(msg_crlf);
m->append(sstring{e->key_data(), e->key_size()});
m->append_static(msg_crlf);
if (with_score) {
m->append_static(msg_batch_tag);
auto&& n = to_sstring(e->score());
m->append(to_sstring(n.size()));
m->append_static(msg_crlf);
m->append(n);
m->append_static(msg_crlf);
}
}
return make_ready_future<scattered_message_ptr>(foreign_ptr<lw_shared_ptr<scattered_message<char>>>(m));
}
else {
return reply_builder::build(msg_nil);
}
}
static future<scattered_message_ptr> build(std::vector<bytes>& data)
{
auto m = make_lw_shared<scattered_message<char>>();
m->append_static(msg_sigle_tag);
m->append(std::move(to_sstring(data.size())));
m->append_static(msg_crlf);
for (size_t i = 0; i < data.size(); ++i) {
auto& uu = data[i];
m->append_static(msg_batch_tag);
m->append(to_sstring(uu.size()));
m->append_static(msg_crlf);
m->append(std::move(uu));
m->append_static(msg_crlf);
}
return make_ready_future<scattered_message_ptr>(foreign_ptr<lw_shared_ptr<scattered_message<char>>>(m));
}
static future<scattered_message_ptr> build(std::vector<foreign_ptr<lw_shared_ptr<bytes>>>& entries)
{
if (!entries.empty()) {
auto m = make_lw_shared<scattered_message<char>>();
m->append_static(msg_sigle_tag);
m->append(std::move(to_sstring(entries.size())));
m->append_static(msg_crlf);
for (size_t i = 0; i < entries.size(); ++i) {
auto& e = entries[i];
m->append_static(msg_batch_tag);
m->append(to_sstring(e->size()));
m->append_static(msg_crlf);
m->append(*e);
m->append_static(msg_crlf);
}
return make_ready_future<scattered_message_ptr>(foreign_ptr<lw_shared_ptr<scattered_message<char>>>(m));
}
return reply_builder::build(msg_nil);
}
static future<scattered_message_ptr> build(std::unordered_map<sstring, double>& data, bool with_score)
{
auto m = make_lw_shared<scattered_message<char>>();
m->append_static(msg_sigle_tag);
if (with_score) {
m->append(std::move(to_sstring(data.size() * 2)));
}
else {
m->append(std::move(to_sstring(data.size())));
}
m->append_static(msg_crlf);
for (auto& d : data) {
m->append_static(msg_batch_tag);
m->append(to_sstring(d.first.size()));
m->append_static(msg_crlf);
m->append(std::move(d.first));
m->append_static(msg_crlf);
if (with_score) {
m->append_static(msg_batch_tag);
auto&& n = to_sstring(d.second);
m->append(to_sstring(n.size()));
m->append_static(msg_crlf);
m->append(n);
m->append_static(msg_crlf);
}
}
return make_ready_future<scattered_message_ptr>(foreign_ptr<lw_shared_ptr<scattered_message<char>>>(m));
}
static future<scattered_message_ptr> build(std::vector<std::tuple<bytes, double, double, double, double>>& u, int flags)
{
auto m = make_lw_shared<scattered_message<char>>();
m->append_static(msg_sigle_tag);
m->append(std::move(to_sstring(u.size())));
m->append_static(msg_crlf);
int temp = 1, temp2 = 2;
bool wd = flags & GEORADIUS_WITHDIST;
bool wh = flags & GEORADIUS_WITHHASH;
bool wc = flags & GEORADIUS_WITHCOORD;
if (wd) temp++;
if (wh) temp++;
if (wc) temp++;
for (size_t i = 0; i < u.size(); ++i) {
m->append_static(msg_sigle_tag);
m->append(std::move(to_sstring(temp)));
m->append_static(msg_crlf);
//key
bytes& key = std::get<0>(u[i]);
m->append_static(msg_batch_tag);
m->append(to_sstring(key.size()));
m->append_static(msg_crlf);
m->append(std::move(key));
m->append_static(msg_crlf);
//dist
if (wd) {
double dist = std::get<2>(u[i]);
geo::from_meters(dist, flags);
auto&& n2 = to_sstring(dist);
m->append_static(msg_batch_tag);
m->append(to_sstring(n2.size()));
m->append_static(msg_crlf);
m->append(std::move(n2));
m->append_static(msg_crlf);
}
//coord
if (wc) {
m->append_static(msg_sigle_tag);
m->append(std::move(to_sstring(temp2)));
m->append_static(msg_crlf);
auto&& n1 = to_sstring(std::get<3>(u[i]));
m->append_static(msg_batch_tag);
m->append(to_sstring(n1.size()));
m->append_static(msg_crlf);
m->append(std::move(n1));
m->append_static(msg_crlf);
auto&& n2 = to_sstring(std::get<4>(u[i]));
m->append_static(msg_batch_tag);
m->append(to_sstring(n2.size()));
m->append_static(msg_crlf);
m->append(std::move(n2));
m->append_static(msg_crlf);
}
//hash
if (wh) {
double& score = std::get<1>(u[i]);
bytes hashstr;
geo::encode_to_geohash_string(score, hashstr);
m->append_static(msg_batch_tag);
m->append(to_sstring(hashstr.size()));
m->append_static(msg_crlf);
m->append(std::move(hashstr));
m->append_static(msg_crlf);
}
}
return make_ready_future<scattered_message_ptr>(foreign_ptr<lw_shared_ptr<scattered_message<char>>>(m));
}
}; // end of class
}