forked from osm2pgsql-dev/osm2pgsql
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdb-copy.cpp
More file actions
202 lines (164 loc) · 5.96 KB
/
Copy pathdb-copy.cpp
File metadata and controls
202 lines (164 loc) · 5.96 KB
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
#include <cassert>
#include "db-copy.hpp"
#include "format.hpp"
#include "pgsql.hpp"
void db_deleter_by_id_t::delete_rows(std::string const &table,
std::string const &column, pg_conn_t *conn)
{
fmt::memory_buffer sql;
// Each deletable contributes an OSM ID and a comma. The highest node ID
// currently has 10 digits, so 15 characters should do for a couple of years.
// Add 50 characters for the SQL statement itself.
sql.reserve(m_deletables.size() * 15 + 50);
fmt::format_to(sql, FMT_STRING("DELETE FROM {} WHERE {} IN ("), table,
column);
for (auto id : m_deletables) {
format_to(sql, FMT_STRING("{},"), id);
}
sql[sql.size() - 1] = ')';
sql.push_back('\0');
conn->exec(sql.data());
}
void db_deleter_by_type_and_id_t::delete_rows(std::string const &table,
std::string const &column,
pg_conn_t *conn)
{
assert(!m_deletables.empty());
fmt::memory_buffer sql;
// Need a VALUES line for each deletable: type (3 bytes), id (15 bytes),
// braces etc. (4 bytes). And additional space for the remainder of the
// SQL command.
sql.reserve(m_deletables.size() * 22 + 200);
if (m_has_type) {
fmt::format_to(sql, "DELETE FROM {} p USING (VALUES ", table);
for (auto const &item : m_deletables) {
fmt::format_to(sql, FMT_STRING("('{}',{}),"), item.osm_type,
item.osm_id);
}
sql.resize(sql.size() - 1);
auto const pos = column.find(',');
assert(pos != std::string::npos);
std::string type = column.substr(0, pos);
fmt::format_to(sql,
") AS t (osm_type, osm_id) WHERE"
" p.{} = t.osm_type AND p.{} = t.osm_id",
type, column.c_str() + pos + 1);
} else {
fmt::format_to(sql, FMT_STRING("DELETE FROM {} WHERE {} IN ("), table,
column);
for (auto const &item : m_deletables) {
format_to(sql, FMT_STRING("{},"), item.osm_id);
}
sql[sql.size() - 1] = ')';
}
sql.push_back('\0');
conn->exec(sql.data());
}
db_copy_thread_t::db_copy_thread_t(std::string const &conninfo)
{
// conninfo is captured by copy here, because we don't know wether the
// reference will still be valid once we get around to running the thread
m_worker = std::thread{thread_t{conninfo, m_shared}};
}
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_shared.queue_mutex};
m_shared.queue_full_cond.wait(lock, [&] {
return m_shared.worker_queue.size() < db_cmd_copy_t::Max_buffers;
});
m_shared.worker_queue.push_back(std::move(buffer));
m_shared.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()) {
add_buffer(std::unique_ptr<db_cmd_t>(new db_cmd_finish_t{}));
m_worker.join();
}
}
db_copy_thread_t::thread_t::thread_t(std::string conninfo, shared &shared)
: m_conninfo(std::move(conninfo)), m_shared(shared)
{}
void db_copy_thread_t::thread_t::operator()()
{
try {
m_conn.reset(new pg_conn_t{m_conninfo});
// Let commits happen faster by delaying when they actually occur.
m_conn->exec("SET synchronous_commit = off");
bool done = false;
while (!done) {
std::unique_ptr<db_cmd_t> item;
{
std::unique_lock<std::mutex> lock{m_shared.queue_mutex};
m_shared.queue_cond.wait(
lock, [&] { return !m_shared.worker_queue.empty(); });
item = std::move(m_shared.worker_queue.front());
m_shared.worker_queue.pop_front();
m_shared.queue_full_cond.notify_one();
}
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();
m_conn.reset();
} catch (std::runtime_error const &e) {
fmt::print(stderr, "DB copy thread failed: {}\n", e.what());
exit(2);
}
}
void db_copy_thread_t::thread_t::write_to_db(db_cmd_copy_t *buffer)
{
if (buffer->has_deletables() ||
(m_inflight && !buffer->target->same_copy_target(*m_inflight.get()))) {
finish_copy();
}
buffer->delete_data(m_conn.get());
if (!m_inflight) {
start_copy(buffer->target);
}
m_conn->copy_data(buffer->buffer, buffer->target->name);
}
void db_copy_thread_t::thread_t::start_copy(
std::shared_ptr<db_target_descr_t> const &target)
{
assert(!m_inflight);
auto const qname = qualified_name(target->schema, target->name);
fmt::memory_buffer sql;
sql.reserve(qname.size() + target->rows.size() + 20);
if (target->rows.empty()) {
fmt::format_to(sql, FMT_STRING("COPY {} FROM STDIN"), qname);
} else {
fmt::format_to(sql, FMT_STRING("COPY {} ({}) FROM STDIN"), qname,
target->rows);
}
sql.push_back('\0');
m_conn->query(PGRES_COPY_IN, sql.data());
m_inflight = target;
}
void db_copy_thread_t::thread_t::finish_copy()
{
if (m_inflight) {
m_conn->end_copy(m_inflight->name);
m_inflight.reset();
}
}