<commit_before><?php
App::uses('DataSource', 'Model/Datasource');

/**
 * Redis Source class
 *
 */
class RedisSource extends DataSource {

/**
 * Datasource description.
 *
 * @var string
 */
	public $description = 'Redis Data Source';

/**
 * Base configuration.
 *
 * @var array
 */
	protected $_baseConfig = array(
		'server' => '127.0.0.1',
		'port' => 6379,
		'password' => '',
		'database' => 0,
		'timeout' => 0,
		'persistent' => true,
		'unix_socket' => '',
		'prefix' => '',
	);

/**
 * Configuration.
 *
 * @var array
 */
	public $config = array();

/**
 * A reference to the physical connection of this DataSource.
 *
 * @var Redis
 */
	protected $_connection = null;

/**
 * Constructor.
 *
 *   Opens the connection to the server (if needed).
 *
 * @param array $config Array of configuration information for the Datasource
 * @param bool $autoConnect Whether or not the datasource should automatically connect
 * @return bool If autoconnect is true, true if the database could be connected, else false, otherwise, (always) true
 * @throws MissingConnectionException when a connection cannot be made
 */
	public function __construct($config = null, $autoConnect = true) {
		parent::__construct($config);

		if (!$this->enabled()) {
			throw new MissingConnectionException(array(
				'class' => get_class($this),
				'message' => __d('cake_dev', 'Selected driver is not enabled'),
				'enabled' => false
			));
		}

		if ($autoConnect) {
			return $this->connect();
		}

		return true;
	}

/**
 * Destructor.
 *
 *   Closes the connection to the server (if needed).
 *
 * @return void
 */
	public function __destruct() {
		if (!$this->config['persistent']) {
			$this->close();
		}
	}

/**
 * Check that the redis extension is installed/loaded.
 *
 * @return bool Whether or not the extension is loaded
 */
	public function enabled() {
		return extension_loaded('redis');
	}

/**
 * Connects to the database using options in the given configuration array.
 *
 * @return bool True if the database could be connected, else false
 * @throws MissingConnectionException
 */
	public function connect() {
		$this->connected = false;

		try {
			$this->_connection = new Redis();
			$this->connected = true;

		} catch (Exception $e) {
			throw new MissingConnectionException(array('class' => get_class($this), 'message' => $e->getMessage()));
		}

		return $this->connected;
	}

/**
 * Closes a connection.
 *
 * @return bool Always true
 */
	public function close() {
		if ($this->isConnected()) {
			$this->_connection->close();
			unset($this->_connection);
		}

		$this->connected = false;

		return true;
	}

/**
 * Checks if the source is connected to the database.
 *
 * @return bool True if the database is connected, else false
 */
	public function isConnected() {
		return $this->connected;
	}<commit_msg>Added functionality and did some cleanup<commit_after><?php
App::uses('DataSource', 'Model/Datasource');

/**
 * Redis Source class
 *
 */
class RedisSource extends DataSource {

/**
 * Datasource description.
 *
 * @var string
 */
	public $description = 'Redis Data Source';

/**
 * Base configuration.
 *
 * @var array
 */
	protected $_baseConfig = array(
		'host' => '127.0.0.1',
		'port' => 6379,
		'password' => '',
		'database' => 0,
		'timeout' => 0,
		'persistent' => false,
		'unix_socket' => '',
		'prefix' => '',
	);

/**
 * Configuration.
 *
 * @var array
 */
	public $config = array();

/**
 * A reference to the physical connection of this DataSource.
 *
 * @var Redis
 */
	protected $_connection = null;

/**
 * Whether or not we are connected to the DataSource.
 *
 * @var bool
 */
	public $connected = false;

/**
	* Constructor.
	*
	* @param array $config Array of configuration information for the Datasource
	* @return bool True if connecting to the DataSource succeeds, else false
	*/
	public function __construct($config = array()) {
		parent::__construct($config);

		if (!$this->enabled()) {
			return false;
		}

		return $this->connect();
	}

/**
 * Destructor.
 *
 *  Closes the connection to the host (if needed).
 *
 * @return void
 */
	public function __destruct() {
		if (!$this->config['persistent']) {
			$this->close();
		}
	}

/**
 * Passes (non-existing) method calls to `Redis`.
 *
 * @param string $name The name of the method being called
 * @param array $arguments An enumerated array containing the parameters passed to the method
 * @return mixed Method return value
 */
	public function __call($name, $arguments) {
		return call_user_func_array(array($this->_connection, $name), $arguments);
	}

/**
 * Check that the redis extension is loaded.
 *
 * @return bool Whether or not the extension is loaded
 */
	public function enabled() {
		return extension_loaded('redis');
	}

/**
 * Connects to the database using options in the given configuration array.
 *
 *  "Connects mean:
 *  - connect
 *  - authenticate
 *  - select
 *  - setPrefix
 *
 * @return bool
 */
	public function connect() {
		$this->connected = $this->_connect();
		$this->connected = $this->connected && $this->_authenticate();
		$this->connected = $this->connected && $this->_select();
		$this->connected = $this->connected && !$this->_setPrefix();

		return $this->connected;
	}

/**
 * Connects to the database using options in the given configuration array.
 *
 * @return bool True if connecting to the DataSource succeeds, else false
 */
	protected function _connect() {
		try {
			$this->_connection = new Redis();

			if ($this->config['unix_socket']) {
				return $this->_connection->connect($this->config['unix_socket']);
			} elseif (!$this->config['persistent']) {
				return $this->_connection->connect(
					$this->config['host'], $this->config['port'], $this->config['timeout']
				);
			} else {
				$persistentId = crc32(serialize($this->config));

				return $this->_connection->pconnect(
					$this->config['host'], $this->config['port'], $this->config['timeout'], $persistentId
				);
			}
		} catch (RedisException $e) {
			return false;
		}
	}

/**
 * Authenticates to the database (if needed) using options in the given configuration array.
 *
 * @return bool True if the authentication succeeded or no password was specified, else false
 */
	protected function _authenticate() {
		if ($this->config['password']) {
			return $this->_connection->auth($this->config['password']);
		}

		return true;
	}

/**
 * Selects a database (if needed) using options in the given configuration array.
 *
 * @return bool True if the select succeeded or no database was specified, else false
 */
	protected function _select() {
		if ($this->config['database']) {
			return $this->_connection->select($this->config['database']);
		}

		return true;
	}

/**
 * Sets a prefix for all keys (if needed) using options in the given configuration array.
 *
 * @return bool True if setting the prefix succeeded or no prefix was specified, else false
 */
	protected function _setPrefix() {
		if ($this->config['prefix']) {
			return $this->_connection->setOption(Redis::OPT_PREFIX, $this->config['prefix']);
		}

		return true;
	}

/**
 * Closes a connection.
 *
 * @return bool Always true
 */
	public function close() {
		if ($this->isConnected()) {
			$this->_connection->close();
		}

		$this->connected = false;
		$this->_connection = null;

		return true;
	}

/**
 * Checks if the source is connected to the database.
 *
 * @return bool True if the database is connected, else false
 */
	public function isConnected() {
		return $this->connected;
	}
{'fraction_non_alphanumeric': 0.1505275811406438, 'fraction_numerical': 0.003472685989047683, 'mean_word_length': 3.5938650306748468, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 91, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, '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': '5606688', 'n_tokens_mistral': 2414, 'n_tokens_neox': 2235, 'n_words': 705}