|
| 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