Skip to content

Commit 6767263

Browse files
authored
Merge pull request #1834 from joto/refactor-conninfo
Refactor: Build conninfo string only once on program start
2 parents fe0900f + 3c6b7ec commit 6767263

13 files changed

+71
-63
lines changed

src/db-check.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ static bool has_table(pg_conn_t const &db_connection, std::string const &schema,
3636

3737
void check_db(options_t const &options)
3838
{
39-
pg_conn_t db_connection{options.database_options.conninfo()};
39+
pg_conn_t db_connection{options.conninfo};
4040

4141
auto const settings = get_postgresql_settings(db_connection);
4242

src/middle-pgsql.cpp

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -691,9 +691,8 @@ void middle_pgsql_t::stop()
691691
} else if (!m_options->append) {
692692
// Building the indexes takes time, so do it asynchronously.
693693
for (auto &table : m_tables) {
694-
table.task_set(thread_pool().submit([&]() {
695-
table.build_index(m_options->database_options.conninfo());
696-
}));
694+
table.task_set(thread_pool().submit(
695+
[&]() { table.build_index(m_options->conninfo); }));
697696
}
698697
}
699698
}
@@ -837,9 +836,8 @@ middle_pgsql_t::middle_pgsql_t(std::shared_ptr<thread_pool_t> thread_pool,
837836
: middle_t(std::move(thread_pool)), m_options(options),
838837
m_cache(std::make_unique<node_locations_t>(
839838
static_cast<std::size_t>(options->cache) * 1024UL * 1024UL)),
840-
m_db_connection(m_options->database_options.conninfo()),
841-
m_copy_thread(
842-
std::make_shared<db_copy_thread_t>(options->database_options.conninfo())),
839+
m_db_connection(m_options->conninfo),
840+
m_copy_thread(std::make_shared<db_copy_thread_t>(options->conninfo)),
843841
m_db_copy(m_copy_thread)
844842
{
845843
if (!options->flat_node_file.empty()) {
@@ -871,7 +869,7 @@ middle_pgsql_t::get_query_instance()
871869
// NOTE: this is thread safe for use in pending async processing only because
872870
// during that process they are only read from
873871
auto mid = std::make_unique<middle_query_pgsql_t>(
874-
m_options->database_options.conninfo(), m_cache, m_persistent_cache);
872+
m_options->conninfo, m_cache, m_persistent_cache);
875873

876874
// We use a connection per table to enable the use of COPY
877875
for (auto &table : m_tables) {

src/options.cpp

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -248,35 +248,35 @@ static bool compare_prefix(std::string const &str,
248248
return std::strncmp(str.c_str(), prefix.c_str(), prefix.size()) == 0;
249249
}
250250

251-
std::string database_options_t::conninfo() const
251+
std::string build_conninfo(database_options_t const &opt)
252252
{
253-
if (compare_prefix(db, "postgresql://") ||
254-
compare_prefix(db, "postgres://")) {
255-
return db;
253+
if (compare_prefix(opt.db, "postgresql://") ||
254+
compare_prefix(opt.db, "postgres://")) {
255+
return opt.db;
256256
}
257257

258258
std::string out{"fallback_application_name='osm2pgsql'"};
259259

260-
if (std::strchr(db.c_str(), '=') != nullptr) {
260+
if (std::strchr(opt.db.c_str(), '=') != nullptr) {
261261
out += " ";
262-
out += db;
262+
out += opt.db;
263263
return out;
264264
}
265265

266-
if (!db.empty()) {
267-
out += " dbname='{}'"_format(db);
266+
if (!opt.db.empty()) {
267+
out += " dbname='{}'"_format(opt.db);
268268
}
269-
if (!username.empty()) {
270-
out += " user='{}'"_format(username);
269+
if (!opt.username.empty()) {
270+
out += " user='{}'"_format(opt.username);
271271
}
272-
if (!password.empty()) {
273-
out += " password='{}'"_format(password);
272+
if (!opt.password.empty()) {
273+
out += " password='{}'"_format(opt.password);
274274
}
275-
if (!host.empty()) {
276-
out += " host='{}'"_format(host);
275+
if (!opt.host.empty()) {
276+
out += " host='{}'"_format(opt.host);
277277
}
278-
if (!port.empty()) {
279-
out += " port='{}'"_format(port);
278+
if (!opt.port.empty()) {
279+
out += " port='{}'"_format(opt.port);
280280
}
281281

282282
return out;
@@ -347,6 +347,7 @@ options_t::options_t(int argc, char *argv[]) : options_t()
347347
return;
348348
}
349349

350+
database_options_t database_options;
350351
bool help_verbose = false; // Will be set when -v/--verbose is set
351352

352353
int c = 0;
@@ -646,6 +647,8 @@ options_t::options_t(int argc, char *argv[]) : options_t()
646647
if (pass_prompt) {
647648
database_options.password = util::get_password();
648649
}
650+
651+
conninfo = build_conninfo(database_options);
649652
}
650653

651654
void options_t::check_options()

src/options.hpp

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,20 +30,18 @@ enum class hstore_column : char
3030
all = 2
3131
};
3232

33-
/**
34-
* Database options, not specific to a table
35-
*/
33+
/// Database connection options.
3634
struct database_options_t
3735
{
3836
std::string db;
3937
std::string username;
4038
std::string host;
4139
std::string password;
4240
std::string port;
43-
44-
std::string conninfo() const;
4541
};
4642

43+
std::string build_conninfo(database_options_t const &opt);
44+
4745
/**
4846
* Outputs can signal their requirements to the middle by setting these fields.
4947
*/
@@ -93,7 +91,7 @@ class options_t
9391
return m_print_help;
9492
}
9593

96-
database_options_t database_options;
94+
std::string conninfo; ///< connection info for database
9795

9896
std::string prefix{"planet_osm"}; ///< prefix for table names
9997

src/osmdata.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ osmdata_t::osmdata_t(std::unique_ptr<dependency_manager_t> dependency_manager,
3030
std::shared_ptr<middle_t> mid,
3131
std::shared_ptr<output_t> output, options_t const &options)
3232
: m_dependency_manager(std::move(dependency_manager)), m_mid(std::move(mid)),
33-
m_output(std::move(output)), m_conninfo(options.database_options.conninfo()),
33+
m_output(std::move(output)), m_conninfo(options.conninfo),
3434
m_bbox(options.bbox), m_num_procs(options.num_procs),
3535
m_append(options.append), m_droptemp(options.droptemp),
3636
m_with_extra_attrs(options.extra_attributes),

src/output-flex.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1967,7 +1967,7 @@ void output_flex_t::relation_modify(osmium::Relation const &rel)
19671967
void output_flex_t::start()
19681968
{
19691969
for (auto &table : m_table_connections) {
1970-
table.connect(get_options()->database_options.conninfo());
1970+
table.connect(get_options()->conninfo);
19711971
table.start(get_options()->append);
19721972
}
19731973
}
@@ -1987,7 +1987,7 @@ output_flex_t::output_flex_t(output_flex_t const *other,
19871987
{
19881988
for (auto &table : *m_tables) {
19891989
auto &tc = m_table_connections.emplace_back(&table, m_copy_thread);
1990-
tc.connect(get_options()->database_options.conninfo());
1990+
tc.connect(get_options()->conninfo);
19911991
tc.prepare();
19921992
}
19931993
}

src/output-gazetteer.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ void output_gazetteer_t::start()
4949
if (!get_options()->append) {
5050
int const srid = get_options()->projection->target_srs();
5151

52-
pg_conn_t conn{get_options()->database_options.conninfo()};
52+
pg_conn_t conn{get_options()->conninfo};
5353

5454
/* Drop any existing table */
5555
conn.exec("DROP TABLE IF EXISTS place CASCADE");

src/output-pgsql.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -407,8 +407,7 @@ void output_pgsql_t::start()
407407
{
408408
for (auto &t : m_tables) {
409409
//setup the table in postgres
410-
t->start(get_options()->database_options.conninfo(),
411-
get_options()->tblsmain_data);
410+
t->start(get_options()->conninfo, get_options()->tblsmain_data);
412411
}
413412
}
414413

src/output.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,7 @@ output_t::create_output(std::shared_ptr<middle_query_t> const &mid,
3030
std::shared_ptr<thread_pool_t> thread_pool,
3131
options_t const &options)
3232
{
33-
auto copy_thread =
34-
std::make_shared<db_copy_thread_t>(options.database_options.conninfo());
33+
auto copy_thread = std::make_shared<db_copy_thread_t>(options.conninfo);
3534

3635
if (options.output_backend == "pgsql") {
3736
return std::make_shared<output_pgsql_t>(mid, std::move(thread_pool),

tests/common-import.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ class import_t
134134
std::initializer_list<std::string> input_data,
135135
std::string const &format = "opl")
136136
{
137-
options.database_options = m_db.db_options();
137+
options.conninfo = m_db.conninfo();
138138

139139
auto thread_pool = std::make_shared<thread_pool_t>(1U);
140140
auto middle = create_middle(thread_pool, options);
@@ -172,7 +172,7 @@ class import_t
172172

173173
void run_file(options_t options, char const *file = nullptr)
174174
{
175-
options.database_options = m_db.db_options();
175+
options.conninfo = m_db.conninfo();
176176

177177
auto thread_pool = std::make_shared<thread_pool_t>(1U);
178178
auto middle = std::make_shared<middle_ram_t>(thread_pool, &options);

0 commit comments

Comments
 (0)