Skip to content
This repository has been archived by the owner on May 24, 2022. It is now read-only.

Issue #79: fixed cannot read off slaves with MongoClient. #110

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
104 changes: 54 additions & 50 deletions app/lib/mongo/RMongo.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@
*/
class RMongo {
private static $_lastId;

private $_mongo;

/**
* Contruct a new object
*
*
* @param string $server Server definition
* @param array $options Options
*/
Expand All @@ -23,42 +23,42 @@ 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();
}
if (method_exists($this->_mongo, "dropDB")) {
$this->_mongo->dropDB($db);
}
}

/**
* Force server to response error
*/
Expand All @@ -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
*/
Expand All @@ -91,10 +91,10 @@ public function getHosts() {
}
return array();
}

/**
* Get the read preference for this connection
*
*
* @return array
* @todo implement it under different versions
*/
Expand All @@ -104,7 +104,7 @@ public function getReadPreference() {
}
return array();
}

/**
* Get last erro
*
Expand All @@ -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
Expand All @@ -151,10 +151,10 @@ public function pairPersistConnect($username = "" , $password = "") {
}
return false;
}

/**
* Create persist connection
*
*
* @param string $username Username
* @param string $password Password
* @return boolean
Expand All @@ -165,10 +165,10 @@ public function persistConnect($username = "" , $password = "" ) {
}
return false;
}

/**
* Get previous error
*
*
* @return array
*/
public function prevError() {
Expand All @@ -177,10 +177,10 @@ public function prevError() {
}
return array();
}

/**
* Reset error
*
*
* @return array
*/
public function resetError() {
Expand All @@ -189,31 +189,31 @@ public function resetError() {
}
return array();
}

/**
* Gets a database collection
*
*
* @param string $db The database name
* @param string $collection The collection name
* @return MongoCollection
*/
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
Expand All @@ -224,7 +224,7 @@ public function setReadPreference($readPreference, array $tags = array()) {
}
return false;
}

/**
* Change slaveOkay setting for this connection
*
Expand All @@ -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
*/
Expand All @@ -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
Expand All @@ -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...
*
Expand All @@ -290,5 +296,3 @@ static function lastInsertId() {
return self::$_lastId;
}
}

?>
2 changes: 0 additions & 2 deletions index.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,5 +38,3 @@
rock_init_lang();
rock_init_plugins();
Rock::start();

?>