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

index.hpp « indexer - github.com/mapsme/omim.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 2de961d7f98f5f467d8648601f3a9629cd2d6f88 (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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
#pragma once
#include "indexer/cell_id.hpp"
#include "indexer/data_factory.hpp"
#include "indexer/feature.hpp"
#include "indexer/feature_covering.hpp"
#include "indexer/features_offsets_table.hpp"
#include "indexer/features_vector.hpp"
#include "indexer/mwm_set.hpp"
#include "indexer/osm_editor.hpp"
#include "indexer/scale_index.hpp"
#include "indexer/unique_index.hpp"

#include "coding/file_container.hpp"

#include "defines.hpp"

#include "base/macros.hpp"

#include <algorithm>
#include <limits>
#include <memory>
#include <utility>
#include <vector>

class MwmInfoEx : public MwmInfo
{
private:
  friend class Index;
  friend class MwmValue;

  // weak_ptr is needed here to access offsets table in already
  // instantiated MwmValue-s for the MWM, including MwmValues in the
  // MwmSet's cache. We can't use shared_ptr because of offsets table
  // must be removed as soon as the last corresponding MwmValue is
  // destroyed. Also, note that this value must be used and modified
  // only in MwmValue::SetTable() method, which, in turn, is called
  // only in the MwmSet critical section, protected by a lock.  So,
  // there's an implicit synchronization on this field.
  std::weak_ptr<feature::FeaturesOffsetsTable> m_table;
};

class MwmValue : public MwmSet::MwmValueBase
{
public:
  FilesContainerR const m_cont;
  IndexFactory m_factory;
  platform::LocalCountryFile const m_file;

  std::shared_ptr<feature::FeaturesOffsetsTable> m_table;

  explicit MwmValue(platform::LocalCountryFile const & localFile);
  void SetTable(MwmInfoEx & info);

  inline feature::DataHeader const & GetHeader() const { return m_factory.GetHeader(); }
  inline feature::RegionData const & GetRegionData() const { return m_factory.GetRegionData(); }
  inline version::MwmVersion const & GetMwmVersion() const { return m_factory.GetMwmVersion(); }
  inline std::string const & GetCountryFileName() const
  {
    return m_file.GetCountryFile().GetName();
  }

  inline bool HasSearchIndex() { return m_cont.IsExist(SEARCH_INDEX_FILE_TAG); }
  inline bool HasGeometryIndex() { return m_cont.IsExist(INDEX_FILE_TAG); }
};

class Index : public MwmSet
{
protected:
  /// MwmSet overrides:
  //@{
  std::unique_ptr<MwmInfo> CreateInfo(platform::LocalCountryFile const & localFile) const override;

  std::unique_ptr<MwmValueBase> CreateValue(MwmInfo & info) const override;
  //@}

public:
  /// Registers a new map.
  std::pair<MwmId, RegResult> RegisterMap(platform::LocalCountryFile const & localFile);

  /// Deregisters a map from internal records.
  ///
  /// \param countryFile A countryFile denoting a map to be deregistered.
  /// \return True if the map was successfully deregistered. If map is locked
  ///         now, returns false.
  bool DeregisterMap(platform::CountryFile const & countryFile);

private:

  template <typename F> class ReadMWMFunctor
  {
    F & m_f;
    osm::Editor & m_editor = osm::Editor::Instance();
  public:
    ReadMWMFunctor(F & f) : m_f(f) {}

    /// Used by Editor to inject new features.
    void operator()(FeatureType & feature)
    {
      m_f(feature);
    }

    void operator()(MwmHandle const & handle, covering::CoveringGetter & cov, int scale) const
    {
      MwmValue const * pValue = handle.GetValue<MwmValue>();
      if (pValue)
      {
        feature::DataHeader const & header = pValue->GetHeader();

        // Prepare needed covering.
        auto const lastScale = header.GetLastScale();

        // In case of WorldCoasts we should pass correct scale in ForEachInIntervalAndScale.
        if (scale > lastScale)
          scale = lastScale;

        // Use last coding scale for covering (see index_builder.cpp).
        covering::Intervals const & intervals = cov.Get<RectId::DEPTH_LEVELS>(lastScale);

        // Prepare features reading.
        FeaturesVector const fv(pValue->m_cont, header, pValue->m_table.get());
        ScaleIndex<ModelReaderPtr> index(pValue->m_cont.GetReader(INDEX_FILE_TAG),
                                         pValue->m_factory);

        // iterate through intervals
        CheckUniqueIndexes checkUnique(header.GetFormat() >= version::Format::v5);
        MwmId const & mwmID = handle.GetId();

        for (auto const & i : intervals)
        {
          index.ForEachInIntervalAndScale(
              [&](uint32_t index)
              {
                if (!checkUnique(index))
                  return;

                FeatureType feature;
                switch (m_editor.GetFeatureStatus(mwmID, index))
                {
                case osm::Editor::FeatureStatus::Deleted:
                case osm::Editor::FeatureStatus::Obsolete:
                  return;
                case osm::Editor::FeatureStatus::Modified:
                  VERIFY(m_editor.GetEditedFeature(mwmID, index, feature), ());
                  m_f(feature);
                  return;
                case osm::Editor::FeatureStatus::Created:
                  CHECK(false, ("Created features index should be generated."));
                case osm::Editor::FeatureStatus::Untouched: break;
                }

                fv.GetByIndex(index, feature);
                feature.SetID(FeatureID(mwmID, index));
                m_f(feature);
              },
              i.first, i.second, scale);
        }
      }
    }
  };

  template <typename F> class ReadFeatureIndexFunctor
  {
    F & m_f;
    osm::Editor & m_editor = osm::Editor::Instance();
  public:
    ReadFeatureIndexFunctor(F & f) : m_f(f) {}

    /// Used by Editor to inject new features.
    void operator()(FeatureID const & fid) const
    {
      m_f(fid);
    }

    void operator()(MwmHandle const & handle, covering::CoveringGetter & cov, int scale) const
    {
      MwmValue const * pValue = handle.GetValue<MwmValue>();
      if (pValue)
      {
        feature::DataHeader const & header = pValue->GetHeader();

        // Prepare needed covering.
        int const lastScale = header.GetLastScale();

        // In case of WorldCoasts we should pass correct scale in ForEachInIntervalAndScale.
        if (scale > lastScale)
          scale = lastScale;

        // Use last coding scale for covering (see index_builder.cpp).
        covering::Intervals const & intervals = cov.Get<RectId::DEPTH_LEVELS>(lastScale);
        ScaleIndex<ModelReaderPtr> const index(pValue->m_cont.GetReader(INDEX_FILE_TAG), pValue->m_factory);

        // Iterate through intervals.
        CheckUniqueIndexes checkUnique(header.GetFormat() >= version::Format::v5);
        MwmId const & mwmID = handle.GetId();

        for (auto const & i : intervals)
        {
          index.ForEachInIntervalAndScale(
              [&](uint32_t index)
              {
                if (osm::Editor::FeatureStatus::Deleted !=
                        m_editor.GetFeatureStatus(mwmID, index) &&
                    checkUnique(index))
                  m_f(FeatureID(mwmID, index));
              },
              i.first, i.second, scale);
        }
      }
    }
  };

public:
  template <typename F>
  void ForEachInRect(F && f, m2::RectD const & rect, int scale) const
  {
    ReadMWMFunctor<F> implFunctor(f);
    ForEachInIntervals(implFunctor, covering::ViewportWithLowLevels, rect, scale);
  }

  template <typename F>
  void ForEachFeatureIDInRect(F && f, m2::RectD const & rect, int scale) const
  {
    ReadFeatureIndexFunctor<F> implFunctor(f);
    ForEachInIntervals(implFunctor, covering::LowLevelsOnly, rect, scale);
  }

  template <typename F>
  void ForEachInScale(F && f, int scale) const
  {
    ReadMWMFunctor<F> implFunctor(f);
    ForEachInIntervals(implFunctor, covering::FullCover, m2::RectD::GetInfiniteRect(), scale);
  }

  template <typename F>
  void ReadFeature(F && f, FeatureID const & feature) const
  {
    return ReadFeatures(forward<F>(f), {feature});
  }

  // "features" must be sorted using FeatureID::operator< as predicate.
  template <typename F>
  void ReadFeatures(F && f, std::vector<FeatureID> const & features) const
  {
    auto fidIter = features.begin();
    auto const endIter = features.end();
    auto & editor = osm::Editor::Instance();
    while (fidIter != endIter)
    {
      MwmId const & id = fidIter->m_mwmId;
      MwmHandle const handle = GetMwmHandleById(id);
      if (handle.IsAlive())
      {
        MwmValue const * pValue = handle.GetValue<MwmValue>();
        FeaturesVector const featureReader(pValue->m_cont, pValue->GetHeader(),
                                           pValue->m_table.get());
        do
        {
          osm::Editor::FeatureStatus const fts = editor.GetFeatureStatus(id, fidIter->m_index);
          ASSERT_NOT_EQUAL(osm::Editor::FeatureStatus::Deleted, fts,
                           ("Deleted feature was cached. It should not be here. Please review your code."));
          FeatureType featureType;
          if (fts == osm::Editor::FeatureStatus::Modified || fts == osm::Editor::FeatureStatus::Created)
          {
            VERIFY(editor.GetEditedFeature(id, fidIter->m_index, featureType), ());
          }
          else
          {
            featureReader.GetByIndex(fidIter->m_index, featureType);
            featureType.SetID(*fidIter);
          }
          f(featureType);
        }
        while (++fidIter != endIter && id == fidIter->m_mwmId);
      }
      else
      {
        // Skip unregistered mwm files.
        while (++fidIter != endIter && id == fidIter->m_mwmId);
      }
    }
  }

  /// Guard for loading features from particular MWM by demand.
  /// @note This guard is suitable when mwm is loaded.
  class FeaturesLoaderGuard
  {
  public:
    FeaturesLoaderGuard(Index const & index, MwmId const & id);

    inline MwmSet::MwmId const & GetId() const { return m_handle.GetId(); }
    std::string GetCountryFileName() const;
    bool IsWorld() const;

    std::unique_ptr<FeatureType> GetOriginalFeatureByIndex(uint32_t index) const;
    std::unique_ptr<FeatureType> GetOriginalOrEditedFeatureByIndex(uint32_t index) const;

    /// Everyone, except Editor core, should use this method.
    WARN_UNUSED_RESULT bool GetFeatureByIndex(uint32_t index, FeatureType & ft) const;

    /// Editor core only method, to get 'untouched', original version of feature.
    WARN_UNUSED_RESULT bool GetOriginalFeatureByIndex(uint32_t index, FeatureType & ft) const;

    size_t GetNumFeatures() const;


  private:
    MwmHandle m_handle;
    std::unique_ptr<FeaturesVector> m_vector;
    osm::Editor & m_editor = osm::Editor::Instance();
  };

  template <typename F>
  void ForEachInRectForMWM(F && f, m2::RectD const & rect, int scale, MwmId const & id) const
  {
    MwmHandle const handle = GetMwmHandleById(id);
    if (handle.IsAlive())
    {
      covering::CoveringGetter cov(rect, covering::ViewportWithLowLevels);
      ReadMWMFunctor<F> fn(f);
      fn(handle, cov, scale);
    }
  }

private:

  template <typename F>
  void ForEachInIntervals(F && f, covering::CoveringMode mode, m2::RectD const & rect,
                          int scale) const
  {
    std::vector<std::shared_ptr<MwmInfo>> mwms;
    GetMwmsInfo(mwms);

    covering::CoveringGetter cov(rect, mode);

    MwmId worldID[2];

    osm::Editor & editor = osm::Editor::Instance();

    for (shared_ptr<MwmInfo> const & info : mwms)
    {
      if (info->m_minScale <= scale && scale <= info->m_maxScale &&
          rect.IsIntersect(info->m_limitRect))
      {
        MwmId const id(info);
        switch (info->GetType())
        {
          case MwmInfo::COUNTRY:
          {
            MwmHandle const handle = GetMwmHandleById(id);
            f(handle, cov, scale);
            // Check created features container.
            // Need to do it on a per-mwm basis, because Drape relies on features in a sorted order.
            editor.ForEachFeatureInMwmRectAndScale(id, f, rect, scale);
          }
          break;

          case MwmInfo::COASTS:
            worldID[0] = id;
            break;

          case MwmInfo::WORLD:
            worldID[1] = id;
            break;
        }
      }
    }

    if (worldID[0].IsAlive())
    {
      MwmHandle const handle = GetMwmHandleById(worldID[0]);
      f(handle, cov, scale);
      // Check edited/created features container.
      // Need to do it on a per-mwm basis, because Drape relies on features in a sorted order.
      editor.ForEachFeatureInMwmRectAndScale(worldID[0], f, rect, scale);
    }

    if (worldID[1].IsAlive())
    {
      MwmHandle const handle = GetMwmHandleById(worldID[1]);
      f(handle, cov, scale);
      // Check edited/created features container.
      // Need to do it on a per-mwm basis, because Drape relies on features in a sorted order.
      editor.ForEachFeatureInMwmRectAndScale(worldID[1], f, rect, scale);
    }
  }
};