Skip to content

PHPLIB-205: Support providing collation per operation #269

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
Oct 16, 2016
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
10 changes: 4 additions & 6 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,15 @@ env:
- MONGO_REPO_TYPE="precise/mongodb-enterprise/"
- SOURCES_LOC="/etc/apt/sources.list.d/mongodb.list"
matrix:
- DRIVER_VERSION=stable SERVER_VERSION=2.6
- DRIVER_VERSION=stable SERVER_VERSION=3.0
- DRIVER_VERSION=stable SERVER_VERSION=3.2
- DRIVER_VERSION=1.2.0alpha3 SERVER_VERSION=2.6
- DRIVER_VERSION=1.2.0alpha3 SERVER_VERSION=3.0
- DRIVER_VERSION=1.2.0alpha3 SERVER_VERSION=3.2

matrix:
fast_finish: true
include:
- php: 5.6
env: DRIVER_VERSION=1.1.0 SERVER_VERSION=3.2
- php: 7.0
env: DRIVER_VERSION=stable SERVER_VERSION=2.4
env: DRIVER_VERSION=1.2.0alpha3 SERVER_VERSION=2.4
- php: 7.0
env: DRIVER_VERSION=devel SERVER_VERSION=3.2
exclude:
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
],
"require": {
"php": ">=5.4",
"ext-mongodb": "^1.1.0"
"ext-mongodb": "^1.2.0"
},
"require-dev": {
"phpunit/phpunit": "^4.8"
Expand Down
16 changes: 16 additions & 0 deletions src/Exception/UnsupportedException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace MongoDB\Exception;

class UnsupportedException extends RuntimeException implements Exception
{
/**
* Thrown when collations are not supported by a server.
*
* @return self
*/
public static function collationNotSupported()
{
return new static('Collations are not supported by the server executing this operation');
}
}
20 changes: 20 additions & 0 deletions src/Operation/Aggregate.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use MongoDB\Driver\Server;
use MongoDB\Exception\InvalidArgumentException;
use MongoDB\Exception\UnexpectedValueException;
use MongoDB\Exception\UnsupportedException;
use ArrayIterator;
use stdClass;
use Traversable;
Expand All @@ -21,6 +22,7 @@
*/
class Aggregate implements Executable
{
private static $wireVersionForCollation = 5;
private static $wireVersionForCursor = 2;
private static $wireVersionForDocumentLevelValidation = 4;
private static $wireVersionForReadConcern = 4;
Expand Down Expand Up @@ -48,6 +50,11 @@ class Aggregate implements Executable
* For servers < 3.2, this option is ignored as document level validation
* is not available.
*
* * collation (document): Collation specification.
*
* This is not supported for server versions < 3.4 and will result in an
* exception at execution time if used.
*
* * maxTimeMS (integer): The maximum amount of time to allow the query to
* run.
*
Expand Down Expand Up @@ -117,6 +124,10 @@ public function __construct($databaseName, $collectionName, array $pipeline, arr
throw InvalidArgumentException::invalidType('"bypassDocumentValidation" option', $options['bypassDocumentValidation'], 'boolean');
}

if (isset($options['collation']) && ! is_array($options['collation']) && ! is_object($options['collation'])) {
throw InvalidArgumentException::invalidType('"collation" option', $options['collation'], 'array or object');
}

if (isset($options['maxTimeMS']) && ! is_integer($options['maxTimeMS'])) {
throw InvalidArgumentException::invalidType('"maxTimeMS" option', $options['maxTimeMS'], 'integer');
}
Expand Down Expand Up @@ -158,9 +169,14 @@ public function __construct($databaseName, $collectionName, array $pipeline, arr
* @param Server $server
* @return Traversable
* @throws UnexpectedValueException if the command response was malformed
* @throws UnsupportedException if collation is used and unsupported
*/
public function execute(Server $server)
{
if (isset($this->options['collation']) && ! \MongoDB\server_supports_feature($server, self::$wireVersionForCollation)) {
throw UnsupportedException::collationNotSupported();
}

$isCursorSupported = \MongoDB\server_supports_feature($server, self::$wireVersionForCursor);
$readPreference = isset($this->options['readPreference']) ? $this->options['readPreference'] : null;

Expand Down Expand Up @@ -212,6 +228,10 @@ private function createCommand(Server $server, $isCursorSupported)
$cmd['bypassDocumentValidation'] = $this->options['bypassDocumentValidation'];
}

if (isset($this->options['collation'])) {
$cmd['collation'] = (object) $this->options['collation'];
}

if (isset($this->options['maxTimeMS'])) {
$cmd['maxTimeMS'] = $this->options['maxTimeMS'];
}
Expand Down
60 changes: 57 additions & 3 deletions src/Operation/BulkWrite.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use MongoDB\Driver\Server;
use MongoDB\Driver\WriteConcern;
use MongoDB\Exception\InvalidArgumentException;
use MongoDB\Exception\UnsupportedException;

/**
* Operation for executing multiple write operations.
Expand All @@ -23,21 +24,23 @@ class BulkWrite implements Executable
const UPDATE_MANY = 'updateMany';
const UPDATE_ONE = 'updateOne';

private static $wireVersionForCollation = 5;
private static $wireVersionForDocumentLevelValidation = 4;

private $databaseName;
private $collectionName;
private $operations;
private $options;
private $isCollationUsed = false;

/**
* Constructs a bulk write operation.
*
* Example array structure for all supported operation types:
*
* [
* [ 'deleteMany' => [ $filter ] ],
* [ 'deleteOne' => [ $filter ] ],
* [ 'deleteMany' => [ $filter, $options ] ],
* [ 'deleteOne' => [ $filter, $options ] ],
* [ 'insertOne' => [ $document ] ],
* [ 'replaceOne' => [ $filter, $replacement, $options ] ],
* [ 'updateMany' => [ $filter, $update, $options ] ],
Expand All @@ -48,8 +51,20 @@ class BulkWrite implements Executable
* writeConcern option is specified for the top-level bulk write operation
* instead of each individual operation.
*
* Supported options for deleteMany and deleteOne operations:
*
* * collation (document): Collation specification.
*
* This is not supported for server versions < 3.4 and will result in an
* exception at execution time if used.
*
* Supported options for replaceOne, updateMany, and updateOne operations:
*
* * collation (document): Collation specification.
*
* This is not supported for server versions < 3.4 and will result in an
* exception at execution time if used.
*
* * upsert (boolean): When true, a new document is created if no document
* matches the query. The default is false.
*
Expand Down Expand Up @@ -108,7 +123,25 @@ public function __construct($databaseName, $collectionName, array $operations, a

case self::DELETE_MANY:
case self::DELETE_ONE:
$operations[$i][$type][1] = ['limit' => ($type === self::DELETE_ONE ? 1 : 0)];
if ( ! isset($args[1])) {
$args[1] = [];
}

if ( ! is_array($args[1])) {
throw InvalidArgumentException::invalidType(sprintf('$operations[%d]["%s"][1]', $i, $type), $args[1], 'array');
}

$args[1]['limit'] = ($type === self::DELETE_ONE ? 1 : 0);

if (isset($args[1]['collation'])) {
$this->isCollationUsed = true;

if ( ! is_array($args[1]['collation']) && ! is_object($args[1]['collation'])) {
throw InvalidArgumentException::invalidType(sprintf('$operations[%d]["%s"][1]["collation"]', $i, $type), $args[1]['collation'], 'array or object');
}
}

$operations[$i][$type][1] = $args[1];

break;

Expand Down Expand Up @@ -136,6 +169,14 @@ public function __construct($databaseName, $collectionName, array $operations, a
$args[2]['multi'] = false;
$args[2] += ['upsert' => false];

if (isset($args[2]['collation'])) {
$this->isCollationUsed = true;

if ( ! is_array($args[2]['collation']) && ! is_object($args[2]['collation'])) {
throw InvalidArgumentException::invalidType(sprintf('$operations[%d]["%s"][2]["collation"]', $i, $type), $args[2]['collation'], 'array or object');
}
}

if ( ! is_bool($args[2]['upsert'])) {
throw InvalidArgumentException::invalidType(sprintf('$operations[%d]["%s"][2]["upsert"]', $i, $type), $args[2]['upsert'], 'boolean');
}
Expand Down Expand Up @@ -169,6 +210,14 @@ public function __construct($databaseName, $collectionName, array $operations, a
$args[2]['multi'] = ($type === self::UPDATE_MANY);
$args[2] += ['upsert' => false];

if (isset($args[2]['collation'])) {
$this->isCollationUsed = true;

if ( ! is_array($args[2]['collation']) && ! is_object($args[2]['collation'])) {
throw InvalidArgumentException::invalidType(sprintf('$operations[%d]["%s"][2]["collation"]', $i, $type), $args[2]['collation'], 'array or object');
}
}

if ( ! is_bool($args[2]['upsert'])) {
throw InvalidArgumentException::invalidType(sprintf('$operations[%d]["%s"][2]["upsert"]', $i, $type), $args[2]['upsert'], 'boolean');
}
Expand Down Expand Up @@ -210,9 +259,14 @@ public function __construct($databaseName, $collectionName, array $operations, a
* @see Executable::execute()
* @param Server $server
* @return BulkWriteResult
* @throws UnsupportedException if collation is used and unsupported
*/
public function execute(Server $server)
{
if ($this->isCollationUsed && ! \MongoDB\server_supports_feature($server, self::$wireVersionForCollation)) {
throw UnsupportedException::collationNotSupported();
}

$options = ['ordered' => $this->options['ordered']];

if (isset($this->options['bypassDocumentValidation']) && \MongoDB\server_supports_feature($server, self::$wireVersionForDocumentLevelValidation)) {
Expand Down
20 changes: 20 additions & 0 deletions src/Operation/Count.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use MongoDB\Driver\Server;
use MongoDB\Exception\InvalidArgumentException;
use MongoDB\Exception\UnexpectedValueException;
use MongoDB\Exception\UnsupportedException;

/**
* Operation for the count command.
Expand All @@ -18,6 +19,7 @@
*/
class Count implements Executable
{
private static $wireVersionForCollation = 5;
private static $wireVersionForReadConcern = 4;

private $databaseName;
Expand All @@ -30,6 +32,11 @@ class Count implements Executable
*
* Supported options:
*
* * collation (document): Collation specification.
*
* This is not supported for server versions < 3.4 and will result in an
* exception at execution time if used.
*
* * hint (string|document): The index to use. If a document, it will be
* interpretted as an index specification and a name will be generated.
*
Expand Down Expand Up @@ -60,6 +67,10 @@ public function __construct($databaseName, $collectionName, $filter = [], array
throw InvalidArgumentException::invalidType('$filter', $filter, 'array or object');
}

if (isset($options['collation']) && ! is_array($options['collation']) && ! is_object($options['collation'])) {
throw InvalidArgumentException::invalidType('"collation" option', $options['collation'], 'array or object');
}

if (isset($options['hint'])) {
if (is_array($options['hint']) || is_object($options['hint'])) {
$options['hint'] = \MongoDB\generate_index_name($options['hint']);
Expand Down Expand Up @@ -103,9 +114,14 @@ public function __construct($databaseName, $collectionName, $filter = [], array
* @param Server $server
* @return integer
* @throws UnexpectedValueException if the command response was malformed
* @throws UnsupportedException if collation is used and unsupported
*/
public function execute(Server $server)
{
if (isset($this->options['collation']) && ! \MongoDB\server_supports_feature($server, self::$wireVersionForCollation)) {
throw UnsupportedException::collationNotSupported();
}

$readPreference = isset($this->options['readPreference']) ? $this->options['readPreference'] : null;

$cursor = $server->executeCommand($this->databaseName, $this->createCommand($server), $readPreference);
Expand Down Expand Up @@ -133,6 +149,10 @@ private function createCommand(Server $server)
$cmd['query'] = (object) $this->filter;
}

if (isset($this->options['collation'])) {
$cmd['collation'] = (object) $this->options['collation'];
}

foreach (['hint', 'limit', 'maxTimeMS', 'skip'] as $option) {
if (isset($this->options[$option])) {
$cmd[$option] = $this->options[$option];
Expand Down
31 changes: 21 additions & 10 deletions src/Operation/CreateCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use MongoDB\Driver\Command;
use MongoDB\Driver\Server;
use MongoDB\Exception\InvalidArgumentException;
use MongoDB\Exception\UnsupportedException;

/**
* Operation for the create command.
Expand All @@ -18,6 +19,8 @@ class CreateCollection implements Executable
const USE_POWER_OF_2_SIZES = 1;
const NO_PADDING = 2;

private static $wireVersionForCollation = 5;

private $databaseName;
private $collectionName;
private $options = [];
Expand All @@ -34,6 +37,11 @@ class CreateCollection implements Executable
* * capped (boolean): Specify true to create a capped collection. If set,
* the size option must also be specified. The default is false.
*
* * collation (document): Collation specification.
*
* This is not supported for server versions < 3.4 and will result in an
* exception at execution time if used.
*
* * flags (integer): Options for the MMAPv1 storage engine only. Must be a
* bitwise combination CreateCollection::USE_POWER_OF_2_SIZES and
* CreateCollection::NO_PADDING. The default is
Expand Down Expand Up @@ -78,6 +86,10 @@ public function __construct($databaseName, $collectionName, array $options = [])
throw InvalidArgumentException::invalidType('"capped" option', $options['capped'], 'boolean');
}

if (isset($options['collation']) && ! is_array($options['collation']) && ! is_object($options['collation'])) {
throw InvalidArgumentException::invalidType('"collation" option', $options['collation'], 'array or object');
}

if (isset($options['flags']) && ! is_integer($options['flags'])) {
throw InvalidArgumentException::invalidType('"flags" option', $options['flags'], 'integer');
}
Expand Down Expand Up @@ -129,9 +141,14 @@ public function __construct($databaseName, $collectionName, array $options = [])
* @see Executable::execute()
* @param Server $server
* @return array|object Command result document
* @throws UnsupportedException if collation is used and unsupported
*/
public function execute(Server $server)
{
if (isset($this->options['collation']) && ! \MongoDB\server_supports_feature($server, self::$wireVersionForCollation)) {
throw UnsupportedException::collationNotSupported();
}

$cursor = $server->executeCommand($this->databaseName, $this->createCommand());

if (isset($this->options['typeMap'])) {
Expand All @@ -156,16 +173,10 @@ private function createCommand()
}
}

if (isset($this->options['indexOptionDefaults'])) {
$cmd['indexOptionDefaults'] = (object) $this->options['indexOptionDefaults'];
}

if (isset($this->options['storageEngine'])) {
$cmd['storageEngine'] = (object) $this->options['storageEngine'];
}

if (isset($this->options['validator'])) {
$cmd['validator'] = (object) $this->options['validator'];
foreach (['collation', 'indexOptionDefaults', 'storageEngine', 'validator'] as $option) {
if (isset($this->options[$option])) {
$cmd[$option] = (object) $this->options[$option];
}
}

return new Command($cmd);
Expand Down
Loading