MultiExecExecutor.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. <?php
  2. /*
  3. * This file is part of the Predis package.
  4. *
  5. * (c) Daniele Alessandri <suppakilla@gmail.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Predis\Pipeline;
  11. use Iterator;
  12. use SplQueue;
  13. use Predis\ClientException;
  14. use Predis\ResponseErrorInterface;
  15. use Predis\ResponseObjectInterface;
  16. use Predis\ServerException;
  17. use Predis\Connection\ConnectionInterface;
  18. use Predis\Connection\SingleConnectionInterface;
  19. use Predis\Profile\ServerProfile;
  20. use Predis\Profile\ServerProfileInterface;
  21. /**
  22. * Implements a pipeline executor that wraps the whole pipeline
  23. * in a MULTI / EXEC context to make sure that it is executed
  24. * correctly.
  25. *
  26. * @author Daniele Alessandri <suppakilla@gmail.com>
  27. */
  28. class MultiExecExecutor implements PipelineExecutorInterface
  29. {
  30. protected $profile;
  31. /**
  32. *
  33. */
  34. public function __construct(ServerProfileInterface $profile = null)
  35. {
  36. $this->setProfile($profile ?: ServerProfile::getDefault());
  37. }
  38. /**
  39. * Allows the pipeline executor to perform operations on the
  40. * connection before starting to execute the commands stored
  41. * in the pipeline.
  42. *
  43. * @param ConnectionInterface $connection Connection instance.
  44. */
  45. protected function checkConnection(ConnectionInterface $connection)
  46. {
  47. if (!$connection instanceof SingleConnectionInterface) {
  48. $class = __CLASS__;
  49. throw new ClientException("$class can be used only with single connections");
  50. }
  51. }
  52. /**
  53. * {@inheritdoc}
  54. */
  55. public function execute(ConnectionInterface $connection, SplQueue $commands)
  56. {
  57. $this->checkConnection($connection);
  58. $cmd = $this->profile->createCommand('multi');
  59. $connection->executeCommand($cmd);
  60. foreach ($commands as $command) {
  61. $connection->writeCommand($command);
  62. }
  63. foreach ($commands as $command) {
  64. $response = $connection->readResponse($command);
  65. if ($response instanceof ResponseErrorInterface) {
  66. $cmd = $this->profile->createCommand('discard');
  67. $connection->executeCommand($cmd);
  68. throw new ServerException($response->getMessage());
  69. }
  70. }
  71. $cmd = $this->profile->createCommand('exec');
  72. $responses = $connection->executeCommand($cmd);
  73. if (!isset($responses)) {
  74. throw new ClientException('The underlying transaction has been aborted by the server');
  75. }
  76. if (count($responses) !== count($commands)) {
  77. throw new ClientException("Invalid number of replies [expected: ".count($commands)." - actual: ".count($responses)."]");
  78. }
  79. $consumer = $responses instanceof Iterator ? 'consumeIteratorResponse' : 'consumeArrayResponse';
  80. return $this->$consumer($commands, $responses);
  81. }
  82. /**
  83. * Consumes an iterator response returned by EXEC.
  84. *
  85. * @param SplQueue $commands Pipelined commands
  86. * @param Iterator $responses Responses returned by EXEC.
  87. * @return array
  88. */
  89. protected function consumeIteratorResponse(SplQueue $commands, Iterator $responses)
  90. {
  91. $values = array();
  92. foreach ($responses as $response) {
  93. $command = $commands->dequeue();
  94. if ($response instanceof ResponseObjectInterface) {
  95. if ($response instanceof Iterator) {
  96. $response = iterator_to_array($response);
  97. $values[] = $command->parseResponse($response);
  98. } else {
  99. $values[] = $response;
  100. }
  101. } else {
  102. $values[] = $command->parseResponse($response);
  103. }
  104. }
  105. return $values;
  106. }
  107. /**
  108. * Consumes an array response returned by EXEC.
  109. *
  110. * @param SplQueue $commands Pipelined commands
  111. * @param Array $responses Responses returned by EXEC.
  112. * @return array
  113. */
  114. protected function consumeArrayResponse(SplQueue $commands, Array &$responses)
  115. {
  116. $size = count($commands);
  117. $values = array();
  118. for ($i = 0; $i < $size; $i++) {
  119. $command = $commands->dequeue();
  120. $response = $responses[$i];
  121. if ($response instanceof ResponseObjectInterface) {
  122. $values[$i] = $response;
  123. } else {
  124. $values[$i] = $command->parseResponse($response);
  125. }
  126. unset($responses[$i]);
  127. }
  128. return $values;
  129. }
  130. /**
  131. * @param ServerProfileInterface $profile Server profile.
  132. */
  133. public function setProfile(ServerProfileInterface $profile)
  134. {
  135. if (!$profile->supportsCommands(array('multi', 'exec', 'discard'))) {
  136. throw new ClientException('The specified server profile must support MULTI, EXEC and DISCARD.');
  137. }
  138. $this->profile = $profile;
  139. }
  140. }