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/>. 19#include "../../template_db/block_backend.h" 20#include "../../template_db/random_file.h" 21#include "../core/settings.h" 22#include "../data/abstract_processing.h" 23#include "../data/collect_members.h" 24#include "../data/filenames.h" 25#include "../data/filter_by_tags.h" 26#include "../data/filter_ids_by_tags.h" 27#include "../data/meta_collector.h" 28#include "../data/regular_expression.h" 29#include "../data/tags_global_reader.h" 30#include "area_query.h" 31#include "bbox_query.h" 38//----------------------------------------------------------------------------- 40bool Query_Statement::area_query_exists_ = false; 42Generic_Statement_Maker< Query_Statement > Query_Statement::statement_maker("query"); 44Query_Statement::Query_Statement 45 (int line_number_, const std::map< std::string, std::string >& input_attributes, Parsed_Query& global_settings) 462567582 q : Output_Statement(line_number_), global_bbox_statement(0) 482567582 q std::map< std::string, std::string > attributes; 502567582 q attributes["into"] = "_"; 512567582 q attributes["type"] = ""; 532567582 q eval_attributes_array(get_name(), attributes, input_attributes); 552567582 q set_output(attributes["into"]); 562567582 q if (attributes["type"] == "node") 582092499 q else if (attributes["type"] == "way") 601494396 q else if (attributes["type"] == "relation") 621032304 q else if (attributes["type"] == "derived") 630 type = QUERY_DERIVED; 641032304 q else if (attributes["type"] == "nwr") 65356115 q type = (QUERY_NODE | QUERY_WAY | QUERY_RELATION); 66676212 q else if (attributes["type"] == "nw") 6728293 q type = (QUERY_NODE | QUERY_WAY); 68394122 q else if (attributes["type"] == "wr") 6920166 q type = (QUERY_WAY | QUERY_RELATION); 7019358 q else if (attributes["type"] == "nr") 710 type = (QUERY_NODE | QUERY_RELATION); 7219358 q else if (attributes["type"] == "area") 7419057 q type = QUERY_AREA | QUERY_WAY | QUERY_CLOSED_WAY; 7519057 q area_query_exists_ = true; 8031 q std::ostringstream temp; 8131 q temp<<"For the attribute \"type\" of the element \"query\"" 8231 q <<" the only allowed values are \"node\", \"way\", \"relation\", \"nwr\", \"nw\", \"wr\", \"nr\", or \"area\"."; 8331 q add_static_error(temp.str()); 862567582 q if (global_settings.get_global_bbox_limitation().valid()) 884222 q global_bbox_statement = new Bbox_Query_Statement(global_settings.get_global_bbox_limitation()); 894222 q constraints.push_back(global_bbox_statement->get_query_constraint()); 94Query_Statement::~Query_Statement() 962567582 q delete global_bbox_statement; 100void Query_Statement::add_statement(Statement* statement, std::string text) 1023921574 q assure_no_text(text, this->get_name()); 1043921574 q Has_Kv_Statement* has_kv(dynamic_cast<Has_Kv_Statement*>(statement)); 1071731376 q substatements.push_back(statement); 1091731376 q if (has_kv->get_value() != "") 112879244 q key_values.push_back(std::make_pair< std::string, std::string > 113879244 q (has_kv->get_key(), has_kv->get_value())); 11511736 q key_nvalues.push_back(std::make_pair< std::string, std::string > 11611736 q (has_kv->get_key(), has_kv->get_value())); 118735180 q else if (has_kv->get_key_regex()) 12015345 q if (has_kv->get_straight()) 12115345 q regkey_regexes.push_back(std::make_pair< Regular_Expression*, Regular_Expression* > 12215345 q (has_kv->get_key_regex(), has_kv->get_regex())); 1240 regkey_nregexes.push_back(std::make_pair< Regular_Expression*, Regular_Expression* > 1250 (has_kv->get_key_regex(), has_kv->get_regex())); 127582145 q else if (has_kv->get_regex()) 12935796 q if (has_kv->get_straight()) 13016845 q key_regexes.push_back(std::make_pair< std::string, Regular_Expression* > 13116845 q (has_kv->get_key(), has_kv->get_regex())); 13318957 q key_nregexes.push_back(std::make_pair< std::string, Regular_Expression* > 13418957 q (has_kv->get_key(), has_kv->get_regex())); 13722559 q keys.push_back(has_kv->get_key()); 1412190463 q Query_Constraint* constraint = statement->get_query_constraint(); 1442190463 q constraints.push_back(constraint); 1452190463 q substatements.push_back(statement); 1480 substatement_error(get_name(), statement); 152void Query_Statement::get_elements_by_id_from_db 153 (std::map< Uint31_Index, std::vector< Area_Skeleton > >& elements, 154 const std::vector< Area_Skeleton::Id_Type >& ids, bool invert_ids, 155 Resource_Manager& rman, File_Properties& file_prop) 1576153 q Request_Context context(this, rman); 1596053 q collect_items_flat(context, file_prop, 1606053 q Id_Predicate< Area_Skeleton >(ids), elements); 16211 q collect_items_flat(context, file_prop, 16311 q Not_Predicate< Area_Skeleton, Id_Predicate< Area_Skeleton > > 16411 q (Id_Predicate< Area_Skeleton >(ids)), elements); 168template< typename TIndex, typename TObject > 169void clear_empty_indices 170 (std::map< TIndex, std::vector< TObject > >& modify) 1726902567 q for (typename std::map< TIndex, std::vector< TObject > >::iterator it = modify.begin(); 1751201785473 q if (!it->second.empty()) 180546728351 q typename std::map< TIndex, std::vector< TObject > >::iterator next_it = it; 181546728351 q if (++next_it == modify.end()) 188546073335 q TIndex idx = next_it->first; 196template< typename Id_Type > 197void filter_ids_by_ntags 198 (const std::map< std::string, std::pair< std::vector< Regular_Expression* >, std::vector< std::string > > >& keys, 199 const Block_Backend< Tag_Index_Local, Id_Type >& items_db, 200 typename Block_Backend< Tag_Index_Local, Id_Type >::Range_Iterator& tag_it, 202 std::vector< Id_Type >& new_ids) 20422527 q std::vector< Id_Type > removed_ids; 20522527 q std::string last_key, last_value; 20622527 q bool key_relevant = false; 20822527 q std::map< std::string, std::pair< std::vector< Regular_Expression* >, std::vector< std::string > > >::const_iterator 211311188827 q while ((!(tag_it == items_db.range_end())) && 212311188827 q (((tag_it.index().index) & 0x7fffff00) == coarse_index)) 214311188226 q if (tag_it.index().key != last_key) 2162854126 q last_value = void_tag_value() + " "; 22113718 q sort(removed_ids.begin(), removed_ids.end()); 22213718 q removed_ids.erase(unique(removed_ids.begin(), removed_ids.end()), removed_ids.end()); 22313718 q new_ids.erase(set_difference(new_ids.begin(), new_ids.end(), 22413718 q removed_ids.begin(), removed_ids.end(), new_ids.begin()), new_ids.end()); 2282854126 q last_key = tag_it.index().key; 2292868326 q while (key_it != keys.end() && last_key > key_it->first) 2322854126 q if (key_it == keys.end()) 2352832226 q if (last_key == key_it->first) 2442858518 q if (tag_it.index().value != last_value) 24780518 q for (std::vector< Regular_Expression* >::const_iterator rit = key_it->second.first.begin(); 248127418 q rit != key_it->second.first.end(); ++rit) 24946912 q valid |= (tag_it.index().value != void_tag_value() && (*rit)->matches(tag_it.index().value)); 25080518 q for (std::vector< std::string >::const_iterator rit = key_it->second.second.begin(); 251114118 q rit != key_it->second.second.end(); ++rit) 2523367 q valid |= (*rit == tag_it.index().value); 25380518 q last_value = tag_it.index().value; 2571401213 q removed_ids.push_back(tag_it.object()); 262246983127 q while ((!(tag_it == items_db.range_end())) && 263246983127 q (((tag_it.index().index) & 0x7fffff00) == coarse_index)) 26622527 q sort(removed_ids.begin(), removed_ids.end()); 26722527 q removed_ids.erase(unique(removed_ids.begin(), removed_ids.end()), removed_ids.end()); 26822527 q new_ids.erase(set_difference(new_ids.begin(), new_ids.end(), 26922527 q removed_ids.begin(), removed_ids.end(), new_ids.begin()), new_ids.end()); 27122527 q sort(new_ids.begin(), new_ids.end()); 275template< typename Id_Type > 276void filter_ids_by_ntags 277 (const std::map< std::string, std::pair< std::vector< Regular_Expression* >, std::vector< std::string > > >& keys, 279 const Block_Backend< Tag_Index_Local, Id_Type >& items_db, 280 typename Block_Backend< Tag_Index_Local, Id_Type >::Range_Iterator& tag_it, 281 const Block_Backend< Tag_Index_Local, Attic< Id_Type > >& attic_items_db, 282 typename Block_Backend< Tag_Index_Local, Attic< Id_Type > >::Range_Iterator& attic_tag_it, 284 std::vector< Id_Type >& new_ids) 28671632 q for (std::map< std::string, std::pair< std::vector< Regular_Expression* >, std::vector< std::string > > >::const_iterator 287151932 q key_it = keys.begin(); key_it != keys.end(); ++key_it) 28980332 q std::map< Id_Type, std::pair< uint64, uint64 > > timestamps; 29091274032 q for (typename std::vector< Id_Type >::const_iterator it = new_ids.begin(); it != new_ids.end(); ++it) 2931026105632 q while ((!(tag_it == items_db.range_end())) && 2941026105632 q ((tag_it.index().index) & 0x7fffff00) == coarse_index && 2951026105632 q tag_it.index().key < key_it->first) 297855139732 q while ((!(attic_tag_it == attic_items_db.range_end())) && 298855139732 q ((attic_tag_it.index().index) & 0x7fffff00) == coarse_index && 299855139732 q attic_tag_it.index().key < key_it->first) 30380332 q std::string last_value = void_tag_value() + " "; 30413055632 q while ((!(tag_it == items_db.range_end())) && 30513055632 q ((tag_it.index().index) & 0x7fffff00) == coarse_index && 30613055632 q tag_it.index().key == key_it->first) 30812975328 q if (std::binary_search(new_ids.begin(), new_ids.end(), tag_it.object())) 310206316 q std::pair< uint64, uint64 >& timestamp_ref = timestamps[tag_it.object()]; 311206316 q timestamp_ref.second = NOW; 313206316 q if (tag_it.index().value != last_value) 31629516 q for (std::vector< Regular_Expression* >::const_iterator rit = key_it->second.first.begin(); 31753716 q rit != key_it->second.first.end(); ++rit) 31824211 q valid |= (tag_it.index().value != void_tag_value() && (*rit)->matches(tag_it.index().value)); 31929516 q for (std::vector< std::string >::const_iterator rit = key_it->second.second.begin(); 32034816 q rit != key_it->second.second.end(); ++rit) 321536 q valid |= (*rit == tag_it.index().value); 32229516 q last_value = tag_it.index().value; 326125113 q timestamp_ref.first = NOW; 33180332 q last_value = void_tag_value() + " "; 3328118832 q while ((!(attic_tag_it == attic_items_db.range_end())) && 3338118832 q ((attic_tag_it.index().index) & 0x7fffff00) == coarse_index && 3348118832 q attic_tag_it.index().key == key_it->first) 3368038530 q if (std::binary_search(new_ids.begin(), new_ids.end(), Id_Type(attic_tag_it.object()))) 338108018 q std::pair< uint64, uint64 >& timestamp_ref = timestamps[attic_tag_it.object()]; 339108018 q if (timestamp < attic_tag_it.object().timestamp && 340108018 q (timestamp_ref.second == 0 || timestamp_ref.second > attic_tag_it.object().timestamp)) 34142317 q timestamp_ref.second = attic_tag_it.object().timestamp; 343108018 q if (attic_tag_it.index().value != last_value) 34622218 q if (attic_tag_it.index().value != void_tag_value()) 34815815 q for (std::vector< Regular_Expression* >::const_iterator rit = key_it->second.first.begin(); 34927815 q rit != key_it->second.first.end(); ++rit) 35012011 q valid |= (attic_tag_it.index().value != void_tag_value() 35112011 q && (*rit)->matches(attic_tag_it.index().value)); 35215815 q for (std::vector< std::string >::const_iterator rit = key_it->second.second.begin(); 35319615 q rit != key_it->second.second.end(); ++rit) 354385 q valid |= (*rit == attic_tag_it.index().value); 35622218 q last_value = attic_tag_it.index().value; 359108018 q if (valid && timestamp < attic_tag_it.object().timestamp && 360108018 q (timestamp_ref.first == 0 || timestamp_ref.first > attic_tag_it.object().timestamp)) 36114110 q timestamp_ref.first = attic_tag_it.object().timestamp; 36780332 q new_ids.reserve(timestamps.size()); 36880332 q for (typename std::map< Id_Type, std::pair< uint64, uint64 > >::const_iterator 36991274032 q it = timestamps.begin(); it != timestamps.end(); ++it) 37191193728 q if (!(0 < it->second.first && it->second.first <= it->second.second)) 37291075628 q new_ids.push_back(it->first); 375685966832 q while ((!(tag_it == items_db.range_end())) && 376685966832 q ((tag_it.index().index) & 0x7fffff00) == coarse_index) 378868700832 q while ((!(attic_tag_it == attic_items_db.range_end())) && 379868700832 q ((attic_tag_it.index().index) & 0x7fffff00) == coarse_index) 384template< class TIndex, class TObject > 386 const std::map< uint32, std::vector< typename TObject::Id_Type > >& ids_by_coarse, 387 std::map< TIndex, std::vector< TObject > >& items, 388 std::map< TIndex, std::vector< Attic< TObject > > >* attic_items) 390780199 q std::map< TIndex, std::vector< TObject > > result; 391780199 q std::map< TIndex, std::vector< Attic< TObject > > > attic_result; 393780199 q typename std::map< TIndex, std::vector< TObject > >::const_iterator item_it = items.begin(); 3970 for (typename std::map< uint32, std::vector< typename TObject::Id_Type > >::const_iterator 3980 it = ids_by_coarse.begin(); it != ids_by_coarse.end(); ++it) 4000 while ((item_it != items.end()) && 4010 ((item_it->first.val() & 0x7fffff00) == it->first)) 4030 for (typename std::vector< TObject >::const_iterator eit = item_it->second.begin(); 4040 eit != item_it->second.end(); ++eit) 4060 if (std::binary_search(it->second.begin(), it->second.end(), eit->id)) 4070 result[item_it->first.val()].push_back(*eit); 415780199 q typename std::map< TIndex, std::vector< Attic< TObject > > >::const_iterator attic_item_it 418780199 q for (typename std::map< uint32, std::vector< typename TObject::Id_Type > >::const_iterator 4194445199 q it = ids_by_coarse.begin(); it != ids_by_coarse.end(); ++it) 421129639155 q while ((item_it != items.end()) && 422129639155 q ((item_it->first.val() & 0x7fffff00) == it->first)) 424125974153 q for (typename std::vector< TObject >::const_iterator eit = item_it->second.begin(); 4256195580153 q eit != item_it->second.end(); ++eit) 4276069606126 q if (std::binary_search(it->second.begin(), it->second.end(), eit->id)) 428106771097 q result[item_it->first.val()].push_back(*eit); 43342746155 q while ((attic_item_it != attic_items->end()) && 43442746155 q ((attic_item_it->first.val() & 0x7fffff00) == it->first)) 4363908181 q for (typename std::vector< Attic< TObject > >::const_iterator eit = attic_item_it->second.begin(); 43731253481 q eit != attic_item_it->second.end(); ++eit) 43927345363 q if (std::binary_search(it->second.begin(), it->second.end(), eit->id)) 44021294050 q attic_result[attic_item_it->first.val()].push_back(*eit); 449780199 q attic_items->swap(attic_result); 453template< class TIndex, class TObject > 454void Query_Statement::filter_by_tags 455 (std::map< TIndex, std::vector< TObject > >& items, 456 std::map< TIndex, std::vector< Attic< TObject > > >* attic_items, 457 uint64 timestamp, const File_Properties& file_prop, const File_Properties* attic_file_prop, 458 Resource_Manager& rman, Transaction& transaction) 4602019447 q if (keys.empty() && key_values.empty() && key_regexes.empty() && regkey_regexes.empty() 4612019447 q && key_nregexes.empty() && key_nvalues.empty()) 464 // generate set of relevant coarse indices 465780199 q std::map< uint32, std::vector< typename TObject::Id_Type > > ids_by_coarse; 466780199 q generate_ids_by_coarse(ids_by_coarse, items); 46835793 q generate_ids_by_coarse(ids_by_coarse, *attic_items); 470 // formulate range query 471780199 q Ranges< Tag_Index_Local > ranges = formulate_range_query(ids_by_coarse); 473 // prepare straight keys 474780199 q std::map< std::string, std::pair< std::string, std::vector< Regular_Expression* > > > key_union; 475918199 q for (std::vector< std::string >::const_iterator it = keys.begin(); it != keys.end(); 4781134199 q for (std::vector< std::pair< std::string, std::string > >::const_iterator it = key_values.begin(); it != key_values.end(); 48035497 q key_union[it->first].first = it->second; 481780199 q for (std::vector< std::pair< std::string, Regular_Expression* > >::const_iterator 482867199 q it = key_regexes.begin(); it != key_regexes.end(); ++it) 4838723 q key_union[it->first].second.push_back(it->second); 485 // iterate over the result 486780199 q std::map< TIndex, std::vector< TObject > > result; 487780199 q std::map< TIndex, std::vector< Attic< TObject > > > attic_result; 490780199 q Block_Backend< Tag_Index_Local, typename TObject::Id_Type > items_db 491780199 q (transaction.data_index(&file_prop)); 492780199 q auto tag_it = items_db.range_begin(ranges); 496423119 q for (typename std::map< uint32, std::vector< typename TObject::Id_Type > >::iterator 4972358119 q it = ids_by_coarse.begin(); it != ids_by_coarse.end(); ++it) 499193584 q if (++coarse_count >= 1024) 5020 rman.health_check(*this); 504193584 q filter_ids_by_tags(key_union, regkey_regexes, items_db, tag_it, it->first, it->second); 50935793 q Block_Backend< Tag_Index_Local, Attic< typename TObject::Id_Type > > attic_items_db 51035793 q (transaction.data_index(attic_file_prop)); 51135793 q auto attic_tag_it = attic_items_db.range_begin(ranges); 51335793 q for (typename std::map< uint32, std::vector< typename TObject::Id_Type > >::iterator 514208793 q it = ids_by_coarse.begin(); it != ids_by_coarse.end(); ++it) 516173081 q if (++coarse_count >= 1024) 5190 rman.health_check(*this); 521173081 q filter_ids_by_tags(key_union, regkey_regexes, timestamp, items_db, tag_it, attic_items_db, attic_tag_it, 522173081 q it->first, it->second); 526780199 q if (key_nregexes.empty() && key_nvalues.empty()) 528573146 q filter_by_ids(ids_by_coarse, items, attic_items); 532 // prepare negated keys 53320760 q std::map< std::string, std::pair< std::vector< Regular_Expression* >, std::vector< std::string > > > nkey_union; 53420760 q for (std::vector< std::pair< std::string, Regular_Expression* > >::const_iterator 53534860 q it = key_nregexes.begin(); it != key_nregexes.end(); ++it) 53614140 q nkey_union[it->first].first.push_back(it->second); 53720760 q for (std::vector< std::pair< std::string, std::string > >::const_iterator 53829760 q it = key_nvalues.begin(); it != key_nvalues.end(); ++it) 5399025 q nkey_union[it->first].second.push_back(it->second); 541 // iterate over the result 54520760 q auto ntag_it = items_db.range_begin(ranges); 5499029 q for (typename std::map< uint32, std::vector< typename TObject::Id_Type > >::iterator 55031529 q it = ids_by_coarse.begin(); it != ids_by_coarse.end(); ++it) 55222527 q if (++coarse_count >= 1024) 5550 rman.health_check(*this); 55722527 q filter_ids_by_ntags(nkey_union, items_db, ntag_it, it->first, it->second); 56211734 q Block_Backend< Tag_Index_Local, Attic< typename TObject::Id_Type > > attic_items_db 56311734 q (transaction.data_index(attic_file_prop)); 56411734 q auto attic_ntag_it = attic_items_db.range_begin(ranges); 56611734 q for (typename std::map< uint32, std::vector< typename TObject::Id_Type > >::iterator 56783334 q it = ids_by_coarse.begin(); it != ids_by_coarse.end(); ++it) 56971632 q if (++coarse_count >= 1024) 5720 rman.health_check(*this); 57471632 q filter_ids_by_ntags(nkey_union, timestamp, items_db, ntag_it, attic_items_db, attic_ntag_it, 57920760 q filter_by_ids(ids_by_coarse, items, attic_items); 583template< class TIndex, class TObject > 584void Query_Statement::filter_by_tags 585 (std::map< TIndex, std::vector< TObject > >& items, 586 const File_Properties& file_prop, 587 Resource_Manager& rman, Transaction& transaction) 58915389 q if (keys.empty() && key_values.empty() && key_regexes.empty() && regkey_regexes.empty() 59015389 q && key_nregexes.empty() && key_nvalues.empty()) 593 // generate set of relevant coarse indices 5944030 q std::map< uint32, std::vector< typename TObject::Id_Type > > ids_by_coarse; 5954030 q generate_ids_by_coarse(ids_by_coarse, items); 597 // formulate range query 5984030 q Ranges< Tag_Index_Local > ranges = formulate_range_query(ids_by_coarse); 600 // prepare straight keys 6014030 q std::map< std::string, std::pair< std::string, std::vector< Regular_Expression* > > > key_union; 6024630 q for (std::vector< std::string >::const_iterator it = keys.begin(); it != keys.end(); 6056430 q for (std::vector< std::pair< std::string, std::string > >::const_iterator it = key_values.begin(); it != key_values.end(); 6072420 q key_union[it->first].first = it->second; 6084030 q for (std::vector< std::pair< std::string, Regular_Expression* > >::const_iterator 6094630 q it = key_regexes.begin(); it != key_regexes.end(); ++it) 61064 q key_union[it->first].second.push_back(it->second); 612 // iterate over the result 6134030 q std::map< TIndex, std::vector< TObject > > result; 6144030 q std::map< TIndex, std::vector< Attic< TObject > > > attic_result; 6174030 q Block_Backend< Tag_Index_Local, typename TObject::Id_Type > items_db 6184030 q (transaction.data_index(&file_prop)); 6194030 q auto tag_it = items_db.range_begin(ranges); 6214030 q typename std::map< TIndex, std::vector< TObject > >::const_iterator item_it 6254030 q for (typename std::map< uint32, std::vector< typename TObject::Id_Type > >::iterator it = ids_by_coarse.begin(); 6264530 q it != ids_by_coarse.end(); ++it) 62853 q if (++coarse_count >= 1024) 6310 rman.health_check(*this); 63353 q std::vector< typename TObject::Id_Type >& ids_by_coarse_ = it->second; 63553 q filter_ids_by_tags_old(ids_by_coarse_, key_union, regkey_regexes, items_db, tag_it, it->first); 637103 q while ((item_it != items.end()) && 638103 q ((item_it->first.val() & 0x7fffff00) == it->first)) 64053 q for (typename std::vector< TObject >::const_iterator eit = item_it->second.begin(); 641173 q eit != item_it->second.end(); ++eit) 643123 q if (std::binary_search(ids_by_coarse_.begin(), ids_by_coarse_.end(), eit->id)) 644123 q result[item_it->first.val()].push_back(*eit); 6534030 q if (key_nregexes.empty() && key_nvalues.empty()) 656 // prepare negated keys 65776 q std::map< std::string, std::pair< std::vector< Regular_Expression* >, std::vector< std::string > > > nkey_union; 65876 q for (std::vector< std::pair< std::string, Regular_Expression* > >::const_iterator 659136 q it = key_nregexes.begin(); it != key_nregexes.end(); ++it) 66065 q nkey_union[it->first].first.push_back(it->second); 66176 q for (std::vector< std::pair< std::string, std::string > >::const_iterator 662106 q it = key_nvalues.begin(); it != key_nvalues.end(); ++it) 66332 q nkey_union[it->first].second.push_back(it->second); 665 // iterate over the result 66976 q auto ntag_it = items_db.range_begin(ranges); 67076 q item_it = items.begin(); 67376 q for (typename std::map< uint32, std::vector< typename TObject::Id_Type > >::iterator it = ids_by_coarse.begin(); 67476 q it != ids_by_coarse.end(); ++it) 6760 if (++coarse_count >= 1024) 6790 rman.health_check(*this); 6810 std::vector< typename TObject::Id_Type >& ids_by_coarse_ = it->second; 6830 sort(ids_by_coarse_.begin(), ids_by_coarse_.end()); 6850 filter_ids_by_ntags(nkey_union, items_db, ntag_it, it->first, ids_by_coarse_); 6870 while ((item_it != items.end()) && 6880 ((item_it->first.val() & 0x7fffff00) == it->first)) 6900 for (typename std::vector< TObject >::const_iterator eit = item_it->second.begin(); 6910 eit != item_it->second.end(); ++eit) 6930 if (std::binary_search(ids_by_coarse_.begin(), ids_by_coarse_.end(), eit->id)) 6940 result[item_it->first.val()].push_back(*eit); 705template< typename Conditions, typename Tags, typename Single_Match > 706bool matches_all_conditions(const Conditions& conds, const Tags& tags, const Single_Match& lambda) 7080 for (auto it_c = conds.begin(); it_c != conds.end(); ++it_c) 7100 auto it_tags = tags.begin(); 7110 for (; it_tags != tags.end(); ++it_tags) 7130 if (lambda(*it_c, *it_tags)) 7160 if (it_tags == tags.end()) 723template< typename Conditions, typename Tags, typename Single_Match > 724bool matches_some_conditions(const Conditions& conds, const Tags& tags, const Single_Match& lambda) 7260 for (auto it_c = conds.begin(); it_c != conds.end(); ++it_c) 7280 for (auto it_tags = tags.begin(); it_tags != tags.end(); ++it_tags) 7300 if (lambda(*it_c, *it_tags)) 738void Query_Statement::filter_by_tags(std::map< Uint31_Index, std::vector< Derived_Structure > >& items) 7400 typedef std::pair< std::string, std::string > Tag_Pair; 7420 for (std::map< Uint31_Index, std::vector< Derived_Structure > >::iterator it_idx = items.begin(); 7430 it_idx != items.end(); ++it_idx) 7450 std::vector< Derived_Structure > result; 7460 for (std::vector< Derived_Structure >::const_iterator it_elem = it_idx->second.begin(); 7470 it_elem != it_idx->second.end(); ++it_elem) 7490 if (matches_all_conditions(key_values, it_elem->tags, 7500 [](const std::pair< std::string, std::string >& cond, const Tag_Pair& tag) 7510 { return tag.first == cond.first && tag.second == cond.second; }) 7520 && matches_all_conditions(keys, it_elem->tags, 7530 [](const std::string& cond, const Tag_Pair& tag) 7540 { return tag.first == cond; }) 7550 && matches_all_conditions(key_regexes, it_elem->tags, 7560 [](const std::pair< std::string, Regular_Expression* >& cond, const Tag_Pair& tag) 7570 { return tag.first == cond.first && cond.second->matches(tag.second); }) 7580 && !matches_some_conditions(key_nvalues, it_elem->tags, 7590 [](const std::pair< std::string, std::string >& cond, const Tag_Pair& tag) 7600 { return tag.first == cond.first && cond.second == tag.second; }) 7610 && !matches_some_conditions(key_nregexes, it_elem->tags, 7620 [](const std::pair< std::string, Regular_Expression* >& cond, const Tag_Pair& tag) 7630 { return tag.first == cond.first && cond.second->matches(tag.second); })) 7640 result.push_back(*it_elem); 7660 result.swap(it_idx->second); 771template< typename Id > 772void Id_Constraint< Id >::restrict_to(const std::vector< Id >& rhs) 7769960 q std::vector< Id > new_ids(rhs.size()); 7779960 q new_ids.erase(std::set_difference( 7789960 q rhs.begin(), rhs.end(), ids.begin(), ids.end(), new_ids.begin()), new_ids.end()); 7841410 q std::vector< Id > new_ids(std::min(ids.size(), rhs.size())); 7851410 q new_ids.erase(std::set_intersection( 7861410 q ids.begin(), ids.end(), rhs.begin(), rhs.end(), new_ids.begin()), new_ids.end()); 792template< class Id_Type > 793void Query_Statement::collect_nodes( 794 const Id_Constraint< Id_Type >& ids, Answer_State& answer_state, Set& into, Resource_Manager& rman) 796468311 q for (std::vector< Query_Constraint* >::iterator it = constraints.begin(); 797808311 q it != constraints.end() && answer_state < data_collected; ++it) 799340222 q if ((*it)->collect_nodes(rman, into, ids.ids, ids.invert)) 8004034 q answer_state = data_collected; 805template< typename Id_Type > 806void Query_Statement::collect_elems( 807 int type, const Id_Constraint< Id_Type >& ids, Answer_State& answer_state, Set& into, Resource_Manager& rman) 8091020489 q for (std::vector< Query_Constraint* >::iterator it = constraints.begin(); 8101804489 q it != constraints.end() && answer_state < data_collected; ++it) 812784396 q if ((*it)->collect(rman, into, type, ids.ids, ids.invert)) 8137461 q answer_state = data_collected; 818void Query_Statement::collect_elems(Answer_State& answer_state, Set& into, Resource_Manager& rman) 8200 for (std::vector< Query_Constraint* >::iterator it = constraints.begin(); 8210 it != constraints.end() && answer_state < data_collected; ++it) 8230 if ((*it)->collect(rman, into)) 8240 answer_state = data_collected; 829template< typename Index > 830Ranges< Index > intersect_ranges(const Ranges< Index >& range_a, std::vector< Index >& range_vec) 835150404216 q for (auto it = range_a.begin(); it != range_a.end(); ++it) 836150066214 q sum += difference(it.lower_bound(), it.upper_bound()); 838338216 q if (sum/256 < range_vec.size()) 841234166 q std::sort(range_vec.begin(), range_vec.end()); 843234166 q auto it_a = range_a.begin(); 844234166 q auto it_vec = range_vec.begin(); 8461022500166 q while (it_a != range_a.end() && it_vec != range_vec.end()) 8481022266166 q if (!(it_a.lower_bound() < Index(it_vec->val() + 0x100))) 8501022209166 q else if (!(*it_vec < it_a.upper_bound())) 852900825161 q else if (Index(it_vec->val() + 0x100) < it_a.upper_bound()) 854900790156 q result.push_back(std::max(it_a.lower_bound(), *it_vec), Index(it_vec->val() + 0x100)); 859357 q result.push_back(std::max(it_a.lower_bound(), *it_vec), it_a.upper_bound()); 868void Query_Statement::apply_all_filters( 869 Resource_Manager& rman, uint64 timestamp, Query_Filter_Strategy check_keys_late, Set& into) 874990567 q for (std::vector< Query_Constraint* >::iterator it = constraints.begin(); 8751904567 q it != constraints.end(); ++it) 881990567 q filter_attic_elements(rman, timestamp, into.nodes, into.attic_nodes); 882990567 q filter_attic_elements(rman, timestamp, into.ways, into.attic_ways); 883990567 q filter_attic_elements(rman, timestamp, into.relations, into.attic_relations); 888990567 q if (check_keys_late == prefer_ranges) 890673447 q filter_by_tags(into.nodes, &into.attic_nodes, timestamp, 891673447 q *osm_base_settings().NODE_TAGS_LOCAL, attic_settings().NODE_TAGS_LOCAL, 892673447 q rman, *rman.get_transaction()); 893673447 q filter_by_tags(into.ways, &into.attic_ways, timestamp, 894673447 q *osm_base_settings().WAY_TAGS_LOCAL, attic_settings().WAY_TAGS_LOCAL, 895673447 q rman, *rman.get_transaction()); 896673447 q filter_by_tags(into.relations, &into.attic_relations, timestamp, 897673447 q *osm_base_settings().RELATION_TAGS_LOCAL, attic_settings().RELATION_TAGS_LOCAL, 898673447 q rman, *rman.get_transaction()); 899673447 q if (rman.get_area_transaction()) 90015389 q filter_by_tags(into.areas, 90115389 q *area_settings().AREA_TAGS_LOCAL, 90215389 q rman, *rman.get_transaction()); 908990567 q for (std::vector< Query_Constraint* >::iterator it = constraints.begin(); 9091904567 q it != constraints.end(); ++it) 910914469 q (*it)->filter(*this, rman, into); 914void filter_elems_for_closed_ways(Set& arg) 91613457 q filter_elems_for_closed_ways(arg.ways); 91713457 q filter_elems_for_closed_ways(arg.attic_ways); 921void Query_Statement::execute(Resource_Manager& rman) 925986567 q Answer_State node_answer_state = nothing; 926986567 q Answer_State way_answer_state = nothing; 927986567 q Answer_State relation_answer_state = nothing; 928986567 q Answer_State area_answer_state = nothing; 929986567 q Answer_State derived_answer_state = nothing; 932986567 q uint64 timestamp = rman.get_desired_timestamp(); 939986567 q Query_Filter_Strategy check_keys_late = ids_required; 9401897567 q for (std::vector< Query_Constraint* >::iterator it = constraints.begin(); it != constraints.end(); ++it) 941911469 q check_keys_late = std::max(check_keys_late, (*it)->delivers_data(rman)); 944986567 q Id_Constraint< Node::Id_Type > node_ids; 945986567 q Id_Constraint< Way::Id_Type > way_ids; 946986567 q Id_Constraint< Relation::Id_Type > relation_ids; 947986567 q Id_Constraint< Area_Skeleton::Id_Type > area_ids; 949986567 q std::vector< Uint32_Index > range_vec_32; 950986567 q std::vector< Uint31_Index > way_range_vec_31; 951986567 q std::vector< Uint31_Index > relation_range_vec_31; 955468311 q progress_1< Node_Skeleton, Node::Id_Type, Uint32_Index >( 956468311 q keys, key_values, key_regexes, regkey_regexes, key_nvalues, key_nregexes, regkey_nregexes, 957468311 q node_ids, range_vec_32, timestamp, check_keys_late, rman, *this); 95912056 q node_answer_state = data_collected; 960468311 q collect_nodes(node_ids, node_answer_state, into, rman); 964592413 q progress_1< Way_Skeleton, Way::Id_Type, Uint31_Index >( 965592413 q keys, key_values, key_regexes, regkey_regexes, key_nvalues, key_nregexes, regkey_nregexes, 966592413 q way_ids, way_range_vec_31, timestamp, check_keys_late, rman, *this); 9687969 q way_answer_state = data_collected; 969592413 q collect_elems(QUERY_WAY, way_ids, way_answer_state, into, rman); 970592413 q if (type & QUERY_CLOSED_WAY) 9716757 q filter_elems_for_closed_ways(into); 975361281 q progress_1< Relation_Skeleton, Relation::Id_Type, Uint31_Index >( 976361281 q keys, key_values, key_regexes, regkey_regexes, key_nvalues, key_nregexes, regkey_nregexes, 977361281 q relation_ids, relation_range_vec_31, timestamp, check_keys_late, rman, *this); 9797562 q relation_answer_state = data_collected; 980361281 q collect_elems(QUERY_RELATION, relation_ids, relation_answer_state, into, rman); 9840 collect_elems(derived_answer_state, into, rman); 9850 filter_by_tags(into.deriveds); 9926757 q keys, key_values, key_regexes, regkey_regexes, key_nvalues, key_nregexes, regkey_nregexes, 9936757 q area_ids, check_keys_late, rman, *this); 99565 q area_answer_state = data_collected; 9966757 q collect_elems(QUERY_AREA, area_ids, area_answer_state, into, rman); 9986757 q catch (const File_Error& e) 10000 if (e.error_number != ENOENT) 10020 area_answer_state = data_collected; 1009986567 q if ((type & QUERY_NODE) && check_keys_late == prefer_ranges) 1011286220 q for (std::vector< Query_Constraint* >::iterator it = constraints.begin(); 1012571220 q it != constraints.end() && node_answer_state < data_collected; ++it) 1014285191 q std::vector< Node::Id_Type > constraint_node_ids; 1015285191 q if ((*it)->get_node_ids(rman, constraint_node_ids)) 10172518 q node_ids.restrict_to(constraint_node_ids); 101942 q node_answer_state = data_collected; 1023986567 q if ((type & QUERY_WAY) && check_keys_late == prefer_ranges) 1025414307 q for (std::vector< Query_Constraint* >::iterator it = constraints.begin(); 1026831307 q it != constraints.end() && way_answer_state < data_collected; ++it) 1028417278 q std::vector< Way::Id_Type > constraint_way_ids; 1029417278 q if ((*it)->get_way_ids(rman, constraint_way_ids)) 10313934 q way_ids.restrict_to(constraint_way_ids); 103332 q way_answer_state = data_collected; 1037986567 q if ((type & QUERY_RELATION) && check_keys_late == prefer_ranges) 1039246201 q for (std::vector< Query_Constraint* >::iterator it = constraints.begin(); 1040480201 q it != constraints.end() && relation_answer_state < data_collected; ++it) 1042234179 q std::vector< Relation::Id_Type > constraint_relation_ids; 1043234179 q if ((*it)->get_relation_ids(rman, constraint_relation_ids)) 10452621 q relation_ids.restrict_to(constraint_relation_ids); 104742 q relation_answer_state = data_collected; 1051986567 q if ((type & QUERY_AREA) && check_keys_late == prefer_ranges) 10532824 q for (std::vector< Query_Constraint* >::iterator it = constraints.begin(); 10545624 q it != constraints.end() && area_answer_state < data_collected; ++it) 10562823 q std::vector< Area_Skeleton::Id_Type > constraint_area_ids; 10572823 q if ((*it)->get_area_ids(rman, constraint_area_ids)) 10592320 q area_ids.restrict_to(constraint_area_ids); 10610 area_answer_state = data_collected; 1066986567 q Ranges< Uint32_Index > node_ranges = Ranges< Uint32_Index >::global(); 1067986567 q Ranges< Uint31_Index > way_ranges = Ranges< Uint31_Index >::global(); 1068986567 q Ranges< Uint31_Index > rel_ranges = Ranges< Uint31_Index >::global(); 1070986567 q if ((type & QUERY_NODE) && node_answer_state < data_collected) 1072304250 q for (std::vector< Query_Constraint* >::iterator it = constraints.begin(); 1074290200 q node_ranges.intersect((*it)->get_node_ranges(rman)).swap(node_ranges); 107710084 q intersect_ranges(node_ranges, range_vec_32).swap(node_ranges); 1079986567 q if ((type & QUERY_WAY) && way_answer_state < data_collected) 1081468357 q for (std::vector< Query_Constraint* >::iterator it = constraints.begin(); 1083426288 q way_ranges.intersect((*it)->get_way_ranges(rman)).swap(way_ranges); 1086164142 q intersect_ranges(way_ranges, way_range_vec_31).swap(way_ranges); 1088986567 q if ((type & QUERY_RELATION) && relation_answer_state < data_collected) 1090250225 q for (std::vector< Query_Constraint* >::iterator it = constraints.begin(); 1092247194 q rel_ranges.intersect((*it)->get_relation_ranges(rman)).swap(rel_ranges); 1094250225 q if (!relation_range_vec_31.empty()) 10957465 q intersect_ranges(rel_ranges, relation_range_vec_31).swap(rel_ranges); 1103468311 q for (std::vector< Query_Constraint* >::iterator it = constraints.begin(); 1104758311 q it != constraints.end() && node_answer_state < data_collected; ++it) 1106290200 q if ((*it)->get_data(*this, rman, into, node_ranges, node_ids.ids, node_ids.invert)) 1107119 q node_answer_state = data_collected; 1112592413 q for (std::vector< Query_Constraint* >::iterator it = constraints.begin(); 11131018413 q it != constraints.end() && way_answer_state < data_collected; ++it) 1115426288 q if ((*it)->get_data(*this, rman, into, way_ranges, type & QUERY_WAY, way_ids.ids, way_ids.invert)) 11180 filter_elems_for_closed_ways(into); 11192523 q way_answer_state = data_collected; 1125361281 q for (std::vector< Query_Constraint* >::iterator it = constraints.begin(); 1126608281 q it != constraints.end() && relation_answer_state < data_collected; ++it) 1128247194 q if ((*it)->get_data(*this, rman, into, rel_ranges, type & QUERY_RELATION, 1129247194 q relation_ids.ids, relation_ids.invert)) 11303030 q relation_answer_state = data_collected; 11356757 q for (std::vector< Query_Constraint* >::iterator it = constraints.begin(); 11369557 q it != constraints.end() && area_answer_state < data_collected; ++it) 11382823 q if ((*it)->get_data(*this, rman, into, {}, type & QUERY_AREA, area_ids.ids, area_ids.invert)) 11390 area_answer_state = data_collected; 1148468311 q if (node_answer_state < data_collected) 115522 q runtime_error("Filters too weak in query statement: specify in addition a bbox, a tag filter, or similar."); 11590 node_ranges = Ranges< Uint32_Index >(); 11600 std::vector< Uint32_Index > req = get_indexes_< Uint32_Index, Node_Skeleton >(node_ids.ids, rman); 11610 for (std::vector< Uint32_Index >::const_iterator it = req.begin(); it != req.end(); ++it) 11620 node_ranges.push_back(*it, ++Uint32_Index(*it)); 11630 node_ranges.sort(); 1167293242 q Collect_Items< Uint32_Index, Node_Skeleton > db_reader( 1168293242 q node_ids.ids, node_ids.invert, node_ranges, *this, rman); 1169293242 q while (db_reader.get_chunk(into.nodes, into.attic_nodes)) 11720 to_filter.nodes.swap(into.nodes); 11730 to_filter.attic_nodes.swap(into.attic_nodes); 11740 apply_all_filters(rman, timestamp, check_keys_late, to_filter); 11750 indexed_set_union(filtered.nodes, to_filter.nodes); 11760 indexed_set_union(filtered.attic_nodes, to_filter.attic_nodes); 1182592413 q if (way_answer_state < data_collected) 118922 q runtime_error("Filters too weak in query statement: specify in addition a bbox, a tag filter, or similar."); 119311 q way_ranges = Ranges< Uint31_Index >(); 119411 q std::vector< Uint31_Index > req = get_indexes_< Uint31_Index, Way_Skeleton >(way_ids.ids, rman); 1195120941 q for (std::vector< Uint31_Index >::const_iterator it = req.begin(); it != req.end(); ++it) 1196120931 q way_ranges.push_back(*it, inc(*it)); 1201443344 q Collect_Items< Uint31_Index, Way_Skeleton > db_reader( 1202443344 q way_ids.ids, way_ids.invert, way_ranges, *this, rman); 1203447344 q while (db_reader.get_chunk(into.ways, into.attic_ways)) 120644 q to_filter.ways.swap(into.ways); 120744 q to_filter.attic_ways.swap(into.attic_ways); 120844 q if (type & QUERY_CLOSED_WAY) 12100 filter_elems_for_closed_ways(to_filter.ways); 12110 filter_elems_for_closed_ways(to_filter.attic_ways); 121344 q apply_all_filters(rman, timestamp, check_keys_late, to_filter); 121444 q indexed_set_union(filtered.ways, to_filter.ways); 121544 q indexed_set_union(filtered.attic_ways, to_filter.attic_ways); 1221361281 q if (relation_answer_state < data_collected) 122711 q rel_ranges = Ranges< Uint31_Index >(); 122811 q std::vector< Uint31_Index > req = get_indexes_< Uint31_Index, Relation_Skeleton >(relation_ids.ids, rman); 1229120711 q for (std::vector< Uint31_Index >::const_iterator it = req.begin(); it != req.end(); ++it) 1230120701 q rel_ranges.push_back(*it, inc(*it)); 1235220201 q Collect_Items< Uint31_Index, Relation_Skeleton > db_reader( 1236220201 q relation_ids.ids, relation_ids.invert, rel_ranges, *this, rman); 1237220201 q while (db_reader.get_chunk(into.relations, into.attic_relations)) 12400 to_filter.relations.swap(into.relations); 12410 to_filter.attic_relations.swap(into.attic_relations); 12420 apply_all_filters(rman, timestamp, check_keys_late, to_filter); 12430 indexed_set_union(filtered.relations, to_filter.relations); 12440 indexed_set_union(filtered.attic_relations, to_filter.attic_relations); 12506757 q if (area_answer_state == nothing && area_ids.empty()) 12510 runtime_error("Filters too weak in query statement: specify in addition a bbox, a tag filter, or similar."); 12546757 q if (area_answer_state < data_collected) 12556153 q get_elements_by_id_from_db(into.areas, area_ids.ids, area_ids.invert, rman, *area_settings().AREAS); 12590 if (e.error_number != ENOENT) 12666757 q filter_elems_for_closed_ways(into); 1267986567 q apply_all_filters(rman, timestamp, check_keys_late, into); 1268986567 q indexed_set_union(into.nodes, filtered.nodes); 1269986567 q indexed_set_union(into.attic_nodes, filtered.attic_nodes); 1270986567 q indexed_set_union(into.ways, filtered.ways); 1271986567 q indexed_set_union(into.attic_ways, filtered.attic_ways); 1272986567 q indexed_set_union(into.relations, filtered.relations); 1273986567 q indexed_set_union(into.attic_relations, filtered.attic_relations); 1274986567 q indexed_set_union(into.areas, filtered.areas); 1275986567 q indexed_set_union(into.deriveds, filtered.deriveds); 1280986567 q clear_empty_indices(into.nodes); 1281986567 q clear_empty_indices(into.attic_nodes); 1283986567 q clear_empty_indices(into.attic_ways); 1284986567 q clear_empty_indices(into.relations); 1285986567 q clear_empty_indices(into.deriveds); 1286986567 q clear_empty_indices(into.areas); 1292//----------------------------------------------------------------------------- 1294Generic_Statement_Maker< Has_Kv_Statement > Has_Kv_Statement::statement_maker("has-kv"); 1296Has_Kv_Statement::Has_Kv_Statement 1297 (int line_number_, const std::map< std::string, std::string >& input_attributes, Parsed_Query& global_settings) 12981731376 q : Statement(line_number_), regex(0), key_regex(0), straight(true), case_sensitive(false) 13001731376 q std::map< std::string, std::string > attributes; 13071731376 q attributes["case"] = "sensitive"; 13091731376 q eval_attributes_array(get_name(), attributes, input_attributes); 13141731376 q if (key == "" && attributes["regk"] == "") 13160 std::ostringstream temp(""); 13170 temp<<"For the attribute \"k\" of the element \"has-kv\"" 13180 <<" the only allowed values are non-empty strings."; 13190 add_static_error(temp.str()); 13221731376 q if (attributes["case"] != "ignore") 13241713376 q if (attributes["case"] != "sensitive") 13250 add_static_error("For the attribute \"case\" of the element \"has-kv\"" 13260 " the only allowed values are \"sensitive\" or \"ignore\"."); 13340 std::ostringstream temp(""); 13350 temp<<"In the element \"has-kv\" only one of the attributes \"k\" and \"regk\"" 13360 " can be nonempty."; 13370 add_static_error(temp.str()); 13410 std::ostringstream temp(""); 13420 temp<<"In the element \"has-kv\" the attribute \"regk\" must be combined with \"regv\"."; 13430 add_static_error(temp.str()); 134815345 q key_regex = new Regular_Expression(attributes["regk"], case_sensitive); 135115345 q catch (Regular_Expression_Error e) 13530 add_static_error("Invalid regular expression: \"" + attributes["regk"] + "\""); 13610 std::ostringstream temp(""); 13620 temp<<"In the element \"has-kv\" only one of the attributes \"v\" and \"regv\"" 13630 " can be nonempty."; 13640 add_static_error(temp.str()); 1369513138 q regex = new Regular_Expression(attributes["regv"], case_sensitive); 1372513138 q catch (Regular_Expression_Error e) 137431 q add_static_error("Invalid regular expression: \"" + attributes["regv"] + "\""); 13781731376 q if (attributes["modv"] == "" || attributes["modv"] == "not") 13801731376 q if (attributes["modv"] == "not") 138230685 q if (attributes["regk"] == "") 13850 add_static_error("In the element \"has-kv\" regular expressions on keys cannot be combined" 13860 " with negation."); 13910 std::ostringstream temp(""); 13920 temp<<"In the element \"has-kv\" the attribute \"modv\" can only be empty or std::set to \"not\"."; 13930 add_static_error(temp.str()); 1398Has_Kv_Statement::~Has_Kv_Statement()