slaveOkay() public method

Sets whether this query can be done on a slave This method will override the static class variable slaveOkay.
public slaveOkay ( boolean $okay = true ) : MongoCursor
$okay boolean If it is okay to query the slave.
return MongoCursor Returns this cursor
Beispiel #1
0
 public function getCollection($collectionName)
 {
     MongoCursor::$slaveOkay = true;
     //if empty($this->dbName) throw error;
     $dbName = $this->dbName;
     Yii::import('application.extensions.mp.db.mongodb.*');
     $this->_collection = new MongoMSCollection($this->_writeServer->{$dbName}, $collectionName);
     $this->_collection->addSlaves($this->_readServers);
     return $this->_collection;
 }
Beispiel #2
0
 public function fetch($key, &$value, $timeout_version = null)
 {
     MongoCursor::$slaveOkay = true;
     $store = self::$_mongodb->findOne(array('key' => $this->create_key($key)));
     if (!is_null($store) && $timeout_version < $store['dateline']) {
         if ($store['ttl'] > 0 && $store['dateline'] + $store['ttl'] < time()) {
             return false;
         }
         $value = $store['value'];
         return true;
     }
     return false;
 }
Beispiel #3
0
 /**
  * Set whether secondary read queries are allowed for this cursor.
  *
  * This method wraps setSlaveOkay() for driver versions before 1.3.0. For
  * newer drivers, this method either wraps setReadPreference() method and
  * specifies SECONDARY_PREFERRED or does nothing, depending on whether
  * setReadPreference() exists.
  *
  * @param boolean $ok
  */
 public function setMongoCursorSlaveOkay($ok)
 {
     if (version_compare(phpversion('mongo'), '1.3.0', '<')) {
         $this->mongoCursor->slaveOkay($ok);
         return;
     }
     /* MongoCursor::setReadPreference() may not exist until 1.4.0. Although
      * we could throw an exception here, it's more user-friendly to NOP.
      */
     if (!method_exists($this->mongoCursor, 'setReadPreference')) {
         return;
     }
     if ($ok) {
         // Preserve existing tags for non-primary read preferences
         $readPref = $this->mongoCursor->getReadPreference();
         $tags = !empty($readPref['tagsets']) ? ReadPreference::convertTagSets($readPref['tagsets']) : array();
         $this->mongoCursor->setReadPreference(\MongoClient::RP_SECONDARY_PREFERRED, $tags);
     } else {
         $this->mongoCursor->setReadPreference(\MongoClient::RP_PRIMARY);
     }
 }
Beispiel #4
0
/**
 *  @Service(activemongo, {
 *      host: {default: 'localhost'},
 *      user: {default: NULL},
 *      pass: {default: NULL},
 *      replicaSet: {default: NULL},
 *      db: {required: true},
 *      opts: { default:{}, type: 'hash'},
 *      path: { require: true, type: array_dir},
 *      temp_dir: { default: '/tmp', type: dir },
 *      w: {default: 1},
 *      devel: {default: true}
 *  }, { shared: true })
 */
function activemongo2_service($config)
{
    if (!$config['replicaSet']) {
        $conn = new \MongoClient($config['host'], $config['opts']);
    } else {
        $conn = new \MongoClient($config['host'], array("replicaSet" => $config['replicaSet']), $config['opts']);
        $conn->setReadPreference(\MongoClient::RP_SECONDARY);
        \MongoCursor::$slaveOkay = true;
    }
    $db = $conn->selectDB($config['db']);
    if ($config['user'] || $config['pass']) {
        $db->authenticate($config['user'], $config['pass']);
    }
    $conf = new \ActiveMongo2\Configuration($config['temp_dir'] . "/activemongo2__" . $db . ".php");
    foreach ((array) $config['path'] as $path) {
        $conf->addModelPath($path);
    }
    if ($config['devel']) {
        $conf->development();
    }
    $conf->setWriteConcern($config['w']);
    $mongo = new \ActiveMongo2\Connection($conf, $conn, $db);
    return $mongo;
}
Beispiel #5
0
} catch (Exception $e) {
    var_dump($e->getCode(), $e->getMessage());
}
echo "STRING:\n";
try {
    $m = new MongoClient("mongodb://localhost/?readPreference");
} catch (Exception $e) {
    var_dump($e->getCode(), $e->getMessage());
}
try {
    $m = new MongoClient("mongodb://localhost/?=true");
} catch (Exception $e) {
    var_dump($e->getCode(), $e->getMessage());
}
try {
    $m = new MongoClient("mongodb://localhost/?timeou=4");
} catch (Exception $e) {
    var_dump($e->getCode(), $e->getMessage());
}
try {
    $m = new MongoClient("mongodb://localhost/?readPreference=nearest;slaveOkay=true");
} catch (Exception $e) {
    var_dump($e->getCode(), $e->getMessage());
}
echo "OTHERS:\n";
MongoCursor::$slaveOkay = true;
try {
    $m = new MongoClient("localhost", array("connect" => false, "readPreference" => "nearest"));
} catch (Exception $e) {
    var_dump($e->getCode(), $e->getMessage());
}
Beispiel #6
0
 /**
  * 查找记录
  * @param string $dbname
  * @param string $table_name 表名
  * @param array $query_condition 字段查找条件
  * @param array $result_condition 查询结果限制条件-limit/sort等
  * @param array $fields 获取字段
  * @return array
  */
 public function find($dbname, $table_name, $query_condition, $result_condition = array(), $fields = array())
 {
     $this->initSlavesConnection();
     MongoCursor::$slaveOkay = true;
     //从查询必备
     $cursor = $this->slaves->{$dbname}->{$table_name}->find($query_condition, $fields)->slaveOkay(true);
     if (!empty($result_condition['start'])) {
         $cursor->skip($result_condition['start']);
     }
     if (!empty($result_condition['limit'])) {
         $cursor->limit($result_condition['limit']);
     }
     if (!empty($result_condition['sort'])) {
         $cursor->sort($result_condition['sort']);
     }
     $result = array();
     try {
         while ($cursor->hasNext()) {
             $result[] = $cursor->getNext();
         }
     } catch (MongoConnectionException $e) {
         $this->error = $e->getMessage();
         return false;
     } catch (MongoCursorTimeoutException $e) {
         $this->error = $e->getMessage();
         return false;
     }
     return $result;
 }
 public function testSlaveOkay2()
 {
     $this->assertFalse(MongoCursor::$slaveOkay);
     MongoCursor::$slaveOkay = true;
     $this->assertTrue(MongoCursor::$slaveOkay);
 }
Beispiel #8
0
 public function slaveOkay($okay = true)
 {
     parent::slaveOkay($okay);
     return $this;
 }
Beispiel #9
0
 /**
  * 设置此次查询是否可以在从db上执行
  * @param Boolean $okay
  * @return muMongoCursor
  */
 public function slaveOkay($okay = true)
 {
     $this->oMongoCursor->slaveOkay($okay);
     return $this;
 }