From a7e766e1aa77fff846d8426658befd9a01fe2861 Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Tue, 11 Aug 2015 15:34:12 +0200 Subject: Share DB object between modules --- brep/buildfile | 3 ++- brep/search.cxx | 9 ++------- brep/shared-database | 24 ++++++++++++++++++++++++ brep/shared-database.cxx | 41 +++++++++++++++++++++++++++++++++++++++++ brep/view.cxx | 9 ++------- 5 files changed, 71 insertions(+), 15 deletions(-) create mode 100644 brep/shared-database create mode 100644 brep/shared-database.cxx (limited to 'brep') diff --git a/brep/buildfile b/brep/buildfile index 13e1d20..5340d22 100644 --- a/brep/buildfile +++ b/brep/buildfile @@ -14,7 +14,8 @@ brep = cxx{package package-odb} libso{brep}: $brep $libs libso{brep}: cxx.export.poptions = -I$out_root -I$src_root -brep = cxx{diagnostics module services search view} cli.cxx{options} +brep = cxx{diagnostics module services search view shared-database} \ + cli.cxx{options} web = ../web/apache/cxx{request service} libso{brep-apache}: $brep $web libso{brep} $libs diff --git a/brep/search.cxx b/brep/search.cxx index fc86284..3aa6503 100644 --- a/brep/search.cxx +++ b/brep/search.cxx @@ -12,14 +12,13 @@ #include #include -#include - #include #include #include #include +#include using namespace std; using namespace odb::core; @@ -35,11 +34,7 @@ namespace brep cli::unknown_mode::fail, cli::unknown_mode::fail); - db_ = make_shared ("", - "", - "brep", - options_->db_host (), - options_->db_port ()); + db_ = shared_database (options_->db_host (), options_->db_port ()); if (options_->results_on_page () > 30) fail << "too many search results on page: " diff --git a/brep/shared-database b/brep/shared-database new file mode 100644 index 0000000..cad526f --- /dev/null +++ b/brep/shared-database @@ -0,0 +1,24 @@ +// file : brep/shared-database -*- C++ -*- +// copyright : Copyright (c) 2014-2015 Code Synthesis Ltd +// license : MIT; see accompanying LICENSE file + +#ifndef BREP_SHARED_DATABASE +#define BREP_SHARED_DATABASE + +#include +#include // shared_ptr + +#include // database + +namespace brep +{ + // Returns pointer to the shared database instance, creating one on the + // first call. On subsequent calls ensures passed host and port equals + // to ones of the shared database instance throwing runtime_error otherwise. + // Is not thread-safe. + // + std::shared_ptr + shared_database (const std::string& host, unsigned int port); +} + +#endif // BREP_SHARED_DATABASE diff --git a/brep/shared-database.cxx b/brep/shared-database.cxx new file mode 100644 index 0000000..fb952d8 --- /dev/null +++ b/brep/shared-database.cxx @@ -0,0 +1,41 @@ +// file : brep/shared-database.cxx -*- C++ -*- +// copyright : Copyright (c) 2014-2015 Code Synthesis Ltd +// license : MIT; see accompanying LICENSE file + +#include + +#include // weak_ptr, shared_ptr, make_shared() +#include // runtime_error + +#include + +using namespace std; +using namespace odb; + +namespace brep +{ + shared_ptr + shared_database (const string& host, unsigned int port) + { + static weak_ptr db; + + // In C++11, function-static variable initialization is + // guaranteed to be thread-safe, thought this doesn't + // seem to be enough in our case (because we are re- + // initializing the weak pointer). + // + if (shared_ptr d = db.lock ()) + { + if (h != d->host () || p != d->port ()) + throw runtime_error ("shared database host/port mismatch"); + + return d; + } + else + { + d = make_shared ("", "", "brep", h, p); + db = d; + return d; + } + } +} diff --git a/brep/view.cxx b/brep/view.cxx index 08e2592..2f3bd17 100644 --- a/brep/view.cxx +++ b/brep/view.cxx @@ -11,12 +11,11 @@ #include #include -#include - #include #include #include +#include using namespace std; using namespace odb::core; @@ -31,11 +30,7 @@ namespace brep cli::unknown_mode::fail, cli::unknown_mode::fail); - db_ = make_shared ("", - "", - "brep", - options_->db_host (), - options_->db_port ()); + db_ = shared_database (options_->db_host (), options_->db_port ()); } void view:: -- cgit v1.1