/usr/share/php/php-nrk-predis/Pipeline/Pipeline.php is in php-nrk-predis 1.0.0-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 | <?php
/*
* This file is part of the Predis package.
*
* (c) Daniele Alessandri <suppakilla@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Predis\Pipeline;
use Exception;
use InvalidArgumentException;
use SplQueue;
use Predis\ClientContextInterface;
use Predis\ClientException;
use Predis\ClientInterface;
use Predis\Command\CommandInterface;
use Predis\Connection\ConnectionInterface;
use Predis\Connection\Aggregate\ReplicationInterface;
use Predis\Response\ErrorInterface as ErrorResponseInterface;
use Predis\Response\ResponseInterface;
use Predis\Response\ServerException;
/**
* Implementation of a command pipeline in which write and read operations of
* Redis commands are pipelined to alleviate the effects of network round-trips.
*
* {@inheritdoc}
*
* @author Daniele Alessandri <suppakilla@gmail.com>
*/
class Pipeline implements ClientContextInterface
{
private $client;
private $pipeline;
private $responses = array();
private $running = false;
/**
* @param ClientInterface $client Client instance used by the context.
*/
public function __construct(ClientInterface $client)
{
$this->client = $client;
$this->pipeline = new SplQueue();
}
/**
* Queues a command into the pipeline buffer.
*
* @param string $method Command ID.
* @param array $arguments Arguments for the command.
*
* @return $this
*/
public function __call($method, $arguments)
{
$command = $this->client->createCommand($method, $arguments);
$this->recordCommand($command);
return $this;
}
/**
* Queues a command instance into the pipeline buffer.
*
* @param CommandInterface $command Command to be queued in the buffer.
*/
protected function recordCommand(CommandInterface $command)
{
$this->pipeline->enqueue($command);
}
/**
* Queues a command instance into the pipeline buffer.
*
* @param CommandInterface $command Command instance to be queued in the buffer.
*
* @return $this
*/
public function executeCommand(CommandInterface $command)
{
$this->recordCommand($command);
return $this;
}
/**
* Throws an exception on -ERR responses returned by Redis.
*
* @param ConnectionInterface $connection Redis connection that returned the error.
* @param ErrorResponseInterface $response Instance of the error response.
*
* @throws ServerException
*/
protected function exception(ConnectionInterface $connection, ErrorResponseInterface $response)
{
$connection->disconnect();
$message = $response->getMessage();
throw new ServerException($message);
}
/**
* Returns the underlying connection to be used by the pipeline.
*
* @return ConnectionInterface
*/
protected function getConnection()
{
$connection = $this->getClient()->getConnection();
if ($connection instanceof ReplicationInterface) {
$connection->switchTo('master');
}
return $connection;
}
/**
* Implements the logic to flush the queued commands and read the responses
* from the current connection.
*
* @param ConnectionInterface $connection Current connection instance.
* @param SplQueue $commands Queued commands.
*
* @return array
*/
protected function executePipeline(ConnectionInterface $connection, SplQueue $commands)
{
foreach ($commands as $command) {
$connection->writeRequest($command);
}
$responses = array();
$exceptions = $this->throwServerExceptions();
while (!$commands->isEmpty()) {
$command = $commands->dequeue();
$response = $connection->readResponse($command);
if (!$response instanceof ResponseInterface) {
$responses[] = $command->parseResponse($response);
} elseif ($response instanceof ErrorResponseInterface && $exceptions) {
$this->exception($connection, $response);
} else {
$responses[] = $response;
}
}
return $responses;
}
/**
* Flushes the buffer holding all of the commands queued so far.
*
* @param bool $send Specifies if the commands in the buffer should be sent to Redis.
*
* @return $this
*/
public function flushPipeline($send = true)
{
if ($send && !$this->pipeline->isEmpty()) {
$responses = $this->executePipeline($this->getConnection(), $this->pipeline);
$this->responses = array_merge($this->responses, $responses);
} else {
$this->pipeline = new SplQueue();
}
return $this;
}
/**
* Marks the running status of the pipeline.
*
* @param bool $bool Sets the running status of the pipeline.
*
* @throws ClientException
*/
private function setRunning($bool)
{
if ($bool && $this->running) {
throw new ClientException('The current pipeline context is already being executed.');
}
$this->running = $bool;
}
/**
* Handles the actual execution of the whole pipeline.
*
* @param mixed $callable Optional callback for execution.
*
* @return array
*
* @throws Exception
* @throws InvalidArgumentException
*/
public function execute($callable = null)
{
if ($callable && !is_callable($callable)) {
throw new InvalidArgumentException('The argument must be a callable object.');
}
$exception = null;
$this->setRunning(true);
try {
if ($callable) {
call_user_func($callable, $this);
}
$this->flushPipeline();
} catch (Exception $exception) {
// NOOP
}
$this->setRunning(false);
if ($exception) {
throw $exception;
}
return $this->responses;
}
/**
* Returns if the pipeline should throw exceptions on server errors.
*
* @return bool
*/
protected function throwServerExceptions()
{
return (bool) $this->client->getOptions()->exceptions;
}
/**
* Returns the underlying client instance used by the pipeline object.
*
* @return ClientInterface
*/
public function getClient()
{
return $this->client;
}
}
|