From 410d775c69483331f9051667d978ccd70259cbb2 Mon Sep 17 00:00:00 2001 From: Vladimir Byko-Ianko Date: Mon, 5 Dec 2016 15:00:09 +0300 Subject: [PATCH] Getting rid of TrafficInfoGetter and using TrafficCache instead. --- routing/edge_estimator.cpp | 8 ++--- routing/edge_estimator.hpp | 4 +-- .../routing_test_tools.cpp | 6 ++-- routing/routing_session.cpp | 8 ++--- routing/routing_session.hpp | 7 ++--- .../routing_tests/applying_traffic_test.cpp | 29 +++++++------------ routing/routing_tests/index_graph_test.cpp | 20 ++++++------- routing/routing_tests/index_graph_tools.cpp | 4 +-- routing/routing_tests/index_graph_tools.hpp | 14 +-------- routing/single_mwm_router.cpp | 2 +- routing/single_mwm_router.hpp | 2 +- traffic/traffic.pro | 4 +-- ...ffic_info_getter.cpp => traffic_cache.cpp} | 4 +-- ...ffic_info_getter.hpp => traffic_cache.hpp} | 13 ++++----- 14 files changed, 50 insertions(+), 75 deletions(-) rename traffic/{traffic_info_getter.cpp => traffic_cache.cpp} (79%) rename traffic/{traffic_info_getter.hpp => traffic_cache.hpp} (67%) diff --git a/routing/edge_estimator.cpp b/routing/edge_estimator.cpp index 1d8cf53d26..e7f7f4a406 100644 --- a/routing/edge_estimator.cpp +++ b/routing/edge_estimator.cpp @@ -32,7 +32,7 @@ inline double TimeBetweenSec(m2::PointD const & from, m2::PointD const & to, dou class CarEdgeEstimator : public EdgeEstimator { public: - CarEdgeEstimator(IVehicleModel const & vehicleModel, traffic::TrafficInfoGetter const & getter); + CarEdgeEstimator(IVehicleModel const & vehicleModel, traffic::TrafficCache const & getter); // EdgeEstimator overrides: void Start(MwmSet::MwmId const & mwmId) override; @@ -42,13 +42,13 @@ public: double CalcHeuristic(m2::PointD const & from, m2::PointD const & to) const override; private: - TrafficInfoGetter const & m_trafficGetter; + TrafficCache const & m_trafficGetter; shared_ptr m_trafficInfo; double const m_maxSpeedMPS; }; CarEdgeEstimator::CarEdgeEstimator(IVehicleModel const & vehicleModel, - traffic::TrafficInfoGetter const & getter) + traffic::TrafficCache const & getter) : m_trafficGetter(getter), m_maxSpeedMPS(vehicleModel.GetMaxSpeed() * kKMPH2MPS) { } @@ -100,7 +100,7 @@ namespace routing { // static shared_ptr EdgeEstimator::CreateForCar(IVehicleModel const & vehicleModel, - traffic::TrafficInfoGetter const & getter) + traffic::TrafficCache const & getter) { return make_shared(vehicleModel, getter); } diff --git a/routing/edge_estimator.hpp b/routing/edge_estimator.hpp index 5dfe5a2801..ac13e4d145 100644 --- a/routing/edge_estimator.hpp +++ b/routing/edge_estimator.hpp @@ -3,7 +3,7 @@ #include "routing/geometry.hpp" #include "routing/vehicle_model.hpp" -#include "traffic/traffic_info_getter.hpp" +#include "traffic/traffic_cache.hpp" #include "indexer/mwm_set.hpp" @@ -27,6 +27,6 @@ public: static shared_ptr CreateForCar(IVehicleModel const & vehicleModel, - traffic::TrafficInfoGetter const & getter); + traffic::TrafficCache const & getter); }; } // namespace routing diff --git a/routing/routing_integration_tests/routing_test_tools.cpp b/routing/routing_integration_tests/routing_test_tools.cpp index 71d3a96f12..d67e4e53a1 100644 --- a/routing/routing_integration_tests/routing_test_tools.cpp +++ b/routing/routing_integration_tests/routing_test_tools.cpp @@ -79,7 +79,7 @@ namespace integration unique_ptr CreateCarRouter(Index & index, storage::CountryInfoGetter const & infoGetter, - TrafficInfoGetterNoJam const & trafficGetter) + traffic::TrafficCache const & trafficGetter) { auto const countryFileGetter = [&infoGetter](m2::PointD const & pt) { return infoGetter.GetRegionCountryId(pt); @@ -109,14 +109,14 @@ namespace integration public: OsrmRouterComponents(vector const & localFiles) : IRouterComponents(localFiles) - , m_carRouter(CreateCarRouter(m_featuresFetcher->GetIndex(), *m_infoGetter, m_trafficGetter)) + , m_carRouter(CreateCarRouter(m_featuresFetcher->GetIndex(), *m_infoGetter, m_trafficCache)) { } IRouter * GetRouter() const override { return m_carRouter.get(); } private: - TrafficInfoGetterNoJam m_trafficGetter; + traffic::TrafficCache m_trafficCache; unique_ptr m_carRouter; }; diff --git a/routing/routing_session.cpp b/routing/routing_session.cpp index a61f85e172..4f3011a87d 100644 --- a/routing/routing_session.cpp +++ b/routing/routing_session.cpp @@ -585,28 +585,28 @@ void RoutingSession::OnTrafficEnabled(bool enable) threads::MutexGuard guard(m_routingSessionMutex); UNUSED_VALUE(guard); if (!enable) - m_trafficCache.Clear(); + Clear(); } void RoutingSession::OnTrafficInfoAdded(TrafficInfo && info) { threads::MutexGuard guard(m_routingSessionMutex); UNUSED_VALUE(guard); - m_trafficCache.Set(move(info)); + Set(move(info)); } void RoutingSession::OnTrafficInfoRemoved(MwmSet::MwmId const & mwmId) { threads::MutexGuard guard(m_routingSessionMutex); UNUSED_VALUE(guard); - m_trafficCache.Remove(mwmId); + Remove(mwmId); } shared_ptr RoutingSession::GetTrafficInfo(MwmSet::MwmId const & mwmId) const { threads::MutexGuard guard(m_routingSessionMutex); UNUSED_VALUE(guard); - return m_trafficCache.Get(mwmId); + return TrafficCache::GetTrafficInfo(mwmId); } string DebugPrint(RoutingSession::State state) diff --git a/routing/routing_session.hpp b/routing/routing_session.hpp index 6e9b2b9a46..d22e9926b0 100644 --- a/routing/routing_session.hpp +++ b/routing/routing_session.hpp @@ -6,8 +6,8 @@ #include "routing/turns.hpp" #include "routing/turns_notification_manager.hpp" +#include "traffic/traffic_cache.hpp" #include "traffic/traffic_info.hpp" -#include "traffic/traffic_info_getter.hpp" #include "platform/location.hpp" #include "platform/measurement_utils.hpp" @@ -39,7 +39,7 @@ struct SpeedCameraRestriction SpeedCameraRestriction() : m_index(0), m_maxSpeedKmH(numeric_limits::max()) {} }; -class RoutingSession : public traffic::TrafficObserver, public traffic::TrafficInfoGetter +class RoutingSession : public traffic::TrafficObserver, public traffic::TrafficCache { friend void UnitTest_TestFollowRoutePercentTest(); @@ -159,7 +159,7 @@ public: void OnTrafficInfoAdded(traffic::TrafficInfo && info) override; void OnTrafficInfoRemoved(MwmSet::MwmId const & mwmId) override; - // TrafficInfoGetter overrides: + // TrafficCache overrides: shared_ptr GetTrafficInfo(MwmSet::MwmId const & mwmId) const override; private: @@ -192,7 +192,6 @@ private: double GetCompletionPercent() const; private: - traffic::TrafficCache m_trafficCache; unique_ptr m_router; shared_ptr m_route; atomic m_state; diff --git a/routing/routing_tests/applying_traffic_test.cpp b/routing/routing_tests/applying_traffic_test.cpp index b58af123a7..52e970858e 100644 --- a/routing/routing_tests/applying_traffic_test.cpp +++ b/routing/routing_tests/applying_traffic_test.cpp @@ -86,43 +86,34 @@ unique_ptr BuildXXGraph(shared_ptr estimator) return graph; } -class TrafficInfoGetterForTesting : public TrafficInfoGetter -{ -public: - // TrafficInfoGetter overrides: - shared_ptr GetTrafficInfo(MwmSet::MwmId const & mwmId) const override - { - return m_trafficCache.Get(mwmId); - } - - void UpdateTrafficInfo(TrafficInfo && trafficInfo) { m_trafficCache.Set(move(trafficInfo)); } - -private: - TrafficCache m_trafficCache; -}; - class ApplyingTrafficTest { public: + class TrafficCacheTest : public TrafficCache + { + public: + void SetTrafficInfo(traffic::TrafficInfo && info) { Set(move(info)); } + }; + ApplyingTrafficTest() { classificator::Load(); } void SetEstimator(TrafficInfo::Coloring && coloring) { - m_trafficGetter = make_unique(); + m_trafficCache = make_unique(); UpdateTrafficInfo(move(coloring)); m_estimator = EdgeEstimator::CreateForCar(*make_shared()->GetVehicleModel(), - *m_trafficGetter); + *m_trafficCache); } shared_ptr GetEstimator() const { return m_estimator; } void UpdateTrafficInfo(TrafficInfo::Coloring && coloring) { - m_trafficGetter->UpdateTrafficInfo(TrafficInfo::BuildForTesting(move(coloring))); + m_trafficCache->SetTrafficInfo(TrafficInfo::BuildForTesting(move(coloring))); } private: - unique_ptr m_trafficGetter; + unique_ptr m_trafficCache; shared_ptr m_estimator; }; diff --git a/routing/routing_tests/index_graph_test.cpp b/routing/routing_tests/index_graph_test.cpp index cf0423556f..9d060f31ad 100644 --- a/routing/routing_tests/index_graph_test.cpp +++ b/routing/routing_tests/index_graph_test.cpp @@ -100,8 +100,8 @@ UNIT_TEST(EdgesTest) loader->AddRoad(4 /* featureId */, true, 1.0 /* speed */, RoadGeometry::Points({{3.0, -1.0}, {3.0, 0.0}, {3.0, 1.0}})); - TrafficInfoGetterNoJam const trafficGetter; - IndexGraph graph(move(loader), CreateEstimator(trafficGetter)); + traffic::TrafficCache const trafficCache; + IndexGraph graph(move(loader), CreateEstimator(trafficCache)); vector joints; joints.emplace_back(MakeJoint({{0, 1}, {3, 1}})); // J0 @@ -145,8 +145,8 @@ UNIT_TEST(FindPathCross) 1 /* featureId */, false, 1.0 /* speed */, RoadGeometry::Points({{0.0, -2.0}, {-1.0, 0.0}, {0.0, 0.0}, {0.0, 1.0}, {0.0, 2.0}})); - TrafficInfoGetterNoJam const trafficGetter; - IndexGraph graph(move(loader), CreateEstimator(trafficGetter)); + traffic::TrafficCache const trafficCache; + IndexGraph graph(move(loader), CreateEstimator(trafficCache)); graph.Import({MakeJoint({{0, 2}, {1, 2}})}); @@ -202,8 +202,8 @@ UNIT_TEST(FindPathManhattan) loader->AddRoad(i + kCitySize, false, 1.0 /* speed */, avenue); } - TrafficInfoGetterNoJam const trafficGetter; - IndexGraph graph(move(loader), CreateEstimator(trafficGetter)); + traffic::TrafficCache const trafficCache; + IndexGraph graph(move(loader), CreateEstimator(trafficCache)); vector joints; for (uint32_t i = 0; i < kCitySize; ++i) @@ -253,8 +253,8 @@ UNIT_TEST(RedressRace) 2 /* featureId */, false, 1.0 /* speed */, RoadGeometry::Points({{0.0, 0.0}, {1.0, 1.0}, {2.0, 1.0}, {3.0, 1.0}, {4.0, 0.0}})); - TrafficInfoGetterNoJam const trafficGetter; - IndexGraph graph(move(loader), CreateEstimator(trafficGetter)); + traffic::TrafficCache const trafficCache; + IndexGraph graph(move(loader), CreateEstimator(trafficCache)); vector joints; joints.emplace_back(MakeJoint({{0, 0}, {1, 0}, {2, 0}})); // J0 @@ -284,8 +284,8 @@ UNIT_TEST(RoadSpeed) loader->AddRoad(1 /* featureId */, false, 1.0 /* speed */, RoadGeometry::Points({{0.0, 0.0}, {2.0, 0.0}, {4.0, 0.0}})); - TrafficInfoGetterNoJam const trafficGetter; - IndexGraph graph(move(loader), CreateEstimator(trafficGetter)); + traffic::TrafficCache const trafficCache; + IndexGraph graph(move(loader), CreateEstimator(trafficCache)); vector joints; joints.emplace_back(MakeJoint({{0, 0}, {1, 0}})); // J0 diff --git a/routing/routing_tests/index_graph_tools.cpp b/routing/routing_tests/index_graph_tools.cpp index 188cd1bea8..390ae86bd6 100644 --- a/routing/routing_tests/index_graph_tools.cpp +++ b/routing/routing_tests/index_graph_tools.cpp @@ -34,9 +34,9 @@ Joint MakeJoint(vector const & points) return joint; } -shared_ptr CreateEstimator(TrafficInfoGetterNoJam const & trafficGetter) +shared_ptr CreateEstimator(traffic::TrafficCache const & trafficCache) { - return EdgeEstimator::CreateForCar(*make_shared()->GetVehicleModel(), trafficGetter); + return EdgeEstimator::CreateForCar(*make_shared()->GetVehicleModel(), trafficCache); } AStarAlgorithm::Result CalculateRoute(IndexGraphStarter & starter, diff --git a/routing/routing_tests/index_graph_tools.hpp b/routing/routing_tests/index_graph_tools.hpp index e504a2b8b8..8cc0f0b94d 100644 --- a/routing/routing_tests/index_graph_tools.hpp +++ b/routing/routing_tests/index_graph_tools.hpp @@ -32,21 +32,9 @@ private: unordered_map m_roads; }; -class TrafficInfoGetterNoJam : public traffic::TrafficInfoGetter -{ -public: - TrafficInfoGetterNoJam() {} - - // TrafficInfoGetter overrides: - shared_ptr GetTrafficInfo(MwmSet::MwmId const &) const override - { - return shared_ptr(); - } -}; - routing::Joint MakeJoint(vector const & points); -shared_ptr CreateEstimator(TrafficInfoGetterNoJam const & trafficGette); +shared_ptr CreateEstimator(traffic::TrafficCache const & trafficCache); routing::AStarAlgorithm::Result CalculateRoute( routing::IndexGraphStarter & graph, vector & roadPoints); diff --git a/routing/single_mwm_router.cpp b/routing/single_mwm_router.cpp index 91a9bf9b14..c93275fe09 100644 --- a/routing/single_mwm_router.cpp +++ b/routing/single_mwm_router.cpp @@ -227,7 +227,7 @@ bool SingleMwmRouter::LoadIndex(MwmSet::MwmId const & mwmId, string const & coun // static unique_ptr SingleMwmRouter::CreateCarRouter( - Index const & index, traffic::TrafficInfoGetter const & getter) + Index const & index, traffic::TrafficCache const & getter) { auto vehicleModelFactory = make_shared(); // @TODO Bicycle turn generation engine is used now. It's ok for the time being. diff --git a/routing/single_mwm_router.hpp b/routing/single_mwm_router.hpp index 90d91ce44d..e351202200 100644 --- a/routing/single_mwm_router.hpp +++ b/routing/single_mwm_router.hpp @@ -33,7 +33,7 @@ public: Route & route); static unique_ptr CreateCarRouter(Index const & index, - traffic::TrafficInfoGetter const & getter); + traffic::TrafficCache const & getter); private: IRouter::ResultCode DoCalculateRoute(MwmSet::MwmId const & mwmId, m2::PointD const & startPoint, diff --git a/traffic/traffic.pro b/traffic/traffic.pro index aaf100a6ad..c02074c080 100644 --- a/traffic/traffic.pro +++ b/traffic/traffic.pro @@ -8,10 +8,10 @@ include($$ROOT_DIR/common.pri) SOURCES += \ speed_groups.cpp \ + traffic_cache.cpp \ traffic_info.cpp \ - traffic_info_getter.cpp \ HEADERS += \ speed_groups.hpp \ + traffic_cache.hpp \ traffic_info.hpp \ - traffic_info_getter.hpp \ diff --git a/traffic/traffic_info_getter.cpp b/traffic/traffic_cache.cpp similarity index 79% rename from traffic/traffic_info_getter.cpp rename to traffic/traffic_cache.cpp index 822d7690d7..ec0373b7e6 100644 --- a/traffic/traffic_info_getter.cpp +++ b/traffic/traffic_cache.cpp @@ -1,4 +1,4 @@ -#include "traffic/traffic_info_getter.hpp" +#include "traffic/traffic_cache.hpp" namespace traffic { @@ -10,7 +10,7 @@ void TrafficCache::Set(TrafficInfo && info) void TrafficCache::Remove(MwmSet::MwmId const & mwmId) { m_trafficInfo.erase(mwmId); } -shared_ptr TrafficCache::Get(MwmSet::MwmId const & mwmId) const +shared_ptr TrafficCache::GetTrafficInfo(MwmSet::MwmId const & mwmId) const { auto it = m_trafficInfo.find(mwmId); diff --git a/traffic/traffic_info_getter.hpp b/traffic/traffic_cache.hpp similarity index 67% rename from traffic/traffic_info_getter.hpp rename to traffic/traffic_cache.hpp index 34933bd900..ba79312a6f 100644 --- a/traffic/traffic_info_getter.hpp +++ b/traffic/traffic_cache.hpp @@ -8,20 +8,17 @@ namespace traffic { -class TrafficInfoGetter +class TrafficCache { public: - virtual ~TrafficInfoGetter() = default; + TrafficCache() : m_trafficInfo() {} + virtual ~TrafficCache() = default; - virtual shared_ptr GetTrafficInfo(MwmSet::MwmId const & mwmId) const = 0; -}; + virtual shared_ptr GetTrafficInfo(MwmSet::MwmId const & mwmId) const; -class TrafficCache final -{ -public: +protected: void Set(traffic::TrafficInfo && info); void Remove(MwmSet::MwmId const & mwmId); - shared_ptr Get(MwmSet::MwmId const & mwmId) const; void Clear(); private: