<commit_before><?php

/**
 * Database connection pool class.
 *
 * PHP Version 5.3
 *
 * @category   Libraries
 * @package    DataAccess
 * @subpackage Libraries
 * @author     <NAME> <<EMAIL>>
 * @author     M2Mobi <<EMAIL>>
 */

namespace Lunr\Libraries\DataAccess;

/**
 * This class implements a simple database connection pool.
 *
 * @category   Libraries
 * @package    DataAccess
 * @subpackage Libraries
 * @author     <NAME> <<EMAIL>>
 * @author     M2Mobi <<EMAIL>>
 */
class DatabaseConnectionPool
{

    /**
     * Reference to the Configuration class
     * @var Configuration
     */
    protected $configuration;

    /**
     * Reference to the Logger class
     * @var Logger
     */
    protected $logger;

    /**
     * Readonly connection pool
     * @var array
     */
    protected $ro_pool;

    /**
     * Read-Write connection pool
     * @var array
     */
    protected $rw_pool;

    /**
     * Constructor.
     *
     * @param Configuration &$configuration Reference to the configuration class
     * @param Logger        &$logger        Reference to the logger class
     */
    public function __construct(&$configuration, &$logger)
    {
        $this->configuration =& $configuration;
        $this->logger        =& $logger;

        $this->ro_pool = array();
        $this->rw_pool = array();
    }

    /**
     * Destructor.
     */
    public function __destruct()
    {
        $this->configuration = NULL;
        $this->logger        = NULL;

        unset($this->ro_pool);
        unset($this->rw_pool);
    }

    /**
     * Get a new readonly connection from the pool.
     *
     * @return DatabaseConnection $db A database connection
     */
    public function get_new_ro_connection()
    {
        return $this->get_connection(TRUE, TRUE);
    }

    /**
     * Get a new read-write connection from the pool.
     *
     * @return DatabaseConnection $db A database connection
     */
    public function get_new_rw_connection()
    {
        return $this->get_connection(TRUE, FALSE);
    }

    /**
     * Get an existing readonly connection from the pool.
     *
     * @return DatabaseConnection $db A database connection
     */
    public function get_ro_connection()
    {
        return $this->get_connection(FALSE, TRUE);
    }

    /**
     * Get an existing read-write connection from the pool.
     *
     * @return DatabaseConnection $db A database connection
     */
    public function get_rw_connection()
    {
        return $this->get_connection(FALSE, FALSE);
    }

    /**
     * Get a database connection.
     *
     * @param Boolean $new Whether to get a new connection or not
     * @param Boolean $ro  Whether to get a readonly connection or not
     *
     * @return DatabaseConnection $db A database connection
     */
    protected function get_connection($new, $ro)
    {
        $store = $ro ? 'ro_pool' : 'rw_pool';

        switch ($this->configuration['db']['driver'])
        {
            case 'mysql':
                $type  = 'MySQLConnection';
                $extra = new MySQLi();
                break;
            default:
                return NULL;
                break;
        }

        if ($new === TRUE)
        {
            $connection = new $type($this->configuration, $this->logger, $extra);

            if ($ro === TRUE)
            {
                $connection->set_readonly(TRUE);
            }

            $this->{$store}[] =& $connection;
        }
        else
        {
            $connection =& $this->$store[0];
        }

        return $connection;
    }
<commit_msg>dataaccess: Create new connection in pool also when pool is empty<commit_after><?php

/**
 * Database connection pool class.
 *
 * PHP Version 5.3
 *
 * @category   Libraries
 * @package    DataAccess
 * @subpackage Libraries
 * @author     <NAME> <<EMAIL>>
 * @author     M2Mobi <<EMAIL>>
 */

namespace Lunr\Libraries\DataAccess;

/**
 * This class implements a simple database connection pool.
 *
 * @category   Libraries
 * @package    DataAccess
 * @subpackage Libraries
 * @author     <NAME>inger <<EMAIL>>
 * @author     M2Mobi <<EMAIL>>
 */
class DatabaseConnectionPool
{

    /**
     * Reference to the Configuration class
     * @var Configuration
     */
    protected $configuration;

    /**
     * Reference to the Logger class
     * @var Logger
     */
    protected $logger;

    /**
     * Readonly connection pool
     * @var array
     */
    protected $ro_pool;

    /**
     * Read-Write connection pool
     * @var array
     */
    protected $rw_pool;

    /**
     * Constructor.
     *
     * @param Configuration &$configuration Reference to the configuration class
     * @param Logger        &$logger        Reference to the logger class
     */
    public function __construct(&$configuration, &$logger)
    {
        $this->configuration =& $configuration;
        $this->logger        =& $logger;

        $this->ro_pool = array();
        $this->rw_pool = array();
    }

    /**
     * Destructor.
     */
    public function __destruct()
    {
        $this->configuration = NULL;
        $this->logger        = NULL;

        unset($this->ro_pool);
        unset($this->rw_pool);
    }

    /**
     * Get a new readonly connection from the pool.
     *
     * @return DatabaseConnection $db A database connection
     */
    public function get_new_ro_connection()
    {
        return $this->get_connection(TRUE, TRUE);
    }

    /**
     * Get a new read-write connection from the pool.
     *
     * @return DatabaseConnection $db A database connection
     */
    public function get_new_rw_connection()
    {
        return $this->get_connection(TRUE, FALSE);
    }

    /**
     * Get an existing readonly connection from the pool.
     *
     * @return DatabaseConnection $db A database connection
     */
    public function get_ro_connection()
    {
        return $this->get_connection(FALSE, TRUE);
    }

    /**
     * Get an existing read-write connection from the pool.
     *
     * @return DatabaseConnection $db A database connection
     */
    public function get_rw_connection()
    {
        return $this->get_connection(FALSE, FALSE);
    }

    /**
     * Get a database connection.
     *
     * @param Boolean $new Whether to get a new connection or not
     * @param Boolean $ro  Whether to get a readonly connection or not
     *
     * @return DatabaseConnection $db A database connection
     */
    protected function get_connection($new, $ro)
    {
        $store = $ro ? 'ro_pool' : 'rw_pool';

        switch ($this->configuration['db']['driver'])
        {
            case 'mysql':
                $type  = 'MySQLConnection';
                $extra = new MySQLi();
                break;
            default:
                return NULL;
                break;
        }

        if (($new === TRUE) || (sizeof($this->$store) == 0))
        {
            $connection = new $type($this->configuration, $this->logger, $extra);

            if ($ro === TRUE)
            {
                $connection->set_readonly(TRUE);
            }

            $this->{$store}[] =& $connection;
        }
        else
        {
            $connection =& $this->$store[0];
        }

        return $connection;
    }

{'fraction_non_alphanumeric': 0.11631108052305575, 'fraction_numerical': 0.0015141087405368204, 'mean_word_length': 1.8561320754716981, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 60, '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': '6125562', 'n_tokens_mistral': 2028, 'n_tokens_neox': 1960, 'n_words': 548}