<commit_before>/*****************************************************************************
 *
 * This file is part of Mapnik (c++ mapping toolkit)
 *
 * Copyright (C) 2011 <NAME>
 *
 * 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,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * 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
 *
 *****************************************************************************/

#ifndef POSTGIS_CONNECTION_MANAGER_HPP
#define POSTGIS_CONNECTION_MANAGER_HPP

#include "connection.hpp"

// mapnik
#include <mapnik/pool.hpp>
#include <mapnik/utils.hpp>

// boost
#include <boost/shared_ptr.hpp>
#include <boost/make_shared.hpp>
#include <boost/optional.hpp>
#ifdef MAPNIK_THREADSAFE
#include <boost/thread/mutex.hpp>
//using boost::mutex;
#endif

// stl
#include <string>
#include <sstream>

using mapnik::Pool;
using mapnik::singleton;
using mapnik::CreateStatic;

template <typename T>
class ConnectionCreator
{

public:
    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)
        : host_(host),
          port_(port),
          dbname_(dbname),
          user_(user),
          pass_(<PASSWORD>),
          connect_timeout_(connect_timeout) {}

    T* operator()() const
    {
        return new T(connection_string_safe(),pass_);
    }

    inline std::string id() const
    {
        return connection_string();
    }

    inline std::string connection_string() const
    {
        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
    {
        std::string connect_str;
        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())
            connect_str +=" connect_timeout=" + *connect_timeout_;
        return connect_str;
    }

private:
    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_;
};

class ConnectionManager : public singleton <ConnectionManager,CreateStatic>
{

    friend class CreateStatic<ConnectionManager>;
    typedef Pool<Connection,ConnectionCreator> PoolType;
    typedef std::map<std::string,boost::shared_ptr<PoolType> > ContType;
    typedef boost::shared_ptr<Connection> HolderType;
    ContType pools_;

public:

    bool registerPool(const ConnectionCreator<Connection>& creator,unsigned initialSize,unsigned maxSize)
    {
#ifdef MAPNIK_THREADSAFE
        //mutex::scoped_lock lock(mutex_);
#endif
        ContType::const_iterator itr = pools_.find(creator.id());

        if (itr != pools_.end())
        {
            itr->second->set_initial_size(initialSize);
            itr->second->set_max_size(maxSize);
        }
        else
        {
            return pools_.insert(
                std::make_pair(creator.id(),
                               boost::make_shared<PoolType>(creator,initialSize,maxSize))).second;
        }
        return false;

    }

    boost::shared_ptr<PoolType> getPool(std::string const& key)
    {
#ifdef MAPNIK_THREADSAFE
        //mutex::scoped_lock lock(mutex_);
#endif
        ContType::const_iterator itr=pools_.find(key);
        if (itr!=pools_.end())
        {
            return itr->second;
        }
        static const boost::shared_ptr<PoolType> emptyPool;
        return emptyPool;
    }

    HolderType get(std::string const& key)
    {
#ifdef MAPNIK_THREADSAFE
        //mutex::scoped_lock lock(mutex_);
#endif
        ContType::const_iterator itr=pools_.find(key);
        if (itr!=pools_.end())
        {
            boost::shared_ptr<PoolType> pool=itr->second;
            return pool->borrowObject();
        }
        return HolderType();
    }
    ConnectionManager() {}
private:
    ConnectionManager(const ConnectionManager&);
    ConnectionManager& operator=(const ConnectionManager);
};

#endif // POSTGIS_CONNECTION_MANAGER_HPP
<commit_msg>cleanup unused code<commit_after>/*****************************************************************************
 *
 * This file is part of Mapnik (c++ mapping toolkit)
 *
 * Copyright (C) 2011 <NAME>
 *
 * 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,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * 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
 *
 *****************************************************************************/

#ifndef POSTGIS_CONNECTION_MANAGER_HPP
#define POSTGIS_CONNECTION_MANAGER_HPP

#include "connection.hpp"

// mapnik
#include <mapnik/pool.hpp>
#include <mapnik/utils.hpp>

// boost
#include <boost/shared_ptr.hpp>
#include <boost/make_shared.hpp>
#include <boost/optional.hpp>

// stl
#include <string>
#include <sstream>

using mapnik::Pool;
using mapnik::singleton;
using mapnik::CreateStatic;

template <typename T>
class ConnectionCreator
{

public:
    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)
        : host_(host),
          port_(port),
          dbname_(dbname),
          user_(user),
          pass_(<PASSWORD>),
          connect_timeout_(connect_timeout) {}

    T* operator()() const
    {
        return new T(connection_string_safe(),pass_);
    }

    inline std::string id() const
    {
        return connection_string();
    }

    inline std::string connection_string() const
    {
        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
    {
        std::string connect_str;
        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())
            connect_str +=" connect_timeout=" + *connect_timeout_;
        return connect_str;
    }

private:
    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_;
};

class ConnectionManager : public singleton <ConnectionManager,CreateStatic>
{

    friend class CreateStatic<ConnectionManager>;
    typedef Pool<Connection,ConnectionCreator> PoolType;
    typedef std::map<std::string,boost::shared_ptr<PoolType> > ContType;
    typedef boost::shared_ptr<Connection> HolderType;
    ContType pools_;

public:

    bool registerPool(const ConnectionCreator<Connection>& creator,unsigned initialSize,unsigned maxSize)
    {
        ContType::const_iterator itr = pools_.find(creator.id());

        if (itr != pools_.end())
        {
            itr->second->set_initial_size(initialSize);
            itr->second->set_max_size(maxSize);
        }
        else
        {
            return pools_.insert(
                std::make_pair(creator.id(),
                               boost::make_shared<PoolType>(creator,initialSize,maxSize))).second;
        }
        return false;

    }

    boost::shared_ptr<PoolType> getPool(std::string const& key)
    {
        ContType::const_iterator itr=pools_.find(key);
        if (itr!=pools_.end())
        {
            return itr->second;
        }
        static const boost::shared_ptr<PoolType> emptyPool;
        return emptyPool;
    }

    ConnectionManager() {}
private:
    ConnectionManager(const ConnectionManager&);
    ConnectionManager& operator=(const ConnectionManager);
};

#endif // POSTGIS_CONNECTION_MANAGER_HPP

{'fraction_non_alphanumeric': 0.14401846647932556, 'fraction_numerical': 0.0034122842232035327, 'mean_word_length': 3.1417290108063174, 'pattern_counts': {'":': 0, '<': 69, '<?xml version=': 0, '>': 93, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3944534', 'n_tokens_mistral': 2837, 'n_tokens_neox': 2730, 'n_words': 704}