aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBoris Kolpackov <boris@codesynthesis.com>2015-08-11 15:34:12 +0200
committerBoris Kolpackov <boris@codesynthesis.com>2015-08-23 13:50:07 +0200
commita7e766e1aa77fff846d8426658befd9a01fe2861 (patch)
tree734e658aca46fa945994db85ea586d79c8933bcd
parent63aaad529c9ae9358f22fd9563aeabc679f4defd (diff)
Share DB object between modules
-rw-r--r--brep/buildfile3
-rw-r--r--brep/search.cxx9
-rw-r--r--brep/shared-database24
-rw-r--r--brep/shared-database.cxx41
-rw-r--r--brep/view.cxx9
-rwxr-xr-xbuild.sh2
6 files changed, 72 insertions, 16 deletions
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 <odb/database.hxx>
#include <odb/transaction.hxx>
-#include <odb/pgsql/database.hxx>
-
#include <butl/path>
#include <web/module>
#include <brep/package>
#include <brep/package-odb>
+#include <brep/shared-database>
using namespace std;
using namespace odb::core;
@@ -35,11 +34,7 @@ namespace brep
cli::unknown_mode::fail,
cli::unknown_mode::fail);
- db_ = make_shared<odb::pgsql::database> ("",
- "",
- "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 <string>
+#include <memory> // shared_ptr
+
+#include <odb/forward.hxx> // 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<odb::core::database>
+ 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 <brep/shared-database>
+
+#include <memory> // weak_ptr, shared_ptr, make_shared()
+#include <stdexcept> // runtime_error
+
+#include <odb/pgsql/database.hxx>
+
+using namespace std;
+using namespace odb;
+
+namespace brep
+{
+ shared_ptr<database>
+ shared_database (const string& host, unsigned int port)
+ {
+ static weak_ptr<pgsql::database> 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<database> d = db.lock ())
+ {
+ if (h != d->host () || p != d->port ())
+ throw runtime_error ("shared database host/port mismatch");
+
+ return d;
+ }
+ else
+ {
+ d = make_shared<database> ("", "", "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 <odb/database.hxx>
#include <odb/transaction.hxx>
-#include <odb/pgsql/database.hxx>
-
#include <web/module>
#include <brep/package>
#include <brep/package-odb>
+#include <brep/shared-database>
using namespace std;
using namespace odb::core;
@@ -31,11 +30,7 @@ namespace brep
cli::unknown_mode::fail,
cli::unknown_mode::fail);
- db_ = make_shared<odb::pgsql::database> ("",
- "",
- "brep",
- options_->db_host (),
- options_->db_port ());
+ db_ = shared_database (options_->db_host (), options_->db_port ());
}
void view::
diff --git a/build.sh b/build.sh
index 0ce4ae3..3d06a80 100755
--- a/build.sh
+++ b/build.sh
@@ -30,7 +30,7 @@ cli --include-with-brackets --include-prefix brep --hxx-suffix "" \
echo "g++ libbrep-apache.so"
s="search.cxx view.cxx module.cxx diagnostics.cxx services.cxx options.cxx \
-../web/apache/request.cxx ../web/apache/service.cxx"
+shared-database.cxx ../web/apache/request.cxx ../web/apache/service.cxx"
g++ -shared $DEBUG -std=c++11 -I. -I/usr/include/apr-1 -I/usr/include/httpd \
-I.. -I../../libbpkg -I../../libbutl -L. -L../../libbpkg/bpkg \