Skip to content

PHPLIB-254 Support maxTimeMS option for CreateIndexes operation #431

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Nov 14, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ interface: phpmethod
operation: ~
optional: true
---
source:
file: apiargs-common-option.yaml
ref: maxTimeMS
---
arg_name: option
name: name
type: string
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
source:
file: apiargs-common-option.yaml
ref: maxTimeMS
---
source:
file: apiargs-MongoDBCollection-common-option.yaml
ref: writeConcern
Expand Down
4 changes: 2 additions & 2 deletions src/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -282,8 +282,8 @@ public function count($filter = [], array $options = [])
*/
public function createIndex($key, array $options = [])
{
$indexOptions = array_diff_key($options, ['writeConcern' => 1]);
$commandOptions = array_intersect_key($options, ['writeConcern' => 1]);
$indexOptions = array_diff_key($options, ['maxTimeMS' => 1, 'writeConcern' => 1]);
$commandOptions = array_intersect_key($options, ['maxTimeMS' => 1, 'writeConcern' => 1]);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In #432 you've also added a test case to test for invalid values for the new option. Can you add one for this new option to provideInvalidConstructorOptions in CreateIndexesTest.php too?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done, thanks!


return current($this->createIndexes([['key' => $key] + $indexOptions], $commandOptions));
}
Expand Down
11 changes: 11 additions & 0 deletions src/Operation/CreateIndexes.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ class CreateIndexes implements Executable
*
* Supported options:
*
* * maxTimeMS (integer): The maximum amount of time to allow the query to
* run.
*
* * writeConcern (MongoDB\Driver\WriteConcern): Write concern.
*
* This is not supported for server versions < 3.4 and will result in an
Expand Down Expand Up @@ -92,6 +95,10 @@ public function __construct($databaseName, $collectionName, array $indexes, arra
$expectedIndex += 1;
}

if (isset($options['maxTimeMS']) && !is_integer($options['maxTimeMS'])) {
throw InvalidArgumentException::invalidType('"maxTimeMS" option', $options['maxTimeMS'], 'integer');
}

if (isset($options['writeConcern']) && ! $options['writeConcern'] instanceof WriteConcern) {
throw InvalidArgumentException::invalidType('"writeConcern" option', $options['writeConcern'], 'MongoDB\Driver\WriteConcern');
}
Expand Down Expand Up @@ -150,6 +157,10 @@ private function executeCommand(Server $server)
'indexes' => $this->indexes,
];

if (isset($this->options['maxTimeMS'])) {
$cmd['maxTimeMS'] = $this->options['maxTimeMS'];
}

if (isset($this->options['writeConcern'])) {
$cmd['writeConcern'] = \MongoDB\write_concern_as_document($this->options['writeConcern']);
}
Expand Down
4 changes: 4 additions & 0 deletions tests/Operation/CreateIndexesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ public function provideInvalidConstructorOptions()
{
$options = [];

foreach ($this->getInvalidIntegerValues() as $value) {
$options[][] = ['maxTimeMS' => $value];
}

foreach ($this->getInvalidWriteConcernValues() as $value) {
$options[][] = ['writeConcern' => $value];
}
Expand Down