Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Only convert tag sets if necessary for setReadPreference()

This ensures the conversion function properly does nothing in driver version 1.3.3+. Adding the NOP logic in the calling context would lead to duplicated code, so we allow the utility function to be invoked even if there is nothing to do.

See: https://jira.mongodb.org/browse/PHP-638
  • Loading branch information...
commit adf94c776797378e237e7b46352c188e286a82c2 1 parent a0534a0
@jmikola jmikola authored
View
2  lib/Doctrine/MongoDB/Collection.php
@@ -635,7 +635,7 @@ public function setSlaveOkay($ok = true)
if ($ok) {
// Preserve existing tags for non-primary read preferences
$readPref = $this->getMongoCollection()->getReadPreference();
- $tags = isset($readPref['tagsets']) ? ReadPreference::convertTagSets($readPref['tagsets']) : array();
+ $tags = !empty($readPref['tagsets']) ? ReadPreference::convertTagSets($readPref['tagsets']) : array();
$this->getMongoCollection()->setReadPreference(\MongoClient::RP_SECONDARY_PREFERRED, $tags);
} else {
$this->getMongoCollection()->setReadPreference(\MongoClient::RP_PRIMARY);
View
2  lib/Doctrine/MongoDB/Cursor.php
@@ -315,7 +315,7 @@ public function setMongoCursorSlaveOkay($ok)
if ($ok) {
// Preserve existing tags for non-primary read preferences
$readPref = $this->mongoCursor->getReadPreference();
- $tags = isset($readPref['tagsets']) ? ReadPreference::convertTagSets($readPref['tagsets']) : array();
+ $tags = !empty($readPref['tagsets']) ? ReadPreference::convertTagSets($readPref['tagsets']) : array();
$this->mongoCursor->setReadPreference(\MongoClient::RP_SECONDARY_PREFERRED, $tags);
} else {
$this->mongoCursor->setReadPreference(\MongoClient::RP_PRIMARY);
View
2  lib/Doctrine/MongoDB/Database.php
@@ -219,7 +219,7 @@ public function setSlaveOkay($ok = true)
if ($ok) {
// Preserve existing tags for non-primary read preferences
$readPref = $this->getMongoDB()->getReadPreference();
- $tags = isset($readPref['tagsets']) ? ReadPreference::convertTagSets($readPref['tagsets']) : array();
+ $tags = !empty($readPref['tagsets']) ? ReadPreference::convertTagSets($readPref['tagsets']) : array();
$this->getMongoDB()->setReadPreference(\MongoClient::RP_SECONDARY_PREFERRED, $tags);
} else {
$this->getMongoDB()->setReadPreference(\MongoClient::RP_PRIMARY);
View
12 lib/Doctrine/MongoDB/Util/ReadPreference.php
@@ -75,11 +75,11 @@ public static function convertNumericType($type)
*
* Example input:
*
- * [['dc:east','use:reporting'],['dc:west'],[]]
+ * [['dc:east', 'use:reporting'], ['dc:west'], []]
*
* Example output:
*
- * [{dc:'east', use:'reporting'},{dc:'west'},{}]
+ * [['dc' => 'east', 'use' => 'reporting'], ['dc' => 'west'], []]
*
* @param array $tagSets
* @return array
@@ -87,6 +87,14 @@ public static function convertNumericType($type)
public static function convertTagSets(array $tagSets)
{
return array_map(function(array $tagSet) {
+ /* If the tag set does not contain a zeroth element, or that element
+ * does not contain a colon character, we can assume this tag set is
+ * already in the format expected by setReadPreference().
+ */
+ if (!isset($tagSet[0]) || false === strpos($tagSet[0], ':')) {
+ return $tagSet;
+ }
+
$result = array();
foreach ($tagSet as $tagAndValue) {
View
47 tests/Doctrine/MongoDB/Tests/Util/ReadPreferenceTest.php
@@ -35,12 +35,57 @@ public function provideTagSets()
array('dc' => 'east', 'use' => 'reporting'),
array('dc' => 'west'),
array(),
- )
+ ),
),
array(
array(array()),
array(array()),
),
+ /* This tag set is impractical, since an empty set matches anything,
+ * but we want to test that elements beyond the first are converted.
+ */
+ array(
+ array(
+ array(),
+ array('dc:west'),
+ array('dc:east', 'use:reporting'),
+ ),
+ array(
+ array(),
+ array('dc' => 'west'),
+ array('dc' => 'east', 'use' => 'reporting'),
+ ),
+ ),
+ );
+ }
+
+ /**
+ * @dataProvider provideTagSetsAcceptedBySetReadPreference
+ */
+ public function testConvertTagSetsShouldNotAlterTagSetsAcceptedBySetReadPreference($tagSet)
+ {
+ $this->assertEquals($tagSet, ReadPreference::convertTagSets($tagSet));
+ }
+
+ public function provideTagSetsAcceptedBySetReadPreference()
+ {
+ return array(
+ array(
+ array(
+ array('dc' => 'east', 'use' => 'reporting'),
+ array('dc' => 'west'),
+ array(),
+ ),
+ ),
+ /* These numeric tag names are likely impractical, but they should
+ * be accepted by setReadPreference() and thus not modified.
+ */
+ array(
+ array(
+ array('0' => 'zero', '1' => 'one'),
+ array(),
+ ),
+ ),
);
}
}
Please sign in to comment.
Something went wrong with that request. Please try again.