Commit c1590fa4 authored by Jeremy Mikola's avatar Jeremy Mikola

Merge pull request #294

parents 3c742f3c 4c61d3c5
...@@ -40,6 +40,7 @@ class Collection ...@@ -40,6 +40,7 @@ class Collection
'root' => 'MongoDB\Model\BSONDocument', 'root' => 'MongoDB\Model\BSONDocument',
]; ];
private static $wireVersionForFindAndModifyWriteConcern = 4; private static $wireVersionForFindAndModifyWriteConcern = 4;
private static $wireVersionForReadConcern = 4;
private $collectionName; private $collectionName;
private $databaseName; private $databaseName;
...@@ -162,13 +163,6 @@ class Collection ...@@ -162,13 +163,6 @@ class Collection
{ {
$hasOutStage = \MongoDB\is_last_pipeline_operator_out($pipeline); $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'])) { if ( ! isset($options['readPreference'])) {
$options['readPreference'] = $this->readPreference; $options['readPreference'] = $this->readPreference;
} }
...@@ -177,12 +171,22 @@ class Collection ...@@ -177,12 +171,22 @@ class Collection
$options['readPreference'] = new ReadPreference(ReadPreference::RP_PRIMARY); $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'])) { if ( ! isset($options['typeMap']) && ( ! isset($options['useCursor']) || $options['useCursor'])) {
$options['typeMap'] = $this->typeMap; $options['typeMap'] = $this->typeMap;
} }
$operation = new Aggregate($this->databaseName, $this->collectionName, $pipeline, $options); $operation = new Aggregate($this->databaseName, $this->collectionName, $pipeline, $options);
$server = $this->manager->selectServer($options['readPreference']);
return $operation->execute($server); return $operation->execute($server);
} }
...@@ -217,17 +221,18 @@ class Collection ...@@ -217,17 +221,18 @@ class Collection
*/ */
public function count($filter = [], array $options = []) public function count($filter = [], array $options = [])
{ {
if ( ! isset($options['readConcern'])) {
$options['readConcern'] = $this->readConcern;
}
if ( ! isset($options['readPreference'])) { if ( ! isset($options['readPreference'])) {
$options['readPreference'] = $this->readPreference; $options['readPreference'] = $this->readPreference;
} }
$operation = new Count($this->databaseName, $this->collectionName, $filter, $options);
$server = $this->manager->selectServer($options['readPreference']); $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); return $operation->execute($server);
} }
...@@ -329,17 +334,18 @@ class Collection ...@@ -329,17 +334,18 @@ class Collection
*/ */
public function distinct($fieldName, $filter = [], array $options = []) public function distinct($fieldName, $filter = [], array $options = [])
{ {
if ( ! isset($options['readConcern'])) {
$options['readConcern'] = $this->readConcern;
}
if ( ! isset($options['readPreference'])) { if ( ! isset($options['readPreference'])) {
$options['readPreference'] = $this->readPreference; $options['readPreference'] = $this->readPreference;
} }
$operation = new Distinct($this->databaseName, $this->collectionName, $fieldName, $filter, $options);
$server = $this->manager->selectServer($options['readPreference']); $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); return $operation->execute($server);
} }
...@@ -419,20 +425,21 @@ class Collection ...@@ -419,20 +425,21 @@ class Collection
*/ */
public function find($filter = [], array $options = []) public function find($filter = [], array $options = [])
{ {
if ( ! isset($options['readConcern'])) {
$options['readConcern'] = $this->readConcern;
}
if ( ! isset($options['readPreference'])) { if ( ! isset($options['readPreference'])) {
$options['readPreference'] = $this->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'])) { if ( ! isset($options['typeMap'])) {
$options['typeMap'] = $this->typeMap; $options['typeMap'] = $this->typeMap;
} }
$operation = new Find($this->databaseName, $this->collectionName, $filter, $options); $operation = new Find($this->databaseName, $this->collectionName, $filter, $options);
$server = $this->manager->selectServer($options['readPreference']);
return $operation->execute($server); return $operation->execute($server);
} }
...@@ -448,14 +455,16 @@ class Collection ...@@ -448,14 +455,16 @@ class Collection
*/ */
public function findOne($filter = [], array $options = []) public function findOne($filter = [], array $options = [])
{ {
if ( ! isset($options['readConcern'])) {
$options['readConcern'] = $this->readConcern;
}
if ( ! isset($options['readPreference'])) { if ( ! isset($options['readPreference'])) {
$options['readPreference'] = $this->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'])) { if ( ! isset($options['typeMap'])) {
$options['typeMap'] = $this->typeMap; $options['typeMap'] = $this->typeMap;
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment