From 2397d5d659b53cd7d7f4787ba6ae5e24189302f2 Mon Sep 17 00:00:00 2001 From: Arsentiy Milchakov Date: Mon, 8 Apr 2019 16:42:27 +0300 Subject: [PATCH] [booking] reduce count of requests to booking during filtration on map --- map/booking_availability_filter.cpp | 6 ++-- map/booking_filter_cache.hpp | 4 +-- map/framework.cpp | 55 +++++++++++++++++++++++++++++ map/framework.hpp | 2 ++ map/search_api.cpp | 16 +++++++++ map/search_api.hpp | 7 ++++ map/viewport_search_callback.cpp | 9 +++-- map/viewport_search_callback.hpp | 11 +++++- search/utils.cpp | 4 +-- search/utils.hpp | 2 +- 10 files changed, 105 insertions(+), 11 deletions(-) diff --git a/map/booking_availability_filter.cpp b/map/booking_availability_filter.cpp index 0456828b0d..ad0535dd76 100644 --- a/map/booking_availability_filter.cpp +++ b/map/booking_availability_filter.cpp @@ -129,9 +129,9 @@ void FillResults(HotelToResults && hotelToResults, std::vector cons void FillResults(HotelToFeatureIds && hotelToFeatureIds, std::vector const & hotelIds, availability::Cache & cache, std::vector & results) { - auto const inserter = [&results](FeatureID result) + auto const inserter = [&results](FeatureID && result) { - results.push_back(result); + results.emplace_back(std::move(result)); }; FillResults(std::move(hotelToFeatureIds), hotelIds, cache, inserter); @@ -279,7 +279,7 @@ void AvailabilityFilter::ApplyFilterInternal(Source const & source, Parameters c ASSERT_LESS_OR_EQUAL(m_apiParams.m_hotelIds.size(), kMaxCountInRequest, ()); - if (m_apiParams.m_hotelIds.empty() || m_apiParams.m_hotelIds.size() > kMaxCountInRequest) + if (m_apiParams.m_hotelIds.empty()) { Source result; FillResults(std::move(hotelsToSourceValue), {} /* hotelIds */, *m_cache, result); diff --git a/map/booking_filter_cache.hpp b/map/booking_filter_cache.hpp index 049f086838..78064df4fe 100644 --- a/map/booking_filter_cache.hpp +++ b/map/booking_filter_cache.hpp @@ -61,9 +61,9 @@ private: HotelsMap m_notReadyHotels; // Max count of |m_hotelToStatus| container. // Count is unlimited when |m_maxCount| is equal to zero. - size_t const m_maxCount = 1000; + size_t const m_maxCount = 5000; // Do not use aging when |m_expiryPeriodSeconds| is equal to zero. - size_t const m_expiryPeriodSeconds = 60; + size_t const m_expiryPeriodSeconds = 300; }; std::string DebugPrint(Cache::HotelStatus status); diff --git a/map/framework.cpp b/map/framework.cpp index 2664e48280..88a1527b4d 100644 --- a/map/framework.cpp +++ b/map/framework.cpp @@ -313,6 +313,11 @@ void Framework::OnUserPositionChanged(m2::PointD const & position, bool hasPosit void Framework::OnViewportChanged(ScreenBase const & screen) { + auto const isSameViewport = m2::IsEqual(screen.ClipRect(), m_currentModelView.ClipRect(), + kMwmPointAccuracy, kMwmPointAccuracy); + if (isSameViewport) + return; + m_currentModelView = screen; GetSearchAPI().OnViewportChanged(GetCurrentViewport()); @@ -3848,6 +3853,56 @@ void Framework::FilterResultsForHotelsQuery(booking::filter::Tasks const & filte m_bookingFilterProcessor.ApplyFilters(results, std::move(tasksInternal), filterTasks.GetMode()); } +void Framework::FilterHotels(booking::filter::Tasks const & filterTasks, + vector && featureIds) +{ + if (featureIds.size() > booking::RawApi::GetMaxHotelsInAvailabilityRequest()) + return; + + using namespace booking::filter; + + TasksRawInternal tasksInternal; + + for (auto const & task : filterTasks) + { + auto const type = task.m_type; + auto const & apiParams = task.m_filterParams.m_apiParams; + auto const & cb = task.m_filterParams.m_callback; + + if (apiParams->IsEmpty()) + continue; + + ParamsRawInternal paramsInternal + { + apiParams, + [this, type, apiParams, cb](vector const & features) + { + if (features.empty()) + return; + + GetPlatform().RunTask(Platform::Thread::Gui, [this, type, features]() + { + switch (type) + { + case Type::Deals: + m_searchMarks.SetSales(features, true /* hasSale */); + break; + case Type::Availability: + m_searchMarks.SetPreparingState(features, false /* isPreparing */); + break; + } + }); + cb(apiParams, features); + } + }; + + tasksInternal.emplace_back(type, std::move(paramsInternal)); + } + + m_bookingFilterProcessor.ApplyFilters(std::move(featureIds), std::move(tasksInternal), + filterTasks.GetMode()); +} + void Framework::OnBookingFilterParamsUpdate(booking::filter::Tasks const & filterTasks) { for (auto const & task : filterTasks) diff --git a/map/framework.hpp b/map/framework.hpp index 78f3b3809d..28ef5d81e3 100644 --- a/map/framework.hpp +++ b/map/framework.hpp @@ -902,6 +902,8 @@ private: public: void FilterResultsForHotelsQuery(booking::filter::Tasks const & filterTasks, search::Results const & results, bool inViewport) override; + void FilterHotels(booking::filter::Tasks const & filterTasks, + vector && featureIds) override; void OnBookingFilterParamsUpdate(booking::filter::Tasks const & filterTasks) override; booking::AvailabilityParams GetLastBookingAvailabilityParams() const; diff --git a/map/search_api.cpp b/map/search_api.cpp index 170497bb77..11e9d86f3b 100644 --- a/map/search_api.cpp +++ b/map/search_api.cpp @@ -9,6 +9,7 @@ #include "search/geometry_utils.hpp" #include "search/hotels_filter.hpp" #include "search/tracer.hpp" +#include "search/utils.hpp" #include "storage/downloader_search_params.hpp" @@ -210,6 +211,7 @@ bool SearchAPI::SearchInViewport(ViewportSearchParams const & params) }; p.m_onResults = ViewportSearchCallback( + m_viewport, static_cast(*this), params.m_bookingFilterTasks, [this, params](Results const & results) { @@ -336,6 +338,20 @@ void SearchAPI::FilterResultsForHotelsQuery(booking::filter::Tasks const & filte m_delegate.FilterResultsForHotelsQuery(filterTasks, results, inViewport); } +void SearchAPI::FilterAllHotelsInViewport(m2::RectD const & viewport, + booking::filter::Tasks const & filterTasks) +{ + vector featureIds; + auto const types = search::GetCategoryTypes("hotel", "en", GetDefaultCategories()); + search::ForEachOfTypesInRect(m_dataSource, types, m_viewport, + [&featureIds](FeatureID const & id) + { + featureIds.push_back(id); + }); + + m_delegate.FilterHotels(filterTasks, move(featureIds)); +} + void SearchAPI::OnBookmarksCreated(vector> const & marks) { vector data; diff --git a/map/search_api.hpp b/map/search_api.hpp index a2707859ba..c6c4dde85b 100644 --- a/map/search_api.hpp +++ b/map/search_api.hpp @@ -84,6 +84,11 @@ public: { } + virtual void FilterHotels(booking::filter::Tasks const & filterTasks, + vector && featureIds) + { + } + virtual void OnBookingFilterParamsUpdate(booking::filter::Tasks const & filterTasks) {} virtual search::ProductInfo GetProductInfo(search::Result const & result) const { return {}; }; @@ -136,6 +141,8 @@ public: search::ProductInfo GetProductInfo(search::Result const & result) const override; void FilterResultsForHotelsQuery(booking::filter::Tasks const & filterTasks, search::Results const & results, bool inViewport) override; + void FilterAllHotelsInViewport(m2::RectD const & viewport, + booking::filter::Tasks const & filterTasks) override; void OnBookmarksCreated(std::vector> const & marks); void OnBookmarksUpdated(std::vector> const & marks); diff --git a/map/viewport_search_callback.cpp b/map/viewport_search_callback.cpp index 88e598333d..40f89c0866 100644 --- a/map/viewport_search_callback.cpp +++ b/map/viewport_search_callback.cpp @@ -1,6 +1,7 @@ #include "map/viewport_search_callback.hpp" #include "search/result.hpp" +#include "search/utils.hpp" #include "base/assert.hpp" @@ -31,10 +32,11 @@ booking::filter::Types FillBookingFilterTypes(search::Results const & results, namespace search { -ViewportSearchCallback::ViewportSearchCallback(Delegate & delegate, +ViewportSearchCallback::ViewportSearchCallback(m2::RectD const & viewport, Delegate & delegate, booking::filter::Tasks const & bookingFilterTasks, OnResults const & onResults) - : m_delegate(delegate) + : m_viewport(viewport) + , m_delegate(delegate) , m_onResults(onResults) , m_firstCall(true) , m_lastResultsSize(0) @@ -94,6 +96,9 @@ void ViewportSearchCallback::operator()(Results const & results) if (results.IsEndedNormal() && results.GetType() == Results::Type::Hotels && !m_bookingFilterTasks.IsEmpty()) { + if (m_viewport.IsValid()) + m_delegate.FilterAllHotelsInViewport(m_viewport, m_bookingFilterTasks); + m_delegate.FilterResultsForHotelsQuery(m_bookingFilterTasks, results, true /* inViewport */); } diff --git a/map/viewport_search_callback.hpp b/map/viewport_search_callback.hpp index 1603df3193..78e508c01a 100644 --- a/map/viewport_search_callback.hpp +++ b/map/viewport_search_callback.hpp @@ -6,7 +6,11 @@ #include "search/result.hpp" #include "search/search_params.hpp" +#include "geometry/rect2d.hpp" + #include +#include +#include namespace search { @@ -30,16 +34,21 @@ public: bool clear, booking::filter::Types types) = 0; virtual void FilterResultsForHotelsQuery(booking::filter::Tasks const & filterTasks, search::Results const & results, bool inViewport) = 0; + virtual void FilterAllHotelsInViewport(m2::RectD const & viewport, + booking::filter::Tasks const & filterTasks) = 0; }; using OnResults = SearchParams::OnResults; - ViewportSearchCallback(Delegate & delegate, booking::filter::Tasks const & bookingFilterTasks, + ViewportSearchCallback(m2::RectD const & viewport, Delegate & delegate, + booking::filter::Tasks const & bookingFilterTasks, OnResults const & onResults); void operator()(Results const & results); private: + m2::RectD m_viewport; + Delegate & m_delegate; OnResults m_onResults; diff --git a/search/utils.cpp b/search/utils.cpp index bcf8e981e4..5f1403915d 100644 --- a/search/utils.cpp +++ b/search/utils.cpp @@ -126,10 +126,10 @@ void ForEachOfTypesInRect(DataSource const & dataSource, vector const } } -bool IsCategorialRequestFuzzy(string const & query, string const & categoryEn) +bool IsCategorialRequestFuzzy(string const & query, string const & categoryName) { auto const & catHolder = GetDefaultCategories(); - auto const types = GetCategoryTypes(categoryEn, "en", catHolder); + auto const types = GetCategoryTypes(categoryName, "en", catHolder); vector queryTokens; SplitUniString(NormalizeAndSimplifyString(query), base::MakeBackInsertFunctor(queryTokens), diff --git a/search/utils.hpp b/search/utils.hpp index 584c18bffd..91fe0515ea 100644 --- a/search/utils.hpp +++ b/search/utils.hpp @@ -132,5 +132,5 @@ void ForEachOfTypesInRect(DataSource const & dataSource, std::vector c m2::RectD const & rect, FeatureIndexCallback const & fn); // Returns true iff |query| contains |categoryEn| synonym. -bool IsCategorialRequestFuzzy(std::string const & query, std::string const & categoryEn); +bool IsCategorialRequestFuzzy(std::string const & query, std::string const & categoryName); } // namespace search