Commit 6dc7d316 authored by Jeremy Mikola's avatar Jeremy Mikola

Extract Collection::listIndexes() to an operation class

parent 99650bb3
...@@ -13,7 +13,6 @@ use MongoDB\Driver\WriteConcern; ...@@ -13,7 +13,6 @@ use MongoDB\Driver\WriteConcern;
use MongoDB\Exception\InvalidArgumentException; use MongoDB\Exception\InvalidArgumentException;
use MongoDB\Exception\UnexpectedTypeException; use MongoDB\Exception\UnexpectedTypeException;
use MongoDB\Model\IndexInfoIterator; use MongoDB\Model\IndexInfoIterator;
use MongoDB\Model\IndexInfoIteratorIterator;
use MongoDB\Model\IndexInput; use MongoDB\Model\IndexInput;
use MongoDB\Operation\Aggregate; use MongoDB\Operation\Aggregate;
use MongoDB\Operation\CreateIndexes; use MongoDB\Operation\CreateIndexes;
...@@ -22,6 +21,7 @@ use MongoDB\Operation\Distinct; ...@@ -22,6 +21,7 @@ use MongoDB\Operation\Distinct;
use MongoDB\Operation\FindOneAndDelete; use MongoDB\Operation\FindOneAndDelete;
use MongoDB\Operation\FindOneAndReplace; use MongoDB\Operation\FindOneAndReplace;
use MongoDB\Operation\FindOneAndUpdate; use MongoDB\Operation\FindOneAndUpdate;
use MongoDB\Operation\ListIndexes;
use Traversable; use Traversable;
class Collection class Collection
...@@ -728,18 +728,15 @@ class Collection ...@@ -728,18 +728,15 @@ class Collection
/** /**
* Returns information for all indexes for the collection. * Returns information for all indexes for the collection.
* *
* @see http://docs.mongodb.org/manual/reference/command/listIndexes/ * @see ListIndexes::__construct() for supported options
* @see http://docs.mongodb.org/manual/reference/method/db.collection.getIndexes/
* @return IndexInfoIterator * @return IndexInfoIterator
*/ */
public function listIndexes() public function listIndexes(array $options = array())
{ {
$readPreference = new ReadPreference(ReadPreference::RP_PRIMARY); $operation = new ListIndexes($this->dbname, $this->collname, $options);
$server = $this->manager->selectServer($readPreference); $server = $this->manager->selectServer(new ReadPreference(ReadPreference::RP_PRIMARY));
return (FeatureDetection::isSupported($server, FeatureDetection::API_LISTINDEXES_CMD)) return $operation->execute($server);
? $this->listIndexesCommand($server)
: $this->listIndexesLegacy($server);
} }
/** /**
...@@ -904,37 +901,4 @@ class Collection ...@@ -904,37 +901,4 @@ class Collection
$bulk->update($filter, $update, $options); $bulk->update($filter, $update, $options);
return $this->manager->executeBulkWrite($this->ns, $bulk, $this->wc); return $this->manager->executeBulkWrite($this->ns, $bulk, $this->wc);
} }
/**
* Returns information for all indexes for this collection using the
* listIndexes command.
*
* @see http://docs.mongodb.org/manual/reference/command/listIndexes/
* @param Server $server
* @return IndexInfoIteratorIterator
*/
private function listIndexesCommand(Server $server)
{
$command = new Command(array('listIndexes' => $this->collname));
$cursor = $server->executeCommand($this->dbname, $command);
$cursor->setTypeMap(array('document' => 'array'));
return new IndexInfoIteratorIterator($cursor);
}
/**
* Returns information for all indexes for this collection by querying the
* "system.indexes" collection (MongoDB <2.8).
*
* @param Server $server
* @return IndexInfoIteratorIterator
*/
private function listIndexesLegacy(Server $server)
{
$query = new Query(array('ns' => $this->ns));
$cursor = $server->executeQuery($this->dbname . '.system.indexes', $query);
$cursor->setTypeMap(array('document' => 'array'));
return new IndexInfoIteratorIterator($cursor);
}
} }
<?php
namespace MongoDB\Operation;
use MongoDB\Driver\Command;
use MongoDB\Driver\Query;
use MongoDB\Driver\Server;
use MongoDB\Model\IndexInfoIterator;
use MongoDB\Model\IndexInfoIteratorIterator;
/**
* Operation for the listIndexes command.
*
* @api
* @see MongoDB\Collection::listIndexes()
* @see http://docs.mongodb.org/manual/reference/command/listIndexes/
*/
class ListIndexes implements Executable
{
private static $wireVersionForCommand = 3;
private $databaseName;
private $collectionName;
private $options;
/**
* Constructs a listIndexes command.
*
* Supported options:
*
* * maxTimeMS (integer): The maximum amount of time to allow the query to
* run.
*
* @param string $databaseName Database name
* @param string $collectionName Collection name
* @param array $options Command options
*/
public function __construct($databaseName, $collectionName, array $options = array())
{
if (isset($options['maxTimeMS']) && ! is_integer($options['maxTimeMS'])) {
throw new InvalidArgumentTypeException('"maxTimeMS" option', $options['maxTimeMS'], 'integer');
}
$this->databaseName = (string) $databaseName;
$this->collectionName = (string) $collectionName;
$this->options = $options;
}
/**
* Execute the operation.
*
* @see Executable::execute()
* @param Server $server
* @return IndexInfoIterator
*/
public function execute(Server $server)
{
return \MongoDB\server_supports_feature($server, self::$wireVersionForCommand)
? $this->executeCommand($server)
: $this->executeLegacy($server);
}
/**
* Returns information for all indexes for this collection using the
* listIndexes command.
*
* @param Server $server
* @return IndexInfoIteratorIterator
*/
private function executeCommand(Server $server)
{
$cmd = array('listIndexes' => $this->collectionName);
if (isset($this->options['maxTimeMS'])) {
$cmd['maxTimeMS'] = $this->options['maxTimeMS'];
}
$cursor = $server->executeCommand($this->databaseName, new Command($cmd));
$cursor->setTypeMap(array('document' => 'array'));
return new IndexInfoIteratorIterator($cursor);
}
/**
* Returns information for all indexes for this collection by querying the
* "system.indexes" collection (MongoDB <3.0).
*
* @param Server $server
* @return IndexInfoIteratorIterator
*/
private function executeLegacy(Server $server)
{
$filter = array('ns' => $this->databaseName . '.' . $this->collectionName);
$options = isset($this->options['maxTimeMS'])
? array('modifiers' => array('$maxTimeMS' => $this->options['maxTimeMS']))
: array();
$cursor = $server->executeQuery($this->databaseName . '.system.indexes', new Query($filter, $options));
$cursor->setTypeMap(array('document' => 'array'));
return new IndexInfoIteratorIterator($cursor);
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment