Merge pull request #170 from vng/search

Fixed search bugs.
This commit is contained in:
Lev Dragunov 2015-10-12 17:33:19 +03:00
commit 208faf9b78
5 changed files with 57 additions and 43 deletions

View file

@ -242,7 +242,7 @@ Result PreResult2::GeneratePointResult(storage::CountryInfoGetter const & infoGe
CategoriesHolder const * pCat,
set<uint32_t> const * pTypes, int8_t locale) const
{
uint8_t const type = GetBestType(pTypes);
uint32_t const type = GetBestType(pTypes);
return Result(m_id, GetCenter(), m_str, GetRegionName(infoGetter, type),
ReadableFeatureType(pCat, type, locale));
}
@ -320,27 +320,19 @@ string PreResult2::DebugPrint() const
uint32_t PreResult2::GetBestType(set<uint32_t> const * pPrefferedTypes) const
{
uint32_t type = 0;
if (pPrefferedTypes)
{
for (uint32_t t : m_types)
if (pPrefferedTypes->count(t) > 0)
{
type = t;
break;
}
}
if (type == 0)
{
type = m_types.GetBestType();
// Do type truncate (2-level is enough for search results) only for
// non-preffered types (types from categories leave original).
ftype::TruncValue(type, 2);
for (uint32_t type : m_types)
{
if (pPrefferedTypes->count(type) > 0)
return type;
}
}
// Do type truncate (2-level is enough for search results) only for
// non-preffered types (types from categories leave original).
uint32_t type = m_types.GetBestType();
ftype::TruncValue(type, 2);
return type;
}

View file

@ -91,11 +91,9 @@ private:
};
LocalityItem::LocalityItem(m2::RectD const & rect, uint32_t population, ID id, string const & name)
: m_rect(rect), m_name(name), m_population(population), m_id(id)
{
}
LocalityFinder::LocalityFinder(Index const * pIndex)
@ -150,26 +148,43 @@ void LocalityFinder::RecreateCache(Cache & cache, m2::RectD rect) const
}
}
void LocalityFinder::SetViewportByIndex(m2::RectD const & rect, size_t idx)
void LocalityFinder::SetViewportByIndex(m2::RectD const & viewport, size_t idx)
{
ASSERT_LESS(idx, (size_t)MAX_VIEWPORT_COUNT, ());
RecreateCache(m_cache[idx], rect);
RecreateCache(m_cache[idx], viewport);
}
void LocalityFinder::GetLocalityInViewport(const m2::PointD & pt, string & name) const
void LocalityFinder::SetReservedViewportIfNeeded(m2::RectD const & viewport)
{
for (size_t i = 0; i < MAX_VIEWPORT_COUNT; ++i)
m_cache[i].GetLocality(pt, name);
size_t constexpr kReservedIndex = 2;
if (m_cache[kReservedIndex].m_rect.IsValid() &&
m_cache[kReservedIndex].m_rect.IsRectInside(viewport))
{
return;
}
SetViewportByIndex(viewport, kReservedIndex);
}
void LocalityFinder::GetLocalityCreateCache(const m2::PointD & pt, string & name) const
void LocalityFinder::GetLocalityInViewport(m2::PointD const & pt, string & name) const
{
name.clear();
for (size_t i = 0; i < MAX_VIEWPORT_COUNT; ++i)
{
m_cache[i].GetLocality(pt, name);
if (!name.empty())
break;
}
}
void LocalityFinder::GetLocalityCreateCache(m2::PointD const & pt, string & name)
{
// search in temporary caches and find most unused cache
size_t minUsageIdx = 0;
size_t minUsage = numeric_limits<size_t>::max();
for (size_t idx = 0; idx < MAX_CACHE_TMP_COUNT; ++idx)
for (size_t idx = 0; idx < MAX_VIEWPORT_COUNT; ++idx)
{
Cache const & cache = m_cache_tmp[idx];
Cache const & cache = m_cache[idx];
cache.GetLocality(pt, name);
if (!name.empty())
return;
@ -181,7 +196,7 @@ void LocalityFinder::GetLocalityCreateCache(const m2::PointD & pt, string & name
}
}
Cache & cache = m_cache_tmp[minUsageIdx];
Cache & cache = m_cache[minUsageIdx];
RecreateCache(cache, MercatorBounds::RectByCenterXYAndSizeInMeters(pt, MAX_RADIUS_CITY));
cache.GetLocality(pt, name);
}
@ -190,9 +205,6 @@ void LocalityFinder::ClearCacheAll()
{
for (size_t i = 0; i < MAX_VIEWPORT_COUNT; ++i)
ClearCache(i);
for (size_t i = 0; i < MAX_CACHE_TMP_COUNT; ++i)
m_cache_tmp[i].Clear();
}
void LocalityFinder::ClearCache(size_t idx)
@ -201,8 +213,6 @@ void LocalityFinder::ClearCache(size_t idx)
m_cache[idx].Clear();
}
void LocalityFinder::Cache::Clear()
{
m_usage = 0;

View file

@ -55,12 +55,14 @@ public:
}
}
void SetViewportByIndex(m2::RectD const & rect, size_t idx);
void SetViewportByIndex(m2::RectD const & viewport, size_t idx);
/// Set new viewport for the reserved slot only if it's no a part of the previous one.
void SetReservedViewportIfNeeded(m2::RectD const & viewport);
/// Check for localities in pre-cached viewports only.
void GetLocalityInViewport(m2::PointD const & pt, string & name) const;
/// Checl for localities in all Index and make new cache if needed.
void GetLocalityCreateCache(m2::PointD const & pt, string & name) const;
/// Check for localities in all Index and make new cache if needed.
void GetLocalityCreateCache(m2::PointD const & pt, string & name);
void ClearCacheAll();
void ClearCache(size_t idx);
@ -74,11 +76,10 @@ private:
Index const * m_pIndex;
enum { MAX_VIEWPORT_COUNT = 3, MAX_CACHE_TMP_COUNT = 4};
enum { MAX_VIEWPORT_COUNT = 3 };
Cache m_cache[MAX_VIEWPORT_COUNT];
mutable Cache m_cache_tmp[MAX_CACHE_TMP_COUNT];
int8_t m_lang;
};
}
} // namespace search

View file

@ -239,7 +239,8 @@ void Engine::SearchAsync()
if (res.GetCount() > 0)
EmitResults(params, res);
}
else
if (res.GetCount() < RESULTS_COUNT)
{
while (!m_query->IsCancelled())
{
@ -266,7 +267,7 @@ void Engine::SearchAsync()
// Make additional search in whole mwm when not enough results (only for non-empty query).
size_t const count = res.GetCount();
if (!viewportSearch && !m_query->IsCancelled() && count < RESULTS_COUNT)
if (!m_query->IsCancelled() && count < RESULTS_COUNT)
{
try
{

View file

@ -931,7 +931,7 @@ void Query::ProcessSuggestions(vector<T> & vec, Results & res) const
return;
int added = 0;
for (typename vector<T>::iterator i = vec.begin(); i != vec.end();)
for (auto i = vec.begin(); i != vec.end();)
{
impl::PreResult2 const & r = **i;
@ -1967,6 +1967,16 @@ void Query::SearchAdditional(Results & res, size_t resCount)
}
}
#ifdef FIND_LOCALITY_TEST
m2::RectD rect;
for (auto const & r : m_results[0])
rect.Add(r.GetCenter());
// Hack with 90.0 is important for the countries divided by 180 meridian.
if (rect.IsValid() && (rect.maxX() - rect.minX()) <= 90.0)
m_locality.SetReservedViewportIfNeeded(rect);
#endif
FlushResults(res, true, resCount);
}
}