Skip to content

Commit 7f1e1bc

Browse files
committed
Remove multi-backend support
Closes zerebubuth#368
1 parent 649370d commit 7f1e1bc

File tree

6 files changed

+20
-134
lines changed

6 files changed

+20
-134
lines changed

CMakeLists.txt

+2-6
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ endif()
3535
###############
3636
# build options
3737
###############
38-
option(ENABLE_APIDB "Enable APIDB backend, as used by the OSM servers" ON)
3938
option(ENABLE_YAJL "Enable JSON output with the YAJL library" ON)
4039
option(ENABLE_BROTLI "Enable Brotli library" ON)
4140
option(ENABLE_FMT_HEADER "Enable FMT header only mode" ON)
@@ -61,7 +60,6 @@ include(BuildLint)
6160
add_library(cgimap_common_compiler_options INTERFACE)
6261

6362
target_compile_features(cgimap_common_compiler_options INTERFACE cxx_std_17)
64-
target_compile_definitions(cgimap_common_compiler_options INTERFACE "ENABLE_APIDB=$<BOOL:${ENABLE_APIDB}>")
6563
target_compile_definitions(cgimap_common_compiler_options INTERFACE CMAKE=1)
6664
target_add_autotools_compatibility_definitions(cgimap_common_compiler_options)
6765

@@ -141,7 +139,7 @@ target_link_libraries(openstreetmap_cgimap
141139
cgimap_common_compiler_options
142140
cgimap_core
143141
cgimap_fcgi
144-
$<$<BOOL:${ENABLE_APIDB}>:cgimap_apidb>
142+
cgimap_apidb
145143
Boost::program_options
146144
PQXX::PQXX)
147145

@@ -181,9 +179,7 @@ if (ENABLE_INSTALL)
181179
if (BUILD_SHARED_LIBS)
182180
install(TARGETS cgimap_core DESTINATION lib)
183181
install(TARGETS cgimap_fcgi DESTINATION lib)
184-
if (ENABLE_APIDB)
185-
install(TARGETS cgimap_apidb DESTINATION lib)
186-
endif()
182+
install(TARGETS cgimap_apidb DESTINATION lib)
187183
endif()
188184
endif()
189185

openstreetmap-cgimap.1

-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ openstreetmap-cgimap \- FCGI version of the OpenStreetMap API
1717
[\fB\-\-moderator-maxdebt \fIMODERATOR_DEBT\fR]
1818
[\fB\-\-port \fIPORT\fR]
1919
[\fB\-\-socket \fISOCKET\fR]
20-
[\fB\-\-backend \fIBACKEND\fR]
2120
] [
2221
[\fB\-\-dbname \fIDBNAME\fR]
2322
[\fB\-\-host \fIHOST\fR]

src/backend.cpp

+13-106
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,7 @@
1010
#include "cgimap/backend.hpp"
1111

1212
#include <fmt/core.h>
13-
#include <stdexcept>
14-
#include <mutex>
13+
#include <memory>
1514

1615
namespace po = boost::program_options;
1716

@@ -43,166 +42,74 @@ po::variables_map first_pass_argments(int argc, char *argv[],
4342
struct registry {
4443
registry() = default;
4544

46-
bool add(std::unique_ptr<backend> ptr);
45+
bool set_backend(std::unique_ptr<backend> ptr);
4746
void setup_options(int argc, char *argv[], po::options_description &desc);
4847
void output_options(std::ostream &out);
4948
std::unique_ptr<data_selection::factory> create(const po::variables_map &options);
5049
std::unique_ptr<data_update::factory> create_data_update(const po::variables_map &options);
5150

5251
private:
53-
using backend_map_t = std::map<std::string, std::unique_ptr<backend> >;
54-
backend_map_t backends;
55-
std::optional<std::string> default_backend;
52+
std::unique_ptr<backend> backend_ptr;
5653
};
5754

58-
bool registry::add(std::unique_ptr<backend> ptr) {
59-
if (default_backend) {
60-
if (ptr->name() <= *default_backend) {
61-
default_backend = ptr->name();
62-
}
63-
} else {
64-
default_backend = ptr->name();
65-
}
66-
67-
std::string name = ptr->name();
68-
backends.emplace(name, std::move(ptr));
69-
55+
bool registry::set_backend(std::unique_ptr<backend> ptr) {
56+
backend_ptr = std::move(ptr);
7057
return true;
7158
}
7259

7360
void registry::setup_options(int argc, char *argv[],
7461
po::options_description &desc) {
75-
if (backends.empty() || !default_backend) {
76-
throw std::runtime_error("No backends available - this is most likely a "
77-
"compile-time configuration error.");
78-
}
79-
80-
std::string all_backends;
81-
82-
for (const backend_map_t::value_type &val : backends) {
83-
if (!all_backends.empty()) {
84-
all_backends += ", ";
85-
}
86-
all_backends += val.second->name();
87-
}
88-
89-
std::string description = fmt::format("backend to use, available options are: {}", all_backends);
90-
91-
desc.add_options()("backend", po::value<std::string>()->default_value(*default_backend),
92-
description.c_str());
9362

9463
po::variables_map vm = first_pass_argments(argc, argv, desc);
9564

96-
std::string bcknd = *default_backend;
97-
98-
// little hack - we want to print *all* the backends when --help is passed, so
99-
// we don't add one here when it's present. it's a nasty way to do it, but i
100-
// can't think of a better one right now...
10165
if (!vm.count("help")) {
102-
103-
if (vm.count("backend")) {
104-
auto itr =
105-
backends.find(vm["backend"].as<std::string>());
106-
if (itr != backends.end()) {
107-
bcknd = itr->first;
108-
}
109-
else {
110-
throw std::runtime_error(fmt::format("unknown backend provided, available options are: {}", all_backends));
111-
}
112-
}
113-
114-
desc.add(backends[bcknd]->options());
66+
desc.add(backend_ptr->options());
11567
}
11668
}
11769

11870
void registry::output_options(std::ostream &out) {
119-
for (const backend_map_t::value_type &val : backends) {
120-
out << val.second->options() << std::endl;
121-
}
71+
out << backend_ptr->options() << std::endl;
12272
}
12373

12474
std::unique_ptr<data_selection::factory>
12575
registry::create(const po::variables_map &options) {
126-
std::string bcknd = *default_backend;
127-
128-
if (options.count("backend")) {
129-
auto itr =
130-
backends.find(options["backend"].as<std::string>());
131-
if (itr != backends.end()) {
132-
bcknd = itr->first;
133-
}
134-
}
135-
136-
return backends[bcknd]->create(options);
76+
return backend_ptr->create(options);
13777
}
13878

13979
std::unique_ptr<data_update::factory>
14080
registry::create_data_update(const po::variables_map &options) {
141-
std::string bcknd = *default_backend;
142-
143-
if (options.count("backend")) {
144-
auto itr =
145-
backends.find(options["backend"].as<std::string>());
146-
if (itr != backends.end()) {
147-
bcknd = itr->first;
148-
}
149-
}
150-
151-
return backends[bcknd]->create_data_update(options);
81+
return backend_ptr->create_data_update(options);
15282
}
15383

154-
registry *registry_ptr = NULL;
155-
std::mutex registry_mut;
84+
std::unique_ptr<registry> registry_ptr = std::make_unique<registry>();
85+
15686

15787
} // anonymous namespace
15888

15989
backend::~backend() = default;
16090

91+
// Registers a single backend, replacing an existing backend
16192
bool register_backend(std::unique_ptr<backend> ptr) {
162-
std::unique_lock<std::mutex> lock(registry_mut);
163-
if (registry_ptr == NULL) {
164-
registry_ptr = new registry;
165-
}
166-
167-
return registry_ptr->add(std::move(ptr));
93+
return registry_ptr->set_backend(std::move(ptr));
16894
}
16995

17096
void setup_backend_options(int argc, char *argv[],
17197
po::options_description &desc) {
172-
std::unique_lock<std::mutex> lock(registry_mut);
173-
if (registry_ptr == NULL) {
174-
registry_ptr = new registry;
175-
}
17698

17799
registry_ptr->setup_options(argc, argv, desc);
178100
}
179101

180102
void output_backend_options(std::ostream &out) {
181-
std::unique_lock<std::mutex> lock(registry_mut);
182-
if (registry_ptr == NULL) {
183-
registry_ptr = new registry;
184-
}
185-
186103
registry_ptr->output_options(out);
187104
}
188105

189106
std::unique_ptr<data_selection::factory>
190107
create_backend(const po::variables_map &options) {
191-
std::unique_lock<std::mutex> lock(registry_mut);
192-
if (registry_ptr == NULL) {
193-
registry_ptr = new registry;
194-
}
195-
196108
return registry_ptr->create(options);
197109
}
198110

199111
std::unique_ptr<data_update::factory>
200112
create_update_backend(const po::variables_map &options) {
201-
std::unique_lock<std::mutex> lock(registry_mut);
202-
if (registry_ptr == NULL) {
203-
registry_ptr = new registry;
204-
}
205-
206113
return registry_ptr->create_data_update(options);
207114
}
208115

src/backend/apidb/CMakeLists.txt

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
##############
22
# cgimap_apidb
33
##############
4-
if(ENABLE_APIDB)
4+
55
add_library(cgimap_apidb)
66

77
target_include_directories(cgimap_apidb PUBLIC
@@ -26,4 +26,4 @@ if(ENABLE_APIDB)
2626
cgimap_common_compiler_options
2727
cgimap_core
2828
PQXX::PQXX)
29-
endif()
29+

src/main.cpp

+3-15
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,7 @@
77
* For a full list of authors see the git log.
88
*/
99

10-
#if ENABLE_APIDB
1110
#include <pqxx/pqxx>
12-
#endif
1311
#include <iostream>
1412
#include <sstream>
1513

@@ -48,10 +46,8 @@ using namespace std::chrono_literals;
4846
#include "cgimap/fcgi_request.hpp"
4947
#include "cgimap/options.hpp"
5048
#include "cgimap/process_request.hpp"
51-
52-
#ifdef ENABLE_APIDB
5349
#include "cgimap/backend/apidb/apidb.hpp"
54-
#endif
50+
5551

5652
namespace po = boost::program_options;
5753

@@ -287,12 +283,6 @@ void daemonise() {
287283
close(2);
288284
}
289285

290-
void setup_backends() {
291-
#if ENABLE_APIDB
292-
register_backend(make_apidb_backend());
293-
#endif
294-
}
295-
296286

297287
void daemon_mode(const po::variables_map &options, int socket)
298288
{
@@ -423,8 +413,8 @@ int main(int argc, char **argv) {
423413
try {
424414
po::variables_map options;
425415

426-
// set up all the backends
427-
setup_backends();
416+
// set up the apidb backend
417+
register_backend(make_apidb_backend());
428418

429419
// get options
430420
get_options(argc, argv, options);
@@ -445,7 +435,6 @@ int main(int argc, char **argv) {
445435
std::cerr << "Error: " << e.what() << "\n(\"openstreetmap-cgimap --help\" for help)" << std::endl;
446436
return 1;
447437

448-
#if ENABLE_APIDB
449438
} catch (const pqxx::sql_error &er) {
450439
logger::message(er.what());
451440
// Catch-all for query related postgres exceptions
@@ -461,7 +450,6 @@ int main(int argc, char **argv) {
461450
std::cerr << "Error: " << e.base().what() << std::endl;
462451
return 1;
463452

464-
#endif
465453
#endif
466454

467455
} catch (const std::exception &e) {

test/CMakeLists.txt

-4
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,6 @@ if(BUILD_TESTING)
159159
COMMAND test_parse_changeset_input)
160160

161161

162-
if(ENABLE_APIDB)
163162
##########################
164163
# test_apidb_backend_nodes
165164
##########################
@@ -283,7 +282,6 @@ if(BUILD_TESTING)
283282

284283
add_test(NAME test_apidb_backend_changeset_uploads
285284
COMMAND pg_virtualenv "$<TARGET_FILE:test_apidb_backend_changeset_uploads>" --db-schema "${CMAKE_CURRENT_SOURCE_DIR}/structure.sql")
286-
endif()
287285

288286
# define check alias target for autotools compatibility
289287
add_custom_target(check COMMAND ${CMAKE_CTEST_COMMAND})
@@ -297,13 +295,11 @@ if(BUILD_TESTING)
297295
test_parse_osmchange_input
298296
test_parse_changeset_input)
299297

300-
if(ENABLE_APIDB)
301298
add_dependencies(check test_apidb_backend_nodes
302299
test_apidb_backend_oauth2
303300
test_apidb_backend_historic
304301
test_apidb_backend_changesets
305302
test_apidb_backend_changeset_downloads
306303
test_apidb_backend_changeset_uploads)
307-
endif()
308304

309305
endif()

0 commit comments

Comments
 (0)