Skip to content

Commit

Permalink
Merge pull request #441 from mmd-osm/patch/cleanup23
Browse files Browse the repository at this point in the history
Minor cleanups
  • Loading branch information
mmd-osm authored Aug 11, 2024
2 parents 4e5e6c6 + 9d3444e commit dcecebb
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 77 deletions.
137 changes: 70 additions & 67 deletions src/api06/changeset_upload/osmchange_tracking.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,105 +24,109 @@ namespace api06 {

// Prepare maps for faster lookup

using object_type_id_pair_t = std::pair< object_type, osm_nwr_signed_id_t >;
using object_type_id_version_tuple_t = std::tuple< object_type, osm_nwr_signed_id_t, osm_version_t>;

std::map< object_type_id_pair_t, object_id_mapping_t > map_create_ids;
std::map< object_type_id_version_tuple_t, object_id_mapping_t > map_modify_ids;
std::map< object_type_id_pair_t, object_id_mapping_t > map_skip_delete_ids;
std::set< object_type_id_pair_t > set_delete_ids;

// Create
std::map< std::pair< object_type, osm_nwr_signed_id_t >, OSMChange_Tracking::object_id_mapping_t > map_create_ids;
for (const auto &id : created_node_ids)
map_create_ids.insert(std::make_pair(std::make_pair(object_type::node, id.old_id), id));
map_create_ids.insert({ { object_type::node, id.old_id }, id });

for (const auto &id : created_way_ids)
map_create_ids.insert(std::make_pair(std::make_pair(object_type::way, id.old_id), id));
map_create_ids.insert({ { object_type::way, id.old_id }, id });

for (const auto &id : created_relation_ids)
map_create_ids.insert(std::make_pair(std::make_pair(object_type::relation, id.old_id), id));
map_create_ids.insert({ { object_type::relation, id.old_id }, id });


// Modify
std::map< std::tuple< object_type, osm_nwr_signed_id_t, osm_version_t>, OSMChange_Tracking::object_id_mapping_t > map_modify_ids;
for (const auto &id : modified_node_ids)
map_modify_ids.insert(std::make_pair(std::make_tuple(object_type::node, id.old_id, id.new_version), id));
map_modify_ids.insert({ { object_type::node, id.old_id, id.new_version }, id });

for (const auto &id : modified_way_ids)
map_modify_ids.insert(std::make_pair(std::make_tuple(object_type::way, id.old_id, id.new_version), id));
map_modify_ids.insert({ { object_type::way, id.old_id, id.new_version }, id });

for (const auto &id : modified_relation_ids)
map_modify_ids.insert(std::make_pair(std::make_tuple(object_type::relation, id.old_id, id.new_version), id));
map_modify_ids.insert({ { object_type::relation, id.old_id, id.new_version}, id });

// Delete (if-unused)
std::map< std::pair< object_type, osm_nwr_signed_id_t>, OSMChange_Tracking::object_id_mapping_t > map_skip_delete_ids;
for (const auto &id : skip_deleted_node_ids)
map_skip_delete_ids.insert(std::make_pair(std::make_pair(object_type::node, id.old_id), id));
map_skip_delete_ids.insert({ { object_type::node, id.old_id }, id });

for (const auto &id : skip_deleted_way_ids)
map_skip_delete_ids.insert(std::make_pair(std::make_pair(object_type::way, id.old_id), id));
map_skip_delete_ids.insert({ { object_type::way, id.old_id }, id });

for (const auto &id : skip_deleted_relation_ids)
map_skip_delete_ids.insert(std::make_pair(std::make_pair(object_type::relation, id.old_id), id));
map_skip_delete_ids.insert({ { object_type::relation, id.old_id }, id });

// Deleted object ids
std::set< std::pair< object_type, osm_nwr_signed_id_t> > set_delete_ids;
for (const auto &id : deleted_node_ids)
set_delete_ids.insert(std::make_pair(object_type::node, id));
set_delete_ids.insert({ object_type::node, id });

for (const auto &id : deleted_way_ids)
set_delete_ids.insert(std::make_pair(object_type::way, id));
set_delete_ids.insert({ object_type::way, id });

for (const auto &id : deleted_relation_ids)
set_delete_ids.insert(std::make_pair(object_type::relation, id));
set_delete_ids.insert({ object_type::relation, id });


std::vector<diffresult_t> result;

// Iterate over all elements in the sequence defined in the osmChange message

for (auto &item : osmchange_orig_sequence) {
for (const auto &item : osmchange_orig_sequence) {

switch (item.op) {

case operation::op_create:
{
auto it = map_create_ids.find(std::make_pair(item.obj_type, item.orig_id));
if (it != map_create_ids.end()) {
diffresult_t row{};
const auto id = it->second;
row.op = item.op;
row.obj_type = item.obj_type;
row.old_id = id.old_id;
row.new_id = id.new_id;
row.new_version = id.new_version;
row.deletion_skipped = false;
result.push_back(row);
auto it = map_create_ids.find({ item.obj_type, item.orig_id });
if (it == map_create_ids.end()) {
throw std::runtime_error ("Element in osmChange message was not processed");
}
else
throw std::runtime_error ("Element in osmChange message was not processed");

diffresult_t row{};
const auto & id = it->second;
row.op = item.op;
row.obj_type = item.obj_type;
row.old_id = id.old_id;
row.new_id = id.new_id;
row.new_version = id.new_version;
row.deletion_skipped = false;
result.push_back(row);
}
break;

case operation::op_modify:
{
auto it = map_modify_ids.find(std::make_tuple(item.obj_type, item.orig_id, item.orig_version + 1));
if (it != map_modify_ids.end()) {
diffresult_t row{};
const auto id = it->second;
row.op = item.op;
row.obj_type = item.obj_type;
row.old_id = id.old_id;
row.new_id = id.new_id;
row.new_version = id.new_version;
row.deletion_skipped = false;
result.push_back(row);
auto it = map_modify_ids.find({ item.obj_type, item.orig_id, item.orig_version + 1 });
if (it == map_modify_ids.end()) {
throw std::runtime_error ("Element in osmChange message was not processed");
}
else
throw std::runtime_error ("Element in osmChange message was not processed");

diffresult_t row{};
const auto & id = it->second;
row.op = item.op;
row.obj_type = item.obj_type;
row.old_id = id.old_id;
row.new_id = id.new_id;
row.new_version = id.new_version;
row.deletion_skipped = false;
result.push_back(row);
}
break;

case operation::op_delete:

if (item.if_unused) {
auto it = map_skip_delete_ids.find(std::make_pair(item.obj_type, item.orig_id));
auto it = map_skip_delete_ids.find({ item.obj_type, item.orig_id });
if (it != map_skip_delete_ids.end()) {
diffresult_t row{};
const auto id = it->second;
const auto & id = it->second;
row.op = item.op;
row.obj_type = item.obj_type;
row.old_id = id.old_id;
Expand All @@ -132,35 +136,34 @@ namespace api06 {
result.push_back(row);
}
else {
auto it = set_delete_ids.find(std::make_pair(item.obj_type, item.orig_id));
if (it != set_delete_ids.end()) {
diffresult_t row{};
row.op = item.op;
row.obj_type = item.obj_type;
row.old_id = it->second;
row.deletion_skipped = false;
result.push_back(row);
}
else {
throw std::runtime_error ("Element in osmChange message was not processed");
auto it = set_delete_ids.find({ item.obj_type, item.orig_id });
if (it == set_delete_ids.end()) {
throw std::runtime_error ("Element in osmChange message was not processed");
}

diffresult_t row{};
row.op = item.op;
row.obj_type = item.obj_type;
row.old_id = it->second;
row.deletion_skipped = false;
result.push_back(row);
}
}

if (!item.if_unused) {
auto it = set_delete_ids.find(std::make_pair(item.obj_type, item.orig_id));
if (it != set_delete_ids.end()) {
diffresult_t row{};
row.op = item.op;
row.obj_type = item.obj_type;
row.old_id = it->second;
row.deletion_skipped = false;
result.push_back(row);
}
else {
throw std::runtime_error ("Element in osmChange message was not processed");
auto it = set_delete_ids.find({ item.obj_type, item.orig_id });
if (it == set_delete_ids.end()) {
throw std::runtime_error ("Element in osmChange message was not processed");
}

diffresult_t row{};
row.op = item.op;
row.obj_type = item.obj_type;
row.old_id = it->second;
row.deletion_skipped = false;
result.push_back(row);
}

break;

case operation::op_undefined:
Expand Down
9 changes: 3 additions & 6 deletions src/backend/apidb/changeset_upload/relation_updater.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -336,26 +336,23 @@ void ApiDB_Relation_Updater::replace_old_ids_in_relations(
// Prepare mapping tables
std::map<osm_nwr_signed_id_t, osm_nwr_id_t> map_relations;
for (auto &i : created_relation_id_mapping) {
auto res = map_relations.insert(
std::pair<osm_nwr_signed_id_t, osm_nwr_id_t>(i.old_id, i.new_id));
auto res = map_relations.insert({ i.old_id, i.new_id });
if (!res.second)
throw http::bad_request(
fmt::format("Duplicate relation placeholder id {:d}.", i.old_id));
}

std::map<osm_nwr_signed_id_t, osm_nwr_id_t> map_ways;
for (auto &i : created_way_id_mapping) {
auto res = map_ways.insert(
std::pair<osm_nwr_signed_id_t, osm_nwr_id_t>(i.old_id, i.new_id));
auto res = map_ways.insert({ i.old_id, i.new_id });
if (!res.second)
throw http::bad_request(
fmt::format("Duplicate way placeholder id {:d}.", i.old_id));
}

std::map<osm_nwr_signed_id_t, osm_nwr_id_t> map_nodes;
for (auto &i : created_node_id_mapping) {
auto res = map_nodes.insert(
std::pair<osm_nwr_signed_id_t, osm_nwr_id_t>(i.old_id, i.new_id));
auto res = map_nodes.insert({ i.old_id, i.new_id });
if (!res.second)
throw http::bad_request(
fmt::format("Duplicate node placeholder id {:d}.", i.old_id));
Expand Down
6 changes: 2 additions & 4 deletions src/backend/apidb/changeset_upload/way_updater.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -278,17 +278,15 @@ void ApiDB_Way_Updater::replace_old_ids_in_ways(
&created_way_id_mapping) {
std::map<osm_nwr_signed_id_t, osm_nwr_id_t> map_ways;
for (auto &i : created_way_id_mapping) {
auto res = map_ways.insert(
std::pair<osm_nwr_signed_id_t, osm_nwr_id_t>(i.old_id, i.new_id));
auto res = map_ways.insert({ i.old_id, i.new_id });
if (!res.second)
throw http::bad_request(
fmt::format("Duplicate way placeholder id {:d}.", i.old_id));
}

std::map<osm_nwr_signed_id_t, osm_nwr_id_t> map_nodes;
for (auto &i : created_node_id_mapping) {
auto res = map_nodes.insert(
std::pair<osm_nwr_signed_id_t, osm_nwr_id_t>(i.old_id, i.new_id));
auto res = map_nodes.insert({ i.old_id, i.new_id });
if (!res.second)
throw http::bad_request(
fmt::format("Duplicate node placeholder id {:d}.", i.old_id));
Expand Down

0 comments on commit dcecebb

Please sign in to comment.