/** * @return array */ protected function getIterationInfo() { $iterationInfo = ['started_iterating' => $this->cursor !== null]; if ($this->cursor !== null) { switch ($this->cursor->getServer()->getType()) { case \MongoDB\Driver\Server::TYPE_RS_ARBITER: $typeString = 'ARBITER'; break; case \MongoDB\Driver\Server::TYPE_MONGOS: $typeString = 'MONGOS'; break; case \MongoDB\Driver\Server::TYPE_RS_PRIMARY: $typeString = 'PRIMARY'; break; case \MongoDB\Driver\Server::TYPE_RS_SECONDARY: $typeString = 'SECONDARY'; break; default: $typeString = 'STANDALONE'; } $cursorId = (string) $this->cursor->getId(); $iterationInfo += ['id' => (int) $cursorId, 'at' => $this->position, 'numReturned' => $this->position, 'server' => sprintf('%s:%d;-;.;%d', $this->cursor->getServer()->getHost(), $this->cursor->getServer()->getPort(), getmypid()), 'host' => $this->cursor->getServer()->getHost(), 'port' => $this->cursor->getServer()->getPort(), 'connection_type_desc' => $typeString]; } return $iterationInfo; }
/** * Actually returns a Traversable object with the DriverCursor within. * If it does not exists yet, create it using the $collection, $command and * $params given. * * @return Traversable */ protected function getCursor() : Traversable { if (!$this->cursor) { $params = $this->getConverter()->toMongoTypes($this->params); $driverCursor = $this->collection->{$this->command}(...$params); $this->cursor = new IteratorIterator($driverCursor); $this->cursor->rewind(); } return $this->cursor; }
/** * @param \MongoDB\Driver\Cursor $cursor Mongo cursor instance to fetch data from. * @param bool $all whether to fetch all rows or only first one. * @return array|bool result. * @see Query::fetchRows() */ protected function fetchRowsInternal($cursor, $all) { $result = []; if ($all) { foreach ($cursor as $row) { $result[] = $row; } } else { if ($row = current($cursor->toArray())) { $result = $row; } else { $result = false; } } return $result; }
/** * Rewind the Iterator to the first element * @link http://php.net/manual/en/iterator.rewind.php * @return void Any returned value is ignored. * @since 5.0.0 */ public function rewind() { $this->cursor->rewind(); }
/** * Fetches rows from the given Mongo cursor. * @param \MongoDB\Driver\Cursor $cursor Mongo cursor instance to fetch data from. * @param boolean $all whether to fetch all rows or only first one. * @param string|callable $indexBy the column name or PHP callback, * by which the query results should be indexed by. * @throws Exception on failure. * @return array|boolean result. */ protected function fetchRows($cursor, $all = true, $indexBy = null) { $token = 'find(' . Json::encode($cursor->getId()) . ')'; Yii::info($token, __METHOD__); try { Yii::beginProfile($token, __METHOD__); $result = $this->fetchRowsInternal($cursor, $all, $indexBy); Yii::endProfile($token, __METHOD__); return $result; } catch (\Exception $e) { Yii::endProfile($token, __METHOD__); throw new Exception($e->getMessage(), (int) $e->getCode(), $e); } }