Skip to content

Commit 0275c1a

Browse files
authored
Merge pull request #909 from lonvia/split-table-t-constructor
Split table_t constructor
2 parents ed0bf5b + 6bc46c2 commit 0275c1a

File tree

5 files changed

+73
-75
lines changed

5 files changed

+73
-75
lines changed

output-multi.cpp

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,10 @@ output_multi_t::output_multi_t(std::string const &name,
2424
m_osm_type(m_processor->interests(geometry_processor::interest_node)
2525
? osmium::item_type::node
2626
: osmium::item_type::way),
27-
m_table(new table_t(
28-
m_options.database_options.conninfo(), name, m_processor->column_type(),
29-
export_list.normal_columns(m_osm_type), m_options.hstore_columns,
30-
m_processor->srid(), m_options.append, m_options.slim, m_options.droptemp,
31-
m_options.hstore_mode, m_options.enable_hstore_index,
32-
m_options.tblsmain_data, m_options.tblsmain_index)),
27+
m_table(new table_t(name, m_processor->column_type(),
28+
export_list.normal_columns(m_osm_type),
29+
m_options.hstore_columns, m_processor->srid(),
30+
m_options.append, m_options.hstore_mode)),
3331
ways_done_tracker(new id_tracker()),
3432
m_expire(m_options.expire_tiles_zoom, m_options.expire_tiles_max_bbox,
3533
m_options.projection),
@@ -66,7 +64,8 @@ output_multi_t::clone(std::shared_ptr<middle_query_t> const &mid) const
6664
}
6765

6866
int output_multi_t::start() {
69-
m_table->start();
67+
m_table->start(m_options.database_options.conninfo(),
68+
m_options.tblsmain_data);
7069
return 0;
7170
}
7271

@@ -178,7 +177,10 @@ int output_multi_t::pending_relation(osmid_t id, int exists) {
178177

179178
void output_multi_t::stop(osmium::thread::Pool *pool)
180179
{
181-
pool->submit(std::bind(&table_t::stop, m_table.get()));
180+
pool->submit([this]() {
181+
m_table->stop(m_options.slim & !m_options.droptemp,
182+
m_options.enable_hstore_index, m_options.tblsmain_index);
183+
});
182184
if (m_options.expire_tiles_zoom_min > 0) {
183185
m_expire.output_and_destroy(m_options.expire_tiles_filename.c_str(),
184186
m_options.expire_tiles_zoom_min);

output-pgsql.cpp

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
#include <unistd.h>
2323

2424
#include <boost/algorithm/string/predicate.hpp>
25-
#include <boost/bind.hpp>
2625
#include <boost/exception_ptr.hpp>
2726
#include <boost/format.hpp>
2827

@@ -242,7 +241,10 @@ void output_pgsql_t::stop(osmium::thread::Pool *pool)
242241
{
243242
// attempt to stop tables in parallel
244243
for (auto &t : m_tables) {
245-
pool->submit(std::bind(&table_t::stop, t.get()));
244+
pool->submit([&]() {
245+
t->stop(m_options.slim & !m_options.droptemp,
246+
m_options.enable_hstore_index, m_options.tblsmain_index);
247+
});
246248
}
247249

248250
if (m_options.expire_tiles_zoom_min > 0) {
@@ -490,7 +492,8 @@ int output_pgsql_t::start()
490492
{
491493
for (auto &t : m_tables) {
492494
//setup the table in postgres
493-
t->start();
495+
t->start(m_options.database_options.conninfo(),
496+
m_options.tblsmain_data);
494497
}
495498

496499
return 0;
@@ -517,7 +520,7 @@ output_pgsql_t::output_pgsql_t(std::shared_ptr<middle_query_t> const &mid,
517520
m_tagtransform = tagtransform_t::make_tagtransform(&m_options, exlist);
518521

519522
//for each table
520-
for (int i = 0; i < t_MAX; i++) {
523+
for (size_t i = 0; i < t_MAX; i++) {
521524

522525
//figure out the columns this table needs
523526
columns_t columns = exlist.normal_columns(
@@ -549,15 +552,10 @@ output_pgsql_t::output_pgsql_t(std::shared_ptr<middle_query_t> const &mid,
549552
util::exit_nicely();
550553
}
551554

552-
//tremble in awe of this massive constructor! seriously we are trying to avoid passing an
553-
//options object because we want to make use of the table_t in output_mutli_t which could
554-
//have a different tablespace/hstores/etc per table
555-
m_tables[i].reset(new table_t(
556-
m_options.database_options.conninfo(), name, type, columns,
557-
m_options.hstore_columns, m_options.projection->target_srs(),
558-
m_options.append, m_options.slim, m_options.droptemp,
559-
m_options.hstore_mode, m_options.enable_hstore_index,
560-
m_options.tblsmain_data, m_options.tblsmain_index));
555+
m_tables[i].reset(new table_t(name, type, columns,
556+
m_options.hstore_columns,
557+
m_options.projection->target_srs(),
558+
m_options.append, m_options.hstore_mode));
561559
}
562560
}
563561

output.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
#include <stdexcept>
1010

1111
#include <boost/format.hpp>
12-
#include <boost/functional/hash.hpp>
1312
#include <boost/property_tree/ptree.hpp>
1413
#include <boost/property_tree/json_parser.hpp>
1514

table.cpp

Lines changed: 43 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,12 @@ typedef boost::format fmt;
1616

1717
#define BUFFER_SEND_SIZE 1024
1818

19-
20-
table_t::table_t(const string& conninfo, const string& name, const string& type, const columns_t& columns, const hstores_t& hstore_columns,
21-
const int srid, const bool append, const bool slim, const bool drop_temp, const int hstore_mode,
22-
const bool enable_hstore_index, const boost::optional<string>& table_space, const boost::optional<string>& table_space_index) :
23-
conninfo(conninfo), name(name), type(type), sql_conn(nullptr), copyMode(false), srid((fmt("%1%") % srid).str()),
24-
append(append), slim(slim), drop_temp(drop_temp), hstore_mode(hstore_mode), enable_hstore_index(enable_hstore_index),
25-
columns(columns), hstore_columns(hstore_columns), table_space(table_space), table_space_index(table_space_index)
19+
table_t::table_t(string const &name, string const &type,
20+
columns_t const &columns, hstores_t const &hstore_columns,
21+
int const srid, bool const append, int const hstore_mode)
22+
: name(name), type(type), sql_conn(nullptr), copyMode(false),
23+
srid((fmt("%1%") % srid).str()), append(append), hstore_mode(hstore_mode),
24+
columns(columns), hstore_columns(hstore_columns)
2625
{
2726
//if we dont have any columns
2827
if(columns.size() == 0 && hstore_mode != HSTORE_ALL)
@@ -36,11 +35,13 @@ table_t::table_t(const string& conninfo, const string& name, const string& type,
3635
del_fmt = fmt("DELETE FROM %1% WHERE osm_id = %2%");
3736
}
3837

39-
table_t::table_t(const table_t& other):
40-
conninfo(other.conninfo), name(other.name), type(other.type), sql_conn(nullptr), copyMode(false), buffer(), srid(other.srid),
41-
append(other.append), slim(other.slim), drop_temp(other.drop_temp), hstore_mode(other.hstore_mode), enable_hstore_index(other.enable_hstore_index),
42-
columns(other.columns), hstore_columns(other.hstore_columns), copystr(other.copystr), table_space(other.table_space),
43-
table_space_index(other.table_space_index), single_fmt(other.single_fmt), del_fmt(other.del_fmt)
38+
table_t::table_t(table_t const &other)
39+
: m_conninfo(other.m_conninfo), name(other.name), type(other.type),
40+
sql_conn(nullptr), copyMode(false), srid(other.srid), append(other.append),
41+
hstore_mode(other.hstore_mode), columns(other.columns),
42+
hstore_columns(other.hstore_columns), copystr(other.copystr),
43+
m_table_space(other.m_table_space), single_fmt(other.single_fmt),
44+
del_fmt(other.del_fmt)
4445
{
4546
// if the other table has already started, then we want to execute
4647
// the same stuff to get into the same state. but if it hasn't, then
@@ -89,19 +90,23 @@ void table_t::commit()
8990
void table_t::connect()
9091
{
9192
//connect
92-
PGconn* _conn = PQconnectdb(conninfo.c_str());
93+
PGconn *_conn = PQconnectdb(m_conninfo.c_str());
9394
if (PQstatus(_conn) != CONNECTION_OK)
9495
throw std::runtime_error((fmt("Connection to database failed: %1%\n") % PQerrorMessage(_conn)).str());
9596
sql_conn = _conn;
9697
//let commits happen faster by delaying when they actually occur
9798
pgsql_exec_simple(sql_conn, PGRES_COMMAND_OK, "SET synchronous_commit TO off;");
9899
}
99100

100-
void table_t::start()
101+
void table_t::start(std::string const &conninfo,
102+
boost::optional<std::string> const &table_space)
101103
{
102104
if(sql_conn)
103105
throw std::runtime_error(name + " cannot start, its already started");
104106

107+
m_conninfo = conninfo;
108+
m_table_space = table_space ? " TABLESPACE " + table_space.get() : "";
109+
105110
connect();
106111
fprintf(stderr, "Setting up table: %s\n", name.c_str());
107112
pgsql_exec_simple(sql_conn, PGRES_COMMAND_OK, "SET client_min_messages = WARNING");
@@ -144,20 +149,10 @@ void table_t::start()
144149
// doesn't need to be RESET on these tables
145150
sql += " WITH ( autovacuum_enabled = FALSE )";
146151
//add the main table space
147-
if (table_space)
148-
sql += " TABLESPACE " + table_space.get();
152+
sql += m_table_space;
149153

150154
//create the table
151155
pgsql_exec_simple(sql_conn, PGRES_COMMAND_OK, sql);
152-
153-
//slim mode needs this to be able to delete from tables in pending
154-
if (slim && !drop_temp) {
155-
sql = (fmt("CREATE INDEX ON %1% USING BTREE (osm_id)") % name).str();
156-
if (table_space_index)
157-
sql += " TABLESPACE " + table_space_index.get();
158-
pgsql_exec_simple(sql_conn, PGRES_COMMAND_OK, sql);
159-
}
160-
161156
}//appending
162157
else {
163158
//check the columns against those in the existing table
@@ -208,7 +203,8 @@ void table_t::start()
208203
copyMode = true;
209204
}
210205

211-
void table_t::stop()
206+
void table_t::stop(bool updateable, bool enable_hstore_index,
207+
boost::optional<std::string> const &table_space_index)
212208
{
213209
stop_copy();
214210
if (!append)
@@ -221,14 +217,13 @@ void table_t::stop()
221217
if (srid == "4326") {
222218
/* libosmium assures validity of geometries in 4326, so the WHERE can be skipped.
223219
Because we know the geom is already in 4326, no reprojection is needed for GeoHashing */
224-
pgsql_exec_simple(
225-
sql_conn, PGRES_COMMAND_OK,
226-
(fmt("CREATE TABLE %1%_tmp %2% AS\n"
227-
" SELECT * FROM %1%\n"
228-
" ORDER BY ST_GeoHash(way,10)\n"
229-
" COLLATE \"C\"") %
230-
name % (table_space ? "TABLESPACE " + table_space.get() : ""))
231-
.str());
220+
pgsql_exec_simple(sql_conn, PGRES_COMMAND_OK,
221+
(fmt("CREATE TABLE %1%_tmp %2% AS\n"
222+
" SELECT * FROM %1%\n"
223+
" ORDER BY ST_GeoHash(way,10)\n"
224+
" COLLATE \"C\"") %
225+
name % m_table_space)
226+
.str());
232227
} else {
233228
/* osm2pgsql's transformation from 4326 to another projection could make a geometry invalid,
234229
and these need to be filtered. Also, a transformation is needed for geohashing.
@@ -244,7 +239,7 @@ void table_t::stop()
244239
" ORDER BY ST_GeoHash(ST_Transform(ST_Envelope(way),4326),10)\n"
245240
// clang-format on
246241
" COLLATE \"C\"") %
247-
name % (table_space ? "TABLESPACE " + table_space.get() : ""))
242+
name % m_table_space)
248243
.str());
249244
pgsql_exec_simple(sql_conn, PGRES_COMMAND_OK, "RESET client_min_messages");
250245
}
@@ -253,17 +248,23 @@ void table_t::stop()
253248
fprintf(stderr, "Copying %s to cluster by geometry finished\n", name.c_str());
254249
fprintf(stderr, "Creating geometry index on %s\n", name.c_str());
255250

251+
std::string tblspc_sql =
252+
table_space_index ? "TABLESPACE " + table_space_index.get() : "";
256253
// Use fillfactor 100 for un-updatable imports
257-
pgsql_exec_simple(sql_conn, PGRES_COMMAND_OK, (fmt("CREATE INDEX ON %1% USING GIST (way) %2% %3%") % name %
258-
(slim && !drop_temp ? "" : "WITH (FILLFACTOR=100)") %
259-
(table_space_index ? "TABLESPACE " + table_space_index.get() : "")).str());
254+
pgsql_exec_simple(sql_conn, PGRES_COMMAND_OK,
255+
(fmt("CREATE INDEX ON %1% USING GIST (way) %2% %3%") %
256+
name % (updateable ? "" : "WITH (FILLFACTOR=100)") %
257+
tblspc_sql)
258+
.str());
260259

261260
/* slim mode needs this to be able to apply diffs */
262-
if (slim && !drop_temp)
263-
{
261+
if (updateable) {
264262
fprintf(stderr, "Creating osm_id index on %s\n", name.c_str());
265-
pgsql_exec_simple(sql_conn, PGRES_COMMAND_OK, (fmt("CREATE INDEX ON %1% USING BTREE (osm_id) %2%") % name %
266-
(table_space_index ? "TABLESPACE " + table_space_index.get() : "")).str());
263+
pgsql_exec_simple(
264+
sql_conn, PGRES_COMMAND_OK,
265+
(fmt("CREATE INDEX ON %1% USING BTREE (osm_id) %2%") % name %
266+
tblspc_sql)
267+
.str());
267268
if (srid != "4326") {
268269
pgsql_exec_simple(
269270
sql_conn, PGRES_COMMAND_OK,

table.hpp

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,16 @@ typedef std::vector<std::string> hstores_t;
1919
class table_t
2020
{
2121
public:
22-
table_t(const std::string& conninfo, const std::string& name, const std::string& type, const columns_t& columns, const hstores_t& hstore_columns, const int srid,
23-
const bool append, const bool slim, const bool droptemp, const int hstore_mode, const bool enable_hstore_index,
24-
const boost::optional<std::string>& table_space, const boost::optional<std::string>& table_space_index);
22+
table_t(std::string const &name, std::string const &type,
23+
columns_t const &columns, hstores_t const &hstore_columns,
24+
const int srid, const bool append, const int hstore_mode);
2525
table_t(const table_t& other);
2626
~table_t();
2727

28-
void start();
29-
void stop();
28+
void start(std::string const &conninfo,
29+
boost::optional<std::string> const &table_space);
30+
void stop(bool updateable, bool enable_hstore_index,
31+
boost::optional<std::string> const &table_space_index);
3032

3133
void begin();
3234
void commit();
@@ -81,23 +83,19 @@ class table_t
8183
void escape4hstore(const char *src, std::string& dst);
8284
void escape_type(const std::string &value, ColumnType flags, std::string& dst);
8385

84-
std::string conninfo;
86+
std::string m_conninfo;
8587
std::string name;
8688
std::string type;
8789
pg_conn *sql_conn;
8890
bool copyMode;
8991
std::string buffer;
9092
std::string srid;
9193
bool append;
92-
bool slim;
93-
bool drop_temp;
9494
int hstore_mode;
95-
bool enable_hstore_index;
9695
columns_t columns;
9796
hstores_t hstore_columns;
9897
std::string copystr;
99-
boost::optional<std::string> table_space;
100-
boost::optional<std::string> table_space_index;
98+
std::string m_table_space;
10199

102100
boost::format single_fmt, del_fmt;
103101
};

0 commit comments

Comments
 (0)