Skip to content

Commit 3997cf1

Browse files
committed
Disentangle progress display from core of osmdata_t
The progress_display_t class is now a libosmium handler created outside osmdata_t.
1 parent 33f5fe1 commit 3997cf1

File tree

6 files changed

+31
-32
lines changed

6 files changed

+31
-32
lines changed

src/osm2pgsql.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,8 @@ int main(int argc, char *argv[])
119119
// populating some of the tables.
120120
util::timer_t timer_parse;
121121

122-
auto const progress = osmdata.process_files(files);
122+
progress_display_t progress{get_logger().show_progress()};
123+
osmdata.process_files(files, progress);
123124

124125
progress.print_status(std::time(nullptr));
125126
fmt::print(stderr, " parse time: {}\n",

src/osmdata.cpp

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,6 @@ void osmdata_t::node(osmium::Node const &node)
233233
}
234234
}
235235
}
236-
m_progress.add_node(node.id());
237236
}
238237

239238
void osmdata_t::way(osmium::Way &way)
@@ -252,7 +251,6 @@ void osmdata_t::way(osmium::Way &way)
252251
way_add(&way);
253252
}
254253
}
255-
m_progress.add_way(way.id());
256254
}
257255

258256
void osmdata_t::relation(osmium::Relation const &rel)
@@ -274,7 +272,6 @@ void osmdata_t::relation(osmium::Relation const &rel)
274272
relation_add(rel);
275273
}
276274
}
277-
m_progress.add_rel(rel.id());
278275
}
279276

280277
void osmdata_t::node_add(osmium::Node const &node) const
@@ -586,7 +583,8 @@ class multithreaded_processor
586583

587584
} // anonymous namespace
588585

589-
progress_display_t osmdata_t::process_file(osmium::io::File const &file)
586+
void osmdata_t::process_file(osmium::io::File const &file,
587+
progress_display_t &progress)
590588
{
591589
osmium::io::Reader reader{file};
592590
type_id_version last{osmium::item_type::node, 0, 0};
@@ -599,21 +597,20 @@ progress_display_t osmdata_t::process_file(osmium::io::File const &file)
599597
"Input file contains deleted objects but "
600598
"you are not in append mode."};
601599
}
602-
osmium::apply_item(object, *this);
600+
osmium::apply_item(object, *this, progress);
603601
}
604602
}
605603
flush();
606604

607605
reader.close();
608-
609-
return m_progress;
610606
}
611607

612-
progress_display_t
613-
osmdata_t::process_files(std::vector<osmium::io::File> const &files)
608+
void osmdata_t::process_files(std::vector<osmium::io::File> const &files,
609+
progress_display_t &progress)
614610
{
615611
if (files.size() == 1) {
616-
return process_file(files.front());
612+
process_file(files.front(), progress);
613+
return;
617614
}
618615

619616
std::vector<data_source_t> data_sources;
@@ -638,7 +635,7 @@ osmdata_t::process_files(std::vector<osmium::io::File> const &files)
638635
"Input file contains deleted objects but "
639636
"you are not in append mode."};
640637
}
641-
osmium::apply_item(element.object(), *this);
638+
osmium::apply_item(element.object(), *this, progress);
642639
}
643640

644641
auto *source = element.data_source();
@@ -652,8 +649,6 @@ osmdata_t::process_files(std::vector<osmium::io::File> const &files)
652649
for (auto &data_source : data_sources) {
653650
data_source.close();
654651
}
655-
656-
return m_progress;
657652
}
658653

659654
void osmdata_t::process_dependents() const

src/osmdata.hpp

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,8 @@ class osmdata_t : public osmium::handler::Handler
6464
/**
6565
* Process the specified OSM files (stage 1a).
6666
*/
67-
progress_display_t
68-
process_files(std::vector<osmium::io::File> const &files);
67+
void process_files(std::vector<osmium::io::File> const &files,
68+
progress_display_t &progress);
6969

7070
/**
7171
* Rest of the processing (stages 1b, 1c, 2, and database postprocessing).
@@ -90,7 +90,8 @@ class osmdata_t : public osmium::handler::Handler
9090
void flush() const;
9191

9292
/// Process a single OSM file (stage 1a).
93-
progress_display_t process_file(osmium::io::File const &file);
93+
void process_file(osmium::io::File const &file,
94+
progress_display_t &progress);
9495

9596
/**
9697
* Run stage 1b and stage 1c processing: Process dependent objects in
@@ -120,9 +121,6 @@ class osmdata_t : public osmium::handler::Handler
120121
// imported).
121122
osmium::Box m_bbox;
122123

123-
// The progress meter will be updated as we go.
124-
progress_display_t m_progress;
125-
126124
// Current type being parsed.
127125
osmium::item_type m_type = osmium::item_type::node;
128126

src/progress-display.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ void progress_display_t::print_summary() const
1616

1717
void progress_display_t::print_status(std::time_t now) const
1818
{
19-
if (get_logger().show_progress()) {
19+
if (m_enabled) {
2020
fmt::print(stderr,
2121
"\rProcessing: Node({}k {:.1f}k/s) Way({}k {:.2f}k/s)"
2222
" Relation({} {:.1f}/s)",

src/progress-display.hpp

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,15 @@
1111

1212
#include <ctime>
1313

14+
#include <osmium/handler.hpp>
15+
1416
#include "osmtypes.hpp"
1517

1618
/**
1719
* The progress_display_t class is used to display how far the processing of
1820
* the input data has progressed.
1921
*/
20-
class progress_display_t
22+
class progress_display_t : public osmium::handler::Handler
2123
{
2224
struct Counter
2325
{
@@ -38,28 +40,28 @@ class progress_display_t
3840
};
3941

4042
public:
41-
progress_display_t() noexcept : m_last_print_time(std::time(nullptr)) {}
43+
progress_display_t(bool enabled = false) noexcept : m_enabled(enabled) {}
4244

4345
void print_summary() const;
4446
void print_status(std::time_t now) const;
4547

46-
void add_node(osmid_t id)
48+
void node(osmium::Node const &node)
4749
{
48-
if (m_node.add(id) % 10000 == 0) {
50+
if (m_node.add(node.id()) % 10000 == 0) {
4951
possibly_print_status();
5052
}
5153
}
5254

53-
void add_way(osmid_t id)
55+
void way(osmium::Way const &way)
5456
{
55-
if (m_way.add(id) % 1000 == 0) {
57+
if (m_way.add(way.id()) % 1000 == 0) {
5658
possibly_print_status();
5759
}
5860
}
5961

60-
void add_rel(osmid_t id)
62+
void relation(osmium::Relation const &relation)
6163
{
62-
if (m_rel.add(id) % 10 == 0) {
64+
if (m_rel.add(relation.id()) % 10 == 0) {
6365
possibly_print_status();
6466
}
6567
}
@@ -107,7 +109,8 @@ class progress_display_t
107109
Counter m_node{};
108110
Counter m_way{};
109111
Counter m_rel{};
110-
std::time_t m_last_print_time;
112+
std::time_t m_last_print_time{std::time(nullptr)};
113+
bool m_enabled;
111114
};
112115

113116
#endif // OSM2PGSQL_PROGRESS_DISPLAY_HPP

tests/common-import.hpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,8 @@ inline void parse_file(options_t const &options,
4444
filepath += options.input_files[0];
4545
}
4646
osmium::io::File file{filepath};
47-
osmdata.process_files({file});
47+
progress_display_t progress;
48+
osmdata.process_files({file}, progress);
4849

4950
if (do_stop) {
5051
osmdata.stop();
@@ -155,7 +156,8 @@ class import_t
155156
for (auto const &data : input_data) {
156157
files.emplace_back(data.data(), data.size(), format);
157158
}
158-
osmdata.process_files(files);
159+
progress_display_t progress;
160+
osmdata.process_files(files, progress);
159161

160162
osmdata.stop();
161163
}

0 commit comments

Comments
 (0)