forked from fastio/1store
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathredis.hh
229 lines (216 loc) · 10.3 KB
/
redis.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
/*
* 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, [email protected]. All rights reserved.
*
*/
#pragma once
#include <functional>
#include "core/sharded.hh"
#include <experimental/optional>
#include <iomanip>
#include <sstream>
#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 <unistd.h>
#include <cstdlib>
#include "structures/geo.hh"
#include "structures/sset_lsa.hh"
#include "keys.hh"
#include "request_wrapper.hh"
namespace redis {
namespace stdx = std::experimental;
struct request_wrapper;
class database;
using message = scattered_message<char>;
using scattered_message_ptr = foreign_ptr<lw_shared_ptr<scattered_message<char>>>;
class redis_service {
private:
inline unsigned get_cpu(const bytes& key) {
return std::hash<bytes>()(key) % smp::count;
}
inline unsigned get_cpu(const redis_key& key) {
return key.hash() % smp::count;
}
public:
redis_service()
{
}
// [TEST APIs]
future<bytes> echo(request_wrapper& args);
future<scattered_message_ptr> ping(request_wrapper& args);
future<scattered_message_ptr> command(request_wrapper& args);
// [COUNTER APIs]
future<scattered_message_ptr> incr(request_wrapper& args);
future<scattered_message_ptr> decr(request_wrapper& args);
future<scattered_message_ptr> incrby(request_wrapper& args);
future<scattered_message_ptr> decrby(request_wrapper& args);
// [STRING APIs]
future<scattered_message_ptr> mset(request_wrapper& args);
future<scattered_message_ptr> set(request_wrapper& args);
future<scattered_message_ptr> del(request_wrapper& args);
future<scattered_message_ptr> exists(request_wrapper& args);
future<scattered_message_ptr> append(request_wrapper& args);
future<scattered_message_ptr> strlen(request_wrapper& args);
future<scattered_message_ptr> get(request_wrapper& args);
future<scattered_message_ptr> mget(request_wrapper& args);
// [LIST APIs]
future<scattered_message_ptr> lpush(request_wrapper& arg);
future<scattered_message_ptr> lpushx(request_wrapper& args);
future<scattered_message_ptr> rpush(request_wrapper& arg);
future<scattered_message_ptr> rpushx(request_wrapper& args);
future<scattered_message_ptr> lpop(request_wrapper& args);
future<scattered_message_ptr> rpop(request_wrapper& args);
future<scattered_message_ptr> llen(request_wrapper& args);
future<scattered_message_ptr> lindex(request_wrapper& args);
future<scattered_message_ptr> linsert(request_wrapper& args);
future<scattered_message_ptr> lset(request_wrapper& args);
future<scattered_message_ptr> lrange(request_wrapper& args);
future<scattered_message_ptr> ltrim(request_wrapper& args);
future<scattered_message_ptr> lrem(request_wrapper& args);
// [HASH APIs]
future<scattered_message_ptr> hdel(request_wrapper& args);
future<scattered_message_ptr> hexists(request_wrapper& args);
future<scattered_message_ptr> hset(request_wrapper& args);
future<scattered_message_ptr> hmset(request_wrapper& args);
future<scattered_message_ptr> hincrby(request_wrapper& args);
future<scattered_message_ptr> hincrbyfloat(request_wrapper& args);
future<scattered_message_ptr> hlen(request_wrapper& args);
future<scattered_message_ptr> hstrlen(request_wrapper& args);
future<scattered_message_ptr> hget(request_wrapper& args);
future<scattered_message_ptr> hgetall(request_wrapper& args);
future<scattered_message_ptr> hgetall_keys(request_wrapper& args);
future<scattered_message_ptr> hgetall_values(request_wrapper& args);
future<scattered_message_ptr> hmget(request_wrapper& args);
// [SET]
future<scattered_message_ptr> sadd(request_wrapper& args);
future<scattered_message_ptr> scard(request_wrapper& args);
future<scattered_message_ptr> srem(request_wrapper& args);
future<scattered_message_ptr> sismember(request_wrapper& args);
future<scattered_message_ptr> smembers(request_wrapper& args);
future<scattered_message_ptr> sdiff(request_wrapper& args);
future<scattered_message_ptr> sdiff_store(request_wrapper& args);
future<scattered_message_ptr> sinter(request_wrapper& args);
future<scattered_message_ptr> sinter_store(request_wrapper& args);
future<scattered_message_ptr> sunion(request_wrapper& args);
future<scattered_message_ptr> sunion_store(request_wrapper& args);
future<scattered_message_ptr> smove(request_wrapper& args);
future<scattered_message_ptr> srandmember(request_wrapper& args);
future<scattered_message_ptr> spop(request_wrapper& args);
future<scattered_message_ptr> type(request_wrapper& args);
future<scattered_message_ptr> expire(request_wrapper& args);
future<scattered_message_ptr> persist(request_wrapper& args);
future<scattered_message_ptr> pexpire(request_wrapper& args);
future<scattered_message_ptr> ttl(request_wrapper& args);
future<scattered_message_ptr> pttl(request_wrapper& args);
// [ZSET]
future<scattered_message_ptr> zadd(request_wrapper& args);
future<scattered_message_ptr> zcard(request_wrapper& args);
future<scattered_message_ptr> zrange(request_wrapper&, bool);
future<scattered_message_ptr> zrangebyscore(request_wrapper&, bool);
future<scattered_message_ptr> zcount(request_wrapper& args);
future<scattered_message_ptr> zincrby(request_wrapper& args);
future<scattered_message_ptr> zrank(request_wrapper&, bool);
future<scattered_message_ptr> zrem(request_wrapper&);
future<scattered_message_ptr> zscore(request_wrapper&);
future<scattered_message_ptr> zunionstore(request_wrapper&);
future<scattered_message_ptr> zinterstore(request_wrapper&);
future<scattered_message_ptr> zdiffstore(request_wrapper&);
future<scattered_message_ptr> zunion(request_wrapper&);
future<scattered_message_ptr> zinter(request_wrapper&);
future<scattered_message_ptr> zdiff(request_wrapper&);
future<scattered_message_ptr> zrangebylex(request_wrapper&);
future<scattered_message_ptr> zlexcount(request_wrapper&);
future<scattered_message_ptr> zrevrangebylex(request_wrapper&);
future<scattered_message_ptr> zremrangebyscore(request_wrapper&);
future<scattered_message_ptr> zremrangebyrank(request_wrapper&);
future<scattered_message_ptr> select(request_wrapper&);
// [GEO]
future<scattered_message_ptr> geoadd(request_wrapper&);
future<scattered_message_ptr> geopos(request_wrapper&);
future<scattered_message_ptr> geodist(request_wrapper&);
future<scattered_message_ptr> geohash(request_wrapper&);
future<scattered_message_ptr> georadius(request_wrapper&, bool);
// [BITMAP]
future<scattered_message_ptr> setbit(request_wrapper&);
future<scattered_message_ptr> getbit(request_wrapper&);
future<scattered_message_ptr> bitcount(request_wrapper&);
future<scattered_message_ptr> bitop(request_wrapper&);
future<scattered_message_ptr> bitpos(request_wrapper&);
future<scattered_message_ptr> bitfield(request_wrapper&);
// [HLL]
future<scattered_message_ptr> pfadd(request_wrapper&);
future<scattered_message_ptr> pfcount(request_wrapper&);
future<scattered_message_ptr> pfmerge(request_wrapper&);
private:
future<std::pair<size_t, int>> zadds_impl(bytes& key, std::unordered_map<bytes, double>&& members, int flags);
future<bool> exists_impl(bytes& key);
future<scattered_message_ptr> srem_impl(bytes& key, bytes& member);
future<scattered_message_ptr> sadd_impl(bytes& key, bytes& member);
future<scattered_message_ptr> sadds_impl(bytes& key, std::vector<bytes>& members);
future<scattered_message_ptr> sadds_impl_return_keys(bytes& key, std::vector<bytes>& members);
future<scattered_message_ptr> sdiff_impl(std::vector<bytes>& keys, bytes* dest);
future<scattered_message_ptr> sinter_impl(std::vector<bytes>& keys, bytes* dest);
future<scattered_message_ptr> sunion_impl(std::vector<bytes>& keys, bytes* dest);
future<scattered_message_ptr> smembers_impl(bytes& key);
future<scattered_message_ptr> pop_impl(request_wrapper& args, bool left);
future<scattered_message_ptr> push_impl(request_wrapper& arg, bool force, bool left);
future<scattered_message_ptr> push_impl(bytes& key, bytes& value, bool force, bool left);
future<scattered_message_ptr> push_impl(bytes& key, std::vector<bytes>& vals, bool force, bool left);
future<bool> srem_direct(bytes& key, bytes& member);
future<bool> sadd_direct(bytes& key, bytes& member);
future<bool> set_impl(bytes& key, bytes& value, long expir, uint8_t flag);
//future<item_ptr> get_impl(bytes& key);
future<bool> remove_impl(bytes& key);
future<int> hdel_impl(bytes& key, bytes& field);
future<scattered_message_ptr> counter_by(request_wrapper& args, bool incr, bool with_step);
using georadius_result_type = std::pair<std::vector<std::tuple<bytes, double, double, double, double>>, int>;
struct zset_args
{
bytes dest;
size_t numkeys;
std::vector<bytes> keys;
std::vector<double> weights;
int aggregate_flag;
};
bool parse_zset_args(request_wrapper& args, zset_args& uargs);
static inline double score_aggregation(const double& old, const double& newscore, int flag)
{
if (flag == ZAGGREGATE_MIN) {
return std::min(old, newscore);
}
else if (flag == ZAGGREGATE_SUM) {
return old + newscore;
}
else {
return std::max(old, newscore);
}
}
};
} /* namespace redis */