From b72424fca7a6af6ff7921101c450850fef875152 Mon Sep 17 00:00:00 2001 From: Karen Arutyunov Date: Sun, 6 Mar 2016 13:52:48 +0300 Subject: Support multiple instances of brep in a single Apache instance --- brep/database.cxx | 58 ++++++++++++++++++++++++++++++++----------------------- 1 file changed, 34 insertions(+), 24 deletions(-) (limited to 'brep/database.cxx') diff --git a/brep/database.cxx b/brep/database.cxx index 9177b55..17605c5 100644 --- a/brep/database.cxx +++ b/brep/database.cxx @@ -4,40 +4,50 @@ #include +#include + #include namespace brep { + namespace options + { + bool + operator< (const db& x, const db& y) + { + int r; + if ((r = x.db_user ().compare (y.db_user ())) != 0 || + (r = x.db_password ().compare (y.db_password ())) != 0 || + (r = x.db_name ().compare (y.db_name ())) != 0 || + (r = x.db_host ().compare (y.db_host ()))) + return r < 0; + + return x.db_port () < y.db_port (); + } + } + shared_ptr shared_database (const options::db& o) { using odb::pgsql::database; - 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 (o.db_user () != d->user () || - o.db_password () != d->password () || - o.db_name () != d->db () || - o.db_host () != d->host () || - o.db_port () != d->port ()) - throw runtime_error ("shared database options mismatch"); - - return d; - } - else + static std::map> databases; + + auto i (databases.find (o)); + if (i != databases.end ()) { - d = make_shared (o.db_user (), - o.db_password (), - o.db_name (), - o.db_host (), - o.db_port ()); - db = d; - return d; + if (shared_ptr d = i->second.lock ()) + return d; } + + shared_ptr d ( + make_shared (o.db_user (), + o.db_password (), + o.db_name (), + o.db_host (), + o.db_port ())); + + databases[o] = d; + return d; } } -- cgit v1.1