Skip to content

Commit 10fb133

Browse files
committed
Refactor: Use templates to create middle tables
Add new middle_pgsql_t::render_template() and middle_pgsql_t::dbexec() functions and use them to create middle tables.
1 parent 0e7f92a commit 10fb133

File tree

2 files changed

+101
-64
lines changed

2 files changed

+101
-64
lines changed

src/middle-pgsql.cpp

Lines changed: 95 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -130,14 +130,25 @@ std::vector<std::string> build_sql(options_t const &options,
130130

131131
middle_pgsql_t::table_desc::table_desc(options_t const &options,
132132
table_sql const &ts)
133-
: m_create_table(build_sql(options, ts.create_table)),
134-
m_prepare_queries(build_sql(options, ts.prepare_queries)),
133+
: m_prepare_queries(build_sql(options, ts.prepare_queries)),
135134
m_copy_target(std::make_shared<db_target_descr_t>(
136135
options.middle_dbschema, build_sql(options, ts.name), "id"))
137136
{
138137
m_create_fw_dep_indexes = build_sql(options, ts.create_fw_dep_indexes);
139138
}
140139

140+
std::string middle_pgsql_t::render_template(std::string_view templ) const
141+
{
142+
template_t sql_template{templ};
143+
sql_template.set_params(m_params);
144+
return sql_template.render();
145+
}
146+
147+
void middle_pgsql_t::dbexec(std::string_view templ) const
148+
{
149+
m_db_connection.exec(render_template(templ));
150+
}
151+
141152
void middle_query_pgsql_t::exec_sql(std::string const &sql_cmd) const
142153
{
143154
m_db_connection.exec(sql_cmd);
@@ -171,14 +182,6 @@ void middle_pgsql_t::table_desc::build_index(
171182
}
172183
}
173184

174-
void middle_pgsql_t::table_desc::create_table(
175-
pg_conn_t const &db_connection) const
176-
{
177-
if (!m_create_table.empty()) {
178-
db_connection.exec(m_create_table);
179-
}
180-
}
181-
182185
void middle_pgsql_t::table_desc::init_max_id(pg_conn_t const &db_connection)
183186
{
184187
auto const qual_name = qualified_name(schema(), name());
@@ -696,7 +699,7 @@ INSERT INTO osm2pgsql_changed_relations
696699
)");
697700

698701
for (auto const &query : queries) {
699-
m_db_connection.exec(build_sql(*m_options, query));
702+
dbexec(query);
700703
}
701704

702705
if (parent_ways) {
@@ -741,12 +744,12 @@ void middle_pgsql_t::get_way_parents(idlist_t const &changed_ways,
741744

742745
m_db_connection.exec("ANALYZE osm2pgsql_changed_ways");
743746

744-
m_db_connection.exec(build_sql(*m_options, R"(
747+
dbexec(R"(
745748
INSERT INTO osm2pgsql_changed_relations
746749
SELECT DISTINCT r.id
747750
FROM {schema}"{prefix}_rels" r, osm2pgsql_changed_ways c
748751
WHERE {schema}"{prefix}_member_ids"(r.members, 'W'::char) && ARRAY[c.id];
749-
)"));
752+
)");
750753

751754
load_id_list(m_db_connection, "osm2pgsql_changed_relations",
752755
parent_relations);
@@ -1046,18 +1049,6 @@ middle_query_pgsql_t::middle_query_pgsql_t(
10461049
m_db_connection.set_config("max_parallel_workers_per_gather", "0");
10471050
}
10481051

1049-
namespace {
1050-
1051-
void table_setup(pg_conn_t const &db_connection,
1052-
middle_pgsql_t::table_desc const &table)
1053-
{
1054-
log_debug("Setting up table '{}'", table.name());
1055-
drop_table_if_exists(db_connection, table.schema(), table.name());
1056-
table.create_table(db_connection);
1057-
}
1058-
1059-
} // anonymous namespace
1060-
10611052
void middle_pgsql_t::start()
10621053
{
10631054
assert(m_middle_state == middle_state::constructed);
@@ -1080,11 +1071,46 @@ void middle_pgsql_t::start()
10801071
}
10811072
m_tables.ways().init_max_id(m_db_connection);
10821073
m_tables.relations().init_max_id(m_db_connection);
1083-
} else {
1084-
table_setup(m_db_connection, m_users_table);
1085-
for (auto const &table : m_tables) {
1086-
table_setup(m_db_connection, table);
1087-
}
1074+
return;
1075+
}
1076+
1077+
if (m_store_options.nodes) {
1078+
log_debug("Setting up table 'nodes'");
1079+
dbexec(R"(DROP TABLE IF EXISTS {schema}"{prefix}_nodes" CASCADE)");
1080+
dbexec("CREATE {unlogged} TABLE {schema}\"{prefix}_nodes\" ("
1081+
" id int8 PRIMARY KEY {using_tablespace},"
1082+
" lat int4 NOT NULL,"
1083+
" lon int4 NOT NULL,"
1084+
"{attribute_columns_definition}"
1085+
" tags jsonb"
1086+
") {data_tablespace}");
1087+
}
1088+
1089+
log_debug("Setting up table 'ways'");
1090+
dbexec(R"(DROP TABLE IF EXISTS {schema}"{prefix}_ways" CASCADE)");
1091+
dbexec("CREATE {unlogged} TABLE {schema}\"{prefix}_ways\" ("
1092+
" id int8 PRIMARY KEY {using_tablespace},"
1093+
"{attribute_columns_definition}"
1094+
" nodes int8[] NOT NULL,"
1095+
" tags jsonb"
1096+
") {data_tablespace}");
1097+
1098+
log_debug("Setting up table 'rels'");
1099+
dbexec(R"(DROP TABLE IF EXISTS {schema}"{prefix}_rels" CASCADE)");
1100+
dbexec("CREATE {unlogged} TABLE {schema}\"{prefix}_rels\" ("
1101+
" id int8 PRIMARY KEY {using_tablespace},"
1102+
"{attribute_columns_definition}"
1103+
" members jsonb NOT NULL,"
1104+
" tags jsonb"
1105+
") {data_tablespace}");
1106+
1107+
if (m_store_options.with_attributes) {
1108+
log_debug("Setting up table 'users'");
1109+
dbexec(R"(DROP TABLE IF EXISTS {schema}"{prefix}_users" CASCADE)");
1110+
dbexec("CREATE TABLE {schema}\"{prefix}_users\" ("
1111+
" id INT4 PRIMARY KEY {using_tablespace},"
1112+
" name TEXT NOT NULL"
1113+
") {data_tablespace}");
10881114
}
10891115
}
10901116

@@ -1168,13 +1194,6 @@ table_sql sql_for_users(middle_pgsql_options const &store_options)
11681194

11691195
sql.name = "{prefix}_users";
11701196

1171-
if (store_options.with_attributes) {
1172-
sql.create_table = "CREATE TABLE {schema}\"{prefix}_users\" ("
1173-
" id INT4 PRIMARY KEY {using_tablespace},"
1174-
" name TEXT NOT NULL"
1175-
") {data_tablespace}";
1176-
}
1177-
11781197
return sql;
11791198
}
11801199

@@ -1185,15 +1204,6 @@ table_sql sql_for_nodes(middle_pgsql_options const &options)
11851204
sql.name = "{prefix}_nodes";
11861205

11871206
if (options.nodes) {
1188-
sql.create_table =
1189-
"CREATE {unlogged} TABLE {schema}\"{prefix}_nodes\" ("
1190-
" id int8 PRIMARY KEY {using_tablespace},"
1191-
" lat int4 NOT NULL,"
1192-
" lon int4 NOT NULL,"
1193-
"{attribute_columns_definition}"
1194-
" tags jsonb"
1195-
") {data_tablespace}";
1196-
11971207
sql.prepare_queries = {
11981208
"PREPARE get_node_list(int8[]) AS"
11991209
" SELECT id, lon, lat FROM {schema}\"{prefix}_nodes\""
@@ -1212,13 +1222,6 @@ table_sql sql_for_ways()
12121222

12131223
sql.name = "{prefix}_ways";
12141224

1215-
sql.create_table = "CREATE {unlogged} TABLE {schema}\"{prefix}_ways\" ("
1216-
" id int8 PRIMARY KEY {using_tablespace},"
1217-
"{attribute_columns_definition}"
1218-
" nodes int8[] NOT NULL,"
1219-
" tags jsonb"
1220-
") {data_tablespace}";
1221-
12221225
sql.prepare_queries = {"PREPARE get_way(int8) AS"
12231226
" SELECT nodes, tags{attribute_columns_use}"
12241227
" FROM {schema}\"{prefix}_ways\" o"
@@ -1251,13 +1254,6 @@ table_sql sql_for_relations()
12511254

12521255
sql.name = "{prefix}_rels";
12531256

1254-
sql.create_table = "CREATE {unlogged} TABLE {schema}\"{prefix}_rels\" ("
1255-
" id int8 PRIMARY KEY {using_tablespace},"
1256-
"{attribute_columns_definition}"
1257-
" members jsonb NOT NULL,"
1258-
" tags jsonb"
1259-
") {data_tablespace}";
1260-
12611257
sql.prepare_queries = {"PREPARE get_rel(int8) AS"
12621258
" SELECT members, tags{attribute_columns_use}"
12631259
" FROM {schema}\"{prefix}_rels\" o"
@@ -1284,6 +1280,43 @@ table_sql sql_for_relations()
12841280
return sql;
12851281
}
12861282

1283+
void init_params(params_t *params, options_t const &options)
1284+
{
1285+
std::string const schema = "\"" + options.middle_dbschema + "\".";
1286+
1287+
params->set("prefix", options.prefix);
1288+
params->set("schema", schema);
1289+
params->set("unlogged", options.droptemp ? "UNLOGGED" : "");
1290+
params->set("data_tablespace", tablespace_clause(options.tblsslim_data));
1291+
params->set("index_tablespace", tablespace_clause(options.tblsslim_index));
1292+
params->set("way_node_index_id_shift", 5);
1293+
1294+
if (options.tblsslim_index.empty()) {
1295+
params->set("using_tablespace", "");
1296+
} else {
1297+
params->set("using_tablespace",
1298+
"USING INDEX TABLESPACE " + options.tblsslim_index);
1299+
}
1300+
1301+
if (options.extra_attributes) {
1302+
params->set("attribute_columns_definition",
1303+
" created timestamp with time zone,"
1304+
" version int4,"
1305+
" changeset_id int4,"
1306+
" user_id int4,");
1307+
params->set("attribute_columns_use",
1308+
", EXTRACT(EPOCH FROM created) AS created, version, "
1309+
"changeset_id, user_id, u.name");
1310+
params->set("users_table_access", "LEFT JOIN " + schema + '"' +
1311+
options.prefix +
1312+
"_users\" u ON o.user_id = u.id");
1313+
} else {
1314+
params->set("attribute_columns_definition", "");
1315+
params->set("attribute_columns_use", "");
1316+
params->set("users_table_access", "");
1317+
}
1318+
}
1319+
12871320
} // anonymous namespace
12881321

12891322
middle_pgsql_t::middle_pgsql_t(std::shared_ptr<thread_pool_t> thread_pool,
@@ -1312,6 +1345,8 @@ middle_pgsql_t::middle_pgsql_t(std::shared_ptr<thread_pool_t> thread_pool,
13121345

13131346
log_debug("Mid: pgsql, cache={}", options->cache);
13141347

1348+
init_params(&m_params, *options);
1349+
13151350
m_tables.nodes() = table_desc{*options, sql_for_nodes(m_store_options)};
13161351
m_tables.ways() = table_desc{*options, sql_for_ways()};
13171352
m_tables.relations() = table_desc{*options, sql_for_relations()};

src/middle-pgsql.hpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#include "db-copy-mgr.hpp"
2727
#include "idlist.hpp"
2828
#include "middle.hpp"
29+
#include "params.hpp"
2930
#include "pgsql.hpp"
3031

3132
class node_locations_t;
@@ -88,7 +89,6 @@ class middle_query_pgsql_t : public middle_query_t
8889
struct table_sql
8990
{
9091
std::string name;
91-
std::string create_table;
9292
std::vector<std::string> prepare_queries;
9393
std::vector<std::string> create_fw_dep_indexes;
9494
};
@@ -151,8 +151,6 @@ struct middle_pgsql_t : public middle_t
151151

152152
std::chrono::microseconds task_wait() { return m_task_result.wait(); }
153153

154-
void create_table(pg_conn_t const &db_connection) const;
155-
156154
void init_max_id(pg_conn_t const &db_connection);
157155

158156
osmid_t max_id() const noexcept { return m_max_id; }
@@ -163,7 +161,6 @@ struct middle_pgsql_t : public middle_t
163161
}
164162

165163
private:
166-
std::string m_create_table;
167164
std::vector<std::string> m_create_fw_dep_indexes;
168165
std::vector<std::string> m_prepare_queries;
169166
std::shared_ptr<db_target_descr_t> m_copy_target;
@@ -193,6 +190,9 @@ struct middle_pgsql_t : public middle_t
193190
void write_users_table();
194191
void update_users_table();
195192

193+
std::string render_template(std::string_view templ) const;
194+
void dbexec(std::string_view templ) const;
195+
196196
std::map<osmium::user_id_type, std::string> m_users;
197197
osmium::nwr_array<table_desc> m_tables;
198198
table_desc m_users_table;
@@ -211,6 +211,8 @@ struct middle_pgsql_t : public middle_t
211211
/// Options for this middle.
212212
middle_pgsql_options m_store_options;
213213

214+
params_t m_params;
215+
214216
bool m_append;
215217
};
216218

0 commit comments

Comments
 (0)