Skip to content

Commit 4556f68

Browse files
authored
Merge pull request #1831 from joto/cleanup-output-construction
Cleanup output construction
2 parents ab21493 + e212e16 commit 4556f68

File tree

10 files changed

+130
-103
lines changed

10 files changed

+130
-103
lines changed

src/output-flex.cpp

Lines changed: 30 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1964,76 +1964,65 @@ void output_flex_t::relation_modify(osmium::Relation const &rel)
19641964
relation_add(rel);
19651965
}
19661966

1967-
void output_flex_t::init_clone()
1967+
void output_flex_t::start()
19681968
{
19691969
for (auto &table : m_table_connections) {
19701970
table.connect(get_options()->database_options.conninfo());
1971-
table.prepare();
1971+
table.start(get_options()->append);
19721972
}
19731973
}
19741974

1975-
void output_flex_t::start()
1975+
output_flex_t::output_flex_t(output_flex_t const *other,
1976+
std::shared_ptr<middle_query_t> mid,
1977+
std::shared_ptr<db_copy_thread_t> copy_thread)
1978+
: output_t(other, std::move(mid)), m_tables(other->m_tables),
1979+
m_stage2_way_ids(other->m_stage2_way_ids),
1980+
m_copy_thread(std::move(copy_thread)), m_lua_state(other->m_lua_state),
1981+
m_expire(other->get_options()->expire_tiles_zoom,
1982+
other->get_options()->expire_tiles_max_bbox,
1983+
other->get_options()->projection),
1984+
m_process_node(other->m_process_node), m_process_way(other->m_process_way),
1985+
m_process_relation(other->m_process_relation),
1986+
m_select_relation_members(other->m_select_relation_members)
19761987
{
1977-
for (auto &table : m_table_connections) {
1978-
table.connect(get_options()->database_options.conninfo());
1979-
table.start(get_options()->append);
1988+
for (auto &table : *m_tables) {
1989+
auto &tc = m_table_connections.emplace_back(&table, m_copy_thread);
1990+
tc.connect(get_options()->database_options.conninfo());
1991+
tc.prepare();
19801992
}
19811993
}
19821994

19831995
std::shared_ptr<output_t>
19841996
output_flex_t::clone(std::shared_ptr<middle_query_t> const &mid,
19851997
std::shared_ptr<db_copy_thread_t> const &copy_thread) const
19861998
{
1987-
return std::make_shared<output_flex_t>(
1988-
mid, m_thread_pool, *get_options(), copy_thread, true, m_lua_state,
1989-
m_process_node, m_process_way, m_process_relation,
1990-
m_select_relation_members, m_tables, m_stage2_way_ids);
1999+
return std::make_shared<output_flex_t>(this, mid, copy_thread);
19912000
}
19922001

19932002
output_flex_t::output_flex_t(
19942003
std::shared_ptr<middle_query_t> const &mid,
19952004
std::shared_ptr<thread_pool_t> thread_pool, options_t const &o,
1996-
std::shared_ptr<db_copy_thread_t> const &copy_thread, bool is_clone,
1997-
std::shared_ptr<lua_State> lua_state, prepared_lua_function_t process_node,
1998-
prepared_lua_function_t process_way,
1999-
prepared_lua_function_t process_relation,
2000-
prepared_lua_function_t select_relation_members,
2001-
std::shared_ptr<std::vector<flex_table_t>> tables,
2002-
std::shared_ptr<idset_t> stage2_way_ids)
2003-
: output_t(mid, std::move(thread_pool), o), m_tables(std::move(tables)),
2004-
m_stage2_way_ids(std::move(stage2_way_ids)), m_copy_thread(copy_thread),
2005-
m_lua_state(std::move(lua_state)),
2006-
m_expire(o.expire_tiles_zoom, o.expire_tiles_max_bbox, o.projection),
2007-
m_process_node(process_node), m_process_way(process_way),
2008-
m_process_relation(process_relation),
2009-
m_select_relation_members(select_relation_members)
2010-
{
2011-
assert(copy_thread);
2012-
2013-
if (!is_clone) {
2014-
init_lua(get_options()->style);
2015-
2016-
// If the osm2pgsql.select_relation_members() Lua function is defined
2017-
// it means we need two-stage processing which in turn means we need
2018-
// the full ways stored in the middle.
2019-
if (m_select_relation_members) {
2020-
m_output_requirements.full_ways = true;
2021-
}
2005+
std::shared_ptr<db_copy_thread_t> const &copy_thread)
2006+
: output_t(mid, std::move(thread_pool), o), m_copy_thread(copy_thread),
2007+
m_expire(o.expire_tiles_zoom, o.expire_tiles_max_bbox, o.projection)
2008+
{
2009+
init_lua(get_options()->style);
2010+
2011+
// If the osm2pgsql.select_relation_members() Lua function is defined
2012+
// it means we need two-stage processing which in turn means we need
2013+
// the full ways stored in the middle.
2014+
if (m_select_relation_members) {
2015+
m_output_requirements.full_ways = true;
20222016
}
20232017

20242018
if (m_tables->empty()) {
20252019
throw std::runtime_error{
20262020
"No tables defined in Lua config. Nothing to do!"};
20272021
}
20282022

2029-
assert(m_table_connections.empty());
20302023
for (auto &table : *m_tables) {
20312024
m_table_connections.emplace_back(&table, m_copy_thread);
20322025
}
2033-
2034-
if (is_clone) {
2035-
init_clone();
2036-
}
20372026
}
20382027

20392028
/**

src/output-flex.hpp

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -97,20 +97,17 @@ class prepared_lua_function_t
9797

9898
class output_flex_t : public output_t
9999
{
100-
101100
public:
101+
/// Constructor for new objects
102102
output_flex_t(
103103
std::shared_ptr<middle_query_t> const &mid,
104104
std::shared_ptr<thread_pool_t> thread_pool, options_t const &options,
105-
std::shared_ptr<db_copy_thread_t> const &copy_thread,
106-
bool is_clone = false, std::shared_ptr<lua_State> lua_state = nullptr,
107-
prepared_lua_function_t process_node = {},
108-
prepared_lua_function_t process_way = {},
109-
prepared_lua_function_t process_relation = {},
110-
prepared_lua_function_t select_relation_members = {},
111-
std::shared_ptr<std::vector<flex_table_t>> tables =
112-
std::make_shared<std::vector<flex_table_t>>(),
113-
std::shared_ptr<idset_t> stage2_way_ids = std::make_shared<idset_t>());
105+
std::shared_ptr<db_copy_thread_t> const &copy_thread);
106+
107+
/// Constructor for cloned objects
108+
output_flex_t(output_flex_t const *other,
109+
std::shared_ptr<middle_query_t> mid,
110+
std::shared_ptr<db_copy_thread_t> copy_thread);
114111

115112
output_flex_t(output_flex_t const &) = delete;
116113
output_flex_t &operator=(output_flex_t const &) = delete;
@@ -174,7 +171,6 @@ class output_flex_t : public output_t
174171
int table_columns();
175172

176173
private:
177-
void init_clone();
178174
void select_relation_members();
179175

180176
/**
@@ -281,12 +277,14 @@ class output_flex_t : public output_t
281277

282278
}; // relation_cache_t
283279

284-
std::shared_ptr<std::vector<flex_table_t>> m_tables;
280+
std::shared_ptr<std::vector<flex_table_t>> m_tables =
281+
std::make_shared<std::vector<flex_table_t>>();
282+
285283
std::vector<table_connection_t> m_table_connections;
286284

287285
// This is shared between all clones of the output and must only be
288286
// accessed while protected using the lua_mutex.
289-
std::shared_ptr<idset_t> m_stage2_way_ids;
287+
std::shared_ptr<idset_t> m_stage2_way_ids = std::make_shared<idset_t>();
290288

291289
std::shared_ptr<db_copy_thread_t> m_copy_thread;
292290

@@ -300,10 +298,10 @@ class output_flex_t : public output_t
300298
relation_cache_t m_relation_cache;
301299
osmium::Node const *m_context_node = nullptr;
302300

303-
prepared_lua_function_t m_process_node;
304-
prepared_lua_function_t m_process_way;
305-
prepared_lua_function_t m_process_relation;
306-
prepared_lua_function_t m_select_relation_members;
301+
prepared_lua_function_t m_process_node{};
302+
prepared_lua_function_t m_process_way{};
303+
prepared_lua_function_t m_process_relation{};
304+
prepared_lua_function_t m_select_relation_members{};
307305

308306
calling_context m_calling_context = calling_context::main;
309307

src/output-gazetteer.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
#include <memory>
2222
#include <string>
2323

24+
output_gazetteer_t::~output_gazetteer_t() = default;
25+
2426
void output_gazetteer_t::delete_unused_classes(char osm_type, osmid_t osm_id)
2527
{
2628
if (get_options()->append) {

src/output-gazetteer.hpp

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -28,32 +28,39 @@ struct middle_query_t;
2828

2929
class output_gazetteer_t : public output_t
3030
{
31-
output_gazetteer_t(output_gazetteer_t const *other,
32-
std::shared_ptr<middle_query_t> const &cloned_mid,
33-
std::shared_ptr<db_copy_thread_t> const &copy_thread)
34-
: output_t(cloned_mid, other->m_thread_pool, *other->get_options()),
35-
m_copy(copy_thread), m_proj(other->get_options()->projection),
36-
m_osmium_buffer(PLACE_BUFFER_SIZE, osmium::memory::Buffer::auto_grow::yes)
37-
{}
38-
3931
public:
32+
/// Constructor for new objects
4033
output_gazetteer_t(std::shared_ptr<middle_query_t> const &mid,
4134
std::shared_ptr<thread_pool_t> thread_pool,
4235
options_t const &options,
4336
std::shared_ptr<db_copy_thread_t> const &copy_thread)
4437
: output_t(mid, std::move(thread_pool), options), m_copy(copy_thread),
45-
m_proj(options.projection),
46-
m_osmium_buffer(PLACE_BUFFER_SIZE, osmium::memory::Buffer::auto_grow::yes)
38+
m_proj(options.projection)
4739
{
4840
m_style.load_style(options.style);
4941
}
5042

43+
/// Constructor for cloned objects
44+
output_gazetteer_t(output_gazetteer_t const *other,
45+
std::shared_ptr<middle_query_t> const &mid,
46+
std::shared_ptr<db_copy_thread_t> const &copy_thread)
47+
: output_t(other, mid), m_copy(copy_thread),
48+
m_proj(other->get_options()->projection)
49+
{}
50+
51+
output_gazetteer_t(output_gazetteer_t const &) = delete;
52+
output_gazetteer_t &operator=(output_gazetteer_t const &) = delete;
53+
54+
output_gazetteer_t(output_gazetteer_t &&) = delete;
55+
output_gazetteer_t &operator=(output_gazetteer_t &&) = delete;
56+
57+
~output_gazetteer_t() override;
58+
5159
std::shared_ptr<output_t>
5260
clone(std::shared_ptr<middle_query_t> const &mid,
5361
std::shared_ptr<db_copy_thread_t> const &copy_thread) const override
5462
{
55-
return std::shared_ptr<output_t>(
56-
new output_gazetteer_t{this, mid, copy_thread});
63+
return std::make_shared<output_gazetteer_t>(this, mid, copy_thread);
5764
}
5865

5966
void start() override;
@@ -93,7 +100,8 @@ class output_gazetteer_t : public output_t
93100
gazetteer_style_t m_style;
94101

95102
std::shared_ptr<reprojection> m_proj;
96-
osmium::memory::Buffer m_osmium_buffer;
103+
osmium::memory::Buffer m_osmium_buffer{
104+
PLACE_BUFFER_SIZE, osmium::memory::Buffer::auto_grow::yes};
97105
};
98106

99107
#endif // OSM2PGSQL_OUTPUT_GAZETTEER_HPP

src/output-null.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@
99

1010
#include "output-null.hpp"
1111

12-
std::shared_ptr<output_t>
13-
output_null_t::clone(std::shared_ptr<middle_query_t> const &mid,
14-
std::shared_ptr<db_copy_thread_t> const &) const
12+
std::shared_ptr<output_t> output_null_t::clone(
13+
std::shared_ptr<middle_query_t> const & /*mid*/,
14+
std::shared_ptr<db_copy_thread_t> const & /*copy_thread*/) const
1515
{
16-
return std::make_shared<output_null_t>(mid, m_thread_pool, *get_options());
16+
return std::make_shared<output_null_t>(*this);
1717
}
1818

1919
output_null_t::output_null_t(std::shared_ptr<middle_query_t> const &mid,

src/output-null.hpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,12 @@ class output_null_t : public output_t
2222
std::shared_ptr<thread_pool_t> thread_pool,
2323
options_t const &options);
2424

25+
output_null_t(output_null_t const &) = default;
26+
output_null_t &operator=(output_null_t const &) = default;
27+
28+
output_null_t(output_null_t &&) = default;
29+
output_null_t &operator=(output_null_t &&) = default;
30+
2531
~output_null_t() override;
2632

2733
std::shared_ptr<output_t>

src/output-pgsql.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -416,8 +416,7 @@ std::shared_ptr<output_t> output_pgsql_t::clone(
416416
std::shared_ptr<middle_query_t> const &mid,
417417
std::shared_ptr<db_copy_thread_t> const &copy_thread) const
418418
{
419-
return std::shared_ptr<output_t>(
420-
new output_pgsql_t{this, mid, copy_thread});
419+
return std::make_shared<output_pgsql_t>(this, mid, copy_thread);
421420
}
422421

423422
output_pgsql_t::output_pgsql_t(
@@ -481,8 +480,7 @@ output_pgsql_t::output_pgsql_t(
481480
output_pgsql_t::output_pgsql_t(
482481
output_pgsql_t const *other, std::shared_ptr<middle_query_t> const &mid,
483482
std::shared_ptr<db_copy_thread_t> const &copy_thread)
484-
: output_t(mid, other->m_thread_pool, *other->get_options()),
485-
m_tagtransform(other->m_tagtransform->clone()),
483+
: output_t(other, mid), m_tagtransform(other->m_tagtransform->clone()),
486484
m_enable_way_area(other->m_enable_way_area),
487485
m_proj(get_options()->projection),
488486
m_expire(get_options()->expire_tiles_zoom,

src/output-pgsql.hpp

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,6 @@
2727

2828
class output_pgsql_t : public output_t
2929
{
30-
output_pgsql_t(output_pgsql_t const *other,
31-
std::shared_ptr<middle_query_t> const &mid,
32-
std::shared_ptr<db_copy_thread_t> const &copy_thread);
33-
3430
public:
3531
enum table_id
3632
{
@@ -41,11 +37,23 @@ class output_pgsql_t : public output_t
4137
t_MAX
4238
};
4339

40+
/// Constructor for new objects
4441
output_pgsql_t(std::shared_ptr<middle_query_t> const &mid,
4542
std::shared_ptr<thread_pool_t> thread_pool,
4643
options_t const &options,
4744
std::shared_ptr<db_copy_thread_t> const &copy_thread);
4845

46+
/// Constructor for cloned objects
47+
output_pgsql_t(output_pgsql_t const *other,
48+
std::shared_ptr<middle_query_t> const &mid,
49+
std::shared_ptr<db_copy_thread_t> const &copy_thread);
50+
51+
output_pgsql_t(output_pgsql_t const &) = delete;
52+
output_pgsql_t &operator=(output_pgsql_t const &) = delete;
53+
54+
output_pgsql_t(output_pgsql_t &&) = delete;
55+
output_pgsql_t &operator=(output_pgsql_t &&) = delete;
56+
4957
~output_pgsql_t() override;
5058

5159
std::shared_ptr<output_t>

src/output.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -66,14 +66,14 @@ output_t::output_t(std::shared_ptr<middle_query_t> mid,
6666
options_t const &options)
6767
: m_mid(std::move(mid)), m_options(&options),
6868
m_thread_pool(std::move(thread_pool))
69+
{}
6970

71+
output_t::output_t(output_t const *other, std::shared_ptr<middle_query_t> mid)
72+
: m_mid(std::move(mid)), m_options(other->m_options),
73+
m_thread_pool(other->m_thread_pool),
74+
m_output_requirements(other->m_output_requirements)
7075
{}
7176

7277
output_t::~output_t() = default;
7378

74-
void output_t::free_middle_references()
75-
{
76-
m_mid.reset();
77-
}
78-
79-
void output_t::merge_expire_trees(output_t *) {}
79+
void output_t::free_middle_references() { m_mid.reset(); }

0 commit comments

Comments
 (0)