|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace MongoDB\Operation; |
| 4 | + |
| 5 | +use MongoDB\FeatureDetection; |
| 6 | +use MongoDB\Driver\Command; |
| 7 | +use MongoDB\Driver\Server; |
| 8 | +use MongoDB\Driver\BulkWrite; |
| 9 | +use MongoDB\Exception\InvalidArgumentException; |
| 10 | +use MongoDB\Exception\RuntimeException; |
| 11 | +use MongoDB\Exception\UnexpectedTypeException; |
| 12 | +use MongoDB\Model\IndexInput; |
| 13 | + |
| 14 | +/** |
| 15 | + * Operation for the createIndexes command. |
| 16 | + * |
| 17 | + * @api |
| 18 | + * @see MongoDB\Collection::createIndex() |
| 19 | + * @see MongoDB\Collection::createIndexes() |
| 20 | + * @see http://docs.mongodb.org/manual/reference/command/createIndexes/ |
| 21 | + */ |
| 22 | +class CreateIndexes implements Executable |
| 23 | +{ |
| 24 | + private static $wireVersionForCommand = 2; |
| 25 | + |
| 26 | + private $databaseName; |
| 27 | + private $collectionName; |
| 28 | + private $indexes = array(); |
| 29 | + |
| 30 | + /** |
| 31 | + * Constructs an aggregate command. |
| 32 | + * |
| 33 | + * Supported options: |
| 34 | + * |
| 35 | + * * allowDiskUse (boolean): Enables writing to temporary files. When set |
| 36 | + * to true, aggregation stages can write data to the _tmp sub-directory |
| 37 | + * in the dbPath directory. The default is false. |
| 38 | + * |
| 39 | + * * batchSize (integer): The number of documents to return per batch. |
| 40 | + * |
| 41 | + * * maxTimeMS (integer): The maximum amount of time to allow the query to |
| 42 | + * run. |
| 43 | + * |
| 44 | + * * useCursor (boolean): Indicates whether the command will request that |
| 45 | + * the server provide results using a cursor. The default is true. |
| 46 | + * |
| 47 | + * For servers < 2.6, this option is ignored as aggregation cursors are |
| 48 | + * not available. |
| 49 | + * |
| 50 | + * For servers >= 2.6, this option allows users to turn off cursors if |
| 51 | + * necessary to aid in mongod/mongos upgrades. |
| 52 | + * |
| 53 | + * @param string $databaseName Database name |
| 54 | + * @param string $collectionName Collection name |
| 55 | + * @param array[] $indexes List of index specifications |
| 56 | + * @throws InvalidArgumentException |
| 57 | + */ |
| 58 | + public function __construct($databaseName, $collectionName, array $indexes) |
| 59 | + { |
| 60 | + if (empty($indexes)) { |
| 61 | + throw new InvalidArgumentException('$indexes is empty'); |
| 62 | + } |
| 63 | + |
| 64 | + foreach ($indexes as $index) { |
| 65 | + if ( ! is_array($index)) { |
| 66 | + throw new UnexpectedTypeException($index, 'array'); |
| 67 | + } |
| 68 | + |
| 69 | + if ( ! isset($index['ns'])) { |
| 70 | + $index['ns'] = $databaseName . '.' . $collectionName; |
| 71 | + } |
| 72 | + |
| 73 | + $this->indexes[] = new IndexInput($index); |
| 74 | + } |
| 75 | + |
| 76 | + $this->databaseName = (string) $databaseName; |
| 77 | + $this->collectionName = (string) $collectionName; |
| 78 | + } |
| 79 | + |
| 80 | + /** |
| 81 | + * Execute the operation. |
| 82 | + * |
| 83 | + * @see Executable::execute() |
| 84 | + * @param Server $server |
| 85 | + * @return string[] The names of the created indexes |
| 86 | + */ |
| 87 | + public function execute(Server $server) |
| 88 | + { |
| 89 | + if (FeatureDetection::isSupported($server, self::$wireVersionForCommand)) { |
| 90 | + $this->executeCommand($server); |
| 91 | + } else { |
| 92 | + $this->executeLegacy($server); |
| 93 | + } |
| 94 | + |
| 95 | + return array_map(function(IndexInput $index) { return (string) $index; }, $this->indexes); |
| 96 | + } |
| 97 | + |
| 98 | + /** |
| 99 | + * Create one or more indexes for the collection using the createIndexes |
| 100 | + * command. |
| 101 | + * |
| 102 | + * @param Server $server |
| 103 | + */ |
| 104 | + private function executeCommand(Server $server) |
| 105 | + { |
| 106 | + $command = new Command(array( |
| 107 | + 'createIndexes' => $this->collectionName, |
| 108 | + 'indexes' => $this->indexes, |
| 109 | + )); |
| 110 | + |
| 111 | + $cursor = $server->executeCommand($this->databaseName, $command); |
| 112 | + $result = current($cursor->toArray()); |
| 113 | + |
| 114 | + if (empty($result['ok'])) { |
| 115 | + throw new RuntimeException(isset($result['errmsg']) ? $result['errmsg'] : 'Unknown error'); |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + /** |
| 120 | + * Create one or more indexes for the collection by inserting into the |
| 121 | + * "system.indexes" collection (MongoDB <2.6). |
| 122 | + * |
| 123 | + * @param Server $server |
| 124 | + * @param IndexInput[] $indexes |
| 125 | + */ |
| 126 | + private function executeLegacy(Server $server, array $indexes) |
| 127 | + { |
| 128 | + $bulk = new BulkWrite(true); |
| 129 | + |
| 130 | + foreach ($this->indexes as $index) { |
| 131 | + $bulk->insert($index); |
| 132 | + } |
| 133 | + |
| 134 | + $server->executeBulkWrite($this->databaseName . '.system.indexes', $bulk); |
| 135 | + } |
| 136 | +} |
0 commit comments