Welcome to mirror list, hosted at ThFree Co, Russian Federation.

retrieval.cpp « search - github.com/mapsme/omim.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: a2ecb76677e212498384ff75a254d115a11bb182 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
#include "retrieval.hpp"

#include "cancel_exception.hpp"
#include "feature_offset_match.hpp"
#include "interval_set.hpp"
#include "search_index_values.hpp"
#include "search_trie.hpp"

#include "v2/mwm_context.hpp"
#include "v2/token_slice.hpp"

#include "indexer/feature.hpp"
#include "indexer/feature_algo.hpp"
#include "indexer/index.hpp"
#include "indexer/osm_editor.hpp"
#include "indexer/scales.hpp"
#include "indexer/search_delimiters.hpp"
#include "indexer/search_string_utils.hpp"
#include "indexer/trie_reader.hpp"

#include "platform/mwm_traits.hpp"
#include "platform/mwm_version.hpp"

#include "coding/compressed_bit_vector.hpp"
#include "coding/reader_wrapper.hpp"

#include "std/algorithm.hpp"

using osm::Editor;

namespace search
{
namespace v2
{
namespace
{
class FeaturesCollector
{
public:
  FeaturesCollector(my::Cancellable const & cancellable, vector<uint64_t> & features)
    : m_cancellable(cancellable), m_features(features), m_counter(0)
  {
  }

  template <typename TValue>
  void operator()(TValue const & value)
  {
    if ((++m_counter & 0xFF) == 0)
      BailIfCancelled(m_cancellable);
    m_features.push_back(value.m_featureId);
  }

  inline void operator()(uint32_t feature) { m_features.push_back(feature); }

  inline void operator()(uint64_t feature) { m_features.push_back(feature); }

private:
  my::Cancellable const & m_cancellable;
  vector<uint64_t> & m_features;
  uint32_t m_counter;
};

class EditedFeaturesHolder
{
public:
  EditedFeaturesHolder(MwmSet::MwmId const & id) : m_id(id)
  {
    auto & editor = Editor::Instance();
    m_deleted = editor.GetFeaturesByStatus(id, Editor::FeatureStatus::Deleted);
    m_modified = editor.GetFeaturesByStatus(id, Editor::FeatureStatus::Modified);
    m_created = editor.GetFeaturesByStatus(id, Editor::FeatureStatus::Created);
  }

  bool ModifiedOrDeleted(uint32_t featureIndex) const
  {
    return binary_search(m_deleted.begin(), m_deleted.end(), featureIndex) ||
           binary_search(m_modified.begin(), m_modified.end(), featureIndex);
  }

  template <typename TFn>
  void ForEachModifiedOrCreated(TFn && fn)
  {
    ForEach(m_modified, fn);
    ForEach(m_created, fn);
  }

private:
  template <typename TFn>
  void ForEach(vector<uint32_t> const & features, TFn & fn)
  {
    auto & editor = Editor::Instance();
    for (auto const index : features)
    {
      FeatureType ft;
      VERIFY(editor.GetEditedFeature(m_id, index, ft), ());
      fn(ft, index);
    }
  }

  MwmSet::MwmId const & m_id;
  vector<uint32_t> m_deleted;
  vector<uint32_t> m_modified;
  vector<uint32_t> m_created;
};

unique_ptr<coding::CompressedBitVector> SortFeaturesAndBuildCBV(vector<uint64_t> && features)
{
  my::SortUnique(features);
  return coding::CompressedBitVectorBuilder::FromBitPositions(move(features));
}

/// Check that any from first matches any from second.
template <class TComp, class T>
bool IsFirstMatchesSecond(vector<T> const & first, vector<T> const & second, TComp const & comp)
{
  if (second.empty())
    return true;

  for (auto const & s : second)
  {
    for (auto const & f : first)
    {
      if (comp(f, s))
        return true;
    }
  }
  return false;
}

bool MatchFeatureByName(FeatureType const & ft, QueryParams const & params)
{
  using namespace strings;

  bool matched = false;
  ft.ForEachName([&](int8_t lang, string const & utf8Name)
  {
    if (utf8Name.empty() || params.m_langs.count(lang) == 0)
      return true;

    vector<UniString> nameTokens;
    NormalizeAndTokenizeString(utf8Name, nameTokens, Delimiters());

    auto const matchPrefix = [](UniString const & s1, UniString const & s2)
    {
      return StartsWith(s1, s2);
    };
    if (!IsFirstMatchesSecond(nameTokens, params.m_prefixTokens, matchPrefix))
      return true;

    for (auto const & synonyms : params.m_tokens)
    {
      if (!IsFirstMatchesSecond(nameTokens, synonyms, equal_to<UniString>()))
        return true;
    }

    matched = true;
    return false;
  });

  return matched;
}

bool MatchFeatureByPostcode(FeatureType const & ft, v2::TokenSlice const & slice)
{
  string const postcode = ft.GetMetadata().Get(feature::Metadata::FMD_POSTCODE);
  vector<strings::UniString> tokens;
  NormalizeAndTokenizeString(postcode, tokens, Delimiters());
  if (slice.Size() > tokens.size())
    return false;
  for (size_t i = 0; i < slice.Size(); ++i)
  {
    if (slice.IsPrefix(i))
    {
      if (!StartsWith(tokens[i], slice.Get(i).front()))
        return false;
    }
    else if (tokens[i] != slice.Get(i).front())
    {
      return false;
    }
  }
  return true;
}

template<typename TValue>
using TrieRoot = trie::Iterator<ValueList<TValue>>;

template <typename TValue, typename TFn>
void WithSearchTrieRoot(MwmValue & value, TFn && fn)
{
  serial::CodingParams codingParams(trie::GetCodingParams(value.GetHeader().GetDefCodingParams()));
  ModelReaderPtr searchReader = value.m_cont.GetReader(SEARCH_INDEX_FILE_TAG);

  auto const trieRoot = trie::ReadTrie<SubReaderWrapper<Reader>, ValueList<TValue>>(
      SubReaderWrapper<Reader>(searchReader.GetPtr()), SingleValueSerializer<TValue>(codingParams));

  return fn(*trieRoot);
}

// Retrieves from the search index corresponding to |value| all
// features matching to |params|.
template <typename TValue>
unique_ptr<coding::CompressedBitVector> RetrieveAddressFeaturesImpl(
    MwmSet::MwmId const & id, MwmValue & value, my::Cancellable const & cancellable,
    QueryParams const & params)
{
  EditedFeaturesHolder holder(id);
  vector<uint64_t> features;
  FeaturesCollector collector(cancellable, features);

  WithSearchTrieRoot<TValue>(value, [&](TrieRoot<TValue> const & root)
  {
    MatchFeaturesInTrie(params, root, [&holder](uint32_t featureIndex)
                        {
                          return !holder.ModifiedOrDeleted(featureIndex);
                        },
                        collector);
  });
  holder.ForEachModifiedOrCreated([&](FeatureType & ft, uint64_t index)
                                  {
                                    if (MatchFeatureByName(ft, params))
                                      features.push_back(index);
                                  });
  return SortFeaturesAndBuildCBV(move(features));
}

template <typename TValue>
unique_ptr<coding::CompressedBitVector> RetrievePostcodeFeaturesImpl(
    MwmSet::MwmId const & id, MwmValue & value, my::Cancellable const & cancellable,
    TokenSlice const & slice)
{
  EditedFeaturesHolder holder(id);
  vector<uint64_t> features;
  FeaturesCollector collector(cancellable, features);

  WithSearchTrieRoot<TValue>(value, [&](TrieRoot<TValue> const & root)
  {
    MatchPostcodesInTrie(slice, root, [&holder](uint32_t featureIndex)
                         {
                           return !holder.ModifiedOrDeleted(featureIndex);
                         },
                         collector);
  });
  holder.ForEachModifiedOrCreated([&](FeatureType & ft, uint64_t index)
                                  {
                                    if (MatchFeatureByPostcode(ft, slice))
                                      features.push_back(index);
                                  });
  return SortFeaturesAndBuildCBV(move(features));
}

// Retrieves from the geometry index corresponding to handle all
// features from |coverage|.
unique_ptr<coding::CompressedBitVector> RetrieveGeometryFeaturesImpl(
    v2::MwmContext const & context, my::Cancellable const & cancellable,
    covering::IntervalsT const & coverage, int scale)
{
  vector<uint64_t> features;

  FeaturesCollector collector(cancellable, features);

  context.ForEachIndex(coverage, scale, collector);
  return SortFeaturesAndBuildCBV(move(features));
}

template <typename T>
struct RetrieveAddressFeaturesAdaptor
{
  template <typename... TArgs>
  unique_ptr<coding::CompressedBitVector> operator()(TArgs &&... args)
  {
    return RetrieveAddressFeaturesImpl<T>(forward<TArgs>(args)...);
  }
};

template <typename T>
struct RetrievePostcodeFeaturesAdaptor
{
  template <typename... TArgs>
  unique_ptr<coding::CompressedBitVector> operator()(TArgs &&... args)
  {
    return RetrievePostcodeFeaturesImpl<T>(forward<TArgs>(args)...);
  }
};

template <template <typename> class T>
struct Selector
{
  template <typename... TArgs>
  unique_ptr<coding::CompressedBitVector> operator()(MwmSet::MwmId const & id, MwmValue & value,
                                                     TArgs &&... args)
  {
    version::MwmTraits mwmTraits(value.GetMwmVersion().GetFormat());

    if (mwmTraits.GetSearchIndexFormat() ==
        version::MwmTraits::SearchIndexFormat::FeaturesWithRankAndCenter)
    {
      T<FeatureWithRankAndCenter> t;
      return t(id, value, forward<TArgs>(args)...);
    }
    if (mwmTraits.GetSearchIndexFormat() ==
        version::MwmTraits::SearchIndexFormat::CompressedBitVector)
    {
      T<FeatureIndexValue> t;
      return t(id, value, forward<TArgs>(args)...);
    }
    return unique_ptr<coding::CompressedBitVector>();
  }
};
}  // namespace

unique_ptr<coding::CompressedBitVector> RetrieveAddressFeatures(MwmSet::MwmId const & id,
                                                                MwmValue & value,
                                                                my::Cancellable const & cancellable,
                                                                QueryParams const & params)
{
  Selector<RetrieveAddressFeaturesAdaptor> selector;
  return selector(id, value, cancellable, params);
}

unique_ptr<coding::CompressedBitVector> RetrievePostcodeFeatures(
    MwmSet::MwmId const & id, MwmValue & value, my::Cancellable const & cancellable,
    TokenSlice const & slice)
{
  Selector<RetrievePostcodeFeaturesAdaptor> selector;
  return selector(id, value, cancellable, slice);
}

unique_ptr<coding::CompressedBitVector> RetrieveGeometryFeatures(
    MwmContext const & context, my::Cancellable const & cancellable,
    m2::RectD const & rect, int scale)
{
  covering::IntervalsT coverage;
  v2::CoverRect(rect, scale, coverage);
  return RetrieveGeometryFeaturesImpl(context, cancellable, coverage, scale);
}

} // namespace v2
} // namespace search