Skip to content

PHPLIB-228: ReadConcern default should only be set when supported by the server #294

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 1, 2016
Merged
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
63 changes: 36 additions & 27 deletions src/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ class Collection
'root' => 'MongoDB\Model\BSONDocument',
];
private static $wireVersionForFindAndModifyWriteConcern = 4;
private static $wireVersionForReadConcern = 4;

private $collectionName;
private $databaseName;
Expand Down Expand Up @@ -162,13 +163,6 @@ public function aggregate(array $pipeline, array $options = [])
{
$hasOutStage = \MongoDB\is_last_pipeline_operator_out($pipeline);

/* A "majority" read concern is not compatible with the $out stage, so
* avoid providing the Collection's read concern if it would conflict.
*/
if ( ! isset($options['readConcern']) && ! ($hasOutStage && $this->readConcern->getLevel() === ReadConcern::MAJORITY)) {
$options['readConcern'] = $this->readConcern;
}

if ( ! isset($options['readPreference'])) {
$options['readPreference'] = $this->readPreference;
}
Expand All @@ -177,12 +171,22 @@ public function aggregate(array $pipeline, array $options = [])
$options['readPreference'] = new ReadPreference(ReadPreference::RP_PRIMARY);
}

$server = $this->manager->selectServer($options['readPreference']);

/* A "majority" read concern is not compatible with the $out stage, so
* avoid providing the Collection's read concern if it would conflict.
*/
if ( ! isset($options['readConcern']) &&
! ($hasOutStage && $this->readConcern->getLevel() === ReadConcern::MAJORITY) &&
\MongoDB\server_supports_feature($server, self::$wireVersionForReadConcern)) {
$options['readConcern'] = $this->readConcern;
}

if ( ! isset($options['typeMap']) && ( ! isset($options['useCursor']) || $options['useCursor'])) {
$options['typeMap'] = $this->typeMap;
}

$operation = new Aggregate($this->databaseName, $this->collectionName, $pipeline, $options);
$server = $this->manager->selectServer($options['readPreference']);

return $operation->execute($server);
}
Expand Down Expand Up @@ -217,17 +221,18 @@ public function bulkWrite(array $operations, array $options = [])
*/
public function count($filter = [], array $options = [])
{
if ( ! isset($options['readConcern'])) {
$options['readConcern'] = $this->readConcern;
}

if ( ! isset($options['readPreference'])) {
$options['readPreference'] = $this->readPreference;
}

$operation = new Count($this->databaseName, $this->collectionName, $filter, $options);
$server = $this->manager->selectServer($options['readPreference']);

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

$operation = new Count($this->databaseName, $this->collectionName, $filter, $options);

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

Expand Down Expand Up @@ -329,17 +334,18 @@ public function deleteOne($filter, array $options = [])
*/
public function distinct($fieldName, $filter = [], array $options = [])
{
if ( ! isset($options['readConcern'])) {
$options['readConcern'] = $this->readConcern;
}

if ( ! isset($options['readPreference'])) {
$options['readPreference'] = $this->readPreference;
}

$operation = new Distinct($this->databaseName, $this->collectionName, $fieldName, $filter, $options);
$server = $this->manager->selectServer($options['readPreference']);

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

$operation = new Distinct($this->databaseName, $this->collectionName, $fieldName, $filter, $options);

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

Expand Down Expand Up @@ -419,20 +425,21 @@ public function dropIndexes(array $options = [])
*/
public function find($filter = [], array $options = [])
{
if ( ! isset($options['readConcern'])) {
$options['readConcern'] = $this->readConcern;
}

if ( ! isset($options['readPreference'])) {
$options['readPreference'] = $this->readPreference;
}

$server = $this->manager->selectServer($options['readPreference']);

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

if ( ! isset($options['typeMap'])) {
$options['typeMap'] = $this->typeMap;
}

$operation = new Find($this->databaseName, $this->collectionName, $filter, $options);
$server = $this->manager->selectServer($options['readPreference']);

return $operation->execute($server);
}
Expand All @@ -448,14 +455,16 @@ public function find($filter = [], array $options = [])
*/
public function findOne($filter = [], array $options = [])
{
if ( ! isset($options['readConcern'])) {
$options['readConcern'] = $this->readConcern;
}

if ( ! isset($options['readPreference'])) {
$options['readPreference'] = $this->readPreference;
}

$server = $this->manager->selectServer($options['readPreference']);

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

if ( ! isset($options['typeMap'])) {
$options['typeMap'] = $this->typeMap;
}
Expand Down