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

transit_graph_loader.cpp « routing - github.com/mapsme/omim.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: c55820f019ca310a3ee5f8f6668283d4fdac6418 (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
#include "routing/transit_graph_loader.hpp"

#include "routing/fake_ending.hpp"
#include "routing/routing_exceptions.hpp"

#include "transit/transit_graph_data.hpp"
#include "transit/transit_serdes.hpp"
#include "transit/transit_types.hpp"

#include "indexer/data_source.hpp"
#include "indexer/mwm_set.hpp"

#include "platform/country_file.hpp"

#include "coding/file_container.hpp"

#include "base/stl_add.hpp"
#include "base/timer.hpp"

#include <unordered_map>
#include <vector>

using namespace std;

namespace routing
{
class TransitGraphLoaderImpl : public TransitGraphLoader
{
public:
  TransitGraphLoaderImpl(DataSourceBase & dataSource, shared_ptr<NumMwmIds> numMwmIds,
                         shared_ptr<EdgeEstimator> estimator);

  // TransitGraphLoader overrides.
  ~TransitGraphLoaderImpl() override = default;

  TransitGraph & GetTransitGraph(NumMwmId mwmId, IndexGraph & indexGraph) override;
  void Clear() override;

private:
  unique_ptr<TransitGraph> CreateTransitGraph(NumMwmId mwmId, IndexGraph & indexGraph) const;

  DataSourceBase & m_dataSource;
  shared_ptr<NumMwmIds> m_numMwmIds;
  shared_ptr<EdgeEstimator> m_estimator;
  unordered_map<NumMwmId, unique_ptr<TransitGraph>> m_graphs;
};

TransitGraphLoaderImpl::TransitGraphLoaderImpl(DataSourceBase & dataSource, shared_ptr<NumMwmIds> numMwmIds,
                                               shared_ptr<EdgeEstimator> estimator)
  : m_dataSource(dataSource), m_numMwmIds(numMwmIds), m_estimator(estimator)
{
}

void TransitGraphLoaderImpl::Clear() { m_graphs.clear(); }

TransitGraph & TransitGraphLoaderImpl::GetTransitGraph(NumMwmId numMwmId, IndexGraph & indexGraph)
{
  auto const it = m_graphs.find(numMwmId);
  if (it != m_graphs.cend())
    return *it->second;

  auto const emplaceRes = m_graphs.emplace(numMwmId, CreateTransitGraph(numMwmId, indexGraph));
  ASSERT(emplaceRes.second, ("Failed to add TransitGraph for", numMwmId, "to TransitGraphLoader."));
  return *(emplaceRes.first)->second;
}

unique_ptr<TransitGraph> TransitGraphLoaderImpl::CreateTransitGraph(NumMwmId numMwmId,
                                                                    IndexGraph & indexGraph) const
{
  platform::CountryFile const & file = m_numMwmIds->GetFile(numMwmId);
  MwmSet::MwmHandle handle = m_dataSource.GetMwmHandleByCountryFile(file);
  if (!handle.IsAlive())
    MYTHROW(RoutingException, ("Can't get mwm handle for", file));

  my::Timer timer;
  auto graph = my::make_unique<TransitGraph>(numMwmId, m_estimator);
  MwmValue const & mwmValue = *handle.GetValue<MwmValue>();
  if (!mwmValue.m_cont.IsExist(TRANSIT_FILE_TAG))
    return graph;

  try
  {
    FilesContainerR::TReader reader(mwmValue.m_cont.GetReader(TRANSIT_FILE_TAG));
    transit::GraphData transitData;
    transitData.DeserializeForRouting(*reader.GetPtr());

    TransitGraph::GateEndings gateEndings;
    MakeGateEndings(transitData.GetGates(), numMwmId, indexGraph, gateEndings);

    graph->Fill(transitData, gateEndings);
  }
  catch (Reader::OpenException const & e)
  {
    LOG(LERROR, ("Error while reading", TRANSIT_FILE_TAG, "section.", e.Msg()));
    throw;
  }

  LOG(LINFO, (TRANSIT_FILE_TAG, "section for", file.GetName(), "loaded in",
              timer.ElapsedSeconds(), "seconds"));
  return graph;
}

// static
unique_ptr<TransitGraphLoader> TransitGraphLoader::Create(DataSourceBase & dataSource,
                                                          shared_ptr<NumMwmIds> numMwmIds,
                                                          shared_ptr<EdgeEstimator> estimator)
{
  return my::make_unique<TransitGraphLoaderImpl>(dataSource, numMwmIds, estimator);
}

}  // namespace routing