From a854db6fdb08ddb6cbbd195898a89628b5c3a391 Mon Sep 17 00:00:00 2001 From: vng Date: Fri, 11 Sep 2015 15:25:03 +0300 Subject: [PATCH] =?UTF-8?q?[search]=20Result=E2=80=99s=20metadata=20advanc?= =?UTF-8?q?ed=20getting.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- map/framework.cpp | 19 +++++++++++++ map/framework.hpp | 2 ++ search/intermediate_result.cpp | 49 ++++++++++++++++++---------------- search/intermediate_result.hpp | 5 ++-- search/result.cpp | 8 +++--- search/result.hpp | 15 ++++++----- 6 files changed, 64 insertions(+), 34 deletions(-) diff --git a/map/framework.cpp b/map/framework.cpp index 22f1b236f7..732d3bdb82 100644 --- a/map/framework.cpp +++ b/map/framework.cpp @@ -27,6 +27,7 @@ #include "search/search_engine.hpp" #include "search/search_query_factory.hpp" #include "search/result.hpp" +#include "search/intermediate_result.hpp" #include "indexer/categories_holder.hpp" #include "indexer/classificator_loader.hpp" @@ -1316,6 +1317,24 @@ bool Framework::GetCurrentPosition(double & lat, double & lon) const return false; } +void Framework::LoadSearchResultMetadata(search::Result & res) const +{ + if (res.m_metadata.m_isInitialized) + return; + + FeatureID const id = res.GetFeatureID(); + if (id.IsValid()) + { + Index::FeaturesLoaderGuard loader(m_model.GetIndex(), id.m_mwmId); + + FeatureType ft; + loader.GetFeatureByIndex(id.m_index, ft); + + search::ProcessMetadata(ft, res.m_metadata); + } + res.m_metadata.m_isInitialized = true; +} + void Framework::ShowSearchResult(search::Result const & res) { UserMarkContainer::Type const type = UserMarkContainer::SEARCH_MARK; diff --git a/map/framework.hpp b/map/framework.hpp index 1b70acb867..4f6c3c61ec 100644 --- a/map/framework.hpp +++ b/map/framework.hpp @@ -341,7 +341,9 @@ public: bool Search(search::SearchParams const & params); bool GetCurrentPosition(double & lat, double & lon) const; + void LoadSearchResultMetadata(search::Result & res) const; void ShowSearchResult(search::Result const & res); + size_t ShowAllSearchResults(); void StartInteractiveSearch(search::SearchParams const & params) { m_lastSearch = params; } diff --git a/search/intermediate_result.cpp b/search/intermediate_result.cpp index 1d9a66a546..c580f94aab 100644 --- a/search/intermediate_result.cpp +++ b/search/intermediate_result.cpp @@ -1,8 +1,3 @@ -#ifndef OMIM_OS_LINUX -// Lib opening_hours is not built for Linux since stdlib doesn't have required functions. -#include "3party/opening_hours/osm_time_range.hpp" -#endif - #include "intermediate_result.hpp" #include "geometry_utils.hpp" @@ -21,6 +16,11 @@ #include "base/string_utils.hpp" #include "base/logging.hpp" +#ifndef OMIM_OS_LINUX +// Lib opening_hours is not built for Linux since stdlib doesn't have required functions. +#include "3party/opening_hours/osm_time_range.hpp" +#endif + namespace search { @@ -29,6 +29,26 @@ namespace search double const DIST_EQUAL_RESULTS = 100.0; double const DIST_SAME_STREET = 5000.0; + +void ProcessMetadata(FeatureType const & ft, Result::Metadata & meta) +{ + ft.ParseMetadata(); + feature::Metadata const & src = ft.GetMetadata(); + + meta.m_cuisine = src.Get(feature::Metadata::FMD_CUISINE); + +#ifndef OMIM_OS_LINUX + // Lib opening_hours is not built for Linux since stdlib doesn't have required functions. + string const openHours = src.Get(feature::Metadata::FMD_OPEN_HOURS); + if (!openHours.empty()) + meta.m_isClosed = OSMTimeRange(openHours)(time(nullptr)).IsClosed(); +#endif + + meta.m_stars = 0; + (void) strings::to_int(src.Get(feature::Metadata::FMD_STARS), meta.m_stars); + meta.m_stars = my::clamp(meta.m_stars, 0, 5); +} + namespace impl { @@ -130,8 +150,7 @@ PreResult2::PreResult2(FeatureType const & f, PreResult1 const * p, m2::PointD c m_region.SetParams(fileName, fCenter); CalcParams(pivot); - f.ParseMetadata(); - ProcessMetadata(f.GetMetadata()); + ProcessMetadata(f, m_metadata); } PreResult2::PreResult2(double lat, double lon) @@ -149,22 +168,6 @@ PreResult2::PreResult2(m2::PointD const & pt, string const & str, uint32_t type) m_types.Assign(type); } -void PreResult2::ProcessMetadata(feature::Metadata const & meta) -{ - m_metadata.m_cuisine = meta.Get(feature::Metadata::FMD_CUISINE); - -#ifndef OMIM_OS_LINUX - // Lib opening_hours is not built for Linux since stdlib doesn't have required functions. - string const openHours = meta.Get(feature::Metadata::FMD_OPEN_HOURS); - if (!openHours.empty()) - m_metadata.m_isClosed = OSMTimeRange(openHours)(time(nullptr)).IsClosed(); -#endif - - m_metadata.m_stars = 0; - strings::to_int(meta.Get(feature::Metadata::FMD_STARS), m_metadata.m_stars); - m_metadata.m_stars = my::clamp(m_metadata.m_stars, 0, 5); -} - namespace { class SkipRegionInfo diff --git a/search/intermediate_result.hpp b/search/intermediate_result.hpp index f395141e2a..d9fb641f51 100644 --- a/search/intermediate_result.hpp +++ b/search/intermediate_result.hpp @@ -165,8 +165,6 @@ private: feature::EGeomType m_geomType; Result::Metadata m_metadata; - - void ProcessMetadata(feature::Metadata const & meta); }; inline string DebugPrint(PreResult2 const & t) @@ -175,4 +173,7 @@ inline string DebugPrint(PreResult2 const & t) } } // namespace search::impl + +void ProcessMetadata(FeatureType const & ft, Result::Metadata & meta); + } // namespace search diff --git a/search/result.cpp b/search/result.cpp index ae57ad26c3..e26ba233f0 100644 --- a/search/result.cpp +++ b/search/result.cpp @@ -15,7 +15,7 @@ Result::Result(FeatureID const & id, m2::PointD const & pt, string const & str, , m_featureType(featureType) , m_metadata(meta) { - PatchNameIfNeeded(); + Init(true); } Result::Result(FeatureID const & id, m2::PointD const & pt, string const & str, @@ -26,14 +26,16 @@ Result::Result(FeatureID const & id, m2::PointD const & pt, string const & str, , m_region(region) , m_type(type) { - PatchNameIfNeeded(); + Init(false); } -void Result::PatchNameIfNeeded() +void Result::Init(bool metadataInitialized) { // Features with empty names can be found after suggestion. if (m_str.empty()) m_str = m_type; + + m_metadata.m_isInitialized = metadataInitialized; } Result::Result(m2::PointD const & pt, string const & str, diff --git a/search/result.hpp b/search/result.hpp index abd4c92a45..04028e1739 100644 --- a/search/result.hpp +++ b/search/result.hpp @@ -28,11 +28,12 @@ public: /// Metadata for search results. Considered valid if m_resultType == RESULT_FEATURE. struct Metadata { - Metadata() : m_isClosed(false), m_stars(0) {} + string m_cuisine; // Valid only if not empty. Used for restaurants. + int m_stars = 0; // Valid only if not 0. Used for hotels. + bool m_isClosed = false; // Valid for any result. - string m_cuisine; // Valid only if not empty. Used for restaurants. - bool m_isClosed; // Valid for any result. - int m_stars; // Valid only if not 0. Used for hotels. + /// true If the struct is already assigned or need to be calculated othrwise. + bool m_isInitialized = false; }; /// For RESULT_FEATURE. @@ -92,7 +93,7 @@ public: void AppendCity(string const & name); private: - void PatchNameIfNeeded(); + void Init(bool metadataInitialized); FeatureID m_id; m2::PointD m_center; @@ -101,6 +102,7 @@ private: string m_suggestionStr; buffer_vector, 4> m_hightlightRanges; +public: Metadata m_metadata; }; @@ -108,7 +110,8 @@ class Results { vector m_vec; - enum StatusT { + enum StatusT + { NONE, // default status ENDED_CANCELLED, // search ended with canceling ENDED // search ended itself