<commit_before><?php

/**
 * Rediska transaction
 * 
 * @author <NAME>
 * @package Rediska
 * @version @package_version@
 * @link http://rediska.geometria-lab.net
 * @license http://www.opensource.org/licenses/bsd-license.php
 */
class Rediska_Transaction
{
    /**
     * Rediska instance
     * 
     * @var Rediska
     */
    protected $_rediska;

    /**
     * Rediska specified connection instance
     * 
     * @var Rediska_Connection
     */
    protected $_connection;

    /**
     * Rediska specified connection instance
     * 
     * @var Rediska_Connection_Specified
     */
    protected $_specifiedConnection;

    /**
     * Commands buffer
     * 
     * @var array
     */
    protected $_commands = array();

    /**
     * If transaction watched something
     *
     * @var bool
     */
    protected $_isWatched = false;

    /**
     * Constructor
     * 
     * @param Rediska            $rediska    Rediska instance
     * @param Rediska_Connection $connection Trancation connection
     */
    public function __construct(Rediska $rediska, Rediska_Connection_Specified $specifiedConnection, Rediska_Connection $connection)
    {
        $this->_rediska             = $rediska;
        $this->_specifiedConnection = $specifiedConnection;
        $this->_connection          = $connection;

        $this->_throwIfNotSupported('Transactions', '1.3.8');
    }

    /**
     * Marks the given keys to be watched for conditional execution of a transaction.
     *
     * @throws Rediska_Transaction_Exception
     * @param  $keyOrKeys Key or array of keys
     * @return Rediska_Transaction
     */
    public function watch($keyOrKeys)
    {
        $this->_throwIfNotSupported('Watch', '2.1');

        $command = array('WATCH');

        if (!is_array($keyOrKeys)) {
            $keys = array($keyOrKeys);
        } else {
            $keys = $keyOrKeys;
        }

        foreach($keys as $key) {
            $command[] = $this->_rediska->getOption('namespace') . $key;
        }

        $exec = new Rediska_Connection_Exec($this->_connection, $command);
        $exec->execute();

        $this->_isWatched = true;

        return $this;
    }

    /**
     * Flushes all the previously watched keys for a transaction.
     *
     * @return Rediska_Transaction
     */
    public function unwatch()
    {
        $this->_throwIfNotSupported('Unwatch', '2.1');

        $command = 'UNWATCH';

        $exec = new Rediska_Connection_Exec($this->_connection, $command);
        $exec->execute();

        $this->_isWatched = false;

        return $this;
    }

    /**
     * Execute transaction
     * 
     * @return array
     */
    public function execute()
    {
        $results = array();

        if (!empty($this->_commands)) {
            $this->_rediska->getProfiler()->start($this);

            $multi = new Rediska_Connection_Exec($this->_connection, 'MULTI');
            $multi->execute();

            foreach($this->_commands as $command) {
                $command->execute();
            }

            $exec = new Rediska_Connection_Exec($this->_connection, 'EXEC');
            $responses = $exec->execute();

            $this->_rediska->getProfiler()->stop();

            if (!$responses) {
                throw new Rediska_Transaction_AbortedException('Transaction has been aborted by server');
            }

            foreach($this->_commands as $i => $command) {
                $results[] = $command->parseResponses(array($responses[$i]));
            }

            $this->_reset();
        }

        return $results;
    }

    /**
     * Magic method for execute
     *<commit_msg>transaction can executed without commands<commit_after><?php

/**
 * Rediska transaction
 * 
 * @author <NAME>
 * @package Rediska
 * @version @package_version@
 * @link http://rediska.geometria-lab.net
 * @license http://www.opensource.org/licenses/bsd-license.php
 */
class Rediska_Transaction
{
    /**
     * Rediska instance
     * 
     * @var Rediska
     */
    protected $_rediska;

    /**
     * Rediska specified connection instance
     * 
     * @var Rediska_Connection
     */
    protected $_connection;

    /**
     * Rediska specified connection instance
     * 
     * @var Rediska_Connection_Specified
     */
    protected $_specifiedConnection;

    /**
     * Commands buffer
     * 
     * @var array
     */
    protected $_commands = array();

    /**
     * If transaction watched something
     *
     * @var bool
     */
    protected $_isWatched = false;

    /**
     * Constructor
     * 
     * @param Rediska            $rediska    Rediska instance
     * @param Rediska_Connection $connection Trancation connection
     */
    public function __construct(Rediska $rediska, Rediska_Connection_Specified $specifiedConnection, Rediska_Connection $connection)
    {
        $this->_rediska             = $rediska;
        $this->_specifiedConnection = $specifiedConnection;
        $this->_connection          = $connection;

        $this->_throwIfNotSupported('Transactions', '1.3.8');
    }

    /**
     * Marks the given keys to be watched for conditional execution of a transaction.
     *
     * @throws Rediska_Transaction_Exception
     * @param  $keyOrKeys Key or array of keys
     * @return Rediska_Transaction
     */
    public function watch($keyOrKeys)
    {
        $this->_throwIfNotSupported('Watch', '2.1');

        $command = array('WATCH');

        if (!is_array($keyOrKeys)) {
            $keys = array($keyOrKeys);
        } else {
            $keys = $keyOrKeys;
        }

        foreach($keys as $key) {
            $command[] = $this->_rediska->getOption('namespace') . $key;
        }

        $exec = new Rediska_Connection_Exec($this->_connection, $command);
        $exec->execute();

        $this->_isWatched = true;

        return $this;
    }

    /**
     * Flushes all the previously watched keys for a transaction.
     *
     * @return Rediska_Transaction
     */
    public function unwatch()
    {
        $this->_throwIfNotSupported('Unwatch', '2.1');

        $command = 'UNWATCH';

        $exec = new Rediska_Connection_Exec($this->_connection, $command);
        $exec->execute();

        $this->_isWatched = false;

        return $this;
    }

    /**
     * Execute transaction
     * 
     * @return array
     */
    public function execute()
    {
        $results = array();

        $this->_rediska->getProfiler()->start($this);

        $multi = new Rediska_Connection_Exec($this->_connection, 'MULTI');
        $multi->execute();

        foreach($this->_commands as $command) {
            $command->execute();
        }

        $exec = new Rediska_Connection_Exec($this->_connection, 'EXEC');
        $responses = $exec->execute();

        $this->_rediska->getProfiler()->stop();

        if (!$responses) {
            throw new Rediska_Transaction_AbortedException('Transaction has been aborted by server');
        }

        foreach($this->_commands as $i => $command) {
            $results[] = $command->parseResponses(array($responses[$i]));
        }

        $this->_reset();

        return $results;
    }

    /**
     * Magic method for execute
     *
{'fraction_non_alphanumeric': 0.127877947295423, 'fraction_numerical': 0.001941747572815534, 'mean_word_length': 2.0581000848176423, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 66, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '5808201', 'n_tokens_mistral': 2050, 'n_tokens_neox': 1968, 'n_words': 424}