2006-03-31 12:32:02 +02:00
|
|
|
/*****************************************************************************
|
2011-11-14 04:37:50 +01:00
|
|
|
*
|
2006-03-31 12:32:02 +02:00
|
|
|
* This file is part of Mapnik (c++ mapping toolkit)
|
2005-06-14 17:06:59 +02:00
|
|
|
*
|
2011-10-23 21:23:04 +02:00
|
|
|
* Copyright (C) 2011 Artem Pavlenko
|
2005-06-14 17:06:59 +02:00
|
|
|
*
|
2006-03-31 12:32:02 +02:00
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
2005-06-14 17:06:59 +02:00
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
2006-03-31 12:32:02 +02:00
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
2005-06-14 17:06:59 +02:00
|
|
|
*
|
2006-03-31 12:32:02 +02:00
|
|
|
*****************************************************************************/
|
2005-06-14 17:06:59 +02:00
|
|
|
|
2012-04-08 02:20:56 +02:00
|
|
|
#ifndef POSTGIS_CONNECTION_MANAGER_HPP
|
|
|
|
#define POSTGIS_CONNECTION_MANAGER_HPP
|
2005-06-14 17:06:59 +02:00
|
|
|
|
2012-04-08 02:20:56 +02:00
|
|
|
#include "connection.hpp"
|
|
|
|
|
|
|
|
// mapnik
|
2006-10-05 11:00:36 +02:00
|
|
|
#include <mapnik/pool.hpp>
|
|
|
|
#include <mapnik/utils.hpp>
|
2012-04-08 02:20:56 +02:00
|
|
|
|
|
|
|
// boost
|
2005-12-12 14:15:33 +01:00
|
|
|
#include <boost/shared_ptr.hpp>
|
2011-10-18 01:41:12 +02:00
|
|
|
#include <boost/make_shared.hpp>
|
2007-06-12 10:59:54 +02:00
|
|
|
#include <boost/optional.hpp>
|
2008-02-04 17:12:13 +01:00
|
|
|
#ifdef MAPNIK_THREADSAFE
|
|
|
|
#include <boost/thread/mutex.hpp>
|
2011-11-19 20:35:07 +01:00
|
|
|
//using boost::mutex;
|
2008-02-04 17:12:13 +01:00
|
|
|
#endif
|
|
|
|
|
2012-04-08 02:20:56 +02:00
|
|
|
// stl
|
|
|
|
#include <string>
|
|
|
|
#include <sstream>
|
|
|
|
|
2007-03-16 11:11:37 +01:00
|
|
|
using mapnik::Pool;
|
|
|
|
using mapnik::singleton;
|
|
|
|
using mapnik::CreateStatic;
|
2005-06-14 17:06:59 +02:00
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
class ConnectionCreator
|
|
|
|
{
|
2007-01-16 16:22:49 +01:00
|
|
|
|
2005-06-14 17:06:59 +02:00
|
|
|
public:
|
2011-11-19 20:35:07 +01:00
|
|
|
ConnectionCreator(boost::optional<std::string> const& host,
|
|
|
|
boost::optional<std::string> const& port,
|
|
|
|
boost::optional<std::string> const& dbname,
|
|
|
|
boost::optional<std::string> const& user,
|
|
|
|
boost::optional<std::string> const& pass,
|
|
|
|
boost::optional<std::string> const& connect_timeout)
|
2010-06-25 17:23:09 +02:00
|
|
|
: host_(host),
|
|
|
|
port_(port),
|
|
|
|
dbname_(dbname),
|
|
|
|
user_(user),
|
2010-07-02 13:42:35 +02:00
|
|
|
pass_(pass),
|
|
|
|
connect_timeout_(connect_timeout) {}
|
2011-11-14 04:37:50 +01:00
|
|
|
|
2010-06-25 17:23:09 +02:00
|
|
|
T* operator()() const
|
|
|
|
{
|
2012-11-28 03:37:22 +01:00
|
|
|
return new T(connection_string_safe(),pass_);
|
2010-06-25 17:23:09 +02:00
|
|
|
}
|
2011-11-14 04:37:50 +01:00
|
|
|
|
|
|
|
inline std::string id() const
|
2010-06-25 17:23:09 +02:00
|
|
|
{
|
|
|
|
return connection_string();
|
|
|
|
}
|
2011-11-14 04:37:50 +01:00
|
|
|
|
2010-06-25 17:23:09 +02:00
|
|
|
inline std::string connection_string() const
|
2012-11-28 03:37:22 +01:00
|
|
|
{
|
|
|
|
std::string connect_str = connection_string_safe();
|
|
|
|
if (pass_ && !pass_->empty()) connect_str += " password=" + *pass_;
|
|
|
|
return connect_str;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline std::string connection_string_safe() const
|
2010-06-25 17:23:09 +02:00
|
|
|
{
|
|
|
|
std::string connect_str;
|
2012-11-28 03:37:22 +01:00
|
|
|
if (host_ && !host_->empty()) connect_str += "host=" + *host_;
|
|
|
|
if (port_ && !port_->empty()) connect_str += " port=" + *port_;
|
|
|
|
if (dbname_ && !dbname_->empty()) connect_str += " dbname=" + *dbname_;
|
|
|
|
if (user_ && !user_->empty()) connect_str += " user=" + *user_;
|
|
|
|
if (connect_timeout_ && !connect_timeout_->empty())
|
2010-07-02 13:52:11 +02:00
|
|
|
connect_str +=" connect_timeout=" + *connect_timeout_;
|
2010-06-25 17:23:09 +02:00
|
|
|
return connect_str;
|
|
|
|
}
|
2011-11-14 04:37:50 +01:00
|
|
|
|
2007-01-16 16:22:49 +01:00
|
|
|
private:
|
2011-11-19 20:35:07 +01:00
|
|
|
boost::optional<std::string> host_;
|
|
|
|
boost::optional<std::string> port_;
|
|
|
|
boost::optional<std::string> dbname_;
|
|
|
|
boost::optional<std::string> user_;
|
|
|
|
boost::optional<std::string> pass_;
|
|
|
|
boost::optional<std::string> connect_timeout_;
|
2005-06-14 17:06:59 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
class ConnectionManager : public singleton <ConnectionManager,CreateStatic>
|
|
|
|
{
|
|
|
|
|
|
|
|
friend class CreateStatic<ConnectionManager>;
|
|
|
|
typedef Pool<Connection,ConnectionCreator> PoolType;
|
2005-12-12 14:15:33 +01:00
|
|
|
typedef std::map<std::string,boost::shared_ptr<PoolType> > ContType;
|
2011-11-14 04:37:50 +01:00
|
|
|
typedef boost::shared_ptr<Connection> HolderType;
|
2005-06-14 17:06:59 +02:00
|
|
|
ContType pools_;
|
|
|
|
|
|
|
|
public:
|
2011-11-14 04:37:50 +01:00
|
|
|
|
|
|
|
bool registerPool(const ConnectionCreator<Connection>& creator,unsigned initialSize,unsigned maxSize)
|
|
|
|
{
|
2008-02-04 17:12:13 +01:00
|
|
|
#ifdef MAPNIK_THREADSAFE
|
2010-09-14 19:08:18 +02:00
|
|
|
//mutex::scoped_lock lock(mutex_);
|
2008-02-04 17:12:13 +01:00
|
|
|
#endif
|
2012-11-28 18:04:53 +01:00
|
|
|
ContType::const_iterator itr = pools_.find(creator.id());
|
|
|
|
|
|
|
|
if (itr != pools_.end())
|
2006-11-17 22:10:28 +01:00
|
|
|
{
|
2012-11-28 18:04:53 +01:00
|
|
|
unsigned cur_size = itr->second->max_size();
|
|
|
|
itr->second->set_max_size(std::max(maxSize, cur_size));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return pools_.insert(
|
|
|
|
std::make_pair(creator.id(),
|
|
|
|
boost::make_shared<PoolType>(creator,initialSize,maxSize))).second;
|
2006-11-17 22:10:28 +01:00
|
|
|
}
|
|
|
|
return false;
|
2011-11-14 04:37:50 +01:00
|
|
|
|
2005-06-14 17:06:59 +02:00
|
|
|
}
|
2011-11-14 04:37:50 +01:00
|
|
|
|
|
|
|
boost::shared_ptr<PoolType> getPool(std::string const& key)
|
2005-06-14 17:06:59 +02:00
|
|
|
{
|
2008-02-04 17:12:13 +01:00
|
|
|
#ifdef MAPNIK_THREADSAFE
|
2010-09-14 19:08:18 +02:00
|
|
|
//mutex::scoped_lock lock(mutex_);
|
2011-11-14 04:37:50 +01:00
|
|
|
#endif
|
2006-11-17 22:10:28 +01:00
|
|
|
ContType::const_iterator itr=pools_.find(key);
|
|
|
|
if (itr!=pools_.end())
|
|
|
|
{
|
|
|
|
return itr->second;
|
|
|
|
}
|
|
|
|
static const boost::shared_ptr<PoolType> emptyPool;
|
|
|
|
return emptyPool;
|
2005-06-14 17:06:59 +02:00
|
|
|
}
|
2011-11-14 04:37:50 +01:00
|
|
|
|
2006-11-27 22:13:15 +01:00
|
|
|
HolderType get(std::string const& key)
|
2005-06-14 17:06:59 +02:00
|
|
|
{
|
2008-02-04 17:12:13 +01:00
|
|
|
#ifdef MAPNIK_THREADSAFE
|
2010-09-14 19:08:18 +02:00
|
|
|
//mutex::scoped_lock lock(mutex_);
|
2011-11-14 04:37:50 +01:00
|
|
|
#endif
|
2006-11-17 22:10:28 +01:00
|
|
|
ContType::const_iterator itr=pools_.find(key);
|
2011-11-14 04:37:50 +01:00
|
|
|
if (itr!=pools_.end())
|
2006-11-17 22:10:28 +01:00
|
|
|
{
|
|
|
|
boost::shared_ptr<PoolType> pool=itr->second;
|
|
|
|
return pool->borrowObject();
|
|
|
|
}
|
2006-11-27 22:13:15 +01:00
|
|
|
return HolderType();
|
2005-06-14 17:06:59 +02:00
|
|
|
}
|
|
|
|
ConnectionManager() {}
|
2007-11-08 22:15:45 +01:00
|
|
|
private:
|
2005-06-14 17:06:59 +02:00
|
|
|
ConnectionManager(const ConnectionManager&);
|
|
|
|
ConnectionManager& operator=(const ConnectionManager);
|
2011-11-14 04:37:50 +01:00
|
|
|
};
|
2005-06-14 17:06:59 +02:00
|
|
|
|
2012-04-08 02:20:56 +02:00
|
|
|
#endif // POSTGIS_CONNECTION_MANAGER_HPP
|