forked from osm2pgsql-dev/osm2pgsql
-
Notifications
You must be signed in to change notification settings - Fork 0
/
db-copy.cpp
238 lines (194 loc) · 5.99 KB
/
db-copy.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
#include <boost/format.hpp>
#include <cassert>
#include <cstdio>
#include <future>
#include <thread>
#include "db-copy.hpp"
#include "pgsql.hpp"
using fmt = boost::format;
db_copy_thread_t::db_copy_thread_t(std::string const &conninfo)
: m_conninfo(conninfo), m_conn(nullptr)
{
m_worker = std::thread([this]() {
try {
worker_thread();
} catch (std::runtime_error const &e) {
fprintf(stderr, "DB writer thread failed due to ERROR: %s\n",
e.what());
exit(2);
}
});
}
db_copy_thread_t::~db_copy_thread_t() { finish(); }
void db_copy_thread_t::add_buffer(std::unique_ptr<db_cmd_t> &&buffer)
{
assert(m_worker.joinable()); // thread must not have been finished
std::unique_lock<std::mutex> lock(m_queue_mutex);
m_worker_queue.push_back(std::move(buffer));
m_queue_cond.notify_one();
}
void db_copy_thread_t::sync_and_wait()
{
std::promise<void> barrier;
std::future<void> sync = barrier.get_future();
add_buffer(std::unique_ptr<db_cmd_t>(new db_cmd_sync_t(std::move(barrier))));
sync.wait();
}
void db_copy_thread_t::finish()
{
if (m_worker.joinable()) {
finish_copy();
add_buffer(std::unique_ptr<db_cmd_t>(new db_cmd_finish_t()));
m_worker.join();
}
}
void db_copy_thread_t::worker_thread()
{
connect();
bool done = false;
while (!done) {
std::unique_ptr<db_cmd_t> item;
{
std::unique_lock<std::mutex> lock(m_queue_mutex);
if (m_worker_queue.empty()) {
m_queue_cond.wait(lock);
continue;
}
item = std::move(m_worker_queue.front());
m_worker_queue.pop_front();
}
switch (item->type) {
case db_cmd_t::Cmd_copy:
write_to_db(static_cast<db_cmd_copy_t *>(item.get()));
break;
case db_cmd_t::Cmd_sync:
finish_copy();
static_cast<db_cmd_sync_t *>(item.get())->barrier.set_value();
break;
case db_cmd_t::Cmd_finish:
done = true;
break;
}
}
finish_copy();
disconnect();
}
void db_copy_thread_t::connect()
{
assert(!m_conn);
PGconn *conn = PQconnectdb(m_conninfo.c_str());
if (PQstatus(conn) != CONNECTION_OK)
throw std::runtime_error(
(fmt("Connection to database failed: %1%\n") % PQerrorMessage(conn))
.str());
m_conn = conn;
// Let commits happen faster by delaying when they actually occur.
pgsql_exec_simple(m_conn, PGRES_COMMAND_OK,
"SET synchronous_commit TO off;");
}
void db_copy_thread_t::disconnect()
{
if (!m_conn)
return;
PQfinish(m_conn);
m_conn = nullptr;
}
void db_copy_thread_t::write_to_db(db_cmd_copy_t *buffer)
{
if (!buffer->deletables.empty() ||
(m_inflight && !buffer->target->same_copy_target(*m_inflight.get())))
finish_copy();
if (!buffer->deletables.empty())
delete_rows(buffer);
if (!m_inflight)
start_copy(buffer->target);
pgsql_CopyData(buffer->target->name.c_str(), m_conn, buffer->buffer);
}
void db_copy_thread_t::delete_rows(db_cmd_copy_t *buffer)
{
assert(!m_inflight);
std::string sql = "DELETE FROM ";
sql.reserve(buffer->target->name.size() + buffer->deletables.size() * 15 +
30);
sql += buffer->target->name;
sql += " WHERE ";
sql += buffer->target->id;
sql += " IN (";
for (auto id : buffer->deletables) {
sql += std::to_string(id);
sql += ',';
}
sql[sql.size() - 1] = ')';
pgsql_exec_simple(m_conn, PGRES_COMMAND_OK, sql);
}
void db_copy_thread_t::start_copy(std::shared_ptr<db_target_descr_t> const &target)
{
m_inflight = target;
std::string copystr = "COPY ";
copystr.reserve(target->name.size() + target->rows.size() + 14);
copystr += target->name;
if (!target->rows.empty()) {
copystr += '(';
copystr += target->rows;
copystr += ')';
}
copystr += " FROM STDIN";
pgsql_exec_simple(m_conn, PGRES_COPY_IN, copystr);
m_inflight = target;
}
void db_copy_thread_t::finish_copy()
{
if (!m_inflight)
return;
if (PQputCopyEnd(m_conn, nullptr) != 1)
throw std::runtime_error((fmt("stop COPY_END for %1% failed: %2%\n") %
m_inflight->name %
PQerrorMessage(m_conn))
.str());
pg_result_t res(PQgetResult(m_conn));
if (PQresultStatus(res.get()) != PGRES_COMMAND_OK)
throw std::runtime_error((fmt("result COPY_END for %1% failed: %2%\n") %
m_inflight->name %
PQerrorMessage(m_conn))
.str());
m_inflight.reset();
}
db_copy_mgr_t::db_copy_mgr_t(std::shared_ptr<db_copy_thread_t> const &processor)
: m_processor(processor)
{}
void db_copy_mgr_t::new_line(std::shared_ptr<db_target_descr_t> const &table)
{
if (!m_current || !m_current->target->same_copy_target(*table.get())) {
if (m_current) {
m_processor->add_buffer(std::move(m_current));
}
m_current.reset(new db_cmd_copy_t(table));
}
}
void db_copy_mgr_t::delete_id(osmid_t osm_id)
{
assert(m_current);
m_current->deletables.push_back(osm_id);
}
void db_copy_mgr_t::sync()
{
// finish any ongoing copy operations
if (m_current) {
m_processor->add_buffer(std::move(m_current));
}
m_processor->sync_and_wait();
}
void db_copy_mgr_t::finish_line()
{
assert(m_current);
auto &buf = m_current->buffer;
assert(!buf.empty());
// Expect that a column has been written last which ended in a '\t'.
// Replace it with the row delimiter '\n'.
auto sz = buf.size();
assert(buf[sz - 1] == '\t');
buf[sz - 1] = '\n';
if (sz > db_cmd_copy_t::Max_buf_size - 100) {
m_processor->add_buffer(std::move(m_current));
}
}