diff --git a/indexer/categories_holder.hpp b/indexer/categories_holder.hpp index 552b60a3f4..71f96452dc 100644 --- a/indexer/categories_holder.hpp +++ b/indexer/categories_holder.hpp @@ -73,8 +73,18 @@ public: } } + /// Search name for type with preffered language. + /// If no name for this language, return first (en) name. + /// @return false if no categories for type. bool GetNameByType(uint32_t type, int8_t lang, string & name) const; + inline bool IsTypeExist(uint32_t type) const + { + // pass any language + string dummy; + return GetNameByType(type, 0, dummy); + } + inline void Swap(CategoriesHolder & r) { m_type2cat.swap(r.m_type2cat); diff --git a/indexer/feature_loader_base.hpp b/indexer/feature_loader_base.hpp index 55fccbd396..962e8d71be 100644 --- a/indexer/feature_loader_base.hpp +++ b/indexer/feature_loader_base.hpp @@ -41,7 +41,6 @@ namespace feature inline int GetScalesCount() const { return static_cast(m_header.GetScalesCount()); } inline int GetScale(int i) const { return m_header.GetScale(i); } inline int GetLastScale() const { return m_header.GetLastScale(); } - inline pair GetScaleRange() const { return m_header.GetScaleRange(); } }; class LoaderBase diff --git a/indexer/features_vector.hpp b/indexer/features_vector.hpp index 92766a1e8a..c66bbd1bee 100644 --- a/indexer/features_vector.hpp +++ b/indexer/features_vector.hpp @@ -28,12 +28,6 @@ public: m_RecordReader.ForEachRecord(DoGetFeatures(m_LoadInfo, toDo)); } - inline serial::CodingParams const & GetCodingParams() const - { - return m_LoadInfo.GetDefCodingParams(); - } - inline pair GetScaleRange() const { return m_LoadInfo.GetScaleRange(); } - private: template class DoGetFeatures { diff --git a/indexer/search_index_builder.cpp b/indexer/search_index_builder.cpp index f1bfccc649..43dad95ec6 100644 --- a/indexer/search_index_builder.cpp +++ b/indexer/search_index_builder.cpp @@ -8,6 +8,7 @@ #include "string_file.hpp" #include "classificator.hpp" #include "feature_visibility.hpp" +#include "categories_holder.hpp" #include "../defines.hpp" @@ -64,6 +65,8 @@ class FeatureInserter { StringsFile & m_names; + CategoriesHolder const & m_categories; + typedef StringsFile::ValueT ValueT; typedef search::trie::ValueReader SaverT; SaverT m_valueSaver; @@ -185,42 +188,52 @@ class FeatureInserter class AvoidEmptyName { - vector m_vec; + vector m_match[2]; + + template + void FillMatch(char const * (& arr)[count][ind]) + { + STATIC_ASSERT ( count > 0 ); + STATIC_ASSERT ( ind > 0 ); + + Classificator const & c = classif(); + + m_match[ind-1].reserve(count); + for (size_t i = 0; i < count; ++i) + { + vector v(arr[i], arr[i] + ind); + m_match[ind-1].push_back(c.GetTypeByPath(v)); + } + } public: AvoidEmptyName() { - char const * arr[][3] = { - { "place", "city", ""}, - { "place", "city", "capital"}, - { "place", "town", ""}, - { "place", "county", ""}, - { "place", "state", ""}, - { "place", "region", ""} + char const * arr1[][1] = { { "highway" }, { "natural" }, { "waterway"} }; + + char const * arr2[][2] = { + { "place", "city" }, + { "place", "town" }, + { "place", "county" }, + { "place", "state" }, + { "place", "region" } }; - Classificator const & c = classif(); - - size_t const count = ARRAY_SIZE(arr); - m_vec.reserve(count); - - for (size_t i = 0; i < count; ++i) - { - vector v; - v.push_back(arr[i][0]); - v.push_back(arr[i][1]); - if (strlen(arr[i][2]) > 0) - v.push_back(arr[i][2]); - - m_vec.push_back(c.GetTypeByPath(v)); - } + FillMatch(arr1); + FillMatch(arr2); } bool IsExist(feature::TypesHolder const & types) const { - for (size_t i = 0; i < m_vec.size(); ++i) - if (types.Has(m_vec[i])) - return true; + for (size_t i = 0; i < types.Size(); ++i) + for (size_t j = 0; j < ARRAY_SIZE(m_match); ++j) + { + uint32_t type = types[i]; + ftype::TruncValue(type, j+1); + + if (find(m_match[j].begin(), m_match[j].end(), type) != m_match[j].end()) + return true; + } return false; } @@ -228,9 +241,10 @@ class FeatureInserter public: FeatureInserter(StringsFile & names, + CategoriesHolder const & catHolder, serial::CodingParams const & cp, pair const & scales) - : m_names(names), m_valueSaver(cp), m_scales(scales) + : m_names(names), m_categories(catHolder), m_valueSaver(cp), m_scales(scales) { } @@ -262,6 +276,10 @@ public: // highway-primary-oneway or amenity-parking-fee. ftype::TruncValue(type, 2); + // Push to index only categorized types. + if (!m_categories.IsTypeExist(type)) + continue; + // Do index only for visible types in mwm. pair const r = feature::DrawableScaleRangeForType(type); if (my::between_s(m_scales.first, m_scales.second, r.first) || @@ -273,16 +291,19 @@ public: } }; -} // unnamed namespace - -void indexer::BuildSearchIndex(FeaturesVector const & featuresV, Writer & writer, - string const & tmpFilePath) +void BuildSearchIndex(FilesContainerR const & cont, CategoriesHolder const & catHolder, + Writer & writer, string const & tmpFilePath) { { - StringsFile names(tmpFilePath); - serial::CodingParams cp(search::GetCPForTrie(featuresV.GetCodingParams())); + feature::DataHeader header; + header.Load(cont.GetReader(HEADER_FILE_TAG)); + FeaturesVector featuresV(cont, header); - featuresV.ForEachOffset(FeatureInserter(names, cp, featuresV.GetScaleRange())); + serial::CodingParams cp(search::GetCPForTrie(header.GetDefCodingParams())); + + StringsFile names(tmpFilePath); + + featuresV.ForEachOffset(FeatureInserter(names, catHolder, cp, header.GetScaleRange())); names.EndAdding(); names.OpenForRead(); @@ -295,6 +316,8 @@ void indexer::BuildSearchIndex(FeaturesVector const & featuresV, Writer & writer FileWriter::DeleteFileX(tmpFilePath); } +} + bool indexer::BuildSearchIndexFromDatFile(string const & fName, bool forceRebuild) { LOG(LINFO, ("Start building search index. Bits = ", search::POINT_CODING_BITS)); @@ -311,13 +334,12 @@ bool indexer::BuildSearchIndexFromDatFile(string const & fName, bool forceRebuil if (!forceRebuild && readCont.IsReaderExist(SEARCH_INDEX_FILE_TAG)) return true; - feature::DataHeader header; - header.Load(readCont.GetReader(HEADER_FILE_TAG)); - - FeaturesVector featuresVector(readCont, header); - FileWriter writer(tmpFile); - BuildSearchIndex(featuresVector, writer, pl.WritablePathForFile(fName + ".search_index_1.tmp")); + + CategoriesHolder catHolder(pl.GetReader(SEARCH_CATEGORIES_FILE_NAME)); + + BuildSearchIndex(readCont, catHolder, writer, + pl.WritablePathForFile(fName + ".search_index_1.tmp")); LOG(LINFO, ("Search index size = ", writer.Size())); } diff --git a/indexer/search_index_builder.hpp b/indexer/search_index_builder.hpp index baaed3d6c8..41ea88b8cb 100644 --- a/indexer/search_index_builder.hpp +++ b/indexer/search_index_builder.hpp @@ -1,12 +1,8 @@ #pragma once #include "../std/string.hpp" -class FeaturesVector; -class Writer; namespace indexer { - void BuildSearchIndex(FeaturesVector const & featuresVector, Writer & writer, - string const & tmpFilePath); bool BuildSearchIndexFromDatFile(string const & fName, bool forceRebuild = false); -} // namespace indexer +}