Skip to content

Commit 3c7d0e2

Browse files
authored
Merge pull request #2344 from lonvia/delete-callback
Add callbacks for delete actions
2 parents 2d97601 + 47ee919 commit 3c7d0e2

File tree

10 files changed

+258
-97
lines changed

10 files changed

+258
-97
lines changed

src/osmdata.cpp

Lines changed: 10 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,7 @@ osmdata_t::osmdata_t(std::shared_ptr<middle_t> mid,
3131
: m_mid(std::move(mid)), m_output(std::move(output)),
3232
m_connection_params(options.connection_params), m_bbox(options.bbox),
3333
m_num_procs(options.num_procs), m_append(options.append),
34-
m_droptemp(options.droptemp),
35-
m_with_extra_attrs(options.extra_attributes ||
36-
options.output_backend == "flex")
34+
m_droptemp(options.droptemp)
3735
{
3836
assert(m_mid);
3937
assert(m_output);
@@ -56,17 +54,12 @@ void osmdata_t::node(osmium::Node const &node)
5654
m_mid->node(node);
5755

5856
if (node.deleted()) {
59-
m_output->node_delete(node.id());
57+
m_output->node_delete(node);
6058
return;
6159
}
6260

63-
bool const has_tags_or_attrs = m_with_extra_attrs || !node.tags().empty();
6461
if (m_append) {
65-
if (has_tags_or_attrs) {
66-
m_output->node_modify(node);
67-
} else {
68-
m_output->node_delete(node.id());
69-
}
62+
m_output->node_modify(node);
7063

7164
// Version 1 means this is a new node, so there can't be an existing
7265
// way or relation referencing it, so we don't have to add that node
@@ -75,7 +68,7 @@ void osmdata_t::node(osmium::Node const &node)
7568
if (node.version() != 1) {
7669
m_changed_nodes.push_back(node.id());
7770
}
78-
} else if (has_tags_or_attrs) {
71+
} else {
7972
m_output->node_add(node);
8073
}
8174
}
@@ -101,17 +94,12 @@ void osmdata_t::way(osmium::Way &way)
10194
m_mid->way(way);
10295

10396
if (way.deleted()) {
104-
m_output->way_delete(way.id());
97+
m_output->way_delete(&way);
10598
return;
10699
}
107100

108-
bool const has_tags_or_attrs = m_with_extra_attrs || !way.tags().empty();
109101
if (m_append) {
110-
if (has_tags_or_attrs) {
111-
m_output->way_modify(&way);
112-
} else {
113-
m_output->way_delete(way.id());
114-
}
102+
m_output->way_modify(&way);
115103

116104
// Version 1 means this is a new way, so there can't be an existing
117105
// relation referencing it, so we don't have to add that way to the
@@ -120,7 +108,7 @@ void osmdata_t::way(osmium::Way &way)
120108
if (way.version() != 1) {
121109
m_changed_ways.push_back(way.id());
122110
}
123-
} else if (has_tags_or_attrs) {
111+
} else {
124112
m_output->way_add(&way);
125113
}
126114
}
@@ -173,19 +161,14 @@ void osmdata_t::relation(osmium::Relation const &rel)
173161
m_mid->relation(rel);
174162

175163
if (rel.deleted()) {
176-
m_output->relation_delete(rel.id());
164+
m_output->relation_delete(rel);
177165
return;
178166
}
179167

180-
bool const has_tags_or_attrs = m_with_extra_attrs || !rel.tags().empty();
181168
if (m_append) {
182-
if (has_tags_or_attrs) {
183-
m_output->relation_modify(rel);
184-
} else {
185-
m_output->relation_delete(rel.id());
186-
}
169+
m_output->relation_modify(rel);
187170
m_changed_relations.push_back(rel.id());
188-
} else if (has_tags_or_attrs) {
171+
} else {
189172
m_output->relation_add(rel);
190173
}
191174
}

src/osmdata.hpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,6 @@ class osmdata_t : public osmium::handler::Handler
117117
unsigned int m_num_procs;
118118
bool m_append;
119119
bool m_droptemp;
120-
bool m_with_extra_attrs;
121120
};
122121

123122
#endif // OSM2PGSQL_OSMDATA_HPP

src/output-flex.cpp

Lines changed: 75 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -158,39 +158,41 @@ void push_osm_object_to_lua_stack(lua_State *lua_state,
158158
luaX_add_table_str(lua_state, "user", object.user());
159159
}
160160

161-
if (object.type() == osmium::item_type::way) {
162-
auto const &way = static_cast<osmium::Way const &>(object);
163-
luaX_add_table_bool(lua_state, "is_closed",
164-
!way.nodes().empty() && way.is_closed());
165-
luaX_add_table_array(lua_state, "nodes", way.nodes(),
166-
[&](osmium::NodeRef const &wn) {
167-
lua_pushinteger(lua_state, wn.ref());
168-
});
169-
} else if (object.type() == osmium::item_type::relation) {
170-
auto const &relation = static_cast<osmium::Relation const &>(object);
171-
luaX_add_table_array(
172-
lua_state, "members", relation.members(),
173-
[&](osmium::RelationMember const &member) {
174-
lua_createtable(lua_state, 0, 3);
175-
std::array<char, 2> tmp{"x"};
176-
tmp[0] = osmium::item_type_to_char(member.type());
177-
luaX_add_table_str(lua_state, "type", tmp.data());
178-
luaX_add_table_int(lua_state, "ref", member.ref());
179-
luaX_add_table_str(lua_state, "role", member.role());
180-
});
181-
}
182-
183-
lua_pushliteral(lua_state, "tags");
184-
lua_createtable(lua_state, 0, (int)object.tags().size());
185-
for (auto const &tag : object.tags()) {
186-
luaX_add_table_str(lua_state, tag.key(), tag.value());
187-
}
188-
lua_rawset(lua_state, -3);
189-
190-
// Set the metatable of this object
191-
lua_pushstring(lua_state, OSM2PGSQL_OSMOBJECT_CLASS);
192-
lua_gettable(lua_state, LUA_REGISTRYINDEX);
193-
lua_setmetatable(lua_state, -2);
161+
if (!object.deleted()) {
162+
if (object.type() == osmium::item_type::way) {
163+
auto const &way = static_cast<osmium::Way const &>(object);
164+
luaX_add_table_bool(lua_state, "is_closed",
165+
!way.nodes().empty() && way.is_closed());
166+
luaX_add_table_array(lua_state, "nodes", way.nodes(),
167+
[&](osmium::NodeRef const &wn) {
168+
lua_pushinteger(lua_state, wn.ref());
169+
});
170+
} else if (object.type() == osmium::item_type::relation) {
171+
auto const &relation = static_cast<osmium::Relation const &>(object);
172+
luaX_add_table_array(
173+
lua_state, "members", relation.members(),
174+
[&](osmium::RelationMember const &member) {
175+
lua_createtable(lua_state, 0, 3);
176+
std::array<char, 2> tmp{"x"};
177+
tmp[0] = osmium::item_type_to_char(member.type());
178+
luaX_add_table_str(lua_state, "type", tmp.data());
179+
luaX_add_table_int(lua_state, "ref", member.ref());
180+
luaX_add_table_str(lua_state, "role", member.role());
181+
});
182+
}
183+
184+
lua_pushliteral(lua_state, "tags");
185+
lua_createtable(lua_state, 0, (int)object.tags().size());
186+
for (auto const &tag : object.tags()) {
187+
luaX_add_table_str(lua_state, tag.key(), tag.value());
188+
}
189+
lua_rawset(lua_state, -3);
190+
191+
// Set the metatable of this object
192+
lua_pushstring(lua_state, OSM2PGSQL_OSMOBJECT_CLASS);
193+
lua_gettable(lua_state, LUA_REGISTRYINDEX);
194+
lua_setmetatable(lua_state, -2);
195+
}
194196
}
195197

196198
/**
@@ -1084,6 +1086,37 @@ void output_flex_t::delete_from_tables(osmium::item_type type, osmid_t osm_id)
10841086
}
10851087
}
10861088

1089+
void output_flex_t::node_delete(osmium::Node const &node)
1090+
{
1091+
if (m_delete_node) {
1092+
m_context_node = &node;
1093+
get_mutex_and_call_lua_function(m_delete_node, node);
1094+
m_context_node = nullptr;
1095+
}
1096+
1097+
node_delete(node.id());
1098+
}
1099+
1100+
void output_flex_t::way_delete(osmium::Way *way)
1101+
{
1102+
if (m_delete_way) {
1103+
m_way_cache.init(way);
1104+
get_mutex_and_call_lua_function(m_delete_way, m_way_cache.get());
1105+
}
1106+
1107+
way_delete(way->id());
1108+
}
1109+
1110+
void output_flex_t::relation_delete(osmium::Relation const &rel)
1111+
{
1112+
if (m_delete_relation) {
1113+
m_relation_cache.init(rel);
1114+
get_mutex_and_call_lua_function(m_delete_relation, rel);
1115+
}
1116+
1117+
relation_delete(rel.id());
1118+
}
1119+
10871120
/* Delete is easy, just remove all traces of this object. We don't need to
10881121
* worry about finding objects that depend on it, since the same diff must
10891122
* contain the change for that also. */
@@ -1146,6 +1179,8 @@ output_flex_t::output_flex_t(output_flex_t const *other,
11461179
m_process_untagged_node(other->m_process_untagged_node),
11471180
m_process_untagged_way(other->m_process_untagged_way),
11481181
m_process_untagged_relation(other->m_process_untagged_relation),
1182+
m_delete_node(other->m_delete_node), m_delete_way(other->m_delete_way),
1183+
m_delete_relation(other->m_delete_relation),
11491184
m_select_relation_members(other->m_select_relation_members),
11501185
m_after_nodes(other->m_after_nodes), m_after_ways(other->m_after_ways),
11511186
m_after_relations(other->m_after_relations)
@@ -1330,6 +1365,13 @@ void output_flex_t::init_lua(std::string const &filename,
13301365
prepared_lua_function_t{lua_state(), calling_context::process_relation,
13311366
"process_untagged_relation"};
13321367

1368+
m_delete_node = prepared_lua_function_t{
1369+
lua_state(), calling_context::process_node, "delete_node"};
1370+
m_delete_way = prepared_lua_function_t{
1371+
lua_state(), calling_context::process_way, "delete_way"};
1372+
m_delete_relation = prepared_lua_function_t{
1373+
lua_state(), calling_context::process_relation, "delete_relation"};
1374+
13331375
m_select_relation_members = prepared_lua_function_t{
13341376
lua_state(), calling_context::select_relation_members,
13351377
"select_relation_members", 1};

src/output-flex.hpp

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,9 @@ struct options_t;
4343
enum class calling_context : std::uint8_t
4444
{
4545
main = 0, ///< In main context, i.e. the Lua script outside any callbacks
46-
process_node = 1, ///< In the process_node() callback
47-
process_way = 2, ///< In the process_way() callback
48-
process_relation = 3, ///< In the process_relation() callback
46+
process_node = 1, ///< Inside a callback where a node is handled
47+
process_way = 2, ///< Inside a callback where a way is handled
48+
process_relation = 3, ///< Inside a callback where a relation is handled
4949
select_relation_members = 4 ///< In the select_relation_members() callback
5050
};
5151

@@ -147,9 +147,9 @@ class output_flex_t : public output_t
147147
void way_modify(osmium::Way *way) override;
148148
void relation_modify(osmium::Relation const &rel) override;
149149

150-
void node_delete(osmid_t id) override;
151-
void way_delete(osmid_t id) override;
152-
void relation_delete(osmid_t id) override;
150+
void node_delete(osmium::Node const &node) override;
151+
void way_delete(osmium::Way *way) override;
152+
void relation_delete(osmium::Relation const &rel) override;
153153

154154
void merge_expire_trees(output_t *other) override;
155155

@@ -204,6 +204,10 @@ class output_flex_t : public output_t
204204
osmium::OSMObject const &
205205
check_and_get_context_object(flex_table_t const &table);
206206

207+
void node_delete(osmid_t id);
208+
void way_delete(osmid_t id);
209+
void relation_delete(osmid_t id);
210+
207211
void delete_from_table(table_connection_t *table_connection,
208212
pg_conn_t const &db_connection,
209213
osmium::item_type type, osmid_t osm_id);
@@ -304,6 +308,10 @@ class output_flex_t : public output_t
304308
prepared_lua_function_t m_process_untagged_way;
305309
prepared_lua_function_t m_process_untagged_relation;
306310

311+
prepared_lua_function_t m_delete_node;
312+
prepared_lua_function_t m_delete_way;
313+
prepared_lua_function_t m_delete_relation;
314+
307315
prepared_lua_function_t m_select_relation_members;
308316

309317
prepared_lua_function_t m_after_nodes;

src/output-null.hpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,9 @@ class output_null_t : public output_t
5050
void way_modify(osmium::Way * /*way*/) override {}
5151
void relation_modify(osmium::Relation const & /*rel*/) override {}
5252

53-
void node_delete(osmid_t /*id*/) override {}
54-
void way_delete(osmid_t /*id*/) override {}
55-
void relation_delete(osmid_t /*id*/) override {}
53+
void node_delete(osmium::Node const & /*node*/) override {}
54+
void way_delete(osmium::Way * /*way*/) override {}
55+
void relation_delete(osmium::Relation const & /*rel*/) override {}
5656
};
5757

5858
#endif // OSM2PGSQL_OUTPUT_NULL_HPP

0 commit comments

Comments
 (0)