userver: userver/storages/redis/command_options.hpp Source File
Loading...
Searching...
No Matches
command_options.hpp
1#pragma once
2
3#include <optional>
4#include <string>
5
6#include <userver/storages/redis/exception.hpp>
7#include <userver/storages/redis/impl/base.hpp>
8#include <userver/storages/redis/impl/command_options.hpp>
9
10#include <userver/storages/redis/scan_tag.hpp>
11
12USERVER_NAMESPACE_BEGIN
13
14namespace storages::redis {
15
16using Longitude = USERVER_NAMESPACE::redis::Longitude;
17using Latitude = USERVER_NAMESPACE::redis::Latitude;
18using BoxWidth = USERVER_NAMESPACE::redis::BoxWidth;
19using BoxHeight = USERVER_NAMESPACE::redis::BoxHeight;
20using CommandControl = USERVER_NAMESPACE::redis::CommandControl;
21using RangeOptions = USERVER_NAMESPACE::redis::RangeOptions;
22using GeoaddArg = USERVER_NAMESPACE::redis::GeoaddArg;
23using GeoradiusOptions = USERVER_NAMESPACE::redis::GeoradiusOptions;
24using GeosearchOptions = USERVER_NAMESPACE::redis::GeosearchOptions;
25using ZaddOptions = USERVER_NAMESPACE::redis::ZaddOptions;
26
27class ScanOptionsBase {
28public:
29 ScanOptionsBase() = default;
30 ScanOptionsBase(ScanOptionsBase& other) = default;
31 ScanOptionsBase(const ScanOptionsBase& other) = default;
32
33 ScanOptionsBase(ScanOptionsBase&& other) = default;
34
35 template <typename... Args>
36 ScanOptionsBase(Args&&... args) {
37 (Apply(std::forward<Args>(args)), ...);
38 }
39
40 class Match {
41 public:
42 explicit Match(std::string value) : value_(std::move(value)) {}
43
44 const std::string& Get() const& { return value_; }
45
46 std::string Get() && { return std::move(value_); }
47
48 private:
49 std::string value_;
50 };
51
52 class Count {
53 public:
54 explicit Count(size_t value) : value_(value) {}
55
56 size_t Get() const { return value_; }
57
58 private:
59 size_t value_;
60 };
61
62 std::optional<Match> ExtractMatch() { return std::move(pattern_); }
63
64 std::optional<Count> ExtractCount() { return std::move(count_); }
65
66private:
67 void Apply(Match pattern) {
68 if (pattern_) throw USERVER_NAMESPACE::redis::InvalidArgumentException("duplicate Match parameter");
69 pattern_ = std::move(pattern);
70 }
71
72 void Apply(Count count) {
73 if (count_) throw USERVER_NAMESPACE::redis::InvalidArgumentException("duplicate Count parameter");
74 count_ = count;
75 }
76
77 std::optional<Match> pattern_;
78 std::optional<Count> count_;
79};
80
81// strong typedef
82template <ScanTag scan_tag>
83class ScanOptionsTmpl : public ScanOptionsBase {
84 using ScanOptionsBase::ScanOptionsBase;
85};
86
87using ScanOptions = ScanOptionsTmpl<ScanTag::kScan>;
88using SscanOptions = ScanOptionsTmpl<ScanTag::kSscan>;
89using HscanOptions = ScanOptionsTmpl<ScanTag::kHscan>;
90using ZscanOptions = ScanOptionsTmpl<ScanTag::kZscan>;
91
92void PutArg(USERVER_NAMESPACE::redis::CmdArgs::CmdArgsArray& args_, std::optional<ScanOptionsBase::Match> arg);
93void PutArg(USERVER_NAMESPACE::redis::CmdArgs::CmdArgsArray& args_, std::optional<ScanOptionsBase::Count> arg);
94
95} // namespace storages::redis
96
97USERVER_NAMESPACE_END