Skip to content

Commit 937cce4

Browse files
authored
Merge pull request #415 from mongodb/phplib-268
PHPLIB-268 Add option maxAwaitTimeMS on getMore commands
2 parents d106138 + 3ece749 commit 937cce4

File tree

5 files changed

+62
-1
lines changed

5 files changed

+62
-1
lines changed

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,10 @@ interface: phpmethod
9393
operation: ~
9494
optional: true
9595
---
96+
source:
97+
file: apiargs-common-option.yaml
98+
ref: maxAwaitTimeMS
99+
---
96100
source:
97101
file: apiargs-common-option.yaml
98102
ref: maxTimeMS

docs/includes/apiargs-common-option.yaml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,17 @@ operation: ~
1010
optional: true
1111
---
1212
arg_name: option
13+
name: maxAwaitTimeMS
14+
type: integer
15+
description: |
16+
Positive integer denoting the time limit in milliseconds for the server to block
17+
a getMore operation if no data is available. This option should only be used if
18+
cursorType is TAILABLE_AWAIT.
19+
interface: phpmethod
20+
operation: ~
21+
optional: true
22+
---
23+
arg_name: option
1324
name: readConcern
1425
type: :php:`MongoDB\\Driver\\ReadConcern <class.mongodb-driver-readconcern>`
1526
description: |

src/Operation/Find.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,9 @@ class Find implements Executable
7878
*
7979
* * max (document): The exclusive upper bound for a specific index.
8080
*
81+
* * maxAwaitTimeMS (integer): The maxium amount of time for the server to wait
82+
* on new documents to satisfy a query, if cursorType is TAILABLE_AWAIT.
83+
*
8184
* * maxScan (integer): Maximum number of documents or index keys to scan
8285
* when executing the query.
8386
*
@@ -179,6 +182,10 @@ public function __construct($databaseName, $collectionName, $filter, array $opti
179182
throw InvalidArgumentException::invalidType('"max" option', $options['max'], 'array or object');
180183
}
181184

185+
if (isset($options['maxAwaitTimeMS']) && ! is_integer($options['maxAwaitTimeMS'])) {
186+
throw InvalidArgumentException::invalidType('"maxAwaitTimeMS" option', $options['maxAwaitTimeMS'], 'integer');
187+
}
188+
182189
if (isset($options['maxScan']) && ! is_integer($options['maxScan'])) {
183190
throw InvalidArgumentException::invalidType('"maxScan" option', $options['maxScan'], 'integer');
184191
}
@@ -298,7 +305,7 @@ private function createQuery()
298305
}
299306
}
300307

301-
foreach (['allowPartialResults', 'batchSize', 'comment', 'hint', 'limit', 'maxScan', 'maxTimeMS', 'noCursorTimeout', 'oplogReplay', 'projection', 'readConcern', 'returnKey', 'showRecordId', 'skip', 'snapshot', 'sort'] as $option) {
308+
foreach (['allowPartialResults', 'batchSize', 'comment', 'hint', 'limit', 'maxAwaitTimeMS', 'maxScan', 'maxTimeMS', 'noCursorTimeout', 'oplogReplay', 'projection', 'readConcern', 'returnKey', 'showRecordId', 'skip', 'snapshot', 'sort'] as $option) {
302309
if (isset($this->options[$option])) {
303310
$options[$option] = $this->options[$option];
304311
}

tests/Operation/FindFunctionalTest.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
namespace MongoDB\Tests\Operation;
44

55
use MongoDB\Driver\BulkWrite;
6+
use MongoDB\Operation\CreateCollection;
67
use MongoDB\Operation\CreateIndexes;
8+
use MongoDB\Operation\DropCollection;
79
use MongoDB\Operation\Find;
810
use MongoDB\Tests\CommandObserver;
911
use stdClass;
@@ -123,6 +125,39 @@ public function provideTypeMapOptionsAndExpectedDocuments()
123125
];
124126
}
125127

128+
public function testMaxAwaitTimeMS()
129+
{
130+
$maxAwaitTimeMS = 10;
131+
132+
// Create a capped collection.
133+
$databaseName = $this->getDatabaseName();
134+
$cappedCollectionName = $this->getCollectionName();
135+
$cappedCollectionOptions = [
136+
'capped' => true,
137+
'max' => 100,
138+
'size' => 1048576,
139+
];
140+
141+
$operation = new CreateCollection($databaseName, $cappedCollectionName, $cappedCollectionOptions);
142+
$operation->execute($this->getPrimaryServer());
143+
144+
// Insert documents into the capped collection.
145+
$bulkWrite = new BulkWrite(['ordered' => true]);
146+
$bulkWrite->insert(['_id' => 1]);
147+
$result = $this->manager->executeBulkWrite($this->getNamespace(), $bulkWrite);
148+
149+
$operation = new Find($databaseName, $cappedCollectionName, [], ['cursorType' => Find::TAILABLE_AWAIT, 'maxAwaitTimeMS' => $maxAwaitTimeMS]);
150+
$cursor = $operation->execute($this->getPrimaryServer());
151+
$it = new \IteratorIterator($cursor);
152+
153+
// Make sure we await results for no more than the maxAwaitTimeMS.
154+
$it->rewind();
155+
$it->next();
156+
$startTime = microtime(true);
157+
$it->next();
158+
$this->assertGreaterThanOrEqual($maxAwaitTimeMS * 0.001, microtime(true) - $startTime);
159+
}
160+
126161
/**
127162
* Create data fixtures.
128163
*

tests/Operation/FindTest.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,10 @@ public function provideInvalidConstructorOptions()
6060
$options[][] = ['max' => $value];
6161
}
6262

63+
foreach ($this->getInvalidIntegerVAlues() as $value) {
64+
$options[][] = ['maxAwaitTimeMS' => $value];
65+
}
66+
6367
foreach ($this->getInvalidIntegerValues() as $value) {
6468
$options[][] = ['maxScan' => $value];
6569
}

0 commit comments

Comments
 (0)