diff --git a/src/Common/ZooKeeper/ZooKeeper.h b/src/Common/ZooKeeper/ZooKeeper.h index 1f761d0c505..1d28caae8d9 100644 --- a/src/Common/ZooKeeper/ZooKeeper.h +++ b/src/Common/ZooKeeper/ZooKeeper.h @@ -225,7 +225,7 @@ public: template MultiGetResponse get(TIter start, TIter end) { - return multiRead( + return multiRead( start, end, zkutil::makeGetRequest, [&](const auto & path) { return asyncGet(path); }); } @@ -253,7 +253,7 @@ public: template MultiGetResponse tryGet(TIter start, TIter end) { - return tryMultiRead( + return multiRead( start, end, zkutil::makeGetRequest, [&](const auto & path) { return asyncTryGet(path); }); } @@ -290,7 +290,7 @@ public: MultiGetChildrenResponse getChildren(TIter start, TIter end, Coordination::ListRequestType list_request_type = Coordination::ListRequestType::ALL) { - return multiRead( + return multiRead( start, end, [list_request_type](const auto & path) { return zkutil::makeListRequest(path, list_request_type); }, @@ -323,7 +323,7 @@ public: MultiGetChildrenResponse tryGetChildren(TIter start, TIter end, Coordination::ListRequestType list_request_type = Coordination::ListRequestType::ALL) { - return tryMultiRead( + return multiRead( start, end, [list_request_type](const auto & path) { return zkutil::makeListRequest(path, list_request_type); }, @@ -495,7 +495,7 @@ private: template using AsyncFunction = std::function(const std::string &)>; - template + template MultiReadResponses multiRead(TIter start, TIter end, RequestFactory request_factory, AsyncFunction async_fun) { if (getApiVersion() >= DB::KeeperApiVersion::WITH_MULTI_READ) @@ -504,36 +504,17 @@ private: for (auto it = start; it != end; ++it) requests.push_back(request_factory(*it)); - auto responses = multi(requests); - return MultiReadResponses{std::move(responses)}; - } - - auto responses_size = std::distance(start, end); - std::vector> future_responses; - - if (responses_size == 0) - return MultiReadResponses(std::move(future_responses)); - - future_responses.reserve(responses_size); - - for (auto it = start; it != end; ++it) - future_responses.push_back(async_fun(*it)); - - return MultiReadResponses{std::move(future_responses)}; - } - - template - MultiReadResponses tryMultiRead(TIter start, TIter end, RequestFactory request_factory, AsyncFunction async_fun) - { - if (getApiVersion() >= DB::KeeperApiVersion::WITH_MULTI_READ) - { - Coordination::Requests requests; - for (auto it = start; it != end; ++it) - requests.push_back(request_factory(*it)); - - Coordination::Responses responses; - tryMulti(requests, responses); - return MultiReadResponses{std::move(responses)}; + if constexpr (try_multi) + { + Coordination::Responses responses; + tryMulti(requests, responses); + return MultiReadResponses{std::move(responses)}; + } + else + { + auto responses = multi(requests); + return MultiReadResponses{std::move(responses)}; + } } auto responses_size = std::distance(start, end);