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 virtual RequestPersist Persist(std::string key) = 0;
177
178 virtual RequestPexpire Pexpire(std::string key, std::chrono::milliseconds ttl) = 0;
179
180 virtual RequestPing Ping(size_t shard) = 0;
181
182 virtual RequestPingMessage PingMessage(size_t shard, std::string message) = 0;
183
184 virtual RequestRename Rename(std::string key, std::string new_key) = 0;
185
186 virtual RequestRpop Rpop(std::string key) = 0;
187
188 virtual RequestRpush Rpush(std::string key, std::string value) = 0;
189
190 virtual RequestRpush Rpush(std::string key, std::vector<std::string> values) = 0;
191
192 virtual RequestRpushx Rpushx(std::string key, std::string element) = 0;
193
194 virtual RequestSadd Sadd(std::string key, std::string member) = 0;
195
196 virtual RequestSadd Sadd(std::string key, std::vector<std::string> members) = 0;
197
198 virtual RequestScard Scard(std::string key) = 0;
199
200 virtual RequestSet Set(std::string key, std::string value) = 0;
201
202 virtual RequestSet Set(std::string key, std::string value, std::chrono::milliseconds ttl) = 0;
203
204 virtual RequestSetIfExist SetIfExist(std::string key, std::string value) = 0;
205
206 virtual RequestSetIfExist SetIfExist(std::string key, std::string value, std::chrono::milliseconds ttl) = 0;
207
208 virtual RequestSetIfNotExist SetIfNotExist(std::string key, std::string value) = 0;
209
210 virtual RequestSetIfNotExist SetIfNotExist(std::string key, std::string value, std::chrono::milliseconds ttl) = 0;
211
212 virtual RequestSetIfNotExistOrGet SetIfNotExistOrGet(std::string key, std::string value) = 0;
213
214 virtual RequestSetIfNotExistOrGet SetIfNotExistOrGet(
215 std::string key,
216 std::string value,
217 std::chrono::milliseconds ttl
218 ) = 0;
219
220 virtual RequestSetex Setex(std::string key, std::chrono::seconds seconds, std::string value) = 0;
221
222 virtual RequestSetAndGetPrevious SetAndGetPrevious(
223 std::string key,
224 std::string value,
225 std::chrono::milliseconds ttl
226 ) = 0;
227
228 virtual RequestSismember Sismember(std::string key, std::string member) = 0;
229
230 virtual RequestSmembers Smembers(std::string key) = 0;
231
232 virtual RequestSrandmember Srandmember(std::string key) = 0;
233
234 virtual RequestSrandmembers Srandmembers(std::string key, int64_t count) = 0;
235
236 virtual RequestSrem Srem(std::string key, std::string member) = 0;
237
238 virtual RequestSrem Srem(std::string key, std::vector<std::string> members) = 0;
239
240 virtual RequestStrlen Strlen(std::string key) = 0;
241
242 virtual RequestTime Time(size_t shard) = 0;
243
244 virtual RequestTtl Ttl(std::string key) = 0;
245
246 virtual RequestType Type(std::string key) = 0;
247
248 virtual RequestZadd Zadd(std::string key, double score, std::string member) = 0;
249
250 virtual RequestZadd Zadd(std::string key, double score, std::string member, const ZaddOptions& options) = 0;
251
252 virtual RequestZadd Zadd(std::string key, std::vector<std::pair<double, std::string>> scored_members) = 0;
253
254 virtual RequestZadd Zadd(
255 std::string key,
256 std::vector<std::pair<double, std::string>> scored_members,
257 const ZaddOptions& options
258 ) = 0;
259
260 virtual RequestZaddIncr ZaddIncr(std::string key, double score, std::string member) = 0;
261
262 virtual RequestZaddIncrExisting ZaddIncrExisting(std::string key, double score, std::string member) = 0;
263
264 virtual RequestZcard Zcard(std::string key) = 0;
265
266 virtual RequestZcount Zcount(std::string key, double min, double max) = 0;
267
268 virtual RequestZrange Zrange(std::string key, int64_t start, int64_t stop) = 0;
269
270 virtual RequestZrangeWithScores ZrangeWithScores(std::string key, int64_t start, int64_t stop) = 0;
271
272 virtual RequestZrangebyscore Zrangebyscore(std::string key, double min, double max) = 0;
273
274 virtual RequestZrangebyscore Zrangebyscore(std::string key, std::string min, std::string max) = 0;
275
276 virtual RequestZrangebyscore Zrangebyscore(
277 std::string key,
278 double min,
279 double max,
280 const RangeOptions& range_options
281 ) = 0;
282
283 virtual RequestZrangebyscore Zrangebyscore(
284 std::string key,
285 std::string min,
286 std::string max,
287 const RangeOptions& range_options
288 ) = 0;
289
290 virtual RequestZrangebyscoreWithScores ZrangebyscoreWithScores(std::string key, double min, double max) = 0;
291
292 virtual RequestZrangebyscoreWithScores ZrangebyscoreWithScores(
293 std::string key,
294 std::string min,
295 std::string max
296 ) = 0;
297
298 virtual RequestZrangebyscoreWithScores ZrangebyscoreWithScores(
299 std::string key,
300 double min,
301 double max,
302 const RangeOptions& range_options
303 ) = 0;
304
305 virtual RequestZrangebyscoreWithScores ZrangebyscoreWithScores(
306 std::string key,
307 std::string min,
308 std::string max,
309 const RangeOptions& range_options
310 ) = 0;
311
312 virtual RequestZrem Zrem(std::string key, std::string member) = 0;
313
314 virtual RequestZrem Zrem(std::string key, std::vector<std::string> members) = 0;
315
316 virtual RequestZremrangebyrank Zremrangebyrank(std::string key, int64_t start, int64_t stop) = 0;
317
318 virtual RequestZremrangebyscore Zremrangebyscore(std::string key, double min, double max) = 0;
319
320 virtual RequestZremrangebyscore Zremrangebyscore(std::string key, std::string min, std::string max) = 0;
321
322 virtual RequestZscore Zscore(std::string key, std::string member) = 0;
323
324 // Hash field expiration commands:
325
326 virtual RequestHexpire Hexpire(std::string key, std::chrono::seconds ttl, std::vector<std::string> fields) = 0;
327
328 virtual RequestHexpire Hexpire(
329 std::string key,
330 std::chrono::seconds ttl,
331 ExpireOptions options,
332 std::vector<std::string> fields
333 ) = 0;
334
335 virtual RequestHexpire Hpexpire(
336 std::string key,
337 std::chrono::milliseconds ttl,
338 std::vector<std::string> fields
339 ) = 0;
340
341 virtual RequestHexpire Hpexpire(
342 std::string key,
343 std::chrono::milliseconds ttl,
344 ExpireOptions options,
345 std::vector<std::string> fields
346 ) = 0;
347
348 virtual RequestHexpire Hexpireat(
349 std::string key,
350 std::chrono::system_clock::time_point deadline,
351 std::vector<std::string> fields
352 ) = 0;
353
354 virtual RequestHexpire Hexpireat(
355 std::string key,
356 std::chrono::system_clock::time_point deadline,
357 ExpireOptions options,
358 std::vector<std::string> fields
359 ) = 0;
360
361 virtual RequestHexpire Hpexpireat(
362 std::string key,
363 std::chrono::system_clock::time_point deadline,
364 std::vector<std::string> fields
365 ) = 0;
366
367 virtual RequestHexpire Hpexpireat(
368 std::string key,
369 std::chrono::system_clock::time_point deadline,
370 ExpireOptions options,
371 std::vector<std::string> fields
372 ) = 0;
373
374 virtual RequestHexpiretime Hexpiretime(std::string key, std::vector<std::string> fields) = 0;
375
376 virtual RequestHpexpiretime Hpexpiretime(std::string key, std::vector<std::string> fields) = 0;
377
378 virtual RequestHttl Httl(std::string key, std::vector<std::string> fields) = 0;
379
380 virtual RequestHpttl Hpttl(std::string key, std::vector<std::string> fields) = 0;
381
382 virtual RequestHpersist Hpersist(std::string key, std::vector<std::string> fields) = 0;
383
384 virtual RequestHgetex Hgetex(std::string key, std::vector<std::string> fields) = 0;
385
386 virtual RequestHgetex Hgetex(std::string key, HgetexOptions options, std::vector<std::string> fields) = 0;
387
388 virtual RequestHsetex Hsetex(std::string key, std::vector<HsetexFieldValue> field_values) = 0;
389
390 virtual RequestHsetex Hsetex(
391 std::string key,
392 HsetexOptions options,
393 std::vector<HsetexFieldValue> field_values
394 ) = 0;
395
396 // JSON module commands:
397
398 virtual RequestJsonSet JsonSet(std::string key, std::string path, formats::json::Value value) = 0;
399
400 virtual RequestJsonSetIfNotExist JsonSetIfNotExist(
401 std::string key,
402 std::string path,
403 formats::json::Value value
404 ) = 0;
405
406 virtual RequestJsonSetIfExist JsonSetIfExist(std::string key, std::string path, formats::json::Value value) = 0;
407
408 virtual RequestJsonGet JsonGet(std::string key) = 0;
409
410 virtual RequestJsonGet JsonGet(std::string key, std::string path) = 0;
411
412 virtual RequestJsonGet JsonGet(std::string key, std::vector<std::string> paths) = 0;
413
414 virtual RequestJsonMget JsonMget(std::vector<std::string> keys, std::string path) = 0;
415
416 virtual RequestJsonMset JsonMset(std::vector<JsonKeyPathValue> key_path_values) = 0;
417
418 // end of redis commands
419};
420
421using TransactionPtr = std::unique_ptr<Transaction>;
422
424public:
425 using Exception::Exception;
426};
427
429public:
430 using Exception::Exception;
431};
432
433} // namespace storages::redis
434
435USERVER_NAMESPACE_END