Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion redis/include/userver/storages/redis/client.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,8 @@ class Client {
size_t key_index,
const CommandControl& command_control
) {
return RequestGeneric<ReplyType>{GenericCommon(std::move(command), std::move(args), key_index, command_control)
return RequestGeneric<ReplyType>{
GenericCommon(std::move(command), std::move(args), key_index, command_control)
};
}

Expand Down Expand Up @@ -695,6 +696,12 @@ class Client {

RequestZscan Zscan(std::string key, const CommandControl& command_control);

virtual RequestJsonGet JsonGet(std::string key, const CommandControl& command_control) = 0;

virtual RequestJsonSet JsonSet(std::string key, std::string value, const CommandControl& command_control) = 0;

virtual RequestJsonDel JsonDel(std::string key, const CommandControl& command_control) = 0;

protected:
virtual RequestEvalCommon EvalCommon(
std::string script,
Expand Down
19 changes: 7 additions & 12 deletions redis/include/userver/storages/redis/request.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,7 @@ class [[nodiscard]] Request final {
using Result = ResultType;
using Reply = ReplyType;

explicit Request(std::unique_ptr<RequestDataBase<ReplyType>>&& impl)
: impl_(std::move(impl))
{}
explicit Request(std::unique_ptr<RequestDataBase<ReplyType>>&& impl) : impl_(std::move(impl)) {}

/// Wait for the request to finish on Redis server, server or request errors (if any) are logged but not thrown.
///
Expand Down Expand Up @@ -83,9 +81,7 @@ class ScanRequest final {
public:
using ReplyElem = typename ScanReplyElem<TScanTag>::type;

explicit ScanRequest(std::unique_ptr<RequestScanDataBase<TScanTag>>&& impl)
: impl_(std::move(impl))
{}
explicit ScanRequest(std::unique_ptr<RequestScanDataBase<TScanTag>>&& impl) : impl_(std::move(impl)) {}

template <typename T = std::vector<ReplyElem>>
T GetAll(std::string request_description) {
Expand All @@ -110,19 +106,15 @@ class ScanRequest final {
using reference = value_type&;
using pointer = value_type*;

explicit Iterator(ScanRequest* stream)
: stream_(stream)
{
explicit Iterator(ScanRequest* stream) : stream_(stream) {
if (stream_ && !stream_->HasMore()) {
stream_ = nullptr;
}
}

class ReplyElemHolder {
public:
ReplyElemHolder(value_type reply_elem)
: reply_elem_(std::move(reply_elem))
{}
ReplyElemHolder(value_type reply_elem) : reply_elem_(std::move(reply_elem)) {}

value_type& operator*() { return reply_elem_; }

Expand Down Expand Up @@ -263,6 +255,9 @@ using RequestZremrangebyrank = Request<size_t>;
using RequestZremrangebyscore = Request<size_t>;
using RequestZscan = ScanRequest<ScanTag::kZscan>;
using RequestZscore = Request<std::optional<double>>;
using RequestJsonGet = Request<std::optional<std::string>>;
using RequestJsonSet = Request<StatusOk, void>;
using RequestJsonDel = Request<size_t>;
/// @}

} // namespace storages::redis
Expand Down
6 changes: 6 additions & 0 deletions redis/include/userver/storages/redis/transaction.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,12 @@ class Transaction {

virtual RequestZscore Zscore(std::string key, std::string member) = 0;

virtual RequestJsonGet JsonGet(std::string key) = 0;

virtual RequestJsonSet JsonSet(std::string key, std::string value) = 0;

virtual RequestJsonDel JsonDel(std::string key) = 0;

// end of redis commands
};

Expand Down
63 changes: 45 additions & 18 deletions redis/src/storages/redis/client_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,7 @@ void DoCheckShard(size_t shard, std::optional<size_t> force_shard_idx) {

} // namespace

ClientImpl::ClientImpl(std::shared_ptr<impl::Sentinel> sentinel)
: redis_client_(std::move(sentinel))
{}
ClientImpl::ClientImpl(std::shared_ptr<impl::Sentinel> sentinel) : redis_client_(std::move(sentinel)) {}

void ClientImpl::WaitConnectedOnce(RedisWaitConnected wait_connected) {
redis_client_->WaitConnectedOnce(wait_connected);
Expand Down Expand Up @@ -115,8 +113,8 @@ RequestBitop ClientImpl::Bitop(

RequestDbsize ClientImpl::Dbsize(size_t shard, const CommandControl& command_control) {
CheckShard(shard, command_control);
return CreateRequest<RequestDbsize>(MakeRequest(CmdArgs{"dbsize"}, shard, false, GetCommandControl(command_control))
);
return CreateRequest<
RequestDbsize>(MakeRequest(CmdArgs{"dbsize"}, shard, false, GetCommandControl(command_control)));
}

RequestDecr ClientImpl::Decr(std::string key, const CommandControl& command_control) {
Expand Down Expand Up @@ -151,8 +149,8 @@ RequestUnlink ClientImpl::Unlink(std::vector<std::string> keys, const CommandCon
return CreateDummyRequest<RequestUnlink>(std::make_shared<Reply>("unlink", 0));
}
auto shard = ShardByKey(keys.at(0), command_control);
return CreateRequest<
RequestUnlink>(MakeRequest(CmdArgs{"unlink", std::move(keys)}, shard, true, GetCommandControl(command_control))
return CreateRequest<RequestUnlink>(
MakeRequest(CmdArgs{"unlink", std::move(keys)}, shard, true, GetCommandControl(command_control))
);
}

Expand Down Expand Up @@ -244,8 +242,8 @@ RequestScriptLoad ClientImpl::ScriptLoad(std::string script, size_t shard, const

RequestExists ClientImpl::Exists(std::string key, const CommandControl& command_control) {
auto shard = ShardByKey(key, command_control);
return CreateRequest<
RequestExists>(MakeRequest(CmdArgs{"exists", std::move(key)}, shard, false, GetCommandControl(command_control))
return CreateRequest<RequestExists>(
MakeRequest(CmdArgs{"exists", std::move(key)}, shard, false, GetCommandControl(command_control))
);
}

Expand All @@ -254,8 +252,8 @@ RequestExists ClientImpl::Exists(std::vector<std::string> keys, const CommandCon
return CreateDummyRequest<RequestExists>(std::make_shared<Reply>("exists", 0));
}
auto shard = ShardByKey(keys.at(0), command_control);
return CreateRequest<
RequestExists>(MakeRequest(CmdArgs{"exists", std::move(keys)}, shard, false, GetCommandControl(command_control))
return CreateRequest<RequestExists>(
MakeRequest(CmdArgs{"exists", std::move(keys)}, shard, false, GetCommandControl(command_control))
);
}

Expand Down Expand Up @@ -749,8 +747,8 @@ TransactionPtr ClientImpl::Multi(Transaction::CheckShards check_shards) {

RequestPersist ClientImpl::Persist(std::string key, const CommandControl& command_control) {
auto shard = ShardByKey(key, command_control);
return CreateRequest<
RequestPersist>(MakeRequest(CmdArgs{"persist", std::move(key)}, shard, true, GetCommandControl(command_control))
return CreateRequest<RequestPersist>(
MakeRequest(CmdArgs{"persist", std::move(key)}, shard, true, GetCommandControl(command_control))
);
}

Expand Down Expand Up @@ -883,9 +881,8 @@ RequestSadd ClientImpl::Sadd(std::string key, std::vector<std::string> members,
ScanRequest<ScanTag::kScan> ClientImpl::Scan(size_t shard, ScanOptions options, const CommandControl& command_control) {
CheckShard(shard, command_control);
return ScanRequest<
ScanTag::kScan>(std::make_unique<
RequestScanData<ScanTag::kScan>>(shared_from_this(), shard, std::move(options), command_control)
);
ScanTag::kScan>(std::make_unique<RequestScanData<
ScanTag::kScan>>(shared_from_this(), shard, std::move(options), command_control));
}

template <ScanTag TScanTag>
Expand Down Expand Up @@ -1089,8 +1086,8 @@ ScanRequest<ScanTag::kSscan> ClientImpl::Sscan(

RequestStrlen ClientImpl::Strlen(std::string key, const CommandControl& command_control) {
auto shard = ShardByKey(key, command_control);
return CreateRequest<
RequestStrlen>(MakeRequest(CmdArgs{"strlen", std::move(key)}, shard, false, GetCommandControl(command_control))
return CreateRequest<RequestStrlen>(
MakeRequest(CmdArgs{"strlen", std::move(key)}, shard, false, GetCommandControl(command_control))
);
}

Expand Down Expand Up @@ -1451,6 +1448,36 @@ RequestZscore ClientImpl::Zscore(std::string key, std::string member, const Comm
));
}

RequestJsonGet ClientImpl::JsonGet(std::string key, const CommandControl& command_control) {
auto shard = ShardByKey(key, command_control);
return CreateRequest<RequestJsonGet>(MakeRequest(
CmdArgs{"json.get", std::move(key), /* path = */ "$"},
shard,
false,
GetCommandControl(command_control)
));
}

RequestJsonSet ClientImpl::JsonSet(std::string key, std::string value, const CommandControl& command_control) {
auto shard = ShardByKey(key, command_control);
return CreateRequest<RequestJsonSet>(MakeRequest(
CmdArgs{"json.set", std::move(key), /* path = */ "$", std::move(value)},
shard,
true,
GetCommandControl(command_control)
));
}

RequestJsonDel ClientImpl::JsonDel(std::string key, const CommandControl& command_control) {
auto shard = ShardByKey(key, command_control);
return CreateRequest<RequestJsonDel>(MakeRequest(
CmdArgs{"json.del", std::move(key), /* path = */ "$"},
shard,
true,
GetCommandControl(command_control)
));
}

// end of redis commands

impl::Sentinel& ClientImpl::GetNative() const { return *redis_client_; }
Expand Down
Loading