MultiExecExecutor.php 4.9 KB

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