Skip to content

Commit 4c61d3c

Browse files
committed
PHPLIB-228: ReadConcern default should only be set when supported by the server
1 parent 3c742f3 commit 4c61d3c

File tree

1 file changed

+36
-27
lines changed

1 file changed

+36
-27
lines changed

src/Collection.php

+36-27
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ class Collection
4040
'root' => 'MongoDB\Model\BSONDocument',
4141
];
4242
private static $wireVersionForFindAndModifyWriteConcern = 4;
43+
private static $wireVersionForReadConcern = 4;
4344

4445
private $collectionName;
4546
private $databaseName;
@@ -162,13 +163,6 @@ public function aggregate(array $pipeline, array $options = [])
162163
{
163164
$hasOutStage = \MongoDB\is_last_pipeline_operator_out($pipeline);
164165

165-
/* A "majority" read concern is not compatible with the $out stage, so
166-
* avoid providing the Collection's read concern if it would conflict.
167-
*/
168-
if ( ! isset($options['readConcern']) && ! ($hasOutStage && $this->readConcern->getLevel() === ReadConcern::MAJORITY)) {
169-
$options['readConcern'] = $this->readConcern;
170-
}
171-
172166
if ( ! isset($options['readPreference'])) {
173167
$options['readPreference'] = $this->readPreference;
174168
}
@@ -177,12 +171,22 @@ public function aggregate(array $pipeline, array $options = [])
177171
$options['readPreference'] = new ReadPreference(ReadPreference::RP_PRIMARY);
178172
}
179173

174+
$server = $this->manager->selectServer($options['readPreference']);
175+
176+
/* A "majority" read concern is not compatible with the $out stage, so
177+
* avoid providing the Collection's read concern if it would conflict.
178+
*/
179+
if ( ! isset($options['readConcern']) &&
180+
! ($hasOutStage && $this->readConcern->getLevel() === ReadConcern::MAJORITY) &&
181+
\MongoDB\server_supports_feature($server, self::$wireVersionForReadConcern)) {
182+
$options['readConcern'] = $this->readConcern;
183+
}
184+
180185
if ( ! isset($options['typeMap']) && ( ! isset($options['useCursor']) || $options['useCursor'])) {
181186
$options['typeMap'] = $this->typeMap;
182187
}
183188

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

187191
return $operation->execute($server);
188192
}
@@ -217,17 +221,18 @@ public function bulkWrite(array $operations, array $options = [])
217221
*/
218222
public function count($filter = [], array $options = [])
219223
{
220-
if ( ! isset($options['readConcern'])) {
221-
$options['readConcern'] = $this->readConcern;
222-
}
223-
224224
if ( ! isset($options['readPreference'])) {
225225
$options['readPreference'] = $this->readPreference;
226226
}
227227

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

230+
if ( ! isset($options['readConcern']) && \MongoDB\server_supports_feature($server, self::$wireVersionForReadConcern)) {
231+
$options['readConcern'] = $this->readConcern;
232+
}
233+
234+
$operation = new Count($this->databaseName, $this->collectionName, $filter, $options);
235+
231236
return $operation->execute($server);
232237
}
233238

@@ -329,17 +334,18 @@ public function deleteOne($filter, array $options = [])
329334
*/
330335
public function distinct($fieldName, $filter = [], array $options = [])
331336
{
332-
if ( ! isset($options['readConcern'])) {
333-
$options['readConcern'] = $this->readConcern;
334-
}
335-
336337
if ( ! isset($options['readPreference'])) {
337338
$options['readPreference'] = $this->readPreference;
338339
}
339340

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

343+
if ( ! isset($options['readConcern']) && \MongoDB\server_supports_feature($server, self::$wireVersionForReadConcern)) {
344+
$options['readConcern'] = $this->readConcern;
345+
}
346+
347+
$operation = new Distinct($this->databaseName, $this->collectionName, $fieldName, $filter, $options);
348+
343349
return $operation->execute($server);
344350
}
345351

@@ -419,20 +425,21 @@ public function dropIndexes(array $options = [])
419425
*/
420426
public function find($filter = [], array $options = [])
421427
{
422-
if ( ! isset($options['readConcern'])) {
423-
$options['readConcern'] = $this->readConcern;
424-
}
425-
426428
if ( ! isset($options['readPreference'])) {
427429
$options['readPreference'] = $this->readPreference;
428430
}
429431

432+
$server = $this->manager->selectServer($options['readPreference']);
433+
434+
if ( ! isset($options['readConcern']) && \MongoDB\server_supports_feature($server, self::$wireVersionForReadConcern)) {
435+
$options['readConcern'] = $this->readConcern;
436+
}
437+
430438
if ( ! isset($options['typeMap'])) {
431439
$options['typeMap'] = $this->typeMap;
432440
}
433441

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

437444
return $operation->execute($server);
438445
}
@@ -448,14 +455,16 @@ public function find($filter = [], array $options = [])
448455
*/
449456
public function findOne($filter = [], array $options = [])
450457
{
451-
if ( ! isset($options['readConcern'])) {
452-
$options['readConcern'] = $this->readConcern;
453-
}
454-
455458
if ( ! isset($options['readPreference'])) {
456459
$options['readPreference'] = $this->readPreference;
457460
}
458461

462+
$server = $this->manager->selectServer($options['readPreference']);
463+
464+
if ( ! isset($options['readConcern']) && \MongoDB\server_supports_feature($server, self::$wireVersionForReadConcern)) {
465+
$options['readConcern'] = $this->readConcern;
466+
}
467+
459468
if ( ! isset($options['typeMap'])) {
460469
$options['typeMap'] = $this->typeMap;
461470
}

0 commit comments

Comments
 (0)