Skip to content

Commit e1db08c

Browse files
committed
Introduce clean after_nodes/ways/relations() functions in handlers
1 parent e01ff74 commit e1db08c

File tree

6 files changed

+53
-34
lines changed

6 files changed

+53
-34
lines changed

src/input.cpp

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,25 @@ prepare_input_files(std::vector<std::string> const &input_files,
204204
return files;
205205
}
206206

207+
static void apply(osmium::OSMObject &object, osmdata_t &osmdata,
208+
progress_display_t &progress)
209+
{
210+
static osmium::item_type last_type = osmium::item_type::node;
211+
212+
if (last_type != object.type()) {
213+
if (last_type == osmium::item_type::node) {
214+
osmdata.after_nodes();
215+
progress.after_nodes();
216+
} else if (last_type == osmium::item_type::way) {
217+
osmdata.after_ways();
218+
progress.after_ways();
219+
}
220+
last_type = object.type();
221+
}
222+
223+
osmium::apply_item(object, osmdata, progress);
224+
}
225+
207226
void process_file(osmium::io::File const &file, osmdata_t &osmdata,
208227
progress_display_t &progress, bool append)
209228
{
@@ -218,11 +237,12 @@ void process_file(osmium::io::File const &file, osmdata_t &osmdata,
218237
"Input file contains deleted objects but "
219238
"you are not in append mode."};
220239
}
221-
osmium::apply_item(object, osmdata, progress);
240+
apply(object, osmdata, progress);
222241
}
223242
}
224243

225-
osmdata.flush();
244+
osmdata.after_relations();
245+
progress.after_relations();
226246

227247
reader.close();
228248
}
@@ -258,7 +278,7 @@ void process_files(std::vector<osmium::io::File> const &files,
258278
"Input file contains deleted objects but "
259279
"you are not in append mode."};
260280
}
261-
osmium::apply_item(element.object(), osmdata, progress);
281+
apply(element.object(), osmdata, progress);
262282
}
263283

264284
auto *source = element.data_source();
@@ -267,7 +287,8 @@ void process_files(std::vector<osmium::io::File> const &files,
267287
}
268288
}
269289

270-
osmdata.flush();
290+
osmdata.after_relations();
291+
progress.after_relations();
271292

272293
for (auto &data_source : data_sources) {
273294
data_source.close();

src/osm2pgsql.cpp

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,6 @@
3737
#include "util.hpp"
3838
#include "version.hpp"
3939

40-
#include <osmium/io/file.hpp>
41-
42-
#include <ctime>
4340
#include <exception>
4441
#include <memory>
4542

@@ -91,7 +88,6 @@ int main(int argc, char *argv[])
9188
progress_display_t progress{get_logger().show_progress()};
9289
process_files(files, osmdata, progress, options.append);
9390

94-
progress.print_status(std::time(nullptr));
9591
fmt::print(stderr, " parse time: {}\n",
9692
util::human_readable_duration(timer_parse.stop()));
9793

src/osmdata.cpp

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -71,13 +71,10 @@ void osmdata_t::node(osmium::Node const &node)
7171
}
7272
}
7373

74+
void osmdata_t::after_nodes() { flush(); }
75+
7476
void osmdata_t::way(osmium::Way &way)
7577
{
76-
if (m_type != osmium::item_type::way) {
77-
m_type = osmium::item_type::way;
78-
flush();
79-
}
80-
8178
if (way.deleted()) {
8279
way_delete(way.id());
8380
} else {
@@ -89,13 +86,10 @@ void osmdata_t::way(osmium::Way &way)
8986
}
9087
}
9188

89+
void osmdata_t::after_ways() { flush(); }
90+
9291
void osmdata_t::relation(osmium::Relation const &rel)
9392
{
94-
if (m_type != osmium::item_type::relation) {
95-
m_type = osmium::item_type::relation;
96-
flush();
97-
}
98-
9993
if (rel.deleted()) {
10094
relation_delete(rel.id());
10195
} else {
@@ -110,6 +104,8 @@ void osmdata_t::relation(osmium::Relation const &rel)
110104
}
111105
}
112106

107+
void osmdata_t::after_relations() { flush(); }
108+
113109
void osmdata_t::node_add(osmium::Node const &node) const
114110
{
115111
m_mid->node_set(node);

src/osmdata.hpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,9 @@ class osmdata_t : public osmium::handler::Handler
4343
void way(osmium::Way &way);
4444
void relation(osmium::Relation const &rel);
4545

46-
void flush() const;
46+
void after_nodes();
47+
void after_ways();
48+
void after_relations();
4749

4850
/**
4951
* Rest of the processing (stages 1b, 1c, 2, and database postprocessing).
@@ -64,6 +66,8 @@ class osmdata_t : public osmium::handler::Handler
6466
void way_delete(osmid_t id) const;
6567
void relation_delete(osmid_t id) const;
6668

69+
void flush() const;
70+
6771
/**
6872
* Run stage 1b and stage 1c processing: Process dependent objects in
6973
* append mode.
@@ -92,9 +96,6 @@ class osmdata_t : public osmium::handler::Handler
9296
// imported).
9397
osmium::Box m_bbox;
9498

95-
// Current type being parsed.
96-
osmium::item_type m_type = osmium::item_type::node;
97-
9899
int m_num_procs;
99100
bool m_append;
100101
bool m_droptemp;

src/progress-display.cpp

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,8 @@ void progress_display_t::possibly_print_status()
3232
{
3333
std::time_t const now = std::time(nullptr);
3434

35-
if (m_last_print_time >= now) {
36-
return;
35+
if (m_last_print_time < now) {
36+
m_last_print_time = now;
37+
print_status(now);
3738
}
38-
m_last_print_time = now;
39-
40-
print_status(now);
4139
}

src/progress-display.hpp

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
* It contains the progress_display_t class.
1010
*/
1111

12+
#include <cstddef>
1213
#include <ctime>
1314

1415
#include <osmium/handler.hpp>
@@ -32,18 +33,15 @@ class progress_display_t : public osmium::handler::Handler
3233
std::size_t add(osmid_t id) noexcept
3334
{
3435
max = id;
35-
if (count == 0) {
36-
start = std::time(nullptr);
37-
}
3836
return ++count;
3937
}
4038
};
4139

4240
public:
43-
progress_display_t(bool enabled = false) noexcept : m_enabled(enabled) {}
44-
45-
void print_summary() const;
46-
void print_status(std::time_t now) const;
41+
progress_display_t(bool enabled = false) noexcept : m_enabled(enabled)
42+
{
43+
m_node.start = std::time(nullptr);
44+
}
4745

4846
void node(osmium::Node const &node)
4947
{
@@ -52,22 +50,31 @@ class progress_display_t : public osmium::handler::Handler
5250
}
5351
}
5452

53+
void after_nodes() { m_way.start = std::time(nullptr); }
54+
5555
void way(osmium::Way const &way)
5656
{
5757
if (m_way.add(way.id()) % 1000 == 0) {
5858
possibly_print_status();
5959
}
6060
}
6161

62+
void after_ways() { m_rel.start = std::time(nullptr); }
63+
6264
void relation(osmium::Relation const &relation)
6365
{
6466
if (m_rel.add(relation.id()) % 10 == 0) {
6567
possibly_print_status();
6668
}
6769
}
6870

71+
void after_relations() const { print_status(std::time(nullptr)); }
72+
73+
void print_summary() const;
74+
6975
private:
7076
void possibly_print_status();
77+
void print_status(std::time_t now) const;
7178

7279
static double count_per_second(osmid_t count, uint64_t elapsed) noexcept
7380
{

0 commit comments

Comments
 (0)