Skip to content

Commit 9d7656e

Browse files
committed
Extract Collection::listIndexes() to an operation class
1 parent 2a39fb4 commit 9d7656e

File tree

2 files changed

+110
-42
lines changed

2 files changed

+110
-42
lines changed

src/Collection.php

Lines changed: 6 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
use MongoDB\Exception\InvalidArgumentException;
1414
use MongoDB\Exception\UnexpectedTypeException;
1515
use MongoDB\Model\IndexInfoIterator;
16-
use MongoDB\Model\IndexInfoIteratorIterator;
1716
use MongoDB\Model\IndexInput;
1817
use MongoDB\Operation\Aggregate;
1918
use MongoDB\Operation\CreateIndexes;
@@ -22,6 +21,7 @@
2221
use MongoDB\Operation\FindOneAndDelete;
2322
use MongoDB\Operation\FindOneAndReplace;
2423
use MongoDB\Operation\FindOneAndUpdate;
24+
use MongoDB\Operation\ListIndexes;
2525
use Traversable;
2626

2727
class Collection
@@ -728,18 +728,15 @@ public function insertOne($document)
728728
/**
729729
* Returns information for all indexes for the collection.
730730
*
731-
* @see http://docs.mongodb.org/manual/reference/command/listIndexes/
732-
* @see http://docs.mongodb.org/manual/reference/method/db.collection.getIndexes/
731+
* @see ListIndexes::__construct() for supported options
733732
* @return IndexInfoIterator
734733
*/
735-
public function listIndexes()
734+
public function listIndexes(array $options = array())
736735
{
737-
$readPreference = new ReadPreference(ReadPreference::RP_PRIMARY);
738-
$server = $this->manager->selectServer($readPreference);
736+
$operation = new ListIndexes($this->dbname, $this->collname, $options);
737+
$server = $this->manager->selectServer(new ReadPreference(ReadPreference::RP_PRIMARY));
739738

740-
return (FeatureDetection::isSupported($server, FeatureDetection::API_LISTINDEXES_CMD))
741-
? $this->listIndexesCommand($server)
742-
: $this->listIndexesLegacy($server);
739+
return $operation->execute($server);
743740
}
744741

745742
/**
@@ -904,37 +901,4 @@ protected function _update($filter, $update, $options)
904901
$bulk->update($filter, $update, $options);
905902
return $this->manager->executeBulkWrite($this->ns, $bulk, $this->wc);
906903
}
907-
908-
/**
909-
* Returns information for all indexes for this collection using the
910-
* listIndexes command.
911-
*
912-
* @see http://docs.mongodb.org/manual/reference/command/listIndexes/
913-
* @param Server $server
914-
* @return IndexInfoIteratorIterator
915-
*/
916-
private function listIndexesCommand(Server $server)
917-
{
918-
$command = new Command(array('listIndexes' => $this->collname));
919-
$cursor = $server->executeCommand($this->dbname, $command);
920-
$cursor->setTypeMap(array('document' => 'array'));
921-
922-
return new IndexInfoIteratorIterator($cursor);
923-
}
924-
925-
/**
926-
* Returns information for all indexes for this collection by querying the
927-
* "system.indexes" collection (MongoDB <2.8).
928-
*
929-
* @param Server $server
930-
* @return IndexInfoIteratorIterator
931-
*/
932-
private function listIndexesLegacy(Server $server)
933-
{
934-
$query = new Query(array('ns' => $this->ns));
935-
$cursor = $server->executeQuery($this->dbname . '.system.indexes', $query);
936-
$cursor->setTypeMap(array('document' => 'array'));
937-
938-
return new IndexInfoIteratorIterator($cursor);
939-
}
940904
}

src/Operation/ListIndexes.php

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
<?php
2+
3+
namespace MongoDB\Operation;
4+
5+
use MongoDB\Driver\Command;
6+
use MongoDB\Driver\Query;
7+
use MongoDB\Driver\Server;
8+
use MongoDB\Model\IndexInfoIterator;
9+
use MongoDB\Model\IndexInfoIteratorIterator;
10+
11+
/**
12+
* Operation for the listIndexes command.
13+
*
14+
* @api
15+
* @see MongoDB\Collection::listIndexes()
16+
* @see http://docs.mongodb.org/manual/reference/command/listIndexes/
17+
*/
18+
class ListIndexes implements Executable
19+
{
20+
private static $wireVersionForCommand = 3;
21+
22+
private $databaseName;
23+
private $collectionName;
24+
private $options;
25+
26+
/**
27+
* Constructs a listIndexes command.
28+
*
29+
* Supported options:
30+
*
31+
* * maxTimeMS (integer): The maximum amount of time to allow the query to
32+
* run.
33+
*
34+
* @param string $databaseName Database name
35+
* @param string $collectionName Collection name
36+
* @param array $options Command options
37+
*/
38+
public function __construct($databaseName, $collectionName, array $options = array())
39+
{
40+
if (isset($options['maxTimeMS']) && ! is_integer($options['maxTimeMS'])) {
41+
throw new InvalidArgumentTypeException('"maxTimeMS" option', $options['maxTimeMS'], 'integer');
42+
}
43+
44+
$this->databaseName = (string) $databaseName;
45+
$this->collectionName = (string) $collectionName;
46+
$this->options = $options;
47+
}
48+
49+
/**
50+
* Execute the operation.
51+
*
52+
* @see Executable::execute()
53+
* @param Server $server
54+
* @return IndexInfoIterator
55+
*/
56+
public function execute(Server $server)
57+
{
58+
return \MongoDB\server_supports_feature($server, self::$wireVersionForCommand)
59+
? $this->executeCommand($server)
60+
: $this->executeLegacy($server);
61+
}
62+
63+
/**
64+
* Returns information for all indexes for this collection using the
65+
* listIndexes command.
66+
*
67+
* @param Server $server
68+
* @return IndexInfoIteratorIterator
69+
*/
70+
private function executeCommand(Server $server)
71+
{
72+
$cmd = array('listIndexes' => $this->collectionName);
73+
74+
if (isset($this->options['maxTimeMS'])) {
75+
$cmd['maxTimeMS'] = $this->options['maxTimeMS'];
76+
}
77+
78+
$cursor = $server->executeCommand($this->databaseName, new Command($cmd));
79+
$cursor->setTypeMap(array('document' => 'array'));
80+
81+
return new IndexInfoIteratorIterator($cursor);
82+
}
83+
84+
/**
85+
* Returns information for all indexes for this collection by querying the
86+
* "system.indexes" collection (MongoDB <3.0).
87+
*
88+
* @param Server $server
89+
* @return IndexInfoIteratorIterator
90+
*/
91+
private function executeLegacy(Server $server)
92+
{
93+
$filter = array('ns' => $this->databaseName . '.' . $this->collectionName);
94+
95+
$options = isset($this->options['maxTimeMS'])
96+
? array('modifiers' => array('$maxTimeMS' => $this->options['maxTimeMS']))
97+
: array();
98+
99+
$cursor = $server->executeQuery($this->databaseName . '.system.indexes', new Query($filter, $options));
100+
$cursor->setTypeMap(array('document' => 'array'));
101+
102+
return new IndexInfoIteratorIterator($cursor);
103+
}
104+
}

0 commit comments

Comments
 (0)