From 712df5f52cf0666e3e9d8ab0f511892da02728c8 Mon Sep 17 00:00:00 2001 From: Arsentiy Milchakov Date: Fri, 8 Nov 2019 15:55:37 +0300 Subject: [PATCH] [storage][promo] possibility to use top_countries_geo_ids from countries.txt is added into storage. --- search/region_info_getter.cpp | 3 ++- search/search_quality/helpers.cpp | 6 ++++-- storage/country_tree.cpp | 36 +++++++++++++++++++++++++------ storage/country_tree.hpp | 6 ++++-- storage/storage.cpp | 22 ++++++++++++++++--- storage/storage.hpp | 2 ++ storage/storage_defines.hpp | 1 + 7 files changed, 61 insertions(+), 15 deletions(-) diff --git a/search/region_info_getter.cpp b/search/region_info_getter.cpp index 5cd3556fd7..f6ca7e72eb 100644 --- a/search/region_info_getter.cpp +++ b/search/region_info_getter.cpp @@ -44,8 +44,9 @@ void RegionInfoGetter::LoadCountriesTree() storage::Affiliations affiliations; storage::CountryNameSynonyms countryNameSynonyms; storage::MwmTopCityGeoIds mwmTopCityGeoIds; + storage::MwmTopCountryGeoIds mwmTopCountryGeoIds; storage::LoadCountriesFromFile(COUNTRIES_FILE, m_countries, affiliations, countryNameSynonyms, - mwmTopCityGeoIds); + mwmTopCityGeoIds, mwmTopCountryGeoIds); } void RegionInfoGetter::SetLocale(string const & locale) diff --git a/search/search_quality/helpers.cpp b/search/search_quality/helpers.cpp index 70fdc219f4..43f177a5a1 100644 --- a/search/search_quality/helpers.cpp +++ b/search/search_quality/helpers.cpp @@ -182,8 +182,10 @@ void InitStorageData(storage::Affiliations & affiliations, storage::CountryTree countries; storage::MwmTopCityGeoIds mwmTopCityGeoIds; - auto const rv = storage::LoadCountriesFromFile(COUNTRIES_FILE, countries, affiliations, - countryNameSynonyms, mwmTopCityGeoIds); + storage::MwmTopCountryGeoIds mwmTopCountryGeoIds; + auto const rv = + storage::LoadCountriesFromFile(COUNTRIES_FILE, countries, affiliations, countryNameSynonyms, + mwmTopCityGeoIds, mwmTopCountryGeoIds); CHECK(rv != -1, ("Can't load countries from:", countriesFile)); } diff --git a/storage/country_tree.cpp b/storage/country_tree.cpp index f412eb8070..53aaa61a6a 100644 --- a/storage/country_tree.cpp +++ b/storage/country_tree.cpp @@ -38,6 +38,10 @@ public: virtual void InsertAffiliation(CountryId const & countryId, string const & affilation) = 0; virtual void InsertCountryNameSynonym(CountryId const & countryId, string const & synonym) = 0; virtual void InsertMwmTopCityGeoId(CountryId const & countryId, uint64_t const & geoObjectId) {} + virtual void InsertTopCountryGeoIds(CountryId const & countryId, + vector const & geoObjectIds) + { + } virtual OldMwmMapping GetMapping() const = 0; }; @@ -47,16 +51,18 @@ class StoreCountries : public StoreInterface Affiliations & m_affiliations; CountryNameSynonyms & m_countryNameSynonyms; MwmTopCityGeoIds & m_mwmTopCityGeoIds; + MwmTopCountryGeoIds & m_mwmTopCountryGeoIds; OldMwmMapping m_idsMapping; public: StoreCountries(CountryTree & countries, Affiliations & affiliations, - CountryNameSynonyms & countryNameSynonyms, - MwmTopCityGeoIds & mwmTopCityGeoIds) + CountryNameSynonyms & countryNameSynonyms, MwmTopCityGeoIds & mwmTopCityGeoIds, + MwmTopCountryGeoIds & mwmTopCountryGeoIds) : m_countries(countries) , m_affiliations(affiliations) , m_countryNameSynonyms(countryNameSynonyms) , m_mwmTopCityGeoIds(mwmTopCityGeoIds) + , m_mwmTopCountryGeoIds(mwmTopCountryGeoIds) { } ~StoreCountries() @@ -112,6 +118,15 @@ public: m_mwmTopCityGeoIds.emplace(countryId, move(id)); } + void InsertTopCountryGeoIds(CountryId const & countryId, + vector const & geoObjectIds) override + { + ASSERT(!countryId.empty(), ()); + ASSERT(!geoObjectIds.empty(), ()); + vector ids(geoObjectIds.cbegin(), geoObjectIds.cend()); + m_mwmTopCountryGeoIds.emplace(countryId, move(ids)); + } + OldMwmMapping GetMapping() const override { return m_idsMapping; } }; @@ -346,6 +361,11 @@ MwmSubtreeAttrs LoadGroupImpl(size_t depth, json_t * node, CountryId const & par if (geoObjectId != 0) store.InsertMwmTopCityGeoId(id, geoObjectId); + vector topCountryIds; + FromJSONObjectOptionalField(node, "top_countries_geo_ids", topCountryIds); + if (!topCountryIds.empty()) + store.InsertTopCountryGeoIds(id, topCountryIds); + int nodeSize; FromJSONObjectOptionalField(node, "s", nodeSize); ASSERT_LESS_OR_EQUAL(0, nodeSize, ()); @@ -399,7 +419,8 @@ bool LoadCountriesImpl(string const & jsonBuffer, StoreInterface & store) int64_t LoadCountriesFromBuffer(string const & jsonBuffer, CountryTree & countries, Affiliations & affiliations, CountryNameSynonyms & countryNameSynonyms, - MwmTopCityGeoIds & mwmTopCityGeoIds) + MwmTopCityGeoIds & mwmTopCityGeoIds, + MwmTopCountryGeoIds & mwmTopCountryGeoIds) { countries.Clear(); affiliations.clear(); @@ -410,8 +431,8 @@ int64_t LoadCountriesFromBuffer(string const & jsonBuffer, CountryTree & countri base::Json root(jsonBuffer.c_str()); FromJSONObject(root.get(), "v", version); - StoreCountries store(countries, affiliations, countryNameSynonyms, - mwmTopCityGeoIds); + StoreCountries store(countries, affiliations, countryNameSynonyms, mwmTopCityGeoIds, + mwmTopCountryGeoIds); if (!LoadCountriesImpl(jsonBuffer, store)) return -1; } @@ -425,12 +446,13 @@ int64_t LoadCountriesFromBuffer(string const & jsonBuffer, CountryTree & countri int64_t LoadCountriesFromFile(string const & path, CountryTree & countries, Affiliations & affiliations, CountryNameSynonyms & countryNameSynonyms, - MwmTopCityGeoIds & mwmTopCityGeoIds) + MwmTopCityGeoIds & mwmTopCityGeoIds, + MwmTopCountryGeoIds & mwmTopCountryGeoIds) { string json; ReaderPtr(GetPlatform().GetReader(path)).ReadAsString(json); return LoadCountriesFromBuffer(json, countries, affiliations, countryNameSynonyms, - mwmTopCityGeoIds); + mwmTopCityGeoIds, mwmTopCountryGeoIds); } void LoadCountryFile2CountryInfo(string const & jsonBuffer, map & id2info) diff --git a/storage/country_tree.hpp b/storage/country_tree.hpp index 3188f61ef2..5ac61de842 100644 --- a/storage/country_tree.hpp +++ b/storage/country_tree.hpp @@ -119,11 +119,13 @@ private: int64_t LoadCountriesFromBuffer(std::string const & buffer, CountryTree & countries, Affiliations & affiliations, CountryNameSynonyms & countryNameSynonyms, - MwmTopCityGeoIds & mwmTopCityGeoIds); + MwmTopCityGeoIds & mwmTopCityGeoIds, + MwmTopCountryGeoIds & mwmTopCountryGeoIds); int64_t LoadCountriesFromFile(std::string const & path, CountryTree & countries, Affiliations & affiliations, CountryNameSynonyms & countryNameSynonyms, - MwmTopCityGeoIds & mwmTopCityGeoIds); + MwmTopCityGeoIds & mwmTopCityGeoIds, + MwmTopCountryGeoIds & mwmTopCountryGeoIds); void LoadCountryFile2CountryInfo(std::string const & jsonBuffer, std::map & id2info); diff --git a/storage/storage.cpp b/storage/storage.cpp index b018e36856..71d9d08d58 100644 --- a/storage/storage.cpp +++ b/storage/storage.cpp @@ -123,7 +123,7 @@ Storage::Storage(string const & referenceCountriesTxtJsonForTesting, { m_currentVersion = LoadCountriesFromBuffer(referenceCountriesTxtJsonForTesting, m_countries, m_affiliations, - m_countryNameSynonyms, m_mwmTopCityGeoIds); + m_countryNameSynonyms, m_mwmTopCityGeoIds, m_mwmTopCountryGeoIds); CHECK_LESS_OR_EQUAL(0, m_currentVersion, ("Can't load test countries file")); CalcMaxMwmSizeBytes(); } @@ -659,8 +659,9 @@ void Storage::LoadCountriesFile(string const & pathToCountriesFile) { if (m_countries.IsEmpty()) { - m_currentVersion = LoadCountriesFromFile(pathToCountriesFile, m_countries, m_affiliations, - m_countryNameSynonyms, m_mwmTopCityGeoIds); + m_currentVersion = + LoadCountriesFromFile(pathToCountriesFile, m_countries, m_affiliations, + m_countryNameSynonyms, m_mwmTopCityGeoIds, m_mwmTopCountryGeoIds); LOG_SHORT(LINFO, ("Loaded countries list for version:", m_currentVersion)); if (m_currentVersion < 0) LOG(LERROR, ("Can't load countries file", pathToCountriesFile)); @@ -1794,6 +1795,21 @@ bool Storage::GetUpdateInfo(CountryId const & countryId, UpdateInfo & updateInfo return true; } +std::vector Storage::GetTopCountryGeoIds(CountryId const & countryId) const +{ + std::vector result; + + auto const collector = [this, &result](CountryId const & id, CountryTree::Node const &) { + auto const it = m_mwmTopCountryGeoIds.find(id); + if (it != m_mwmTopCountryGeoIds.cend()) + result.insert(result.end(), it->second.cbegin(), it->second.cend()); + }; + + ForEachAncestorExceptForTheRoot(countryId, collector); + + return result; +} + void Storage::PushToJustDownloaded(Queue::iterator justDownloadedItem) { m_justDownloaded.insert(justDownloadedItem->GetCountryId()); diff --git a/storage/storage.hpp b/storage/storage.hpp index cf7a87384d..8699853d9f 100644 --- a/storage/storage.hpp +++ b/storage/storage.hpp @@ -257,6 +257,7 @@ private: Affiliations m_affiliations; CountryNameSynonyms m_countryNameSynonyms; MwmTopCityGeoIds m_mwmTopCityGeoIds; + MwmTopCountryGeoIds m_mwmTopCountryGeoIds; MwmSize m_maxMwmSizeBytes = 0; @@ -464,6 +465,7 @@ public: CountryNameSynonyms const & GetCountryNameSynonyms() const { return m_countryNameSynonyms; } MwmTopCityGeoIds const & GetMwmTopCityGeoIds() const { return m_mwmTopCityGeoIds; } + std::vector GetTopCountryGeoIds(CountryId const & countryId) const; /// \brief Calls |toDo| for each node for subtree with |root|. /// For example ForEachInSubtree(GetRootId()) calls |toDo| for every node including diff --git a/storage/storage_defines.hpp b/storage/storage_defines.hpp index 6cde63257b..28bfb56ff6 100644 --- a/storage/storage_defines.hpp +++ b/storage/storage_defines.hpp @@ -27,6 +27,7 @@ using Affiliations = std::unordered_map>; using CountryNameSynonyms = std::unordered_map; /// Map from CountryId into city GeoObject id. using MwmTopCityGeoIds = std::unordered_map; +using MwmTopCountryGeoIds = std::unordered_map>; extern const storage::CountryId kInvalidCountryId;