userver: userver/storages/redis/transaction.hpp Source File
Loading...
Searching...
No Matches
transaction.hpp
Go to the documentation of this file.
1#pragma once
2
3/// @file userver/storages/redis/transaction.hpp
4/// @brief @copybrief storages::redis::Transaction
5
6#include <memory>
7#include <string>
8
9#include <userver/storages/redis/bit_operation.hpp>
10#include <userver/storages/redis/command_options.hpp>
11#include <userver/storages/redis/request.hpp>
12
13USERVER_NAMESPACE_BEGIN
14
15namespace storages::redis {
16
17/// @brief Atomic sequence of Redis commands (https://redis.io/topics/transactions), that is usually retrieved from
18/// storages::redis::Client::Multi().
19///
20/// @note Redis transaction implements isolation, but not
21/// all-or-nothing semantics (IOW a subcommand may fail, but the following
22/// subcommands will succeed).
23///
24/// Membef functions add commands to the `Transaction` object. For each added command a future-like object is returned.
25/// You can get the result of each transaction's subcommand by calling `Get()` method for these objects.
26/// Commands are be sent to a server after calling `Exec()` that returns `RequestExec` object.
27/// You should not call `Get()` method in a future-like subcommand's object
28/// before calling `Get()` method on `RequestExec` object.
29///
30/// @snippet redis/src/storages/redis/client_redistest.cpp redis transaction sample
32public:
33 enum class CheckShards { kNo, kSame };
34
35 virtual ~Transaction() = default;
36
37 /// Finish current atomic sequence of commands and send it to a server.
38 /// Returns 'future-like' request object.
39 /// The data will not be set for the future-like objects for subcommands if
40 /// `Get()` method of the returned object is not called or redis did not return an array with command responses.
41 /// In the last case `Get()` will throw a corresponding exception.
42 virtual RequestExec Exec(const CommandControl& command_control) = 0;
43
44 // redis commands:
45
46 virtual RequestAppend Append(std::string key, std::string value) = 0;
47
48 virtual RequestBitop Bitop(BitOperation op, std::string dest, std::vector<std::string> srcs) = 0;
49
50 virtual RequestDbsize Dbsize(size_t shard) = 0;
51
52 virtual RequestDecr Decr(std::string key) = 0;
53
54 virtual RequestDel Del(std::string key) = 0;
55
56 virtual RequestDel Del(std::vector<std::string> keys) = 0;
57
58 virtual RequestUnlink Unlink(std::string key) = 0;
59
60 virtual RequestUnlink Unlink(std::vector<std::string> keys) = 0;
61
62 virtual RequestExists Exists(std::string key) = 0;
63
64 virtual RequestExists Exists(std::vector<std::string> keys) = 0;
65
66 virtual RequestExpire Expire(std::string key, std::chrono::seconds ttl) = 0;
67
68 virtual RequestExpire Expire(std::string key, std::chrono::seconds ttl, ExpireOptions options) = 0;
69
70 virtual RequestGeoadd Geoadd(std::string key, GeoaddArg point_member) = 0;
71
72 virtual RequestGeoadd Geoadd(std::string key, std::vector<GeoaddArg> point_members) = 0;
73
74 virtual RequestGeopos Geopos(std::string key, std::vector<std::string> members) = 0;
75
76 virtual RequestGeoradius Georadius(
77 std::string key,
78 Longitude lon,
79 Latitude lat,
80 double radius,
81 const GeoradiusOptions& georadius_options
82 ) = 0;
83
84 virtual RequestGeosearch Geosearch(
85 std::string key,
86 std::string member,
87 double radius,
88 const GeosearchOptions& geosearch_options
89 ) = 0;
90
91 virtual RequestGeosearch Geosearch(
92 std::string key,
93 std::string member,
94 BoxWidth width,
95 BoxHeight height,
96 const GeosearchOptions& geosearch_options
97 ) = 0;
98
99 virtual RequestGeosearch Geosearch(
100 std::string key,
101 Longitude lon,
102 Latitude lat,
103 double radius,
104 const GeosearchOptions& geosearch_options
105 ) = 0;
106
107 virtual RequestGeosearch Geosearch(
108 std::string key,
109 Longitude lon,
110 Latitude lat,
111 BoxWidth width,
112 BoxHeight height,
113 const GeosearchOptions& geosearch_options
114 ) = 0;
115
116 virtual RequestGet Get(std::string key) = 0;
117
118 virtual RequestGetdel Getdel(std::string key) = 0;
119
120 virtual RequestGetset Getset(std::string key, std::string value) = 0;
121
122 virtual RequestHdel Hdel(std::string key, std::string field) = 0;
123
124 virtual RequestHdel Hdel(std::string key, std::vector<std::string> fields) = 0;
125
126 virtual RequestHexists Hexists(std::string key, std::string field) = 0;
127
128 virtual RequestHget Hget(std::string key, std::string field) = 0;
129
130 virtual RequestHgetall Hgetall(std::string key) = 0;
131
132 virtual RequestHincrby Hincrby(std::string key, std::string field, int64_t increment) = 0;
133
134 virtual RequestHincrbyfloat Hincrbyfloat(std::string key, std::string field, double increment) = 0;
135
136 virtual RequestHkeys Hkeys(std::string key) = 0;
137
138 virtual RequestHlen Hlen(std::string key) = 0;
139
140 virtual RequestHmget Hmget(std::string key, std::vector<std::string> fields) = 0;
141
142 virtual RequestHmset Hmset(std::string key, std::vector<std::pair<std::string, std::string>> field_values) = 0;
143
144 virtual RequestHset Hset(std::string key, std::string field, std::string value) = 0;
145
146 virtual RequestHsetnx Hsetnx(std::string key, std::string field, std::string value) = 0;
147
148 virtual RequestHvals Hvals(std::string key) = 0;
149
150 virtual RequestIncr Incr(std::string key) = 0;
151
152 virtual RequestKeys Keys(std::string keys_pattern, size_t shard) = 0;
153
154 virtual RequestLindex Lindex(std::string key, int64_t index) = 0;
155
156 virtual RequestLlen Llen(std::string key) = 0;
157
158 virtual RequestLpop Lpop(std::string key) = 0;
159
160 virtual RequestLpush Lpush(std::string key, std::string value) = 0;
161
162 virtual RequestLpush Lpush(std::string key, std::vector<std::string> values) = 0;
163
164 virtual RequestLpushx Lpushx(std::string key, std::string element) = 0;
165
166 virtual RequestLrange Lrange(std::string key, int64_t start, int64_t stop) = 0;
167
168 virtual RequestLrem Lrem(std::string key, int64_t count, std::string element) = 0;
169
170 virtual RequestLtrim Ltrim(std::string key, int64_t start, int64_t stop) = 0;
171
172 virtual RequestMget Mget(std::vector<std::string> keys) = 0;
173
174 virtual RequestMset Mset(std::vector<std::pair<std::string, std::string>> key_values) = 0;
175
176 /// @throws InvalidArgumentException in cluster mode if the keys belong to different hash slots
177 virtual RequestMsetex Msetex(std::vector<std::pair<std::string, std::string>> key_values) = 0;
178
179 /// @throws InvalidArgumentException in cluster mode if the keys belong to different hash slots
180 virtual RequestMsetex Msetex(
181 std::vector<std::pair<std::string, std::string>> key_values,
182 MsetexOptions options
183 ) = 0;
184
185 virtual RequestPersist Persist(std::string key) = 0;
186
187 virtual RequestPexpire Pexpire(std::string key, std::chrono::milliseconds ttl) = 0;
188
189 virtual RequestPing Ping(size_t shard) = 0;
190
191 virtual RequestPingMessage PingMessage(size_t shard, std::string message) = 0;
192
193 virtual RequestRename Rename(std::string key, std::string new_key) = 0;
194
195 virtual RequestRpop Rpop(std::string key) = 0;
196
197 virtual RequestRpush Rpush(std::string key, std::string value) = 0;
198
199 virtual RequestRpush Rpush(std::string key, std::vector<std::string> values) = 0;
200
201 virtual RequestRpushx Rpushx(std::string key, std::string element) = 0;
202
203 virtual RequestSadd Sadd(std::string key, std::string member) = 0;
204
205 virtual RequestSadd Sadd(std::string key, std::vector<std::string> members) = 0;
206
207 virtual RequestScard Scard(std::string key) = 0;
208
209 virtual RequestSet Set(std::string key, std::string value) = 0;
210
211 virtual RequestSet Set(std::string key, std::string value, std::chrono::milliseconds ttl) = 0;
212
213 virtual RequestSetIfExist SetIfExist(std::string key, std::string value) = 0;
214
215 virtual RequestSetIfExist SetIfExist(std::string key, std::string value, std::chrono::milliseconds ttl) = 0;
216
217 virtual RequestSetIfNotExist SetIfNotExist(std::string key, std::string value) = 0;
218
219 virtual RequestSetIfNotExist SetIfNotExist(std::string key, std::string value, std::chrono::milliseconds ttl) = 0;
220
221 virtual RequestSetIfNotExistOrGet SetIfNotExistOrGet(std::string key, std::string value) = 0;
222
223 virtual RequestSetIfNotExistOrGet SetIfNotExistOrGet(
224 std::string key,
225 std::string value,
226 std::chrono::milliseconds ttl
227 ) = 0;
228
229 virtual RequestSetex Setex(std::string key, std::chrono::seconds seconds, std::string value) = 0;
230
231 virtual RequestSetAndGetPrevious SetAndGetPrevious(
232 std::string key,
233 std::string value,
234 std::chrono::milliseconds ttl
235 ) = 0;
236
237 virtual RequestSismember Sismember(std::string key, std::string member) = 0;
238
239 virtual RequestSmembers Smembers(std::string key) = 0;
240
241 virtual RequestSrandmember Srandmember(std::string key) = 0;
242
243 virtual RequestSrandmembers Srandmembers(std::string key, int64_t count) = 0;
244
245 virtual RequestSrem Srem(std::string key, std::string member) = 0;
246
247 virtual RequestSrem Srem(std::string key, std::vector<std::string> members) = 0;
248
249 virtual RequestStrlen Strlen(std::string key) = 0;
250
251 virtual RequestTime Time(size_t shard) = 0;
252
253 virtual RequestTtl Ttl(std::string key) = 0;
254
255 virtual RequestType Type(std::string key) = 0;
256
257 virtual RequestZadd Zadd(std::string key, double score, std::string member) = 0;
258
259 virtual RequestZadd Zadd(std::string key, double score, std::string member, const ZaddOptions& options) = 0;
260
261 virtual RequestZadd Zadd(std::string key, std::vector<std::pair<double, std::string>> scored_members) = 0;
262
263 virtual RequestZadd Zadd(
264 std::string key,
265 std::vector<std::pair<double, std::string>> scored_members,
266 const ZaddOptions& options
267 ) = 0;
268
269 virtual RequestZaddIncr ZaddIncr(std::string key, double score, std::string member) = 0;
270
271 virtual RequestZaddIncrExisting ZaddIncrExisting(std::string key, double score, std::string member) = 0;
272
273 virtual RequestZcard Zcard(std::string key) = 0;
274
275 virtual RequestZcount Zcount(std::string key, double min, double max) = 0;
276
277 virtual RequestZrange Zrange(std::string key, int64_t start, int64_t stop) = 0;
278
279 virtual RequestZrangeWithScores ZrangeWithScores(std::string key, int64_t start, int64_t stop) = 0;
280
281 virtual RequestZrangebyscore Zrangebyscore(std::string key, double min, double max) = 0;
282
283 virtual RequestZrangebyscore Zrangebyscore(std::string key, std::string min, std::string max) = 0;
284
285 virtual RequestZrangebyscore Zrangebyscore(
286 std::string key,
287 double min,
288 double max,
289 const RangeOptions& range_options
290 ) = 0;
291
292 virtual RequestZrangebyscore Zrangebyscore(
293 std::string key,
294 std::string min,
295 std::string max,
296 const RangeOptions& range_options
297 ) = 0;
298
299 virtual RequestZrangebyscoreWithScores ZrangebyscoreWithScores(std::string key, double min, double max) = 0;
300
301 virtual RequestZrangebyscoreWithScores ZrangebyscoreWithScores(
302 std::string key,
303 std::string min,
304 std::string max
305 ) = 0;
306
307 virtual RequestZrangebyscoreWithScores ZrangebyscoreWithScores(
308 std::string key,
309 double min,
310 double max,
311 const RangeOptions& range_options
312 ) = 0;
313
314 virtual RequestZrangebyscoreWithScores ZrangebyscoreWithScores(
315 std::string key,
316 std::string min,
317 std::string max,
318 const RangeOptions& range_options
319 ) = 0;
320
321 virtual RequestZrem Zrem(std::string key, std::string member) = 0;
322
323 virtual RequestZrem Zrem(std::string key, std::vector<std::string> members) = 0;
324
325 virtual RequestZremrangebyrank Zremrangebyrank(std::string key, int64_t start, int64_t stop) = 0;
326
327 virtual RequestZremrangebyscore Zremrangebyscore(std::string key, double min, double max) = 0;
328
329 virtual RequestZremrangebyscore Zremrangebyscore(std::string key, std::string min, std::string max) = 0;
330
331 virtual RequestZscore Zscore(std::string key, std::string member) = 0;
332
333 // Hash field expiration commands:
334
335 virtual RequestHexpire Hexpire(std::string key, std::chrono::seconds ttl, std::vector<std::string> fields) = 0;
336
337 virtual RequestHexpire Hexpire(
338 std::string key,
339 std::chrono::seconds ttl,
340 ExpireOptions options,
341 std::vector<std::string> fields
342 ) = 0;
343
344 virtual RequestHexpire Hpexpire(
345 std::string key,
346 std::chrono::milliseconds ttl,
347 std::vector<std::string> fields
348 ) = 0;
349
350 virtual RequestHexpire Hpexpire(
351 std::string key,
352 std::chrono::milliseconds ttl,
353 ExpireOptions options,
354 std::vector<std::string> fields
355 ) = 0;
356
357 virtual RequestHexpire Hexpireat(
358 std::string key,
359 std::chrono::system_clock::time_point deadline,
360 std::vector<std::string> fields
361 ) = 0;
362
363 virtual RequestHexpire Hexpireat(
364 std::string key,
365 std::chrono::system_clock::time_point deadline,
366 ExpireOptions options,
367 std::vector<std::string> fields
368 ) = 0;
369
370 virtual RequestHexpire Hpexpireat(
371 std::string key,
372 std::chrono::system_clock::time_point deadline,
373 std::vector<std::string> fields
374 ) = 0;
375
376 virtual RequestHexpire Hpexpireat(
377 std::string key,
378 std::chrono::system_clock::time_point deadline,
379 ExpireOptions options,
380 std::vector<std::string> fields
381 ) = 0;
382
383 virtual RequestHexpiretime Hexpiretime(std::string key, std::vector<std::string> fields) = 0;
384
385 virtual RequestHpexpiretime Hpexpiretime(std::string key, std::vector<std::string> fields) = 0;
386
387 virtual RequestHttl Httl(std::string key, std::vector<std::string> fields) = 0;
388
389 virtual RequestHpttl Hpttl(std::string key, std::vector<std::string> fields) = 0;
390
391 virtual RequestHpersist Hpersist(std::string key, std::vector<std::string> fields) = 0;
392
393 virtual RequestHgetex Hgetex(std::string key, std::vector<std::string> fields) = 0;
394
395 virtual RequestHgetex Hgetex(std::string key, HgetexOptions options, std::vector<std::string> fields) = 0;
396
397 virtual RequestHsetex Hsetex(std::string key, std::vector<HsetexFieldValue> field_values) = 0;
398
399 virtual RequestHsetex Hsetex(
400 std::string key,
401 HsetexOptions options,
402 std::vector<HsetexFieldValue> field_values
403 ) = 0;
404
405 // JSON module commands:
406
407 virtual RequestJsonSet JsonSet(std::string key, std::string path, formats::json::Value value) = 0;
408
409 virtual RequestJsonSetIfNotExist JsonSetIfNotExist(
410 std::string key,
411 std::string path,
412 formats::json::Value value
413 ) = 0;
414
415 virtual RequestJsonSetIfExist JsonSetIfExist(std::string key, std::string path, formats::json::Value value) = 0;
416
417 virtual RequestJsonGet JsonGet(std::string key) = 0;
418
419 virtual RequestJsonGet JsonGet(std::string key, std::string path) = 0;
420
421 virtual RequestJsonGet JsonGet(std::string key, std::vector<std::string> paths) = 0;
422
423 virtual RequestJsonMget JsonMget(std::vector<std::string> keys, std::string path) = 0;
424
425 virtual RequestJsonMset JsonMset(std::vector<JsonKeyPathValue> key_path_values) = 0;
426
427 // end of redis commands
428};
429
430using TransactionPtr = std::unique_ptr<Transaction>;
431
433public:
434 using Exception::Exception;
435};
436
438public:
439 using Exception::Exception;
440};
441
442} // namespace storages::redis
443
444USERVER_NAMESPACE_END