Skip to content

Commit c422c49

Browse files
committed
Introduce flush when type of import object changes
This replaces the endCopy operation in the middle_t read functions. These functions are const and never should have called a function that changes state. Instead of falling out of copy then, all copy operations are finished, when processing of an OSM type is done. Read functions should never be called when actually processing the same table, so the one-time end-copy should be sufficient.
1 parent c9030bb commit c422c49

File tree

9 files changed

+51
-8
lines changed

9 files changed

+51
-8
lines changed

middle-pgsql.cpp

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -311,7 +311,8 @@ size_t middle_pgsql_t::local_nodes_get_list(osmium::WayNodeList *nodes) const
311311
// get any remaining nodes from the DB
312312
buffer[buffer.size() - 1] = '}';
313313

314-
pgsql_endCopy(node_table);
314+
// Nodes must have been written back at this point.
315+
assert(node_table->copyMode == 0);
315316

316317
PGconn *sql_conn = node_table->sql_conn;
317318

@@ -462,8 +463,8 @@ bool middle_pgsql_t::ways_get(osmid_t id, osmium::memory::Buffer &buffer) const
462463
char const *paramValues[1];
463464
PGconn *sql_conn = way_table->sql_conn;
464465

465-
// Make sure we're out of copy mode */
466-
pgsql_endCopy(way_table);
466+
// Make sure we're out of copy mode
467+
assert(way_table->copyMode == 0);
467468

468469
char tmp[16];
469470
snprintf(tmp, sizeof(tmp), "%" PRIdOSMID, id);
@@ -511,7 +512,8 @@ size_t middle_pgsql_t::rel_way_members_get(osmium::Relation const &rel,
511512
// replace last , with } to complete list of ids
512513
tmp2[tmp2.length() - 1] = '}';
513514

514-
pgsql_endCopy(way_table);
515+
// Make sures all ways have been written back.
516+
assert(way_table->copyMode == 0);
515517

516518
PGconn *sql_conn = way_table->sql_conn;
517519

@@ -696,8 +698,8 @@ bool middle_pgsql_t::relations_get(osmid_t id, osmium::memory::Buffer &buffer) c
696698
PGconn *sql_conn = rel_table->sql_conn;
697699
taglist_t member_temp;
698700

699-
// Make sure we're out of copy mode */
700-
pgsql_endCopy(rel_table);
701+
// Make sure we're out of copy mode
702+
assert(rel_table->copyMode == 0);
701703

702704
snprintf(tmp, sizeof(tmp), "%" PRIdOSMID, id);
703705
paramValues[0] = tmp;
@@ -791,7 +793,7 @@ idlist_t middle_pgsql_t::relations_using_way(osmid_t way_id) const
791793
char const *paramValues[1];
792794
char buffer[64];
793795
// Make sure we're out of copy mode */
794-
pgsql_endCopy( rel_table );
796+
assert(rel_table->copyMode == 0);
795797

796798
sprintf(buffer, "%" PRIdOSMID, way_id);
797799
paramValues[0] = buffer;
@@ -1029,6 +1031,13 @@ void middle_pgsql_t::commit(void) {
10291031
}
10301032
}
10311033

1034+
void middle_pgsql_t::flush()
1035+
{
1036+
for (auto &table : tables) {
1037+
pgsql_endCopy(&table);
1038+
}
1039+
}
1040+
10321041
void middle_pgsql_t::pgsql_stop_one(table_desc *table)
10331042
{
10341043
time_t start, end;

middle-pgsql.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ struct middle_pgsql_t : public slim_middle_t {
4343
void relations_delete(osmid_t id) override;
4444
void relation_changed(osmid_t id) override;
4545

46+
void flush() override;
47+
4648
void iterate_ways(middle_t::pending_processor& pf) override;
4749
void iterate_relations(pending_processor& pf) override;
4850

middle-ram.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,8 @@ struct middle_ram_t : public middle_t {
107107
int relations_delete(osmid_t id);
108108
int relation_changed(osmid_t id);
109109

110+
void flush() override {}
111+
110112
idlist_t relations_using_way(osmid_t way_id) const override;
111113

112114
void iterate_ways(middle_t::pending_processor& pf) override;

middle.hpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,9 @@ struct middle_t : public middle_query_t {
9292
virtual void ways_set(osmium::Way const &way) = 0;
9393
virtual void relations_set(osmium::Relation const &rel) = 0;
9494

95+
/// Write all pending data to permanent storage.
96+
virtual void flush() = 0;
97+
9598
struct pending_processor {
9699
virtual ~pending_processor() {}
97100
virtual void enqueue_ways(osmid_t id) = 0;

osmdata.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,11 @@ void osmdata_t::start() {
180180
mid->start(outs[0]->get_options());
181181
}
182182

183+
void osmdata_t::type_changed()
184+
{
185+
mid->flush();
186+
}
187+
183188
namespace {
184189

185190
//TODO: have the main thread using the main middle to query the middle for batches of ways (configurable number)

osmdata.hpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ class output_t;
1414
struct middle_t;
1515
class reprojection;
1616

17-
class osmdata_t {
17+
class osmdata_t
18+
{
1819
public:
1920
osmdata_t(std::shared_ptr<middle_t> mid_,
2021
std::shared_ptr<output_t> const &out_,
@@ -25,6 +26,7 @@ class osmdata_t {
2526
~osmdata_t();
2627

2728
void start();
29+
void type_changed();
2830
void stop();
2931

3032
int node_add(osmium::Node const &node);

parse-osmium.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,13 +124,19 @@ void parse_osmium_t::stream_file(const std::string &filename, const std::string
124124

125125
fprintf(stderr, "Using %s parser.\n", osmium::io::as_string(infile.format()));
126126

127+
m_type = osmium::item_type::node;
127128
osmium::io::Reader reader(infile);
128129
osmium::apply(reader, *this);
129130
reader.close();
130131
}
131132

132133
void parse_osmium_t::node(osmium::Node const &node)
133134
{
135+
if (m_type != osmium::item_type::node) {
136+
m_type = osmium::item_type::node;
137+
m_data->type_changed();
138+
}
139+
134140
if (node.deleted()) {
135141
m_data->node_delete(node.id());
136142
} else {
@@ -159,6 +165,11 @@ void parse_osmium_t::node(osmium::Node const &node)
159165

160166
void parse_osmium_t::way(osmium::Way& way)
161167
{
168+
if (m_type != osmium::item_type::way) {
169+
m_type = osmium::item_type::way;
170+
m_data->type_changed();
171+
}
172+
162173
if (way.deleted()) {
163174
m_data->way_delete(way.id());
164175
} else {
@@ -173,6 +184,11 @@ void parse_osmium_t::way(osmium::Way& way)
173184

174185
void parse_osmium_t::relation(osmium::Relation const &rel)
175186
{
187+
if (m_type != osmium::item_type::relation) {
188+
m_type = osmium::item_type::relation;
189+
m_data->type_changed();
190+
}
191+
176192
if (rel.deleted()) {
177193
m_data->relation_delete(rel.id());
178194
} else {

parse-osmium.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,8 @@ class parse_osmium_t: public osmium::handler::Handler
129129
bool m_append;
130130
boost::optional<osmium::Box> m_bbox;
131131
parse_stats_t m_stats;
132+
// Current type being parsed.
133+
osmium::item_type m_type;
132134
};
133135

134136
#endif

tests/mockups.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ struct dummy_middle_t : public middle_t {
99

1010
void start(const options_t *) override { }
1111
void stop(osmium::thread::Pool &) override {}
12+
void flush() override {}
1213
void cleanup(void) { }
1314
void analyze(void) override { }
1415
void commit(void) override { }
@@ -46,6 +47,7 @@ struct dummy_slim_middle_t : public slim_middle_t {
4647

4748
void start(const options_t *) override { }
4849
void stop(osmium::thread::Pool &) override {}
50+
void flush() override {}
4951
void cleanup(void) { }
5052
void analyze(void) override { }
5153
void commit(void) override { }

0 commit comments

Comments
 (0)