Skip to content

Commit 876492a

Browse files
committed
Merge pull request #750
2 parents 3fa8991 + 2cdc10f commit 876492a

File tree

5 files changed

+181
-1
lines changed

5 files changed

+181
-1
lines changed

php_phongo.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -903,10 +903,14 @@ int phongo_execute_command(mongoc_client_t *client, php_phongo_command_type_t ty
903903

904904
if (command->max_await_time_ms) {
905905
bson_append_bool(&initial_reply, "awaitData", -1, 1);
906-
bson_append_int32(&initial_reply, "maxAwaitTimeMS", -1, command->max_await_time_ms);
906+
bson_append_int64(&initial_reply, "maxAwaitTimeMS", -1, command->max_await_time_ms);
907907
bson_append_bool(&initial_reply, "tailable", -1, 1);
908908
}
909909

910+
if (command->batch_size) {
911+
bson_append_int64(&initial_reply, "batchSize", -1, command->batch_size);
912+
}
913+
910914
cmd_cursor = mongoc_cursor_new_from_command_reply(client, &initial_reply, server_id);
911915
bson_destroy(&reply);
912916
} else {

php_phongo_structs.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ typedef struct {
4747
PHONGO_ZEND_OBJECT_PRE
4848
bson_t *bson;
4949
uint32_t max_await_time_ms;
50+
uint32_t batch_size;
5051
PHONGO_ZEND_OBJECT_POST
5152
} php_phongo_command_t;
5253

src/MongoDB/Command.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,9 @@ static bool php_phongo_command_init_max_await_time_ms(php_phongo_command_t *inte
5959
* (where applicable). */
6060
static bool php_phongo_command_init(php_phongo_command_t *intern, zval *filter, zval *options TSRMLS_DC) /* {{{ */
6161
{
62+
bson_iter_t iter;
63+
bson_iter_t sub_iter;
64+
6265
intern->bson = bson_new();
6366

6467
php_phongo_zval_to_bson(filter, PHONGO_BSON_NONE, intern->bson, NULL TSRMLS_CC);
@@ -69,6 +72,14 @@ static bool php_phongo_command_init(php_phongo_command_t *intern, zval *filter,
6972
return false;
7073
}
7174

75+
if (bson_iter_init(&iter, intern->bson) && bson_iter_find_descendant(&iter, "cursor.batchSize", &sub_iter) && BSON_ITER_HOLDS_INT(&sub_iter)) {
76+
int64_t batch_size = bson_iter_as_int64(&sub_iter);
77+
78+
if (batch_size >= 0 && batch_size <= UINT32_MAX) {
79+
intern->batch_size = (uint32_t) batch_size;
80+
}
81+
}
82+
7283
if (!options) {
7384
return true;
7485
}
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
--TEST--
2+
MongoDB\Driver\Command non-zero batchSize applies to getMore
3+
--SKIPIF--
4+
<?php require __DIR__ . "/../utils/basic-skipif.inc"; ?>
5+
<?php NEEDS('STANDALONE'); CLEANUP(STANDALONE); ?>
6+
--FILE--
7+
<?php
8+
require_once __DIR__ . "/../utils/basic.inc";
9+
10+
class Test implements MongoDB\Driver\Monitoring\CommandSubscriber
11+
{
12+
public function executeCommand()
13+
{
14+
MongoDB\Driver\Monitoring\addSubscriber($this);
15+
16+
$manager = new MongoDB\Driver\Manager(STANDALONE);
17+
18+
$bulkWrite = new MongoDB\Driver\BulkWrite;
19+
20+
for ($i = 0; $i < 5; $i++) {
21+
$bulkWrite->insert(['_id' => $i]);
22+
}
23+
24+
$writeResult = $manager->executeBulkWrite(NS, $bulkWrite);
25+
printf("Inserted: %d\n", $writeResult->getInsertedCount());
26+
27+
$command = new MongoDB\Driver\Command([
28+
'aggregate' => COLLECTION_NAME,
29+
'pipeline' => [['$match' => new stdClass]],
30+
'cursor' => ['batchSize' => 2]
31+
]);
32+
$cursor = $manager->executeCommand(DATABASE_NAME, $command);
33+
34+
$cursor->toArray();
35+
36+
MongoDB\Driver\Monitoring\removeSubscriber($this);
37+
}
38+
39+
public function commandStarted(MongoDB\Driver\Monitoring\CommandStartedEvent $event)
40+
{
41+
$command = $event->getCommand();
42+
43+
if ($event->getCommandName() === 'aggregate') {
44+
printf("aggregate command specifies batchSize: %d\n", $command->cursor->batchSize);
45+
}
46+
47+
if ($event->getCommandName() === 'getMore') {
48+
printf("getMore command specifies batchSize: %d\n", $command->batchSize);
49+
}
50+
}
51+
52+
public function commandSucceeded(MongoDB\Driver\Monitoring\CommandSucceededEvent $event)
53+
{
54+
$reply = $event->getReply();
55+
56+
if ($event->getCommandName() === 'aggregate') {
57+
printf("aggregate response contains %d document(s)\n", count($reply->cursor->firstBatch));
58+
}
59+
60+
if ($event->getCommandName() === 'getMore') {
61+
printf("getMore response contains %d document(s)\n", count($reply->cursor->nextBatch));
62+
}
63+
}
64+
65+
public function commandFailed(MongoDB\Driver\Monitoring\CommandFailedEvent $event)
66+
{
67+
}
68+
}
69+
70+
(new Test)->executeCommand();
71+
72+
?>
73+
===DONE===
74+
<?php exit(0); ?>
75+
--EXPECT--
76+
Inserted: 5
77+
aggregate command specifies batchSize: 2
78+
aggregate response contains 2 document(s)
79+
getMore command specifies batchSize: 2
80+
getMore response contains 2 document(s)
81+
getMore command specifies batchSize: 2
82+
getMore response contains 1 document(s)
83+
===DONE===
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
--TEST--
2+
MongoDB\Driver\Command batchSize of zero is ignored for getMore
3+
--SKIPIF--
4+
<?php require __DIR__ . "/../utils/basic-skipif.inc"; ?>
5+
<?php NEEDS('STANDALONE'); CLEANUP(STANDALONE); ?>
6+
--FILE--
7+
<?php
8+
require_once __DIR__ . "/../utils/basic.inc";
9+
10+
class Test implements MongoDB\Driver\Monitoring\CommandSubscriber
11+
{
12+
public function executeCommand()
13+
{
14+
MongoDB\Driver\Monitoring\addSubscriber($this);
15+
16+
$manager = new MongoDB\Driver\Manager(STANDALONE);
17+
18+
$bulkWrite = new MongoDB\Driver\BulkWrite;
19+
20+
for ($i = 0; $i < 5; $i++) {
21+
$bulkWrite->insert(['_id' => $i]);
22+
}
23+
24+
$writeResult = $manager->executeBulkWrite(NS, $bulkWrite);
25+
printf("Inserted: %d\n", $writeResult->getInsertedCount());
26+
27+
$command = new MongoDB\Driver\Command([
28+
'aggregate' => COLLECTION_NAME,
29+
'pipeline' => [['$match' => new stdClass]],
30+
'cursor' => ['batchSize' => 0]
31+
]);
32+
$cursor = $manager->executeCommand(DATABASE_NAME, $command);
33+
34+
$cursor->toArray();
35+
36+
MongoDB\Driver\Monitoring\removeSubscriber($this);
37+
}
38+
39+
public function commandStarted(MongoDB\Driver\Monitoring\CommandStartedEvent $event)
40+
{
41+
$command = $event->getCommand();
42+
43+
if ($event->getCommandName() === 'aggregate') {
44+
printf("aggregate command specifies batchSize: %d\n", $command->cursor->batchSize);
45+
}
46+
47+
if ($event->getCommandName() === 'getMore') {
48+
printf("getMore command specifies batchSize: %s\n", isset($command->batchSize) ? 'yes' : 'no');
49+
}
50+
}
51+
52+
public function commandSucceeded(MongoDB\Driver\Monitoring\CommandSucceededEvent $event)
53+
{
54+
$reply = $event->getReply();
55+
56+
if ($event->getCommandName() === 'aggregate') {
57+
printf("aggregate response contains %d document(s)\n", count($reply->cursor->firstBatch));
58+
}
59+
60+
if ($event->getCommandName() === 'getMore') {
61+
printf("getMore response contains %d document(s)\n", count($reply->cursor->nextBatch));
62+
}
63+
}
64+
65+
public function commandFailed(MongoDB\Driver\Monitoring\CommandFailedEvent $event)
66+
{
67+
}
68+
}
69+
70+
(new Test)->executeCommand();
71+
72+
?>
73+
===DONE===
74+
<?php exit(0); ?>
75+
--EXPECT--
76+
Inserted: 5
77+
aggregate command specifies batchSize: 0
78+
aggregate response contains 0 document(s)
79+
getMore command specifies batchSize: no
80+
getMore response contains 5 document(s)
81+
===DONE===

0 commit comments

Comments
 (0)