Skip to content

Commit f453be4

Browse files
committed
Refactor of middle: Remove slim middle class and change middle API
This commit changes the way the middle is called. The main interface for feeding data to the middle are now the functions node/way/relation which are called for every object, regardless of whether it is new, changed, or deleted. The middle can then figure out itself how to handle these. This will allow for possible optimizations when the middle can handle an update better than a delete and create. This also makes it natural to remove the slim_middle_t class, its functionality is now rolled into the middle_t class. This makes the generic middle API independent of whether we are using slim mode or not, allowing middles which behave as slim or not depending on their own criteria. More refactoring based on this in later commits.
1 parent e1e538d commit f453be4

File tree

9 files changed

+230
-211
lines changed

9 files changed

+230
-211
lines changed

src/middle-pgsql.cpp

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,46 @@ std::size_t middle_query_pgsql_t::get_way_node_locations_db(
314314
return count;
315315
}
316316

317+
void middle_pgsql_t::node(osmium::Node const &node)
318+
{
319+
if (node.deleted()) {
320+
node_delete(node.id());
321+
} else {
322+
if (m_options->append) {
323+
node_delete(node.id());
324+
node_set(node);
325+
} else {
326+
node_set(node);
327+
}
328+
}
329+
}
330+
331+
void middle_pgsql_t::way(osmium::Way const &way) {
332+
if (way.deleted()) {
333+
way_delete(way.id());
334+
} else {
335+
if (m_options->append) {
336+
way_delete(way.id());
337+
way_set(way);
338+
} else {
339+
way_set(way);
340+
}
341+
}
342+
}
343+
344+
void middle_pgsql_t::relation(osmium::Relation const &relation) {
345+
if (relation.deleted()) {
346+
relation_delete(relation.id());
347+
} else {
348+
if (m_options->append) {
349+
relation_delete(relation.id());
350+
relation_set(relation);
351+
} else {
352+
relation_set(relation);
353+
}
354+
}
355+
}
356+
317357
void middle_pgsql_t::node_set(osmium::Node const &node)
318358
{
319359
m_cache->set(node.id(), node.location());

src/middle-pgsql.hpp

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ struct table_sql {
5454
char const *create_fw_dep_indexes = "";
5555
};
5656

57-
struct middle_pgsql_t : public slim_middle_t
57+
struct middle_pgsql_t : public middle_t
5858
{
5959
explicit middle_pgsql_t(options_t const *options);
6060

@@ -63,14 +63,9 @@ struct middle_pgsql_t : public slim_middle_t
6363
void analyze() override;
6464
void commit() override;
6565

66-
void node_set(osmium::Node const &node) override;
67-
void node_delete(osmid_t id) override;
68-
69-
void way_set(osmium::Way const &way) override;
70-
void way_delete(osmid_t id) override;
71-
72-
void relation_set(osmium::Relation const &rel) override;
73-
void relation_delete(osmid_t id) override;
66+
void node(osmium::Node const &node) override;
67+
void way(osmium::Way const &way) override;
68+
void relation(osmium::Relation const &rel) override;
7469

7570
void flush() override;
7671

@@ -122,6 +117,15 @@ struct middle_pgsql_t : public slim_middle_t
122117
NUM_TABLES
123118
};
124119

120+
void node_set(osmium::Node const &node);
121+
void node_delete(osmid_t id);
122+
123+
void way_set(osmium::Way const &way);
124+
void way_delete(osmid_t id);
125+
126+
void relation_set(osmium::Relation const &rel);
127+
void relation_delete(osmid_t id);
128+
125129
void buffer_store_tags(osmium::OSMObject const &obj, bool attrs);
126130

127131
idlist_t get_ids(const char* stmt, osmid_t osm_id);

src/middle-ram.cpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,18 +30,21 @@
3030
*
3131
*/
3232

33-
void middle_ram_t::node_set(osmium::Node const &node)
33+
void middle_ram_t::node(osmium::Node const &node)
3434
{
35+
assert(node.visible());
3536
m_cache->set(node.id(), node.location());
3637
}
3738

38-
void middle_ram_t::way_set(osmium::Way const &way)
39+
void middle_ram_t::way(osmium::Way const &way)
3940
{
41+
assert(way.visible());
4042
m_ways.set(way.id(), new ramWay{way, m_extra_attributes});
4143
}
4244

43-
void middle_ram_t::relation_set(osmium::Relation const &rel)
45+
void middle_ram_t::relation(osmium::Relation const &rel)
4446
{
47+
assert(rel.visible());
4548
m_rels.set(rel.id(), new ramRel{rel, m_extra_attributes});
4649
}
4750

src/middle-ram.hpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -99,17 +99,18 @@ struct middle_ram_t : public middle_t, public middle_query_t
9999
void analyze() override {}
100100
void commit() override {}
101101

102-
void node_set(osmium::Node const &node) override;
102+
void node(osmium::Node const &node) override;
103+
void way(osmium::Way const &way) override;
104+
void relation(osmium::Relation const &rel) override;
105+
103106
size_t nodes_get_list(osmium::WayNodeList *nodes) const override;
104107

105-
void way_set(osmium::Way const &way) override;
106108
bool way_get(osmid_t id, osmium::memory::Buffer &buffer) const override;
107109
size_t rel_way_members_get(osmium::Relation const &rel, rolelist_t *roles,
108110
osmium::memory::Buffer &buffer) const override;
109111

110112
bool relation_get(osmid_t id,
111113
osmium::memory::Buffer &buffer) const override;
112-
void relation_set(osmium::Relation const &rel) override;
113114

114115
void flush() override {}
115116

src/middle.hpp

Lines changed: 8 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,8 @@
11
#ifndef OSM2PGSQL_MIDDLE_HPP
22
#define OSM2PGSQL_MIDDLE_HPP
33

4-
/**
5-
* Common middle layer interface
6-
* Each middle layer data store must provide methods for
7-
* storing and retrieving node and way data.
8-
*/
9-
104
#include <osmium/memory/buffer.hpp>
115

12-
#include <cstddef>
136
#include <memory>
147

158
#include "osmtypes.hpp"
@@ -70,10 +63,8 @@ struct middle_query_t : std::enable_shared_from_this<middle_query_t>
7063
inline middle_query_t::~middle_query_t() = default;
7164

7265
/**
73-
* Interface for storing raw OSM data in an intermediate object store.
74-
*
75-
* This interface only allows for setting OSM data once, not changing it.
76-
* If you need updates and deletions, look at the derived class slim_middle_t.
66+
* Interface for storing "raw" OSM data in an intermediate object store and
67+
* getting it back.
7768
*/
7869
struct middle_t
7970
{
@@ -84,23 +75,14 @@ struct middle_t
8475
virtual void analyze() = 0;
8576
virtual void commit() = 0;
8677

87-
/**
88-
* Add a node to data storage. The node must not already be in the
89-
* data storage.
90-
*/
91-
virtual void node_set(osmium::Node const &node) = 0;
78+
/// This is called for every added, changed or deleted node.
79+
virtual void node(osmium::Node const &node) = 0;
9280

93-
/**
94-
* Add a way to data storage. The way must not already be in the data
95-
* storage.
96-
*/
97-
virtual void way_set(osmium::Way const &way) = 0;
81+
/// This is called for every added, changed or deleted way.
82+
virtual void way(osmium::Way const &way) = 0;
9883

99-
/**
100-
* Add a relation to data storage. The way must not already be in the
101-
* data storage.
102-
*/
103-
virtual void relation_set(osmium::Relation const &rel) = 0;
84+
/// This is called for every added, changed or deleted relation.
85+
virtual void relation(osmium::Relation const &relation) = 0;
10486

10587
/**
10688
* Ensure all pending data is written to the storage.
@@ -122,32 +104,4 @@ struct middle_t
122104

123105
inline middle_t::~middle_t() = default;
124106

125-
/**
126-
* Extends the middle_t interface to allow updates and deletions of objects.
127-
*/
128-
struct slim_middle_t : public middle_t
129-
{
130-
~slim_middle_t() override = 0;
131-
132-
/**
133-
* Delete a node from data storage. Either because you want it removed
134-
* entirely or before you can node_set() a new version of it.
135-
*/
136-
virtual void node_delete(osmid_t id) = 0;
137-
138-
/**
139-
* Delete a way from data storage. Either because you want it removed
140-
* entirely or before you can way_set() a new version of it.
141-
*/
142-
virtual void way_delete(osmid_t id) = 0;
143-
144-
/**
145-
* Delete a relation from data storage. Either because you want it removed
146-
* entirely or before you can relation_set() a new version of it.
147-
*/
148-
virtual void relation_delete(osmid_t id) = 0;
149-
};
150-
151-
inline slim_middle_t::~slim_middle_t() = default;
152-
153107
#endif // OSM2PGSQL_MIDDLE_HPP

src/osmdata.cpp

Lines changed: 14 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -34,21 +34,12 @@ osmdata_t::osmdata_t(std::unique_ptr<dependency_manager_t> dependency_manager,
3434
assert(!m_outs.empty());
3535
}
3636

37-
/**
38-
* For modify and delete member functions a middle_t is not enough, an object
39-
* of the derived class slim_middle_t is needed. This function does the
40-
* conversion. It should always succeed, because the modify and delete
41-
* functions are never called for non-slim middles.
42-
*/
43-
slim_middle_t &osmdata_t::slim_middle() const noexcept
44-
{
45-
auto *slim = dynamic_cast<slim_middle_t *>(m_mid.get());
46-
assert(slim);
47-
return *slim;
48-
}
49-
5037
void osmdata_t::node(osmium::Node const &node)
5138
{
39+
if (!m_bbox.valid() || node.deleted() || m_bbox.contains(node.location())) {
40+
m_mid->node(node);
41+
}
42+
5243
if (node.deleted()) {
5344
node_delete(node.id());
5445
} else {
@@ -75,6 +66,8 @@ void osmdata_t::after_nodes() { flush(); }
7566

7667
void osmdata_t::way(osmium::Way &way)
7768
{
69+
m_mid->way(way);
70+
7871
if (way.deleted()) {
7972
way_delete(way.id());
8073
} else {
@@ -90,6 +83,14 @@ void osmdata_t::after_ways() { flush(); }
9083

9184
void osmdata_t::relation(osmium::Relation const &rel)
9285
{
86+
if (m_append && !rel.deleted()) {
87+
for (auto const &out : m_outs) {
88+
out->select_relation_members(rel.id());
89+
}
90+
}
91+
92+
m_mid->relation(rel);
93+
9394
if (rel.deleted()) {
9495
relation_delete(rel.id());
9596
} else {
@@ -108,8 +109,6 @@ void osmdata_t::after_relations() { flush(); }
108109

109110
void osmdata_t::node_add(osmium::Node const &node) const
110111
{
111-
m_mid->node_set(node);
112-
113112
if (m_with_extra_attrs || !node.tags().empty()) {
114113
for (auto const &out : m_outs) {
115114
out->node_add(node);
@@ -119,8 +118,6 @@ void osmdata_t::node_add(osmium::Node const &node) const
119118

120119
void osmdata_t::way_add(osmium::Way *way) const
121120
{
122-
m_mid->way_set(*way);
123-
124121
if (m_with_extra_attrs || !way->tags().empty()) {
125122
for (auto const &out : m_outs) {
126123
out->way_add(way);
@@ -130,8 +127,6 @@ void osmdata_t::way_add(osmium::Way *way) const
130127

131128
void osmdata_t::relation_add(osmium::Relation const &rel) const
132129
{
133-
m_mid->relation_set(rel);
134-
135130
if (m_with_extra_attrs || !rel.tags().empty()) {
136131
for (auto const &out : m_outs) {
137132
out->relation_add(rel);
@@ -141,11 +136,6 @@ void osmdata_t::relation_add(osmium::Relation const &rel) const
141136

142137
void osmdata_t::node_modify(osmium::Node const &node) const
143138
{
144-
auto &slim = slim_middle();
145-
146-
slim.node_delete(node.id());
147-
slim.node_set(node);
148-
149139
for (auto const &out : m_outs) {
150140
out->node_modify(node);
151141
}
@@ -155,11 +145,6 @@ void osmdata_t::node_modify(osmium::Node const &node) const
155145

156146
void osmdata_t::way_modify(osmium::Way *way) const
157147
{
158-
auto &slim = slim_middle();
159-
160-
slim.way_delete(way->id());
161-
slim.way_set(*way);
162-
163148
for (auto const &out : m_outs) {
164149
out->way_modify(way);
165150
}
@@ -169,15 +154,6 @@ void osmdata_t::way_modify(osmium::Way *way) const
169154

170155
void osmdata_t::relation_modify(osmium::Relation const &rel) const
171156
{
172-
auto &slim = slim_middle();
173-
174-
for (auto const &out : m_outs) {
175-
out->select_relation_members(rel.id());
176-
}
177-
178-
slim.relation_delete(rel.id());
179-
slim.relation_set(rel);
180-
181157
for (auto const &out : m_outs) {
182158
out->relation_modify(rel);
183159
}
@@ -188,26 +164,20 @@ void osmdata_t::node_delete(osmid_t id) const
188164
for (auto const &out : m_outs) {
189165
out->node_delete(id);
190166
}
191-
192-
slim_middle().node_delete(id);
193167
}
194168

195169
void osmdata_t::way_delete(osmid_t id) const
196170
{
197171
for (auto const &out : m_outs) {
198172
out->way_delete(id);
199173
}
200-
201-
slim_middle().way_delete(id);
202174
}
203175

204176
void osmdata_t::relation_delete(osmid_t id) const
205177
{
206178
for (auto const &out : m_outs) {
207179
out->relation_delete(id);
208180
}
209-
210-
slim_middle().relation_delete(id);
211181
}
212182

213183
void osmdata_t::start() const

src/osmdata.hpp

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
class options_t;
2323
class output_t;
2424
struct middle_t;
25-
struct slim_middle_t;
2625

2726
/**
2827
* This class guides the processing of the OSM data through its multiple
@@ -84,8 +83,6 @@ class osmdata_t : public osmium::handler::Handler
8483
*/
8584
void postprocess_database() const;
8685

87-
slim_middle_t &slim_middle() const noexcept;
88-
8986
std::unique_ptr<dependency_manager_t> m_dependency_manager;
9087
std::shared_ptr<middle_t> m_mid;
9188
std::vector<std::shared_ptr<output_t>> m_outs;

0 commit comments

Comments
 (0)