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