Skip to content

Commit b7ec16d

Browse files
committed
Split middle interface between insert query and insert
Middle has three interface classes: middle_query_t, middle_t and slim_middle_t. They inherit from each other. This is problematic when one wants to supply a middle implementation that follows a similar inheritence structure. There is actualy no need that the interfaces for inserting (middle_t and slim_middle_t) inherit from middle_query_t. The code is very clear about which interface is needed by which class. This change splits off the middle_query_t interface and cleans up the code where the clear distinction was still missing. Needs some larger rework of the middle test which did rely on being able to access both interface types. There is now a template class for testing middle that also contains some more common boilerplate code.
1 parent 56c8f56 commit b7ec16d

26 files changed

+466
-507
lines changed

CMakeLists.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,6 @@ set(osm2pgsql_lib_SOURCES
173173
id-tracker.cpp
174174
middle-pgsql.cpp
175175
middle-ram.cpp
176-
middle.cpp
177176
node-persistent-cache.cpp
178177
node-ram-cache.cpp
179178
options.cpp

geometry-processor.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,8 @@ relation_helper::relation_helper()
8181
: data(1024, osmium::memory::Buffer::auto_grow::yes)
8282
{}
8383

84-
size_t relation_helper::set(osmium::Relation const &rel, middle_t const *mid)
84+
size_t relation_helper::set(osmium::Relation const &rel,
85+
middle_query_t const *mid)
8586
{
8687
// cleanup
8788
data.clear();
@@ -93,7 +94,7 @@ size_t relation_helper::set(osmium::Relation const &rel, middle_t const *mid)
9394
return num_ways;
9495
}
9596

96-
void relation_helper::add_way_locations(middle_t const *mid)
97+
void relation_helper::add_way_locations(middle_query_t const *mid)
9798
{
9899
for (auto &w : data.select<osmium::Way>()) {
99100
mid->nodes_get_list(&(w.nodes()));

geometry-processor.hpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
#include "tagtransform.hpp"
1414

1515
struct middle_query_t;
16-
struct middle_t;
1716
struct options_t;
1817
class reprojection;
1918

@@ -86,8 +85,8 @@ class relation_helper
8685
public:
8786
relation_helper();
8887

89-
size_t set(osmium::Relation const &rel, middle_t const *mid);
90-
void add_way_locations(middle_t const *mid);
88+
size_t set(osmium::Relation const &rel, middle_query_t const *mid);
89+
void add_way_locations(middle_query_t const *mid);
9190

9291
rolelist_t roles;
9392
osmium::memory::Buffer data;

middle-pgsql.cpp

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -949,10 +949,9 @@ void middle_pgsql_t::analyze()
949949
}
950950
}
951951

952-
void middle_pgsql_t::start(const options_t *out_options_)
952+
void middle_pgsql_t::start(const options_t *options)
953953
{
954-
out_options = out_options_;
955-
bool dropcreate = !out_options->append; ///< If tables need to be dropped and created anew
954+
out_options = options;
956955

957956
ways_pending_tracker.reset(new id_tracker());
958957
rels_pending_tracker.reset(new id_tracker());
@@ -983,13 +982,13 @@ void middle_pgsql_t::start(const options_t *out_options_)
983982
for (auto &table : tables) {
984983
table.connect(out_options);
985984

986-
if (dropcreate) {
985+
if (!append) {
987986
pgsql_exec(table.sql_conn, PGRES_COMMAND_OK,
988987
"DROP TABLE IF EXISTS %s CASCADE", table.name());
989988
}
990989

991990
table.begin();
992-
if (dropcreate) {
991+
if (!append) {
993992
table.create();
994993
}
995994
table.prepare_queries(append);

middle-pgsql.hpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@
1717
#include "node-ram-cache.hpp"
1818
#include "pgsql.hpp"
1919

20-
struct middle_pgsql_t : public slim_middle_t {
20+
struct middle_pgsql_t : public slim_middle_t, public middle_query_t
21+
{
2122
middle_pgsql_t();
2223

2324
void start(const options_t *out_options_) override;
@@ -115,6 +116,7 @@ struct middle_pgsql_t : public slim_middle_t {
115116

116117
bool append;
117118
bool mark_pending;
119+
options_t const *out_options;
118120

119121
std::shared_ptr<node_ram_cache> cache;
120122
std::shared_ptr<node_persistent_cache> persistent_cache;

middle-ram.cpp

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,12 @@ void middle_ram_t::nodes_set(osmium::Node const &node)
4040

4141
void middle_ram_t::ways_set(osmium::Way const &way)
4242
{
43-
ways.set(way.id(), new ramWay(way, out_options->extra_attributes));
43+
ways.set(way.id(), new ramWay(way, extra_attributes));
4444
}
4545

4646
void middle_ram_t::relations_set(osmium::Relation const &rel)
4747
{
48-
rels.set(rel.id(), new ramRel(rel, out_options->extra_attributes));
48+
rels.set(rel.id(), new ramRel(rel, extra_attributes));
4949
}
5050

5151
size_t middle_ram_t::nodes_get_list(osmium::WayNodeList *nodes) const
@@ -155,11 +155,10 @@ void middle_ram_t::analyze(void)
155155
/* No need */
156156
}
157157

158-
void middle_ram_t::start(const options_t *out_options_)
158+
void middle_ram_t::start(const options_t *options)
159159
{
160-
out_options = out_options_;
161-
cache.reset(
162-
new node_ram_cache(out_options->alloc_chunkwise, out_options->cache));
160+
extra_attributes = options->extra_attributes;
161+
cache.reset(new node_ram_cache(options->alloc_chunkwise, options->cache));
163162
}
164163

165164
void middle_ram_t::stop(osmium::thread::Pool &pool)
@@ -196,6 +195,8 @@ idlist_t middle_ram_t::relations_using_way(osmid_t) const
196195
std::shared_ptr<middle_query_t>
197196
middle_ram_t::get_query_instance(std::shared_ptr<middle_t> const &mid) const
198197
{
198+
auto me = std::dynamic_pointer_cast<middle_ram_t>(mid);
199+
assert(me);
199200
// No copy here because readonly access is thread safe.
200-
return std::static_pointer_cast<middle_query_t>(mid);
201+
return std::static_pointer_cast<middle_query_t>(me);
201202
}

middle-ram.hpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@
99
#ifndef MIDDLE_RAM_H
1010
#define MIDDLE_RAM_H
1111

12+
#include <array>
1213
#include <memory>
14+
#include <vector>
1315

1416
#include "middle.hpp"
15-
#include <vector>
16-
#include <array>
1717

1818
struct node_ram_cache;
1919
struct options_t;
@@ -80,11 +80,12 @@ class elem_cache_t
8080
}
8181
};
8282

83-
struct middle_ram_t : public middle_t {
83+
struct middle_ram_t : public middle_t, public middle_query_t
84+
{
8485
middle_ram_t();
8586
virtual ~middle_ram_t();
8687

87-
void start(const options_t *out_options_) override;
88+
void start(const options_t *options) override;
8889
void stop(osmium::thread::Pool &pool) override;
8990
void analyze(void) override;
9091
void commit(void) override;
@@ -152,6 +153,7 @@ struct middle_ram_t : public middle_t {
152153
elem_cache_t<ramRel, 10> rels;
153154

154155
std::unique_ptr<node_ram_cache> cache;
156+
bool extra_attributes;
155157

156158
/* the previous behaviour of iterate_ways was to delete all ways as they
157159
* were being iterated. this doesn't work now that the output handles its

middle.cpp

Lines changed: 0 additions & 14 deletions
This file was deleted.

middle.hpp

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,10 @@
2020
struct options_t;
2121

2222
/**
23-
* object which stores OSM node/ways/relations from the input file
23+
* Infterface for returning information about raw OSM input data from a cache.
2424
*/
2525
struct middle_query_t {
26-
virtual ~middle_query_t() {}
26+
virtual ~middle_query_t() = 0;
2727

2828
/**
2929
* Retrives node locations for the given node list.
@@ -75,15 +75,16 @@ struct middle_query_t {
7575
virtual idlist_t relations_using_way(osmid_t way_id) const = 0;
7676
};
7777

78+
inline middle_query_t::~middle_query_t() = default;
79+
7880
/**
79-
* A specialized middle backend which is persistent, and supports updates
81+
* Interface for storing raw OSM data in an intermediate cache.
8082
*/
81-
struct middle_t : public middle_query_t {
82-
static std::shared_ptr<middle_t> create_middle(bool slim);
83-
84-
virtual ~middle_t() {}
83+
struct middle_t
84+
{
85+
virtual ~middle_t() = 0;
8586

86-
virtual void start(const options_t *out_options_) = 0;
87+
virtual void start(options_t const *out_options) = 0;
8788
virtual void stop(osmium::thread::Pool &pool) = 0;
8889
virtual void analyze(void) = 0;
8990
virtual void commit(void) = 0;
@@ -110,12 +111,16 @@ struct middle_t : public middle_query_t {
110111

111112
virtual std::shared_ptr<middle_query_t>
112113
get_query_instance(std::shared_ptr<middle_t> const &mid) const = 0;
113-
114-
const options_t* out_options;
115114
};
116115

116+
inline middle_t::~middle_t() = default;
117+
118+
/**
119+
* Extended interface for permanent caching of raw OSM data.
120+
* It also allows updates.
121+
*/
117122
struct slim_middle_t : public middle_t {
118-
virtual ~slim_middle_t() {}
123+
virtual ~slim_middle_t() = 0;
119124

120125
virtual void nodes_delete(osmid_t id) = 0;
121126
virtual void node_changed(osmid_t id) = 0;
@@ -127,4 +132,6 @@ struct slim_middle_t : public middle_t {
127132
virtual void relation_changed(osmid_t id) = 0;
128133
};
129134

135+
inline slim_middle_t::~slim_middle_t() = default;
136+
130137
#endif

options.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,13 @@
77
#else
88
#define basename /*SKIP IT*/
99
#endif
10-
#include <stdio.h>
11-
#include <string.h>
12-
#include <stdexcept>
13-
#include <sstream>
14-
#include <thread> // for number of threads
1510
#include <boost/format.hpp>
11+
#include <cstdio>
12+
#include <cstring>
1613
#include <osmium/version.hpp>
14+
#include <sstream>
15+
#include <stdexcept>
16+
#include <thread> // for number of threads
1717

1818
#ifdef HAVE_LUA
1919
extern "C" {

0 commit comments

Comments
 (0)