From 7b5a71128fd8faa94494a0d1186a6332bc4c6c3b Mon Sep 17 00:00:00 2001 From: "Frederic G. MARAND" Date: Mon, 20 Jul 2015 12:01:15 +0200 Subject: [PATCH] Issue #79: fixed cannot read off slaves with MongoClient. - use setReadPreference() instead of setSlaveOkay(). --- app/lib/mongo/RMongo.php | 104 ++++++++++++++++++++------------------- index.php | 2 - 2 files changed, 54 insertions(+), 52 deletions(-) diff --git a/app/lib/mongo/RMongo.php b/app/lib/mongo/RMongo.php index 7331152..d886750 100644 --- a/app/lib/mongo/RMongo.php +++ b/app/lib/mongo/RMongo.php @@ -6,12 +6,12 @@ */ class RMongo { private static $_lastId; - + private $_mongo; - + /** * Contruct a new object - * + * * @param string $server Server definition * @param array $options Options */ @@ -23,34 +23,34 @@ public function __construct($server, array $options = array()) { $this->_mongo = new Mongo($server, $options); } } - + /** * Closes this connection - * + * * @param boolean|string $connection Connection * @return boolean */ public function close($connection) { return $this->_mongo->close($connection); } - + /** * Connects to a database server */ public function connect() { return $this->_mongo->connect(); } - + /** * Drops a database - * + * * @param mixed $db The database to drop. Can be a MongoDB object or the name of the database * @return array */ public function dropDB($db) { if (!is_object($db)) { $db = $this->selectDB($db); - } + } if (method_exists($db, "drop")) { return $db->drop(); } @@ -58,7 +58,7 @@ public function dropDB($db) { $this->_mongo->dropDB($db); } } - + /** * Force server to response error */ @@ -68,20 +68,20 @@ public function forceError() { } return false; } - + /** * Gets a database - * + * * @param string $dbname The database name * @return MongoDB */ public function __get($dbname) { return $this->_mongo->$dbname; } - + /** * Updates status for all associated hosts - * + * * @return array * @todo implement it under different versions */ @@ -91,10 +91,10 @@ public function getHosts() { } return array(); } - + /** * Get the read preference for this connection - * + * * @return array * @todo implement it under different versions */ @@ -104,7 +104,7 @@ public function getReadPreference() { } return array(); } - + /** * Get last erro * @@ -116,31 +116,31 @@ public function lastError() { } return array(); } - + /** * Lists all of the databases available - * + * * @return array */ public function listDBs() { return $this->_mongo->listDBs(); } - + /** * Connect pair servers - * + * * @return boolean */ public function pairConnect() { if (method_exists($this->_mongo, "pairConnect")) { return $this->_mongo->pairConnect(); - } + } return false; } - + /** * Create pair persist connection - * + * * @param string $username * @param string $password * @return boolean @@ -151,10 +151,10 @@ public function pairPersistConnect($username = "" , $password = "") { } return false; } - + /** * Create persist connection - * + * * @param string $username Username * @param string $password Password * @return boolean @@ -165,10 +165,10 @@ public function persistConnect($username = "" , $password = "" ) { } return false; } - + /** * Get previous error - * + * * @return array */ public function prevError() { @@ -177,10 +177,10 @@ public function prevError() { } return array(); } - + /** * Reset error - * + * * @return array */ public function resetError() { @@ -189,10 +189,10 @@ public function resetError() { } return array(); } - + /** * Gets a database collection - * + * * @param string $db The database name * @param string $collection The collection name * @return MongoCollection @@ -200,20 +200,20 @@ public function resetError() { public function selectCollection($db, $collection) { return $this->_mongo->selectCollection($db, $collection); } - + /** * Gets a database - * + * * @param string $db The database name * @return MongoDB */ public function selectDB($db) { return $this->_mongo->selectDB($db); } - + /** * Set the read preference for this connection - * + * * @param int $readPreference The read preference mode: Mongo::RP_PRIMARY, Mongo::RP_PRIMARY_PREFERRED, Mongo::RP_SECONDARY, Mongo::RP_SECONDARY_PREFERRED, or Mongo::RP_NEAREST * @param array $tags An array of zero or more tag sets, where each tag set is itself an array of criteria used to match tags on replica set members * @return boolean @@ -224,7 +224,7 @@ public function setReadPreference($readPreference, array $tags = array()) { } return false; } - + /** * Change slaveOkay setting for this connection * @@ -233,23 +233,29 @@ public function setReadPreference($readPreference, array $tags = array()) { */ public function setSlaveOkay($ok) { if (method_exists($this->_mongo, "setSlaveOkay")) { - return $this->_mongo->setSlaveOkay($ok); + $result = $this->_mongo->setSlaveOkay($ok); } - return false; + elseif (method_exists($this->_mongo, 'setReadPreference')) { + $result = $this->_mongo->setReadPreference(MongoClient::RP_PRIMARY_PREFERRED); + } + else { + $result = false; + } + return $result; } - + /** * String representation of this connection - * + * * @return string */ public function __toString() { return $this->_mongo->__toString(); } - + /** * Get mongo driver version - * + * * @return string * @since 1.1.4 */ @@ -262,10 +268,10 @@ public static function getVersion() { } return "0"; } - + /** * Compare another version with current version - * + * * @param string $version Version to compare * @return integer -1,0,1 * @since 1.1.4 @@ -274,13 +280,13 @@ public static function compareVersion($version) { $currentVersion = self::getVersion(); preg_match("/^[\\.\\d]+/", $currentVersion, $match); $number = $match[0]; - return version_compare($number, $version); + return version_compare($number, $version); } - + static function setLastInsertId($lastId) { self::$_lastId = $lastId; } - + /** * Enter description here... * @@ -290,5 +296,3 @@ static function lastInsertId() { return self::$_lastId; } } - -?> \ No newline at end of file diff --git a/index.php b/index.php index 0f26fa6..608fe77 100644 --- a/index.php +++ b/index.php @@ -38,5 +38,3 @@ rock_init_lang(); rock_init_plugins(); Rock::start(); - -?> \ No newline at end of file