1 /**
2  * SPDX-License-Identifier: GPL-2.0-or-later
3  *
4  * This file is part of osm2pgsql (https://osm2pgsql.org/).
5  *
6  * Copyright (C) 2006-2021 by the osm2pgsql developer community.
7  * For a full list of authors see the git log.
8  */
9 
10 #include "db-check.hpp"
11 #include "dependency-manager.hpp"
12 #include "input.hpp"
13 #include "logging.hpp"
14 #include "middle.hpp"
15 #include "options.hpp"
16 #include "osmdata.hpp"
17 #include "output.hpp"
18 #include "util.hpp"
19 #include "version.hpp"
20 
21 #include <osmium/util/memory.hpp>
22 
23 #include <exception>
24 #include <memory>
25 #include <utility>
26 
run(options_t const & options)27 static void run(options_t const &options)
28 {
29     auto const files = prepare_input_files(
30         options.input_files, options.input_format, options.append);
31 
32     auto thread_pool = std::make_shared<thread_pool_t>(
33         options.parallel_indexing ? options.num_procs : 1U);
34     log_debug("Started pool with {} threads.", thread_pool->num_threads());
35 
36     auto middle = create_middle(thread_pool, options);
37     middle->start();
38 
39     auto output = output_t::create_output(middle->get_query_instance(),
40                                           thread_pool, options);
41 
42     middle->set_requirements(output->get_requirements());
43 
44     auto dependency_manager =
45         options.with_forward_dependencies
46             ? std::make_unique<full_dependency_manager_t>(middle)
47             : std::make_unique<dependency_manager_t>();
48 
49     osmdata_t osmdata{std::move(dependency_manager), middle, output, options};
50 
51     osmdata.start();
52 
53     // Processing: In this phase the input file(s) are read and parsed,
54     // populating some of the tables.
55     process_files(files, &osmdata, options.append,
56                   get_logger().show_progress());
57 
58     // Process pending ways and relations. Cluster database tables and
59     // create indexes.
60     osmdata.stop();
61 }
62 
main(int argc,char * argv[])63 int main(int argc, char *argv[])
64 {
65     try {
66         log_info("osm2pgsql version {}", get_osm2pgsql_version());
67 
68         options_t const options{argc, argv};
69         if (options.early_return()) {
70             return 0;
71         }
72 
73         util::timer_t timer_overall;
74 
75         check_db(options);
76 
77         run(options);
78 
79         // Output overall memory usage. This only works on Linux.
80         osmium::MemoryUsage mem;
81         if (mem.peak() != 0) {
82             log_debug("Overall memory usage: peak={}MByte current={}MByte",
83                       mem.peak(), mem.current());
84         }
85 
86         log_info("osm2pgsql took {} overall.",
87                  util::human_readable_duration(timer_overall.stop()));
88     } catch (std::exception const &e) {
89         log_error("{}", e.what());
90         return 1;
91     }
92 
93     return 0;
94 }
95