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/// Atomic sequence of commands (https://redis.io/topics/transactions).
18/// Please note that Redis transaction implements isolation, but not
19/// all-or-nothing semantics (IOW a subcommand may fail, but the following
20/// subcommands will succeed).
21/// Methods will add commands to the `Transaction` object.
22/// For each added command a future-like object will be returned.
23/// You can get the result of each transaction's subcommand by calling `Get()`
24/// method for these objects.
25/// Commands will be sent to a server after calling `Exec()` that returns
26/// `RequestExec` object.
27/// You should not call `Get()` method in a future-like subcommand's object
28/// before calling `Get()` method on `RequestExec` object.
30public:
31 enum class CheckShards { kNo, kSame };
32
33 virtual ~Transaction() = default;
34
35 /// Finish current atomic sequence of commands and send it to a server.
36 /// Returns 'future-like' request object.
37 /// The data will not be set for the future-like objects for subcommands if
38 /// `Get()` method of the returned object is not called or redis did not
39 /// return an array with command responses.
40 /// In the last case `Get()` will throw a corresponding exception.
41 virtual RequestExec Exec(const CommandControl& command_control) = 0;
42
43 // redis commands:
44
45 virtual RequestAppend Append(std::string key, std::string value) = 0;
46
47 virtual RequestBitop Bitop(BitOperation op, std::string dest, std::vector<std::string> srcs) = 0;
48
49 virtual RequestDbsize Dbsize(size_t shard) = 0;
50
51 virtual RequestDecr Decr(std::string key) = 0;
52
53 virtual RequestDel Del(std::string key) = 0;
54
55 virtual RequestDel Del(std::vector<std::string> keys) = 0;
56
57 virtual RequestUnlink Unlink(std::string key) = 0;
58
59 virtual RequestUnlink Unlink(std::vector<std::string> keys) = 0;
60
61 virtual RequestExists Exists(std::string key) = 0;
62
63 virtual RequestExists Exists(std::vector<std::string> keys) = 0;
64
65 virtual RequestExpire Expire(std::string key, std::chrono::seconds ttl) = 0;
66
67 virtual RequestGeoadd Geoadd(std::string key, GeoaddArg point_member) = 0;
68
69 virtual RequestGeoadd Geoadd(std::string key, std::vector<GeoaddArg> point_members) = 0;
70
71 virtual RequestGeoradius Georadius(
72 std::string key,
73 Longitude lon,
74 Latitude lat,
75 double radius,
76 const GeoradiusOptions& georadius_options
77 ) = 0;
78
79 virtual RequestGeosearch
80 Geosearch(std::string key, std::string member, double radius, const GeosearchOptions& geosearch_options) = 0;
81
82 virtual RequestGeosearch Geosearch(
83 std::string key,
84 std::string member,
85 BoxWidth width,
86 BoxHeight height,
87 const GeosearchOptions& geosearch_options
88 ) = 0;
89
90 virtual RequestGeosearch Geosearch(
91 std::string key,
92 Longitude lon,
93 Latitude lat,
94 double radius,
95 const GeosearchOptions& geosearch_options
96 ) = 0;
97
98 virtual RequestGeosearch Geosearch(
99 std::string key,
100 Longitude lon,
101 Latitude lat,
102 BoxWidth width,
103 BoxHeight height,
104 const GeosearchOptions& geosearch_options
105 ) = 0;
106
107 virtual RequestGet Get(std::string key) = 0;
108
109 virtual RequestGetset Getset(std::string key, std::string value) = 0;
110
111 virtual RequestHdel Hdel(std::string key, std::string field) = 0;
112
113 virtual RequestHdel Hdel(std::string key, std::vector<std::string> fields) = 0;
114
115 virtual RequestHexists Hexists(std::string key, std::string field) = 0;
116
117 virtual RequestHget Hget(std::string key, std::string field) = 0;
118
119 virtual RequestHgetall Hgetall(std::string key) = 0;
120
121 virtual RequestHincrby Hincrby(std::string key, std::string field, int64_t increment) = 0;
122
123 virtual RequestHincrbyfloat Hincrbyfloat(std::string key, std::string field, double increment) = 0;
124
125 virtual RequestHkeys Hkeys(std::string key) = 0;
126
127 virtual RequestHlen Hlen(std::string key) = 0;
128
129 virtual RequestHmget Hmget(std::string key, std::vector<std::string> fields) = 0;
130
131 virtual RequestHmset Hmset(std::string key, std::vector<std::pair<std::string, std::string>> field_values) = 0;
132
133 virtual RequestHset Hset(std::string key, std::string field, std::string value) = 0;
134
135 virtual RequestHsetnx Hsetnx(std::string key, std::string field, std::string value) = 0;
136
137 virtual RequestHvals Hvals(std::string key) = 0;
138
139 virtual RequestIncr Incr(std::string key) = 0;
140
141 virtual RequestKeys Keys(std::string keys_pattern, size_t shard) = 0;
142
143 virtual RequestLindex Lindex(std::string key, int64_t index) = 0;
144
145 virtual RequestLlen Llen(std::string key) = 0;
146
147 virtual RequestLpop Lpop(std::string key) = 0;
148
149 virtual RequestLpush Lpush(std::string key, std::string value) = 0;
150
151 virtual RequestLpush Lpush(std::string key, std::vector<std::string> values) = 0;
152
153 virtual RequestLpushx Lpushx(std::string key, std::string element) = 0;
154
155 virtual RequestLrange Lrange(std::string key, int64_t start, int64_t stop) = 0;
156
157 virtual RequestLrem Lrem(std::string key, int64_t count, std::string element) = 0;
158
159 virtual RequestLtrim Ltrim(std::string key, int64_t start, int64_t stop) = 0;
160
161 virtual RequestMget Mget(std::vector<std::string> keys) = 0;
162
163 virtual RequestMset Mset(std::vector<std::pair<std::string, std::string>> key_values) = 0;
164
165 virtual RequestPersist Persist(std::string key) = 0;
166
167 virtual RequestPexpire Pexpire(std::string key, std::chrono::milliseconds ttl) = 0;
168
169 virtual RequestPing Ping(size_t shard) = 0;
170
171 virtual RequestPingMessage PingMessage(size_t shard, std::string message) = 0;
172
173 virtual RequestRename Rename(std::string key, std::string new_key) = 0;
174
175 virtual RequestRpop Rpop(std::string key) = 0;
176
177 virtual RequestRpush Rpush(std::string key, std::string value) = 0;
178
179 virtual RequestRpush Rpush(std::string key, std::vector<std::string> values) = 0;
180
181 virtual RequestRpushx Rpushx(std::string key, std::string element) = 0;
182
183 virtual RequestSadd Sadd(std::string key, std::string member) = 0;
184
185 virtual RequestSadd Sadd(std::string key, std::vector<std::string> members) = 0;
186
187 virtual RequestScard Scard(std::string key) = 0;
188
189 virtual RequestSet Set(std::string key, std::string value) = 0;
190
191 virtual RequestSet Set(std::string key, std::string value, std::chrono::milliseconds ttl) = 0;
192
193 virtual RequestSetIfExist SetIfExist(std::string key, std::string value) = 0;
194
195 virtual RequestSetIfExist SetIfExist(std::string key, std::string value, std::chrono::milliseconds ttl) = 0;
196
197 virtual RequestSetIfNotExist SetIfNotExist(std::string key, std::string value) = 0;
198
199 virtual RequestSetIfNotExist SetIfNotExist(std::string key, std::string value, std::chrono::milliseconds ttl) = 0;
200
201 virtual RequestSetex Setex(std::string key, std::chrono::seconds seconds, std::string value) = 0;
202
203 virtual RequestSismember Sismember(std::string key, std::string member) = 0;
204
205 virtual RequestSmembers Smembers(std::string key) = 0;
206
207 virtual RequestSrandmember Srandmember(std::string key) = 0;
208
209 virtual RequestSrandmembers Srandmembers(std::string key, int64_t count) = 0;
210
211 virtual RequestSrem Srem(std::string key, std::string member) = 0;
212
213 virtual RequestSrem Srem(std::string key, std::vector<std::string> members) = 0;
214
215 virtual RequestStrlen Strlen(std::string key) = 0;
216
217 virtual RequestTime Time(size_t shard) = 0;
218
219 virtual RequestTtl Ttl(std::string key) = 0;
220
221 virtual RequestType Type(std::string key) = 0;
222
223 virtual RequestZadd Zadd(std::string key, double score, std::string member) = 0;
224
225 virtual RequestZadd Zadd(std::string key, double score, std::string member, const ZaddOptions& options) = 0;
226
227 virtual RequestZadd Zadd(std::string key, std::vector<std::pair<double, std::string>> scored_members) = 0;
228
229 virtual RequestZadd
230 Zadd(std::string key, std::vector<std::pair<double, std::string>> scored_members, const ZaddOptions& options) = 0;
231
232 virtual RequestZaddIncr ZaddIncr(std::string key, double score, std::string member) = 0;
233
234 virtual RequestZaddIncrExisting ZaddIncrExisting(std::string key, double score, std::string member) = 0;
235
236 virtual RequestZcard Zcard(std::string key) = 0;
237
238 virtual RequestZcount Zcount(std::string key, double min, double max) = 0;
239
240 virtual RequestZrange Zrange(std::string key, int64_t start, int64_t stop) = 0;
241
242 virtual RequestZrangeWithScores ZrangeWithScores(std::string key, int64_t start, int64_t stop) = 0;
243
244 virtual RequestZrangebyscore Zrangebyscore(std::string key, double min, double max) = 0;
245
246 virtual RequestZrangebyscore Zrangebyscore(std::string key, std::string min, std::string max) = 0;
247
248 virtual RequestZrangebyscore
249 Zrangebyscore(std::string key, double min, double max, const RangeOptions& range_options) = 0;
250
251 virtual RequestZrangebyscore
252 Zrangebyscore(std::string key, std::string min, std::string max, const RangeOptions& range_options) = 0;
253
254 virtual RequestZrangebyscoreWithScores ZrangebyscoreWithScores(std::string key, double min, double max) = 0;
255
256 virtual RequestZrangebyscoreWithScores
257 ZrangebyscoreWithScores(std::string key, std::string min, std::string max) = 0;
258
259 virtual RequestZrangebyscoreWithScores
260 ZrangebyscoreWithScores(std::string key, double min, double max, const RangeOptions& range_options) = 0;
261
262 virtual RequestZrangebyscoreWithScores
263 ZrangebyscoreWithScores(std::string key, std::string min, std::string max, const RangeOptions& range_options) = 0;
264
265 virtual RequestZrem Zrem(std::string key, std::string member) = 0;
266
267 virtual RequestZrem Zrem(std::string key, std::vector<std::string> members) = 0;
268
269 virtual RequestZremrangebyrank Zremrangebyrank(std::string key, int64_t start, int64_t stop) = 0;
270
271 virtual RequestZremrangebyscore Zremrangebyscore(std::string key, double min, double max) = 0;
272
273 virtual RequestZremrangebyscore Zremrangebyscore(std::string key, std::string min, std::string max) = 0;
274
275 virtual RequestZscore Zscore(std::string key, std::string member) = 0;
276
277 // end of redis commands
278};
279
281
282class EmptyTransactionException : public USERVER_NAMESPACE::redis::Exception {
283public:
284 using USERVER_NAMESPACE::redis::Exception::Exception;
285};
286
287class NotStartedTransactionException : public USERVER_NAMESPACE::redis::Exception {
288public:
289 using USERVER_NAMESPACE::redis::Exception::Exception;
290};
291
292} // namespace storages::redis
293
294USERVER_NAMESPACE_END