forked from osm2pgsql-dev/osm2pgsql
-
Notifications
You must be signed in to change notification settings - Fork 0
/
db-copy.hpp
448 lines (394 loc) · 11.1 KB
/
db-copy.hpp
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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
#ifndef DB_COPY_HPP
#define DB_COPY_HPP
#include <condition_variable>
#include <deque>
#include <future>
#include <memory>
#include <mutex>
#include <string>
#include <thread>
#include <vector>
#include "osmtypes.hpp"
struct pg_conn;
/**
* Table information necessary for building SQL queries.
*/
struct db_target_descr_t
{
/// Name of the target table for the copy operation.
std::string name;
/// Comma-separated list of rows for copy operation (when empty: all rows)
std::string rows;
/// Name of id column used when deleting objects.
std::string id;
/**
* Check if the buffer would use exactly the same copy operation.
*/
bool same_copy_target(db_target_descr_t const &other) const noexcept
{
return (this == &other) || (name == other.name && rows == other.rows);
}
db_target_descr_t() = default;
db_target_descr_t(char const *n, char const *i, char const *r = "")
: name(n), rows(r), id(i)
{
}
};
/**
* A command for the copy thread to execute.
*/
class db_cmd_t
{
public:
enum cmd_t
{
Cmd_copy, ///< Copy buffer content into given target.
Cmd_sync, ///< Synchronize with parent.
Cmd_finish
};
virtual ~db_cmd_t() = default;
cmd_t type;
protected:
explicit db_cmd_t(cmd_t t)
: type(t)
{
}
};
struct db_cmd_copy_t : public db_cmd_t
{
enum { Max_buf_size = 10 * 1024 * 1024 };
/// Name of the target table for the copy operation
std::shared_ptr<db_target_descr_t> target;
/// Vector with object to delete before copying
std::vector<osmid_t> deletables;
/// actual copy buffer
std::string buffer;
explicit db_cmd_copy_t(std::shared_ptr<db_target_descr_t> const &t)
: db_cmd_t(db_cmd_t::Cmd_copy), target(t)
{
buffer.reserve(Max_buf_size);
}
};
struct db_cmd_sync_t : public db_cmd_t
{
std::promise<void> barrier;
explicit db_cmd_sync_t(std::promise<void> &&b)
: db_cmd_t(db_cmd_t::Cmd_sync), barrier(std::move(b))
{
}
};
struct db_cmd_finish_t : public db_cmd_t
{
db_cmd_finish_t() : db_cmd_t(db_cmd_t::Cmd_finish) {}
};
/**
* The worker thread that streams copy data into the database.
*/
class db_copy_thread_t
{
public:
db_copy_thread_t(std::string const &conninfo);
~db_copy_thread_t();
/**
* Add another command for the worker.
*/
void add_buffer(std::unique_ptr<db_cmd_t> &&buffer);
/**
* Send sync command and wait for the notification.
*/
void sync_and_wait();
/**
* Finish the copy process.
*
* Only returns when all remaining data has been committed to the
* database.
*/
void finish();
private:
void worker_thread();
void connect();
void disconnect();
void write_to_db(db_cmd_copy_t *buffer);
void start_copy(std::shared_ptr<db_target_descr_t> const &target);
void finish_copy();
void delete_rows(db_cmd_copy_t *buffer);
std::string m_conninfo;
pg_conn *m_conn;
std::thread m_worker;
std::mutex m_queue_mutex;
std::condition_variable m_queue_cond;
std::deque<std::unique_ptr<db_cmd_t>> m_worker_queue;
// Target for copy operation currently ongoing.
std::shared_ptr<db_target_descr_t> m_inflight;
};
/**
* Management class that fills and manages copy buffers.
*/
class db_copy_mgr_t
{
public:
db_copy_mgr_t(std::shared_ptr<db_copy_thread_t> const &processor);
/**
* Start a new table row.
*
* Also starts a new buffer if either the table is not the same as
* the table of currently buffered data or no buffer is pending.
*/
void new_line(std::shared_ptr<db_target_descr_t> const &table);
/**
* Finish a table row.
*
* Adds the row delimiter to the buffer. If the buffer is at capacity
* it will be forwarded to the copy thread.
*/
void finish_line();
/**
* Add many simple columns.
*
* See add_column().
*/
template <typename T, typename ...ARGS>
void add_columns(T value, ARGS&&... args)
{
add_column(value);
add_columns(args...);
}
template <typename T>
void add_columns(T value)
{
add_column(value);
}
/**
* Add a column entry of simple type.
*
* Writes the column with the escaping apporpriate for the type and
* a column delimiter.
*/
template <typename T>
void add_column(T value)
{
add_value(value);
m_current->buffer += '\t';
}
/**
* Add an empty column.
*
* Adds a NULL value for the column.
*/
void add_null_column() { m_current->buffer += "\\N\t"; }
/**
* Start an array column.
*
* An array is a list of simple elements of the same type.
*
* Must be finished with a call to finish_array().
*/
void new_array() { m_current->buffer += "{"; }
/**
* Add a single value to an array column.
*
* Adds the value in the format appropriate for an array and a value
* separator.
*/
template <typename T>
void add_array_elem(T value)
{
add_value(value);
m_current->buffer += ',';
}
void add_array_elem(std::string const &s) { add_array_elem(s.c_str()); }
void add_array_elem(char const *s)
{
assert(m_current);
m_current->buffer += '"';
add_escaped_string(s);
m_current->buffer += "\",";
}
/**
* Finish an array column previously started with new_array().
*
* The array may be empty. If it does contain elements, the separator after
* the final element is replaced with the closing array bracket.
*/
void finish_array()
{
auto idx = m_current->buffer.size() - 1;
if (m_current->buffer[idx] == '{')
m_current->buffer += '}';
else
m_current->buffer[idx] = '}';
m_current->buffer += '\t';
}
/**
* Start a hash column.
*
* A hash column contains a list of key/value pairs. May be represented
* by a hstore or json in Postgresql.
*
* currently a hstore column is written which does not have any start
* markers.
*
* Must be closed with a finish_hash() call.
*/
void new_hash() { /* nothing */}
void add_hash_elem(std::string const &k, std::string const &v)
{
add_hash_elem(k.c_str(), v.c_str());
}
/**
* Add a key/value pair to a hash column.
*
* Key and value must be strings and will be appropriately escaped.
* A separator for the next pair is added at the end.
*/
void add_hash_elem(char const *k, char const *v)
{
m_current->buffer += '"';
add_escaped_string(k);
m_current->buffer += "\"=>\"";
add_escaped_string(v);
m_current->buffer += "\",";
}
/**
* Add a key/value pair to a hash column without escaping.
*
* Key and value must be strings and will NOT be appropriately escaped.
* A separator for the next pair is added at the end.
*/
void add_hash_elem_noescape(char const *k, char const *v)
{
m_current->buffer += '"';
m_current->buffer += k;
m_current->buffer += "\"=>\"";
m_current->buffer += v;
m_current->buffer += "\",";
}
/**
* Add a key (unescaped) and a numeric value to a hash column.
*
* Key must be string and come from a safe source because it will NOT be
* escaped! The value should be convertible using std::to_string.
* A separator for the next pair is added at the end.
*
* This method is suitable to insert safe input, e.g. numeric OSM metadata
* (eg. uid) but not unsafe input like user names.
*/
template <typename T>
void add_hstore_num_noescape(char const *k, T const value)
{
m_current->buffer += '"';
m_current->buffer += k;
m_current->buffer += "\"=>\"";
m_current->buffer += std::to_string(value);
m_current->buffer += "\",";
}
/**
* Close a hash previously started with new_hash().
*
* The hash may be empty. If elements were present, the separator
* of the final element is overwritten with the closing \t.
*/
void finish_hash()
{
auto idx = m_current->buffer.size() - 1;
if (!m_current->buffer.empty() && m_current->buffer[idx] == ',') {
m_current->buffer[idx] = '\t';
} else {
m_current->buffer += '\t';
}
}
/**
* Add a column with the given WKB geometry in WKB hex format.
*
* The geometry is converted on-the-fly from WKB binary to WKB hex.
*/
void add_hex_geom(std::string const &wkb)
{
char const *lookup_hex = "0123456789ABCDEF";
for (char c : wkb) {
m_current->buffer += lookup_hex[(c >> 4) & 0xf];
m_current->buffer += lookup_hex[c & 0xf];
}
m_current->buffer += '\t';
}
/**
* Mark an OSM object for deletion in the current table.
*
* The object is guaranteed to be deleted before any lines
* following the delete_id() are inserted.
*/
void delete_id(osmid_t osm_id);
/**
* Synchronize with worker.
*
* Only returns when all previously issued commands are done.
*/
void sync();
private:
template <typename T>
void add_value(T value)
{
m_current->buffer += std::to_string(value);
}
void add_value(double value)
{
char tmp[32];
snprintf(tmp, sizeof(tmp), "%g", value);
m_current->buffer += tmp;
}
void add_value(std::string const &s) { add_value(s.c_str()); }
void add_value(char const *s)
{
assert(m_current);
for (char const *c = s; *c; ++c) {
switch (*c) {
case '"':
m_current->buffer += "\\\"";
break;
case '\\':
m_current->buffer += "\\\\";
break;
case '\n':
m_current->buffer += "\\n";
break;
case '\r':
m_current->buffer += "\\r";
break;
case '\t':
m_current->buffer += "\\t";
break;
default:
m_current->buffer += *c;
break;
}
}
}
void add_escaped_string(char const *s)
{
for (char const *c = s; *c; ++c) {
switch (*c) {
case '"':
m_current->buffer += "\\\\\"";
break;
case '\\':
m_current->buffer += "\\\\\\\\";
break;
case '\n':
m_current->buffer += "\\n";
break;
case '\r':
m_current->buffer += "\\r";
break;
case '\t':
m_current->buffer += "\\t";
break;
default:
m_current->buffer += *c;
break;
}
}
}
std::shared_ptr<db_copy_thread_t> m_processor;
std::unique_ptr<db_cmd_copy_t> m_current;
};
#endif