Skip to content

Commit 5d1a941

Browse files
committed
Refactor middle: Replace analyze and flush methods
Instead of the analyze() and flush() functions on the middle, the more generic functions after_nodes(), after_ways(), and after_relations() are introduced. Each middle can do in them whatever it needs at that place of reading the inputs. In the case of the middle-pgsql we'll now do the analyze of each of the nodes, ways, and relations table right after they have been filled. This also fixes a bug: The analyze() functions in the middle were never called.
1 parent d8b0623 commit 5d1a941

File tree

10 files changed

+78
-64
lines changed

10 files changed

+78
-64
lines changed

src/middle-pgsql.cpp

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#include "node-ram-cache.hpp"
2727
#include "options.hpp"
2828
#include "osmtypes.hpp"
29+
#include "pgsql-helper.hpp"
2930
#include "util.hpp"
3031

3132
/**
@@ -607,13 +608,29 @@ void middle_pgsql_t::relation_delete(osmid_t osm_id)
607608
m_db_copy.delete_object(osm_id);
608609
}
609610

610-
void middle_pgsql_t::analyze()
611+
void middle_pgsql_t::after_nodes()
611612
{
612-
for (auto const &table : m_tables) {
613-
m_db_connection.exec("ANALYZE {}"_format(table.name()));
613+
m_db_copy.sync();
614+
if (m_options->flat_node_file.empty()) {
615+
auto const &table = m_tables[NODE_TABLE];
616+
analyze_table(m_db_connection, table.schema(), table.name());
614617
}
615618
}
616619

620+
void middle_pgsql_t::after_ways()
621+
{
622+
m_db_copy.sync();
623+
auto const &table = m_tables[WAY_TABLE];
624+
analyze_table(m_db_connection, table.schema(), table.name());
625+
}
626+
627+
void middle_pgsql_t::after_relations()
628+
{
629+
m_db_copy.sync();
630+
auto const &table = m_tables[REL_TABLE];
631+
analyze_table(m_db_connection, table.schema(), table.name());
632+
}
633+
617634
middle_query_pgsql_t::middle_query_pgsql_t(
618635
std::string const &conninfo, std::shared_ptr<node_ram_cache> const &cache,
619636
std::shared_ptr<node_persistent_cache> const &persistent_cache)
@@ -658,8 +675,6 @@ void middle_pgsql_t::commit()
658675
m_copy_thread->finish();
659676
}
660677

661-
void middle_pgsql_t::flush() { m_db_copy.sync(); }
662-
663678
void middle_pgsql_t::stop(thread_pool_t &pool)
664679
{
665680
m_cache.reset();

src/middle-pgsql.hpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,14 +60,15 @@ struct middle_pgsql_t : public middle_t
6060

6161
void start() override;
6262
void stop(thread_pool_t &pool) override;
63-
void analyze() override;
6463
void commit() override;
6564

6665
void node(osmium::Node const &node) override;
6766
void way(osmium::Way const &way) override;
6867
void relation(osmium::Relation const &rel) override;
6968

70-
void flush() override;
69+
void after_nodes() override;
70+
void after_ways() override;
71+
void after_relations() override;
7172

7273
idlist_t get_ways_by_node(osmid_t osm_id) override;
7374
idlist_t get_rels_by_node(osmid_t osm_id) override;

src/middle-ram.hpp

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,6 @@ struct middle_ram_t : public middle_t, public middle_query_t
9696

9797
void start() override {}
9898
void stop(thread_pool_t &pool) override;
99-
void analyze() override {}
10099
void commit() override {}
101100

102101
void node(osmium::Node const &node) override;
@@ -112,8 +111,6 @@ struct middle_ram_t : public middle_t, public middle_query_t
112111
bool relation_get(osmid_t id,
113112
osmium::memory::Buffer &buffer) const override;
114113

115-
void flush() override {}
116-
117114
std::shared_ptr<middle_query_t> get_query_instance() override;
118115

119116
private:

src/middle.hpp

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,6 @@ struct middle_t
7474

7575
virtual void start() = 0;
7676
virtual void stop(thread_pool_t &pool) = 0;
77-
virtual void analyze() = 0;
7877
virtual void commit() = 0;
7978

8079
/// This is called for every added, changed or deleted node.
@@ -86,16 +85,14 @@ struct middle_t
8685
/// This is called for every added, changed or deleted relation.
8786
virtual void relation(osmium::Relation const &relation) = 0;
8887

89-
/**
90-
* Ensure all pending data is written to the storage.
91-
*
92-
* You can only query objects from the storage after they have been
93-
* flushed.
94-
*
95-
* The function is called after setting all the nodes, then after setting
96-
* all the ways, and again after setting all the relations.
97-
*/
98-
virtual void flush() = 0;
88+
/// Called after all nodes from the input file(s) have been processed.
89+
virtual void after_nodes() {}
90+
91+
/// Called after all ways from the input file(s) have been processed.
92+
virtual void after_ways() {}
93+
94+
/// Called after all relations from the input file(s) have been processed.
95+
virtual void after_relations() {}
9996

10097
virtual idlist_t get_ways_by_node(osmid_t) { return {}; }
10198
virtual idlist_t get_rels_by_node(osmid_t) { return {}; }

src/osmdata.cpp

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ void osmdata_t::node(osmium::Node const &node)
6262
}
6363
}
6464

65-
void osmdata_t::after_nodes() { flush(); }
65+
void osmdata_t::after_nodes() { m_mid->after_nodes(); }
6666

6767
void osmdata_t::way(osmium::Way &way)
6868
{
@@ -79,7 +79,7 @@ void osmdata_t::way(osmium::Way &way)
7979
}
8080
}
8181

82-
void osmdata_t::after_ways() { flush(); }
82+
void osmdata_t::after_ways() { m_mid->after_ways(); }
8383

8484
void osmdata_t::relation(osmium::Relation const &rel)
8585
{
@@ -105,7 +105,7 @@ void osmdata_t::relation(osmium::Relation const &rel)
105105
}
106106
}
107107

108-
void osmdata_t::after_relations() { flush(); }
108+
void osmdata_t::after_relations() { m_mid->after_relations(); }
109109

110110
void osmdata_t::node_add(osmium::Node const &node) const
111111
{
@@ -187,11 +187,6 @@ void osmdata_t::start() const
187187
}
188188
}
189189

190-
void osmdata_t::flush() const
191-
{
192-
m_mid->flush();
193-
}
194-
195190
namespace {
196191

197192
/**

src/osmdata.hpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,6 @@ class osmdata_t : public osmium::handler::Handler
6565
void way_delete(osmid_t id) const;
6666
void relation_delete(osmid_t id) const;
6767

68-
void flush() const;
69-
7068
/**
7169
* Run stage 1b and stage 1c processing: Process dependent objects in
7270
* append mode.

src/pgsql-helper.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,10 @@ void create_geom_check_trigger(pg_conn_t *db_connection,
2828
" {}();"_format(table + "_osm2pgsql_valid",
2929
qualified_name(schema, table), func_name));
3030
}
31+
32+
void analyze_table(pg_conn_t const &db_connection, std::string const &schema,
33+
std::string const &name)
34+
{
35+
auto const qual_name = qualified_name(schema, name);
36+
db_connection.exec("ANALYZE {}"_format(qual_name));
37+
}

src/pgsql-helper.hpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
11
#ifndef OSM2PGSQL_PGSQL_HELPER_HPP
22
#define OSM2PGSQL_PGSQL_HELPER_HPP
33

4+
#include <string>
5+
46
#include "pgsql.hpp"
57

68
void create_geom_check_trigger(pg_conn_t *db_connection,
79
std::string const &schema,
810
std::string const &table,
911
std::string const &geom_column);
1012

13+
void analyze_table(pg_conn_t const &db_connection, std::string const &schema,
14+
std::string const &name);
15+
1116
#endif // OSM2PGSQL_PGSQL_HELPER_HPP

0 commit comments

Comments
 (0)