Skip to content

Commit 1af82a4

Browse files
authored
PHPLIB-254 Support maxTimeMS option for CreateIndexes operation (#431)
* PHPLIB-254 Support maxTimeMS option for CreateIndexes operation
1 parent dcd6e84 commit 1af82a4

File tree

5 files changed

+25
-2
lines changed

5 files changed

+25
-2
lines changed

docs/includes/apiargs-MongoDBCollection-method-createIndex-option.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,10 @@ interface: phpmethod
4141
operation: ~
4242
optional: true
4343
---
44+
source:
45+
file: apiargs-common-option.yaml
46+
ref: maxTimeMS
47+
---
4448
arg_name: option
4549
name: name
4650
type: string

docs/includes/apiargs-MongoDBCollection-method-createIndexes-option.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
source:
2+
file: apiargs-common-option.yaml
3+
ref: maxTimeMS
4+
---
15
source:
26
file: apiargs-MongoDBCollection-common-option.yaml
37
ref: writeConcern

src/Collection.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -282,8 +282,8 @@ public function count($filter = [], array $options = [])
282282
*/
283283
public function createIndex($key, array $options = [])
284284
{
285-
$indexOptions = array_diff_key($options, ['writeConcern' => 1]);
286-
$commandOptions = array_intersect_key($options, ['writeConcern' => 1]);
285+
$indexOptions = array_diff_key($options, ['maxTimeMS' => 1, 'writeConcern' => 1]);
286+
$commandOptions = array_intersect_key($options, ['maxTimeMS' => 1, 'writeConcern' => 1]);
287287

288288
return current($this->createIndexes([['key' => $key] + $indexOptions], $commandOptions));
289289
}

src/Operation/CreateIndexes.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,9 @@ class CreateIndexes implements Executable
5151
*
5252
* Supported options:
5353
*
54+
* * maxTimeMS (integer): The maximum amount of time to allow the query to
55+
* run.
56+
*
5457
* * writeConcern (MongoDB\Driver\WriteConcern): Write concern.
5558
*
5659
* This is not supported for server versions < 3.4 and will result in an
@@ -92,6 +95,10 @@ public function __construct($databaseName, $collectionName, array $indexes, arra
9295
$expectedIndex += 1;
9396
}
9497

98+
if (isset($options['maxTimeMS']) && !is_integer($options['maxTimeMS'])) {
99+
throw InvalidArgumentException::invalidType('"maxTimeMS" option', $options['maxTimeMS'], 'integer');
100+
}
101+
95102
if (isset($options['writeConcern']) && ! $options['writeConcern'] instanceof WriteConcern) {
96103
throw InvalidArgumentException::invalidType('"writeConcern" option', $options['writeConcern'], 'MongoDB\Driver\WriteConcern');
97104
}
@@ -150,6 +157,10 @@ private function executeCommand(Server $server)
150157
'indexes' => $this->indexes,
151158
];
152159

160+
if (isset($this->options['maxTimeMS'])) {
161+
$cmd['maxTimeMS'] = $this->options['maxTimeMS'];
162+
}
163+
153164
if (isset($this->options['writeConcern'])) {
154165
$cmd['writeConcern'] = \MongoDB\write_concern_as_document($this->options['writeConcern']);
155166
}

tests/Operation/CreateIndexesTest.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@ public function provideInvalidConstructorOptions()
2828
{
2929
$options = [];
3030

31+
foreach ($this->getInvalidIntegerValues() as $value) {
32+
$options[][] = ['maxTimeMS' => $value];
33+
}
34+
3135
foreach ($this->getInvalidWriteConcernValues() as $value) {
3236
$options[][] = ['writeConcern' => $value];
3337
}

0 commit comments

Comments
 (0)