<commit_before><?php
/**
 * DBAccess.php
 *
 * PHP version 5
 *
 * @category PHP
 * @package  /src/class/
 * @author   <NAME> <<EMAIL>>
 * @license  MIT Licence
 * @version  GIT: <indievox-inc/DBAccess>
 * @link     https://github.com/indievox-inc/DBAccess
 */

namespace iNDIEVOX\DBAccess;

/**
 * DBAccess
 *
 * @category PHP
 * @package  /src/class/
 * @author   <NAME> <<EMAIL>>
 * @license  MIT Licence
 * @version  Release: <0.0.1>
 * @link     https://github.com/indievox-inc/DBAccess
 */
class DBAccess
{

    protected static $db_obj;
    protected static $instance_count = 0;
    public $current_mode;
    public $context_status;
    protected $db_config = array();
    protected $db_name;
    protected $db_connection;
    protected $db_name_poll = array();
    protected $db_connection_poll = array();

    /**
     * Method getInstance to get db_obj
     *
     * @param array $db_config # database config
     *
     * @return object $db_obj
     */
    public static function getInstance($db_config)
    {

        if (!self::$db_obj || !isset(self::$db_obj) || empty(self::$db_obj)) {

            self::$db_obj = new DBAccess($db_config);

        }

        $this_db_obj = self::$db_obj;

        // round robin switch slave connection
        if (   $this_db_obj->context_status=='one_time'
            && $this_db_obj->current_mode!='master'
            && $this_db_obj->current_mode!='random'
        ) {

            if (!empty($this_db_obj->db_config["slave_database_name"])) {

                $slave_database_count = count($this_db_obj->db_config["slave_database_name"]);
                $slave_index = array_search(
                    $this_db_obj->current_mode,
                    $this_db_obj->db_config["slave_database_name"]
                );
                $new_slave_index = $slave_index+1;

                if ($new_slave_index < $slave_database_count) {

                    $options = array('mode' => $this_db_obj->db_config["slave_database_name"][$new_slave_index]);
                    $slave_db_choose = $this_db_obj->db_config["slave_database_name"][$new_slave_index];

                } else {

                    $options = array('mode' => $this_db_obj->db_config["slave_database_name"][0]);
                    $slave_db_choose = $this_db_obj->db_config["slave_database_name"][0];

                }

                if (   !$this_db_obj->db_connection_poll[$slave_db_choose]
                    || !isset($this_db_obj->db_connection_poll[$slave_db_choose])
                    || empty($this_db_obj->db_connection_poll[$slave_db_choose])
                ) {

                    $this_db_obj->connectSlave($options);

                } else {

                    // switch to this slave
                    $this_db_obj->current_mode  = $slave_db_choose;
                    $this_db_obj->db_name       = $this_db_obj->db_name_poll[$slave_db_choose];
                    $this_db_obj->db_connection = $this_db_obj->db_connection_poll[$slave_db_choose];

                }

            }// end if (!empty($slave_database_name))

        }

        return $this_db_obj;

    }// end function getInstance

    /**
     * Method __construct initialize instance
     *
     * @param array $db_config # database config
     *
     * @return void
     */
    private function __construct($db_config)
    {

        $this->db_config            = $db_config;
        $this->context_status       = 'one_time';
        $this->db_name_poll         = array();<commit_msg>add forceSwitchMaster and forceSwitchMasterWholeContext method<commit_after><?php
/**
 * DBAccess.php
 *
 * PHP version 5
 *
 * @category PHP
 * @package  /src/class/
 * @author   <NAME> <<EMAIL>>
 * @license  MIT Licence
 * @version  GIT: <indievox-inc/DBAccess>
 * @link     https://github.com/indievox-inc/DBAccess
 */

namespace iNDIEVOX\DBAccess;

/**
 * DBAccess
 *
 * @category PHP
 * @package  /src/class/
 * @author   <NAME> <<EMAIL>>
 * @license  MIT Licence
 * @version  Release: <0.0.1>
 * @link     https://github.com/indievox-inc/DBAccess
 */
class DBAccess
{

    protected static $db_obj;
    protected static $instance_count = 0;
    public $current_mode;
    public $context_status;
    protected $db_config = array();
    protected $db_name;
    protected $db_connection;
    protected $db_name_poll = array();
    protected $db_connection_poll = array();

    /**
     * Method getInstance to get db_obj
     *
     * @param array $db_config # database config
     *
     * @return object $db_obj
     */
    public static function getInstance($db_config)
    {

        if (!self::$db_obj || !isset(self::$db_obj) || empty(self::$db_obj)) {

            self::$db_obj = new DBAccess($db_config);

        }

        $this_db_obj = self::$db_obj;

        // round robin switch slave connection
        if (   $this_db_obj->context_status=='one_time'
            && $this_db_obj->current_mode!='master'
            && $this_db_obj->current_mode!='random'
        ) {

            if (!empty($this_db_obj->db_config["slave_database_name"])) {

                $slave_database_count = count($this_db_obj->db_config["slave_database_name"]);
                $slave_index = array_search(
                    $this_db_obj->current_mode,
                    $this_db_obj->db_config["slave_database_name"]
                );
                $new_slave_index = $slave_index+1;

                if ($new_slave_index < $slave_database_count) {

                    $options = array('mode' => $this_db_obj->db_config["slave_database_name"][$new_slave_index]);
                    $slave_db_choose = $this_db_obj->db_config["slave_database_name"][$new_slave_index];

                } else {

                    $options = array('mode' => $this_db_obj->db_config["slave_database_name"][0]);
                    $slave_db_choose = $this_db_obj->db_config["slave_database_name"][0];

                }

                if (   !$this_db_obj->db_connection_poll[$slave_db_choose]
                    || !isset($this_db_obj->db_connection_poll[$slave_db_choose])
                    || empty($this_db_obj->db_connection_poll[$slave_db_choose])
                ) {

                    $this_db_obj->connectSlave($options);

                } else {

                    // switch to this slave
                    $this_db_obj->current_mode  = $slave_db_choose;
                    $this_db_obj->db_name       = $this_db_obj->db_name_poll[$slave_db_choose];
                    $this_db_obj->db_connection = $this_db_obj->db_connection_poll[$slave_db_choose];

                }

            }// end if (!empty($slave_database_name))

        }

        return $this_db_obj;

    }// end function getInstance

    /**
     * Method forceSwitchMaster
     *
     * @return void
     */
    public static function forceSwitchMaster()
    {

        // force read master
        $db_change_mode_options = array('mode'=>'master');
        self::$db_obj->changeMode($db_change_mode_options);
        self::$db_obj->context_status = "one_time";

    }// end function forceSwitchMaster

    /**
     * Method forceSwitchMaster
     *
     * @return void
     */
    public static function forceSwitchMasterWholeContext()
    {

        // force read master
        $db_change_mode_options = array('mode'=>'master');
        self::$db_obj->changeMode($db_change_mode_options);
        self::$db_obj->context_status = "whole_context";

    }// end function forceSwitchMaster

    /**
     * Method __construct initialize instance
     *
     * @param array $db_config # database config
     *
     * @return void
     */
    private function __construct($db_config)
    {

        $this->db_config            = $db_config;
        $this->context_status       = 'one_time';
        $this->db_name_poll         = array();
{'fraction_non_alphanumeric': 0.12583120204603582, 'fraction_numerical': 0.0020460358056265983, 'mean_word_length': 2.154901169826543, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 75, 'https://': 4, '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': '2845890', 'n_tokens_mistral': 2486, 'n_tokens_neox': 2384, 'n_words': 375}