public function test_serialized_col_name_validators()
 {
     $cf = new ColumnFamily($this->client, 'CompositeNames');
     $columns = array(serialize(array(0, 'meta')) => 'foo', serialize(array(1, 'blah')) => 10);
     $cf->insert('key', $columns);
     $this->assertEquals($columns, $cf->get('key'));
 }
示例#2
0
 protected function expireValue($key)
 {
     unset($this->cache[$key]);
     $cf = new ColumnFamily($this->cassandra, $this->config['column_family']);
     try {
         // __data key set as C* requires a field
         $cf->remove($key, array('__data'));
     } catch (\Exception $e) {
         return false;
     }
     return true;
 }
示例#3
0
 * been installed.
 *
 */
require_once __DIR__ . '/../lib/autoload.php';
use phpcassa\Connection\ConnectionPool;
use phpcassa\ColumnFamily;
use phpcassa\ColumnSlice;
use phpcassa\SystemManager;
use phpcassa\Schema\StrategyClass;
// Create a new keyspace and column family
$sys = new SystemManager('127.0.0.1');
$sys->create_keyspace('Keyspace1', array("strategy_class" => StrategyClass::SIMPLE_STRATEGY, "strategy_options" => array('replication_factor' => '1')));
$sys->create_column_family('Keyspace1', 'Letters', array("column_type" => "Standard", "comparator_type" => "LongType", "key_validation_class" => "UTF8Type", "default_validation_class" => "UTF8Type"));
// Start a connection pool, create our ColumnFamily instance
$pool = new ConnectionPool('Keyspace1', array('127.0.0.1'));
$letters = new ColumnFamily($pool, 'Letters');
// Insert a few records
$columns = array(1 => "a", 2 => "b", 3 => "c", 4 => "d", 5 => "e");
$letters->insert('key', $columns);
function print_slice($columns)
{
    echo "(";
    foreach ($columns as $number => $letter) {
        echo "{$number} => {$letter}, ";
    }
    echo ")\n";
}
// Fetch everything >= 2
$slice = new ColumnSlice(2);
print_slice($letters->get('key', $slice));
// Fetch everything between 2 and 4, inclusive
示例#4
0
 *
 */
require_once __DIR__ . '/../lib/autoload.php';
use phpcassa\Connection\ConnectionPool;
use phpcassa\ColumnFamily;
use phpcassa\ColumnSlice;
use phpcassa\SystemManager;
use phpcassa\Schema\StrategyClass;
// Create a new keyspace and column family
$sys = new SystemManager('127.0.0.1');
$sys->create_keyspace('Keyspace1', array("strategy_class" => StrategyClass::SIMPLE_STRATEGY, "strategy_options" => array('replication_factor' => '1')));
// Use composites for column names and row keys
$sys->create_column_family('Keyspace1', 'Composites', array("comparator_type" => "CompositeType(LongType, AsciiType)", "key_validation_class" => "CompositeType(AsciiType, LongType)"));
// Start a connection pool, create our ColumnFamily instance
$pool = new ConnectionPool('Keyspace1', array('127.0.0.1'));
$cf = new ColumnFamily($pool, 'Composites');
// Make it easier to work with non-scalar types
$cf->insert_format = ColumnFamily::ARRAY_FORMAT;
$cf->return_format = ColumnFamily::ARRAY_FORMAT;
// Insert a few records
$key1 = array("key", 1);
$key2 = array("key", 2);
$columns = array(array(array(0, "a"), "val0a"), array(array(1, "a"), "val1a"), array(array(1, "b"), "val1b"), array(array(1, "c"), "val1c"), array(array(2, "a"), "val2a"), array(array(3, "a"), "val3a"));
$cf->insert($key1, $columns);
$cf->insert($key2, $columns);
// Fetch a user record
$row = $cf->get($key1);
$col1 = $row[0];
list($name, $value) = $col1;
echo "Column name: ";
print_r($name);
 public function test_initial_connection_failure()
 {
     $servers = array('localhost', 'foobar');
     $pool = new SilentConnectionPool(self::$KS, $servers);
     $pool->fill();
     $stats = $pool->stats();
     $this->assertEquals($stats['created'], 5);
     $this->assertTrue($stats['failed'] == 5 || $stats['failed'] == 4);
     $cf = new ColumnFamily($pool, self::$CF);
     foreach (range(1, 50) as $i) {
         $cf->insert('key', array('c' => 'v'));
     }
     $pool->dispose();
     $servers = array('barfoo', 'foobar');
     $pool = new SilentConnectionPool(self::$KS, $servers);
     $this->setExpectedException('\\phpcassa\\Connection\\NoServerAvailable');
     $pool->fill();
 }
示例#6
0
 * If you're using Linux, verify that 'php5-cli' or a similar package has
 * been installed.
 *
 */
require_once __DIR__ . '/../lib/autoload.php';
use phpcassa\Connection\ConnectionPool;
use phpcassa\ColumnFamily;
use phpcassa\SystemManager;
use phpcassa\Schema\StrategyClass;
// Create a new keyspace and column family
$sys = new SystemManager('127.0.0.1');
$sys->create_keyspace('Keyspace1', array("strategy_class" => StrategyClass::SIMPLE_STRATEGY, "strategy_options" => array('replication_factor' => '1')));
$sys->create_column_family('Keyspace1', 'Users');
// Start a connection pool, create our ColumnFamily instance
$pool = new ConnectionPool('Keyspace1', array('127.0.0.1'));
$users = new ColumnFamily($pool, 'Users');
// Insert a few records
$users->insert('user0', array("name" => "joe", "state" => "TX"));
$users->insert('user1', array("name" => "bob", "state" => "CA"));
// Fetch a user record
$user = $users->get('user0');
$name = $user["name"];
echo "Fetched user {$name}\n";
// Fetch both at once
$both = $users->multiget(array('user0', 'user1'));
foreach ($both as $user_id => $columns) {
    echo "{$user_id} state: " . $columns["state"] . "\n";
}
// Only fetch the name of user1
$columns = $users->get('user1', $column_slice = null, $column_names = array("name"));
echo "Name is " . $columns["name"] . "\n";
 /**
  * Remove all items from the cache.
  *
  * @return void
  */
 public function flush()
 {
     $this->columnFamily->truncate();
 }
示例#8
0
require_once __DIR__ . '/../lib/autoload.php';
use phpcassa\Connection\ConnectionPool;
use phpcassa\ColumnFamily;
/*
 * Instead to do some crazy token calculation,
 * we can crease an array with tokens that are interested for us.
 */
$tokens = array(0 => "0", 1 => "21267647932558653966460912964485513216", 2 => "42535295865117307932921825928971026432", 3 => "63802943797675961899382738893456539648", 4 => "85070591730234615865843651857942052864", 5 => "106338239662793269832304564822427566080", 6 => "127605887595351923798765477786913079296", 7 => "148873535527910577765226390751398592512", 8 => "170141183460469231731687303715884105727");
/*
 * instead of Lucas number, you can specify token outside the ring,
 * however it will not work if you specify 0 (zero) as end of the ring.
 * 			8 => "200000000000000000000000000000000000000",
 */
// Connect to Cassandra and create an instance of the CF.
$pool = new ConnectionPool('test', array('127.0.0.1'));
$cf = new ColumnFamily($pool, 'a');
$column_to_show = "id";
// Collect the begin and end token from command line...
$begin_token = @$argv[1] ? $argv[1] : 0;
$end_token = @$argv[2] ? $argv[2] : 8;
// Collect the begin and end token from query_string...
if (@$_REQUEST["begin_token"]) {
    $begin_token = $_REQUEST["begin_token"];
}
if (@$_REQUEST["end_token"]) {
    $end_token = $_REQUEST["end_token"];
}
// Use <pre> it for web output
echo "<pre>\n\n";
// Use the iterator...
$br = 0;
示例#9
0
 public function test_get_indexed_slices()
 {
     $this->require_opp();
     $indexed_cf = new ColumnFamily($this->pool, 'Indexed1');
     $indexed_cf->truncate();
     $columns = array('birthdate' => 1);
     foreach (range(1, 3) as $i) {
         $indexed_cf->insert('key' . $i, $columns);
     }
     $expr = new IndexExpression($column_name = 'birthdate', $value = 1);
     $clause = new IndexClause(array($expr), 10000);
     $result = $indexed_cf->get_indexed_slices($clause);
     $count = 0;
     foreach ($result as $key => $cols) {
         $count++;
         $this->assertEquals($columns, $cols);
         $this->assertEquals($key, "key{$count}");
     }
     $this->assertEquals($count, 3);
     # Insert and remove a matching row at the beginning
     $indexed_cf->insert('key0', $columns);
     $indexed_cf->remove('key0');
     # Insert and remove a matching row at the end
     $indexed_cf->insert('key4', $columns);
     $indexed_cf->remove('key4');
     # Remove a matching row from the middle
     $indexed_cf->remove('key2');
     $result = $indexed_cf->get_indexed_slices($clause);
     $count = 0;
     foreach ($result as $key => $cols) {
         $count++;
         $this->assertContains($key, array("key1", "key3"));
     }
     $this->assertEquals($count, 2);
     $indexed_cf->truncate();
     $keys = array();
     foreach (range(1, 1000) as $i) {
         $indexed_cf->insert("key{$i}", $columns);
         if ($i % 50 != 0) {
             $indexed_cf->remove("key{$i}");
         } else {
             $keys[] = "key{$i}";
         }
     }
     $count = 0;
     foreach ($result as $key => $cols) {
         $count++;
         $this->assertContains($key, $keys);
         unset($keys[$key]);
     }
     $this->assertEquals($count, 20);
     $indexed_cf->truncate();
 }
示例#10
0
 * If you're using Linux, verify that 'php5-cli' or a similar package has
 * been installed.
 *
 */
require_once __DIR__ . '/../lib/autoload.php';
use phpcassa\Connection\ConnectionPool;
use phpcassa\ColumnFamily;
use phpcassa\SystemManager;
use phpcassa\Schema\StrategyClass;
// Create a new keyspace and column family
$sys = new SystemManager('127.0.0.1');
$sys->create_keyspace('Keyspace1', array("strategy_class" => StrategyClass::SIMPLE_STRATEGY, "strategy_options" => array('replication_factor' => '1')));
$sys->create_column_family('Keyspace1', 'Counts', array("default_validation_class" => 'CounterColumnType'));
// Start a connection pool, create our ColumnFamily instance
$pool = new ConnectionPool('Keyspace1', array('127.0.0.1'));
$count_cf = new ColumnFamily($pool, 'Counts');
// ColumnFamily::add() is the easiest way to increment a counter
$count_cf->add("key1", "col1");
$results = $count_cf->get("key1");
$current_count = $results["col1"];
echo "After one add(): {$current_count}\n";
// You can specify a value other than 1 to adjust the counter by
$count_cf->add("key1", "col1", 10);
$results = $count_cf->get("key1");
$current_count = $results["col1"];
echo "After add(10): {$current_count}\n";
// ColumnFamily::insert() will also increment values, but you can
// adjust multiple columns at the same time
$count_cf->insert("key1", array("col1" => 3, "col2" => -1));
$results = $count_cf->get("key1");
$current_count = $results["col1"];
 $keyspace_name = '';
 if (isset($_POST['keyspace_name'])) {
     $keyspace_name = $_POST['keyspace_name'];
 }
 $columnfamily_name = '';
 if (isset($_POST['columnfamily_name'])) {
     $columnfamily_name = $_POST['columnfamily_name'];
 }
 try {
     $pool = new ConnectionPool($keyspace_name, $cluster_helper->getArrayOfNodesForCurrentCluster(), null, 5, 5000, 5000, 10000, $cluster_helper->getCredentialsForCurrentCluster());
     $cf_def = ColumnFamilyHelper::getCFInKeyspace($keyspace_name, $columnfamily_name);
     $is_super_cf = $cf_def->column_type == 'Super';
     if ($is_super_cf) {
         $column_family = new SuperColumnFamily($pool, $columnfamily_name);
     } else {
         $column_family = new ColumnFamily($pool, $columnfamily_name);
     }
     if ($action == 'dec') {
         $value *= -1;
     }
     $column_family->add($key, $super_column, $column, $value);
     $new_value = $column_family->get($key);
     if ($column_family->cfdef->column_type == 'Super') {
         $new_value = $new_value[$super_column][$column];
     } else {
         $new_value = $new_value[$column];
     }
     redirect('counters.php?keyspace_name=' . $keyspace_name . '&columnfamily_name=' . $columnfamily_name . '&new_value=' . $new_value);
 } catch (Exception $e) {
     $_SESSION['message'] = $e->getMessage();
     redirect('counters.php?keyspace_name=' . $keyspace_name . '&columnfamily_name=' . $columnfamily_name . '&error=1');
示例#12
0
 */
require_once __DIR__ . '/../lib/autoload.php';
use phpcassa\Connection\ConnectionPool;
use phpcassa\ColumnFamily;
use phpcassa\ColumnSlice;
use phpcassa\SystemManager;
use phpcassa\Schema\StrategyClass;
use phpcassa\UUID;
// Create a new keyspace and column family
$sys = new SystemManager('127.0.0.1');
$sys->create_keyspace('Keyspace1', array("strategy_class" => StrategyClass::SIMPLE_STRATEGY, "strategy_options" => array('replication_factor' => '1')));
// We'll use TimeUUIDs for column names and UUIDs for row keys
$sys->create_column_family('Keyspace1', 'UserLogs', array("comparator_type" => "TimeUUIDType", "key_validation_class" => "LexicalUUIDType"));
// Start a connection pool, create our ColumnFamily instance
$pool = new ConnectionPool('Keyspace1', array('127.0.0.1'));
$user_logs = new ColumnFamily($pool, 'UserLogs');
// Don't use dictionary-style arrays for data
$user_logs->insert_format = ColumnFamily::ARRAY_FORMAT;
$user_logs->return_format = ColumnFamily::ARRAY_FORMAT;
// Make a couple of user IDs with non-Time UUIDs
$user1_id = UUID::uuid4();
$user2_id = UUID::uuid4();
// Insert some log messages
$user1_logs = array();
$user2_logs = array();
for ($i = 0; $i < 5; $i++) {
    $user1_logs[] = array(UUID::uuid1(), "message{$i}");
    $user2_logs[] = array(UUID::uuid1(), "message{$i}");
}
$user_logs->batch_insert(array(array($user1_id, $user1_logs), array($user2_id, $user2_logs)));
echo "Using ColumnFamily::ARRAY_FORMAT\n";
示例#13
0
 public function test_get_indexed_slices()
 {
     $cf = new ColumnFamily($this->pool, 'Indexed1');
     $cf->insert_format = ColumnFamily::ARRAY_FORMAT;
     $cf->return_format = ColumnFamily::ARRAY_FORMAT;
     $cols = array(array('birthdate', 1), array('col', 'val'));
     $rows = array(array(self::$KEYS[0], $cols), array(self::$KEYS[1], $cols), array(self::$KEYS[2], $cols));
     $cf->batch_insert($rows);
     $expr = new IndexExpression('birthdate', 1);
     $clause = new IndexClause(array($expr));
     $result = iterator_to_array($cf->get_indexed_slices($clause));
     // usort($rows, array("ArrayFormatCFTest", "sort_rows"));
     usort($result, array("ArrayFormatCFTest", "sort_rows"));
     $this->assertEquals($rows, $result);
 }