Skip to content

Commit 16ba340

Browse files
committed
middle-pgsql: reorganise table descriptions
Remove vector and use a fixed sized array instead. Also remove the pointer indirections and use index consts into the array.
1 parent c422c49 commit 16ba340

File tree

2 files changed

+75
-72
lines changed

2 files changed

+75
-72
lines changed

middle-pgsql.cpp

Lines changed: 66 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,7 @@ void middle_pgsql_t::local_nodes_set(osmium::Node const &node)
258258
{
259259
copy_buffer.reserve(node.tags().byte_size() + 100);
260260

261-
bool copy = node_table->copyMode;
261+
bool copy = tables[NODE_TABLE].copyMode;
262262
char delim = copy ? '\t' : '\0';
263263
const char *paramValues[4] = {
264264
copy_buffer.c_str(),
@@ -276,10 +276,10 @@ void middle_pgsql_t::local_nodes_set(osmium::Node const &node)
276276

277277
if (copy) {
278278
copy_buffer += '\n';
279-
pgsql_CopyData(__FUNCTION__, node_table->sql_conn, copy_buffer);
279+
pgsql_CopyData(__FUNCTION__, tables[NODE_TABLE].sql_conn, copy_buffer);
280280
} else {
281281
buffer_correct_params(paramValues, 4);
282-
pgsql_execPrepared(node_table->sql_conn, "insert_node", 3,
282+
pgsql_execPrepared(tables[NODE_TABLE].sql_conn, "insert_node", 3,
283283
(const char *const *)paramValues, PGRES_COMMAND_OK);
284284
}
285285
}
@@ -312,9 +312,9 @@ size_t middle_pgsql_t::local_nodes_get_list(osmium::WayNodeList *nodes) const
312312
buffer[buffer.size() - 1] = '}';
313313

314314
// Nodes must have been written back at this point.
315-
assert(node_table->copyMode == 0);
315+
assert(tables[NODE_TABLE].copyMode == 0);
316316

317-
PGconn *sql_conn = node_table->sql_conn;
317+
PGconn *sql_conn = tables[NODE_TABLE].sql_conn;
318318

319319
char const *paramValues[1];
320320
paramValues[0] = buffer.c_str();
@@ -366,11 +366,12 @@ void middle_pgsql_t::local_nodes_delete(osmid_t osm_id)
366366
char const *paramValues[1];
367367
char buffer[64];
368368
// Make sure we're out of copy mode */
369-
pgsql_endCopy( node_table );
369+
pgsql_endCopy(&tables[NODE_TABLE]);
370370

371371
sprintf( buffer, "%" PRIdOSMID, osm_id );
372372
paramValues[0] = buffer;
373-
pgsql_execPrepared(node_table->sql_conn, "delete_node", 1, paramValues, PGRES_COMMAND_OK );
373+
pgsql_execPrepared(tables[NODE_TABLE].sql_conn, "delete_node", 1,
374+
paramValues, PGRES_COMMAND_OK);
374375
}
375376

376377
void middle_pgsql_t::nodes_delete(osmid_t osm_id)
@@ -391,24 +392,26 @@ void middle_pgsql_t::node_changed(osmid_t osm_id)
391392
char const *paramValues[1];
392393
char buffer[64];
393394
// Make sure we're out of copy mode */
394-
pgsql_endCopy( way_table );
395-
pgsql_endCopy( rel_table );
395+
pgsql_endCopy(&tables[WAY_TABLE]);
396+
pgsql_endCopy(&tables[REL_TABLE]);
396397

397398
sprintf( buffer, "%" PRIdOSMID, osm_id );
398399
paramValues[0] = buffer;
399400

400401
//keep track of whatever ways and rels these nodes intersect
401402
//TODO: dont need to stop the copy above since we are only reading?
402-
auto res = pgsql_execPrepared(way_table->sql_conn, "mark_ways_by_node", 1,
403-
paramValues, PGRES_TUPLES_OK);
403+
auto res =
404+
pgsql_execPrepared(tables[WAY_TABLE].sql_conn, "mark_ways_by_node", 1,
405+
paramValues, PGRES_TUPLES_OK);
404406
for (int i = 0; i < PQntuples(res.get()); ++i) {
405407
char *end;
406408
osmid_t marked = strtoosmid(PQgetvalue(res.get(), i, 0), &end, 10);
407409
ways_pending_tracker->mark(marked);
408410
}
409411

410412
//do the rels too
411-
res = pgsql_execPrepared(rel_table->sql_conn, "mark_rels_by_node", 1, paramValues, PGRES_TUPLES_OK );
413+
res = pgsql_execPrepared(tables[REL_TABLE].sql_conn, "mark_rels_by_node", 1,
414+
paramValues, PGRES_TUPLES_OK);
412415
for (int i = 0; i < PQntuples(res.get()); ++i) {
413416
char *end;
414417
osmid_t marked = strtoosmid(PQgetvalue(res.get(), i, 0), &end, 10);
@@ -419,7 +422,7 @@ void middle_pgsql_t::node_changed(osmid_t osm_id)
419422
void middle_pgsql_t::ways_set(osmium::Way const &way)
420423
{
421424
copy_buffer.reserve(way.nodes().size() * 10 + way.tags().byte_size() + 100);
422-
bool copy = way_table->copyMode;
425+
bool copy = tables[WAY_TABLE].copyMode;
423426
char delim = copy ? '\t' : '\0';
424427
// Three params: id, nodes, tags */
425428
const char *paramValues[4] = { copy_buffer.c_str(), };
@@ -450,21 +453,21 @@ void middle_pgsql_t::ways_set(osmium::Way const &way)
450453

451454
if (copy) {
452455
copy_buffer += '\n';
453-
pgsql_CopyData(__FUNCTION__, way_table->sql_conn, copy_buffer);
456+
pgsql_CopyData(__FUNCTION__, tables[WAY_TABLE].sql_conn, copy_buffer);
454457
} else {
455458
buffer_correct_params(paramValues, 3);
456-
pgsql_execPrepared(way_table->sql_conn, "insert_way", 3,
457-
(const char * const *)paramValues, PGRES_COMMAND_OK);
459+
pgsql_execPrepared(tables[WAY_TABLE].sql_conn, "insert_way", 3,
460+
(const char *const *)paramValues, PGRES_COMMAND_OK);
458461
}
459462
}
460463

461464
bool middle_pgsql_t::ways_get(osmid_t id, osmium::memory::Buffer &buffer) const
462465
{
463466
char const *paramValues[1];
464-
PGconn *sql_conn = way_table->sql_conn;
467+
PGconn *sql_conn = tables[WAY_TABLE].sql_conn;
465468

466469
// Make sure we're out of copy mode
467-
assert(way_table->copyMode == 0);
470+
assert(tables[WAY_TABLE].copyMode == 0);
468471

469472
char tmp[16];
470473
snprintf(tmp, sizeof(tmp), "%" PRIdOSMID, id);
@@ -510,12 +513,12 @@ size_t middle_pgsql_t::rel_way_members_get(osmium::Relation const &rel,
510513
return 0; // no ways found
511514
}
512515
// replace last , with } to complete list of ids
513-
tmp2[tmp2.length() - 1] = '}';
516+
tmp2[tmp2.length() - 1] = '}';
514517

515518
// Make sures all ways have been written back.
516-
assert(way_table->copyMode == 0);
519+
assert(tables[WAY_TABLE].copyMode == 0);
517520

518-
PGconn *sql_conn = way_table->sql_conn;
521+
PGconn *sql_conn = tables[WAY_TABLE].sql_conn;
519522

520523
paramValues[0] = tmp2.c_str();
521524
auto res = pgsql_execPrepared(sql_conn, "get_way_list", 1, paramValues,
@@ -567,18 +570,19 @@ void middle_pgsql_t::ways_delete(osmid_t osm_id)
567570
char const *paramValues[1];
568571
char buffer[64];
569572
// Make sure we're out of copy mode */
570-
pgsql_endCopy( way_table );
573+
pgsql_endCopy(&tables[WAY_TABLE]);
571574

572575
sprintf( buffer, "%" PRIdOSMID, osm_id );
573576
paramValues[0] = buffer;
574-
pgsql_execPrepared(way_table->sql_conn, "delete_way", 1, paramValues, PGRES_COMMAND_OK );
577+
pgsql_execPrepared(tables[WAY_TABLE].sql_conn, "delete_way", 1, paramValues,
578+
PGRES_COMMAND_OK);
575579
}
576580

577581
void middle_pgsql_t::iterate_ways(middle_t::pending_processor& pf)
578582
{
579583

580584
// Make sure we're out of copy mode */
581-
pgsql_endCopy( way_table );
585+
pgsql_endCopy(&tables[WAY_TABLE]);
582586

583587
// enqueue the jobs
584588
osmid_t id;
@@ -598,15 +602,16 @@ void middle_pgsql_t::way_changed(osmid_t osm_id)
598602
char const *paramValues[1];
599603
char buffer[64];
600604
// Make sure we're out of copy mode */
601-
pgsql_endCopy( rel_table );
605+
pgsql_endCopy(&tables[REL_TABLE]);
602606

603607
sprintf( buffer, "%" PRIdOSMID, osm_id );
604608
paramValues[0] = buffer;
605609

606610
//keep track of whatever rels this way intersects
607611
//TODO: dont need to stop the copy above since we are only reading?
608-
auto res = pgsql_execPrepared(rel_table->sql_conn, "mark_rels_by_way", 1,
609-
paramValues, PGRES_TUPLES_OK);
612+
auto res =
613+
pgsql_execPrepared(tables[REL_TABLE].sql_conn, "mark_rels_by_way", 1,
614+
paramValues, PGRES_TUPLES_OK);
610615
for (int i = 0; i < PQntuples(res.get()); ++i) {
611616
char *end;
612617
osmid_t marked = strtoosmid(PQgetvalue(res.get(), i, 0), &end, 10);
@@ -626,7 +631,7 @@ void middle_pgsql_t::relations_set(osmium::Relation const &rel)
626631

627632
// Params: id, way_off, rel_off, parts, members, tags */
628633
const char *paramValues[6] = { copy_buffer.c_str(), };
629-
bool copy = rel_table->copyMode;
634+
bool copy = tables[REL_TABLE].copyMode;
630635
char delim = copy ? '\t' : '\0';
631636

632637
copy_buffer = std::to_string(rel.id());
@@ -683,23 +688,23 @@ void middle_pgsql_t::relations_set(osmium::Relation const &rel)
683688

684689
if (copy) {
685690
copy_buffer+= '\n';
686-
pgsql_CopyData(__FUNCTION__, rel_table->sql_conn, copy_buffer);
691+
pgsql_CopyData(__FUNCTION__, tables[REL_TABLE].sql_conn, copy_buffer);
687692
} else {
688693
buffer_correct_params(paramValues, 6);
689-
pgsql_execPrepared(rel_table->sql_conn, "insert_rel", 6,
690-
(const char * const *)paramValues, PGRES_COMMAND_OK);
694+
pgsql_execPrepared(tables[REL_TABLE].sql_conn, "insert_rel", 6,
695+
(const char *const *)paramValues, PGRES_COMMAND_OK);
691696
}
692697
}
693698

694699
bool middle_pgsql_t::relations_get(osmid_t id, osmium::memory::Buffer &buffer) const
695700
{
696701
char tmp[16];
697702
char const *paramValues[1];
698-
PGconn *sql_conn = rel_table->sql_conn;
703+
PGconn *sql_conn = tables[REL_TABLE].sql_conn;
699704
taglist_t member_temp;
700705

701706
// Make sure we're out of copy mode
702-
assert(rel_table->copyMode == 0);
707+
assert(tables[REL_TABLE].copyMode == 0);
703708

704709
snprintf(tmp, sizeof(tmp), "%" PRIdOSMID, id);
705710
paramValues[0] = tmp;
@@ -730,17 +735,19 @@ void middle_pgsql_t::relations_delete(osmid_t osm_id)
730735
char const *paramValues[1];
731736
char buffer[64];
732737
// Make sure we're out of copy mode */
733-
pgsql_endCopy( way_table );
734-
pgsql_endCopy( rel_table );
738+
pgsql_endCopy(&tables[WAY_TABLE]);
739+
pgsql_endCopy(&tables[REL_TABLE]);
735740

736741
sprintf( buffer, "%" PRIdOSMID, osm_id );
737742
paramValues[0] = buffer;
738-
pgsql_execPrepared(rel_table->sql_conn, "delete_rel", 1, paramValues, PGRES_COMMAND_OK );
743+
pgsql_execPrepared(tables[REL_TABLE].sql_conn, "delete_rel", 1, paramValues,
744+
PGRES_COMMAND_OK);
739745

740746
//keep track of whatever ways this relation interesects
741747
//TODO: dont need to stop the copy above since we are only reading?
742-
auto res = pgsql_execPrepared(way_table->sql_conn, "mark_ways_by_rel", 1,
743-
paramValues, PGRES_TUPLES_OK);
748+
auto res =
749+
pgsql_execPrepared(tables[WAY_TABLE].sql_conn, "mark_ways_by_rel", 1,
750+
paramValues, PGRES_TUPLES_OK);
744751
for (int i = 0; i < PQntuples(res.get()); ++i) {
745752
char *end;
746753
osmid_t marked = strtoosmid(PQgetvalue(res.get(), i, 0), &end, 10);
@@ -751,7 +758,7 @@ void middle_pgsql_t::relations_delete(osmid_t osm_id)
751758
void middle_pgsql_t::iterate_relations(pending_processor& pf)
752759
{
753760
// Make sure we're out of copy mode */
754-
pgsql_endCopy( rel_table );
761+
pgsql_endCopy(&tables[REL_TABLE]);
755762

756763
// enqueue the jobs
757764
osmid_t id;
@@ -771,15 +778,15 @@ void middle_pgsql_t::relation_changed(osmid_t osm_id)
771778
char const *paramValues[1];
772779
char buffer[64];
773780
// Make sure we're out of copy mode */
774-
pgsql_endCopy( rel_table );
781+
pgsql_endCopy(&tables[REL_TABLE]);
775782

776783
sprintf( buffer, "%" PRIdOSMID, osm_id );
777784
paramValues[0] = buffer;
778785

779786
//keep track of whatever ways and rels these nodes intersect
780787
//TODO: dont need to stop the copy above since we are only reading?
781788
//TODO: can we just mark the id without querying? the where clause seems intersect reltable.parts with the id
782-
auto res = pgsql_execPrepared(rel_table->sql_conn, "mark_rels", 1,
789+
auto res = pgsql_execPrepared(tables[REL_TABLE].sql_conn, "mark_rels", 1,
783790
paramValues, PGRES_TUPLES_OK);
784791
for (int i = 0; i < PQntuples(res.get()); ++i) {
785792
char *end;
@@ -793,13 +800,14 @@ idlist_t middle_pgsql_t::relations_using_way(osmid_t way_id) const
793800
char const *paramValues[1];
794801
char buffer[64];
795802
// Make sure we're out of copy mode */
796-
assert(rel_table->copyMode == 0);
803+
assert(tables[REL_TABLE].copyMode == 0);
797804

798805
sprintf(buffer, "%" PRIdOSMID, way_id);
799806
paramValues[0] = buffer;
800807

801-
auto result = pgsql_execPrepared(rel_table->sql_conn, "rels_using_way", 1,
802-
paramValues, PGRES_TUPLES_OK);
808+
auto result =
809+
pgsql_execPrepared(tables[REL_TABLE].sql_conn, "rels_using_way", 1,
810+
paramValues, PGRES_TUPLES_OK);
803811
const int ntuples = PQntuples(result.get());
804812
idlist_t rel_ids;
805813
rel_ids.resize((size_t) ntuples);
@@ -948,7 +956,7 @@ void middle_pgsql_t::start(const options_t *out_options_)
948956
// and pass that via the constructor to middle_t, so that middle_t
949957
// itself doesn't need to know about details of the output.
950958
if (out_options->output_backend == "gazetteer") {
951-
way_table->array_indexes = nullptr;
959+
tables[WAY_TABLE].array_indexes = nullptr;
952960
mark_pending = false;
953961
}
954962

@@ -1073,24 +1081,22 @@ void middle_pgsql_t::stop(osmium::thread::Pool &pool)
10731081
if (out_options->droptemp) {
10741082
// Dropping the tables is fast, so do it synchronously to guarantee
10751083
// that the space is freed before creating the other indices.
1076-
for (int i = 0; i < num_tables; ++i) {
1077-
pgsql_stop_one(&tables[i]);
1084+
for (auto &t : tables) {
1085+
pgsql_stop_one(&t);
10781086
}
10791087
} else {
1080-
for (int i = 0; i < num_tables; ++i) {
1081-
pool.submit(
1082-
std::bind(&middle_pgsql_t::pgsql_stop_one, this, &tables[i]));
1088+
for (auto &t : tables) {
1089+
pool.submit(std::bind(&middle_pgsql_t::pgsql_stop_one, this, &t));
10831090
}
10841091
}
10851092
}
10861093

10871094
middle_pgsql_t::middle_pgsql_t()
1088-
: num_tables(0), node_table(nullptr), way_table(nullptr), rel_table(nullptr),
1089-
append(false), mark_pending(true), build_indexes(true)
1095+
: append(false), mark_pending(true), build_indexes(true)
10901096
{
10911097
// clang-format off
10921098
/*table = t_node,*/
1093-
tables.push_back(table_desc(
1099+
tables[NODE_TABLE] = table_desc(
10941100
/*name*/ "%p_nodes",
10951101
/*start*/ "BEGIN;\n",
10961102
/*create*/ "CREATE %m TABLE %p_nodes (id " POSTGRES_OSMID_TYPE " PRIMARY KEY {USING INDEX TABLESPACE %i}, lat int4 not null, lon int4 not null) {TABLESPACE %t};\n",
@@ -1102,8 +1108,8 @@ middle_pgsql_t::middle_pgsql_t()
11021108
/*copy*/ "COPY %p_nodes FROM STDIN;\n",
11031109
/*analyze*/ "ANALYZE %p_nodes;\n",
11041110
/*stop*/ "COMMIT;\n"
1105-
));
1106-
tables.push_back(table_desc(
1111+
);
1112+
tables[WAY_TABLE] = table_desc(
11071113
/*table t_way,*/
11081114
/*name*/ "%p_ways",
11091115
/*start*/ "BEGIN;\n",
@@ -1121,8 +1127,8 @@ middle_pgsql_t::middle_pgsql_t()
11211127
/*analyze*/ "ANALYZE %p_ways;\n",
11221128
/*stop*/ "COMMIT;\n",
11231129
/*array_indexes*/ "CREATE INDEX %p_ways_nodes ON %p_ways USING gin (nodes) WITH (FASTUPDATE=OFF) {TABLESPACE %i};\n"
1124-
));
1125-
tables.push_back(table_desc(
1130+
);
1131+
tables[REL_TABLE] = table_desc(
11261132
/*table = t_rel,*/
11271133
/*name*/ "%p_rels",
11281134
/*start*/ "BEGIN;\n",
@@ -1141,20 +1147,12 @@ middle_pgsql_t::middle_pgsql_t()
11411147
/*analyze*/ "ANALYZE %p_rels;\n",
11421148
/*stop*/ "COMMIT;\n",
11431149
/*array_indexes*/ "CREATE INDEX %p_rels_parts ON %p_rels USING gin (parts) WITH (FASTUPDATE=OFF) {TABLESPACE %i};\n"
1144-
));
1150+
);
11451151
// clang-format on
1146-
1147-
// set up the rest of the variables from the tables.
1148-
num_tables = tables.size();
1149-
assert(num_tables == 3);
1150-
1151-
node_table = &tables[0];
1152-
way_table = &tables[1];
1153-
rel_table = &tables[2];
11541152
}
11551153

11561154
middle_pgsql_t::~middle_pgsql_t() {
1157-
for (auto& table: tables) {
1155+
for (auto &table : tables) {
11581156
if (table.sql_conn) {
11591157
PQfinish(table.sql_conn);
11601158
}
@@ -1180,7 +1178,7 @@ middle_pgsql_t::get_query_instance(std::shared_ptr<middle_t> const &from) const
11801178
mid->persistent_cache = src->persistent_cache;
11811179

11821180
// We use a connection per table to enable the use of COPY
1183-
for (int i = 0; i < num_tables; i++) {
1181+
for (int i = 0; i < NUM_TABLES; i++) {
11841182
mid->connect(mid->tables[i]);
11851183
PGconn* sql_conn = mid->tables[i].sql_conn;
11861184

0 commit comments

Comments
 (0)