Ejemplo n.º 1
0
 public static function get_client($write_mode = false)
 {
     // * Try to connect to every cassandra node in order
     // * Failed connections will be retried
     // * Once a connection is opened, it stays open
     // * TODO: add random and round robin order
     // * TODO: add write-preferred and read-preferred nodes
     if (self::$last_get instanceof CassandraClient) {
         return self::$last_get;
     }
     shuffle(self::$connections);
     foreach (self::$connections as $connection) {
         try {
             $transport = $connection['transport'];
             $client = $connection['client'];
             if (!$transport->isOpen()) {
                 $transport->open();
             }
             self::$last_get = $client;
             return $client;
         } catch (TException $tx) {
             self::$last_error = 'TException: ' . $tx->getMessage() . "\n";
             continue;
         }
     }
     throw new Exception("Could not connect to a cassandra server");
 }
Ejemplo n.º 2
0
 /**
  * Constructs a new CDM model
  *
  * @param  key  record key to load
  */
 public function __construct($key = NULL)
 {
     if (!CDM::$_connected) {
         foreach (Kohana::config('cdm')->connection['hosts'] as $host) {
             // Connect to cassandra nodes
             CassandraConn::add_node($host['hostname'], $host['port']);
         }
         CDM::$_connected = TRUE;
     }
     if (!isset($this->_keyspace)) {
         // Find the application keyspace if not already set
         $this->_keyspace = Kohana::config('cdm')->keyspace;
     }
     if (!isset($this->_column_family)) {
         // Use singular version of model name for the column family
         $this->_column_family = Inflector::singular(strtolower(substr(get_class($this), 10)));
     }
     if (!isset(CDM::$_managers[$this->_keyspace][$this->_column_family])) {
         // Create column manager
         CDM::$_managers[$this->_keyspace][$this->_column_family] = new CassandraCF($this->_keyspace, $this->_column_family, $this->_is_super);
     }
     $this->_manager = CDM::$_managers[$this->_keyspace][$this->_column_family];
     if ($key !== NULL) {
         // Load initial record
         $this->find($key);
     }
 }
Ejemplo n.º 3
0
 public static function connect()
 {
     foreach (self::$nodes as $node) {
         CassandraConn::add_node($node['host'], $node['port']);
     }
     self::loadColumnFamilies();
 }
Ejemplo n.º 4
0
 public function remove($key, $column_name = null)
 {
     $timestamp = time();
     $column_path = new cassandra_ColumnPath();
     $column_path->column_family = $this->column_family;
     if ($this->is_super) {
         $column_path->super_column = $this->unparse_column_name($column_name, true);
     } else {
         $column_path->column = $this->unparse_column_name($column_name, false);
     }
     $client = CassandraConn::get_client();
     $resp = $client->remove($this->keyspace, $key, $column_path, $timestamp, $this->write_consistency_level);
     return $resp;
 }
Ejemplo n.º 5
0
<?php

function record_time(&$start, $usage = "")
{
    $end = microtime(true);
    $cost = $end - $start;
    $cost = ceil(1000000 * $cost);
    if ($usage) {
        echo "{$usage} use time {$cost} us\n";
    }
    $start = $end;
}
echo "<pre>\n";
record_time($start, "");
require_once 'config.php';
CassandraConn::add_node('localhost', 9160);
class Test
{
    static function testUUID()
    {
        $id = UUID::generate(UUID::UUID_TIME, UUID::FMT_STRING, 'node', 'ns');
        echo "{$id}\n";
        $id = UUID::generate(UUID::UUID_TIME, UUID::FMT_STRING, 'node', 'ns');
        echo "{$id}\n";
        $id = UUID::generate(UUID::UUID_TIME, UUID::FMT_STRING, 'node', 'ns');
        echo "{$id}\n";
        $packid = UUID::convert($id, UUID::FMT_STRING, UUID::FMT_BINARY);
        echo strlen($packid) . "\n";
        echo UUID::convert($packid, UUID::FMT_BINARY, UUID::FMT_STRING) . "\n";
    }
    public static function testSuper()
Ejemplo n.º 6
0
 /**
  * 清除指定colnames
  * @param $key
  * @param $super
  * @param $colnames
  * @return
  */
 public function erase($key, $colnames, $sc_name = null)
 {
     $mu = new cassandra_Mutation();
     $del = new cassandra_Deletion();
     $slice = new cassandra_SlicePredicate();
     $slice->column_names = array('name', 'time');
     $del->predicate = $slice;
     $del->super_column = $this->unparse_column_name($sc_name, $this->column_type);
     $del->timestamp = CassandraUtil::get_time();
     $mu->deletion = $del;
     $mus[] = $mu;
     $client = CassandraConn::get_client();
     $mmap = array();
     $mmap[$key][$this->column_family] = $mus;
     $client->batch_mutate($this->keyspace, $mmap, $this->write_consistency_level);
 }