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 RequestGetset Getset(std::string key, std::string value) = 0;
119
120 virtual RequestHdel Hdel(std::string key, std::string field) = 0;
121
122 virtual RequestHdel Hdel(std::string key, std::vector<std::string> fields) = 0;
123
124 virtual RequestHexists Hexists(std::string key, std::string field) = 0;
125
126 virtual RequestHget Hget(std::string key, std::string field) = 0;
127
128 virtual RequestHgetall Hgetall(std::string key) = 0;
129
130 virtual RequestHincrby Hincrby(std::string key, std::string field, int64_t increment) = 0;
131
132 virtual RequestHincrbyfloat Hincrbyfloat(std::string key, std::string field, double increment) = 0;
133
134 virtual RequestHkeys Hkeys(std::string key) = 0;
135
136 virtual RequestHlen Hlen(std::string key) = 0;
137
138 virtual RequestHmget Hmget(std::string key, std::vector<std::string> fields) = 0;
139
140 virtual RequestHmset Hmset(std::string key, std::vector<std::pair<std::string, std::string>> field_values) = 0;
141
142 virtual RequestHset Hset(std::string key, std::string field, std::string value) = 0;
143
144 virtual RequestHsetnx Hsetnx(std::string key, std::string field, std::string value) = 0;
145
146 virtual RequestHvals Hvals(std::string key) = 0;
147
148 virtual RequestIncr Incr(std::string key) = 0;
149
150 virtual RequestKeys Keys(std::string keys_pattern, size_t shard) = 0;
151
152 virtual RequestLindex Lindex(std::string key, int64_t index) = 0;
153
154 virtual RequestLlen Llen(std::string key) = 0;
155
156 virtual RequestLpop Lpop(std::string key) = 0;
157
158 virtual RequestLpush Lpush(std::string key, std::string value) = 0;
159
160 virtual RequestLpush Lpush(std::string key, std::vector<std::string> values) = 0;
161
162 virtual RequestLpushx Lpushx(std::string key, std::string element) = 0;
163
164 virtual RequestLrange Lrange(std::string key, int64_t start, int64_t stop) = 0;
165
166 virtual RequestLrem Lrem(std::string key, int64_t count, std::string element) = 0;
167
168 virtual RequestLtrim Ltrim(std::string key, int64_t start, int64_t stop) = 0;
169
170 virtual RequestMget Mget(std::vector<std::string> keys) = 0;
171
172 virtual RequestMset Mset(std::vector<std::pair<std::string, std::string>> key_values) = 0;
173
174 virtual RequestPersist Persist(std::string key) = 0;
175
176 virtual RequestPexpire Pexpire(std::string key, std::chrono::milliseconds ttl) = 0;
177
178 virtual RequestPing Ping(size_t shard) = 0;
179
180 virtual RequestPingMessage PingMessage(size_t shard, std::string message) = 0;
181
182 virtual RequestRename Rename(std::string key, std::string new_key) = 0;
183
184 virtual RequestRpop Rpop(std::string key) = 0;
185
186 virtual RequestRpush Rpush(std::string key, std::string value) = 0;
187
188 virtual RequestRpush Rpush(std::string key, std::vector<std::string> values) = 0;
189
190 virtual RequestRpushx Rpushx(std::string key, std::string element) = 0;
191
192 virtual RequestSadd Sadd(std::string key, std::string member) = 0;
193
194 virtual RequestSadd Sadd(std::string key, std::vector<std::string> members) = 0;
195
196 virtual RequestScard Scard(std::string key) = 0;
197
198 virtual RequestSet Set(std::string key, std::string value) = 0;
199
200 virtual RequestSet Set(std::string key, std::string value, std::chrono::milliseconds ttl) = 0;
201
202 virtual RequestSetIfExist SetIfExist(std::string key, std::string value) = 0;
203
204 virtual RequestSetIfExist SetIfExist(std::string key, std::string value, std::chrono::milliseconds ttl) = 0;
205
206 virtual RequestSetIfNotExist SetIfNotExist(std::string key, std::string value) = 0;
207
208 virtual RequestSetIfNotExist SetIfNotExist(std::string key, std::string value, std::chrono::milliseconds ttl) = 0;
209
210 virtual RequestSetIfNotExistOrGet SetIfNotExistOrGet(std::string key, std::string value) = 0;
211
212 virtual RequestSetIfNotExistOrGet SetIfNotExistOrGet(
213 std::string key,
214 std::string value,
215 std::chrono::milliseconds ttl
216 ) = 0;
217
218 virtual RequestSetex Setex(std::string key, std::chrono::seconds seconds, std::string value) = 0;
219
220 virtual RequestSetAndGetPrevious SetAndGetPrevious(
221 std::string key,
222 std::string value,
223 std::chrono::milliseconds ttl
224 ) = 0;
225
226 virtual RequestSismember Sismember(std::string key, std::string member) = 0;
227
228 virtual RequestSmembers Smembers(std::string key) = 0;
229
230 virtual RequestSrandmember Srandmember(std::string key) = 0;
231
232 virtual RequestSrandmembers Srandmembers(std::string key, int64_t count) = 0;
233
234 virtual RequestSrem Srem(std::string key, std::string member) = 0;
235
236 virtual RequestSrem Srem(std::string key, std::vector<std::string> members) = 0;
237
238 virtual RequestStrlen Strlen(std::string key) = 0;
239
240 virtual RequestTime Time(size_t shard) = 0;
241
242 virtual RequestTtl Ttl(std::string key) = 0;
243
244 virtual RequestType Type(std::string key) = 0;
245
246 virtual RequestZadd Zadd(std::string key, double score, std::string member) = 0;
247
248 virtual RequestZadd Zadd(std::string key, double score, std::string member, const ZaddOptions& options) = 0;
249
250 virtual RequestZadd Zadd(std::string key, std::vector<std::pair<double, std::string>> scored_members) = 0;
251
252 virtual RequestZadd Zadd(
253 std::string key,
254 std::vector<std::pair<double, std::string>> scored_members,
255 const ZaddOptions& options
256 ) = 0;
257
258 virtual RequestZaddIncr ZaddIncr(std::string key, double score, std::string member) = 0;
259
260 virtual RequestZaddIncrExisting ZaddIncrExisting(std::string key, double score, std::string member) = 0;
261
262 virtual RequestZcard Zcard(std::string key) = 0;
263
264 virtual RequestZcount Zcount(std::string key, double min, double max) = 0;
265
266 virtual RequestZrange Zrange(std::string key, int64_t start, int64_t stop) = 0;
267
268 virtual RequestZrangeWithScores ZrangeWithScores(std::string key, int64_t start, int64_t stop) = 0;
269
270 virtual RequestZrangebyscore Zrangebyscore(std::string key, double min, double max) = 0;
271
272 virtual RequestZrangebyscore Zrangebyscore(std::string key, std::string min, std::string max) = 0;
273
274 virtual RequestZrangebyscore Zrangebyscore(
275 std::string key,
276 double min,
277 double max,
278 const RangeOptions& range_options
279 ) = 0;
280
281 virtual RequestZrangebyscore Zrangebyscore(
282 std::string key,
283 std::string min,
284 std::string max,
285 const RangeOptions& range_options
286 ) = 0;
287
288 virtual RequestZrangebyscoreWithScores ZrangebyscoreWithScores(std::string key, double min, double max) = 0;
289
290 virtual RequestZrangebyscoreWithScores ZrangebyscoreWithScores(
291 std::string key,
292 std::string min,
293 std::string max
294 ) = 0;
295
296 virtual RequestZrangebyscoreWithScores ZrangebyscoreWithScores(
297 std::string key,
298 double min,
299 double max,
300 const RangeOptions& range_options
301 ) = 0;
302
303 virtual RequestZrangebyscoreWithScores ZrangebyscoreWithScores(
304 std::string key,
305 std::string min,
306 std::string max,
307 const RangeOptions& range_options
308 ) = 0;
309
310 virtual RequestZrem Zrem(std::string key, std::string member) = 0;
311
312 virtual RequestZrem Zrem(std::string key, std::vector<std::string> members) = 0;
313
314 virtual RequestZremrangebyrank Zremrangebyrank(std::string key, int64_t start, int64_t stop) = 0;
315
316 virtual RequestZremrangebyscore Zremrangebyscore(std::string key, double min, double max) = 0;
317
318 virtual RequestZremrangebyscore Zremrangebyscore(std::string key, std::string min, std::string max) = 0;
319
320 virtual RequestZscore Zscore(std::string key, std::string member) = 0;
321
322 // Hash field expiration commands:
323
324 virtual RequestHexpire Hexpire(std::string key, std::chrono::seconds ttl, std::vector<std::string> fields) = 0;
325
326 virtual RequestHexpire Hexpire(
327 std::string key,
328 std::chrono::seconds ttl,
329 ExpireOptions options,
330 std::vector<std::string> fields
331 ) = 0;
332
333 virtual RequestHexpire Hpexpire(
334 std::string key,
335 std::chrono::milliseconds ttl,
336 std::vector<std::string> fields
337 ) = 0;
338
339 virtual RequestHexpire Hpexpire(
340 std::string key,
341 std::chrono::milliseconds ttl,
342 ExpireOptions options,
343 std::vector<std::string> fields
344 ) = 0;
345
346 virtual RequestHexpire Hexpireat(
347 std::string key,
348 std::chrono::system_clock::time_point deadline,
349 std::vector<std::string> fields
350 ) = 0;
351
352 virtual RequestHexpire Hexpireat(
353 std::string key,
354 std::chrono::system_clock::time_point deadline,
355 ExpireOptions options,
356 std::vector<std::string> fields
357 ) = 0;
358
359 virtual RequestHexpire Hpexpireat(
360 std::string key,
361 std::chrono::system_clock::time_point deadline,
362 std::vector<std::string> fields
363 ) = 0;
364
365 virtual RequestHexpire Hpexpireat(
366 std::string key,
367 std::chrono::system_clock::time_point deadline,
368 ExpireOptions options,
369 std::vector<std::string> fields
370 ) = 0;
371
372 virtual RequestHexpiretime Hexpiretime(std::string key, std::vector<std::string> fields) = 0;
373
374 virtual RequestHpexpiretime Hpexpiretime(std::string key, std::vector<std::string> fields) = 0;
375
376 virtual RequestHttl Httl(std::string key, std::vector<std::string> fields) = 0;
377
378 virtual RequestHpttl Hpttl(std::string key, std::vector<std::string> fields) = 0;
379
380 virtual RequestHpersist Hpersist(std::string key, std::vector<std::string> fields) = 0;
381
382 virtual RequestHgetex Hgetex(std::string key, std::vector<std::string> fields) = 0;
383
384 virtual RequestHgetex Hgetex(std::string key, HgetexOptions options, std::vector<std::string> fields) = 0;
385
386 virtual RequestHsetex Hsetex(std::string key, std::vector<HsetexFieldValue> field_values) = 0;
387
388 virtual RequestHsetex Hsetex(
389 std::string key,
390 HsetexOptions options,
391 std::vector<HsetexFieldValue> field_values
392 ) = 0;
393
394 // JSON module commands:
395
396 virtual RequestJsonSet JsonSet(std::string key, std::string path, formats::json::Value value) = 0;
397
398 virtual RequestJsonSetIfNotExist JsonSetIfNotExist(
399 std::string key,
400 std::string path,
401 formats::json::Value value
402 ) = 0;
403
404 virtual RequestJsonSetIfExist JsonSetIfExist(std::string key, std::string path, formats::json::Value value) = 0;
405
406 virtual RequestJsonGet JsonGet(std::string key) = 0;
407
408 virtual RequestJsonGet JsonGet(std::string key, std::string path) = 0;
409
410 virtual RequestJsonGet JsonGet(std::string key, std::vector<std::string> paths) = 0;
411
412 virtual RequestJsonMget JsonMget(std::vector<std::string> keys, std::string path) = 0;
413
414 virtual RequestJsonMset JsonMset(std::vector<JsonKeyPathValue> key_path_values) = 0;
415
416 // end of redis commands
417};
418
419using TransactionPtr = std::unique_ptr<Transaction>;
420
422public:
423 using Exception::Exception;
424};
425
427public:
428 using Exception::Exception;
429};
430
431} // namespace storages::redis
432
433USERVER_NAMESPACE_END