Skip to content

Commit 523ca61

Browse files
committed
PHPLIB-130: Support readConcern option for Database and Collection
1 parent ff9d54c commit 523ca61

File tree

4 files changed

+71
-4
lines changed

4 files changed

+71
-4
lines changed

src/Collection.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use MongoDB\Driver\Command;
66
use MongoDB\Driver\Cursor;
77
use MongoDB\Driver\Manager;
8+
use MongoDB\Driver\ReadConcern;
89
use MongoDB\Driver\ReadPreference;
910
use MongoDB\Driver\Server;
1011
use MongoDB\Driver\WriteConcern;
@@ -42,6 +43,7 @@ class Collection
4243
private $collectionName;
4344
private $databaseName;
4445
private $manager;
46+
private $readConcern;
4547
private $readPreference;
4648
private $writeConcern;
4749

@@ -53,6 +55,9 @@ class Collection
5355
*
5456
* Supported options:
5557
*
58+
* * readConcern (MongoDB\Driver\ReadConcern): The default read concern to
59+
* use for collection operations. Defaults to the Manager's read concern.
60+
*
5661
* * readPreference (MongoDB\Driver\ReadPreference): The default read
5762
* preference to use for collection operations. Defaults to the Manager's
5863
* read preference.
@@ -77,6 +82,10 @@ public function __construct(Manager $manager, $namespace, array $options = [])
7782
$this->databaseName = $parts[0];
7883
$this->collectionName = $parts[1];
7984

85+
if (isset($options['readConcern']) && ! $options['readConcern'] instanceof ReadConcern) {
86+
throw new InvalidArgumentTypeException('"readConcern" option', $options['readConcern'], 'MongoDB\Driver\ReadConcern');
87+
}
88+
8089
if (isset($options['readPreference']) && ! $options['readPreference'] instanceof ReadPreference) {
8190
throw new InvalidArgumentTypeException('"readPreference" option', $options['readPreference'], 'MongoDB\Driver\ReadPreference');
8291
}
@@ -86,6 +95,7 @@ public function __construct(Manager $manager, $namespace, array $options = [])
8695
}
8796

8897
$this->manager = $manager;
98+
$this->readConcern = isset($options['readConcern']) ? $options['readConcern'] : $this->manager->getReadConcern();
8999
$this->readPreference = isset($options['readPreference']) ? $options['readPreference'] : $this->manager->getReadPreference();
90100
$this->writeConcern = isset($options['writeConcern']) ? $options['writeConcern'] : $this->manager->getWriteConcern();
91101
}
@@ -102,6 +112,7 @@ public function __debugInfo()
102112
'collectionName' => $this->collectionName,
103113
'databaseName' => $this->databaseName,
104114
'manager' => $this->manager,
115+
'readConcern' => $this->readConcern,
105116
'readPreference' => $this->readPreference,
106117
'writeConcern' => $this->writeConcern,
107118
];
@@ -621,6 +632,10 @@ public function updateOne($filter, $update, array $options = [])
621632
*
622633
* Supported options:
623634
*
635+
* * readConcern (MongoDB\Driver\ReadConcern): The default read concern to
636+
* use for collection operations. Defaults to this Collection's read
637+
* concern.
638+
*
624639
* * readPreference (MongoDB\Driver\ReadPreference): The default read
625640
* preference to use for collection operations. Defaults to this
626641
* Collection's read preference.
@@ -634,6 +649,10 @@ public function updateOne($filter, $update, array $options = [])
634649
*/
635650
public function withOptions(array $options = [])
636651
{
652+
if ( ! isset($options['readConcern'])) {
653+
$options['readConcern'] = $this->readConcern;
654+
}
655+
637656
if ( ! isset($options['readPreference'])) {
638657
$options['readPreference'] = $this->readPreference;
639658
}

src/Database.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use MongoDB\Driver\Cursor;
88
use MongoDB\Driver\Manager;
99
use MongoDB\Driver\Query;
10+
use MongoDB\Driver\ReadConcern;
1011
use MongoDB\Driver\ReadPreference;
1112
use MongoDB\Driver\Server;
1213
use MongoDB\Driver\WriteConcern;
@@ -22,6 +23,7 @@ class Database
2223
{
2324
private $databaseName;
2425
private $manager;
26+
private $readConcern;
2527
private $readPreference;
2628
private $writeConcern;
2729

@@ -33,6 +35,10 @@ class Database
3335
*
3436
* Supported options:
3537
*
38+
* * readConcern (MongoDB\Driver\ReadConcern): The default read concern to
39+
* use for database operations and selected collections. Defaults to the
40+
* Manager's read concern.
41+
*
3642
* * readPreference (MongoDB\Driver\ReadPreference): The default read
3743
* preference to use for database operations and selected collections.
3844
* Defaults to the Manager's read preference.
@@ -52,6 +58,10 @@ public function __construct(Manager $manager, $databaseName, array $options = []
5258
throw new InvalidArgumentException('$databaseName is invalid: ' . $databaseName);
5359
}
5460

61+
if (isset($options['readConcern']) && ! $options['readConcern'] instanceof ReadConcern) {
62+
throw new InvalidArgumentTypeException('"readConcern" option', $options['readConcern'], 'MongoDB\Driver\ReadConcern');
63+
}
64+
5565
if (isset($options['readPreference']) && ! $options['readPreference'] instanceof ReadPreference) {
5666
throw new InvalidArgumentTypeException('"readPreference" option', $options['readPreference'], 'MongoDB\Driver\ReadPreference');
5767
}
@@ -62,6 +72,7 @@ public function __construct(Manager $manager, $databaseName, array $options = []
6272

6373
$this->manager = $manager;
6474
$this->databaseName = (string) $databaseName;
75+
$this->readConcern = isset($options['readConcern']) ? $options['readConcern'] : $this->manager->getReadConcern();
6576
$this->readPreference = isset($options['readPreference']) ? $options['readPreference'] : $this->manager->getReadPreference();
6677
$this->writeConcern = isset($options['writeConcern']) ? $options['writeConcern'] : $this->manager->getWriteConcern();
6778
}
@@ -77,6 +88,7 @@ public function __debugInfo()
7788
return [
7889
'databaseName' => $this->databaseName,
7990
'manager' => $this->manager,
91+
'readConcern' => $this->readConcern,
8092
'readPreference' => $this->readPreference,
8193
'writeConcern' => $this->writeConcern,
8294
];
@@ -191,6 +203,10 @@ public function listCollections(array $options = [])
191203
*
192204
* Supported options:
193205
*
206+
* * readConcern (MongoDB\Driver\ReadConcern): The default read concern to
207+
* use for collection operations. Defaults to the Database's read
208+
* concern.
209+
*
194210
* * readPreference (MongoDB\Driver\ReadPreference): The default read
195211
* preference to use for collection operations. Defaults to the
196212
* Database's read preference.
@@ -205,6 +221,10 @@ public function listCollections(array $options = [])
205221
*/
206222
public function selectCollection($collectionName, array $options = [])
207223
{
224+
if ( ! isset($options['readConcern'])) {
225+
$options['readConcern'] = $this->readConcern;
226+
}
227+
208228
if ( ! isset($options['readPreference'])) {
209229
$options['readPreference'] = $this->readPreference;
210230
}
@@ -221,6 +241,10 @@ public function selectCollection($collectionName, array $options = [])
221241
*
222242
* Supported options:
223243
*
244+
* * readConcern (MongoDB\Driver\ReadConcern): The default read concern to
245+
* use for database operations and selected collections. Defaults to this
246+
* Database's read concern.
247+
*
224248
* * readPreference (MongoDB\Driver\ReadPreference): The default read
225249
* preference to use for database operations and selected collections.
226250
* Defaults to this Database's read preference.
@@ -234,6 +258,10 @@ public function selectCollection($collectionName, array $options = [])
234258
*/
235259
public function withOptions(array $options = [])
236260
{
261+
if ( ! isset($options['readConcern'])) {
262+
$options['readConcern'] = $this->readConcern;
263+
}
264+
237265
if ( ! isset($options['readPreference'])) {
238266
$options['readPreference'] = $this->readPreference;
239267
}

tests/Collection/CollectionFunctionalTest.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use MongoDB\Collection;
66
use MongoDB\Driver\BulkWrite;
7+
use MongoDB\Driver\ReadConcern;
78
use MongoDB\Driver\ReadPreference;
89
use MongoDB\Driver\WriteConcern;
910

@@ -114,6 +115,7 @@ public function testFindOne()
114115
public function testWithOptionsInheritsReadPreferenceAndWriteConcern()
115116
{
116117
$collectionOptions = [
118+
'readConcern' => new ReadConcern(ReadConcern::LOCAL),
117119
'readPreference' => new ReadPreference(ReadPreference::RP_SECONDARY_PREFERRED),
118120
'writeConcern' => new WriteConcern(WriteConcern::MAJORITY),
119121
];
@@ -122,6 +124,8 @@ public function testWithOptionsInheritsReadPreferenceAndWriteConcern()
122124
$clone = $collection->withOptions();
123125
$debug = $clone->__debugInfo();
124126

127+
$this->assertInstanceOf('MongoDB\Driver\ReadConcern', $debug['readConcern']);
128+
$this->assertSame(ReadConcern::LOCAL, $debug['readConcern']->getLevel());
125129
$this->assertInstanceOf('MongoDB\Driver\ReadPreference', $debug['readPreference']);
126130
$this->assertSame(ReadPreference::RP_SECONDARY_PREFERRED, $debug['readPreference']->getMode());
127131
$this->assertInstanceOf('MongoDB\Driver\WriteConcern', $debug['writeConcern']);
@@ -131,13 +135,16 @@ public function testWithOptionsInheritsReadPreferenceAndWriteConcern()
131135
public function testWithOptionsPassesReadPreferenceAndWriteConcern()
132136
{
133137
$collectionOptions = [
138+
'readConcern' => new ReadConcern(ReadConcern::LOCAL),
134139
'readPreference' => new ReadPreference(ReadPreference::RP_SECONDARY_PREFERRED),
135140
'writeConcern' => new WriteConcern(WriteConcern::MAJORITY),
136141
];
137142

138143
$clone = $this->collection->withOptions($collectionOptions);
139144
$debug = $clone->__debugInfo();
140145

146+
$this->assertInstanceOf('MongoDB\Driver\ReadConcern', $debug['readConcern']);
147+
$this->assertSame(ReadConcern::LOCAL, $debug['readConcern']->getLevel());
141148
$this->assertInstanceOf('MongoDB\Driver\ReadPreference', $debug['readPreference']);
142149
$this->assertSame(ReadPreference::RP_SECONDARY_PREFERRED, $debug['readPreference']->getMode());
143150
$this->assertInstanceOf('MongoDB\Driver\WriteConcern', $debug['writeConcern']);

tests/Database/DatabaseFunctionalTest.php

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use MongoDB\Database;
66
use MongoDB\Driver\BulkWrite;
7+
use MongoDB\Driver\ReadConcern;
78
use MongoDB\Driver\ReadPreference;
89
use MongoDB\Driver\WriteConcern;
910

@@ -100,9 +101,10 @@ public function testDrop()
100101
$this->assertCollectionCount($this->getNamespace(), 0);
101102
}
102103

103-
public function testSelectCollectionInheritsReadPreferenceAndWriteConcern()
104+
public function testSelectCollectionInheritsOptions()
104105
{
105106
$databaseOptions = [
107+
'readConcern' => new ReadConcern(ReadConcern::LOCAL),
106108
'readPreference' => new ReadPreference(ReadPreference::RP_SECONDARY_PREFERRED),
107109
'writeConcern' => new WriteConcern(WriteConcern::MAJORITY),
108110
];
@@ -111,31 +113,37 @@ public function testSelectCollectionInheritsReadPreferenceAndWriteConcern()
111113
$collection = $database->selectCollection($this->getCollectionName());
112114
$debug = $collection->__debugInfo();
113115

116+
$this->assertInstanceOf('MongoDB\Driver\ReadConcern', $debug['readConcern']);
117+
$this->assertSame(ReadConcern::LOCAL, $debug['readConcern']->getLevel());
114118
$this->assertInstanceOf('MongoDB\Driver\ReadPreference', $debug['readPreference']);
115119
$this->assertSame(ReadPreference::RP_SECONDARY_PREFERRED, $debug['readPreference']->getMode());
116120
$this->assertInstanceOf('MongoDB\Driver\WriteConcern', $debug['writeConcern']);
117121
$this->assertSame(WriteConcern::MAJORITY, $debug['writeConcern']->getW());
118122
}
119123

120-
public function testSelectCollectionPassesReadPreferenceAndWriteConcern()
124+
public function testSelectCollectionPassesOptions()
121125
{
122126
$collectionOptions = [
127+
'readConcern' => new ReadConcern(ReadConcern::LOCAL),
123128
'readPreference' => new ReadPreference(ReadPreference::RP_SECONDARY_PREFERRED),
124129
'writeConcern' => new WriteConcern(WriteConcern::MAJORITY),
125130
];
126131

127132
$collection = $this->database->selectCollection($this->getCollectionName(), $collectionOptions);
128133
$debug = $collection->__debugInfo();
129134

135+
$this->assertInstanceOf('MongoDB\Driver\ReadConcern', $debug['readConcern']);
136+
$this->assertSame(ReadConcern::LOCAL, $debug['readConcern']->getLevel());
130137
$this->assertInstanceOf('MongoDB\Driver\ReadPreference', $debug['readPreference']);
131138
$this->assertSame(ReadPreference::RP_SECONDARY_PREFERRED, $debug['readPreference']->getMode());
132139
$this->assertInstanceOf('MongoDB\Driver\WriteConcern', $debug['writeConcern']);
133140
$this->assertSame(WriteConcern::MAJORITY, $debug['writeConcern']->getW());
134141
}
135142

136-
public function testWithOptionsInheritsReadPreferenceAndWriteConcern()
143+
public function testWithOptionsInheritsOptions()
137144
{
138145
$databaseOptions = [
146+
'readConcern' => new ReadConcern(ReadConcern::LOCAL),
139147
'readPreference' => new ReadPreference(ReadPreference::RP_SECONDARY_PREFERRED),
140148
'writeConcern' => new WriteConcern(WriteConcern::MAJORITY),
141149
];
@@ -144,22 +152,27 @@ public function testWithOptionsInheritsReadPreferenceAndWriteConcern()
144152
$clone = $database->withOptions();
145153
$debug = $clone->__debugInfo();
146154

155+
$this->assertInstanceOf('MongoDB\Driver\ReadConcern', $debug['readConcern']);
156+
$this->assertSame(ReadConcern::LOCAL, $debug['readConcern']->getLevel());
147157
$this->assertInstanceOf('MongoDB\Driver\ReadPreference', $debug['readPreference']);
148158
$this->assertSame(ReadPreference::RP_SECONDARY_PREFERRED, $debug['readPreference']->getMode());
149159
$this->assertInstanceOf('MongoDB\Driver\WriteConcern', $debug['writeConcern']);
150160
$this->assertSame(WriteConcern::MAJORITY, $debug['writeConcern']->getW());
151161
}
152162

153-
public function testWithOptionsPassesReadPreferenceAndWriteConcern()
163+
public function testWithOptionsPassesOptions()
154164
{
155165
$databaseOptions = [
166+
'readConcern' => new ReadConcern(ReadConcern::LOCAL),
156167
'readPreference' => new ReadPreference(ReadPreference::RP_SECONDARY_PREFERRED),
157168
'writeConcern' => new WriteConcern(WriteConcern::MAJORITY),
158169
];
159170

160171
$clone = $this->database->withOptions($databaseOptions);
161172
$debug = $clone->__debugInfo();
162173

174+
$this->assertInstanceOf('MongoDB\Driver\ReadConcern', $debug['readConcern']);
175+
$this->assertSame(ReadConcern::LOCAL, $debug['readConcern']->getLevel());
163176
$this->assertInstanceOf('MongoDB\Driver\ReadPreference', $debug['readPreference']);
164177
$this->assertSame(ReadPreference::RP_SECONDARY_PREFERRED, $debug['readPreference']->getMode());
165178
$this->assertInstanceOf('MongoDB\Driver\WriteConcern', $debug['writeConcern']);

0 commit comments

Comments
 (0)