<commit_before>#include "search/v2/features_layer_matcher.hpp"

#include "search/reverse_geocoder.hpp"

#include "indexer/scales.hpp"

#include "base/assert.hpp"

namespace search
{
namespace v2
{
// static
double const FeaturesLayerMatcher::kBuildingRadiusMeters = 50;

FeaturesLayerMatcher::FeaturesLayerMatcher(Index & index, my::Cancellable const & cancellable)
  : m_context(nullptr)
  , m_reverseGeocoder(index)
  , m_nearbyStreetsCache("FeatureToNearbyStreets")
  , m_matchingStreetsCache("BuildingToStreet")
  , m_loader(scales::GetUpperScale(), ReverseGeocoder::kLookupRadiusM)
  , m_cancellable(cancellable)
{
}

void FeaturesLayerMatcher::SetContext(MwmContext * context)
{
  ASSERT(context, ());
  if (m_context == context)
    return;

  m_context = context;
  m_houseToStreetTable = HouseToStreetTable::Load(m_context->m_value);
  ASSERT(m_houseToStreetTable, ());
  m_loader.SetContext(context);
}

void FeaturesLayerMatcher::OnQueryFinished()
{
  m_nearbyStreetsCache.ClearIfNeeded();
  m_matchingStreetsCache.ClearIfNeeded();
  m_loader.OnQueryFinished();
}

uint32_t FeaturesLayerMatcher::GetMatchingStreet(uint32_t houseId)
{
  auto entry = m_matchingStreetsCache.Get(houseId);
  if (!entry.second)
    return entry.first;

  FeatureType houseFeature;
  GetByIndex(houseId, houseFeature);

  entry.first = GetMatchingStreetImpl(houseId, houseFeature);
  return entry.first;
}

uint32_t FeaturesLayerMatcher::GetMatchingStreet(uint32_t houseId, FeatureType & houseFeature)
{
  auto entry = m_matchingStreetsCache.Get(houseId);
  if (!entry.second)
    return entry.first;

  entry.first = GetMatchingStreetImpl(houseId, houseFeature);
  return entry.first;
}

vector<FeaturesLayerMatcher::TStreet> const &
FeaturesLayerMatcher::GetNearbyStreets(uint32_t featureId)
{
  auto entry = m_nearbyStreetsCache.Get(featureId);
  if (!entry.second)
    return entry.first;

  FeatureType feature;
  GetByIndex(featureId, feature);

  GetNearbyStreetsImpl(feature, entry.first);
  return entry.first;
}

vector<FeaturesLayerMatcher::TStreet> const &
FeaturesLayerMatcher::GetNearbyStreets(uint32_t featureId, FeatureType & feature)
{
  auto entry = m_nearbyStreetsCache.Get(featureId);
  if (!entry.second)
    return entry.first;

  GetNearbyStreetsImpl(feature, entry.first);
  return entry.first;
}

void FeaturesLayerMatcher::GetNearbyStreetsImpl(FeatureType & feature, vector<TStreet> & streets)
{
  m_reverseGeocoder.GetNearbyStreets(feature, streets);
  for (size_t i = 0; i < streets.size(); ++i)
  {
    if (streets[i].m_distanceMeters > ReverseGeocoder::kLookupRadiusM)
    {
      streets.resize(i);
      return;
    }
  }
}

uint32_t FeaturesLayerMatcher::GetMatchingStreetImpl(uint32_t houseId, FeatureType & houseFeature)
{
  auto const & streets = GetNearbyStreets(houseId, houseFeature);

  uint32_t streetId = kInvalidId;
  uint32_t streetIndex;
  if (!m_houseToStreetTable->Get(houseId, streetIndex))
    streetIndex = streets.size();

  if (streetIndex < streets.size() && streets[streetIndex].m_id.m_mwmId == m_context->m_id)
    streetId = streets[streetIndex].m_id.m_index;
  return streetId;
}

}  // namespace v2
}  // namespace search
<commit_msg>[search] Use the nearest street (in 100 meters) in search process if there is no stored feature’s street.<commit_after>#include "search/v2/features_layer_matcher.hpp"

#include "search/reverse_geocoder.hpp"

#include "indexer/scales.hpp"

#include "base/assert.hpp"

namespace search
{
namespace v2
{
// static
double const FeaturesLayerMatcher::kBuildingRadiusMeters = 50;

FeaturesLayerMatcher::FeaturesLayerMatcher(Index & index, my::Cancellable const & cancellable)
  : m_context(nullptr)
  , m_reverseGeocoder(index)
  , m_nearbyStreetsCache("FeatureToNearbyStreets")
  , m_matchingStreetsCache("BuildingToStreet")
  , m_loader(scales::GetUpperScale(), ReverseGeocoder::kLookupRadiusM)
  , m_cancellable(cancellable)
{
}

void FeaturesLayerMatcher::SetContext(MwmContext * context)
{
  ASSERT(context, ());
  if (m_context == context)
    return;

  m_context = context;
  m_houseToStreetTable = HouseToStreetTable::Load(m_context->m_value);
  ASSERT(m_houseToStreetTable, ());
  m_loader.SetContext(context);
}

void FeaturesLayerMatcher::OnQueryFinished()
{
  m_nearbyStreetsCache.ClearIfNeeded();
  m_matchingStreetsCache.ClearIfNeeded();
  m_loader.OnQueryFinished();
}

uint32_t FeaturesLayerMatcher::GetMatchingStreet(uint32_t houseId)
{
  auto entry = m_matchingStreetsCache.Get(houseId);
  if (!entry.second)
    return entry.first;

  FeatureType houseFeature;
  GetByIndex(houseId, houseFeature);

  entry.first = GetMatchingStreetImpl(houseId, houseFeature);
  return entry.first;
}

uint32_t FeaturesLayerMatcher::GetMatchingStreet(uint32_t houseId, FeatureType & houseFeature)
{
  auto entry = m_matchingStreetsCache.Get(houseId);
  if (!entry.second)
    return entry.first;

  entry.first = GetMatchingStreetImpl(houseId, houseFeature);
  return entry.first;
}

vector<FeaturesLayerMatcher::TStreet> const &
FeaturesLayerMatcher::GetNearbyStreets(uint32_t featureId)
{
  auto entry = m_nearbyStreetsCache.Get(featureId);
  if (!entry.second)
    return entry.first;

  FeatureType feature;
  GetByIndex(featureId, feature);

  GetNearbyStreetsImpl(feature, entry.first);
  return entry.first;
}

vector<FeaturesLayerMatcher::TStreet> const &
FeaturesLayerMatcher::GetNearbyStreets(uint32_t featureId, FeatureType & feature)
{
  auto entry = m_nearbyStreetsCache.Get(featureId);
  if (!entry.second)
    return entry.first;

  GetNearbyStreetsImpl(feature, entry.first);
  return entry.first;
}

void FeaturesLayerMatcher::GetNearbyStreetsImpl(FeatureType & feature, vector<TStreet> & streets)
{
  m_reverseGeocoder.GetNearbyStreets(feature, streets);
  for (size_t i = 0; i < streets.size(); ++i)
  {
    if (streets[i].m_distanceMeters > ReverseGeocoder::kLookupRadiusM)
    {
      streets.resize(i);
      return;
    }
  }
}

uint32_t FeaturesLayerMatcher::GetMatchingStreetImpl(uint32_t houseId, FeatureType & houseFeature)
{
  auto const & streets = GetNearbyStreets(houseId, houseFeature);

  uint32_t index;
  if (m_houseToStreetTable->Get(houseId, index) && index < streets.size())
    return streets[index].m_id.m_index;

  // If there is no saved street for feature, assume that it's a nearest street if it's too close.
  if (!streets.empty() && streets[0].m_distanceMeters < 100.0)
    return streets[0].m_id.m_index;

  return kInvalidId;
}

}  // namespace v2
}  // namespace search

{'fraction_non_alphanumeric': 0.11034164358264081, 'fraction_numerical': 0.009079716835949523, 'mean_word_length': 5.7910135841170325, 'pattern_counts': {'":': 0, '<': 14, '<?xml version=': 0, '>': 16, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4994846', 'n_tokens_mistral': 2321, 'n_tokens_neox': 2172, 'n_words': 377}