diff --git a/routing/index_graph.cpp b/routing/index_graph.cpp index 8cd879231b..6089d4bf94 100644 --- a/routing/index_graph.cpp +++ b/routing/index_graph.cpp @@ -80,7 +80,11 @@ void IndexGraph::GetEdgeList(Segment const & segment, bool isOutgoing, vector const & joints) { diff --git a/routing/index_graph.hpp b/routing/index_graph.hpp index a20861de94..4155c5caa6 100644 --- a/routing/index_graph.hpp +++ b/routing/index_graph.hpp @@ -65,6 +65,8 @@ public: m_roadIndex.PushFromSerializer(jointId, rp); } + bool IsBuilt() const { return m_isBuilt; } + template void ForEachRoad(F && f) const { @@ -95,5 +97,6 @@ private: JointIndex m_jointIndex; RestrictionVec m_restrictions; RoadAccess m_roadAccess; + bool m_isBuilt = false; }; } // namespace routing diff --git a/routing/index_graph_loader.cpp b/routing/index_graph_loader.cpp index 42fb13f23c..1563c7270f 100644 --- a/routing/index_graph_loader.cpp +++ b/routing/index_graph_loader.cpp @@ -23,11 +23,15 @@ public: shared_ptr estimator, Index & index); // IndexGraphLoader overrides: - virtual IndexGraph & GetIndexGraph(NumMwmId numMwmId) override; - virtual void Clear() override; + Geometry & GetGeometry(NumMwmId numMwmId) override; + IndexGraph & GetIndexGraph(NumMwmId numMwmId) override; + void Clear() override; private: - IndexGraph & Load(NumMwmId mwmId); + /// \brief Constructs IndexGraph without deserializing data and building IndexGraph. + IndexGraph & Init(NumMwmId mwmId); + /// \brief Deserializes data and builds IndexGraph. + IndexGraph & Deserialize(NumMwmId numMwmId, IndexGraph & graph); VehicleType m_vehicleType; bool m_loadAltitudes; @@ -54,16 +58,25 @@ IndexGraphLoaderImpl::IndexGraphLoaderImpl( CHECK(m_estimator, ()); } +Geometry & IndexGraphLoaderImpl::GetGeometry(NumMwmId numMwmId) +{ + auto it = m_graphs.find(numMwmId); + if (it != m_graphs.end()) + return it->second->GetGeometry(); + + return Init(numMwmId).GetGeometry(); +} + IndexGraph & IndexGraphLoaderImpl::GetIndexGraph(NumMwmId numMwmId) { auto it = m_graphs.find(numMwmId); if (it != m_graphs.end()) - return *it->second; + return it->second->IsBuilt() ? *it->second : Deserialize(numMwmId, *it->second); - return Load(numMwmId); + return Deserialize(numMwmId, Init(numMwmId)); } -IndexGraph & IndexGraphLoaderImpl::Load(NumMwmId numMwmId) +IndexGraph & IndexGraphLoaderImpl::Init(NumMwmId numMwmId) { platform::CountryFile const & file = m_numMwmIds->GetFile(numMwmId); MwmSet::MwmHandle handle = m_index.GetMwmHandleByCountryFile(file); @@ -73,15 +86,22 @@ IndexGraph & IndexGraphLoaderImpl::Load(NumMwmId numMwmId) shared_ptr vehicleModel = m_vehicleModelFactory->GetVehicleModelForCountry(file.GetName()); - auto graphPtr = make_unique( - GeometryLoader::Create(m_index, handle, vehicleModel, m_loadAltitudes), - m_estimator); - IndexGraph & graph = *graphPtr; + return *( + m_graphs[numMwmId] = make_unique( + GeometryLoader::Create(m_index, handle, vehicleModel, m_loadAltitudes), m_estimator)); +} + +IndexGraph & IndexGraphLoaderImpl::Deserialize(NumMwmId numMwmId, IndexGraph & graph) +{ + CHECK(!graph.IsBuilt(), ()); + platform::CountryFile const & file = m_numMwmIds->GetFile(numMwmId); + MwmSet::MwmHandle handle = m_index.GetMwmHandleByCountryFile(file); + if (!handle.IsAlive()) + MYTHROW(RoutingException, ("Can't get mwm handle for", file)); my::Timer timer; MwmValue const & mwmValue = *handle.GetValue(); DeserializeIndexGraph(mwmValue, m_vehicleType, graph); - m_graphs[numMwmId] = move(graphPtr); LOG(LINFO, (ROUTING_FILE_TAG, "section for", file.GetName(), "loaded in", timer.ElapsedSeconds(), "seconds")); return graph; diff --git a/routing/index_graph_loader.hpp b/routing/index_graph_loader.hpp index 768cf09476..63d698ed89 100644 --- a/routing/index_graph_loader.hpp +++ b/routing/index_graph_loader.hpp @@ -18,6 +18,7 @@ class IndexGraphLoader public: virtual ~IndexGraphLoader() = default; + virtual Geometry & GetGeometry(NumMwmId numMwmId) = 0; virtual IndexGraph & GetIndexGraph(NumMwmId mwmId) = 0; virtual void Clear() = 0; diff --git a/routing/routing_tests/index_graph_tools.hpp b/routing/routing_tests/index_graph_tools.hpp index 671948d73c..1ca3886a76 100644 --- a/routing/routing_tests/index_graph_tools.hpp +++ b/routing/routing_tests/index_graph_tools.hpp @@ -88,6 +88,7 @@ public: // IndexGraphLoader overrides: ~TestIndexGraphLoader() override = default; + Geometry & GetGeometry(NumMwmId mwmId) override { return GetIndexGraph(mwmId).GetGeometry(); } IndexGraph & GetIndexGraph(NumMwmId mwmId) override; void Clear() override; diff --git a/routing/single_vehicle_world_graph.cpp b/routing/single_vehicle_world_graph.cpp index 60b7a027d8..47552df7ac 100644 --- a/routing/single_vehicle_world_graph.cpp +++ b/routing/single_vehicle_world_graph.cpp @@ -116,7 +116,7 @@ unique_ptr SingleVehicleWorldGraph::GetTransitInfo(Segment const &) RoadGeometry const & SingleVehicleWorldGraph::GetRoadGeometry(NumMwmId mwmId, uint32_t featureId) { - return m_loader->GetIndexGraph(mwmId).GetGeometry().GetRoad(featureId); + return m_loader->GetGeometry(mwmId).GetRoad(featureId); } void SingleVehicleWorldGraph::GetTwinsInner(Segment const & segment, bool isOutgoing,