Skip to content

Commit f0433c2

Browse files
authored
Merge pull request #2209 from joto/variant-for-copy
Refactor: Simplifiy code by using std::variant for copy mgr
2 parents ee134ca + 6bd67a7 commit f0433c2

File tree

4 files changed

+169
-174
lines changed

4 files changed

+169
-174
lines changed

src/db-copy-mgr.hpp

Lines changed: 55 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -36,20 +36,19 @@ class db_copy_mgr_t
3636
*/
3737
void new_line(std::shared_ptr<db_target_descr_t> const &table)
3838
{
39-
if (!m_current || !m_current->target->same_copy_target(*table)) {
39+
if (!m_current || !m_current.target->same_copy_target(*table)) {
4040
if (m_current) {
41-
m_processor->add_buffer(std::move(m_current));
41+
m_processor->send_command(std::move(m_current));
4242
}
43-
44-
m_current = std::make_unique<db_cmd_copy_delete_t<DELETER>>(table);
43+
m_current = db_cmd_copy_delete_t<DELETER>(table);
4544
}
46-
m_committed = m_current->buffer.size();
45+
m_committed = m_current.buffer.size();
4746
}
4847

4948
void rollback_line()
5049
{
5150
assert(m_current);
52-
m_current->buffer.resize(m_committed);
51+
m_current.buffer.resize(m_committed);
5352
}
5453

5554
/**
@@ -62,16 +61,17 @@ class db_copy_mgr_t
6261
{
6362
assert(m_current);
6463

65-
auto &buf = m_current->buffer;
64+
auto &buf = m_current.buffer;
6665
assert(!buf.empty());
6766

6867
// Expect that a column has been written last which ended in a '\t'.
6968
// Replace it with the row delimiter '\n'.
7069
assert(buf.back() == '\t');
7170
buf.back() = '\n';
7271

73-
if (m_current->is_full()) {
74-
m_processor->add_buffer(std::move(m_current));
72+
if (m_current.is_full()) {
73+
m_processor->send_command(std::move(m_current));
74+
m_current = {};
7575
}
7676
}
7777

@@ -96,15 +96,15 @@ class db_copy_mgr_t
9696
void add_column(T value)
9797
{
9898
add_value(value);
99-
m_current->buffer += '\t';
99+
m_current.buffer += '\t';
100100
}
101101

102102
/**
103103
* Add an empty column.
104104
*
105105
* Adds a NULL value for the column.
106106
*/
107-
void add_null_column() { m_current->buffer += "\\N\t"; }
107+
void add_null_column() { m_current.buffer += "\\N\t"; }
108108

109109
/**
110110
* Start an array column.
@@ -113,7 +113,7 @@ class db_copy_mgr_t
113113
*
114114
* Must be finished with a call to finish_array().
115115
*/
116-
void new_array() { m_current->buffer += "{"; }
116+
void new_array() { m_current.buffer += "{"; }
117117

118118
/**
119119
* Add a single value to an array column.
@@ -124,7 +124,7 @@ class db_copy_mgr_t
124124
void add_array_elem(osmid_t value)
125125
{
126126
add_value(value);
127-
m_current->buffer += ',';
127+
m_current.buffer += ',';
128128
}
129129

130130
/**
@@ -135,13 +135,13 @@ class db_copy_mgr_t
135135
*/
136136
void finish_array()
137137
{
138-
assert(!m_current->buffer.empty());
139-
if (m_current->buffer.back() == '{') {
140-
m_current->buffer += '}';
138+
assert(!m_current.buffer.empty());
139+
if (m_current.buffer.back() == '{') {
140+
m_current.buffer += '}';
141141
} else {
142-
m_current->buffer.back() = '}';
142+
m_current.buffer.back() = '}';
143143
}
144-
m_current->buffer += '\t';
144+
m_current.buffer += '\t';
145145
}
146146

147147
/**
@@ -172,11 +172,11 @@ class db_copy_mgr_t
172172
*/
173173
void add_hash_elem(char const *k, char const *v)
174174
{
175-
m_current->buffer += '"';
175+
m_current.buffer += '"';
176176
add_escaped_string(k);
177-
m_current->buffer += "\"=>\"";
177+
m_current.buffer += "\"=>\"";
178178
add_escaped_string(v);
179-
m_current->buffer += "\",";
179+
m_current.buffer += "\",";
180180
}
181181

182182
/**
@@ -187,11 +187,11 @@ class db_copy_mgr_t
187187
*/
188188
void add_hash_elem_noescape(char const *k, char const *v)
189189
{
190-
m_current->buffer += '"';
191-
m_current->buffer += k;
192-
m_current->buffer += "\"=>\"";
193-
m_current->buffer += v;
194-
m_current->buffer += "\",";
190+
m_current.buffer += '"';
191+
m_current.buffer += k;
192+
m_current.buffer += "\"=>\"";
193+
m_current.buffer += v;
194+
m_current.buffer += "\",";
195195
}
196196

197197
/**
@@ -207,11 +207,11 @@ class db_copy_mgr_t
207207
template <typename T>
208208
void add_hstore_num_noescape(char const *k, T const value)
209209
{
210-
m_current->buffer += '"';
211-
m_current->buffer += k;
212-
m_current->buffer += "\"=>\"";
213-
m_current->buffer += std::to_string(value);
214-
m_current->buffer += "\",";
210+
m_current.buffer += '"';
211+
m_current.buffer += k;
212+
m_current.buffer += "\"=>\"";
213+
m_current.buffer += std::to_string(value);
214+
m_current.buffer += "\",";
215215
}
216216

217217
/**
@@ -222,11 +222,11 @@ class db_copy_mgr_t
222222
*/
223223
void finish_hash()
224224
{
225-
auto const idx = m_current->buffer.size() - 1;
226-
if (!m_current->buffer.empty() && m_current->buffer[idx] == ',') {
227-
m_current->buffer[idx] = '\t';
225+
auto const idx = m_current.buffer.size() - 1;
226+
if (!m_current.buffer.empty() && m_current.buffer[idx] == ',') {
227+
m_current.buffer[idx] = '\t';
228228
} else {
229-
m_current->buffer += '\t';
229+
m_current.buffer += '\t';
230230
}
231231
}
232232

@@ -241,10 +241,10 @@ class db_copy_mgr_t
241241

242242
for (auto c : wkb) {
243243
unsigned int const num = static_cast<unsigned char>(c);
244-
m_current->buffer += lookup_hex[(num >> 4U) & 0xfU];
245-
m_current->buffer += lookup_hex[num & 0xfU];
244+
m_current.buffer += lookup_hex[(num >> 4U) & 0xfU];
245+
m_current.buffer += lookup_hex[num & 0xfU];
246246
}
247-
m_current->buffer += '\t';
247+
m_current.buffer += '\t';
248248
}
249249

250250
/**
@@ -257,14 +257,15 @@ class db_copy_mgr_t
257257
void delete_object(ARGS &&... args)
258258
{
259259
assert(m_current);
260-
m_current->add_deletable(std::forward<ARGS>(args)...);
260+
m_current.add_deletable(std::forward<ARGS>(args)...);
261261
}
262262

263263
void flush()
264264
{
265265
// flush current buffer if there is one
266266
if (m_current) {
267-
m_processor->add_buffer(std::move(m_current));
267+
m_processor->send_command(std::move(m_current));
268+
m_current = {};
268269
}
269270
// close any ongoing copy operations
270271
m_processor->end_copy();
@@ -285,7 +286,7 @@ class db_copy_mgr_t
285286
template <typename T>
286287
void add_value(T value)
287288
{
288-
m_current->buffer += fmt::to_string(value);
289+
m_current.buffer += fmt::to_string(value);
289290
}
290291

291292
void add_value(std::string const &s) { add_value(s.c_str()); }
@@ -296,22 +297,22 @@ class db_copy_mgr_t
296297
for (char const *c = s; *c; ++c) {
297298
switch (*c) {
298299
case '"':
299-
m_current->buffer += "\\\"";
300+
m_current.buffer += "\\\"";
300301
break;
301302
case '\\':
302-
m_current->buffer += "\\\\";
303+
m_current.buffer += "\\\\";
303304
break;
304305
case '\n':
305-
m_current->buffer += "\\n";
306+
m_current.buffer += "\\n";
306307
break;
307308
case '\r':
308-
m_current->buffer += "\\r";
309+
m_current.buffer += "\\r";
309310
break;
310311
case '\t':
311-
m_current->buffer += "\\t";
312+
m_current.buffer += "\\t";
312313
break;
313314
default:
314-
m_current->buffer += *c;
315+
m_current.buffer += *c;
315316
break;
316317
}
317318
}
@@ -322,29 +323,29 @@ class db_copy_mgr_t
322323
for (char const *c = s; *c; ++c) {
323324
switch (*c) {
324325
case '"':
325-
m_current->buffer += R"(\\")";
326+
m_current.buffer += R"(\\")";
326327
break;
327328
case '\\':
328-
m_current->buffer += R"(\\\\)";
329+
m_current.buffer += R"(\\\\)";
329330
break;
330331
case '\n':
331-
m_current->buffer += "\\n";
332+
m_current.buffer += "\\n";
332333
break;
333334
case '\r':
334-
m_current->buffer += "\\r";
335+
m_current.buffer += "\\r";
335336
break;
336337
case '\t':
337-
m_current->buffer += "\\t";
338+
m_current.buffer += "\\t";
338339
break;
339340
default:
340-
m_current->buffer += *c;
341+
m_current.buffer += *c;
341342
break;
342343
}
343344
}
344345
}
345346

346347
std::shared_ptr<db_copy_thread_t> m_processor;
347-
std::unique_ptr<db_cmd_copy_delete_t<DELETER>> m_current;
348+
db_cmd_copy_delete_t<DELETER> m_current;
348349
std::size_t m_committed = 0;
349350
};
350351

0 commit comments

Comments
 (0)