Optimizing FB schema:

* Replaced Response union with nullable fields
* "Ok" code replaced with a boolean and a separate structure for a error code and message.
* Inlined geometry, thus removing one layer of indirection.
This commit is contained in:
Denis Chaplygin 2019-08-08 11:46:25 +03:00
parent affa8a4653
commit 13287d9970
7 changed files with 335 additions and 605 deletions

View File

@ -6,17 +6,18 @@ include "trip.fbs";
namespace osrm.engine.api.fbresult;
union ServiceResponse {
match: Match,
nearest: Nearest,
route: Route,
table: Table,
trip: Trip
table Error {
code: string;
message: string;
}
table FBResult {
code: string;
message: string;
error: bool = false;
code: Error;
data_version: string;
response: ServiceResponse;
match: Match;
nearest: Nearest;
route: Route;
table: Table;
trip: Trip;
}

File diff suppressed because it is too large Load Diff

View File

@ -70,16 +70,11 @@ table Intersection {
lanes: [Lane];
}
table Geometry {
polyline: string;
polyline6: string;
coordinates: [Position];
}
table Step {
distance: double;
duration: double;
geometry: Geometry;
polyline: string;
coordinates: [Position];
weight: double;
name: string;
ref: string;
@ -109,7 +104,8 @@ table RouteObject {
weight: double;
weight_name: string;
confidence: double; //Used only by 'Match' service
geometry: Geometry;
polyline: string;
coordinates: [Position];
legs: [Leg];
}

View File

@ -48,8 +48,6 @@ class NearestAPI final : public BaseAPI
flatbuffers::FlatBufferBuilder &fb_result) const
{
fbresult::FBResultBuilder response(fb_result);
response.add_code(fb_result.CreateString("Ok"));
response.add_response_type(osrm::engine::api::fbresult::ServiceResponse::ServiceResponse_nearest);
fbresult::NearestBuilder nearest(fb_result);
std::vector<flatbuffers::Offset<fbresult::Waypoint>> waypoints;
@ -72,6 +70,7 @@ class NearestAPI final : public BaseAPI
auto waypoints_vector = fb_result.CreateVector(waypoints);
nearest.add_waypoints(waypoints_vector);
response.add_nearest(nearest.Finish());
fb_result.Finish(response.Finish());
}
void MakeResponse(const std::vector<std::vector<PhantomNodeWithDistance>> &phantom_nodes,

View File

@ -66,8 +66,6 @@ public:
{
fbresult::FBResultBuilder response(fb_result);
response.add_code(fb_result.CreateString("Ok"));
response.add_response_type(osrm::engine::api::fbresult::ServiceResponse::ServiceResponse_route);
fbresult::RouteBuilder route(fb_result);
std::vector<flatbuffers::Offset<fbresult::RouteObject>> routes;
@ -86,7 +84,7 @@ public:
auto routes_vector = fb_result.CreateVector(routes);
route.add_routes(routes_vector);
route.add_waypoints(BaseAPI::MakeWaypoints(fb_result, all_start_end_points));
response.add_response(route.Finish().Union());
response.add_route(route.Finish());
auto data_timestamp = facade.GetTimestamp();
if (!data_timestamp.empty())
@ -127,16 +125,15 @@ public:
}
protected:
template <typename ForwardIter>
flatbuffers::Offset<fbresult::Geometry> MakeGeometry(flatbuffers::FlatBufferBuilder& builder, ForwardIter begin, ForwardIter end) const
template <typename BuilderType, typename ForwardIter>
void MakeGeometry(BuilderType builder, ForwardIter begin, ForwardIter end) const
{
fbresult::GeometryBuilder geometry(builder);
if (parameters.geometries == RouteParameters::GeometriesType::Polyline) {
auto polyline_string = builder.CreateString(encodePolyline<100000>(begin, end));
geometry.add_polyline(polyline_string);
auto polyline_string = builder.fbb_.CreateString(encodePolyline<100000>(begin, end));
builder.add_polyline(polyline_string);
} else if (parameters.geometries == RouteParameters::GeometriesType::Polyline6) {
auto polyline_string = builder.CreateString(encodePolyline<1000000>(begin, end));
geometry.add_polyline6(polyline_string);
auto polyline_string = builder.fbb_.CreateString(encodePolyline<1000000>(begin, end));
builder.add_polyline(polyline_string);
} else {
std::vector<fbresult::Position> coordinates;
coordinates.resize(std::distance(begin, end));
@ -144,10 +141,9 @@ public:
return fbresult::Position{static_cast<double>(util::toFloating(c.lon)),
static_cast<double>(util::toFloating(c.lat))};
});
auto coordinates_vector = builder.CreateVectorOfStructs(coordinates);
geometry.add_coordinates(coordinates_vector);
auto coordinates_vector = builder.fbb_.CreateVectorOfStructs(coordinates);
builder.add_coordinates(coordinates_vector);
}
return geometry.Finish();
}
boost::optional<util::json::Value> MakeGeometry(boost::optional<std::vector<Coordinate>>&& annotations) const
@ -304,8 +300,7 @@ public:
//Fill geometry
auto overview = MakeOverview(leg_geometries);
if(overview) {
auto geometry = MakeGeometry(fb_result, overview->begin(), overview->end());
routeObject.add_geometry(geometry);
MakeGeometry(routeObject, overview->begin(), overview->end());
}
//Fill legs
@ -363,8 +358,7 @@ public:
stepBuilder.add_driving_side(step.is_left_hand_driving);
//Geometry
auto geometry = MakeGeometry(fb_result, leg_geometry.locations.begin() + step.geometry_begin, leg_geometry.locations.begin() + step.geometry_end);
stepBuilder.add_geometry(geometry);
MakeGeometry(stepBuilder, leg_geometry.locations.begin() + step.geometry_begin, leg_geometry.locations.begin() + step.geometry_end);
//Maneuver
fbresult::StepManeuverBuilder maneuver(fb_result);
fbresult::Position maneuverPosition{static_cast<double>(util::toFloating(step.maneuver.location.lon)),

View File

@ -71,8 +71,6 @@ class TableAPI final : public BaseAPI
auto number_of_destinations = parameters.destinations.size();
fbresult::FBResultBuilder response(fb_result);
response.add_code(fb_result.CreateString("Ok"));
response.add_response_type(osrm::engine::api::fbresult::ServiceResponse::ServiceResponse_table);
fbresult::TableBuilder table(fb_result);
@ -113,7 +111,7 @@ class TableAPI final : public BaseAPI
{
table.add_fallback_speed_cells(MakeEstimatesTable(fb_result, fallback_speed_cells));
}
response.add_table(table.Finish());
fb_result.Finish(response.Finish());
}

View File

@ -75,10 +75,13 @@ class BasePlugin
json_result.values["message"] = message;
};
void operator()(flatbuffers::FlatBufferBuilder& fb_result) {
osrm::engine::api::fbresult::FBResultBuilder error(fb_result);
error.add_code(fb_result.CreateString(code));
error.add_message(fb_result.CreateString(message));
error.add_response_type(osrm::engine::api::fbresult::ServiceResponse::ServiceResponse_NONE);
api::fbresult::FBResultBuilder error(fb_result);
error.add_error(true);
api::fbresult::ErrorBuilder codeBuilder(fb_result);
codeBuilder.add_code(fb_result.CreateString(code));
codeBuilder.add_message(fb_result.CreateString(message));
error.add_code(codeBuilder.Finish());
fb_result.Finish(error.Finish());
};
void operator()(std::string& str_result) {