userver: userver/storages/redis/command_options.hpp Source File
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages Concepts
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), compare(compare), return_value(return_value) {}
71 constexpr ZaddOptions(Exist exist, Compare compare, ReturnValue return_value = ReturnValue::kAddedCount)
72 : exist(exist), compare(compare), return_value(return_value) {}
73
74 constexpr ZaddOptions(ReturnValue return_value, Exist exist = Exist::kAddAlways, Compare compare = Compare::kNone)
75 : exist(exist), compare(compare), return_value(return_value) {}
76 constexpr ZaddOptions(ReturnValue return_value, Compare compare, Exist exist = Exist::kAddAlways)
77 : exist(exist), compare(compare), return_value(return_value) {}
78
79 constexpr ZaddOptions(
80 Compare compare,
81 Exist exist = Exist::kAddAlways,
82 ReturnValue return_value = ReturnValue::kAddedCount
83 )
84 : exist(exist), compare(compare), return_value(return_value) {}
85 constexpr ZaddOptions(Compare compare, ReturnValue return_value, Exist exist = Exist::kAddAlways)
86 : exist(exist), compare(compare), return_value(return_value) {}
87
88 Exist exist = Exist::kAddAlways;
89 Compare compare = Compare::kNone;
90 ReturnValue return_value = ReturnValue::kAddedCount;
91};
92
93constexpr ZaddOptions operator|(ZaddOptions::Exist exist, ZaddOptions::ReturnValue return_value) {
94 return {exist, return_value};
95}
96constexpr ZaddOptions operator|(ZaddOptions::Exist exist, ZaddOptions::Compare compare) { return {exist, compare}; }
97constexpr ZaddOptions operator|(ZaddOptions::Compare compare, ZaddOptions::Exist exist) { return {compare, exist}; }
98constexpr ZaddOptions operator|(ZaddOptions::Compare compare, ZaddOptions::ReturnValue return_value) {
99 return {compare, return_value};
100}
101constexpr ZaddOptions operator|(ZaddOptions::ReturnValue return_value, ZaddOptions::Exist exist) {
102 return {return_value, exist};
103}
104constexpr ZaddOptions operator|(ZaddOptions::ReturnValue return_value, ZaddOptions::Compare compare) {
105 return {return_value, compare};
106}
107
108/// @brief A command option to use in Scan, Hscan, Scan and Zscan commands.
109///
110/// Sample usage:
111/// @snippet redis/src/storages/redis/client_scan_redistest.cpp Sample Scan usage
112class Match final {
113public:
114 explicit Match(std::string value) : value_(std::move(value)) {}
115
116 const std::string& Get() const& { return value_; }
117
118 std::string Get() && { return std::move(value_); }
119
120private:
121 std::string value_;
122};
123
124/// @brief A hint for Scan, Hscan, Scan and Zscan commands.
125class Count final {
126public:
127 explicit constexpr Count(std::size_t value) noexcept : value_(value) {}
128
129 constexpr std::size_t Get() const noexcept { return value_; }
130
131private:
132 std::size_t value_;
133};
134
135/// @brief A command option to use in Scan, Hscan, Scan and Zscan commands that combines Match and Count options.
137public:
138 using Match = storages::redis::Match;
139 using Count = storages::redis::Count;
140
141 ScanOptionsGeneric() = default;
142
143 ScanOptionsGeneric(const ScanOptionsGeneric& other) = default;
144 ScanOptionsGeneric& operator=(const ScanOptionsGeneric& other) = default;
145
146 ScanOptionsGeneric(ScanOptionsGeneric&& other) = default;
147 ScanOptionsGeneric& operator=(ScanOptionsGeneric&& other) = default;
148
149 template <typename... Args>
150 ScanOptionsGeneric(Args... args) {
151 (Apply(std::move(args)), ...);
152 }
153
154 const std::optional<Match>& GetMatchOptional() const noexcept { return pattern_; }
155
156 const std::optional<Count>& GetCountOptional() const noexcept { return count_; }
157
158 std::optional<Match> ExtractMatch() noexcept { return std::move(pattern_); }
159
160 std::optional<Count> ExtractCount() noexcept { return std::move(count_); }
161
162private:
163 void Apply(Match pattern) {
164 if (pattern_) throw InvalidArgumentException("duplicate Match parameter");
165 pattern_ = std::move(pattern);
166 }
167
168 void Apply(Count count) {
169 if (count_) throw InvalidArgumentException("duplicate Count parameter");
170 count_ = count;
171 }
172
173 std::optional<Match> pattern_;
174 std::optional<Count> count_;
175};
176
177template <ScanTag scan_tag>
178using ScanOptionsTmpl [[deprecated("Just use ScanOptionsGeneric")]] = ScanOptionsGeneric;
179
180using ScanOptions = ScanOptionsGeneric;
181using SscanOptions = ScanOptionsGeneric;
182using HscanOptions = ScanOptionsGeneric;
183using ZscanOptions = ScanOptionsGeneric;
184
186 enum class Exist { kSetAlways, kSetIfNotExist, kSetIfExist };
187
188 int seconds = 0;
189 int milliseconds = 0;
190 Exist exist = Exist::kSetAlways;
191};
192
194 bool withscores = false;
195};
196
198 ScoreOptions score_options;
199 RangeOptions range_options;
200};
201
202} // namespace storages::redis
203
204USERVER_NAMESPACE_END