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 "../../expat/expat_justparse_interface.h" 20#include "../../template_db/dispatcher.h" 21#include "../frontend/console_output.h" 22#include "../frontend/user_interface.h" 23#include "../frontend/web_output.h" 24#include "../osm-backend/clone_database.h" 25#include "../statements/osm_script.h" 26#include "../statements/statement.h" 27#include "resource_manager.h" 28#include "scripting_core.h" 33#include <sys/resource.h> 34#include <sys/select.h> 49int main(int argc, char *argv[]) 51 // read command line arguments 522448612 q std::string db_dir = ""; 532448612 q std::string clone_db_dir = ""; 542448612 q uint log_level = Error_Output::ASSISTING; 552448612 q Debug_Level debug_level = parser_execute; 562448612 q Clone_Settings clone_settings; 582448612 q bool respect_timeout = true; 642448612 q if (!(strncmp(argv[argpos], "--db-dir=", 9))) 66612612 q db_dir = ((std::string)argv[argpos]).substr(9); 690 std::cerr<<"Please provide a directory name after --db-dir=\n"; 72612612 q if (db_dir[db_dir.size()-1] != '/') 751836612 q else if (!(strcmp(argv[argpos], "--quiet"))) 760 log_level = Error_Output::QUIET; 771836612 q else if (!(strcmp(argv[argpos], "--concise"))) 780 log_level = Error_Output::CONCISE; 791836612 q else if (!(strcmp(argv[argpos], "--progress"))) 800 log_level = Error_Output::PROGRESS; 811836612 q else if (!(strcmp(argv[argpos], "--verbose"))) 820 log_level = Error_Output::VERBOSE; 831836612 q else if (!(strcmp(argv[argpos], "--rules"))) 860 respect_timeout = false; 881836612 q else if (!(strcmp(argv[argpos], "--dump-xml"))) 89612612 q debug_level = parser_dump_xml; 901224612 q else if (!(strcmp(argv[argpos], "--dump-pretty-ql"))) 91612612 q debug_level = parser_dump_pretty_map_ql; 92612612 q else if (!(strcmp(argv[argpos], "--dump-compact-ql"))) 93612612 q debug_level = parser_dump_compact_map_ql; 940 else if (!(strcmp(argv[argpos], "--dump-bbox-ql"))) 950 debug_level = parser_dump_bbox_map_ql; 960 else if (!(strncmp(argv[argpos], "--clone=", 8))) 980 clone_db_dir = ((std::string)argv[argpos]).substr(8); 990 if ((clone_db_dir.size() > 0) && (clone_db_dir[clone_db_dir.size()-1] != '/')) 1000 clone_db_dir += '/'; 1020 else if (!(strncmp(argv[argpos], "--clone-file=", 13))) 1030 clone_settings.single_file_name = ((std::string)argv[argpos]).substr(13); 1040 else if (!(strncmp(argv[argpos], "--request=", 10))) 1050 xml_raw = ((std::string)argv[argpos]).substr(10); 1060 else if (!(strncmp(argv[argpos], "--clone-compression=", 20))) 1080 if (std::string(argv[argpos]).substr(20) == "no") 1090 clone_settings.compression_method = File_Blocks_Index_Base::NO_COMPRESSION; 1100 else if (std::string(argv[argpos]).substr(20) == "gz") 1110 clone_settings.compression_method = File_Blocks_Index_Base::ZLIB_COMPRESSION; 1130 else if (std::string(argv[argpos]).substr(20) == "lz4") 1140 clone_settings.compression_method = File_Blocks_Index_Base::LZ4_COMPRESSION; 1190 std::cerr<<"For --clone-compression, please use \"no\", \"gz\", or \"lz4\" as value.\n"; 121 std::cerr<<"For --clone-compression, please use \"no\" or \"gz\" as value.\n"; 1260 else if (!(strncmp(argv[argpos], "--clone-map-compression=", 24))) 1280 if (std::string(argv[argpos]).substr(24) == "no") 1290 clone_settings.map_compression_method = File_Blocks_Index_Base::NO_COMPRESSION; 1300 else if (std::string(argv[argpos]).substr(24) == "gz") 1310 clone_settings.map_compression_method = File_Blocks_Index_Base::ZLIB_COMPRESSION; 1330 else if (std::string(argv[argpos]).substr(24) == "lz4") 1340 clone_settings.map_compression_method = File_Blocks_Index_Base::LZ4_COMPRESSION; 1390 std::cerr<<"For --clone-map-compression, please use \"no\", \"gz\", or \"lz4\" as value.\n"; 141 std::cerr<<"For --clone-map-compression, please use \"no\" or \"gz\" as value.\n"; 1460 else if (!(strcmp(argv[argpos], "--version"))) 1480 std::cout<<"Overpass API version "<<basic_settings().version<<" "<<basic_settings().source_hash<<"\n"; 1530 std::cout<<"Unknown argument: "<<argv[argpos]<<"\n\n" 1540 "Accepted arguments are:\n" 1550 " --db-dir=$DB_DIR: The directory where the database resides. If you set this parameter\n" 1560 " then osm3s_query will read from the database without using the dispatcher management.\n" 1570 " --dump-xml: Don't execute the query but only dump the query in XML format.\n" 1580 " --dump-pretty-ql: Don't execute the query but only dump the query in pretty QL format.\n" 1590 " --dump-compact-ql: Don't execute the query but only dump the query in compact QL format.\n" 1600 " --dump-bbox-ql: Don't execute the query but only dump the query in a suitable form\n" 1610 " for an OpenLayers slippy map.\n" 1620 " --clone=$TARGET_DIR: Write a consistent copy of the entire database to the given $TARGET_DIR.\n" 1630 " --clone-compression=$METHOD: Use a specific compression method $METHOD for clone bin files\n" 1640 " --clone-map-compression=$METHOD: Use a specific compression method $METHOD for clone map files\n" 1650 " --clone-file=$FILENAME: Restrict cloning to the given file name (provided without directories).\n" 1660 " --rules: Ignore all time limits and allow area creation by this query.\n" 1670 " --request=$QL: Use $QL instead of standard input as the request text.\n" 1680 " --quiet: Don't print anything on stderr.\n" 1690 " --concise: Print concise information on stderr.\n" 1700 " --progress: Print also progress information on stderr.\n" 1710 " --verbose: Print everything that happens on stderr.\n" 1720 " --version: Print version and exit.\n"; 1792448612 q Error_Output* error_output(new Console_Output(log_level)); 1802448612 q Statement::set_error_output(error_output); 182 // connect to dispatcher and get database dir 1852448612 q Parsed_Query global_settings; 188 // open read transaction and log this. 1890 area_level = determine_area_level(error_output, area_level); 1900 Dispatcher_Stub dispatcher( 1910 db_dir, error_output, "-- clone database --", 1920 area_level, 24*60*60, 1024*1024*1024, global_settings); 1930 copy_file(dispatcher.resource_manager().get_transaction()->get_db_dir() + "/replicate_id", 1940 clone_db_dir + "/replicate_id"); 1960 clone_database(*dispatcher.resource_manager().get_transaction(), clone_db_dir, clone_settings); 2022448612 q xml_raw = get_xml_console(error_output); 2042448612 q if ((error_output) && (error_output->display_encoding_errors())) 2072448612 q Statement::Factory stmt_factory(global_settings); 2082448612 q if (!parse_and_validate(stmt_factory, global_settings, xml_raw, error_output, debug_level)) 2102358597 q if (debug_level != parser_execute) 213595595 q Osm_Script_Statement* osm_script = 0; 214595595 q if (!get_statement_stack()->empty()) 215595595 q osm_script = dynamic_cast< Osm_Script_Statement* >(get_statement_stack()->front()); 217595595 q uint32 max_allowed_time = 0; 218595595 q uint64 max_allowed_space = 0; 221595595 q max_allowed_time = osm_script->get_max_allowed_time(); 222595595 q max_allowed_space = osm_script->get_max_allowed_space(); 2260 Osm_Script_Statement temp(0, std::map< std::string, std::string >(), global_settings); 2270 max_allowed_time = temp.get_max_allowed_time(); 2280 max_allowed_space = temp.get_max_allowed_space(); 231 // Allow rules to run for unlimited time 2330 max_allowed_time = 0; 236595595 q error_output->runtime_remark( 237595595 q "Timeout is " + to_string(max_allowed_time) + " and maxsize is " + to_string(max_allowed_space) + "."); 239 // open read transaction and log this. 240595595 q area_level = determine_area_level(error_output, area_level); 242595595 q db_dir, error_output, xml_raw, 243595595 q area_level, max_allowed_time, max_allowed_space, global_settings); 244595595 q if (osm_script && osm_script->get_desired_timestamp()) 245595595 q dispatcher.resource_manager().set_desired_timestamp(osm_script->get_desired_timestamp()); 247595595 q Web_Output web_output(log_level); 248595595 q web_output.set_output_handler(global_settings.get_output_handler()); 249595595 q web_output.write_payload_header("", dispatcher.get_timestamp(), 250595595 q area_level > 0 ? dispatcher.get_area_timestamp() : "", false); 252595595 q dispatcher.resource_manager().start_cpu_timer(0); 253595595 q for (std::vector< Statement* >::const_iterator it(get_statement_stack()->begin()); 2541190595 q it != get_statement_stack()->end(); ++it) 255595595 q (*it)->execute(dispatcher.resource_manager()); 256595595 q dispatcher.resource_manager().stop_cpu_timer(0); 259 /*if (osm_script && osm_script->get_type() == "custom") 261 uint32 count = osm_script->get_written_elements_count(); 264 web_output.write_html_header 265 (dispatcher.get_timestamp(), 266 area_level > 0 ? dispatcher.get_area_timestamp() : ""); 267 std::cout<<"<p>No results found.</p>\n"; 268 web_output.write_footer(); 272 std::cout<<"Status: 302 Moved\n"; 273 std::cout<<"Location: " 274 <<osm_script->adapt_url("http://www.openstreetmap.org/browse/{{{type}}}/{{{id}}}") 279 web_output.write_html_header 280 (dispatcher.get_timestamp(), 281 area_level > 0 ? dispatcher.get_area_timestamp() : ""); 282 osm_script->write_output(); 283 web_output.write_footer(); 286 else if (osm_script && osm_script->get_type() == "popup") 288 web_output.write_html_header 289 (dispatcher.get_timestamp(), 290 area_level > 0 ? dispatcher.get_area_timestamp() : "", 200, 291 osm_script->template_contains_js(), false); 292 osm_script->write_output(); 293 web_output.write_footer(); 3020 if (e.origin != "Dispatcher_Stub::Dispatcher_Stub::1") 3040 std::ostringstream temp; 3060 if (e.origin == "Dispatcher_Client::1") 3070 temp<<"The dispatcher (i.e. the database management system) is turned off."; 3080 else if (e.error_number == 0) 3090 temp<<"open64: "<<e.filename<<' '<<e.origin; 3110 temp<<"open64: "<<e.error_number<<' '<<strerror(e.error_number)<<' '<<e.filename<<' '<<e.origin; 3140 error_output->runtime_error(temp.str()); 3200 std::ostringstream temp; 3220 temp<<"Query timed out in \""<<e.stmt_name<<"\" at line "<<e.line_number 3230 <<" after "<<e.runtime<<" seconds."; 3250 temp<<"Query run out of memory in \""<<e.stmt_name<<"\" at line " 3260 <<e.line_number<<" using about "<<e.size/(1024*1024)<<" MB of RAM."; 3280 error_output->runtime_error(temp.str()); 3340 error_output->runtime_error("Context error: " + e.message); 3440 getrlimit(RLIMIT_AS, &limit); 3450 std::ostringstream temp; 3460 temp<<"Query run out of memory using about "<<limit.rlim_cur/(1024*1024)<<" MB of RAM."; 3470 error_output->runtime_error(temp.str()); 3510 error_output->runtime_error(std::string("Query failed with the exception: ") + e.what());