MongoCommandCursor.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. <?php
  2. /*
  3. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  4. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  5. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  6. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  7. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  8. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  9. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  10. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  11. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  12. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  13. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  14. */
  15. if (class_exists('MongoCommandCursor', false)) {
  16. return;
  17. }
  18. use Alcaeus\MongoDbAdapter\AbstractCursor;
  19. use Alcaeus\MongoDbAdapter\TypeConverter;
  20. class MongoCommandCursor extends AbstractCursor implements MongoCursorInterface
  21. {
  22. /**
  23. * @var array
  24. */
  25. private $command;
  26. /**
  27. * MongoCommandCursor constructor.
  28. * @param MongoClient $connection
  29. * @param string $ns
  30. * @param array $command
  31. */
  32. public function __construct(MongoClient $connection, $ns, array $command = [])
  33. {
  34. parent::__construct($connection, $ns);
  35. $this->command = $command;
  36. }
  37. /**
  38. * @param MongoClient $connection
  39. * @param string $hash
  40. * @param array $document
  41. * @return MongoCommandCursor
  42. */
  43. public static function createFromDocument(MongoClient $connection, $hash, array $document)
  44. {
  45. throw new \Exception('Not implemented');
  46. }
  47. /**
  48. * @return \MongoDB\Driver\Cursor
  49. */
  50. protected function ensureCursor()
  51. {
  52. if ($this->cursor === null) {
  53. $convertedCommand = TypeConverter::fromLegacy($this->command);
  54. if (isset($convertedCommand->cursor)) {
  55. if ($convertedCommand->cursor === true || $convertedCommand->cursor === []) {
  56. $convertedCommand->cursor = new \stdClass();
  57. }
  58. }
  59. $originalReadPreference = null;
  60. if (!$this->supportsReadPreference()) {
  61. $originalReadPreference = $this->readPreference;
  62. $this->setReadPreference(\MongoClient::RP_PRIMARY);
  63. }
  64. try {
  65. $this->cursor = $this->db->command($convertedCommand, $this->getOptions());
  66. } finally {
  67. if ($originalReadPreference) {
  68. $this->readPreference = $originalReadPreference;
  69. }
  70. }
  71. }
  72. return $this->cursor;
  73. }
  74. /**
  75. * @return array
  76. */
  77. protected function getCursorInfo()
  78. {
  79. return [
  80. 'ns' => $this->ns,
  81. 'limit' => 0,
  82. 'batchSize' => $this->batchSize,
  83. 'skip' => 0,
  84. 'flags' => 0,
  85. 'query' => $this->command,
  86. 'fields' => null,
  87. ];
  88. }
  89. /**
  90. * @return array
  91. */
  92. protected function getIterationInfo()
  93. {
  94. $iterationInfo = parent::getIterationInfo();
  95. if ($iterationInfo['started_iterating']) {
  96. $iterationInfo += [
  97. 'firstBatchAt' => $iterationInfo['at'],
  98. 'firstBatchNumReturned' => $iterationInfo['numReturned'],
  99. ];
  100. $iterationInfo['at'] = 0;
  101. $iterationInfo['numReturned'] = 0;
  102. }
  103. return $iterationInfo;
  104. }
  105. /**
  106. * @return array
  107. */
  108. public function __sleep()
  109. {
  110. return ['command'] + parent::__sleep();
  111. }
  112. /**
  113. * @see https://github.com/mongodb/mongo-php-driver-legacy/blob/1.6.14/db.c#L51
  114. * @return bool
  115. */
  116. private function supportsReadPreference()
  117. {
  118. if ($this->command === []) {
  119. return false;
  120. }
  121. $firstKey = array_keys($this->command)[0];
  122. switch ($firstKey) {
  123. case 'count':
  124. case 'group':
  125. case 'dbStats':
  126. case 'geoNear':
  127. case 'geoWalk':
  128. case 'distinct':
  129. case 'aggregate':
  130. case 'collStats':
  131. case 'geoSearch':
  132. case 'parallelCollectionScan':
  133. return true;
  134. case 'mapreduce':
  135. case 'mapReduce':
  136. return (isset($this->command['out']) &&
  137. is_array($this->command['out']) &&
  138. array_key_exists('inline', $this->command['out']));
  139. default:
  140. return false;
  141. }
  142. }
  143. }