Overpass API regression suite coverage (Saxony) / overpass_api/dispatch/osm3s_query.cc

overpass_api/dispatch/osm3s_query.cc

90 of 235 instrumented lines run by the suite (38.3%) at 8edf11b0419c (this file on GitHub).

runnever runcolumns: line, executions, queries (click for the queries that run the line)

1/** Copyright 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018 Roland Olbricht et al.
2 *
3 * This file is part of Overpass_API.
4 *
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.
9 *
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.
14 *
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/>.
17 */
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"
30#include <errno.h>
31#include <fcntl.h>
32#include <sys/mman.h>
33#include <sys/resource.h>
34#include <sys/select.h>
35#include <sys/stat.h>
36#include <sys/time.h>
37#include <sys/types.h>
38#include <unistd.h>
40#include <cstdlib>
41#include <cstring>
42#include <fstream>
43#include <iostream>
44#include <sstream>
45#include <string>
46#include <vector>
49int main(int argc, char *argv[])
502768692 q{
51 // read command line arguments
522768692 q std::string db_dir = "";
532768692 q std::string clone_db_dir = "";
542768692 q uint log_level = Error_Output::ASSISTING;
552768692 q Debug_Level debug_level = parser_execute;
562768692 q Clone_Settings clone_settings;
572768692 q int area_level = 0;
582768692 q bool respect_timeout = true;
592768692 q std::string xml_raw;
612768692 q int argpos = 1;
625536692 q while (argpos < argc)
632768692 q {
642768692 q if (!(strncmp(argv[argpos], "--db-dir=", 9)))
65692692 q {
66692692 q db_dir = ((std::string)argv[argpos]).substr(9);
67692692 q if (db_dir.empty())
680 {
690 std::cerr<<"Please provide a directory name after --db-dir=\n";
700 return 0;
710 }
72692692 q if (db_dir[db_dir.size()-1] != '/')
730 db_dir += '/';
74692692 q }
752076692 q else if (!(strcmp(argv[argpos], "--quiet")))
760 log_level = Error_Output::QUIET;
772076692 q else if (!(strcmp(argv[argpos], "--concise")))
780 log_level = Error_Output::CONCISE;
792076692 q else if (!(strcmp(argv[argpos], "--progress")))
800 log_level = Error_Output::PROGRESS;
812076692 q else if (!(strcmp(argv[argpos], "--verbose")))
820 log_level = Error_Output::VERBOSE;
832076692 q else if (!(strcmp(argv[argpos], "--rules")))
840 {
850 area_level = 2;
860 respect_timeout = false;
870 }
882076692 q else if (!(strcmp(argv[argpos], "--dump-xml")))
89692692 q debug_level = parser_dump_xml;
901384692 q else if (!(strcmp(argv[argpos], "--dump-pretty-ql")))
91692692 q debug_level = parser_dump_pretty_map_ql;
92692692 q else if (!(strcmp(argv[argpos], "--dump-compact-ql")))
93692692 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)))
970 {
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 += '/';
1010 }
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)))
1070 {
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;
1120#ifdef HAVE_LZ4
1130 else if (std::string(argv[argpos]).substr(20) == "lz4")
1140 clone_settings.compression_method = File_Blocks_Index_Base::LZ4_COMPRESSION;
1150#endif
1160 else
1170 {
1180#ifdef HAVE_LZ4
1190 std::cerr<<"For --clone-compression, please use \"no\", \"gz\", or \"lz4\" as value.\n";
120#else
121 std::cerr<<"For --clone-compression, please use \"no\" or \"gz\" as value.\n";
122#endif
1230 return 0;
1240 }
1250 }
1260 else if (!(strncmp(argv[argpos], "--clone-map-compression=", 24)))
1270 {
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;
1320#ifdef HAVE_LZ4
1330 else if (std::string(argv[argpos]).substr(24) == "lz4")
1340 clone_settings.map_compression_method = File_Blocks_Index_Base::LZ4_COMPRESSION;
1350#endif
1360 else
1370 {
1380#ifdef HAVE_LZ4
1390 std::cerr<<"For --clone-map-compression, please use \"no\", \"gz\", or \"lz4\" as value.\n";
140#else
141 std::cerr<<"For --clone-map-compression, please use \"no\" or \"gz\" as value.\n";
142#endif
1430 return 0;
1440 }
1450 }
1460 else if (!(strcmp(argv[argpos], "--version")))
1470 {
1480 std::cout<<"Overpass API version "<<basic_settings().version<<" "<<basic_settings().source_hash<<"\n";
1490 return 0;
1500 }
1510 else
1520 {
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";
1740 return 0;
1750 }
1762768692 q ++argpos;
1772768692 q }
1792768692 q Error_Output* error_output(new Console_Output(log_level));
1802768692 q Statement::set_error_output(error_output);
182 // connect to dispatcher and get database dir
1832768692 q try
1842768692 q {
1852768692 q Parsed_Query global_settings;
1862768692 q if (clone_db_dir != "")
1870 {
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);
1980 return 0;
1990 }
2012768692 q if (xml_raw.empty())
2022768692 q xml_raw = get_xml_console(error_output);
2042768692 q if ((error_output) && (error_output->display_encoding_errors()))
2050 return 0;
2072768692 q Statement::Factory stmt_factory(global_settings);
2082768692 q if (!parse_and_validate(stmt_factory, global_settings, xml_raw, error_output, debug_level))
20912436 q return 0;
2102644676 q if (debug_level != parser_execute)
2111971659 q return 0;
213673673 q Osm_Script_Statement* osm_script = 0;
214673673 q if (!get_statement_stack()->empty())
215673673 q osm_script = dynamic_cast< Osm_Script_Statement* >(get_statement_stack()->front());
217673673 q uint32 max_allowed_time = 0;
218673673 q uint64 max_allowed_space = 0;
219673673 q if (osm_script)
221673673 q max_allowed_time = osm_script->get_max_allowed_time();
222673673 q max_allowed_space = osm_script->get_max_allowed_space();
2240 else
2250 {
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();
2290 }
231 // Allow rules to run for unlimited time
232673673 q if (!respect_timeout)
2330 max_allowed_time = 0;
235673673 q if (error_output)
236673673 q error_output->runtime_remark(
237673673 q "Timeout is " + to_string(max_allowed_time) + " and maxsize is " + to_string(max_allowed_space) + ".");
239 // open read transaction and log this.
240673673 q area_level = determine_area_level(error_output, area_level);
241673673 q Dispatcher_Stub dispatcher(
242673673 q db_dir, error_output, xml_raw,
243673673 q area_level, max_allowed_time, max_allowed_space, global_settings);
244673673 q if (osm_script && osm_script->get_desired_timestamp())
245673673 q dispatcher.resource_manager().set_desired_timestamp(osm_script->get_desired_timestamp());
247673673 q Web_Output web_output(log_level);
248673673 q web_output.set_output_handler(global_settings.get_output_handler());
249673673 q web_output.write_payload_header("", dispatcher.get_timestamp(),
250673673 q area_level > 0 ? dispatcher.get_area_timestamp() : "", false);
252673673 q dispatcher.resource_manager().start_cpu_timer(0);
253673673 q for (std::vector< Statement* >::const_iterator it(get_statement_stack()->begin());
2541346673 q it != get_statement_stack()->end(); ++it)
255673673 q (*it)->execute(dispatcher.resource_manager());
256673673 q dispatcher.resource_manager().stop_cpu_timer(0);
258 //TODO
259 /*if (osm_script && osm_script->get_type() == "custom")
260 {
261 uint32 count = osm_script->get_written_elements_count();
262 if (count == 0)
263 {
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();
269 }
270 else if (count == 1)
271 {
272 std::cout<<"Status: 302 Moved\n";
273 std::cout<<"Location: "
274 <<osm_script->adapt_url("http://www.openstreetmap.org/browse/{{{type}}}/{{{id}}}")
275 <<"\n\n";
276 }
277 else
278 {
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();
284 }
285 }
286 else if (osm_script && osm_script->get_type() == "popup")
287 {
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();
294 }
295 else*/
296673673 q web_output.write_footer();
298673673 q return 0;
2992644676 q }
3002768692 q catch(File_Error e)
3012768692 q {
3020 if (e.origin != "Dispatcher_Stub::Dispatcher_Stub::1")
3030 {
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;
3100 else
3110 temp<<"open64: "<<e.error_number<<' '<<strerror(e.error_number)<<' '<<e.filename<<' '<<e.origin;
3130 if (error_output)
3140 error_output->runtime_error(temp.str());
3150 }
3160 return 1;
3170 }
3182768692 q catch(Resource_Error e)
3192768692 q {
3200 std::ostringstream temp;
3210 if (e.timed_out)
3220 temp<<"Query timed out in \""<<e.stmt_name<<"\" at line "<<e.line_number
3230 <<" after "<<e.runtime<<" seconds.";
3240 else
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.";
3270 if (error_output)
3280 error_output->runtime_error(temp.str());
3300 return 2;
3310 }
3322768692 q catch(Context_Error e)
3332768692 q {
3340 error_output->runtime_error("Context error: " + e.message);
3350 return 3;
3360 }
3372768692 q catch(Exit_Error e)
3382768692 q {
3390 return 4;
3400 }
3412768692 q catch(std::bad_alloc& e)
3422768692 q {
3430 rlimit limit;
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());
3480 }
3492768692 q catch(std::exception& e)
3502768692 q {
3510 error_output->runtime_error(std::string("Query failed with the exception: ") + e.what());
3520 return 4;
3530 }