Skip to content

Commit 3537f17

Browse files
committed
Add after_nodes/ways/relations() callback functions in Lua
1 parent f9c1650 commit 3537f17

File tree

3 files changed

+107
-5
lines changed

3 files changed

+107
-5
lines changed

src/output-flex.cpp

Lines changed: 49 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -804,6 +804,15 @@ int output_flex_t::expire_output_table()
804804
return 1;
805805
}
806806

807+
void output_flex_t::call_lua_function(prepared_lua_function_t func)
808+
{
809+
lua_pushvalue(lua_state(), func.index());
810+
if (luaX_pcall(lua_state(), 0, func.nresults())) {
811+
throw fmt_error("Failed to execute Lua function 'osm2pgsql.{}': {}.",
812+
func.name(), lua_tostring(lua_state(), -1));
813+
}
814+
}
815+
807816
void output_flex_t::call_lua_function(prepared_lua_function_t func,
808817
osmium::OSMObject const &object)
809818
{
@@ -823,6 +832,13 @@ void output_flex_t::call_lua_function(prepared_lua_function_t func,
823832
m_calling_context = calling_context::main;
824833
}
825834

835+
void output_flex_t::get_mutex_and_call_lua_function(
836+
prepared_lua_function_t func)
837+
{
838+
std::lock_guard<std::mutex> const guard{lua_mutex};
839+
call_lua_function(func);
840+
}
841+
826842
void output_flex_t::get_mutex_and_call_lua_function(
827843
prepared_lua_function_t func, osmium::OSMObject const &object)
828844
{
@@ -960,11 +976,32 @@ void output_flex_t::sync()
960976
}
961977
}
962978

963-
void output_flex_t::after_nodes() { flush_tables(m_table_connections); }
979+
void output_flex_t::after_nodes()
980+
{
981+
if (m_after_nodes) {
982+
get_mutex_and_call_lua_function(m_after_nodes);
983+
}
984+
985+
flush_tables(m_table_connections);
986+
}
964987

965-
void output_flex_t::after_ways() { flush_tables(m_table_connections); }
988+
void output_flex_t::after_ways()
989+
{
990+
if (m_after_ways) {
991+
get_mutex_and_call_lua_function(m_after_ways);
992+
}
966993

967-
void output_flex_t::after_relations() { flush_tables(m_table_connections); }
994+
flush_tables(m_table_connections);
995+
}
996+
997+
void output_flex_t::after_relations()
998+
{
999+
if (m_after_relations) {
1000+
get_mutex_and_call_lua_function(m_after_relations);
1001+
}
1002+
1003+
flush_tables(m_table_connections);
1004+
}
9681005

9691006
void output_flex_t::stop()
9701007
{
@@ -1138,7 +1175,9 @@ output_flex_t::output_flex_t(output_flex_t const *other,
11381175
m_copy_thread(std::move(copy_thread)), m_lua_state(other->m_lua_state),
11391176
m_process_node(other->m_process_node), m_process_way(other->m_process_way),
11401177
m_process_relation(other->m_process_relation),
1141-
m_select_relation_members(other->m_select_relation_members)
1178+
m_select_relation_members(other->m_select_relation_members),
1179+
m_after_nodes(other->m_after_nodes), m_after_ways(other->m_after_ways),
1180+
m_after_relations(other->m_after_relations)
11421181
{
11431182
for (auto &table : *m_tables) {
11441183
table.prepare(m_db_connection);
@@ -1369,6 +1408,12 @@ void output_flex_t::init_lua(std::string const &filename,
13691408
m_select_relation_members = prepared_lua_function_t{
13701409
lua_state(), calling_context::select_relation_members,
13711410
"select_relation_members", 1};
1411+
m_after_nodes = prepared_lua_function_t{lua_state(), calling_context::main,
1412+
"after_nodes"};
1413+
m_after_ways = prepared_lua_function_t{lua_state(), calling_context::main,
1414+
"after_ways"};
1415+
m_after_relations = prepared_lua_function_t{
1416+
lua_state(), calling_context::main, "after_relations"};
13721417

13731418
lua_remove(lua_state(), 1); // global "osm2pgsql"
13741419
}

src/output-flex.hpp

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,14 +181,19 @@ class output_flex_t : public output_t
181181
private:
182182
void select_relation_members();
183183

184+
/// Call a Lua function that was "prepared" earlier.
185+
void call_lua_function(prepared_lua_function_t func);
186+
184187
/**
185188
* Call a Lua function that was "prepared" earlier with the OSMObject
186189
* as its only parameter.
187190
*/
188191
void call_lua_function(prepared_lua_function_t func,
189192
osmium::OSMObject const &object);
190193

191-
/// Aquire the lua_mutex and the call `call_lua_function()`.
194+
/// Aquire the lua_mutex and then call `call_lua_function()`.
195+
void get_mutex_and_call_lua_function(prepared_lua_function_t func);
196+
192197
void get_mutex_and_call_lua_function(prepared_lua_function_t func,
193198
osmium::OSMObject const &object);
194199

@@ -296,6 +301,9 @@ class output_flex_t : public output_t
296301
prepared_lua_function_t m_process_way{};
297302
prepared_lua_function_t m_process_relation{};
298303
prepared_lua_function_t m_select_relation_members{};
304+
prepared_lua_function_t m_after_nodes{};
305+
prepared_lua_function_t m_after_ways{};
306+
prepared_lua_function_t m_after_relations{};
299307

300308
calling_context m_calling_context = calling_context::main;
301309

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
Feature: Lua callbacks are called
2+
3+
Scenario: Check access to osm2pgsql object from Lua
4+
Given the input file 'liechtenstein-2013-08-03.osm.pbf'
5+
And the lua style
6+
"""
7+
local nodes = 0
8+
local ways = 0
9+
local relations = 0
10+
local n = 0
11+
12+
osm2pgsql.define_node_table('dummy', {})
13+
14+
function osm2pgsql.process_node(object)
15+
nodes = nodes + 1
16+
end
17+
18+
function osm2pgsql.process_way(object)
19+
ways = ways + 1
20+
end
21+
22+
function osm2pgsql.process_relation(object)
23+
relations = relations + 1
24+
end
25+
26+
local function out()
27+
print(n .. 'n=' .. nodes .. '@')
28+
print(n .. 'w=' .. ways .. '@')
29+
print(n .. 'r=' .. relations .. '@')
30+
n = n + 1
31+
end
32+
33+
osm2pgsql.after_nodes = out
34+
osm2pgsql.after_ways = out
35+
osm2pgsql.after_relations = out
36+
"""
37+
When running osm2pgsql flex
38+
Then the standard output contains
39+
"""
40+
0n=1562@
41+
0w=0@
42+
0r=0@
43+
1n=1562@
44+
1w=7105@
45+
1r=0@
46+
2n=1562@
47+
2w=7105@
48+
2r=113@
49+
"""

0 commit comments

Comments
 (0)