userver: userver/storages/redis/command_options.hpp Source File
Loading...
Searching...
No Matches
command_options.hpp
Go to the documentation of this file.
1#pragma once
2
5
6#include <optional>
7#include <string>
8#include <vector>
9
16
17USERVER_NAMESPACE_BEGIN
18
19namespace storages::redis {
20
21using Longitude = utils::StrongTypedef<struct LongitudeTag, double>;
22using Latitude = utils::StrongTypedef<struct LatitudeTag, double>;
23using BoxWidth = utils::StrongTypedef<struct BoxWidthTag, double>;
24using BoxHeight = utils::StrongTypedef<struct BoxHeightTag, double>;
25
27class ExecCondition {
28public:
30 enum class Type {
39 };
40
42 static ExecCondition IfEq(std::string key, std::string value);
44 static ExecCondition IfNe(std::string key, std::string value);
46 static ExecCondition Nx(std::string key);
48 static ExecCondition Xx(std::string key);
49
51 Type GetType() const { return type_; }
53 const std::string& GetKey() const { return key_; }
55 const std::string& GetValue() const { return value_; }
56
57private:
58 ExecCondition(Type type, std::string key, std::string value);
59
60 Type type_;
61 std::string key_;
62 std::string value_;
63};
64
71
73 std::optional<size_t> offset;
74 std::optional<size_t> count;
75};
76
77struct GeoaddArg {
78 double lon;
79 double lat;
80 std::string member;
81};
82
84 enum class Sort { kNone, kAsc, kDesc };
85 enum class Unit { kM, kKm, kMi, kFt };
86
87 Unit unit = Unit::kM;
88 bool withcoord = false;
89 bool withdist = false;
90 bool withhash = false;
91 size_t count = 0;
92 Sort sort = Sort::kNone;
93};
94
96 enum class Sort { kNone, kAsc, kDesc };
97 enum class Unit { kM, kKm, kMi, kFt };
98
99 Unit unit = Unit::kM;
100 bool withcoord = false;
101 bool withdist = false;
102 bool withhash = false;
103 size_t count = 0;
104 Sort sort = Sort::kNone;
105};
106
107struct ZaddOptions {
108 enum class Exist { kAddAlways, kAddIfNotExist, kAddIfExist };
109 enum class Compare { kNone, kGreaterThan, kLessThan };
110 enum class ReturnValue { kAddedCount, kChangedCount };
111
112 ZaddOptions() = default;
113 constexpr ZaddOptions(
114 Exist exist,
115 ReturnValue return_value = ReturnValue::kAddedCount,
116 Compare compare = Compare::kNone
117 )
118 : exist(exist),
119 compare(compare),
120 return_value(return_value)
121 {}
122 constexpr ZaddOptions(Exist exist, Compare compare, ReturnValue return_value = ReturnValue::kAddedCount)
123 : exist(exist),
124 compare(compare),
125 return_value(return_value)
126 {}
127
128 constexpr ZaddOptions(ReturnValue return_value, Exist exist = Exist::kAddAlways, Compare compare = Compare::kNone)
129 : exist(exist),
130 compare(compare),
131 return_value(return_value)
132 {}
133 constexpr ZaddOptions(ReturnValue return_value, Compare compare, Exist exist = Exist::kAddAlways)
134 : exist(exist),
135 compare(compare),
136 return_value(return_value)
137 {}
138
139 constexpr ZaddOptions(
140 Compare compare,
141 Exist exist = Exist::kAddAlways,
142 ReturnValue return_value = ReturnValue::kAddedCount
143 )
144 : exist(exist),
145 compare(compare),
146 return_value(return_value)
147 {}
148 constexpr ZaddOptions(Compare compare, ReturnValue return_value, Exist exist = Exist::kAddAlways)
149 : exist(exist),
150 compare(compare),
151 return_value(return_value)
152 {}
153
154 Exist exist = Exist::kAddAlways;
155 Compare compare = Compare::kNone;
156 ReturnValue return_value = ReturnValue::kAddedCount;
157};
158
159constexpr ZaddOptions operator|(ZaddOptions::Exist exist, ZaddOptions::ReturnValue return_value) {
160 return {exist, return_value};
161}
162constexpr ZaddOptions operator|(ZaddOptions::Exist exist, ZaddOptions::Compare compare) { return {exist, compare}; }
163constexpr ZaddOptions operator|(ZaddOptions::Compare compare, ZaddOptions::Exist exist) { return {compare, exist}; }
164constexpr ZaddOptions operator|(ZaddOptions::Compare compare, ZaddOptions::ReturnValue return_value) {
165 return {compare, return_value};
166}
167constexpr ZaddOptions operator|(ZaddOptions::ReturnValue return_value, ZaddOptions::Exist exist) {
168 return {return_value, exist};
169}
170constexpr ZaddOptions operator|(ZaddOptions::ReturnValue return_value, ZaddOptions::Compare compare) {
171 return {return_value, compare};
172}
173
178class Match final {
179public:
180 explicit Match(std::string value)
181 : value_(std::move(value))
182 {}
183
184 const std::string& Get() const& { return value_; }
185
186 std::string Get() && { return std::move(value_); }
187
188private:
189 std::string value_;
190};
191
193class Count final {
194public:
195 constexpr explicit Count(std::size_t value) noexcept : value_(value) {}
196
197 constexpr std::size_t Get() const noexcept { return value_; }
198
199private:
200 std::size_t value_;
201};
202
204class ScanOptionsGeneric {
205public:
206 using Match = storages::redis::Match;
207 using Count = storages::redis::Count;
208
209 ScanOptionsGeneric() = default;
210
211 ScanOptionsGeneric(const ScanOptionsGeneric& other) = default;
212 ScanOptionsGeneric& operator=(const ScanOptionsGeneric& other) = default;
213
214 ScanOptionsGeneric(ScanOptionsGeneric&& other) = default;
215 ScanOptionsGeneric& operator=(ScanOptionsGeneric&& other) = default;
216
217 template <typename... Args>
218 ScanOptionsGeneric(Args... args) {
219 (Apply(std::move(args)), ...);
220 }
221
222 const std::optional<Match>& GetMatchOptional() const noexcept { return pattern_; }
223
224 const std::optional<Count>& GetCountOptional() const noexcept { return count_; }
225
226 std::optional<Match> ExtractMatch() noexcept { return std::move(pattern_); }
227
228 std::optional<Count> ExtractCount() noexcept { return std::move(count_); }
229
230private:
231 void Apply(Match pattern) {
232 if (pattern_) {
233 throw InvalidArgumentException("duplicate Match parameter");
234 }
235 pattern_ = std::move(pattern);
236 }
237
238 void Apply(Count count) {
239 if (count_) {
240 throw InvalidArgumentException("duplicate Count parameter");
241 }
242 count_ = count;
243 }
244
245 std::optional<Match> pattern_;
246 std::optional<Count> count_;
247};
248
249template <ScanTag TScanTag>
250using ScanOptionsTmpl [[deprecated("Just use ScanOptionsGeneric")]] = ScanOptionsGeneric;
251
252using ScanOptions = ScanOptionsGeneric;
253using SscanOptions = ScanOptionsGeneric;
254using HscanOptions = ScanOptionsGeneric;
255using ZscanOptions = ScanOptionsGeneric;
256
258 enum class Exist { kSetAlways, kSetIfNotExist, kSetIfExist };
259
260 int seconds = 0;
261 int milliseconds = 0;
262 Exist exist = Exist::kSetAlways;
263};
264
268 enum class Exist : std::uint8_t {
275 };
276
291
293 TtlAction ttl_action{TtlAction::kNone};
294 std::chrono::milliseconds ttl{0};
295
301 static MsetexOptions Expire(std::chrono::milliseconds ttl);
303 static MsetexOptions ExpireAt(std::chrono::system_clock::time_point deadline);
304
309 return *this;
310 }
311
314 exist = Exist::kSetIfAllExist;
315 return *this;
316 }
317
320 return std::move(*this);
321 }
322
324 exist = Exist::kSetIfAllExist;
325 return std::move(*this);
326 }
327};
328
329struct ExpireOptions {
330 enum class Exist { kSetAlways, kSetIfNotExist, kSetIfExist };
331 enum class Compare { kNone, kGreaterThan, kLessThan };
332
333 ExpireOptions() = default;
334 constexpr ExpireOptions(Exist exist, Compare compare = Compare::kNone)
335 : exist(exist),
336 compare(compare)
337 {
338 if (exist == Exist::kSetIfNotExist && compare != Compare::kNone) {
339 // @see https://redis-docs.ru/commands/expire/
340 throw std::invalid_argument("When exist is kSetIfNotExist, compare must be kNone");
341 }
342 }
343
344 constexpr ExpireOptions(Compare compare, Exist exist = Exist::kSetAlways)
345 : exist(exist),
346 compare(compare)
347 {
348 if (exist == Exist::kSetIfNotExist && compare != Compare::kNone) {
349 // @see https://redis-docs.ru/commands/expire/
350 throw std::invalid_argument("When exist is kSetIfNotExist, compare must be kNone");
351 }
352 }
353
354 Exist exist = Exist::kSetAlways;
355 Compare compare = Compare::kNone;
356};
357
359 bool withscores = false;
360};
361
363 ScoreOptions score_options;
364 RangeOptions range_options;
365};
366
368struct JsonKeyPathValue final {
369 std::string key;
370 std::string path;
372};
373
391
392 TtlAction ttl_action{TtlAction::kKeep};
393 std::chrono::milliseconds ttl{0};
394
400 static HgetexOptions Expire(std::chrono::milliseconds ttl);
402 static HgetexOptions ExpireAt(std::chrono::system_clock::time_point deadline);
403};
404
407 std::string field;
408 std::string value;
409};
410
414 enum class Exist : std::uint8_t {
421 };
422
437
439 TtlAction ttl_action{TtlAction::kNone};
440 std::chrono::milliseconds ttl{0};
441
447 static HsetexOptions Expire(std::chrono::milliseconds ttl);
449 static HsetexOptions ExpireAt(std::chrono::system_clock::time_point deadline);
450
455 return *this;
456 }
457
460 exist = Exist::kSetIfAllExist;
461 return *this;
462 }
463
466 return std::move(*this);
467 }
468
470 exist = Exist::kSetIfAllExist;
471 return std::move(*this);
472 }
473};
474
475} // namespace storages::redis
476
477USERVER_NAMESPACE_END