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

Commit

Permalink
Merge pull request #1620 from alexispeter/mongodb-cursor-batchSize
Browse files Browse the repository at this point in the history
mongodb increase batchSize to an arbitrary, high number
  • Loading branch information
alexispeter committed Feb 2, 2015
2 parents ee807b2 + 0f21202 commit 8a6d698
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 2 deletions.
15 changes: 13 additions & 2 deletions library/CM/MongoDb/Client.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

class CM_MongoDb_Client {
class CM_MongoDb_Client extends CM_Class_Abstract {

/** @var CM_MongoDb_Client|null $_client */
private $_client = null;
Expand Down Expand Up @@ -130,6 +130,10 @@ public function findOne($collection, array $criteria = null, array $projection =
* @return Iterator
*/
public function find($collection, array $criteria = null, array $projection = null, array $aggregation = null) {
$batchSize = null;
if (isset(self::_getConfig()->batchSize)) {
$batchSize = (int) self::_getConfig()->batchSize;
}
$criteria = (array) $criteria;
$projection = (array) $projection;
CM_Debug::getInstance()->incStats('mongo', "find `{$collection}`: " . CM_Params::jsonEncode(['projection' => $projection,
Expand All @@ -144,10 +148,17 @@ public function find($collection, array $criteria = null, array $projection = nu
if ($criteria) {
array_unshift($pipeline, ['$match' => $criteria]);
}
$resultCursor = $collection->aggregateCursor($pipeline);
$options = [];
if (null !== $batchSize) {
$options['cursor'] = ['batchSize' => $batchSize];
}
$resultCursor = $collection->aggregateCursor($pipeline, $options);
} else {
$resultCursor = $collection->find($criteria, $projection);
}
if (null !== $batchSize) {
$resultCursor->batchSize($batchSize);
}
return $resultCursor;
}

Expand Down
2 changes: 2 additions & 0 deletions resources/config/default.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@

$config->CM_Db_Db->delayedEnabled = true;

$config->CM_MongoDb_Client->batchSize = null;

$config->CM_Model_User->class = 'CM_Model_User';

$config->CM_Params->class = 'CM_Params';
Expand Down
20 changes: 20 additions & 0 deletions tests/library/CM/MongoDb/ClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,26 @@ public function testFind() {
$this->assertEquals([['foo' => 1], ['foo' => 1], ['foo' => 2], ['foo' => 3]], $actual);
}

public function testFindBatchSize() {
$mongoDb = CM_Service_Manager::getInstance()->getMongoDb();
$collectionName = 'findBatchSize';
CM_Config::get()->CM_MongoDb_Client->batchSize = null;

$cursor = $mongoDb->find($collectionName);
$this->assertSame(0, $cursor->info()['batchSize']);
$cursor = $mongoDb->find($collectionName, null, null, ['$match' => ['foo' => 'bar']]);
$this->assertSame(0, $cursor->info()['batchSize']);
$this->assertSame(101, $cursor->info()['query']['cursor']['batchSize']);

CM_Config::get()->CM_MongoDb_Client->batchSize = 10;

$cursor = $mongoDb->find($collectionName);
$this->assertSame(10, $cursor->info()['batchSize']);
$cursor = $mongoDb->find($collectionName, null, null, ['$match' => ['foo' => 'bar']]);
$this->assertSame(10, $cursor->info()['batchSize']);
$this->assertSame(10, $cursor->info()['query']['cursor']['batchSize']);
}

public function testFindAndModify() {
$mongoDb = CM_Service_Manager::getInstance()->getMongoDb();
$collectionName = 'findAndModify';
Expand Down

0 comments on commit 8a6d698

Please sign in to comment.