Skip to content

PHPLIB-125: Support bypassDocumentValidation option on write commands #65

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 17, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions src/Operation/Aggregate.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
class Aggregate implements Executable
{
private static $wireVersionForCursor = 2;
private static $wireVersionForDocumentLevelValidation = 4;

private $databaseName;
private $collectionName;
Expand All @@ -39,6 +40,13 @@ class Aggregate implements Executable
*
* * batchSize (integer): The number of documents to return per batch.
*
* * bypassDocumentValidation (boolean): If true, allows the write to opt
* out of document level validation. This only applies when the $out
* stage is specified.
*
* For servers < 3.2, this option is ignored as document level validation
* is not available.
*
* * maxTimeMS (integer): The maximum amount of time to allow the query to
* run.
*
Expand Down Expand Up @@ -92,6 +100,10 @@ public function __construct($databaseName, $collectionName, array $pipeline, arr
throw new InvalidArgumentTypeException('"batchSize" option', $options['batchSize'], 'integer');
}

if (isset($options['bypassDocumentValidation']) && ! is_bool($options['bypassDocumentValidation'])) {
throw new InvalidArgumentTypeException('"bypassDocumentValidation" option', $options['bypassDocumentValidation'], 'boolean');
}

if (isset($options['maxTimeMS']) && ! is_integer($options['maxTimeMS'])) {
throw new InvalidArgumentTypeException('"maxTimeMS" option', $options['maxTimeMS'], 'integer');
}
Expand Down Expand Up @@ -163,6 +175,10 @@ private function createCommand(Server $server, $isCursorSupported)

$cmd['allowDiskUse'] = $this->options['allowDiskUse'];

if (isset($this->options['bypassDocumentValidation']) && \MongoDB\server_supports_feature($server, self::$wireVersionForDocumentLevelValidation)) {
$cmd['bypassDocumentValidation'] = $this->options['bypassDocumentValidation'];
}

if (isset($this->options['maxTimeMS'])) {
$cmd['maxTimeMS'] = $this->options['maxTimeMS'];
}
Expand Down
17 changes: 16 additions & 1 deletion src/Operation/BulkWrite.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ class BulkWrite implements Executable
const UPDATE_MANY = 'updateMany';
const UPDATE_ONE = 'updateOne';

private static $wireVersionForDocumentLevelValidation = 4;

private $databaseName;
private $collectionName;
private $operations;
Expand Down Expand Up @@ -54,6 +56,9 @@ class BulkWrite implements Executable
*
* Supported options for the bulk write operation:
*
* * bypassDocumentValidation (boolean): If true, allows the write to opt
* out of document level validation.
*
* * ordered (boolean): If true, when an insert fails, return without
* performing the remaining writes. If false, when a write fails,
* continue with the remaining writes, if any. The default is true.
Expand Down Expand Up @@ -182,6 +187,10 @@ public function __construct($databaseName, $collectionName, array $operations, a

$options += ['ordered' => true];

if (isset($options['bypassDocumentValidation']) && ! is_bool($options['bypassDocumentValidation'])) {
throw new InvalidArgumentTypeException('"bypassDocumentValidation" option', $options['bypassDocumentValidation'], 'boolean');
}

if ( ! is_bool($options['ordered'])) {
throw new InvalidArgumentTypeException('"ordered" option', $options['ordered'], 'boolean');
}
Expand All @@ -205,7 +214,13 @@ public function __construct($databaseName, $collectionName, array $operations, a
*/
public function execute(Server $server)
{
$bulk = new Bulk(['ordered' => $this->options['ordered']]);
$options = ['ordered' => $this->options['ordered']];

if (isset($this->options['bypassDocumentValidation']) && \MongoDB\server_supports_feature($server, self::$wireVersionForDocumentLevelValidation)) {
$options['bypassDocumentValidation'] = $this->options['bypassDocumentValidation'];
}

$bulk = new Bulk($options);
$insertedIds = [];

foreach ($this->operations as $i => $operation) {
Expand Down
18 changes: 16 additions & 2 deletions src/Operation/FindAndModify.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
*/
class FindAndModify implements Executable
{
private static $wireVersionForDocumentLevelValidation = 4;

private $databaseName;
private $collectionName;
private $options;
Expand All @@ -28,6 +30,9 @@ class FindAndModify implements Executable
*
* Supported options:
*
* * bypassDocumentValidation (boolean): If true, allows the write to opt
* out of document level validation.
*
* * fields (document): Limits the fields to return for the matching
* document.
*
Expand Down Expand Up @@ -66,6 +71,10 @@ public function __construct($databaseName, $collectionName, array $options)
'upsert' => false,
];

if (isset($options['bypassDocumentValidation']) && ! is_bool($options['bypassDocumentValidation'])) {
throw new InvalidArgumentTypeException('"bypassDocumentValidation" option', $options['bypassDocumentValidation'], 'boolean');
}

if (isset($options['fields']) && ! is_array($options['fields']) && ! is_object($options['fields'])) {
throw new InvalidArgumentTypeException('"fields" option', $options['fields'], 'array or object');
}
Expand Down Expand Up @@ -116,7 +125,7 @@ public function __construct($databaseName, $collectionName, array $options)
*/
public function execute(Server $server)
{
$cursor = $server->executeCommand($this->databaseName, $this->createCommand());
$cursor = $server->executeCommand($this->databaseName, $this->createCommand($server));
$result = current($cursor->toArray());

if ( ! isset($result->value)) {
Expand Down Expand Up @@ -144,9 +153,10 @@ public function execute(Server $server)
/**
* Create the findAndModify command.
*
* @param Server $server
* @return Command
*/
private function createCommand()
private function createCommand(Server $server)
{
$cmd = ['findAndModify' => $this->collectionName];

Expand All @@ -167,6 +177,10 @@ private function createCommand()
$cmd['maxTimeMS'] = $this->options['maxTimeMS'];
}

if (isset($this->options['bypassDocumentValidation']) && \MongoDB\server_supports_feature($server, self::$wireVersionForDocumentLevelValidation)) {
$cmd['bypassDocumentValidation'] = $this->options['bypassDocumentValidation'];
}

return new Command($cmd);
}
}
3 changes: 3 additions & 0 deletions src/Operation/FindOneAndReplace.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ class FindOneAndReplace implements Executable
*
* Supported options:
*
* * bypassDocumentValidation (boolean): If true, allows the write to opt
* out of document level validation.
*
* * maxTimeMS (integer): The maximum amount of time to allow the query to
* run.
*
Expand Down
3 changes: 3 additions & 0 deletions src/Operation/FindOneAndUpdate.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ class FindOneAndUpdate implements Executable
*
* Supported options:
*
* * bypassDocumentValidation (boolean): If true, allows the write to opt
* out of document level validation.
*
* * maxTimeMS (integer): The maximum amount of time to allow the query to
* run.
*
Expand Down
17 changes: 16 additions & 1 deletion src/Operation/InsertMany.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
*/
class InsertMany implements Executable
{
private static $wireVersionForDocumentLevelValidation = 4;

private $databaseName;
private $collectionName;
private $documents;
Expand All @@ -28,6 +30,9 @@ class InsertMany implements Executable
*
* Supported options:
*
* * bypassDocumentValidation (boolean): If true, allows the write to opt
* out of document level validation.
*
* * ordered (boolean): If true, when an insert fails, return without
* performing the remaining writes. If false, when a write fails,
* continue with the remaining writes, if any. The default is true.
Expand Down Expand Up @@ -62,6 +67,10 @@ public function __construct($databaseName, $collectionName, array $documents, ar

$options += ['ordered' => true];

if (isset($options['bypassDocumentValidation']) && ! is_bool($options['bypassDocumentValidation'])) {
throw new InvalidArgumentTypeException('"bypassDocumentValidation" option', $options['bypassDocumentValidation'], 'boolean');
}

if ( ! is_bool($options['ordered'])) {
throw new InvalidArgumentTypeException('"ordered" option', $options['ordered'], 'boolean');
}
Expand All @@ -85,7 +94,13 @@ public function __construct($databaseName, $collectionName, array $documents, ar
*/
public function execute(Server $server)
{
$bulk = new Bulk(['ordered' => $this->options['ordered']]);
$options = ['ordered' => $this->options['ordered']];

if (isset($this->options['bypassDocumentValidation']) && \MongoDB\server_supports_feature($server, self::$wireVersionForDocumentLevelValidation)) {
$options['bypassDocumentValidation'] = $this->options['bypassDocumentValidation'];
}

$bulk = new Bulk($options);
$insertedIds = [];

foreach ($this->documents as $i => $document) {
Expand Down
17 changes: 16 additions & 1 deletion src/Operation/InsertOne.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
*/
class InsertOne implements Executable
{
private static $wireVersionForDocumentLevelValidation = 4;

private $databaseName;
private $collectionName;
private $document;
Expand All @@ -27,6 +29,9 @@ class InsertOne implements Executable
*
* Supported options:
*
* * bypassDocumentValidation (boolean): If true, allows the write to opt
* out of document level validation.
*
* * writeConcern (MongoDB\Driver\WriteConcern): Write concern.
*
* @param string $databaseName Database name
Expand All @@ -41,6 +46,10 @@ public function __construct($databaseName, $collectionName, $document, array $op
throw new InvalidArgumentTypeException('$document', $document, 'array or object');
}

if (isset($options['bypassDocumentValidation']) && ! is_bool($options['bypassDocumentValidation'])) {
throw new InvalidArgumentTypeException('"bypassDocumentValidation" option', $options['bypassDocumentValidation'], 'boolean');
}

if (isset($options['writeConcern']) && ! $options['writeConcern'] instanceof WriteConcern) {
throw new InvalidArgumentTypeException('"writeConcern" option', $options['writeConcern'], 'MongoDB\Driver\WriteConcern');
}
Expand All @@ -60,7 +69,13 @@ public function __construct($databaseName, $collectionName, $document, array $op
*/
public function execute(Server $server)
{
$bulk = new Bulk();
$options = [];

if (isset($this->options['bypassDocumentValidation']) && \MongoDB\server_supports_feature($server, self::$wireVersionForDocumentLevelValidation)) {
$options['bypassDocumentValidation'] = $this->options['bypassDocumentValidation'];
}

$bulk = new Bulk($options);
$insertedId = $bulk->insert($this->document);

if ($insertedId === null) {
Expand Down
3 changes: 3 additions & 0 deletions src/Operation/ReplaceOne.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ class ReplaceOne implements Executable
*
* Supported options:
*
* * bypassDocumentValidation (boolean): If true, allows the write to opt
* out of document level validation.
*
* * upsert (boolean): When true, a new document is created if no document
* matches the query. The default is false.
*
Expand Down
21 changes: 18 additions & 3 deletions src/Operation/Update.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
*/
class Update implements Executable
{
private static $wireVersionForDocumentLevelValidation = 4;

private $databaseName;
private $collectionName;
private $filter;
Expand All @@ -31,6 +33,9 @@ class Update implements Executable
*
* Supported options:
*
* * bypassDocumentValidation (boolean): If true, allows the write to opt
* out of document level validation.
*
* * multi (boolean): When true, updates all documents matching the query.
* This option cannot be true if the $update argument is a replacement
* document (i.e. contains no update operators). The default is false.
Expand Down Expand Up @@ -63,6 +68,10 @@ public function __construct($databaseName, $collectionName, $filter, $update, ar
'upsert' => false,
];

if (isset($options['bypassDocumentValidation']) && ! is_bool($options['bypassDocumentValidation'])) {
throw new InvalidArgumentTypeException('"bypassDocumentValidation" option', $options['bypassDocumentValidation'], 'boolean');
}

if ( ! is_bool($options['multi'])) {
throw new InvalidArgumentTypeException('"multi" option', $options['multi'], 'boolean');
}
Expand Down Expand Up @@ -95,13 +104,19 @@ public function __construct($databaseName, $collectionName, $filter, $update, ar
*/
public function execute(Server $server)
{
$options = [
$updateOptions = [
'multi' => $this->options['multi'],
'upsert' => $this->options['upsert'],
];

$bulk = new Bulk();
$bulk->update($this->filter, $this->update, $options);
$bulkOptions = [];

if (isset($this->options['bypassDocumentValidation']) && \MongoDB\server_supports_feature($server, self::$wireVersionForDocumentLevelValidation)) {
$bulkOptions['bypassDocumentValidation'] = $this->options['bypassDocumentValidation'];
}

$bulk = new Bulk($bulkOptions);
$bulk->update($this->filter, $this->update, $updateOptions);

$writeConcern = isset($this->options['writeConcern']) ? $this->options['writeConcern'] : null;
$writeResult = $server->executeBulkWrite($this->databaseName . '.' . $this->collectionName, $bulk, $writeConcern);
Expand Down
3 changes: 3 additions & 0 deletions src/Operation/UpdateMany.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ class UpdateMany implements Executable
*
* Supported options:
*
* * bypassDocumentValidation (boolean): If true, allows the write to opt
* out of document level validation.
*
* * upsert (boolean): When true, a new document is created if no document
* matches the query. The default is false.
*
Expand Down
3 changes: 3 additions & 0 deletions src/Operation/UpdateOne.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ class UpdateOne implements Executable
*
* Supported options:
*
* * bypassDocumentValidation (boolean): If true, allows the write to opt
* out of document level validation.
*
* * upsert (boolean): When true, a new document is created if no document
* matches the query. The default is false.
*
Expand Down
4 changes: 4 additions & 0 deletions tests/Operation/AggregateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ public function provideInvalidConstructorOptions()
$options[][] = ['batchSize' => $value];
}

foreach ($this->getInvalidBooleanValues() as $value) {
$options[][] = ['bypassDocumentValidation' => $value];
}

foreach ($this->getInvalidIntegerValues() as $value) {
$options[][] = ['maxTimeMS' => $value];
}
Expand Down
4 changes: 4 additions & 0 deletions tests/Operation/BulkWriteTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,10 @@ public function provideInvalidConstructorOptions()
{
$options = [];

foreach ($this->getInvalidBooleanValues() as $value) {
$options[][] = ['bypassDocumentValidation' => $value];
}

foreach ($this->getInvalidBooleanValues() as $value) {
$options[][] = ['ordered' => $value];
}
Expand Down
4 changes: 4 additions & 0 deletions tests/Operation/FindAndModifyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ public function provideInvalidConstructorOptions()
{
$options = [];

foreach ($this->getInvalidBooleanValues() as $value) {
$options[][] = ['bypassDocumentValidation' => $value];
}

foreach ($this->getInvalidDocumentValues() as $value) {
$options[][] = ['fields' => $value];
}
Expand Down
4 changes: 4 additions & 0 deletions tests/Operation/InsertManyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ public function provideInvalidConstructorOptions()
{
$options = [];

foreach ($this->getInvalidBooleanValues() as $value) {
$options[][] = ['bypassDocumentValidation' => $value];
}

foreach ($this->getInvalidBooleanValues() as $value) {
$options[][] = ['ordered' => $value];
}
Expand Down
4 changes: 4 additions & 0 deletions tests/Operation/InsertOneTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ public function provideInvalidConstructorOptions()
{
$options = [];

foreach ($this->getInvalidBooleanValues() as $value) {
$options[][] = ['bypassDocumentValidation' => $value];
}

foreach ($this->getInvalidWriteConcernValues() as $value) {
$options[][] = ['writeConcern' => $value];
}
Expand Down
Loading