Skip to content

Commit

Permalink
Review fixes.
Browse files Browse the repository at this point in the history
  • Loading branch information
mpimenov authored and tatiana-yan committed Feb 16, 2021
1 parent 5532046 commit c9b9ac3
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 10 deletions.
25 changes: 16 additions & 9 deletions search/processor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
#include <algorithm>
#include <cctype>
#include <memory>
#include <optional>
#include <set>
#include <sstream>
#include <utility>
Expand Down Expand Up @@ -441,12 +442,11 @@ void Processor::SearchByFeatureId()
if (m_query.size() > kMaxFeatureIdStringSize)
return;

using Trie = base::MemTrie<storage::CountryId, base::VectorValues<bool>>;
static Trie countriesTrie;
if (countriesTrie.GetNumNodes() == 1)
if (m_countriesTrie == nullptr)
{
m_countriesTrie = make_unique<CountriesTrie>();
for (auto const & country : m_infoGetter.GetCountries())
countriesTrie.Add(country.m_countryId, true);
m_countriesTrie->Add(country.m_countryId, true);
}

auto trimLeadingSpaces = [](string & s) {
Expand All @@ -473,15 +473,16 @@ void Processor::SearchByFeatureId()
return false;
};

auto const eatMwmName = [&trimLeadingSpaces](string & s, storage::CountryId & mwmName) -> bool {
auto const eatMwmName = [this, &trimLeadingSpaces](string & s,
storage::CountryId & mwmName) -> bool {
trimLeadingSpaces(s);

// Greedily eat as much as possible because some country names are prefixes of others.
optional<size_t> lastPos;
for (size_t i = 0; i < s.size(); ++i)
{
// todo(@m) This must be much faster but MemTrie's iterators do not expose nodes.
if (countriesTrie.HasKey(s.substr(0, i)))
if (m_countriesTrie->HasKey(s.substr(0, i)))
lastPos = i;
}
if (!lastPos)
Expand Down Expand Up @@ -519,6 +520,8 @@ void Processor::SearchByFeatureId()
string query(m_query);
strings::Trim(query);

strings::EatPrefix(query, "?");

string const kFidPrefix = "fid";
bool hasPrefix = false;

Expand Down Expand Up @@ -582,12 +585,16 @@ void Processor::SearchByFeatureId()
}
}

auto const tryEmitting = [this, &infos](storage::CountryId const & mwmName, uint32_t fid) {
auto const tryEmitting = [this, &infos](storage::CountryId const & mwmName,
optional<uint32_t> version, uint32_t fid) {
for (auto const & info : infos)
{
if (info->GetCountryName() != mwmName)
continue;

if (version && version != info->GetVersion())
continue;

auto guard = make_unique<FeaturesLoaderGuard>(m_dataSource, MwmSet::MwmId(info));
if (fid >= guard->GetNumFeatures())
continue;
Expand Down Expand Up @@ -615,7 +622,7 @@ void Processor::SearchByFeatureId()
if (parenPref == parenSuff && eatMwmName(s, mwmName) && strings::EatPrefix(s, ",") &&
eatFid(s, fid))
{
tryEmitting(mwmName, fid);
tryEmitting(mwmName, {} /* version */, fid);
}
}

Expand All @@ -635,7 +642,7 @@ void Processor::SearchByFeatureId()
ok = ok && eatFid(s, fid);
ok = ok && strings::EatPrefix(s, " }");
if (ok)
tryEmitting(mwmName, fid);
tryEmitting(mwmName, version, fid);
}
}

Expand Down
5 changes: 4 additions & 1 deletion search/processor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include "geometry/rect2d.hpp"

#include "base/cancellable.hpp"
#include "base/mem_trie.hpp"
#include "base/string_utils.hpp"

#include <cstddef>
Expand Down Expand Up @@ -116,7 +117,7 @@ class Processor : public base::Cancellable
protected:
// Show feature by FeatureId. May try to guess as much as possible after the "fid=" prefix but
// at least supports the formats below.
// 0. fid=123 to search for the feature with index 123, results ordered by distance
// 0. fid=123 or ?fid=123 to search for the feature with index 123, results ordered by distance
// from |m_position| or |m_viewport|, whichever is present and closer.
// 1. fid=MwmName,123 or fid=(MwmName,123) to search for the feature with
// index 123 in the Mwm "MwmName" (for example, "Laos" or "Laos.mwm").
Expand All @@ -139,6 +140,8 @@ class Processor : public base::Cancellable

CategoriesHolder const & m_categories;
storage::CountryInfoGetter const & m_infoGetter;
using CountriesTrie = base::MemTrie<storage::CountryId, base::VectorValues<bool>>;
std::unique_ptr<CountriesTrie> m_countriesTrie;

std::string m_region;
std::string m_query;
Expand Down

0 comments on commit c9b9ac3

Please sign in to comment.