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
3/// @file
4/// @brief Definitions of structures representing options for different commands
5
6#include <optional>
7#include <string>
8#include <vector>
9
10#include <userver/storages/redis/base.hpp>
11#include <userver/storages/redis/command_control.hpp>
12#include <userver/storages/redis/exception.hpp>
13#include <userver/storages/redis/scan_tag.hpp>
14
15USERVER_NAMESPACE_BEGIN
16
17namespace storages::redis {
18
19using Longitude = utils::StrongTypedef<struct LongitudeTag, double>;
20using Latitude = utils::StrongTypedef<struct LatitudeTag, double>;
21using BoxWidth = utils::StrongTypedef<struct BoxWidthTag, double>;
22using BoxHeight = utils::StrongTypedef<struct BoxHeightTag, double>;
23
25 std::optional<size_t> offset;
26 std::optional<size_t> count;
27};
28
29struct GeoaddArg {
30 double lon;
31 double lat;
32 std::string member;
33};
34
36 enum class Sort { kNone, kAsc, kDesc };
37 enum class Unit { kM, kKm, kMi, kFt };
38
39 Unit unit = Unit::kM;
40 bool withcoord = false;
41 bool withdist = false;
42 bool withhash = false;
43 size_t count = 0;
44 Sort sort = Sort::kNone;
45};
46
48 enum class Sort { kNone, kAsc, kDesc };
49 enum class Unit { kM, kKm, kMi, kFt };
50
51 Unit unit = Unit::kM;
52 bool withcoord = false;
53 bool withdist = false;
54 bool withhash = false;
55 size_t count = 0;
56 Sort sort = Sort::kNone;
57};
58
60 enum class Exist { kAddAlways, kAddIfNotExist, kAddIfExist };
61 enum class Compare { kNone, kGreaterThan, kLessThan };
62 enum class ReturnValue { kAddedCount, kChangedCount };
63
64 ZaddOptions() = default;
65 constexpr ZaddOptions(
66 Exist exist,
67 ReturnValue return_value = ReturnValue::kAddedCount,
68 Compare compare = Compare::kNone
69 )
70 : exist(exist),
71 compare(compare),
72 return_value(return_value)
73 {}
74 constexpr ZaddOptions(Exist exist, Compare compare, ReturnValue return_value = ReturnValue::kAddedCount)
75 : exist(exist),
76 compare(compare),
77 return_value(return_value)
78 {}
79
80 constexpr ZaddOptions(ReturnValue return_value, Exist exist = Exist::kAddAlways, Compare compare = Compare::kNone)
81 : exist(exist),
82 compare(compare),
83 return_value(return_value)
84 {}
85 constexpr ZaddOptions(ReturnValue return_value, Compare compare, Exist exist = Exist::kAddAlways)
86 : exist(exist),
87 compare(compare),
88 return_value(return_value)
89 {}
90
91 constexpr ZaddOptions(
92 Compare compare,
93 Exist exist = Exist::kAddAlways,
94 ReturnValue return_value = ReturnValue::kAddedCount
95 )
96 : exist(exist),
97 compare(compare),
98 return_value(return_value)
99 {}
100 constexpr ZaddOptions(Compare compare, ReturnValue return_value, Exist exist = Exist::kAddAlways)
101 : exist(exist),
102 compare(compare),
103 return_value(return_value)
104 {}
105
106 Exist exist = Exist::kAddAlways;
107 Compare compare = Compare::kNone;
108 ReturnValue return_value = ReturnValue::kAddedCount;
109};
110
111constexpr ZaddOptions operator|(ZaddOptions::Exist exist, ZaddOptions::ReturnValue return_value) {
112 return {exist, return_value};
113}
114constexpr ZaddOptions operator|(ZaddOptions::Exist exist, ZaddOptions::Compare compare) { return {exist, compare}; }
115constexpr ZaddOptions operator|(ZaddOptions::Compare compare, ZaddOptions::Exist exist) { return {compare, exist}; }
116constexpr ZaddOptions operator|(ZaddOptions::Compare compare, ZaddOptions::ReturnValue return_value) {
117 return {compare, return_value};
118}
119constexpr ZaddOptions operator|(ZaddOptions::ReturnValue return_value, ZaddOptions::Exist exist) {
120 return {return_value, exist};
121}
122constexpr ZaddOptions operator|(ZaddOptions::ReturnValue return_value, ZaddOptions::Compare compare) {
123 return {return_value, compare};
124}
125
126/// @brief A command option to use in Scan, Hscan, Scan and Zscan commands.
127///
128/// Sample usage:
129/// @snippet redis/src/storages/redis/client_scan_redistest.cpp Sample Scan usage
130class Match final {
131public:
132 explicit Match(std::string value)
133 : value_(std::move(value))
134 {}
135
136 const std::string& Get() const& { return value_; }
137
138 std::string Get() && { return std::move(value_); }
139
140private:
141 std::string value_;
142};
143
144/// @brief A hint for Scan, Hscan, Scan and Zscan commands.
145class Count final {
146public:
147 explicit constexpr Count(std::size_t value) noexcept : value_(value) {}
148
149 constexpr std::size_t Get() const noexcept { return value_; }
150
151private:
152 std::size_t value_;
153};
154
155/// @brief A command option to use in Scan, Hscan, Scan and Zscan commands that combines Match and Count options.
157public:
158 using Match = storages::redis::Match;
159 using Count = storages::redis::Count;
160
161 ScanOptionsGeneric() = default;
162
163 ScanOptionsGeneric(const ScanOptionsGeneric& other) = default;
164 ScanOptionsGeneric& operator=(const ScanOptionsGeneric& other) = default;
165
166 ScanOptionsGeneric(ScanOptionsGeneric&& other) = default;
167 ScanOptionsGeneric& operator=(ScanOptionsGeneric&& other) = default;
168
169 template <typename... Args>
170 ScanOptionsGeneric(Args... args) {
171 (Apply(std::move(args)), ...);
172 }
173
174 const std::optional<Match>& GetMatchOptional() const noexcept { return pattern_; }
175
176 const std::optional<Count>& GetCountOptional() const noexcept { return count_; }
177
178 std::optional<Match> ExtractMatch() noexcept { return std::move(pattern_); }
179
180 std::optional<Count> ExtractCount() noexcept { return std::move(count_); }
181
182private:
183 void Apply(Match pattern) {
184 if (pattern_) {
185 throw InvalidArgumentException("duplicate Match parameter");
186 }
187 pattern_ = std::move(pattern);
188 }
189
190 void Apply(Count count) {
191 if (count_) {
192 throw InvalidArgumentException("duplicate Count parameter");
193 }
194 count_ = count;
195 }
196
197 std::optional<Match> pattern_;
198 std::optional<Count> count_;
199};
200
201template <ScanTag TScanTag>
202using ScanOptionsTmpl [[deprecated("Just use ScanOptionsGeneric")]] = ScanOptionsGeneric;
203
204using ScanOptions = ScanOptionsGeneric;
205using SscanOptions = ScanOptionsGeneric;
206using HscanOptions = ScanOptionsGeneric;
207using ZscanOptions = ScanOptionsGeneric;
208
210 enum class Exist { kSetAlways, kSetIfNotExist, kSetIfExist };
211
212 int seconds = 0;
213 int milliseconds = 0;
214 Exist exist = Exist::kSetAlways;
215};
216
218 enum class Exist { kSetAlways, kSetIfNotExist, kSetIfExist };
219 enum class Compare { kNone, kGreaterThan, kLessThan };
220
221 ExpireOptions() = default;
222 constexpr ExpireOptions(Exist exist, Compare compare = Compare::kNone)
223 : exist(exist),
224 compare(compare)
225 {
226 if (exist == Exist::kSetIfNotExist && compare != Compare::kNone) {
227 // @see https://redis-docs.ru/commands/expire/
228 throw std::invalid_argument("When exist is kSetIfNotExist, compare must be kNone");
229 }
230 }
231
232 constexpr ExpireOptions(Compare compare, Exist exist = Exist::kSetAlways)
233 : exist(exist),
234 compare(compare)
235 {
236 if (exist == Exist::kSetIfNotExist && compare != Compare::kNone) {
237 // @see https://redis-docs.ru/commands/expire/
238 throw std::invalid_argument("When exist is kSetIfNotExist, compare must be kNone");
239 }
240 }
241
242 Exist exist = Exist::kSetAlways;
243 Compare compare = Compare::kNone;
244};
245
247 bool withscores = false;
248};
249
251 ScoreOptions score_options;
252 RangeOptions range_options;
253};
254
255} // namespace storages::redis
256
257USERVER_NAMESPACE_END