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

data_source.hpp « indexer - github.com/mapsme/omim.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 8e8edfb2d0c8e0ff4315929f69eaf131fdfc7051 (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
#pragma once

#include "indexer/cell_id.hpp"
#include "indexer/feature.hpp"
#include "indexer/feature_covering.hpp"
#include "indexer/features_offsets_table.hpp"
#include "indexer/feature_source.hpp"
#include "indexer/features_vector.hpp"
#include "indexer/mwm_set.hpp"
#include "indexer/scale_index.hpp"
#include "indexer/unique_index.hpp"

#include "coding/file_container.hpp"

#include "base/macros.hpp"

#include <algorithm>
#include <cstdint>
#include <functional>
#include <limits>
#include <memory>
#include <type_traits>
#include <utility>
#include <vector>

#include "defines.hpp"

template <typename FeatureSource, typename Element>
class ReadMWMFunctor final
{
public:
  using Fn = std::function<void(Element &)>;

  explicit ReadMWMFunctor(Fn const & fn) : m_fn(fn) {}

  template <typename T>
  std::enable_if_t<std::is_same<T, FeatureID const>::value> ProcessElement(FeatureSource & src,
                                                                           uint32_t index) const
  {
    if (FeatureStatus::Deleted == src.GetFeatureStatus(index))
      return;

    m_fn(src.GetFeatureId(index));
  }

  template <typename T>
  std::enable_if_t<std::is_same<T, FeatureType>::value> ProcessElement(FeatureSource & src,
                                                                     uint32_t index) const
  {
    FeatureType feature;
    switch (src.GetFeatureStatus(index))
    {
    case FeatureStatus::Created: CHECK(false, ("Created features index should be generated."));
    case FeatureStatus::Deleted:
    case FeatureStatus::Obsolete: return;
    case FeatureStatus::Modified:
    {
      VERIFY(src.GetModifiedFeature(index, feature), ());
      break;
    }
    case FeatureStatus::Untouched:
    {
      src.GetOriginalFeature(index, feature);
      break;
    }
    }
    m_fn(feature);
  }

  // Reads features visible at |scale| covered by |cov| from mwm and applies |m_fn| to it.
  // Feature reading process consists of two steps: untouched (original) features reading and
  // touched (created, edited etc.) features reading.
  void operator()(MwmSet::MwmHandle const & handle, covering::CoveringGetter & cov, int scale) const
  {
    FeatureSource src(handle);

    MwmValue const * pValue = handle.GetValue<MwmValue>();
    if (pValue)
    {
      // Untouched (original) features reading. Applies covering |cov| to geometry index, gets
      // feature ids from it, gets untouched features by ids from |src| and applies |m_fn| by
      // ProcessElement.
      feature::DataHeader const & header = pValue->GetHeader();
      CheckUniqueIndexes checkUnique(header.GetFormat() >= version::Format::v5);

      // In case of WorldCoasts we should pass correct scale in ForEachInIntervalAndScale.
      auto const lastScale = header.GetLastScale();
      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> index(pValue->m_cont.GetReader(INDEX_FILE_TAG), pValue->m_factory);

      // iterate through intervals
      for (auto const & i : intervals)
      {
        index.ForEachInIntervalAndScale(i.first, i.second, scale, [&](uint32_t index) {
          if (!checkUnique(index))
            return;
          ProcessElement<Element>(src, index);
        });
      }
    }
    // Check created features container.
    // Need to do it on a per-mwm basis, because Drape relies on features in a sorted order.
    // Touched (created, edited) features reading.
    src.ForEachInRectAndScale(cov.GetRect(), scale, m_fn);
  }

private:
  Fn m_fn;
};

class DataSourceBase : public MwmSet
{
public:
  using FeatureConstCallback = std::function<void(FeatureType const &)>;
  using FeatureCallback = std::function<void(FeatureType &)>;
  using FeatureIdCallback = std::function<void(FeatureID const &)>;

  ~DataSourceBase() override = default;

  /// 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);

  virtual void ForEachFeatureIDInRect(FeatureIdCallback const & f, m2::RectD const & rect,
                                      int scale) const = 0;
  virtual void ForEachInRect(FeatureCallback const & f, m2::RectD const & rect,
                             int scale) const = 0;
  virtual void ForEachInScale(FeatureCallback const & f, int scale) const = 0;
  virtual void ForEachInRectForMWM(FeatureCallback const & f, m2::RectD const & rect, int scale,
                                   MwmId const & id) const = 0;
  // "features" must be sorted using FeatureID::operator< as predicate.
  virtual void ReadFeatures(FeatureConstCallback const & fn,
                            std::vector<FeatureID> const & features) const = 0;

  void ReadFeature(FeatureConstCallback const & fn, FeatureID const & feature) const
  {
    return ReadFeatures(fn, {feature});
  }

protected:
  using ReaderCallback = std::function<void(MwmSet::MwmHandle const & handle,
                                            covering::CoveringGetter & cov, int scale)>;

  void ForEachInIntervals(ReaderCallback const & fn, covering::CoveringMode mode,
                          m2::RectD const & rect, int scale) const;

  /// MwmSet overrides:
  std::unique_ptr<MwmInfo> CreateInfo(platform::LocalCountryFile const & localFile) const override;
  std::unique_ptr<MwmValueBase> CreateValue(MwmInfo & info) const override;
};

template <typename FeatureSource>
class DataSourceImpl : public DataSourceBase
{
public:
  void ForEachFeatureIDInRect(FeatureIdCallback const & f, m2::RectD const & rect,
                              int scale) const override
  {
    ReadMWMFunctor<FeatureSource, FeatureID const> readFunctor(f);
    ForEachInIntervals(readFunctor, covering::LowLevelsOnly, rect, scale);
  }

  void ForEachInRect(FeatureCallback const & f, m2::RectD const & rect, int scale) const override
  {
    ReadMWMFunctor<FeatureSource, FeatureType> readFunctor(f);
    ForEachInIntervals(readFunctor, covering::ViewportWithLowLevels, rect, scale);
  }

  void ForEachInScale(FeatureCallback const & f, int scale) const override
  {
    ReadMWMFunctor<FeatureSource, FeatureType> readFunctor(f);
    ForEachInIntervals(readFunctor, covering::FullCover, m2::RectD::GetInfiniteRect(), scale);
  }

  void ForEachInRectForMWM(FeatureCallback const & f, m2::RectD const & rect, int scale,
                           MwmId const & id) const override
  {
    MwmHandle const handle = GetMwmHandleById(id);
    if (handle.IsAlive())
    {
      covering::CoveringGetter cov(rect, covering::ViewportWithLowLevels);
      ReadMWMFunctor<FeatureSource, FeatureType> readFunctor(f);
      readFunctor(handle, cov, scale);
    }
  }

  void ReadFeatures(FeatureConstCallback const & fn,
                    std::vector<FeatureID> const & features) const override
  {
    ASSERT(std::is_sorted(features.begin(), features.end()), ());

    auto fidIter = features.begin();
    auto const endIter = features.end();
    while (fidIter != endIter)
    {
      MwmId const & id = fidIter->m_mwmId;
      MwmHandle const handle = GetMwmHandleById(id);
      if (handle.IsAlive())
      {
        // Prepare features reading.
        FeatureSource src(handle);
        do
        {
          auto const fts = src.GetFeatureStatus(fidIter->m_index);
          ASSERT_NOT_EQUAL(
              FeatureStatus::Deleted, fts,
              ("Deleted feature was cached. It should not be here. Please review your code."));
          FeatureType featureType;
          if (fts == FeatureStatus::Modified || fts == FeatureStatus::Created)
            VERIFY(src.GetModifiedFeature(fidIter->m_index, featureType), ());
          else
            src.GetOriginalFeature(fidIter->m_index, featureType);
          fn(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(DataSourceBase const & dataSource, MwmId const & id)
      : m_handle(dataSource.GetMwmHandleById(id)), m_source(m_handle)
    {
    }

    MwmSet::MwmId const & GetId() const { return m_handle.GetId(); }

    std::string GetCountryFileName() const
    {
      if (!m_handle.IsAlive())
        return string();

      return m_handle.GetValue<MwmValue>()->GetCountryFileName();
    }

    bool IsWorld() const
    {
      if (!m_handle.IsAlive())
        return false;

      return m_handle.GetValue<MwmValue>()->GetHeader().GetType() == feature::DataHeader::world;
    }

    std::unique_ptr<FeatureType> GetOriginalFeatureByIndex(uint32_t index) const
    {
      auto feature = make_unique<FeatureType>();
      if (GetOriginalFeatureByIndex(index, *feature))
        return feature;

      return {};
    }

    std::unique_ptr<FeatureType> GetOriginalOrEditedFeatureByIndex(uint32_t index) const
    {
      auto feature = make_unique<FeatureType>();
      if (!m_handle.IsAlive())
        return {};

      ASSERT_NOT_EQUAL(m_source.GetFeatureStatus(index), FeatureStatus::Created, ());
      if (GetFeatureByIndex(index, *feature))
        return feature;

      return {};
    }

    /// Everyone, except Editor core, should use this method.
    WARN_UNUSED_RESULT bool GetFeatureByIndex(uint32_t index, FeatureType & ft) const
    {
      if (!m_handle.IsAlive())
        return false;

      ASSERT_NOT_EQUAL(
          FeatureStatus::Deleted, m_source.GetFeatureStatus(index),
          ("Deleted feature was cached. It should not be here. Please review your code."));
      if (m_source.GetModifiedFeature(index, ft))
        return true;
      return GetOriginalFeatureByIndex(index, ft);
    }

    /// Editor core only method, to get 'untouched', original version of feature.
    WARN_UNUSED_RESULT bool GetOriginalFeatureByIndex(uint32_t index, FeatureType & ft) const
    {
      return m_handle.IsAlive() ? m_source.GetOriginalFeature(index, ft) : false;
    }

    size_t GetNumFeatures() const { return m_source.GetNumFeatures(); }

  private:
    MwmHandle m_handle;
    FeatureSource m_source;
  };
};

using DataSource = DataSourceImpl<FeatureSource>;