Skip to content

Commit 821775e

Browse files
authored
Merge pull request #898 from lonvia/shared-pointer-for-middle
Hand outputs a shared pointer to the middle query object
2 parents 2f01a3c + 8857d51 commit 821775e

32 files changed

+298
-286
lines changed

osm2pgsql.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,11 +60,13 @@ int main(int argc, char *argv[])
6060

6161
if (options.slim) {
6262
auto mid = std::make_shared<middle_pgsql_t>();
63-
outputs = output_t::create_outputs(mid.get(), options);
63+
outputs = output_t::create_outputs(
64+
std::static_pointer_cast<middle_query_t>(mid), options);
6465
middle = std::static_pointer_cast<middle_t>(mid);
6566
} else {
6667
auto mid = std::make_shared<middle_ram_t>();
67-
outputs = output_t::create_outputs(mid.get(), options);
68+
outputs = output_t::create_outputs(
69+
std::static_pointer_cast<middle_query_t>(mid), options);
6870
middle = std::static_pointer_cast<middle_t>(mid);
6971
}
7072

osmdata.cpp

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -186,8 +186,7 @@ namespace {
186186
//since the fetching from middle should be faster than the processing in each backend.
187187

188188
struct pending_threaded_processor : public middle_t::pending_processor {
189-
typedef std::vector<std::shared_ptr<output_t>> output_vec_t;
190-
typedef std::pair<std::shared_ptr<middle_query_t>, output_vec_t> clone_t;
189+
using output_vec_t = std::vector<std::shared_ptr<output_t>>;
191190

192191
static void do_jobs(output_vec_t const& outputs, pending_queue_t& queue, size_t& ids_done, std::mutex& mutex, int append, bool ways) {
193192
while (true) {
@@ -253,11 +252,11 @@ struct pending_threaded_processor : public middle_t::pending_processor {
253252
//clone the outs
254253
output_vec_t out_clones;
255254
for (const auto& out: outs) {
256-
out_clones.push_back(out->clone(mid_clone.get()));
255+
out_clones.push_back(out->clone(mid_clone));
257256
}
258257

259258
//keep the clones for a specific thread to use
260-
clones.push_back(clone_t(mid_clone, out_clones));
259+
clones.push_back(out_clones);
261260
}
262261
}
263262

@@ -282,11 +281,10 @@ struct pending_threaded_processor : public middle_t::pending_processor {
282281

283282
//make the threads and start them
284283
std::vector<std::future<void>> workers;
285-
for (size_t i = 0; i < clones.size(); ++i) {
286-
workers.push_back(std::async(std::launch::async,
287-
do_jobs, std::cref(clones[i].second),
288-
std::ref(queue), std::ref(ids_done),
289-
std::ref(mutex), append, true));
284+
for (auto const &clone : clones) {
285+
workers.push_back(std::async(
286+
std::launch::async, do_jobs, std::cref(clone), std::ref(queue),
287+
std::ref(ids_done), std::ref(mutex), append, true));
290288
}
291289
workers.push_back(std::async(std::launch::async, print_stats,
292290
std::ref(queue), std::ref(mutex)));
@@ -315,10 +313,12 @@ struct pending_threaded_processor : public middle_t::pending_processor {
315313

316314
//collect all the new rels that became pending from each
317315
//output in each thread back to their respective main outputs
318-
for (const auto& clone: clones) {
316+
for (auto const &clone : clones) {
319317
//for each clone/original output
320-
for(output_vec_t::const_iterator original_output = outs.begin(), clone_output = clone.second.begin();
321-
original_output != outs.end() && clone_output != clone.second.end(); ++original_output, ++clone_output) {
318+
for (output_vec_t::const_iterator original_output = outs.begin(),
319+
clone_output = clone.begin();
320+
original_output != outs.end() && clone_output != clone.end();
321+
++original_output, ++clone_output) {
322322
//done copying ways for now
323323
clone_output->get()->commit();
324324
//merge the pending from this threads copy of output back
@@ -344,11 +344,10 @@ struct pending_threaded_processor : public middle_t::pending_processor {
344344

345345
//make the threads and start them
346346
std::vector<std::future<void>> workers;
347-
for (size_t i = 0; i < clones.size(); ++i) {
348-
workers.push_back(std::async(std::launch::async,
349-
do_jobs, std::cref(clones[i].second),
350-
std::ref(queue), std::ref(ids_done),
351-
std::ref(mutex), append, false));
347+
for (auto const &clone : clones) {
348+
workers.push_back(std::async(
349+
std::launch::async, do_jobs, std::cref(clone), std::ref(queue),
350+
std::ref(ids_done), std::ref(mutex), append, false));
352351
}
353352
workers.push_back(std::async(std::launch::async, print_stats,
354353
std::ref(queue), std::ref(mutex)));
@@ -376,10 +375,12 @@ struct pending_threaded_processor : public middle_t::pending_processor {
376375
ids_done = 0;
377376

378377
//collect all expiry tree informations together into one
379-
for (const auto& clone: clones) {
378+
for (auto const &clone : clones) {
380379
//for each clone/original output
381-
for(output_vec_t::const_iterator original_output = outs.begin(), clone_output = clone.second.begin();
382-
original_output != outs.end() && clone_output != clone.second.end(); ++original_output, ++clone_output) {
380+
for (output_vec_t::const_iterator original_output = outs.begin(),
381+
clone_output = clone.begin();
382+
original_output != outs.end() && clone_output != clone.end();
383+
++original_output, ++clone_output) {
383384
//done copying rels for now
384385
clone_output->get()->commit();
385386
//merge the expire tree from this threads copy of output back
@@ -389,8 +390,8 @@ struct pending_threaded_processor : public middle_t::pending_processor {
389390
}
390391

391392
private:
392-
//middle and output copies
393-
std::vector<clone_t> clones;
393+
// output copies, one vector per thread
394+
std::vector<output_vec_t> clones;
394395
output_vec_t outs; //would like to move ownership of outs to osmdata_t and middle passed to output_t instead of owned by it
395396
//how many jobs do we have in the queue to start with
396397
size_t ids_queued;

output-gazetteer.hpp

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -14,36 +14,38 @@
1414
#include "pgsql.hpp"
1515
#include "util.hpp"
1616

17-
18-
class output_gazetteer_t : public output_t {
19-
public:
20-
output_gazetteer_t(const middle_query_t *mid_, const options_t &options_)
21-
: output_t(mid_, options_), Connection(NULL), ConnectionDelete(NULL),
22-
ConnectionError(NULL), copy_active(false),
23-
m_builder(options_.projection, true), single_fmt("%1%"),
17+
class output_gazetteer_t : public output_t
18+
{
19+
output_gazetteer_t(output_gazetteer_t const *other,
20+
std::shared_ptr<middle_query_t> const &cloned_mid)
21+
: output_t(cloned_mid, other->m_options), Connection(NULL),
22+
ConnectionDelete(NULL), ConnectionError(NULL), copy_active(false),
23+
m_builder(other->m_options.projection, true),
24+
single_fmt(other->single_fmt),
2425
osmium_buffer(PLACE_BUFFER_SIZE, osmium::memory::Buffer::auto_grow::yes)
2526
{
2627
buffer.reserve(PLACE_BUFFER_SIZE);
27-
m_style.load_style(options_.style);
28+
connect();
2829
}
2930

30-
output_gazetteer_t(const output_gazetteer_t &other)
31-
: output_t(other.m_mid, other.m_options), Connection(NULL),
32-
ConnectionDelete(NULL), ConnectionError(NULL), copy_active(false),
33-
m_builder(other.m_options.projection, true), single_fmt(other.single_fmt),
31+
public:
32+
output_gazetteer_t(std::shared_ptr<middle_query_t> const &mid,
33+
options_t const &options)
34+
: output_t(mid, options), Connection(NULL), ConnectionDelete(NULL),
35+
ConnectionError(NULL), copy_active(false),
36+
m_builder(options.projection, true), single_fmt("%1%"),
3437
osmium_buffer(PLACE_BUFFER_SIZE, osmium::memory::Buffer::auto_grow::yes)
3538
{
3639
buffer.reserve(PLACE_BUFFER_SIZE);
37-
connect();
40+
m_style.load_style(options.style);
3841
}
3942

4043
virtual ~output_gazetteer_t() {}
4144

42-
std::shared_ptr<output_t> clone(const middle_query_t* cloned_middle) const override
45+
std::shared_ptr<output_t>
46+
clone(std::shared_ptr<middle_query_t> const &mid) const override
4347
{
44-
output_gazetteer_t *clone = new output_gazetteer_t(*this);
45-
clone->m_mid = cloned_middle;
46-
return std::shared_ptr<output_t>(clone);
48+
return std::shared_ptr<output_t>(new output_gazetteer_t(this, mid));
4749
}
4850

4951
int start() override;

output-multi.cpp

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,12 @@
1111
#include <boost/algorithm/string/predicate.hpp>
1212
#include <vector>
1313

14-
output_multi_t::output_multi_t(const std::string &name,
14+
output_multi_t::output_multi_t(std::string const &name,
1515
std::shared_ptr<geometry_processor> processor_,
16-
const struct export_list &export_list_,
17-
const middle_query_t *mid_,
18-
const options_t &options_)
19-
: output_t(mid_, options_),
16+
export_list const &export_list_,
17+
std::shared_ptr<middle_query_t> const &mid,
18+
options_t const &options)
19+
: output_t(mid, options),
2020
m_tagtransform(tagtransform_t::make_tagtransform(&m_options)),
2121
m_export_list(new export_list(export_list_)), m_processor(processor_),
2222
m_proj(m_options.projection),
@@ -40,31 +40,31 @@ output_multi_t::output_multi_t(const std::string &name,
4040
{
4141
}
4242

43-
output_multi_t::output_multi_t(const output_multi_t &other)
44-
: output_t(other.m_mid, other.m_options),
43+
output_multi_t::output_multi_t(output_multi_t const *other,
44+
std::shared_ptr<middle_query_t> const &mid)
45+
: output_t(mid, other->m_options),
4546
m_tagtransform(tagtransform_t::make_tagtransform(&m_options)),
46-
m_export_list(new export_list(*other.m_export_list)),
47-
m_processor(other.m_processor), m_proj(other.m_proj),
48-
m_osm_type(other.m_osm_type), m_table(new table_t(*other.m_table)),
47+
m_export_list(new export_list(*other->m_export_list)),
48+
m_processor(other->m_processor), m_proj(other->m_proj),
49+
m_osm_type(other->m_osm_type), m_table(new table_t(*other->m_table)),
4950
// NOTE: we need to know which ways were used by relations so each thread
5051
// must have a copy of the original marked done ways, its read only so its
5152
// ok
52-
ways_done_tracker(other.ways_done_tracker),
53+
ways_done_tracker(other->ways_done_tracker),
5354
m_expire(m_options.expire_tiles_zoom, m_options.expire_tiles_max_bbox,
5455
m_options.projection),
5556
buffer(1024, osmium::memory::Buffer::auto_grow::yes),
5657
m_builder(m_options.projection, m_options.enable_multi),
57-
m_way_area(other.m_way_area)
58+
m_way_area(other->m_way_area)
5859
{
5960
}
6061

6162
output_multi_t::~output_multi_t() = default;
6263

63-
std::shared_ptr<output_t> output_multi_t::clone(const middle_query_t* cloned_middle) const
64+
std::shared_ptr<output_t>
65+
output_multi_t::clone(std::shared_ptr<middle_query_t> const &mid) const
6466
{
65-
auto *clone = new output_multi_t(*this);
66-
clone->m_mid = cloned_middle;
67-
return std::shared_ptr<output_t>(clone);
67+
return std::shared_ptr<output_t>(new output_multi_t(this, mid));
6868
}
6969

7070
int output_multi_t::start() {
@@ -365,7 +365,7 @@ int output_multi_t::process_relation(osmium::Relation const &rel,
365365
if (!filter) {
366366
//TODO: move this into geometry processor, figure a way to come back for tag transform
367367
//grab ways/nodes of the members in the relation, bail if none were used
368-
if (m_relation_helper.set(rel, m_mid) < 1)
368+
if (m_relation_helper.set(rel, m_mid.get()) < 1)
369369
return 0;
370370

371371
//NOTE: make_polygon is preset here this is to force the tag matching
@@ -383,7 +383,7 @@ int output_multi_t::process_relation(osmium::Relation const &rel,
383383
outtags, true);
384384
if (!filter)
385385
{
386-
m_relation_helper.add_way_locations(m_mid);
386+
m_relation_helper.add_way_locations(m_mid.get());
387387
auto geoms = m_processor->process_relation(
388388
rel, m_relation_helper.data, &m_builder);
389389
for (const auto geom : geoms) {

output-multi.hpp

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,21 @@ struct export_list;
2323
struct middle_query_t;
2424
struct options_t;
2525

26-
class output_multi_t : public output_t {
26+
class output_multi_t : public output_t
27+
{
28+
output_multi_t(output_multi_t const *other,
29+
std::shared_ptr<middle_query_t> const &mid);
30+
2731
public:
28-
output_multi_t(const std::string &name,
32+
output_multi_t(std::string const &name,
2933
std::shared_ptr<geometry_processor> processor_,
30-
const export_list &export_list_,
31-
const middle_query_t* mid_, const options_t &options_);
32-
output_multi_t(const output_multi_t& other);
34+
export_list const &export_list_,
35+
std::shared_ptr<middle_query_t> const &mid,
36+
options_t const &options);
3337
virtual ~output_multi_t();
3438

35-
std::shared_ptr<output_t> clone(const middle_query_t* cloned_middle) const override;
39+
std::shared_ptr<output_t>
40+
clone(std::shared_ptr<middle_query_t> const &mid) const override;
3641

3742
int start() override;
3843
void stop(osmium::thread::Pool *pool) override;

output-null.cpp

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -62,17 +62,22 @@ int output_null_t::relation_modify(osmium::Relation const &) {
6262
return 0;
6363
}
6464

65-
std::shared_ptr<output_t> output_null_t::clone(const middle_query_t* cloned_middle) const {
66-
output_null_t *clone = new output_null_t(*this);
67-
clone->m_mid = cloned_middle;
68-
return std::shared_ptr<output_t>(clone);
65+
std::shared_ptr<output_t>
66+
output_null_t::clone(std::shared_ptr<middle_query_t> const &mid) const
67+
{
68+
return std::shared_ptr<output_t>(new output_null_t(this, mid));
6969
}
7070

71-
output_null_t::output_null_t(const middle_query_t* mid_, const options_t &options_): output_t(mid_, options_) {
71+
output_null_t::output_null_t(std::shared_ptr<middle_query_t> const &mid,
72+
options_t const &options)
73+
: output_t(mid, options)
74+
{
7275
}
7376

74-
output_null_t::output_null_t(const output_null_t& other): output_t(other.m_mid, other.m_options) {
77+
output_null_t::output_null_t(output_null_t const *other,
78+
std::shared_ptr<middle_query_t> const &mid)
79+
: output_t(mid, other->m_options)
80+
{
7581
}
7682

77-
output_null_t::~output_null_t() {
78-
}
83+
output_null_t::~output_null_t() = default;

output-null.hpp

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,16 @@
77
#include "output.hpp"
88

99
class output_null_t : public output_t {
10+
output_null_t(output_null_t const *other,
11+
std::shared_ptr<middle_query_t> const &mid);
12+
1013
public:
11-
output_null_t(const middle_query_t* mid_, const options_t &options);
12-
output_null_t(const output_null_t& other);
14+
output_null_t(std::shared_ptr<middle_query_t> const &mid,
15+
options_t const &options);
1316
virtual ~output_null_t();
1417

15-
std::shared_ptr<output_t> clone(const middle_query_t* cloned_middle) const override;
18+
std::shared_ptr<output_t>
19+
clone(std::shared_ptr<middle_query_t> const &mid) const override;
1620

1721
int start() override;
1822
void stop(osmium::thread::Pool *pool) override;

output-pgsql.cpp

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -502,14 +502,14 @@ int output_pgsql_t::start()
502502
return 0;
503503
}
504504

505-
std::shared_ptr<output_t> output_pgsql_t::clone(const middle_query_t* cloned_middle) const
505+
std::shared_ptr<output_t>
506+
output_pgsql_t::clone(std::shared_ptr<middle_query_t> const &mid) const
506507
{
507-
auto *clone = new output_pgsql_t(*this);
508-
clone->m_mid = cloned_middle;
509-
return std::shared_ptr<output_t>(clone);
508+
return std::shared_ptr<output_t>(new output_pgsql_t(this, mid));
510509
}
511510

512-
output_pgsql_t::output_pgsql_t(const middle_query_t *mid, const options_t &o)
511+
output_pgsql_t::output_pgsql_t(std::shared_ptr<middle_query_t> const &mid,
512+
options_t const &o)
513513
: output_t(mid, o), m_builder(o.projection, o.enable_multi),
514514
expire(o.expire_tiles_zoom, o.expire_tiles_max_bbox, o.projection),
515515
ways_done_tracker(new id_tracker()),
@@ -576,23 +576,24 @@ output_pgsql_t::output_pgsql_t(const middle_query_t *mid, const options_t &o)
576576
}
577577
}
578578

579-
output_pgsql_t::output_pgsql_t(const output_pgsql_t &other)
580-
: output_t(other.m_mid, other.m_options),
579+
output_pgsql_t::output_pgsql_t(output_pgsql_t const *other,
580+
std::shared_ptr<middle_query_t> const &mid)
581+
: output_t(mid, other->m_options),
581582
m_tagtransform(tagtransform_t::make_tagtransform(&m_options)),
582-
m_enable_way_area(other.m_enable_way_area),
583-
m_export_list(new export_list(*other.m_export_list)),
584-
m_builder(m_options.projection, other.m_options.enable_multi),
583+
m_enable_way_area(other->m_enable_way_area),
584+
m_export_list(new export_list(*other->m_export_list)),
585+
m_builder(m_options.projection, other->m_options.enable_multi),
585586
expire(m_options.expire_tiles_zoom, m_options.expire_tiles_max_bbox,
586587
m_options.projection),
587588
//NOTE: we need to know which ways were used by relations so each thread
588589
//must have a copy of the original marked done ways, its read only so its ok
589-
ways_done_tracker(other.ways_done_tracker),
590+
ways_done_tracker(other->ways_done_tracker),
590591
buffer(1024, osmium::memory::Buffer::auto_grow::yes),
591592
rels_buffer(1024, osmium::memory::Buffer::auto_grow::yes)
592593
{
593-
for(std::vector<std::shared_ptr<table_t> >::const_iterator t = other.m_tables.begin(); t != other.m_tables.end(); ++t) {
594+
for (auto const &t : other->m_tables) {
594595
//copy constructor will just connect to the already there table
595-
m_tables.push_back(std::shared_ptr<table_t>(new table_t(**t)));
596+
m_tables.push_back(std::shared_ptr<table_t>(new table_t(*t.get())));
596597
}
597598
}
598599

0 commit comments

Comments
 (0)