-
-
Notifications
You must be signed in to change notification settings - Fork 477
Add callbacks for delete actions #2344
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -158,39 +158,41 @@ void push_osm_object_to_lua_stack(lua_State *lua_state, | |
luaX_add_table_str(lua_state, "user", object.user()); | ||
} | ||
|
||
if (object.type() == osmium::item_type::way) { | ||
auto const &way = static_cast<osmium::Way const &>(object); | ||
luaX_add_table_bool(lua_state, "is_closed", | ||
!way.nodes().empty() && way.is_closed()); | ||
luaX_add_table_array(lua_state, "nodes", way.nodes(), | ||
[&](osmium::NodeRef const &wn) { | ||
lua_pushinteger(lua_state, wn.ref()); | ||
}); | ||
} else if (object.type() == osmium::item_type::relation) { | ||
auto const &relation = static_cast<osmium::Relation const &>(object); | ||
luaX_add_table_array( | ||
lua_state, "members", relation.members(), | ||
[&](osmium::RelationMember const &member) { | ||
lua_createtable(lua_state, 0, 3); | ||
std::array<char, 2> tmp{"x"}; | ||
tmp[0] = osmium::item_type_to_char(member.type()); | ||
luaX_add_table_str(lua_state, "type", tmp.data()); | ||
luaX_add_table_int(lua_state, "ref", member.ref()); | ||
luaX_add_table_str(lua_state, "role", member.role()); | ||
}); | ||
} | ||
|
||
lua_pushliteral(lua_state, "tags"); | ||
lua_createtable(lua_state, 0, (int)object.tags().size()); | ||
for (auto const &tag : object.tags()) { | ||
luaX_add_table_str(lua_state, tag.key(), tag.value()); | ||
} | ||
lua_rawset(lua_state, -3); | ||
|
||
// Set the metatable of this object | ||
lua_pushstring(lua_state, OSM2PGSQL_OSMOBJECT_CLASS); | ||
lua_gettable(lua_state, LUA_REGISTRYINDEX); | ||
lua_setmetatable(lua_state, -2); | ||
if (!object.deleted()) { | ||
if (object.type() == osmium::item_type::way) { | ||
auto const &way = static_cast<osmium::Way const &>(object); | ||
luaX_add_table_bool(lua_state, "is_closed", | ||
!way.nodes().empty() && way.is_closed()); | ||
luaX_add_table_array(lua_state, "nodes", way.nodes(), | ||
[&](osmium::NodeRef const &wn) { | ||
lua_pushinteger(lua_state, wn.ref()); | ||
}); | ||
} else if (object.type() == osmium::item_type::relation) { | ||
auto const &relation = static_cast<osmium::Relation const &>(object); | ||
luaX_add_table_array( | ||
lua_state, "members", relation.members(), | ||
[&](osmium::RelationMember const &member) { | ||
lua_createtable(lua_state, 0, 3); | ||
std::array<char, 2> tmp{"x"}; | ||
tmp[0] = osmium::item_type_to_char(member.type()); | ||
luaX_add_table_str(lua_state, "type", tmp.data()); | ||
luaX_add_table_int(lua_state, "ref", member.ref()); | ||
luaX_add_table_str(lua_state, "role", member.role()); | ||
}); | ||
} | ||
|
||
lua_pushliteral(lua_state, "tags"); | ||
lua_createtable(lua_state, 0, (int)object.tags().size()); | ||
for (auto const &tag : object.tags()) { | ||
luaX_add_table_str(lua_state, tag.key(), tag.value()); | ||
} | ||
lua_rawset(lua_state, -3); | ||
|
||
// Set the metatable of this object | ||
lua_pushstring(lua_state, OSM2PGSQL_OSMOBJECT_CLASS); | ||
lua_gettable(lua_state, LUA_REGISTRYINDEX); | ||
lua_setmetatable(lua_state, -2); | ||
} | ||
} | ||
|
||
/** | ||
|
@@ -1084,6 +1086,38 @@ void output_flex_t::delete_from_tables(osmium::item_type type, osmid_t osm_id) | |
} | ||
} | ||
|
||
void output_flex_t::node_delete(osmium::Node const &node) | ||
{ | ||
if (m_delete_node) { | ||
m_context_node = &node; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You are reusing an existing context. This is fine, but the comments in |
||
get_mutex_and_call_lua_function(m_delete_node, node); | ||
m_context_node = nullptr; | ||
} | ||
|
||
node_delete(node.id()); | ||
} | ||
|
||
void output_flex_t::way_delete(osmium::Way *way) | ||
{ | ||
if (m_delete_way) { | ||
m_way_cache.init(way); | ||
get_mutex_and_call_lua_function(m_delete_way, m_way_cache.get()); | ||
} | ||
|
||
way_delete(way->id()); | ||
} | ||
|
||
void output_flex_t::relation_delete(osmium::Relation const &rel) | ||
{ | ||
if (m_delete_relation) { | ||
m_relation_cache.init(rel); | ||
select_relation_members(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Deleted relations have no members, so there is no way of selecting any of them for two-stage processing. So this line can be removed. |
||
get_mutex_and_call_lua_function(m_delete_relation, rel); | ||
} | ||
|
||
relation_delete(rel.id()); | ||
} | ||
|
||
/* Delete is easy, just remove all traces of this object. We don't need to | ||
* worry about finding objects that depend on it, since the same diff must | ||
* contain the change for that also. */ | ||
|
@@ -1146,6 +1180,8 @@ output_flex_t::output_flex_t(output_flex_t const *other, | |
m_process_untagged_node(other->m_process_untagged_node), | ||
m_process_untagged_way(other->m_process_untagged_way), | ||
m_process_untagged_relation(other->m_process_untagged_relation), | ||
m_delete_node(other->m_delete_node), m_delete_way(other->m_delete_way), | ||
m_delete_relation(other->m_delete_relation), | ||
m_select_relation_members(other->m_select_relation_members), | ||
m_after_nodes(other->m_after_nodes), m_after_ways(other->m_after_ways), | ||
m_after_relations(other->m_after_relations) | ||
|
@@ -1330,6 +1366,13 @@ void output_flex_t::init_lua(std::string const &filename, | |
prepared_lua_function_t{lua_state(), calling_context::process_relation, | ||
"process_untagged_relation"}; | ||
|
||
m_delete_node = prepared_lua_function_t{ | ||
lua_state(), calling_context::process_node, "delete_node"}; | ||
m_delete_way = prepared_lua_function_t{ | ||
lua_state(), calling_context::process_way, "delete_way"}; | ||
m_delete_relation = prepared_lua_function_t{ | ||
lua_state(), calling_context::process_relation, "delete_relation"}; | ||
|
||
m_select_relation_members = prepared_lua_function_t{ | ||
lua_state(), calling_context::select_relation_members, | ||
"select_relation_members", 1}; | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is weird that the process callbacks get a "real Lua object", and the delete callbacks only get a "Lua table" as parameter. This might have all sorts of consequences later, no sure. It definitely prevents us from ever calling methods on deleted OSM objects.