1/** Copyright 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018 Roland Olbricht et al. 3 * This file is part of Overpass_API. 5 * Overpass_API is free software: you can redistribute it and/or modify 6 * it under the terms of the GNU Affero General Public License as 7 * published by the Free Software Foundation, either version 3 of the 8 * License, or (at your option) any later version. 10 * Overpass_API is distributed in the hope that it will be useful, 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 * GNU General Public License for more details. 15 * You should have received a copy of the GNU Affero General Public License 16 * along with Overpass_API. If not, see <http://www.gnu.org/licenses/>. 25#include "../../template_db/block_backend.h" 26#include "../../template_db/block_backend_write.h" 27#include "../../template_db/random_file.h" 28#include "../../template_db/transaction.h" 29#include "../core/datatypes.h" 30#include "../core/settings.h" 31#include "area_updater.h" 34Area_Updater::Area_Updater(Transaction& transaction_) 350 : transaction(&transaction_), external_transaction(true), 360 total_area_blocks_count(0) 39Area_Updater::Area_Updater(std::string db_dir_) 400 : transaction(0), external_transaction(false), 410 db_dir(db_dir_), total_area_blocks_count(0) 44void Area_Updater::add_blocks 45 (const std::map< Uint31_Index, std::vector< Area_Block > >& area_blocks_) 470 for (std::map< Uint31_Index, std::vector< Area_Block > >::const_iterator 480 it(area_blocks_.begin()); it != area_blocks_.end(); ++it) 500 for (std::vector< Area_Block >::const_iterator it2(it->second.begin()); 510 it2 != it->second.end(); ++it2) 520 area_blocks[it->first].push_back(*it2); 530 total_area_blocks_count += it->second.size(); 57void Area_Updater::update() 590 if (!external_transaction) 600 transaction = new Nonsynced_Transaction(Access_Mode::writeable, false, db_dir, ""); 620 sort(areas_to_insert.begin(), areas_to_insert.end()); 640 std::map< Uint31_Index, std::set< Area_Skeleton > > locations_to_delete; 650 std::map< Uint31_Index, std::set< Area_Block > > blocks_to_delete; 660 update_area_ids(locations_to_delete, blocks_to_delete); 670 update_members(locations_to_delete, blocks_to_delete); 690 std::vector< Tag_Entry< uint32 > > tags_to_delete; 700 prepare_delete_tags(tags_to_delete, locations_to_delete); 710 update_area_tags_local(tags_to_delete); 720 update_area_tags_global(tags_to_delete); 740 ids_to_modify.clear(); 750 areas_to_insert.clear(); 770 total_area_blocks_count = 0; 790 if (!external_transaction) 83void Area_Updater::update_area_ids 84 (std::map< Uint31_Index, std::set< Area_Skeleton > >& locations_to_delete, 85 std::map< Uint31_Index, std::set< Area_Block > >& blocks_to_delete) 870 std::vector< Uint31_Index > blocks_req; 89 // process the areas themselves 900 Block_Backend< Uint31_Index, Area_Skeleton > area_locations_db 910 (transaction->data_index(area_settings().AREAS)); 920 for (Block_Backend< Uint31_Index, Area_Skeleton >::Flat_Iterator 930 it(area_locations_db.flat_begin()); 940 !(it == area_locations_db.flat_end()); ++it) 960 if (ids_to_modify.find(it.object().id) != ids_to_modify.end()) 980 for (auto it2 = it.object().used_indices.begin(); it2 != it.object().used_indices.end(); ++it2) 990 blocks_req.push_back(*it2); 1000 locations_to_delete[it.index().val()].insert(it.object()); 1030 std::sort(blocks_req.begin(), blocks_req.end()); 1040 blocks_req.erase(std::unique(blocks_req.begin(), blocks_req.end()), blocks_req.end()); 1060 Block_Backend< Uint31_Index, Area_Block > area_blocks_db 1070 (transaction->data_index(area_settings().AREA_BLOCKS)); 1080 for (Block_Backend< Uint31_Index, Area_Block >::Discrete_Iterator 1090 it(area_blocks_db.discrete_begin(blocks_req.begin(), blocks_req.end())); 1100 !(it == area_blocks_db.discrete_end()); ++it) 1120 if (ids_to_modify.find(it.object().id) != ids_to_modify.end()) 1130 blocks_to_delete[it.index()].insert(it.object()); 117void Area_Updater::update_members 118 (const std::map< Uint31_Index, std::set< Area_Skeleton > >& locations_to_delete, 119 const std::map< Uint31_Index, std::set< Area_Block > >& blocks_to_delete) 1210 std::map< Uint31_Index, std::set< Area_Skeleton > > locations_to_insert; 1220 for (std::vector< std::pair< Area_Location, Uint31_Index > >::const_iterator 1230 it(areas_to_insert.begin()); it != areas_to_insert.end(); ++it) 1240 locations_to_insert[it->second].insert(Area_Skeleton(it->first)); 1260 Block_Backend< Uint31_Index, Area_Skeleton > area_locations 1270 (transaction->data_index(area_settings().AREAS)); 1280 area_locations.update(locations_to_delete, locations_to_insert); 1300 std::map< Uint31_Index, std::set< Area_Block > > blocks_to_insert; 1310 for (std::map< Uint31_Index, std::vector< Area_Block > >::const_iterator 1320 it(area_blocks.begin()); it != area_blocks.end(); ++it) 1340 for (std::vector< Area_Block >::const_iterator it2(it->second.begin()); 1350 it2 != it->second.end(); ++it2) 1360 blocks_to_insert[it->first].insert(*it2); 1390 Block_Backend< Uint31_Index, Area_Block > area_blocks_db 1400 (transaction->data_index(area_settings().AREA_BLOCKS)); 1410 area_blocks_db.update(blocks_to_delete, blocks_to_insert); 144void Area_Updater::prepare_delete_tags 145 (std::vector< Tag_Entry< uint32 > >& tags_to_delete, 146 const std::map< Uint31_Index, std::set< Area_Skeleton > >& to_delete) 148 // make indices appropriately coarse 1490 std::map< uint32, std::set< Area::Id_Type > > to_delete_coarse; 1500 for (std::map< Uint31_Index, std::set< Area_Skeleton > >::const_iterator 1510 it(to_delete.begin()); it != to_delete.end(); ++it) 1530 std::set< Area::Id_Type >& handle(to_delete_coarse[it->first.val() & 0xffffff00]); 1540 for (std::set< Area_Skeleton >::const_iterator it2(it->second.begin()); 1550 it2 != it->second.end(); ++it2) 1560 handle.insert(it2->id); 159 // formulate range query 1600 Ranges< Tag_Index_Local > ranges; 1610 for (auto it = to_delete_coarse.begin(); it != to_delete_coarse.end(); ++it) 1620 ranges.push_back({ it->first, "", "" }, { it->first + 1, "", "" }); 165 // iterate over the result 1660 Block_Backend< Tag_Index_Local, Uint32_Index > areas_db 1670 (transaction->data_index(area_settings().AREA_TAGS_LOCAL)); 1680 Tag_Index_Local current_index; 1690 Tag_Entry< uint32 > tag_entry; 1700 current_index.index = 0xffffffff; 1710 std::set< Area::Id_Type >* handle = 0; 1720 for (auto it = areas_db.range_begin(ranges); !(it == areas_db.range_end()); ++it) 1740 if (!(current_index == it.index())) 1760 if ((current_index.index != 0xffffffff) && (!tag_entry.ids.empty())) 1770 tags_to_delete.push_back(tag_entry); 1780 current_index = it.index(); 1790 tag_entry.index = it.index().index; 1800 tag_entry.key = it.index().key; 1810 tag_entry.value = it.index().value; 1820 tag_entry.ids.clear(); 1840 handle = &to_delete_coarse[it.index().index]; 1870 if (handle->find(it.object().val()) != handle->end()) 1880 tag_entry.ids.push_back(it.object().val()); 1900 if ((current_index.index != 0xffffffff) && (!tag_entry.ids.empty())) 1910 tags_to_delete.push_back(tag_entry); 195Area_Location* binary_search_for_id 196 (std::vector< std::pair< Area_Location, Uint31_Index > >& vect, uint32 id) 1990 uint32 upper(vect.size()); 2010 while (upper > lower) 2030 uint32 pos((upper + lower)/2); 2040 if (id < vect[pos].first.id) 2060 else if (vect[pos].first.id == id) 2070 return &(vect[pos].first); 214void Area_Updater::update_area_tags_local 215 (const std::vector< Tag_Entry< uint32 > >& tags_to_delete) 2170 std::map< Tag_Index_Local, std::set< Uint32_Index > > db_to_delete; 2180 std::map< Tag_Index_Local, std::set< Uint32_Index > > db_to_insert; 2200 for (std::vector< Tag_Entry< uint32 > >::const_iterator it(tags_to_delete.begin()); 2210 it != tags_to_delete.end(); ++it) 2230 Tag_Index_Local index; 2240 index.index = it->index; 2250 index.key = it->key; 2260 index.value = it->value; 2280 std::set< Uint32_Index > area_ids; 2290 for (std::vector< uint32 >::const_iterator it2(it->ids.begin()); 2300 it2 != it->ids.end(); ++it2) 2310 area_ids.insert(*it2); 2330 db_to_delete[index] = area_ids; 2360 std::vector< std::pair< Area_Location, Uint31_Index > >::const_iterator 2370 rit(areas_to_insert.begin()); 2380 for (std::set< Area::Id_Type >::const_iterator it(ids_to_modify.begin()); 2390 it != ids_to_modify.end(); ++it) 2410 if ((rit != areas_to_insert.end()) && (*it == rit->first.id)) 2430 Tag_Index_Local index; 2440 index.index = rit->second.val() & 0xffffff00; 2460 for (std::vector< std::pair< std::string, std::string > >::const_iterator 2470 it2(rit->first.tags.begin()); it2 != rit->first.tags.end(); ++it2) 2490 index.key = it2->first; 2500 index.value = it2->second; 2510 db_to_insert[index].insert(rit->first.id); 2520 db_to_delete[index].insert(rit->first.id); 2580 Block_Backend< Tag_Index_Local, Uint32_Index > areas_db 2590 (transaction->data_index(area_settings().AREA_TAGS_LOCAL)); 2600 areas_db.update(db_to_delete, db_to_insert); 263void Area_Updater::update_area_tags_global 264 (const std::vector< Tag_Entry< uint32 > >& tags_to_delete) 2660 std::map< Tag_Index_Global, std::set< Uint32_Index > > db_to_delete; 2670 std::map< Tag_Index_Global, std::set< Uint32_Index > > db_to_insert; 2690 for (std::vector< Tag_Entry< uint32 > >::const_iterator it(tags_to_delete.begin()); 2700 it != tags_to_delete.end(); ++it) 2720 Tag_Index_Global index; 2730 index.key = it->key; 2740 index.value = it->value; 2760 for (std::vector< uint32 >::const_iterator it2(it->ids.begin()); 2770 it2 != it->ids.end(); ++it2) 2780 db_to_delete[index].insert(*it2); 2810 std::vector< std::pair< Area_Location, Uint31_Index > >::const_iterator 2820 rit(areas_to_insert.begin()); 2830 for (std::set< Area::Id_Type >::const_iterator it(ids_to_modify.begin()); 2840 it != ids_to_modify.end(); ++it) 2860 if ((rit != areas_to_insert.end()) && (*it == rit->first.id)) 2880 Tag_Index_Global index; 2900 for (std::vector< std::pair< std::string, std::string > >::const_iterator 2910 it2(rit->first.tags.begin()); it2 != rit->first.tags.end(); ++it2) 2930 index.key = it2->first; 2940 index.value = it2->second; 2950 db_to_insert[index].insert(rit->first.id); 2960 db_to_delete[index].insert(rit->first.id); 3020 Block_Backend< Tag_Index_Global, Uint32_Index > areas_db 3030 (transaction->data_index(area_settings().AREA_TAGS_GLOBAL)); 3040 areas_db.update(db_to_delete, db_to_insert);