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) 462306522 q : Output_Statement(line_number_), global_bbox_statement(0) 482306522 q std::map< std::string, std::string > attributes; 502306522 q attributes["into"] = "_"; 512306522 q attributes["type"] = ""; 532306522 q eval_attributes_array(get_name(), attributes, input_attributes); 552306522 q set_output(attributes["into"]); 562306522 q if (attributes["type"] == "node") 581958470 q else if (attributes["type"] == "way") 601493394 q else if (attributes["type"] == "relation") 621081311 q else if (attributes["type"] == "derived") 630 type = QUERY_DERIVED; 641081311 q else if (attributes["type"] == "nwr") 65426133 q type = (QUERY_NODE | QUERY_WAY | QUERY_RELATION); 66655208 q else if (attributes["type"] == "nw") 6729799 q type = (QUERY_NODE | QUERY_WAY); 68358113 q else if (attributes["type"] == "wr") 6919665 q type = (QUERY_WAY | QUERY_RELATION); 7016249 q else if (attributes["type"] == "nr") 710 type = (QUERY_NODE | QUERY_RELATION); 7216249 q else if (attributes["type"] == "area") 7415948 q type = QUERY_AREA | QUERY_WAY | QUERY_CLOSED_WAY; 7515948 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()); 862306522 q if (global_settings.get_global_bbox_limitation().valid()) 883318 q global_bbox_statement = new Bbox_Query_Statement(global_settings.get_global_bbox_limitation()); 893318 q constraints.push_back(global_bbox_statement->get_query_constraint()); 94Query_Statement::~Query_Statement() 962306522 q delete global_bbox_statement; 100void Query_Statement::add_statement(Statement* statement, std::string text) 1023423518 q assure_no_text(text, this->get_name()); 1043423518 q Has_Kv_Statement* has_kv(dynamic_cast<Has_Kv_Statement*>(statement)); 1071599333 q substatements.push_back(statement); 1091599333 q if (has_kv->get_value() != "") 112771207 q key_values.push_back(std::make_pair< std::string, std::string > 113771207 q (has_kv->get_key(), has_kv->get_value())); 11514445 q key_nvalues.push_back(std::make_pair< std::string, std::string > 11614445 q (has_kv->get_key(), has_kv->get_value())); 118684166 q else if (has_kv->get_key_regex()) 12014442 q if (has_kv->get_straight()) 12114442 q regkey_regexes.push_back(std::make_pair< Regular_Expression*, Regular_Expression* > 12214442 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())); 127540135 q else if (has_kv->get_regex()) 12932190 q if (has_kv->get_straight()) 13014141 q key_regexes.push_back(std::make_pair< std::string, Regular_Expression* > 13114141 q (has_kv->get_key(), has_kv->get_regex())); 13318053 q key_nregexes.push_back(std::make_pair< std::string, Regular_Expression* > 13418053 q (has_kv->get_key(), has_kv->get_regex())); 13721959 q keys.push_back(has_kv->get_key()); 1411824405 q Query_Constraint* constraint = statement->get_query_constraint(); 1441824405 q constraints.push_back(constraint); 1451824405 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) 1575145 q Request_Context context(this, rman); 1595044 q collect_items_flat(context, file_prop, 1605044 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) 1726153507 q for (typename std::map< TIndex, std::vector< TObject > >::iterator it = modify.begin(); 1753529096442 q if (!it->second.empty()) 1802162744334 q typename std::map< TIndex, std::vector< TObject > >::iterator next_it = it; 1812162744334 q if (++next_it == modify.end()) 1882161982320 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) 204127725 q std::vector< Id_Type > removed_ids; 205127725 q std::string last_key, last_value; 206127725 q bool key_relevant = false; 208127725 q std::map< std::string, std::pair< std::vector< Regular_Expression* >, std::vector< std::string > > >::const_iterator 211525748525 q while ((!(tag_it == items_db.range_end())) && 212525748525 q (((tag_it.index().index) & 0x7fffff00) == coarse_index)) 214525740424 q if (tag_it.index().key != last_key) 2164443024 q last_value = void_tag_value() + " "; 22188015 q sort(removed_ids.begin(), removed_ids.end()); 22288015 q removed_ids.erase(unique(removed_ids.begin(), removed_ids.end()), removed_ids.end()); 22388015 q new_ids.erase(set_difference(new_ids.begin(), new_ids.end(), 22488015 q removed_ids.begin(), removed_ids.end(), new_ids.begin()), new_ids.end()); 2284443024 q last_key = tag_it.index().key; 2294483324 q while (key_it != keys.end() && last_key > key_it->first) 2324443024 q if (key_it == keys.end()) 2354323424 q if (last_key == key_it->first) 2448087515 q if (tag_it.index().value != last_value) 247369215 q for (std::vector< Regular_Expression* >::const_iterator rit = key_it->second.first.begin(); 248397615 q rit != key_it->second.first.end(); ++rit) 2492849 q valid |= (tag_it.index().value != void_tag_value() && (*rit)->matches(tag_it.index().value)); 250369215 q for (std::vector< std::string >::const_iterator rit = key_it->second.second.begin(); 251715715 q rit != key_it->second.second.end(); ++rit) 25234656 q valid |= (*rit == tag_it.index().value); 253369215 q last_value = tag_it.index().value; 257281212 q removed_ids.push_back(tag_it.object()); 262120402225 q while ((!(tag_it == items_db.range_end())) && 263120402225 q (((tag_it.index().index) & 0x7fffff00) == coarse_index)) 266127725 q sort(removed_ids.begin(), removed_ids.end()); 267127725 q removed_ids.erase(unique(removed_ids.begin(), removed_ids.end()), removed_ids.end()); 268127725 q new_ids.erase(set_difference(new_ids.begin(), new_ids.end(), 269127725 q removed_ids.begin(), removed_ids.end(), new_ids.begin()), new_ids.end()); 271127725 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) 286387527 q for (std::map< std::string, std::pair< std::vector< Regular_Expression* >, std::vector< std::string > > >::const_iterator 287807227 q key_it = keys.begin(); key_it != keys.end(); ++key_it) 289419727 q std::map< Id_Type, std::pair< uint64, uint64 > > timestamps; 290345291827 q for (typename std::vector< Id_Type >::const_iterator it = new_ids.begin(); it != new_ids.end(); ++it) 293384368727 q while ((!(tag_it == items_db.range_end())) && 294384368727 q ((tag_it.index().index) & 0x7fffff00) == coarse_index && 295384368727 q tag_it.index().key < key_it->first) 297665451827 q while ((!(attic_tag_it == attic_items_db.range_end())) && 298665451827 q ((attic_tag_it.index().index) & 0x7fffff00) == coarse_index && 299665451827 q attic_tag_it.index().key < key_it->first) 303419727 q std::string last_value = void_tag_value() + " "; 30410101127 q while ((!(tag_it == items_db.range_end())) && 30510101127 q ((tag_it.index().index) & 0x7fffff00) == coarse_index && 30610101127 q tag_it.index().key == key_it->first) 3089681420 q if (std::binary_search(new_ids.begin(), new_ids.end(), tag_it.object())) 310518312 q std::pair< uint64, uint64 >& timestamp_ref = timestamps[tag_it.object()]; 311518312 q timestamp_ref.second = NOW; 313518312 q if (tag_it.index().value != last_value) 316108712 q for (std::vector< Regular_Expression* >::const_iterator rit = key_it->second.first.begin(); 317124112 q rit != key_it->second.first.end(); ++rit) 31815411 q valid |= (tag_it.index().value != void_tag_value() && (*rit)->matches(tag_it.index().value)); 319108712 q for (std::vector< std::string >::const_iterator rit = key_it->second.second.begin(); 320202012 q rit != key_it->second.second.end(); ++rit) 3219333 q valid |= (*rit == tag_it.index().value); 322108712 q last_value = tag_it.index().value; 32644810 q timestamp_ref.first = NOW; 331419727 q last_value = void_tag_value() + " "; 33214834927 q while ((!(attic_tag_it == attic_items_db.range_end())) && 33314834927 q ((attic_tag_it.index().index) & 0x7fffff00) == coarse_index && 33414834927 q attic_tag_it.index().key == key_it->first) 33614415222 q if (std::binary_search(new_ids.begin(), new_ids.end(), Id_Type(attic_tag_it.object()))) 3385112312 q std::pair< uint64, uint64 >& timestamp_ref = timestamps[attic_tag_it.object()]; 3395112312 q if (timestamp < attic_tag_it.object().timestamp && 3405112312 q (timestamp_ref.second == 0 || timestamp_ref.second > attic_tag_it.object().timestamp)) 34126207 q timestamp_ref.second = attic_tag_it.object().timestamp; 3435112312 q if (attic_tag_it.index().value != last_value) 34696812 q if (attic_tag_it.index().value != void_tag_value()) 34863211 q for (std::vector< Regular_Expression* >::const_iterator rit = key_it->second.first.begin(); 34979211 q rit != key_it->second.first.end(); ++rit) 3501609 q valid |= (attic_tag_it.index().value != void_tag_value() 3511609 q && (*rit)->matches(attic_tag_it.index().value)); 35263211 q for (std::vector< std::string >::const_iterator rit = key_it->second.second.begin(); 353110411 q rit != key_it->second.second.end(); ++rit) 3544723 q valid |= (*rit == attic_tag_it.index().value); 35696812 q last_value = attic_tag_it.index().value; 3595112312 q if (valid && timestamp < attic_tag_it.object().timestamp && 3605112312 q (timestamp_ref.first == 0 || timestamp_ref.first > attic_tag_it.object().timestamp)) 3613736 q timestamp_ref.first = attic_tag_it.object().timestamp; 367419727 q new_ids.reserve(timestamps.size()); 368419727 q for (typename std::map< Id_Type, std::pair< uint64, uint64 > >::const_iterator 369345291827 q it = timestamps.begin(); it != timestamps.end(); ++it) 371344872123 q if (!(0 < it->second.first && it->second.first <= it->second.second)) 372344841823 q new_ids.push_back(it->first); 375126499127 q while ((!(tag_it == items_db.range_end())) && 376126499127 q ((tag_it.index().index) & 0x7fffff00) == coarse_index) 378336843927 q while ((!(attic_tag_it == attic_items_db.range_end())) && 379336843927 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) 390606154 q std::map< TIndex, std::vector< TObject > > result; 391606154 q std::map< TIndex, std::vector< Attic< TObject > > > attic_result; 393606154 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); 415606154 q typename std::map< TIndex, std::vector< Attic< TObject > > >::const_iterator attic_item_it 418606154 q for (typename std::map< uint32, std::vector< typename TObject::Id_Type > >::const_iterator 41910247154 q it = ids_by_coarse.begin(); it != ids_by_coarse.end(); ++it) 421473905118 q while ((item_it != items.end()) && 422473905118 q ((item_it->first.val() & 0x7fffff00) == it->first)) 424464264115 q for (typename std::vector< TObject >::const_iterator eit = item_it->second.begin(); 42529677164115 q eit != item_it->second.end(); ++eit) 4272921290087 q if (std::binary_search(it->second.begin(), it->second.end(), eit->id)) 428348820367 q result[item_it->first.val()].push_back(*eit); 433109943118 q while ((attic_item_it != attic_items->end()) && 434109943118 q ((attic_item_it->first.val() & 0x7fffff00) == it->first)) 43610030262 q for (typename std::vector< Attic< TObject > >::const_iterator eit = attic_item_it->second.begin(); 43729326162 q eit != attic_item_it->second.end(); ++eit) 43919295934 q if (std::binary_search(it->second.begin(), it->second.end(), eit->id)) 44016674627 q attic_result[attic_item_it->first.val()].push_back(*eit); 449606154 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) 4601629378 q if (keys.empty() && key_values.empty() && key_regexes.empty() && regkey_regexes.empty() 4611629378 q && key_nregexes.empty() && key_nvalues.empty()) 464 // generate set of relevant coarse indices 465606154 q std::map< uint32, std::vector< typename TObject::Id_Type > > ids_by_coarse; 466606154 q generate_ids_by_coarse(ids_by_coarse, items); 46827672 q generate_ids_by_coarse(ids_by_coarse, *attic_items); 470 // formulate range query 471606154 q Ranges< Tag_Index_Local > ranges = formulate_range_query(ids_by_coarse); 473 // prepare straight keys 474606154 q std::map< std::string, std::pair< std::string, std::vector< Regular_Expression* > > > key_union; 475720154 q for (std::vector< std::string >::const_iterator it = keys.begin(); it != keys.end(); 478837154 q for (std::vector< std::pair< std::string, std::string > >::const_iterator it = key_values.begin(); it != key_values.end(); 48023157 q key_union[it->first].first = it->second; 481606154 q for (std::vector< std::pair< std::string, Regular_Expression* > >::const_iterator 482666154 q it = key_regexes.begin(); it != key_regexes.end(); ++it) 4836014 q key_union[it->first].second.push_back(it->second); 485 // iterate over the result 486606154 q std::map< TIndex, std::vector< TObject > > result; 487606154 q std::map< TIndex, std::vector< Attic< TObject > > > attic_result; 490606154 q Block_Backend< Tag_Index_Local, typename TObject::Id_Type > items_db 491606154 q (transaction.data_index(&file_prop)); 492606154 q auto tag_it = items_db.range_begin(ranges); 49633094 q for (typename std::map< uint32, std::vector< typename TObject::Id_Type > >::iterator 497459494 q it = ids_by_coarse.begin(); it != ids_by_coarse.end(); ++it) 499426458 q if (++coarse_count >= 1024) 50222 q rman.health_check(*this); 504426458 q filter_ids_by_tags(key_union, regkey_regexes, items_db, tag_it, it->first, it->second); 50927672 q Block_Backend< Tag_Index_Local, Attic< typename TObject::Id_Type > > attic_items_db 51027672 q (transaction.data_index(attic_file_prop)); 51127672 q auto attic_tag_it = attic_items_db.range_begin(ranges); 51327672 q for (typename std::map< uint32, std::vector< typename TObject::Id_Type > >::iterator 514565372 q it = ids_by_coarse.begin(); it != ids_by_coarse.end(); ++it) 516537762 q if (++coarse_count >= 1024) 5190 rman.health_check(*this); 521537762 q filter_ids_by_tags(key_union, regkey_regexes, timestamp, items_db, tag_it, attic_items_db, attic_tag_it, 522537762 q it->first, it->second); 526606154 q if (key_nregexes.empty() && key_nvalues.empty()) 528417107 q filter_by_ids(ids_by_coarse, items, attic_items); 532 // prepare negated keys 53318956 q std::map< std::string, std::pair< std::vector< Regular_Expression* >, std::vector< std::string > > > nkey_union; 53418956 q for (std::vector< std::pair< std::string, Regular_Expression* > >::const_iterator 53530956 q it = key_nregexes.begin(); it != key_nregexes.end(); ++it) 53612035 q nkey_union[it->first].first.push_back(it->second); 53718956 q for (std::vector< std::pair< std::string, std::string > >::const_iterator 53828556 q it = key_nvalues.begin(); it != key_nvalues.end(); ++it) 5399626 q nkey_union[it->first].second.push_back(it->second); 541 // iterate over the result 54518956 q auto ntag_it = items_db.range_begin(ranges); 5499330 q for (typename std::map< uint32, std::vector< typename TObject::Id_Type > >::iterator 550137030 q it = ids_by_coarse.begin(); it != ids_by_coarse.end(); ++it) 552127725 q if (++coarse_count >= 1024) 5550 rman.health_check(*this); 557127725 q filter_ids_by_ntags(nkey_union, items_db, ntag_it, it->first, it->second); 5629628 q Block_Backend< Tag_Index_Local, Attic< typename TObject::Id_Type > > attic_items_db 5639628 q (transaction.data_index(attic_file_prop)); 5649628 q auto attic_ntag_it = attic_items_db.range_begin(ranges); 5669628 q for (typename std::map< uint32, std::vector< typename TObject::Id_Type > >::iterator 567397128 q it = ids_by_coarse.begin(); it != ids_by_coarse.end(); ++it) 569387527 q if (++coarse_count >= 1024) 5720 rman.health_check(*this); 574387527 q filter_ids_by_ntags(nkey_union, timestamp, items_db, ntag_it, attic_items_db, attic_ntag_it, 575387527 q it->first, it->second); 57918956 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) 5899367 q if (keys.empty() && key_values.empty() && key_regexes.empty() && regkey_regexes.empty() 5909367 q && key_nregexes.empty() && key_nvalues.empty()) 593 // generate set of relevant coarse indices 5943124 q std::map< uint32, std::vector< typename TObject::Id_Type > > ids_by_coarse; 5953124 q generate_ids_by_coarse(ids_by_coarse, items); 597 // formulate range query 5983124 q Ranges< Tag_Index_Local > ranges = formulate_range_query(ids_by_coarse); 600 // prepare straight keys 6013124 q std::map< std::string, std::pair< std::string, std::vector< Regular_Expression* > > > key_union; 6023824 q for (std::vector< std::string >::const_iterator it = keys.begin(); it != keys.end(); 6054924 q for (std::vector< std::pair< std::string, std::string > >::const_iterator it = key_values.begin(); it != key_values.end(); 6071812 q key_union[it->first].first = it->second; 6083124 q for (std::vector< std::pair< std::string, Regular_Expression* > >::const_iterator 6093524 q it = key_regexes.begin(); it != key_regexes.end(); ++it) 61043 q key_union[it->first].second.push_back(it->second); 612 // iterate over the result 6133124 q std::map< TIndex, std::vector< TObject > > result; 6143124 q std::map< TIndex, std::vector< Attic< TObject > > > attic_result; 6173124 q Block_Backend< Tag_Index_Local, typename TObject::Id_Type > items_db 6183124 q (transaction.data_index(&file_prop)); 6193124 q auto tag_it = items_db.range_begin(ranges); 6213124 q typename std::map< TIndex, std::vector< TObject > >::const_iterator item_it 6253124 q for (typename std::map< uint32, std::vector< typename TObject::Id_Type > >::iterator it = ids_by_coarse.begin(); 62673024 q it != ids_by_coarse.end(); ++it) 6286993 q if (++coarse_count >= 1024) 6310 rman.health_check(*this); 6336993 q std::vector< typename TObject::Id_Type >& ids_by_coarse_ = it->second; 6356993 q filter_ids_by_tags_old(ids_by_coarse_, key_union, regkey_regexes, items_db, tag_it, it->first); 63715703 q while ((item_it != items.end()) && 63815703 q ((item_it->first.val() & 0x7fffff00) == it->first)) 6408713 q for (typename std::vector< TObject >::const_iterator eit = item_it->second.begin(); 64186823 q eit != item_it->second.end(); ++eit) 64378113 q if (std::binary_search(ids_by_coarse_.begin(), ids_by_coarse_.end(), eit->id)) 64432113 q result[item_it->first.val()].push_back(*eit); 6533124 q if (key_nregexes.empty() && key_nvalues.empty()) 656 // prepare negated keys 65755 q std::map< std::string, std::pair< std::vector< Regular_Expression* >, std::vector< std::string > > > nkey_union; 65855 q for (std::vector< std::pair< std::string, Regular_Expression* > >::const_iterator 65995 q it = key_nregexes.begin(); it != key_nregexes.end(); ++it) 66043 q nkey_union[it->first].first.push_back(it->second); 66155 q for (std::vector< std::pair< std::string, std::string > >::const_iterator 66285 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 66955 q auto ntag_it = items_db.range_begin(ranges); 67055 q item_it = items.begin(); 67355 q for (typename std::map< uint32, std::vector< typename TObject::Id_Type > >::iterator it = ids_by_coarse.begin(); 67455 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) 7763930 q std::vector< Id > new_ids(rhs.size()); 7773930 q new_ids.erase(std::set_difference( 7783930 q rhs.begin(), rhs.end(), ids.begin(), ids.end(), new_ids.begin()), new_ids.end()); 78473 q std::vector< Id > new_ids(std::min(ids.size(), rhs.size())); 78573 q new_ids.erase(std::set_intersection( 78673 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) 796446299 q for (std::vector< Query_Constraint* >::iterator it = constraints.begin(); 797755299 q it != constraints.end() && answer_state < data_collected; ++it) 799309207 q if ((*it)->collect_nodes(rman, into, ids.ids, ids.invert)) 8002519 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) 809976458 q for (std::vector< Query_Constraint* >::iterator it = constraints.begin(); 8101687458 q it != constraints.end() && answer_state < data_collected; ++it) 812711359 q if ((*it)->collect(rman, into, type, ids.ids, ids.invert)) 8136152 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) 83535137196 q for (auto it = range_a.begin(); it != range_a.end(); ++it) 83634794195 q sum += difference(it.lower_bound(), it.upper_bound()); 838343196 q if (sum/256 < range_vec.size()) 841300180 q std::sort(range_vec.begin(), range_vec.end()); 843300180 q auto it_a = range_a.begin(); 844300180 q auto it_vec = range_vec.begin(); 8461149176180 q while (it_a != range_a.end() && it_vec != range_vec.end()) 8481148876180 q if (!(it_a.lower_bound() < Index(it_vec->val() + 0x100))) 8501148856180 q else if (!(*it_vec < it_a.upper_bound())) 8521132630177 q else if (Index(it_vec->val() + 0x100) < it_a.upper_bound()) 8541132563175 q result.push_back(std::max(it_a.lower_bound(), *it_vec), Index(it_vec->val() + 0x100)); 859674 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) 874879507 q for (std::vector< Query_Constraint* >::iterator it = constraints.begin(); 8751630507 q it != constraints.end(); ++it) 881879507 q filter_attic_elements(rman, timestamp, into.nodes, into.attic_nodes); 882879507 q filter_attic_elements(rman, timestamp, into.ways, into.attic_ways); 883879507 q filter_attic_elements(rman, timestamp, into.relations, into.attic_relations); 888879507 q if (check_keys_late == prefer_ranges) 890543378 q filter_by_tags(into.nodes, &into.attic_nodes, timestamp, 891543378 q *osm_base_settings().NODE_TAGS_LOCAL, attic_settings().NODE_TAGS_LOCAL, 892543378 q rman, *rman.get_transaction()); 893543378 q filter_by_tags(into.ways, &into.attic_ways, timestamp, 894543378 q *osm_base_settings().WAY_TAGS_LOCAL, attic_settings().WAY_TAGS_LOCAL, 895543378 q rman, *rman.get_transaction()); 896543378 q filter_by_tags(into.relations, &into.attic_relations, timestamp, 897543378 q *osm_base_settings().RELATION_TAGS_LOCAL, attic_settings().RELATION_TAGS_LOCAL, 898543378 q rman, *rman.get_transaction()); 899543378 q if (rman.get_area_transaction()) 9009367 q filter_by_tags(into.areas, 9019367 q *area_settings().AREA_TAGS_LOCAL, 9029367 q rman, *rman.get_transaction()); 908879507 q for (std::vector< Query_Constraint* >::iterator it = constraints.begin(); 9091630507 q it != constraints.end(); ++it) 910751401 q (*it)->filter(*this, rman, into); 914void filter_elems_for_closed_ways(Set& arg) 91610848 q filter_elems_for_closed_ways(arg.ways); 91710848 q filter_elems_for_closed_ways(arg.attic_ways); 921void Query_Statement::execute(Resource_Manager& rman) 925879507 q Answer_State node_answer_state = nothing; 926879507 q Answer_State way_answer_state = nothing; 927879507 q Answer_State relation_answer_state = nothing; 928879507 q Answer_State area_answer_state = nothing; 929879507 q Answer_State derived_answer_state = nothing; 932879507 q uint64 timestamp = rman.get_desired_timestamp(); 939879507 q Query_Filter_Strategy check_keys_late = ids_required; 9401630507 q for (std::vector< Query_Constraint* >::iterator it = constraints.begin(); it != constraints.end(); ++it) 941751401 q check_keys_late = std::max(check_keys_late, (*it)->delivers_data(rman)); 944879507 q Id_Constraint< Node::Id_Type > node_ids; 945879507 q Id_Constraint< Way::Id_Type > way_ids; 946879507 q Id_Constraint< Relation::Id_Type > relation_ids; 947879507 q Id_Constraint< Area_Skeleton::Id_Type > area_ids; 949879507 q std::vector< Uint32_Index > range_vec_32; 950879507 q std::vector< Uint31_Index > way_range_vec_31; 951879507 q std::vector< Uint31_Index > relation_range_vec_31; 955446299 q progress_1< Node_Skeleton, Node::Id_Type, Uint32_Index >( 956446299 q keys, key_values, key_regexes, regkey_regexes, key_nvalues, key_nregexes, regkey_nregexes, 957446299 q node_ids, range_vec_32, timestamp, check_keys_late, rman, *this); 95911152 q node_answer_state = data_collected; 960446299 q collect_nodes(node_ids, node_answer_state, into, rman); 964562389 q progress_1< Way_Skeleton, Way::Id_Type, Uint31_Index >( 965562389 q keys, key_values, key_regexes, regkey_regexes, key_nvalues, key_nregexes, regkey_nregexes, 966562389 q way_ids, way_range_vec_31, timestamp, check_keys_late, rman, *this); 9685548 q way_answer_state = data_collected; 969562389 q collect_elems(QUERY_WAY, way_ids, way_answer_state, into, rman); 970562389 q if (type & QUERY_CLOSED_WAY) 9715448 q filter_elems_for_closed_ways(into); 975360279 q progress_1< Relation_Skeleton, Relation::Id_Type, Uint31_Index >( 976360279 q keys, key_values, key_regexes, regkey_regexes, key_nvalues, key_nregexes, regkey_nregexes, 977360279 q relation_ids, relation_range_vec_31, timestamp, check_keys_late, rman, *this); 9796457 q relation_answer_state = data_collected; 980360279 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); 9925448 q keys, key_values, key_regexes, regkey_regexes, key_nvalues, key_nregexes, regkey_nregexes, 9935448 q area_ids, check_keys_late, rman, *this); 99533 q area_answer_state = data_collected; 9965448 q collect_elems(QUERY_AREA, area_ids, area_answer_state, into, rman); 9985448 q catch (const File_Error& e) 10000 if (e.error_number != ENOENT) 10020 area_answer_state = data_collected; 1009879507 q if ((type & QUERY_NODE) && check_keys_late == prefer_ranges) 1011255205 q for (std::vector< Query_Constraint* >::iterator it = constraints.begin(); 1012531205 q it != constraints.end() && node_answer_state < data_collected; ++it) 1014276187 q std::vector< Node::Id_Type > constraint_node_ids; 1015276187 q if ((*it)->get_node_ids(rman, constraint_node_ids)) 10171513 q node_ids.restrict_to(constraint_node_ids); 101942 q node_answer_state = data_collected; 1023879507 q if ((type & QUERY_WAY) && check_keys_late == prefer_ranges) 1025349268 q for (std::vector< Query_Constraint* >::iterator it = constraints.begin(); 1026706268 q it != constraints.end() && way_answer_state < data_collected; ++it) 1028357251 q std::vector< Way::Id_Type > constraint_way_ids; 1029357251 q if ((*it)->get_way_ids(rman, constraint_way_ids)) 10311512 q way_ids.restrict_to(constraint_way_ids); 103342 q way_answer_state = data_collected; 1037879507 q if ((type & QUERY_RELATION) && check_keys_late == prefer_ranges) 1039243199 q for (std::vector< Query_Constraint* >::iterator it = constraints.begin(); 1040491199 q it != constraints.end() && relation_answer_state < data_collected; ++it) 1042248187 q std::vector< Relation::Id_Type > constraint_relation_ids; 1043248187 q if ((*it)->get_relation_ids(rman, constraint_relation_ids)) 10451513 q relation_ids.restrict_to(constraint_relation_ids); 104721 q relation_answer_state = data_collected; 1051879507 q if ((type & QUERY_AREA) && check_keys_late == prefer_ranges) 105365 q for (std::vector< Query_Constraint* >::iterator it = constraints.begin(); 1054115 q it != constraints.end() && area_answer_state < data_collected; ++it) 105654 q std::vector< Area_Skeleton::Id_Type > constraint_area_ids; 105754 q if ((*it)->get_area_ids(rman, constraint_area_ids)) 105911 q area_ids.restrict_to(constraint_area_ids); 10610 area_answer_state = data_collected; 1066879507 q Ranges< Uint32_Index > node_ranges = Ranges< Uint32_Index >::global(); 1067879507 q Ranges< Uint31_Index > way_ranges = Ranges< Uint31_Index >::global(); 1068879507 q Ranges< Uint31_Index > rel_ranges = Ranges< Uint31_Index >::global(); 1070879507 q if ((type & QUERY_NODE) && node_answer_state < data_collected) 1072306247 q for (std::vector< Query_Constraint* >::iterator it = constraints.begin(); 1074279192 q node_ranges.intersect((*it)->get_node_ranges(rman)).swap(node_ranges); 10779980 q intersect_ranges(node_ranges, range_vec_32).swap(node_ranges); 1079879507 q if ((type & QUERY_WAY) && way_answer_state < data_collected) 1081465353 q for (std::vector< Query_Constraint* >::iterator it = constraints.begin(); 1083373264 q way_ranges.intersect((*it)->get_way_ranges(rman)).swap(way_ranges); 1086180147 q intersect_ranges(way_ranges, way_range_vec_31).swap(way_ranges); 1088879507 q if ((type & QUERY_RELATION) && relation_answer_state < data_collected) 1090271234 q for (std::vector< Query_Constraint* >::iterator it = constraints.begin(); 1092263200 q rel_ranges.intersect((*it)->get_relation_ranges(rman)).swap(rel_ranges); 1094271234 q if (!relation_range_vec_31.empty()) 10956457 q intersect_ranges(rel_ranges, relation_range_vec_31).swap(rel_ranges); 1103446299 q for (std::vector< Query_Constraint* >::iterator it = constraints.begin(); 1104725299 q it != constraints.end() && node_answer_state < data_collected; ++it) 1106279192 q if ((*it)->get_data(*this, rman, into, node_ranges, node_ids.ids, node_ids.invert)) 11071110 q node_answer_state = data_collected; 1112562389 q for (std::vector< Query_Constraint* >::iterator it = constraints.begin(); 1113935389 q it != constraints.end() && way_answer_state < data_collected; ++it) 1115373264 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); 11192320 q way_answer_state = data_collected; 1125360279 q for (std::vector< Query_Constraint* >::iterator it = constraints.begin(); 1126623279 q it != constraints.end() && relation_answer_state < data_collected; ++it) 1128263200 q if ((*it)->get_data(*this, rman, into, rel_ranges, type & QUERY_RELATION, 1129263200 q relation_ids.ids, relation_ids.invert)) 11302928 q relation_answer_state = data_collected; 11355448 q for (std::vector< Query_Constraint* >::iterator it = constraints.begin(); 11365948 q it != constraints.end() && area_answer_state < data_collected; ++it) 113854 q if ((*it)->get_data(*this, rman, into, {}, type & QUERY_AREA, area_ids.ids, area_ids.invert)) 11390 area_answer_state = data_collected; 1148446299 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(); 1167295241 q Collect_Items< Uint32_Index, Node_Skeleton > db_reader( 1168295241 q node_ids.ids, node_ids.invert, node_ranges, *this, rman); 1169295241 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); 1182562389 q if (way_answer_state < data_collected) 118933 q runtime_error("Filters too weak in query statement: specify in addition a bbox, a tag filter, or similar."); 119322 q way_ranges = Ranges< Uint31_Index >(); 119422 q std::vector< Uint31_Index > req = get_indexes_< Uint31_Index, Way_Skeleton >(way_ids.ids, rman); 119583592 q for (std::vector< Uint31_Index >::const_iterator it = req.begin(); it != req.end(); ++it) 119683572 q way_ranges.push_back(*it, inc(*it)); 1201442343 q Collect_Items< Uint31_Index, Way_Skeleton > db_reader( 1202442343 q way_ids.ids, way_ids.invert, way_ranges, *this, rman); 1203442343 q while (db_reader.get_chunk(into.ways, into.attic_ways)) 12060 to_filter.ways.swap(into.ways); 12070 to_filter.attic_ways.swap(into.attic_ways); 12080 if (type & QUERY_CLOSED_WAY) 12100 filter_elems_for_closed_ways(to_filter.ways); 12110 filter_elems_for_closed_ways(to_filter.attic_ways); 12130 apply_all_filters(rman, timestamp, check_keys_late, to_filter); 12140 indexed_set_union(filtered.ways, to_filter.ways); 12150 indexed_set_union(filtered.attic_ways, to_filter.attic_ways); 1221360279 q if (relation_answer_state < data_collected) 122722 q rel_ranges = Ranges< Uint31_Index >(); 122822 q std::vector< Uint31_Index > req = get_indexes_< Uint31_Index, Relation_Skeleton >(relation_ids.ids, rman); 1229101792 q for (std::vector< Uint31_Index >::const_iterator it = req.begin(); it != req.end(); ++it) 1230101772 q rel_ranges.push_back(*it, inc(*it)); 1235242213 q Collect_Items< Uint31_Index, Relation_Skeleton > db_reader( 1236242213 q relation_ids.ids, relation_ids.invert, rel_ranges, *this, rman); 1237242213 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); 12505448 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."); 12545448 q if (area_answer_state < data_collected) 12555145 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) 12665448 q filter_elems_for_closed_ways(into); 1267879507 q apply_all_filters(rman, timestamp, check_keys_late, into); 1268879507 q indexed_set_union(into.nodes, filtered.nodes); 1269879507 q indexed_set_union(into.attic_nodes, filtered.attic_nodes); 1270879507 q indexed_set_union(into.ways, filtered.ways); 1271879507 q indexed_set_union(into.attic_ways, filtered.attic_ways); 1272879507 q indexed_set_union(into.relations, filtered.relations); 1273879507 q indexed_set_union(into.attic_relations, filtered.attic_relations); 1274879507 q indexed_set_union(into.areas, filtered.areas); 1275879507 q indexed_set_union(into.deriveds, filtered.deriveds); 1280879507 q clear_empty_indices(into.nodes); 1281879507 q clear_empty_indices(into.attic_nodes); 1283879507 q clear_empty_indices(into.attic_ways); 1284879507 q clear_empty_indices(into.relations); 1285879507 q clear_empty_indices(into.deriveds); 1286879507 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) 12981599333 q : Statement(line_number_), regex(0), key_regex(0), straight(true), case_sensitive(false) 13001599333 q std::map< std::string, std::string > attributes; 13071599333 q attributes["case"] = "sensitive"; 13091599333 q eval_attributes_array(get_name(), attributes, input_attributes); 13141599333 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()); 13221599333 q if (attributes["case"] != "ignore") 13241581332 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()); 134814442 q key_regex = new Regular_Expression(attributes["regk"], case_sensitive); 135114442 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()); 1369465126 q regex = new Regular_Expression(attributes["regv"], case_sensitive); 1372465126 q catch (Regular_Expression_Error e) 13740 add_static_error("Invalid regular expression: \"" + attributes["regv"] + "\""); 13781599333 q if (attributes["modv"] == "" || attributes["modv"] == "not") 13801599333 q if (attributes["modv"] == "not") 138232488 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()