Skip to content

Commit

Permalink
Don't use location cache if not needed
Browse files Browse the repository at this point in the history
  • Loading branch information
oxidase committed Oct 4, 2017
1 parent 476bc34 commit 545097c
Show file tree
Hide file tree
Showing 8 changed files with 50 additions and 14 deletions.
40 changes: 30 additions & 10 deletions features/options/extract/lua.feature
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,14 @@ Feature: osrm-extract lua ways:get_nodes()
"""
functions = require('testbot')
function way_function(profile, way, result)
functions.process_way = function(profile, way, result)
for _, node in ipairs(way:get_nodes()) do
print('node id ' .. node:id())
end
result.forward_mode = mode.driving
result.forward_speed = 1
end
functions.process_way = way_function
return functions
"""
And the node map
Expand All @@ -32,20 +31,43 @@ Feature: osrm-extract lua ways:get_nodes()
And stdout should contain "node id 2"


Scenario: osrm-extract location-dependent data without add-locations-to-ways preprocessing and node locations cache
Given the profile file
"""
functions = require('testbot')
functions.process_way = function(profile, way, result, relations)
print(way:get_location_tag('driving_side'))
end
return functions
"""
And the node map
"""
a b
"""
And the ways
| nodes |
| ab |
And the data has been saved to disk

When I try to run "osrm-extract --profile {profile_file} {osm_file} --location-dependent-data test/data/regions/null-island.geojson --use-locations-cache=false"
Then it should exit with an error
And stderr should contain "invalid location"

Scenario: osrm-extract location-dependent data
Given the profile file
"""
functions = require('testbot')
function way_function(profile, way, result, relations)
functions.process_way = function(profile, way, result, relations)
for _, key in ipairs({'answer', 'boolean', 'object', 'array'}) do
print (key .. ' ' .. tostring(way:get_location_tag(key)))
end
result.forward_mode = mode.driving
result.forward_speed = 1
end
functions.process_way = way_function
return functions
"""
And the node map
Expand All @@ -57,7 +79,7 @@ Feature: osrm-extract lua ways:get_nodes()
| ab |
And the data has been saved to disk

When I run "osrm-extract --profile {profile_file} {osm_file} --location-dependent-data test/data/regions/null-island.geojson"
When I run "osrm-extract --profile {profile_file} {osm_file} --location-dependent-data test/data/regions/null-island.geojson --use-locations-cache=false"
Then it should exit successfully
And stdout should contain "answer 42"
And stdout should contain "boolean true"
Expand All @@ -70,14 +92,13 @@ Feature: osrm-extract lua ways:get_nodes()
"""
functions = require('testbot')
function way_function(profile, way, result, relations)
functions.process_way = function(profile, way, result, relations)
print('ISO3166-1 ' .. (way:get_location_tag('ISO3166-1') or 'none'))
print('answer ' .. (way:get_location_tag('answer') or 'none'))
result.forward_mode = mode.driving
result.forward_speed = 1
end
functions.process_way = way_function
return functions
"""
And the node locations
Expand All @@ -95,7 +116,7 @@ Feature: osrm-extract lua ways:get_nodes()
| ef | Null Island |
And the data has been saved to disk

When I run "osrm-extract --profile {profile_file} {osm_file} --location-dependent-data test/data/regions/null-island.geojson --location-dependent-data test/data/regions/hong-kong.geojson"
When I run "osrm-extract --profile {profile_file} {osm_file} --location-dependent-data test/data/regions/null-island.geojson --location-dependent-data test/data/regions/hong-kong.geojson --use-locations-cache=false"
Then it should exit successfully
And stdout should not contain "1 GeoJSON polygon"
And stdout should contain "2 GeoJSON polygons"
Expand All @@ -108,13 +129,12 @@ Feature: osrm-extract lua ways:get_nodes()
"""
functions = require('testbot')
function way_function(profile, way, result, relations)
functions.process_way = function(profile, way, result, relations)
print ('answer ' .. tostring(way:get_location_tag('answer')))
result.forward_mode = mode.driving
result.forward_speed = 1
end
functions.process_way = way_function
return functions
"""
And the node map
Expand Down
4 changes: 3 additions & 1 deletion include/extractor/extractor_config.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,8 @@ struct ExtractorConfig final : storage::IOConfig
".osrm.icd",
".osrm.cnbg",
".osrm.cnbg_to_ebg"}),
requested_num_threads(0)
requested_num_threads(0),
use_locations_cache(true)
{
}

Expand All @@ -88,6 +89,7 @@ struct ExtractorConfig final : storage::IOConfig

bool use_metadata;
bool parse_conditionals;
bool use_locations_cache;
};
}
}
Expand Down
2 changes: 2 additions & 0 deletions include/extractor/scripting_environment.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ class ScriptingEnvironment
std::vector<std::pair<const osmium::Way &, ExtractionWay>> &resulting_ways,
std::vector<std::pair<const osmium::Relation &, ExtractionRelation>> &resulting_relations,
std::vector<InputConditionalTurnRestriction> &resulting_restrictions) = 0;

virtual bool HasLocationDependentData() const = 0;
};
}
}
Expand Down
2 changes: 2 additions & 0 deletions include/extractor/scripting_environment_lua.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,8 @@ class Sol2ScriptingEnvironment final : public ScriptingEnvironment
std::vector<std::pair<const osmium::Relation &, ExtractionRelation>> &resulting_relations,
std::vector<InputConditionalTurnRestriction> &resulting_restrictions) override;

bool HasLocationDependentData() const { return !location_dependent_data.empty(); }

private:
LuaScriptingContext &GetSol2Context();

Expand Down
5 changes: 3 additions & 2 deletions src/extractor/extractor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -453,9 +453,10 @@ Extractor::ParseOSMData(ScriptingEnvironment &scripting_environment,
osmium::io::Reader reader(
input_file, osmium::osm_entity_bits::node | osmium::osm_entity_bits::way, read_meta);

// TODO: make location_cacher conditional
const auto pipeline =
buffer_reader(reader) & location_cacher & buffer_transformer & buffer_storage;
scripting_environment.HasLocationDependentData() && config.use_locations_cache
? buffer_reader(reader) & location_cacher & buffer_transformer & buffer_storage
: buffer_reader(reader) & buffer_transformer & buffer_storage;
tbb::parallel_pipeline(num_threads, pipeline);
}

Expand Down
2 changes: 2 additions & 0 deletions src/extractor/scripting_environment_lua.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,8 @@ void Sol2ScriptingEnvironment::InitContext(LuaScriptingContext &context)
[](const osmium::Way &way) { return sol::as_table(way.nodes()); },
"get_location_tag",
[&context](const osmium::Way &way, const char *key) {
if (context.location_dependent_data.empty())
return sol::object(sol::nil);
// HEURISTIC: use a single node (last) of the way to localize the way
// For more complicated scenarios a proper merging of multiple tags
// at one or many locations must be provided
Expand Down
7 changes: 6 additions & 1 deletion src/tools/extract.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,12 @@ return_code parseArguments(int argc,
boost::program_options::value<std::vector<boost::filesystem::path>>(
&extractor_config.location_dependent_data_paths)
->composing(),
"GeoJSON files with location-dependent data");
"GeoJSON files with location-dependent data")(
"use-locations-cache",
boost::program_options::value<bool>(&extractor_config.use_locations_cache)
->implicit_value(true)
->default_value(extractor_config.use_locations_cache),
"Use internal nodes locations cache for location-dependent data lookups");

bool dummy;
// hidden options, will be allowed on command line, but will not be
Expand Down
2 changes: 2 additions & 0 deletions unit_tests/mocks/mock_scripting_environment.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ class MockScriptingEnvironment : public extractor::ScriptingEnvironment
std::vector<extractor::InputConditionalTurnRestriction> &) override final
{
}

bool HasLocationDependentData() const { return false; };
};

} // namespace test
Expand Down

0 comments on commit 545097c

Please sign in to comment.