Skip to content
This repository has been archived by the owner on Nov 11, 2020. It is now read-only.

Commit

Permalink
Merge branch 'cursor-maxTimeMS'
Browse files Browse the repository at this point in the history
* cursor-maxTimeMS:
  Add support for maxTimeMS option in cursors
  • Loading branch information
alcaeus committed Jan 6, 2016
2 parents 4d3f855 + e459513 commit 7b4d519
Show file tree
Hide file tree
Showing 8 changed files with 119 additions and 1 deletion.
18 changes: 18 additions & 0 deletions lib/Doctrine/MongoDB/Cursor.php
Expand Up @@ -68,6 +68,7 @@ class Cursor implements CursorInterface
protected $options = array();
protected $batchSize;
protected $limit;
protected $maxTimeMS;
protected $readPreference;
protected $readPreferenceTags;
protected $skip;
Expand Down Expand Up @@ -445,6 +446,20 @@ public function limit($num)
return $this;
}

/**
* Wrapper method for MongoCursor::maxTimeMS().
*
* @see http://php.net/manual/en/mongocursor.maxtimems.php
* @param integer $ms
* @return self
*/
public function maxTimeMS($ms)
{
$this->maxTimeMS = (integer) $ms;
$this->mongoCursor->maxTimeMS($this->maxTimeMS);
return $this;
}

/**
* Wrapper method for MongoCursor::next().
*
Expand Down Expand Up @@ -480,6 +495,9 @@ public function recreate()
if ($this->limit !== null) {
$this->mongoCursor->limit($this->limit);
}
if ($this->maxTimeMS !== null) {
$this->mongoCursor->maxTimeMS($this->maxTimeMS);
}
if ($this->skip !== null) {
$this->mongoCursor->skip($this->skip);
}
Expand Down
14 changes: 14 additions & 0 deletions lib/Doctrine/MongoDB/EagerCursor.php
Expand Up @@ -362,6 +362,20 @@ public function limit($num)
return $this;
}

/**
* {@inheritdoc}
*/
public function maxTimeMS($ms)
{
// Need to use method_exists - adding to CursorInterface is not allowed
// due to SemVer restrictions
if (method_exists($this->cursor, 'maxTimeMS')) {
$this->cursor->maxTimeMS($ms);
}

return $this;
}

/**
* {@inheritDoc}
*/
Expand Down
13 changes: 13 additions & 0 deletions lib/Doctrine/MongoDB/LoggableCursor.php
Expand Up @@ -102,6 +102,19 @@ public function limit($num)
return parent::limit($num);
}

/**
* @see Cursor::maxTimeMS()
*/
public function maxTimeMS($ms)
{
$this->log(array(
'maxTimeMS' => true,
'maxTimeMSNum' => $ms,
));

return parent::maxTimeMS($ms);
}

/**
* @see Cursor::skip()
*/
Expand Down
12 changes: 12 additions & 0 deletions lib/Doctrine/MongoDB/Query/Builder.php
Expand Up @@ -1072,6 +1072,18 @@ public function maxDistance($maxDistance)
return $this;
}

/**
* Specifies a cumulative time limit in milliseconds for processing operations on a cursor.
*
* @param int $ms
* @return $this
*/
public function maxTimeMS($ms)
{
$this->query['maxTimeMS'] = $ms;
return $this;
}

/**
* Updates the value of the field to a specified value if the specified value is less than the current value of the field.
*
Expand Down
2 changes: 1 addition & 1 deletion lib/Doctrine/MongoDB/Query/Query.php
Expand Up @@ -395,7 +395,7 @@ protected function prepareCursor(Cursor $cursor)
$cursor->setReadPreference($this->query['readPreference'], $this->query['readPreferenceTags']);
}

foreach ($this->getQueryOptions('hint', 'immortal', 'limit', 'skip', 'slaveOkay', 'sort') as $key => $value) {
foreach ($this->getQueryOptions('hint', 'immortal', 'limit', 'maxTimeMS', 'skip', 'slaveOkay', 'sort') as $key => $value) {
$cursor->$key($value);
}

Expand Down
34 changes: 34 additions & 0 deletions tests/Doctrine/MongoDB/Tests/CursorTest.php
Expand Up @@ -319,6 +319,40 @@ public function testRecreate()
$cursor->recreate();
}

public function testSetMaxTimeMSWhenRecreateCursor()
{
$self = $this;

$setCursorExpectations = function($mongoCursor) use ($self) {
$mongoCursor->expects($self->once())
->method('maxTimeMS')
->with(30000);
};

$mongoCursor = $this->getMockMongoCursor();
$recreatedMongoCursor = $this->getMockMongoCursor();

$setCursorExpectations($mongoCursor);
$setCursorExpectations($recreatedMongoCursor);

$mongoCollection = $this->getMockCollection();
$mongoCollection->expects($this->once())
->method('find')
->with(array('x' => 9500), array())
->will($this->returnValue($recreatedMongoCursor));

$collection = $this->getMockCollection();
$collection->expects($this->once())
->method('getMongoCollection')
->will($this->returnValue($mongoCollection));

$cursor = $this->getTestCursor($collection, $mongoCursor, array('x' => 9500));

$cursor->maxTimeMS(30000);

$cursor->recreate();
}

private function getMockCollection()
{
return $this->getMockBuilder('Doctrine\MongoDB\Collection')
Expand Down
1 change: 1 addition & 0 deletions tests/Doctrine/MongoDB/Tests/LoggableCursorTest.php
Expand Up @@ -23,6 +23,7 @@ public function provideLoggedMethods()
array('limit'),
array('hint', array()),
array('snapshot'),
array('maxTimeMS', array())
);
}
}
26 changes: 26 additions & 0 deletions tests/Doctrine/MongoDB/Tests/Query/QueryTest.php
Expand Up @@ -286,6 +286,32 @@ public function testUseIdentifierKeys()
$this->assertSame($cursor, $query->execute());
}

public function testSpecifyMaxTimeMSOnCursor()
{
$cursor = $this->getMockCursor();
$collection = $this->getMockCollection();

$collection->expects($this->once())
->method('find')
->with($this->equalTo(array('foo' => 'bar')))
->will($this->returnValue($cursor));

$cursor->expects($this->once())
->method('maxTimeMS')
->with($this->equalTo(30000))
->will($this->returnValue($cursor));

$queryArray = array(
'type' => Query::TYPE_FIND,
'query' => array('foo' => 'bar'),
'maxTimeMS' => 30000
);

$query = new Query($collection, $queryArray, array());

$this->assertSame($cursor, $query->execute());
}

/**
* @return \Doctrine\MongoDB\Collection
*/
Expand Down

0 comments on commit 7b4d519

Please sign in to comment.