Skip to content

Commit 34b88a1

Browse files
committed
Client::bulkWrite() and ClientBulkWrite operation
1 parent 7dc20c4 commit 34b88a1

File tree

2 files changed

+121
-0
lines changed

2 files changed

+121
-0
lines changed

src/Client.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
use MongoDB\Builder\BuilderEncoder;
2525
use MongoDB\Builder\Pipeline;
2626
use MongoDB\Codec\Encoder;
27+
use MongoDB\Driver\BulkWriteCommand;
28+
use MongoDB\Driver\BulkWriteCommandResult;
2729
use MongoDB\Driver\ClientEncryption;
2830
use MongoDB\Driver\Exception\InvalidArgumentException as DriverInvalidArgumentException;
2931
use MongoDB\Driver\Exception\RuntimeException as DriverRuntimeException;
@@ -39,6 +41,7 @@
3941
use MongoDB\Model\BSONArray;
4042
use MongoDB\Model\BSONDocument;
4143
use MongoDB\Model\DatabaseInfo;
44+
use MongoDB\Operation\ClientBulkWrite;
4245
use MongoDB\Operation\DropDatabase;
4346
use MongoDB\Operation\ListDatabaseNames;
4447
use MongoDB\Operation\ListDatabases;
@@ -189,6 +192,32 @@ final public function addSubscriber(Subscriber $subscriber): void
189192
$this->manager->addSubscriber($subscriber);
190193
}
191194

195+
/**
196+
* Executes multiple write operations.
197+
*
198+
* @see ClientBulkWrite::__construct() for supported options
199+
* @param string $databaseName Database name
200+
* @param array $options Additional options
201+
* @throws UnsupportedException if options are unsupported on the selected server
202+
* @throws InvalidArgumentException for parameter/option parsing errors
203+
* @throws DriverRuntimeException for other driver errors (e.g. connection errors)
204+
*/
205+
public function bulkWrite(BulkWriteCommand|BulkWriteCommandBuilder $bulk, array $options = []): ?BulkWriteCommandResult
206+
{
207+
if (! isset($options['writeConcern']) && ! is_in_transaction($options)) {
208+
$options['writeConcern'] = $this->writeConcern;
209+
}
210+
211+
if ($bulk instanceof BulkWriteCommandBuilder) {
212+
$bulk = $bulk->bulkWriteCommand;
213+
}
214+
215+
$operation = new ClientBulkWrite($bulk, $options);
216+
$server = select_server_for_write($this->manager, $options);
217+
218+
return $operation->execute($server);
219+
}
220+
192221
/**
193222
* Returns a ClientEncryption instance for explicit encryption and decryption
194223
*

src/Operation/ClientBulkWrite.php

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
<?php
2+
/*
3+
* Copyright 2015-present MongoDB, Inc.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* https://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
namespace MongoDB\Operation;
19+
20+
use MongoDB\Driver\BulkWriteCommand;
21+
use MongoDB\Driver\BulkWriteCommandResult;
22+
use MongoDB\Driver\Exception\RuntimeException as DriverRuntimeException;
23+
use MongoDB\Driver\Server;
24+
use MongoDB\Driver\Session;
25+
use MongoDB\Driver\WriteConcern;
26+
use MongoDB\Exception\InvalidArgumentException;
27+
use MongoDB\Exception\UnsupportedException;
28+
29+
use function array_filter;
30+
use function count;
31+
32+
/**
33+
* Operation for executing multiple write operations via the bulkWrite command.
34+
*
35+
* @see \MongoDB\Client::bulkWrite()
36+
*/
37+
final class ClientBulkWrite
38+
{
39+
/**
40+
* Constructs a client-level bulk write operation.
41+
*
42+
* Most options for the bulkWrite command and its write operations are
43+
* specified when building the extension's BulkWriteCommand object. This
44+
* operation only accepts options for Server::executeBulkWriteCommand().
45+
*
46+
* Supported options:
47+
*
48+
* * session (MongoDB\Driver\Session): Client session.
49+
*
50+
* * writeConcern (MongoDB\Driver\WriteConcern): Write concern.
51+
*
52+
* @param BulkWriteCommand $bulkWriteCommand Assembled bulk write command
53+
* @param array $options Command options
54+
* @throws InvalidArgumentException for parameter/option parsing errors
55+
*/
56+
public function __construct(private BulkWriteCommand $bulkWriteCommand, private array $options = [])
57+
{
58+
if (count($bulkWriteCommand) === 0) {
59+
throw new InvalidArgumentException('$bulkWriteCommand is empty');
60+
}
61+
62+
if (isset($options['session']) && ! $options['session'] instanceof Session) {
63+
throw InvalidArgumentException::invalidType('"session" option', $options['session'], Session::class);
64+
}
65+
66+
if (isset($options['writeConcern']) && ! $options['writeConcern'] instanceof WriteConcern) {
67+
throw InvalidArgumentException::invalidType('"writeConcern" option', $options['writeConcern'], WriteConcern::class);
68+
}
69+
70+
if (isset($options['writeConcern']) && $options['writeConcern']->isDefault()) {
71+
unset($options['writeConcern']);
72+
}
73+
}
74+
75+
/**
76+
* Execute the operation.
77+
*
78+
* @throws UnsupportedException if write concern is used and unsupported
79+
* @throws DriverRuntimeException for other driver errors (e.g. connection errors)
80+
*/
81+
public function execute(Server $server): ?BulkWriteCommandResult
82+
{
83+
$inTransaction = isset($this->options['session']) && $this->options['session']->isInTransaction();
84+
if ($inTransaction && isset($this->options['writeConcern'])) {
85+
throw UnsupportedException::writeConcernNotSupportedInTransaction();
86+
}
87+
88+
$options = array_filter($this->options, fn ($value) => isset($value));
89+
90+
return $server->executeBulkWriteCommand($this->bulkWriteCommand, $options);
91+
}
92+
}

0 commit comments

Comments
 (0)