Collapses Double OSRM <-> Engine <-> .. PImpl Indirection, Resolves #3019.

This commit is contained in:
Daniel J. Hofmann 2016-10-14 15:55:21 +02:00 committed by Daniel J. H
parent 2a2abe9e0f
commit 18bc02f087
10 changed files with 75 additions and 117 deletions

View File

@ -1,71 +1,44 @@
#ifndef ENGINE_HPP #ifndef ENGINE_HPP
#define ENGINE_HPP #define ENGINE_HPP
#include "storage/shared_barriers.hpp"
#include "engine/api/match_parameters.hpp"
#include "engine/api/nearest_parameters.hpp"
#include "engine/api/route_parameters.hpp"
#include "engine/api/table_parameters.hpp"
#include "engine/api/tile_parameters.hpp"
#include "engine/api/trip_parameters.hpp"
#include "engine/data_watchdog.hpp"
#include "engine/datafacade/datafacade_base.hpp"
#include "engine/engine_config.hpp"
#include "engine/plugins/match.hpp"
#include "engine/plugins/nearest.hpp"
#include "engine/plugins/table.hpp"
#include "engine/plugins/tile.hpp"
#include "engine/plugins/trip.hpp"
#include "engine/plugins/viaroute.hpp"
#include "engine/status.hpp" #include "engine/status.hpp"
#include "util/json_container.hpp" #include "util/json_container.hpp"
#include <memory> #include <memory>
#include <mutex> #include <mutex>
#include <string> #include <string>
#include <unordered_map>
namespace osrm namespace osrm
{ {
namespace util
{
namespace json
{
struct Object;
}
}
namespace storage
{
struct SharedBarriers;
}
// Fwd decls
namespace engine namespace engine
{ {
struct EngineConfig;
namespace api
{
struct RouteParameters;
struct TableParameters;
struct NearestParameters;
struct TripParameters;
struct MatchParameters;
struct TileParameters;
}
namespace plugins
{
class ViaRoutePlugin;
class TablePlugin;
class NearestPlugin;
class TripPlugin;
class MatchPlugin;
class TilePlugin;
}
// End fwd decls
namespace datafacade
{
class BaseDataFacade;
}
class DataWatchdog;
class Engine final class Engine final
{ {
public: public:
explicit Engine(const EngineConfig &config); explicit Engine(const EngineConfig &config);
Engine(Engine &&) noexcept; Engine(Engine &&) noexcept = delete;
Engine &operator=(Engine &&) noexcept; Engine &operator=(Engine &&) noexcept = delete;
// Impl. in cpp since for unique_ptr of incomplete types Engine(const Engine &) = delete;
~Engine(); Engine &operator=(const Engine &) = delete;
Status Route(const api::RouteParameters &parameters, util::json::Object &result) const; Status Route(const api::RouteParameters &parameters, util::json::Object &result) const;
Status Table(const api::TableParameters &parameters, util::json::Object &result) const; Status Table(const api::TableParameters &parameters, util::json::Object &result) const;
@ -78,12 +51,12 @@ class Engine final
std::unique_ptr<storage::SharedBarriers> lock; std::unique_ptr<storage::SharedBarriers> lock;
std::unique_ptr<DataWatchdog> watchdog; std::unique_ptr<DataWatchdog> watchdog;
std::unique_ptr<plugins::ViaRoutePlugin> route_plugin; const plugins::ViaRoutePlugin route_plugin;
std::unique_ptr<plugins::TablePlugin> table_plugin; const plugins::TablePlugin table_plugin;
std::unique_ptr<plugins::NearestPlugin> nearest_plugin; const plugins::NearestPlugin nearest_plugin;
std::unique_ptr<plugins::TripPlugin> trip_plugin; const plugins::TripPlugin trip_plugin;
std::unique_ptr<plugins::MatchPlugin> match_plugin; const plugins::MatchPlugin match_plugin;
std::unique_ptr<plugins::TilePlugin> tile_plugin; const plugins::TilePlugin tile_plugin;
// note in case of shared memory this will be empty, since the watchdog // note in case of shared memory this will be empty, since the watchdog
// will provide us with the up-to-date facade // will provide us with the up-to-date facade

View File

@ -35,13 +35,13 @@ class MatchPlugin : public BasePlugin
Status HandleRequest(const std::shared_ptr<datafacade::BaseDataFacade> facade, Status HandleRequest(const std::shared_ptr<datafacade::BaseDataFacade> facade,
const api::MatchParameters &parameters, const api::MatchParameters &parameters,
util::json::Object &json_result); util::json::Object &json_result) const;
private: private:
SearchEngineData heaps; mutable SearchEngineData heaps;
routing_algorithms::MapMatching<datafacade::BaseDataFacade> map_matching; mutable routing_algorithms::MapMatching<datafacade::BaseDataFacade> map_matching;
routing_algorithms::ShortestPathRouting<datafacade::BaseDataFacade> shortest_path; mutable routing_algorithms::ShortestPathRouting<datafacade::BaseDataFacade> shortest_path;
int max_locations_map_matching; const int max_locations_map_matching;
}; };
} }
} }

View File

@ -22,12 +22,12 @@ class TablePlugin final : public BasePlugin
Status HandleRequest(const std::shared_ptr<datafacade::BaseDataFacade> facade, Status HandleRequest(const std::shared_ptr<datafacade::BaseDataFacade> facade,
const api::TableParameters &params, const api::TableParameters &params,
util::json::Object &result); util::json::Object &result) const;
private: private:
SearchEngineData heaps; mutable SearchEngineData heaps;
routing_algorithms::ManyToManyRouting<datafacade::BaseDataFacade> distance_table; mutable routing_algorithms::ManyToManyRouting<datafacade::BaseDataFacade> distance_table;
int max_locations_distance_table; const int max_locations_distance_table;
}; };
} }
} }

View File

@ -29,14 +29,14 @@ namespace plugins
class TripPlugin final : public BasePlugin class TripPlugin final : public BasePlugin
{ {
private: private:
SearchEngineData heaps; mutable SearchEngineData heaps;
routing_algorithms::ShortestPathRouting<datafacade::BaseDataFacade> shortest_path; mutable routing_algorithms::ShortestPathRouting<datafacade::BaseDataFacade> shortest_path;
routing_algorithms::ManyToManyRouting<datafacade::BaseDataFacade> duration_table; mutable routing_algorithms::ManyToManyRouting<datafacade::BaseDataFacade> duration_table;
int max_locations_trip; const int max_locations_trip;
InternalRouteResult ComputeRoute(const datafacade::BaseDataFacade &facade, InternalRouteResult ComputeRoute(const datafacade::BaseDataFacade &facade,
const std::vector<PhantomNode> &phantom_node_list, const std::vector<PhantomNode> &phantom_node_list,
const std::vector<NodeID> &trip); const std::vector<NodeID> &trip) const;
public: public:
explicit TripPlugin(const int max_locations_trip_) explicit TripPlugin(const int max_locations_trip_)
@ -46,7 +46,7 @@ class TripPlugin final : public BasePlugin
Status HandleRequest(const std::shared_ptr<datafacade::BaseDataFacade> facade, Status HandleRequest(const std::shared_ptr<datafacade::BaseDataFacade> facade,
const api::TripParameters &parameters, const api::TripParameters &parameters,
util::json::Object &json_result); util::json::Object &json_result) const;
}; };
} }
} }

View File

@ -28,18 +28,18 @@ namespace plugins
class ViaRoutePlugin final : public BasePlugin class ViaRoutePlugin final : public BasePlugin
{ {
private: private:
SearchEngineData heaps; mutable SearchEngineData heaps;
routing_algorithms::ShortestPathRouting<datafacade::BaseDataFacade> shortest_path; mutable routing_algorithms::ShortestPathRouting<datafacade::BaseDataFacade> shortest_path;
routing_algorithms::AlternativeRouting<datafacade::BaseDataFacade> alternative_path; mutable routing_algorithms::AlternativeRouting<datafacade::BaseDataFacade> alternative_path;
routing_algorithms::DirectShortestPathRouting<datafacade::BaseDataFacade> direct_shortest_path; mutable routing_algorithms::DirectShortestPathRouting<datafacade::BaseDataFacade> direct_shortest_path;
int max_locations_viaroute; const int max_locations_viaroute;
public: public:
explicit ViaRoutePlugin(int max_locations_viaroute); explicit ViaRoutePlugin(int max_locations_viaroute);
Status HandleRequest(const std::shared_ptr<datafacade::BaseDataFacade> facade, Status HandleRequest(const std::shared_ptr<datafacade::BaseDataFacade> facade,
const api::RouteParameters &route_parameters, const api::RouteParameters &route_parameters,
util::json::Object &json_result); util::json::Object &json_result) const;
}; };
} }
} }

View File

@ -1,17 +1,8 @@
#include "engine/api/route_parameters.hpp"
#include "engine/data_watchdog.hpp"
#include "engine/engine.hpp" #include "engine/engine.hpp"
#include "engine/api/route_parameters.hpp"
#include "engine/engine_config.hpp" #include "engine/engine_config.hpp"
#include "engine/status.hpp" #include "engine/status.hpp"
#include "engine/plugins/match.hpp"
#include "engine/plugins/nearest.hpp"
#include "engine/plugins/table.hpp"
#include "engine/plugins/tile.hpp"
#include "engine/plugins/trip.hpp"
#include "engine/plugins/viaroute.hpp"
#include "engine/datafacade/datafacade_base.hpp"
#include "engine/datafacade/internal_datafacade.hpp" #include "engine/datafacade/internal_datafacade.hpp"
#include "engine/datafacade/shared_datafacade.hpp" #include "engine/datafacade/shared_datafacade.hpp"
@ -34,11 +25,12 @@ namespace
// Abstracted away the query locking into a template function // Abstracted away the query locking into a template function
// Works the same for every plugin. // Works the same for every plugin.
template <typename ParameterT, typename PluginT, typename ResultT> template <typename ParameterT, typename PluginT, typename ResultT>
osrm::engine::Status RunQuery(const std::unique_ptr<osrm::engine::DataWatchdog> &watchdog, osrm::engine::Status
const std::shared_ptr<osrm::engine::datafacade::BaseDataFacade> &facade, RunQuery(const std::unique_ptr<osrm::engine::DataWatchdog> &watchdog,
const ParameterT &parameters, const std::shared_ptr<osrm::engine::datafacade::BaseDataFacade> &facade,
PluginT &plugin, const ParameterT &parameters,
ResultT &result) PluginT &plugin,
ResultT &result)
{ {
if (watchdog) if (watchdog)
{ {
@ -62,7 +54,14 @@ namespace engine
Engine::Engine(const EngineConfig &config) Engine::Engine(const EngineConfig &config)
: lock(config.use_shared_memory ? std::make_unique<storage::SharedBarriers>() : lock(config.use_shared_memory ? std::make_unique<storage::SharedBarriers>()
: std::unique_ptr<storage::SharedBarriers>()) : std::unique_ptr<storage::SharedBarriers>()),
route_plugin(config.max_locations_viaroute), //
table_plugin(config.max_locations_distance_table), //
nearest_plugin(config.max_results_nearest), //
trip_plugin(config.max_locations_trip), //
match_plugin(config.max_locations_map_matching), //
tile_plugin() //
{ {
if (config.use_shared_memory) if (config.use_shared_memory)
{ {
@ -81,53 +80,39 @@ Engine::Engine(const EngineConfig &config)
{ {
throw util::exception("Invalid file paths given!"); throw util::exception("Invalid file paths given!");
} }
immutable_data_facade = std::make_shared<datafacade::InternalDataFacade>(config.storage_config); immutable_data_facade =
std::make_shared<datafacade::InternalDataFacade>(config.storage_config);
} }
// Register plugins
using namespace plugins;
route_plugin = std::make_unique<ViaRoutePlugin>(config.max_locations_viaroute);
table_plugin = std::make_unique<TablePlugin>(config.max_locations_distance_table);
nearest_plugin = std::make_unique<NearestPlugin>(config.max_results_nearest);
trip_plugin = std::make_unique<TripPlugin>(config.max_locations_trip);
match_plugin = std::make_unique<MatchPlugin>(config.max_locations_map_matching);
tile_plugin = std::make_unique<TilePlugin>();
} }
// make sure we deallocate the unique ptr at a position where we know the size of the plugins
Engine::~Engine() = default;
Engine::Engine(Engine &&) noexcept = default;
Engine &Engine::operator=(Engine &&) noexcept = default;
Status Engine::Route(const api::RouteParameters &params, util::json::Object &result) const Status Engine::Route(const api::RouteParameters &params, util::json::Object &result) const
{ {
return RunQuery(watchdog, immutable_data_facade, params, *route_plugin, result); return RunQuery(watchdog, immutable_data_facade, params, route_plugin, result);
} }
Status Engine::Table(const api::TableParameters &params, util::json::Object &result) const Status Engine::Table(const api::TableParameters &params, util::json::Object &result) const
{ {
return RunQuery(watchdog, immutable_data_facade, params, *table_plugin, result); return RunQuery(watchdog, immutable_data_facade, params, table_plugin, result);
} }
Status Engine::Nearest(const api::NearestParameters &params, util::json::Object &result) const Status Engine::Nearest(const api::NearestParameters &params, util::json::Object &result) const
{ {
return RunQuery(watchdog, immutable_data_facade, params, *nearest_plugin, result); return RunQuery(watchdog, immutable_data_facade, params, nearest_plugin, result);
} }
Status Engine::Trip(const api::TripParameters &params, util::json::Object &result) const Status Engine::Trip(const api::TripParameters &params, util::json::Object &result) const
{ {
return RunQuery(watchdog, immutable_data_facade, params, *trip_plugin, result); return RunQuery(watchdog, immutable_data_facade, params, trip_plugin, result);
} }
Status Engine::Match(const api::MatchParameters &params, util::json::Object &result) const Status Engine::Match(const api::MatchParameters &params, util::json::Object &result) const
{ {
return RunQuery(watchdog, immutable_data_facade, params, *match_plugin, result); return RunQuery(watchdog, immutable_data_facade, params, match_plugin, result);
} }
Status Engine::Tile(const api::TileParameters &params, std::string &result) const Status Engine::Tile(const api::TileParameters &params, std::string &result) const
{ {
return RunQuery(watchdog, immutable_data_facade, params, *tile_plugin, result); return RunQuery(watchdog, immutable_data_facade, params, tile_plugin, result);
} }
} // engine ns } // engine ns

View File

@ -107,7 +107,7 @@ void filterCandidates(const std::vector<util::Coordinate> &coordinates,
Status MatchPlugin::HandleRequest(const std::shared_ptr<datafacade::BaseDataFacade> facade, Status MatchPlugin::HandleRequest(const std::shared_ptr<datafacade::BaseDataFacade> facade,
const api::MatchParameters &parameters, const api::MatchParameters &parameters,
util::json::Object &json_result) util::json::Object &json_result) const
{ {
BOOST_ASSERT(parameters.IsValid()); BOOST_ASSERT(parameters.IsValid());

View File

@ -30,7 +30,7 @@ TablePlugin::TablePlugin(const int max_locations_distance_table)
Status TablePlugin::HandleRequest(const std::shared_ptr<datafacade::BaseDataFacade> facade, Status TablePlugin::HandleRequest(const std::shared_ptr<datafacade::BaseDataFacade> facade,
const api::TableParameters &params, const api::TableParameters &params,
util::json::Object &result) util::json::Object &result) const
{ {
BOOST_ASSERT(params.IsValid()); BOOST_ASSERT(params.IsValid());

View File

@ -116,7 +116,7 @@ SCC_Component SplitUnaccessibleLocations(const std::size_t number_of_locations,
InternalRouteResult TripPlugin::ComputeRoute(const datafacade::BaseDataFacade &facade, InternalRouteResult TripPlugin::ComputeRoute(const datafacade::BaseDataFacade &facade,
const std::vector<PhantomNode> &snapped_phantoms, const std::vector<PhantomNode> &snapped_phantoms,
const std::vector<NodeID> &trip) const std::vector<NodeID> &trip) const
{ {
InternalRouteResult min_route; InternalRouteResult min_route;
// given he final trip, compute total duration and return the route and location permutation // given he final trip, compute total duration and return the route and location permutation
@ -143,7 +143,7 @@ InternalRouteResult TripPlugin::ComputeRoute(const datafacade::BaseDataFacade &f
Status TripPlugin::HandleRequest(const std::shared_ptr<datafacade::BaseDataFacade> facade, Status TripPlugin::HandleRequest(const std::shared_ptr<datafacade::BaseDataFacade> facade,
const api::TripParameters &parameters, const api::TripParameters &parameters,
util::json::Object &json_result) util::json::Object &json_result) const
{ {
BOOST_ASSERT(parameters.IsValid()); BOOST_ASSERT(parameters.IsValid());

View File

@ -29,7 +29,7 @@ ViaRoutePlugin::ViaRoutePlugin(int max_locations_viaroute)
Status ViaRoutePlugin::HandleRequest(const std::shared_ptr<datafacade::BaseDataFacade> facade, Status ViaRoutePlugin::HandleRequest(const std::shared_ptr<datafacade::BaseDataFacade> facade,
const api::RouteParameters &route_parameters, const api::RouteParameters &route_parameters,
util::json::Object &json_result) util::json::Object &json_result) const
{ {
BOOST_ASSERT(route_parameters.IsValid()); BOOST_ASSERT(route_parameters.IsValid());