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/impl/base.hpp>
7#include <userver/storages/redis/impl/command_options.hpp>
8#include <userver/storages/redis/impl/exception.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 {
28 public:
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
66 private:
67 void Apply(Match pattern) {
68 if (pattern_)
69 throw USERVER_NAMESPACE::redis::InvalidArgumentException(
70 "duplicate Match parameter");
71 pattern_ = std::move(pattern);
72 }
73
74 void Apply(Count count) {
75 if (count_)
76 throw USERVER_NAMESPACE::redis::InvalidArgumentException(
77 "duplicate Count parameter");
78 count_ = count;
79 }
80
81 std::optional<Match> pattern_;
82 std::optional<Count> count_;
83};
84
85// strong typedef
86template <ScanTag scan_tag>
87class ScanOptionsTmpl : public ScanOptionsBase {
88 using ScanOptionsBase::ScanOptionsBase;
89};
90
91using ScanOptions = ScanOptionsTmpl<ScanTag::kScan>;
92using SscanOptions = ScanOptionsTmpl<ScanTag::kSscan>;
93using HscanOptions = ScanOptionsTmpl<ScanTag::kHscan>;
94using ZscanOptions = ScanOptionsTmpl<ScanTag::kZscan>;
95
96void PutArg(USERVER_NAMESPACE::redis::CmdArgs::CmdArgsArray& args_,
97 std::optional<ScanOptionsBase::Match> arg);
98void PutArg(USERVER_NAMESPACE::redis::CmdArgs::CmdArgsArray& args_,
99 std::optional<ScanOptionsBase::Count> arg);
100
101} // namespace storages::redis
102
103USERVER_NAMESPACE_END