Skip to content

Commit 3c9dfda

Browse files
committed
Make db_target_descr_t into a real class
1 parent 565aa31 commit 3c9dfda

File tree

4 files changed

+71
-56
lines changed

4 files changed

+71
-56
lines changed

src/db-copy.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -188,24 +188,24 @@ void db_copy_thread_t::thread_t::write_to_db(db_cmd_copy_t *buffer)
188188
start_copy(buffer->target);
189189
}
190190

191-
m_conn->copy_send(buffer->buffer, buffer->target->name);
191+
m_conn->copy_send(buffer->buffer, buffer->target->name());
192192
}
193193

194194
void db_copy_thread_t::thread_t::start_copy(
195195
std::shared_ptr<db_target_descr_t> const &target)
196196
{
197197
assert(!m_inflight);
198198

199-
auto const qname = qualified_name(target->schema, target->name);
199+
auto const qname = qualified_name(target->schema(), target->name());
200200
fmt::memory_buffer sql;
201-
sql.reserve(qname.size() + target->rows.size() + 20);
202-
if (target->rows.empty()) {
201+
sql.reserve(qname.size() + target->rows().size() + 20);
202+
if (target->rows().empty()) {
203203
fmt::format_to(std::back_inserter(sql),
204204
FMT_STRING("COPY {} FROM STDIN"), qname);
205205
} else {
206206
fmt::format_to(std::back_inserter(sql),
207207
FMT_STRING("COPY {} ({}) FROM STDIN"), qname,
208-
target->rows);
208+
target->rows());
209209
}
210210

211211
sql.push_back('\0');
@@ -217,7 +217,7 @@ void db_copy_thread_t::thread_t::start_copy(
217217
void db_copy_thread_t::thread_t::finish_copy()
218218
{
219219
if (m_inflight) {
220-
m_conn->copy_end(m_inflight->name);
220+
m_conn->copy_end(m_inflight->name());
221221
m_inflight.reset();
222222
}
223223
}

src/db-copy.hpp

Lines changed: 33 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -27,35 +27,45 @@
2727
/**
2828
* Table information necessary for building SQL queries.
2929
*/
30-
struct db_target_descr_t
30+
class db_target_descr_t
3131
{
32-
/// Schema of the target table.
33-
std::string schema;
34-
/// Name of the target table for the copy operation.
35-
std::string name;
36-
/// Name of id column used when deleting objects.
37-
std::string id;
38-
/// Comma-separated list of rows for copy operation (when empty: all rows)
39-
std::string rows;
32+
public:
33+
db_target_descr_t(std::string schema, std::string name, std::string id,
34+
std::string rows = {})
35+
: m_schema(std::move(schema)), m_name(std::move(name)), m_id(std::move(id)),
36+
m_rows(std::move(rows))
37+
{
38+
assert(!m_schema.empty());
39+
assert(!m_name.empty());
40+
assert(!m_id.empty());
41+
}
42+
43+
std::string const &schema() const noexcept { return m_schema; }
44+
std::string const &name() const noexcept { return m_name; }
45+
std::string const &id() const noexcept { return m_id; }
46+
std::string const &rows() const noexcept { return m_rows; }
47+
48+
void set_rows(std::string rows) { m_rows = std::move(rows); }
4049

4150
/**
4251
* Check if the buffer would use exactly the same copy operation.
4352
*/
4453
bool same_copy_target(db_target_descr_t const &other) const noexcept
4554
{
46-
return (this == &other) || (schema == other.schema &&
47-
name == other.name && rows == other.rows);
55+
return (this == &other) ||
56+
(m_schema == other.m_schema && m_name == other.m_name &&
57+
m_id == other.m_id && m_rows == other.m_rows);
4858
}
4959

50-
db_target_descr_t() = delete;
51-
52-
db_target_descr_t(std::string s, std::string n, std::string i,
53-
std::string r = {})
54-
: schema(std::move(s)), name(std::move(n)), id(std::move(i)),
55-
rows(std::move(r))
56-
{
57-
assert(!schema.empty());
58-
}
60+
private:
61+
/// Schema of the target table.
62+
std::string m_schema;
63+
/// Name of the target table for the copy operation.
64+
std::string m_name;
65+
/// Name of id column used when deleting objects.
66+
std::string m_id;
67+
/// Comma-separated list of rows for copy operation (when empty: all rows)
68+
std::string m_rows;
5969
};
6070

6171
/**
@@ -203,8 +213,9 @@ class db_cmd_copy_delete_t : public db_cmd_copy_t
203213
void delete_data(pg_conn_t *conn) override
204214
{
205215
if (m_deleter.has_data()) {
206-
m_deleter.delete_rows(qualified_name(target->schema, target->name),
207-
target->id, conn);
216+
m_deleter.delete_rows(
217+
qualified_name(target->schema(), target->name()), target->id(),
218+
conn);
208219
}
209220
}
210221

src/middle-pgsql.hpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -133,10 +133,13 @@ struct middle_pgsql_t : public middle_t
133133

134134
std::string const &schema() const noexcept
135135
{
136-
return m_copy_target->schema;
136+
return m_copy_target->schema();
137137
}
138138

139-
std::string const &name() const noexcept { return m_copy_target->name; }
139+
std::string const &name() const noexcept
140+
{
141+
return m_copy_target->name();
142+
}
140143

141144
std::shared_ptr<db_target_descr_t> const &copy_target() const noexcept
142145
{

src/table.cpp

Lines changed: 27 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -75,17 +75,17 @@ void table_t::start(std::string const &conninfo, std::string const &table_space)
7575
{
7676
if (m_sql_conn) {
7777
throw fmt_error("{} cannot start, its already started.",
78-
m_target->name);
78+
m_target->name());
7979
}
8080

8181
m_conninfo = conninfo;
8282
m_table_space = tablespace_clause(table_space);
8383

8484
connect();
85-
log_info("Setting up table '{}'", m_target->name);
86-
auto const qual_name = qualified_name(m_target->schema, m_target->name);
87-
auto const qual_tmp_name = qualified_name(
88-
m_target->schema, m_target->name + "_tmp");
85+
log_info("Setting up table '{}'", m_target->name());
86+
auto const qual_name = qualified_name(m_target->schema(), m_target->name());
87+
auto const qual_tmp_name =
88+
qualified_name(m_target->schema(), m_target->name() + "_tmp");
8989

9090
// we are making a new table
9191
if (!m_append) {
@@ -132,8 +132,8 @@ void table_t::start(std::string const &conninfo, std::string const &table_space)
132132
m_sql_conn->exec(sql);
133133

134134
if (m_srid != "4326") {
135-
create_geom_check_trigger(m_sql_conn.get(), m_target->schema,
136-
m_target->name, "ST_IsValid(NEW.way)");
135+
create_geom_check_trigger(m_sql_conn.get(), m_target->schema(),
136+
m_target->name(), "ST_IsValid(NEW.way)");
137137
}
138138
}
139139

@@ -143,7 +143,7 @@ void table_t::start(std::string const &conninfo, std::string const &table_space)
143143
void table_t::prepare()
144144
{
145145
//let postgres cache this query as it will presumably happen a lot
146-
auto const qual_name = qualified_name(m_target->schema, m_target->name);
146+
auto const qual_name = qualified_name(m_target->schema(), m_target->name());
147147
m_sql_conn->exec("PREPARE get_wkb(int8) AS"
148148
" SELECT way FROM {} WHERE osm_id = $1",
149149
qual_name);
@@ -173,7 +173,7 @@ void table_t::generate_copy_column_list()
173173
// add geom column
174174
joiner.add("way");
175175

176-
m_target->rows = joiner();
176+
m_target->set_rows(joiner());
177177
}
178178

179179
void table_t::stop(bool updateable, bool enable_hstore_index,
@@ -182,17 +182,17 @@ void table_t::stop(bool updateable, bool enable_hstore_index,
182182
// make sure that all data is written to the DB before continuing
183183
m_copy.sync();
184184

185-
auto const qual_name = qualified_name(m_target->schema, m_target->name);
186-
auto const qual_tmp_name = qualified_name(
187-
m_target->schema, m_target->name + "_tmp");
185+
auto const qual_name = qualified_name(m_target->schema(), m_target->name());
186+
auto const qual_tmp_name =
187+
qualified_name(m_target->schema(), m_target->name() + "_tmp");
188188

189189
if (!m_append) {
190190
if (m_srid != "4326") {
191-
drop_geom_check_trigger(m_sql_conn.get(), m_target->schema,
192-
m_target->name);
191+
drop_geom_check_trigger(m_sql_conn.get(), m_target->schema(),
192+
m_target->name());
193193
}
194194

195-
log_info("Clustering table '{}' by geometry...", m_target->name);
195+
log_info("Clustering table '{}' by geometry...", m_target->name());
196196

197197
std::string sql = fmt::format("CREATE TABLE {} {} AS SELECT * FROM {}",
198198
qual_tmp_name, m_table_space, qual_name);
@@ -202,7 +202,7 @@ void table_t::stop(bool updateable, bool enable_hstore_index,
202202
sql += " ORDER BY ";
203203
if (postgis_version.major == 2 && postgis_version.minor < 4) {
204204
log_debug("Using GeoHash for clustering table '{}'",
205-
m_target->name);
205+
m_target->name());
206206
if (m_srid == "4326") {
207207
sql += "ST_GeoHash(way,10)";
208208
} else {
@@ -211,7 +211,7 @@ void table_t::stop(bool updateable, bool enable_hstore_index,
211211
sql += " COLLATE \"C\"";
212212
} else {
213213
log_debug("Using native order for clustering table '{}'",
214-
m_target->name);
214+
m_target->name());
215215
// Since Postgis 2.4 the order function for geometries gives
216216
// useful results.
217217
sql += "way";
@@ -221,9 +221,9 @@ void table_t::stop(bool updateable, bool enable_hstore_index,
221221

222222
m_sql_conn->exec("DROP TABLE {}", qual_name);
223223
m_sql_conn->exec(R"(ALTER TABLE {} RENAME TO "{}")", qual_tmp_name,
224-
m_target->name);
224+
m_target->name());
225225

226-
log_info("Creating geometry index on table '{}'...", m_target->name);
226+
log_info("Creating geometry index on table '{}'...", m_target->name());
227227

228228
// Use fillfactor 100 for un-updatable imports
229229
m_sql_conn->exec("CREATE INDEX ON {} USING GIST (way) {} {}", qual_name,
@@ -232,20 +232,21 @@ void table_t::stop(bool updateable, bool enable_hstore_index,
232232

233233
/* slim mode needs this to be able to apply diffs */
234234
if (updateable) {
235-
log_info("Creating osm_id index on table '{}'...", m_target->name);
235+
log_info("Creating osm_id index on table '{}'...",
236+
m_target->name());
236237
m_sql_conn->exec("CREATE INDEX ON {} USING BTREE (osm_id) {}",
237238
qual_name, tablespace_clause(table_space_index));
238239
if (m_srid != "4326") {
239-
create_geom_check_trigger(m_sql_conn.get(), m_target->schema,
240-
m_target->name,
240+
create_geom_check_trigger(m_sql_conn.get(), m_target->schema(),
241+
m_target->name(),
241242
"ST_IsValid(NEW.way)");
242243
}
243244
}
244245

245246
/* Create hstore index if selected */
246247
if (enable_hstore_index) {
247248
log_info("Creating hstore indexes on table '{}'...",
248-
m_target->name);
249+
m_target->name());
249250
if (m_hstore_mode != hstore_column::none) {
250251
m_sql_conn->exec("CREATE INDEX ON {} USING GIN (tags) {}",
251252
qual_name,
@@ -257,8 +258,8 @@ void table_t::stop(bool updateable, bool enable_hstore_index,
257258
tablespace_clause(table_space_index));
258259
}
259260
}
260-
log_info("Analyzing table '{}'...", m_target->name);
261-
analyze_table(*m_sql_conn, m_target->schema, m_target->name);
261+
log_info("Analyzing table '{}'...", m_target->name());
262+
analyze_table(*m_sql_conn, m_target->schema(), m_target->name());
262263
}
263264
teardown();
264265
}
@@ -369,7 +370,7 @@ void table_t::write_hstore_columns(taglist_t const &tags)
369370
void table_t::task_wait()
370371
{
371372
auto const run_time = m_task_result.wait();
372-
log_info("All postprocessing on table '{}' done in {}.", m_target->name,
373+
log_info("All postprocessing on table '{}' done in {}.", m_target->name(),
373374
util::human_readable_duration(run_time));
374375
}
375376

0 commit comments

Comments
 (0)