diff --git a/redis/include/userver/storages/redis/client.hpp b/redis/include/userver/storages/redis/client.hpp index 6a97a5e0f4e5..d85d588a3a3b 100644 --- a/redis/include/userver/storages/redis/client.hpp +++ b/redis/include/userver/storages/redis/client.hpp @@ -158,7 +158,8 @@ class Client { size_t key_index, const CommandControl& command_control ) { - return RequestGeneric{GenericCommon(std::move(command), std::move(args), key_index, command_control) + return RequestGeneric{ + GenericCommon(std::move(command), std::move(args), key_index, command_control) }; } @@ -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, diff --git a/redis/include/userver/storages/redis/request.hpp b/redis/include/userver/storages/redis/request.hpp index e1ea3afea3bd..3880361618fa 100644 --- a/redis/include/userver/storages/redis/request.hpp +++ b/redis/include/userver/storages/redis/request.hpp @@ -34,9 +34,7 @@ class [[nodiscard]] Request final { using Result = ResultType; using Reply = ReplyType; - explicit Request(std::unique_ptr>&& impl) - : impl_(std::move(impl)) - {} + explicit Request(std::unique_ptr>&& 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. /// @@ -83,9 +81,7 @@ class ScanRequest final { public: using ReplyElem = typename ScanReplyElem::type; - explicit ScanRequest(std::unique_ptr>&& impl) - : impl_(std::move(impl)) - {} + explicit ScanRequest(std::unique_ptr>&& impl) : impl_(std::move(impl)) {} template > T GetAll(std::string request_description) { @@ -110,9 +106,7 @@ 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; } @@ -120,9 +114,7 @@ class ScanRequest final { 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_; } @@ -263,6 +255,9 @@ using RequestZremrangebyrank = Request; using RequestZremrangebyscore = Request; using RequestZscan = ScanRequest; using RequestZscore = Request>; +using RequestJsonGet = Request>; +using RequestJsonSet = Request; +using RequestJsonDel = Request; /// @} } // namespace storages::redis diff --git a/redis/include/userver/storages/redis/transaction.hpp b/redis/include/userver/storages/redis/transaction.hpp index 5951c9687fb7..0facb35f5765 100644 --- a/redis/include/userver/storages/redis/transaction.hpp +++ b/redis/include/userver/storages/redis/transaction.hpp @@ -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 }; diff --git a/redis/src/storages/redis/client_impl.cpp b/redis/src/storages/redis/client_impl.cpp index 5f901659d92d..ed20b30a30b8 100644 --- a/redis/src/storages/redis/client_impl.cpp +++ b/redis/src/storages/redis/client_impl.cpp @@ -40,9 +40,7 @@ void DoCheckShard(size_t shard, std::optional force_shard_idx) { } // namespace -ClientImpl::ClientImpl(std::shared_ptr sentinel) - : redis_client_(std::move(sentinel)) -{} +ClientImpl::ClientImpl(std::shared_ptr sentinel) : redis_client_(std::move(sentinel)) {} void ClientImpl::WaitConnectedOnce(RedisWaitConnected wait_connected) { redis_client_->WaitConnectedOnce(wait_connected); @@ -115,8 +113,8 @@ RequestBitop ClientImpl::Bitop( RequestDbsize ClientImpl::Dbsize(size_t shard, const CommandControl& command_control) { CheckShard(shard, command_control); - return CreateRequest(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) { @@ -151,8 +149,8 @@ RequestUnlink ClientImpl::Unlink(std::vector keys, const CommandCon return CreateDummyRequest(std::make_shared("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( + MakeRequest(CmdArgs{"unlink", std::move(keys)}, shard, true, GetCommandControl(command_control)) ); } @@ -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( + MakeRequest(CmdArgs{"exists", std::move(key)}, shard, false, GetCommandControl(command_control)) ); } @@ -254,8 +252,8 @@ RequestExists ClientImpl::Exists(std::vector keys, const CommandCon return CreateDummyRequest(std::make_shared("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( + MakeRequest(CmdArgs{"exists", std::move(keys)}, shard, false, GetCommandControl(command_control)) ); } @@ -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( + MakeRequest(CmdArgs{"persist", std::move(key)}, shard, true, GetCommandControl(command_control)) ); } @@ -883,9 +881,8 @@ RequestSadd ClientImpl::Sadd(std::string key, std::vector members, ScanRequest ClientImpl::Scan(size_t shard, ScanOptions options, const CommandControl& command_control) { CheckShard(shard, command_control); return ScanRequest< - ScanTag::kScan>(std::make_unique< - RequestScanData>(shared_from_this(), shard, std::move(options), command_control) - ); + ScanTag::kScan>(std::make_unique>(shared_from_this(), shard, std::move(options), command_control)); } template @@ -1089,8 +1086,8 @@ ScanRequest 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( + MakeRequest(CmdArgs{"strlen", std::move(key)}, shard, false, GetCommandControl(command_control)) ); } @@ -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(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(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(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_; } diff --git a/redis/src/storages/redis/client_impl.hpp b/redis/src/storages/redis/client_impl.hpp index 21afdd6cd5db..bfca58a03aa8 100644 --- a/redis/src/storages/redis/client_impl.hpp +++ b/redis/src/storages/redis/client_impl.hpp @@ -124,11 +124,17 @@ class ClientImpl final : public Client, public std::enable_shared_from_this point_members, const CommandControl& command_control) - override; + RequestGeoadd Geoadd( + std::string key, + std::vector point_members, + const CommandControl& command_control + ) override; - RequestGeopos Geopos(std::string key, std::vector members, const CommandControl& command_control) - override; + RequestGeopos Geopos( + std::string key, + std::vector members, + const CommandControl& command_control + ) override; RequestGeoradius Georadius( std::string key, @@ -189,8 +195,12 @@ class ClientImpl final : public Client, public std::enable_shared_from_this fields, const CommandControl& command_control) - override; + RequestHmget Hmget( + std::string key, + std::vector fields, + const CommandControl& command_control + ) override; RequestHmset Hmset( std::string key, @@ -212,14 +225,25 @@ class ClientImpl final : public Client, public std::enable_shared_from_this Hscan(std::string key, HscanOptions options, const CommandControl& command_control) - override; + ScanRequest Hscan( + std::string key, + HscanOptions options, + const CommandControl& command_control + ) override; - RequestHset Hset(std::string key, std::string field, std::string value, const CommandControl& command_control) - override; + RequestHset Hset( + std::string key, + std::string field, + std::string value, + const CommandControl& command_control + ) override; - RequestHsetnx Hsetnx(std::string key, std::string field, std::string value, const CommandControl& command_control) - override; + RequestHsetnx Hsetnx( + std::string key, + std::string field, + std::string value, + const CommandControl& command_control + ) override; RequestHvals Hvals(std::string key, const CommandControl& command_control) override; @@ -235,22 +259,31 @@ class ClientImpl final : public Client, public std::enable_shared_from_this values, const CommandControl& command_control) - override; + RequestLpush Lpush( + std::string key, + std::vector values, + const CommandControl& command_control + ) override; RequestLpushx Lpushx(std::string key, std::string element, const CommandControl& command_control) override; RequestLrange Lrange(std::string key, int64_t start, int64_t stop, const CommandControl& command_control) override; - RequestLrem Lrem(std::string key, int64_t count, std::string element, const CommandControl& command_control) - override; + RequestLrem Lrem( + std::string key, + int64_t count, + std::string element, + const CommandControl& command_control + ) override; RequestLtrim Ltrim(std::string key, int64_t start, int64_t stop, const CommandControl& command_control) override; RequestMget Mget(std::vector keys, const CommandControl& command_control) override; - RequestMset Mset(std::vector> key_values, const CommandControl& command_control) - override; + RequestMset Mset( + std::vector> key_values, + const CommandControl& command_control + ) override; TransactionPtr Multi() override; @@ -258,15 +291,22 @@ class ClientImpl final : public Client, public std::enable_shared_from_this values, const CommandControl& command_control) - override; + RequestRpush Rpush( + std::string key, + std::vector values, + const CommandControl& command_control + ) override; RequestRpushx Rpushx(std::string key, std::string element, const CommandControl& command_control) override; @@ -310,8 +353,11 @@ class ClientImpl final : public Client, public std::enable_shared_from_this members, const CommandControl& command_control) override; - ScanRequest Sscan(std::string key, SscanOptions options, const CommandControl& command_control) - override; + ScanRequest Sscan( + std::string key, + SscanOptions options, + const CommandControl& command_control + ) override; RequestStrlen Strlen(std::string key, const CommandControl& command_control) override; @@ -386,8 +435,12 @@ class ClientImpl final : public Client, public std::enable_shared_from_this Zscan(std::string key, ZscanOptions options, const CommandControl& command_control) - override; + ScanRequest Zscan( + std::string key, + ZscanOptions options, + const CommandControl& command_control + ) override; RequestZscore Zscore(std::string key, std::string member, const CommandControl& command_control) override; + RequestJsonGet JsonGet(std::string key, const CommandControl& command_control) override; + + RequestJsonSet JsonSet(std::string key, std::string value, const CommandControl& command_control) override; + + RequestJsonDel JsonDel(std::string key, const CommandControl& command_control) override; + // end of redis commands friend class TransactionImpl; diff --git a/redis/src/storages/redis/client_redistest.cpp b/redis/src/storages/redis/client_redistest.cpp index 48ee1dd6776c..7d848c5c6323 100644 --- a/redis/src/storages/redis/client_redistest.cpp +++ b/redis/src/storages/redis/client_redistest.cpp @@ -1022,6 +1022,20 @@ UTEST_F(RedisClientTest, Zscore) { EXPECT_EQ(client->Zscore("zset", "two", {}).Get(), 2.); } +UTEST_F(RedisClientTest, JsonSetGetDel) { + auto client = GetClient(); + + auto expected_json_string = R"({"some-key": "some-value"})"; + + EXPECT_EQ(client->JsonGet("json.set", {}).Get(), std::nullopt); + + client->JsonSet("json.set", expected_json_string, {}).Get(); + + EXPECT_EQ(client->JsonGet("json.set", {}).Get(), expected_json_string); + EXPECT_EQ(client->JsonDel("json.set", {}).Get(), 1); + EXPECT_EQ(client->JsonGet("json.set", {}).Get(), std::nullopt); +} + UTEST_F(RedisClientTest, TransactionType) { auto client = GetClient(); /// [redis transaction sample] diff --git a/redis/src/storages/redis/exception.cpp b/redis/src/storages/redis/exception.cpp index 45f142bdae1b..020c68410d8e 100644 --- a/redis/src/storages/redis/exception.cpp +++ b/redis/src/storages/redis/exception.cpp @@ -9,10 +9,10 @@ USERVER_NAMESPACE_BEGIN namespace storages::redis { RequestFailedException::RequestFailedException(const std::string& request_description, ReplyStatus status) - : Exception(fmt::format("{} request failed with status '{}'", request_description, *kReplyStatusMap.TryFind(status)) + : Exception( + fmt::format("{} request failed with status '{}'", request_description, *kReplyStatusMap.TryFind(status)) ), - status_(status) -{} + status_(status) {} ReplyStatus RequestFailedException::GetStatus() const { return status_; } diff --git a/redis/src/storages/redis/transaction_impl.cpp b/redis/src/storages/redis/transaction_impl.cpp index 81c6f2678589..2b1042e71a86 100644 --- a/redis/src/storages/redis/transaction_impl.cpp +++ b/redis/src/storages/redis/transaction_impl.cpp @@ -19,10 +19,7 @@ RequestExec CreateExecRequest(impl::Request&& request, std::vector client, CheckShards check_shards) - : client_(std::move(client)), - check_shards_(check_shards), - cmd_args_({"MULTI"}) -{} + : client_(std::move(client)), check_shards_(check_shards), cmd_args_({"MULTI"}) {} RequestExec TransactionImpl::Exec(const CommandControl& command_control) { if (!shard_) { @@ -706,6 +703,21 @@ RequestZscore TransactionImpl::Zscore(std::string key, std::string member) { return AddCmd("zscore", false, std::move(key), std::move(member)); } +RequestJsonGet TransactionImpl::JsonGet(std::string key) { + UpdateShard(key); + return AddCmd("json.get", /* master = */ false, std::move(key), /* path = */ "$"); +} + +RequestJsonSet TransactionImpl::JsonSet(std::string key, std::string value) { + UpdateShard(key); + return AddCmd("json.set", /* master = */ true, std::move(key), /* path = */ "$", std::move(value)); +} + +RequestJsonDel TransactionImpl::JsonDel(std::string key) { + UpdateShard(key); + return AddCmd("json.del", /* master = */ true, std::move(key), /* path = */ "$"); +} + // end of redis commands void TransactionImpl::UpdateShard(const std::string& key) { diff --git a/redis/src/storages/redis/transaction_impl.hpp b/redis/src/storages/redis/transaction_impl.hpp index 9018b005e3af..74233341128f 100644 --- a/redis/src/storages/redis/transaction_impl.hpp +++ b/redis/src/storages/redis/transaction_impl.hpp @@ -28,8 +28,7 @@ class TransactionImpl final : public Transaction { public: template ResultPromise(engine::Promise&& promise, To>) - : impl_(std::make_unique>(std::move(promise))) - {} + : impl_(std::make_unique>(std::move(promise))) {} ResultPromise(ResultPromise&& other) = default; void ProcessReply(ReplyData&& reply_data, const std::string& request_description) { @@ -47,9 +46,7 @@ class TransactionImpl final : public Transaction { template class ResultPromiseImpl : public ResultPromiseImplBase { public: - ResultPromiseImpl(engine::Promise&& promise) - : promise_(std::move(promise)) - {} + ResultPromiseImpl(engine::Promise&& promise) : promise_(std::move(promise)) {} void ProcessReply(ReplyData&& reply_data, const std::string& request_description) override { try { @@ -239,8 +236,11 @@ class TransactionImpl final : public Transaction { RequestSetIfNotExistOrGet SetIfNotExistOrGet(std::string key, std::string value) override; - RequestSetIfNotExistOrGet SetIfNotExistOrGet(std::string key, std::string value, std::chrono::milliseconds ttl) - override; + RequestSetIfNotExistOrGet SetIfNotExistOrGet( + std::string key, + std::string value, + std::chrono::milliseconds ttl + ) override; RequestSetex Setex(std::string key, std::chrono::seconds seconds, std::string value) override; @@ -292,8 +292,12 @@ class TransactionImpl final : public Transaction { RequestZrangebyscore Zrangebyscore(std::string key, std::string min, std::string max) override; - RequestZrangebyscore Zrangebyscore(std::string key, double min, double max, const RangeOptions& range_options) - override; + RequestZrangebyscore Zrangebyscore( + std::string key, + double min, + double max, + const RangeOptions& range_options + ) override; RequestZrangebyscore Zrangebyscore( std::string key, @@ -332,6 +336,12 @@ class TransactionImpl final : public Transaction { RequestZscore Zscore(std::string key, std::string member) override; + RequestJsonGet JsonGet(std::string key) override; + + RequestJsonSet JsonSet(std::string key, std::string value) override; + + RequestJsonDel JsonDel(std::string key) override; + // end of redis commands private: diff --git a/redis/testing/include/userver/storages/redis/mock_client_base.hpp b/redis/testing/include/userver/storages/redis/mock_client_base.hpp index e810e5b2dca0..c9b26c74dbfa 100644 --- a/redis/testing/include/userver/storages/redis/mock_client_base.hpp +++ b/redis/testing/include/userver/storages/redis/mock_client_base.hpp @@ -123,11 +123,17 @@ class MockClientBase : public Client, public std::enable_shared_from_this point_members, const CommandControl& command_control) - override; + RequestGeoadd Geoadd( + std::string key, + std::vector point_members, + const CommandControl& command_control + ) override; - RequestGeopos Geopos(std::string key, std::vector members, const CommandControl& command_control) - override; + RequestGeopos Geopos( + std::string key, + std::vector members, + const CommandControl& command_control + ) override; RequestGeoradius Georadius( std::string key, @@ -188,8 +194,12 @@ class MockClientBase : public Client, public std::enable_shared_from_this fields, const CommandControl& command_control) - override; + RequestHmget Hmget( + std::string key, + std::vector fields, + const CommandControl& command_control + ) override; RequestHmset Hmset( std::string key, @@ -211,14 +224,25 @@ class MockClientBase : public Client, public std::enable_shared_from_this Hscan(std::string key, HscanOptions options, const CommandControl& command_control) - override; + ScanRequest Hscan( + std::string key, + HscanOptions options, + const CommandControl& command_control + ) override; - RequestHset Hset(std::string key, std::string field, std::string value, const CommandControl& command_control) - override; + RequestHset Hset( + std::string key, + std::string field, + std::string value, + const CommandControl& command_control + ) override; - RequestHsetnx Hsetnx(std::string key, std::string field, std::string value, const CommandControl& command_control) - override; + RequestHsetnx Hsetnx( + std::string key, + std::string field, + std::string value, + const CommandControl& command_control + ) override; RequestHvals Hvals(std::string key, const CommandControl& command_control) override; @@ -234,34 +258,50 @@ class MockClientBase : public Client, public std::enable_shared_from_this values, const CommandControl& command_control) - override; + RequestLpush Lpush( + std::string key, + std::vector values, + const CommandControl& command_control + ) override; RequestLpushx Lpushx(std::string key, std::string element, const CommandControl& command_control) override; RequestLrange Lrange(std::string key, int64_t start, int64_t stop, const CommandControl& command_control) override; - RequestLrem Lrem(std::string key, int64_t count, std::string element, const CommandControl& command_control) - override; + RequestLrem Lrem( + std::string key, + int64_t count, + std::string element, + const CommandControl& command_control + ) override; RequestLtrim Ltrim(std::string key, int64_t start, int64_t stop, const CommandControl& command_control) override; RequestMget Mget(std::vector keys, const CommandControl& command_control) override; - RequestMset Mset(std::vector> key_values, const CommandControl& command_control) - override; + RequestMset Mset( + std::vector> key_values, + const CommandControl& command_control + ) override; RequestPersist Persist(std::string key, const CommandControl& command_control) override; - RequestPexpire Pexpire(std::string key, std::chrono::milliseconds ttl, const CommandControl& command_control) - override; + RequestPexpire Pexpire( + std::string key, + std::chrono::milliseconds ttl, + const CommandControl& command_control + ) override; RequestPing Ping(size_t shard, const CommandControl& command_control) override; RequestPingMessage Ping(size_t shard, std::string message, const CommandControl& command_control) override; - void Publish(std::string channel, std::string message, const CommandControl& command_control, PubShard policy) - override; + void Publish( + std::string channel, + std::string message, + const CommandControl& command_control, + PubShard policy + ) override; void Spublish(std::string channel, std::string message, const CommandControl& command_control) override; @@ -271,8 +311,11 @@ class MockClientBase : public Client, public std::enable_shared_from_this values, const CommandControl& command_control) - override; + RequestRpush Rpush( + std::string key, + std::vector values, + const CommandControl& command_control + ) override; RequestRpushx Rpushx(std::string key, std::string element, const CommandControl& command_control) override; @@ -302,8 +345,11 @@ class MockClientBase : public Client, public std::enable_shared_from_this members, const CommandControl& command_control) override; - ScanRequest Sscan(std::string key, SscanOptions options, const CommandControl& command_control) - override; + ScanRequest Sscan( + std::string key, + SscanOptions options, + const CommandControl& command_control + ) override; RequestStrlen Strlen(std::string key, const CommandControl& command_control) override; @@ -378,8 +427,12 @@ class MockClientBase : public Client, public std::enable_shared_from_this Zscan(std::string key, ZscanOptions options, const CommandControl& command_control) - override; + ScanRequest Zscan( + std::string key, + ZscanOptions options, + const CommandControl& command_control + ) override; RequestZscore Zscore(std::string key, std::string member, const CommandControl& command_control) override; + RequestJsonGet JsonGet(std::string key, const CommandControl& command_control) override; + + RequestJsonSet JsonSet(std::string key, std::string value, const CommandControl& command_control) override; + + RequestJsonDel JsonDel(std::string key, const CommandControl& command_control) override; + // end of redis commands TransactionPtr Multi() final; diff --git a/redis/testing/include/userver/storages/redis/mock_client_google.hpp b/redis/testing/include/userver/storages/redis/mock_client_google.hpp index 6c92100e113e..0efdd45c0708 100644 --- a/redis/testing/include/userver/storages/redis/mock_client_google.hpp +++ b/redis/testing/include/userver/storages/redis/mock_client_google.hpp @@ -30,8 +30,10 @@ class GMockClient : public MockClientBase { MOCK_METHOD( RequestBitop, Bitop, - (BitOperation op, std::string dest_key, std::vector src_keys, const CommandControl& command_control - ), + (BitOperation op, + std::string dest_key, + std::vector src_keys, + const CommandControl& command_control), (override) ); @@ -541,6 +543,17 @@ class GMockClient : public MockClientBase { (override) ); + MOCK_METHOD(RequestJsonGet, JsonGet, (std::string key, const CommandControl& command_control), (override)); + + MOCK_METHOD( + RequestJsonSet, + JsonSet, + (std::string key, std::string value, const CommandControl& command_control), + (override) + ); + + MOCK_METHOD(RequestJsonDel, JsonDel, (std::string key, const CommandControl& command_control), (override)); + MOCK_METHOD( RequestEvalCommon, EvalCommon, diff --git a/redis/testing/include/userver/storages/redis/mock_publish_waiter.hpp b/redis/testing/include/userver/storages/redis/mock_publish_waiter.hpp index 1038bbc1d656..52ef3b22e926 100644 --- a/redis/testing/include/userver/storages/redis/mock_publish_waiter.hpp +++ b/redis/testing/include/userver/storages/redis/mock_publish_waiter.hpp @@ -38,8 +38,7 @@ class MockPublishWaiter { T&& matcher, size_t times_called = 1 ) - : debug_channel_name_(std::move(debug_channel_name)) - { + : debug_channel_name_(std::move(debug_channel_name)) { UINVARIANT(times_called > 0, "times_called must be > 0"); using ::testing::_; @@ -57,9 +56,8 @@ class MockPublishWaiter { } void Wait() { - EXPECT_TRUE(on_received_.WaitForEventFor(std::chrono::seconds{30}) - ) << "Failed to detect publishing to channel " - << debug_channel_name_ << " within 30 seconds"; + EXPECT_TRUE(on_received_.WaitForEventFor(std::chrono::seconds{30})) + << "Failed to detect publishing to channel " << debug_channel_name_ << " within 30 seconds"; } const std::string& GetPublishedMessage(size_t index) const { return published_messages_.at(index); } diff --git a/redis/testing/include/userver/storages/redis/mock_transaction.hpp b/redis/testing/include/userver/storages/redis/mock_transaction.hpp index 2357a2bb048f..fdb0a4f45610 100644 --- a/redis/testing/include/userver/storages/redis/mock_transaction.hpp +++ b/redis/testing/include/userver/storages/redis/mock_transaction.hpp @@ -189,8 +189,11 @@ class MockTransaction final : public Transaction { RequestSetIfNotExistOrGet SetIfNotExistOrGet(std::string key, std::string value) override; - RequestSetIfNotExistOrGet SetIfNotExistOrGet(std::string key, std::string value, std::chrono::milliseconds ttl) - override; + RequestSetIfNotExistOrGet SetIfNotExistOrGet( + std::string key, + std::string value, + std::chrono::milliseconds ttl + ) override; RequestSetex Setex(std::string key, std::chrono::seconds seconds, std::string value) override; @@ -242,8 +245,12 @@ class MockTransaction final : public Transaction { RequestZrangebyscore Zrangebyscore(std::string key, std::string min, std::string max) override; - RequestZrangebyscore Zrangebyscore(std::string key, double min, double max, const RangeOptions& range_options) - override; + RequestZrangebyscore Zrangebyscore( + std::string key, + double min, + double max, + const RangeOptions& range_options + ) override; RequestZrangebyscore Zrangebyscore( std::string key, @@ -282,6 +289,12 @@ class MockTransaction final : public Transaction { RequestZscore Zscore(std::string key, std::string member) override; + RequestJsonGet JsonGet(std::string key) override; + + RequestJsonSet JsonSet(std::string key, std::string value) override; + + RequestJsonDel JsonDel(std::string key) override; + // end of redis commands private: diff --git a/redis/testing/include/userver/storages/redis/mock_transaction_impl_base.hpp b/redis/testing/include/userver/storages/redis/mock_transaction_impl_base.hpp index 4a1eb221abeb..43278b93ac1c 100644 --- a/redis/testing/include/userver/storages/redis/mock_transaction_impl_base.hpp +++ b/redis/testing/include/userver/storages/redis/mock_transaction_impl_base.hpp @@ -279,6 +279,12 @@ class MockTransactionImplBase { virtual RequestZscore Zscore(std::string key, std::string member); + virtual RequestJsonGet JsonGet(std::string key); + + virtual RequestJsonSet JsonSet(std::string key, std::string value); + + virtual RequestJsonDel JsonDel(std::string key); + // end of redis commands }; diff --git a/redis/testing/src/storages/redis/mock_client_base.cpp b/redis/testing/src/storages/redis/mock_client_base.cpp index 61e46cedeaf8..8997c5d27d97 100644 --- a/redis/testing/src/storages/redis/mock_client_base.cpp +++ b/redis/testing/src/storages/redis/mock_client_base.cpp @@ -15,16 +15,13 @@ constexpr std::string_view kNotMocked{"Redis method is not mocked"}; } MockClientBase::MockClientBase() - : mock_transaction_impl_creator_(std::make_unique>()) -{} + : mock_transaction_impl_creator_(std::make_unique>()) {} MockClientBase::MockClientBase( std::shared_ptr mock_transaction_impl_creator, std::optional force_shard_idx ) - : mock_transaction_impl_creator_(std::move(mock_transaction_impl_creator)), - force_shard_idx_(force_shard_idx) -{} + : mock_transaction_impl_creator_(std::move(mock_transaction_impl_creator)), force_shard_idx_(force_shard_idx) {} MockClientBase::~MockClientBase() = default; @@ -908,6 +905,22 @@ RequestZscore MockClientBase::Zscore( AbortWithStacktrace(kNotMocked); } +RequestJsonGet MockClientBase::JsonGet(std::string /*key*/, const CommandControl& /*command_control*/) { + AbortWithStacktrace(kNotMocked); +} + +RequestJsonSet MockClientBase::JsonSet( + std::string /*key*/, + std::string /*value*/, + const CommandControl& /*command_control*/ +) { + AbortWithStacktrace(kNotMocked); +} + +RequestJsonDel MockClientBase::JsonDel(std::string /*key*/, const CommandControl& /*command_control*/) { + AbortWithStacktrace(kNotMocked); +} + // end of redis commands TransactionPtr MockClientBase::Multi() { diff --git a/redis/testing/src/storages/redis/mock_transaction.cpp b/redis/testing/src/storages/redis/mock_transaction.cpp index e5f97078ea52..a1ce532054a6 100644 --- a/redis/testing/src/storages/redis/mock_transaction.cpp +++ b/redis/testing/src/storages/redis/mock_transaction.cpp @@ -15,8 +15,7 @@ class MockTransaction::ResultPromise { public: template ResultPromise(engine::Promise&& promise, Request&& subrequest) - : impl_(std::make_unique>(std::move(promise), std::move(subrequest))) - {} + : impl_(std::make_unique>(std::move(promise), std::move(subrequest))) {} ResultPromise(ResultPromise&& other) = default; @@ -34,9 +33,7 @@ class MockTransaction::ResultPromise { class ResultPromiseImpl : public ResultPromiseImplBase { public: ResultPromiseImpl(engine::Promise&& promise, Request&& subrequest) - : promise_(std::move(promise)), - subrequest_(std::move(subrequest)) - {} + : promise_(std::move(promise)), subrequest_(std::move(subrequest)) {} void ProcessReply(const std::string& request_description) override { try { @@ -64,8 +61,7 @@ class MockTransaction::ResultPromise { class MockTransaction::MockRequestExecDataImpl final : public RequestDataBase { public: MockRequestExecDataImpl(std::vector>&& result_promises) - : result_promises_(std::move(result_promises)) - {} + : result_promises_(std::move(result_promises)) {} void Wait() override {} @@ -94,10 +90,7 @@ MockTransaction::MockTransaction( std::unique_ptr impl, CheckShards check_shards ) - : client_(std::move(client)), - check_shards_(check_shards), - impl_(std::move(impl)) -{} + : client_(std::move(client)), check_shards_(check_shards), impl_(std::move(impl)) {} MockTransaction::~MockTransaction() = default; @@ -697,6 +690,21 @@ RequestZscore MockTransaction::Zscore(std::string key, std::string member) { return AddSubrequest(impl_->Zscore(std::move(key), std::move(member))); } +RequestJsonGet MockTransaction::JsonGet(std::string key) { + UpdateShard(key); + return AddSubrequest(impl_->JsonGet(std::move(key))); +} + +RequestJsonSet MockTransaction::JsonSet(std::string key, std::string value) { + UpdateShard(key); + return AddSubrequest(impl_->JsonSet(std::move(key), std::move(value))); +} + +RequestJsonDel MockTransaction::JsonDel(std::string key) { + UpdateShard(key); + return AddSubrequest(impl_->JsonDel(std::move(key))); +} + // end of redis commands void MockTransaction::UpdateShard(const std::string& key) { diff --git a/redis/testing/src/storages/redis/mock_transaction_impl_base.cpp b/redis/testing/src/storages/redis/mock_transaction_impl_base.cpp index 268037da990d..5ed0f9161ed6 100644 --- a/redis/testing/src/storages/redis/mock_transaction_impl_base.cpp +++ b/redis/testing/src/storages/redis/mock_transaction_impl_base.cpp @@ -503,6 +503,17 @@ RequestZscore MockTransactionImplBase::Zscore(std::string /*key*/, std::string / AbortWithStacktrace("Redis method not mocked"); } +RequestJsonGet MockTransactionImplBase::JsonGet(std::string /*key*/) { AbortWithStacktrace("Redis method not mocked"); } + +RequestJsonSet MockTransactionImplBase::JsonSet( + std::string /*key*/, + std::string /*value*/ +) { + AbortWithStacktrace("Redis method not mocked"); +} + +RequestJsonDel MockTransactionImplBase::JsonDel(std::string /*key*/) { AbortWithStacktrace("Redis method not mocked"); } + // end of redis commands } // namespace storages::redis