Skip to content

Commit 6c5e826

Browse files
committed
Rename BulkWriteCommandBuilder to ClientBulkWrite
Also renames the operation class to ClientBulkWriteCommand to avoid aliasing in Client.
1 parent e6d5397 commit 6c5e826

12 files changed

+130
-26
lines changed

src/Client.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
use MongoDB\Model\BSONArray;
4242
use MongoDB\Model\BSONDocument;
4343
use MongoDB\Model\DatabaseInfo;
44-
use MongoDB\Operation\ClientBulkWrite;
44+
use MongoDB\Operation\ClientBulkWriteCommand;
4545
use MongoDB\Operation\DropDatabase;
4646
use MongoDB\Operation\ListDatabaseNames;
4747
use MongoDB\Operation\ListDatabases;
@@ -193,26 +193,26 @@ final public function addSubscriber(Subscriber $subscriber): void
193193
}
194194

195195
/**
196-
* Executes multiple write operations.
196+
* Executes multiple write operations across multiple namespaces.
197197
*
198-
* @see ClientBulkWrite::__construct() for supported options
199-
* @param string $databaseName Database name
200-
* @param array $options Additional options
198+
* @param BulkWriteCommand|ClientBulkWrite $bulk Assembled bulk write command or builder
199+
* @param array $options Additional options
201200
* @throws UnsupportedException if options are unsupported on the selected server
202201
* @throws InvalidArgumentException for parameter/option parsing errors
203202
* @throws DriverRuntimeException for other driver errors (e.g. connection errors)
203+
* @see ClientBulkWriteCommand::__construct() for supported options
204204
*/
205-
public function bulkWrite(BulkWriteCommand|BulkWriteCommandBuilder $bulk, array $options = []): ?BulkWriteCommandResult
205+
public function bulkWrite(BulkWriteCommand|ClientBulkWrite $bulk, array $options = []): ?BulkWriteCommandResult
206206
{
207207
if (! isset($options['writeConcern']) && ! is_in_transaction($options)) {
208208
$options['writeConcern'] = $this->writeConcern;
209209
}
210210

211-
if ($bulk instanceof BulkWriteCommandBuilder) {
211+
if ($bulk instanceof ClientBulkWrite) {
212212
$bulk = $bulk->bulkWriteCommand;
213213
}
214214

215-
$operation = new ClientBulkWrite($bulk, $options);
215+
$operation = new ClientBulkWriteCommand($bulk, $options);
216216
$server = select_server_for_write($this->manager, $options);
217217

218218
return $operation->execute($server);

src/BulkWriteCommandBuilder.php renamed to src/ClientBulkWrite.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
use function is_bool;
2828
use function is_string;
2929

30-
final readonly class BulkWriteCommandBuilder
30+
final readonly class ClientBulkWrite
3131
{
3232
private function __construct(
3333
public BulkWriteCommand $bulkWriteCommand,

src/Operation/ClientBulkWrite.php renamed to src/Operation/ClientBulkWriteCommand.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
*
3535
* @see \MongoDB\Client::bulkWrite()
3636
*/
37-
final class ClientBulkWrite
37+
final class ClientBulkWriteCommand
3838
{
3939
/**
4040
* Constructs a client-level bulk write operation.
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
<?php
2+
3+
namespace MongoDB\Tests\SpecTests\Crud;
4+
5+
use MongoDB\ClientBulkWrite;
6+
use MongoDB\Driver\Exception\BulkWriteCommandException;
7+
use MongoDB\Driver\Monitoring\CommandFailedEvent;
8+
use MongoDB\Driver\Monitoring\CommandStartedEvent;
9+
use MongoDB\Driver\Monitoring\CommandSubscriber;
10+
use MongoDB\Driver\Monitoring\CommandSucceededEvent;
11+
use MongoDB\Tests\SpecTests\FunctionalTestCase;
12+
13+
use function str_repeat;
14+
15+
/**
16+
* Prose test 11: MongoClient.bulkWrite batch splits when the addition of a new namespace exceeds the maximum message size
17+
*
18+
* @see https://github.com/mongodb/specifications/tree/master/source/crud/tests#11-mongoclientbulkwrite-batch-splits-when-the-addition-of-a-new-namespace-exceeds-the-maximum-message-size
19+
*/
20+
class Prose11_BulkWriteBatchSplitsWhenNamespaceExceedsMessageSizeTest extends FunctionalTestCase
21+
{
22+
private const UNKNOWN_ERROR = 8;
23+
24+
public function testBatchSplit(): void
25+
{
26+
if ($this->isServerless()) {
27+
$this->markTestSkipped('bulkWrite command is not supported');
28+
}
29+
30+
$this->skipIfServerVersion('<', '8.0', 'bulkWrite command is not supported');
31+
32+
$client = self::createTestClient();
33+
34+
$maxBsonObjectSize = $this->getPrimaryServer()->getInfo()['maxBsonObjectSize'] ?? null;
35+
self::assertIsInt($maxBsonObjectSize);
36+
37+
$this->configureFailPoint([
38+
'configureFailPoint' => 'failCommand',
39+
'mode' => ['times' => 1],
40+
'data' => [
41+
'failCommands' => ['getMore'],
42+
'errorCode' => self::UNKNOWN_ERROR,
43+
],
44+
]);
45+
46+
$collection = $client->selectCollection($this->getDatabaseName(), $this->getCollectionName());
47+
$collection->drop();
48+
49+
$bulkWrite = ClientBulkWrite::createWithCollection($collection, ['verboseResults' => true]);
50+
$bulkWrite->updateOne(
51+
['_id' => str_repeat('a', (int) ($maxBsonObjectSize / 2))],
52+
['$set' => ['x' => 1]],
53+
['upsert' => true],
54+
);
55+
$bulkWrite->updateOne(
56+
['_id' => str_repeat('b', (int) ($maxBsonObjectSize / 2))],
57+
['$set' => ['x' => 1]],
58+
['upsert' => true],
59+
);
60+
61+
$subscriber = new class implements CommandSubscriber {
62+
public int $numGetMoreObserved = 0;
63+
public int $numKillCursorsObserved = 0;
64+
65+
public function commandStarted(CommandStartedEvent $event): void
66+
{
67+
if ($event->getCommandName() === 'getMore') {
68+
++$this->numGetMoreObserved;
69+
} elseif ($event->getCommandName() === 'killCursors') {
70+
++$this->numKillCursorsObserved;
71+
}
72+
}
73+
74+
public function commandSucceeded(CommandSucceededEvent $event): void
75+
{
76+
}
77+
78+
public function commandFailed(CommandFailedEvent $event): void
79+
{
80+
}
81+
};
82+
83+
$client->addSubscriber($subscriber);
84+
85+
try {
86+
$client->bulkWrite($bulkWrite);
87+
self::fail('BulkWriteCommandException was not thrown');
88+
} catch (BulkWriteCommandException $e) {
89+
$errorReply = $e->getErrorReply();
90+
$this->assertNotNull($errorReply);
91+
$this->assertSame(self::UNKNOWN_ERROR, $errorReply['code'] ?? null);
92+
93+
// PHPC will also apply the top-level error code to BulkWriteCommandException
94+
$this->assertSame(self::UNKNOWN_ERROR, $e->getCode());
95+
96+
$partialResult = $e->getPartialResult();
97+
self::assertNotNull($partialResult);
98+
self::assertSame(2, $partialResult->getUpsertedCount());
99+
self::assertCount(1, $partialResult->getUpdateResults());
100+
self::assertSame(1, $subscriber->numGetMoreObserved);
101+
self::assertSame(1, $subscriber->numKillCursorsObserved);
102+
}
103+
}
104+
}

tests/SpecTests/Crud/Prose3_BulkWriteSplitsOnMaxWriteBatchSizeTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace MongoDB\Tests\SpecTests\Crud;
44

5-
use MongoDB\BulkWriteCommandBuilder;
5+
use MongoDB\ClientBulkWrite;
66
use MongoDB\Driver\Monitoring\CommandFailedEvent;
77
use MongoDB\Driver\Monitoring\CommandStartedEvent;
88
use MongoDB\Driver\Monitoring\CommandSubscriber;
@@ -30,7 +30,7 @@ public function testSplitOnMaxWriteBatchSize(): void
3030
self::assertIsInt($maxWriteBatchSize);
3131

3232
$collection = $client->selectCollection($this->getDatabaseName(), $this->getCollectionName());
33-
$bulkWrite = BulkWriteCommandBuilder::createWithCollection($collection);
33+
$bulkWrite = ClientBulkWrite::createWithCollection($collection);
3434

3535
for ($i = 0; $i < $maxWriteBatchSize + 1; ++$i) {
3636
$bulkWrite->insertOne(['a' => 'b']);

tests/SpecTests/Crud/Prose4_BulkWriteSplitsOnMaxMessageSizeBytesTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace MongoDB\Tests\SpecTests\Crud;
44

5-
use MongoDB\BulkWriteCommandBuilder;
5+
use MongoDB\ClientBulkWrite;
66
use MongoDB\Driver\Monitoring\CommandFailedEvent;
77
use MongoDB\Driver\Monitoring\CommandStartedEvent;
88
use MongoDB\Driver\Monitoring\CommandSubscriber;
@@ -36,7 +36,7 @@ public function testSplitOnMaxWriteBatchSize(): void
3636
$document = ['a' => str_repeat('b', $maxBsonObjectSize - 500)];
3737

3838
$collection = $client->selectCollection($this->getDatabaseName(), $this->getCollectionName());
39-
$bulkWrite = BulkWriteCommandBuilder::createWithCollection($collection);
39+
$bulkWrite = ClientBulkWrite::createWithCollection($collection);
4040

4141
for ($i = 0; $i < $numModels; ++$i) {
4242
$bulkWrite->insertOne($document);

tests/SpecTests/Crud/Prose5_BulkWriteCollectsWriteConcernErrorsAcrossBatchesTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace MongoDB\Tests\SpecTests\Crud;
44

5-
use MongoDB\BulkWriteCommandBuilder;
5+
use MongoDB\ClientBulkWrite;
66
use MongoDB\Driver\Exception\BulkWriteCommandException;
77
use MongoDB\Driver\Monitoring\CommandFailedEvent;
88
use MongoDB\Driver\Monitoring\CommandStartedEvent;
@@ -43,7 +43,7 @@ public function testCollectWriteConcernErrors(): void
4343
]);
4444

4545
$collection = $client->selectCollection($this->getDatabaseName(), $this->getCollectionName());
46-
$bulkWrite = BulkWriteCommandBuilder::createWithCollection($collection);
46+
$bulkWrite = ClientBulkWrite::createWithCollection($collection);
4747

4848
for ($i = 0; $i < $maxWriteBatchSize + 1; ++$i) {
4949
$bulkWrite->insertOne(['a' => 'b']);

tests/SpecTests/Crud/Prose6_BulkWriteHandlesWriteErrorsAcrossBatchesTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace MongoDB\Tests\SpecTests\Crud;
44

5-
use MongoDB\BulkWriteCommandBuilder;
5+
use MongoDB\ClientBulkWrite;
66
use MongoDB\Driver\Exception\BulkWriteCommandException;
77
use MongoDB\Driver\Monitoring\CommandFailedEvent;
88
use MongoDB\Driver\Monitoring\CommandStartedEvent;
@@ -39,7 +39,7 @@ public function testOrdered(): void
3939
$collection->drop();
4040
$collection->insertOne(['_id' => 1]);
4141

42-
$bulkWrite = BulkWriteCommandBuilder::createWithCollection($collection, ['ordered' => true]);
42+
$bulkWrite = ClientBulkWrite::createWithCollection($collection, ['ordered' => true]);
4343

4444
for ($i = 0; $i < $maxWriteBatchSize + 1; ++$i) {
4545
$bulkWrite->insertOne(['_id' => 1]);
@@ -68,7 +68,7 @@ public function testUnordered(): void
6868
$collection->drop();
6969
$collection->insertOne(['_id' => 1]);
7070

71-
$bulkWrite = BulkWriteCommandBuilder::createWithCollection($collection, ['ordered' => false]);
71+
$bulkWrite = ClientBulkWrite::createWithCollection($collection, ['ordered' => false]);
7272

7373
for ($i = 0; $i < $maxWriteBatchSize + 1; ++$i) {
7474
$bulkWrite->insertOne(['_id' => 1]);

tests/SpecTests/Crud/Prose7_BulkWriteHandlesCursorRequiringGetMoreTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace MongoDB\Tests\SpecTests\Crud;
44

5-
use MongoDB\BulkWriteCommandBuilder;
5+
use MongoDB\ClientBulkWrite;
66
use MongoDB\Driver\Monitoring\CommandFailedEvent;
77
use MongoDB\Driver\Monitoring\CommandStartedEvent;
88
use MongoDB\Driver\Monitoring\CommandSubscriber;
@@ -34,7 +34,7 @@ public function testHandlesCursor(): void
3434
$collection = $client->selectCollection($this->getDatabaseName(), $this->getCollectionName());
3535
$collection->drop();
3636

37-
$bulkWrite = BulkWriteCommandBuilder::createWithCollection($collection, ['verboseResults' => true]);
37+
$bulkWrite = ClientBulkWrite::createWithCollection($collection, ['verboseResults' => true]);
3838
$bulkWrite->updateOne(
3939
['_id' => str_repeat('a', (int) ($maxBsonObjectSize / 2))],
4040
['$set' => ['x' => 1]],

tests/SpecTests/Crud/Prose8_BulkWriteHandlesCursorRequiringGetMoreWithinTransactionTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace MongoDB\Tests\SpecTests\Crud;
44

5-
use MongoDB\BulkWriteCommandBuilder;
5+
use MongoDB\ClientBulkWrite;
66
use MongoDB\Driver\Monitoring\CommandFailedEvent;
77
use MongoDB\Driver\Monitoring\CommandStartedEvent;
88
use MongoDB\Driver\Monitoring\CommandSubscriber;
@@ -35,7 +35,7 @@ public function testHandlesCursorWithinTransaction(): void
3535
$collection = $client->selectCollection($this->getDatabaseName(), $this->getCollectionName());
3636
$collection->drop();
3737

38-
$bulkWrite = BulkWriteCommandBuilder::createWithCollection($collection, ['verboseResults' => true]);
38+
$bulkWrite = ClientBulkWrite::createWithCollection($collection, ['verboseResults' => true]);
3939
$bulkWrite->updateOne(
4040
['_id' => str_repeat('a', (int) ($maxBsonObjectSize / 2))],
4141
['$set' => ['x' => 1]],

tests/SpecTests/Crud/Prose9_BulkWriteHandlesGetMoreErrorTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace MongoDB\Tests\SpecTests\Crud;
44

5-
use MongoDB\BulkWriteCommandBuilder;
5+
use MongoDB\ClientBulkWrite;
66
use MongoDB\Driver\Exception\BulkWriteCommandException;
77
use MongoDB\Driver\Monitoring\CommandFailedEvent;
88
use MongoDB\Driver\Monitoring\CommandStartedEvent;
@@ -46,7 +46,7 @@ public function testHandlesGetMoreError(): void
4646
$collection = $client->selectCollection($this->getDatabaseName(), $this->getCollectionName());
4747
$collection->drop();
4848

49-
$bulkWrite = BulkWriteCommandBuilder::createWithCollection($collection, ['verboseResults' => true]);
49+
$bulkWrite = ClientBulkWrite::createWithCollection($collection, ['verboseResults' => true]);
5050
$bulkWrite->updateOne(
5151
['_id' => str_repeat('a', (int) ($maxBsonObjectSize / 2))],
5252
['$set' => ['x' => 1]],

tests/UnifiedSpecTests/Operation.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,7 @@ private function executeForClient(Client $client)
259259
assertArrayHasKey('models', $args);
260260
assertIsArray($args['models']);
261261

262-
// Options for BulkWriteCommand and Server::executeBulkWriteCommand() will be mixed
262+
// Options for ClientBulkWriteCommand and Server::executeBulkWriteCommand() will be mixed
263263
$options = array_diff_key($args, ['models' => 1]);
264264

265265
return $client->bulkWrite(

0 commit comments

Comments
 (0)