Permalink
Browse files

Added new ActiveRecord-style CassandraModel class

  • Loading branch information...
1 parent cd88857 commit 0536b1b6d85e3702cb3a52bc1281e9c3fadc853c @kallaspriit committed Apr 6, 2012
Showing with 1,956 additions and 810 deletions.
  1. +669 −669 Cassandra.php
  2. +502 −0 CassandraModel.php
  3. +172 −0 example-paginate-keys.php
  4. +3 −2 example.php
  5. +471 −0 test/CassandraModelTest.php
  6. +139 −139 test/CassandraTest.php
View
1,338 Cassandra.php
669 additions, 669 deletions not shown because the diff is too large. Please use a local Git client to view these changes.
View
502 CassandraModel.php
@@ -0,0 +1,502 @@
+<?php
+/**
+ * Cassandra-PHP-Client Library (CPCL).
+ *
+ * Cassandra PHP-based client library for managing and querying your Cassandra
+ * cluster. It's a high-level library performing all the rather complex
+ * low-level lifting and providing a simple to learn and use interface.
+ *
+ * Includes ideas and code snippets from PHPCassa project.
+ *
+ * Copyright (C) 2011 by Priit Kallas <kallaspriit@gmail.com>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ *
+ * @author Priit Kallas <kallaspriit@gmail.com>
+ * @package Cassandra
+ * @version 1.0
+ */
+
+/**
+ * Provides an abstraction layer on top of Cassandra where you can extend this
+ * class where the class name determines the column-family name and public
+ * members the columns.
+ */
+class CassandraModel {
+
+ /**
+ * Default connection to use.
+ *
+ * @var Cassandra
+ */
+ protected static $_defaultConnection;
+
+ /**
+ * Active connection.
+ *
+ * @var Cassandra
+ */
+ protected $_connection;
+
+ /**
+ * Row key
+ *
+ * @var string
+ */
+ protected $_key;
+
+ /**
+ * Constructor, optionally sets the row key and connection to use.
+ *
+ * If no connection is given, uses the default connection that you should
+ * set once using CassandraModel::setDefaultConnection().
+ *
+ * @param string $key Optional row key if creating a new entry
+ * @param Cassandra $connection Connection to use instead of default
+ */
+ public function __construct($key = null, Cassandra $connection = null) {
+ $this->_key = $key;
+
+ if ($connection !== null) {
+ $this->setConnection($connection);
+ } else {
+ $this->_connection = self::$_defaultConnection;
+ }
+ }
+
+ /**
+ * Static method to get an instance of the class.
+ *
+ * Optionally sets the row key and connection to use. If no connection is
+ * given, uses the default connection that you should set once using
+ * CassandraModel::setDefaultConnection().
+ *
+ * @param string $key Optional row key if creating a new entry
+ * @param Cassandra $connection Connection to use instead of default
+ * @return CassandraModel Called class instance
+ */
+ public static function getInstance(
+ $key = null,
+ Cassandra $connection = null
+ ) {
+ $className = get_called_class();
+
+ return new $className($key, $connection);
+ }
+
+ /**
+ * Sets the default Cassandra connection to use.
+ *
+ * You will usually call it once during boostrapping so you wouldn't need
+ * to set it again every time in constructor or the static methods.
+ *
+ * @param Cassandra $connection Connection to use by default
+ */
+ public static function setDefaultConnection(Cassandra $connection) {
+ self::$_defaultConnection = $connection;
+ }
+
+ /**
+ * Returns the current default connection.
+ *
+ * @return Cassandra
+ */
+ public static function getDefaultConnection() {
+ return self::$_defaultConnection;
+ }
+
+ /**
+ * Overrides the default connection for current instance.
+ *
+ * @param Cassandra $connection Connection to use
+ */
+ public function setConnection(Cassandra $connection) {
+ $this->_connection = $connection;
+ }
+
+ /**
+ * Returns current instance Cassandra connection.
+ *
+ * @return Cassandra
+ */
+ public function getConnection() {
+ return $this->_connection;
+ }
+
+ /**
+ * Populates model with data.
+ *
+ * Array keys should match model public members.
+ *
+ * @param array $data Data to use
+ * @return CassandraModel Self for chaining
+ */
+ public function populate(array $data) {
+ foreach ($data as $key => $value) {
+ $this->$key = $value;
+ }
+
+ return $this;
+ }
+
+ /**
+ * Returns the row key, also setting it if key is provided.
+ *
+ * @param string $newKey Optioal new key to use
+ * @return string Active row key
+ */
+ public function key($newKey = null) {
+ if (isset($newKey)) {
+ $this->_key = $newKey;
+ }
+
+ return $this->_key;
+ }
+
+ /**
+ * Returns model data as an associative array.
+ *
+ * @return array
+ */
+ public function getData() {
+ $columns = $this->getSetColumnNames();
+ $data = array();
+
+ foreach ($columns as $column) {
+ $data[$column] = $this->$column;
+ }
+
+ return $data;
+ }
+
+ /**
+ * Loads data into the model by row key.
+ *
+ * Returns the model instance if entry was found and null otherwise.
+ *
+ * @param mixed $rowKey Value of the row key to find item by.
+ * @param integer $consistency Option override of default consistency level
+ * @param Cassandra $connection If not set, the default connection is used
+ * @return CassandraModel|null Model instance or null if not found
+ */
+ public static function load(
+ $rowKey,
+ $consistency = null,
+ Cassandra $connection = null
+ ) {
+ $columnFamily = self::getColumnFamilyName();
+ $columns = self::getColumnNames();
+
+ if (!isset($connection)) {
+ $connection = self::$_defaultConnection;
+ }
+
+ if (empty($columns)) {
+ $columns = null;
+ }
+
+ $data = $connection->cf($columnFamily)->get(
+ $rowKey,
+ $columns,
+ null,
+ null,
+ false,
+ 100,
+ null,
+ $consistency
+ );
+
+ if ($data !== null) {
+ $instance = self::getInstance();
+ $instance->populate($data);
+ $instance->_key = $rowKey;
+ $instance->setConnection($connection);
+
+ return $instance;
+ } else {
+ return null;
+ }
+ }
+
+ /**
+ * Returns a range of rows.
+ *
+ * This is available only when using the order preserving partitioner. Can
+ * be used to paginate data etc. Returns an iterator that fetches the data
+ * from the database in chunks.
+ *
+ * @param string $startKey Starting key, null to start with first
+ * @param string $endKey Ending key, null for all
+ * @param integer $consistency Option override of default consistency level
+ * @param Cassandra $connection If not set, the default connection is used
+ * @return CassandraRangeDataIterator Key range iterator
+ */
+ public static function getKeyRange(
+ $startKey = null,
+ $endKey = null,
+ $consistency = null,
+ Cassandra $connection = null
+ ) {
+ $columnFamily = self::getColumnFamilyName();
+ $columns = self::getColumnNames();
+
+ if (!isset($connection)) {
+ $connection = self::$_defaultConnection;
+ }
+
+ return $connection->cf($columnFamily)->getKeyRange(
+ $startKey,
+ $endKey,
+ null,
+ $columns,
+ null,
+ null,
+ false,
+ 100,
+ null,
+ $consistency,
+ 100
+ );
+ }
+
+ /**
+ * Returns a range of columns for a key.
+ *
+ * @param string $key Row key
+ * @param string $startColumn Starting column, null for first
+ * @param string $endColumn Ending column, null for last
+ * @param integer $limit Maximum number of columns to return
+ * @param integer $consistency Consistency to use, null for default
+ * @param Cassandra $connection Connection to use
+ * @return array Columns of data in given range up to given limit items
+ */
+ public static function getColumnRange(
+ $key,
+ $startColumn = null,
+ $endColumn = null,
+ $limit = 100,
+ $consistency = null,
+ Cassandra $connection = null
+ ) {
+ $columnFamily = self::getColumnFamilyName();
+
+ if (!isset($connection)) {
+ $connection = self::$_defaultConnection;
+ }
+
+ return $connection->cf($columnFamily)->get(
+ $key,
+ null,
+ $startColumn,
+ $endColumn,
+ false,
+ $limit,
+ null,
+ $consistency
+ );
+ }
+
+ /**
+ * Saves the row into the database.
+ *
+ * You can set a new key for the row and also populate the model with data
+ * if you haven't done so already.
+ *
+ * You may also set the consistency if you like it to be something else from
+ * tge default of Cassandra::CONSISTENCY_ONE
+ *
+ * @param string $newKey Optional new row key
+ * @param array $populate Optionally populate the model with data
+ * @param integer $consistency Option override of default consistency level
+ * @return boolean Was saving the entry successful
+ */
+ public function save(
+ $newKey = null,
+ array $populate = null,
+ $consistency = null
+ ) {
+ if (isset($newKey)) {
+ $this->_key = $newKey;
+ }
+
+ if (is_array($populate)) {
+ $this->populate($populate);
+ }
+
+ $columnFamily = self::getColumnFamilyName();
+
+ $this->_connection->cf($columnFamily)->set(
+ $this->_key,
+ $this->getData(),
+ $consistency
+ );
+
+ return true;
+ }
+
+ /**
+ * Inserts a new row into the database.
+ *
+ * This is a convenience proxy to creating an instance of the model,
+ * populating it and calling save().
+ *
+ * @param string $key Row key to use
+ * @param array $data Data to populate the model with
+ * @param integer $consistency Option override of default consistency level
+ * @param Cassandra $connection If not set, the default connection is used
+ * @return boolean Was adding the entry successful
+ */
+ public static function insert(
+ $key,
+ array $data,
+ $consistency = null,
+ Cassandra $connection = null
+ ) {
+ return self::getInstance(null, $connection)->save(
+ $key, $data, $consistency
+ );
+ }
+
+ /**
+ * Removes an entry or some columns of it.
+ *
+ * If the columns is left to null, the entire row is deleted.
+ *
+ * If you already have an instance of the model, use delete().
+ *
+ * @param string $key Row key to delete
+ * @param array $columns Optional columns to delete
+ * @param integer $consistency Option override of default consistency level
+ * @param Cassandra $connection If not set, the default connection is used
+ * @return boolean Was removing the entry successful
+ */
+ public static function remove(
+ $key,
+ array $columns = null,
+ $consistency = null,
+ Cassandra $connection = null
+ ) {
+ $model = self::getInstance($key, $connection);
+
+ return $model->delete($columns, $consistency);
+ }
+
+ /**
+ * Removes an entry or some columns of it.
+ *
+ * If the columns is left to null, the entire row is deleted.
+ *
+ * Uses the currently set row key, you can change it with key() method.
+ *
+ * You can remove a row by calling remove() statically.
+ *
+ * @param array $columns Optional columns to delete
+ * @param integer $consistency Option override of default consistency level
+ * @return boolean Was removing the entry successful
+ */
+ public function delete(array $columns = null, $consistency = null) {
+ $columnFamily = self::getColumnFamilyName();
+
+ $this->_connection->cf($columnFamily)->remove(
+ $this->_key,
+ $columns,
+ null,
+ $consistency
+ );
+
+ return true;
+ }
+
+ /**
+ * Returns the column family name that corresponds to given model class.
+ *
+ * The transformation is done in following steps:
+ * 1. remove "Model" from the end of class name
+ * 2. add a underscore before every upper-case word except first
+ * 3. make everything lowercase
+ *
+ * So for example, class names to table names:
+ * - UserModel > user
+ * - ForumTopicsModel > forum_topics
+ *
+ * @return string The table name
+ */
+ public static function getColumnFamilyName() {
+ $className = get_called_class();
+
+ $components = preg_split(
+ '/([A-Z][^A-Z]*)/',
+ substr($className, 0, -5),
+ -1,
+ PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE
+ );
+
+ return mb_strtolower(implode('_', $components));
+ }
+
+ /**
+ * Returns the column names of the model.
+ *
+ * The column names are determined from public class vars.
+ *
+ * @return array
+ */
+ public static function getColumnNames() {
+ return array_keys(
+ get_class_public_vars(get_called_class())
+ );
+ }
+
+ /**
+ * Returns the column names of the model that have been set for current
+ * object.
+ *
+ * The column names are determined from public object vars.
+ *
+ * @return array
+ */
+ public function getSetColumnNames() {
+ return array_keys(get_object_public_vars($this));
+ }
+
+}
+
+if (!function_exists('get_class_public_vars')) {
+ /**
+ * Returns publicly available properties of a class.
+ *
+ * @param string $class_name Class name
+ * @return array Public var names
+ */
+ function get_class_public_vars($class_name) {
+ return get_class_vars($class_name);
+ }
+}
+
+if (!function_exists('get_object_public_vars')) {
+ /**
+ * Returns publicly available properties of an object.
+ *
+ * @param object $object Object to probe
+ * @return array Public var names
+ */
+ function get_object_public_vars($object) {
+ return get_object_vars($object);
+ }
+}
View
172 example-paginate-keys.php
@@ -0,0 +1,172 @@
+<?php
+
+$startTime = microtime(true);
+
+// show all the errors
+error_reporting(E_ALL);
+session_start();
+
+// the only file that needs including into your project
+require_once 'Cassandra.php';
+require_once 'CassandraModel.php';
+
+class UserModel extends CassandraModel {
+ public $email;
+ public $name;
+ public $age;
+}
+
+// list of seed servers to randomly connect to
+// all the parameters are optional and default to given values
+$servers = array(
+ array(
+ 'host' => '127.0.0.1',
+ 'port' => 9160,
+ 'use-framed-transport' => true,
+ 'send-timeout-ms' => 1000,
+ 'receive-timeout-ms' => 1000
+ )
+);
+
+// create a named singleton, the second parameter name defaults to "main"
+// you can have several named singletons with different server pools
+$cassandra = Cassandra::createInstance($servers);
+
+/*
+// drop the example keyspace and ignore errors should it not exist
+try {
+ $cassandra->dropKeyspace('CassandraRowsPaginationExample');
+} catch (Exception $e) {}
+*/
+
+// set the default connection for the model
+CassandraModel::setDefaultConnection($cassandra);
+
+try {
+ // start using the created keyspace
+ $cassandra->useKeyspace('CassandraRowsPaginationExample');
+} catch (Exception $e) {
+ // create a new keyspace, accepts extra parameters for replication options
+ // normally you don't do it every time
+ $cassandra->createKeyspace('CassandraRowsPaginationExample');
+
+ // use the keyspace again properly
+ $cassandra->useKeyspace('CassandraRowsPaginationExample');
+
+ // create a standard column family with given column metadata
+ $cassandra->createStandardColumnFamily(
+ 'CassandraRowsPaginationExample', // keyspace name
+ 'user', // the column-family name
+ array( // list of columns with metadata
+ array(
+ 'name' => 'name',
+ 'type' => Cassandra::TYPE_UTF8,
+ 'index-type' => Cassandra::INDEX_KEYS, // create secondary index
+ 'index-name' => 'NameIdx'
+ ),
+ array(
+ 'name' => 'email',
+ 'type' => Cassandra::TYPE_UTF8
+ ),
+ array(
+ 'name' => 'age',
+ 'type' => Cassandra::TYPE_INTEGER,
+ 'index-type' => Cassandra::INDEX_KEYS,
+ 'index-name' => 'AgeIdx'
+ )
+ )
+ // actually accepts more parameters with reasonable defaults
+ );
+
+ $columns = array(
+ 'name' => 'Columns test',
+ 'age' => 99,
+ 'email' => 'columns-test@cpcl.com',
+ );
+
+ // create some example data
+ for ($i = 0; $i < 1000; $i++) {
+ UserModel::insert('user-'.sprintf('%04d', $i), array(
+ 'name' => 'User #'.$i,
+ 'age' => round($i / 10),
+ 'email' => 'user-'.$i.'@cpcl.com',
+ ));
+
+ $columns[] = 'Column #'.$i;
+ }
+
+ UserModel::insert('columns-test', $columns);
+}
+
+echo '<pre>';
+
+/*
+$firstUser = UserModel::load('user-0000');
+print_r($firstUser->getData());
+*/
+
+$reverse = false;
+$start = null;
+
+if (isset($_GET['next'])) {
+ $start = $_GET['next'];
+} else if (isset($_GET['previous'])) {
+ $reverse = true;
+
+ if (isset($_SESSION['start-'.$_GET['previous']])) {
+ $start = $_SESSION['start-'.$_GET['previous']];
+ }
+}
+
+$data = UserModel::getKeyRange($start);
+
+$count = 0;
+$itemsPerPage = 10;
+$prevExists = $start != null;
+$nextExists = false;
+$firstKey = null;
+$lastKey = null;
+$items = array();
+
+foreach ($data as $key => $user) {
+ if ($count == 0) {
+ $firstKey = $key;
+ }
+
+ $lastKey = $key;
+
+ if ($count == $itemsPerPage) {
+ $nextExists = true;
+
+ break;
+ }
+
+ $items[$key] = $user;
+
+ $count++;
+}
+
+if (!$reverse) {
+ $_SESSION['start-'.$lastKey] = $start;
+}
+
+foreach ($items as $key => $item) {
+ echo $key.': '.print_r($item, true);
+}
+
+
+if ($prevExists) {
+ echo '<a href="?previous='.$firstKey.'">Previous</a>';
+} else {
+ echo 'Previous';
+}
+
+echo ' | ';
+
+if ($nextExists) {
+ echo '<a href="?next='.$lastKey.'">Next</a>';
+} else {
+ echo 'Next';
+}
+
+echo '<p>It took '.round(microtime(true) - $startTime, 3).' seconds</p>';
View
5 example.php
@@ -239,9 +239,10 @@
// we can still use the additional filters of columns
$tallinn = $cassandra->get('cities.Estonia.Tallinn:population,size');
echo 'Super-column cities.Estonia.Tallinn: <pre>'.print_r($tallinn, true).'</pre><hr/>';
-
+/*
// you can delete all the data in a column family using "truncate"
$cassandra->truncate('user');
// you may choose to drop an entire keyspace
-$cassandra->dropKeyspace('CassandraExample');
+$cassandra->dropKeyspace('CassandraExample');
+*/
View
471 test/CassandraModelTest.php
@@ -0,0 +1,471 @@
+<?php
+
+require_once '../Cassandra.php';
+require_once '../CassandraModel.php';
+
+class UserModel extends CassandraModel {
+ public $email;
+ public $name;
+ public $age;
+}
+
+class CassandraModelTest extends PHPUnit_Framework_TestCase {
+
+ protected $servers;
+
+ /**
+ * @var Cassandra
+ */
+ protected $cassandra;
+ protected $cassandra2;
+
+ protected static $setupComplete = false;
+
+ public function setup() {
+ if (function_exists('apc_clear_cache')) {
+ apc_clear_cache();
+ }
+
+ $this->servers = array(
+ array(
+ 'host' => '127.0.0.1',
+ 'port' => 9160
+ )
+ );
+
+ $this->cassandra = Cassandra::createInstance($this->servers);
+ $this->cassandra2 = Cassandra::createInstance($this->servers, 'Another');
+
+ if (!self::$setupComplete) {
+ try {
+ $this->cassandra->dropKeyspace('CassandraModelTest');
+ } catch (Exception $e) {}
+
+ $this->cassandra->setMaxCallRetries(5);
+ $this->cassandra->createKeyspace('CassandraModelTest');
+ $this->cassandra->useKeyspace('CassandraModelTest');
+
+ $this->cassandra->createStandardColumnFamily(
+ 'CassandraModelTest',
+ 'user',
+ array(
+ array(
+ 'name' => 'email',
+ 'type' => Cassandra::TYPE_UTF8
+ ),
+ array(
+ 'name' => 'name',
+ 'type' => Cassandra::TYPE_UTF8,
+ 'index-type' => Cassandra::INDEX_KEYS,
+ 'index-name' => 'NameIdx'
+ ),
+ array(
+ 'name' => 'age',
+ 'type' => Cassandra::TYPE_INTEGER,
+ 'index-type' => Cassandra::INDEX_KEYS,
+ 'index-name' => 'AgeIdx'
+ )
+ )
+ );
+
+ /*
+ $this->cassandra->createSuperColumnFamily(
+ 'CassandraModelTest',
+ 'cities',
+ array(
+ array(
+ 'name' => 'population',
+ 'type' => Cassandra::TYPE_INTEGER
+ ),
+ array(
+ 'name' => 'comment',
+ 'type' => Cassandra::TYPE_UTF8
+ )
+ ),
+ Cassandra::TYPE_UTF8,
+ Cassandra::TYPE_UTF8,
+ Cassandra::TYPE_UTF8,
+ 'Capitals supercolumn test',
+ 1000,
+ 1000,
+ 0.5
+ );
+ */
+
+ self::$setupComplete = true;
+ } else {
+ $this->cassandra->useKeyspace('CassandraModelTest');
+ }
+
+ $this->cassandra2->useKeyspace('CassandraModelTest');
+ }
+
+ public function tearDown() {
+ unset($this->cassandra);
+ unset($this->cassandra2);
+
+ if (function_exists('apc_clear_cache')) {
+ apc_clear_cache();
+ }
+ }
+
+ public function testNoConnectionByDefault() {
+ $this->assertNull(CassandraModel::getDefaultConnection());
+ }
+
+ public function testDefaultConnectionCanBeSet() {
+ $this->assertNull(CassandraModel::getDefaultConnection());
+
+ CassandraModel::setDefaultConnection($this->cassandra);
+
+ $this->assertEquals(
+ CassandraModel::getDefaultConnection(),
+ $this->cassandra
+ );
+ }
+
+ public function testDefaultConnectionIsInherited() {
+ CassandraModel::setDefaultConnection($this->cassandra);
+
+ $model = new UserModel();
+
+ $this->assertEquals(
+ $model->getConnection(),
+ $this->cassandra
+ );
+ }
+
+ public function testConnectionCanBeSetInContructor() {
+ CassandraModel::setDefaultConnection($this->cassandra);
+
+ $model = new UserModel(null, $this->cassandra2);
+
+ $this->assertEquals(
+ $model->getConnection(),
+ $this->cassandra2
+ );
+ }
+
+ public function testConnectionCanBeChangedLater() {
+ CassandraModel::setDefaultConnection($this->cassandra);
+
+ $model = new UserModel();
+ $model->setConnection($this->cassandra2);
+
+ $this->assertEquals(
+ $model->getConnection(),
+ $this->cassandra2
+ );
+ }
+
+ public function testRowCanBeLoadedByKey() {
+ $this->cassandra->set(
+ 'user.john',
+ array(
+ 'email' => 'john@smith.com',
+ 'name' => 'John Smith',
+ 'age' => 23
+ )
+ );
+
+ $model = UserModel::load('john');
+
+ $this->assertNotNull($model);
+ $this->assertEquals($model->email, 'john@smith.com');
+ $this->assertEquals($model->name, 'John Smith');
+ $this->assertEquals($model->age, 23);
+ }
+
+ public function testRowCanBeLoadedByKey2() {
+ $this->cassandra->set(
+ 'user.john',
+ array(
+ 'email' => 'john@smith.com',
+ 'name' => 'John Smith',
+ 'age' => 23
+ )
+ );
+
+ $model = UserModel::load('john');
+
+ $this->assertEquals(
+ $model->getData(),
+ array(
+ 'email' => 'john@smith.com',
+ 'name' => 'John Smith',
+ 'age' => 23
+ )
+ );
+ }
+
+ public function testKeyIsNullByDefault() {
+ $model = new UserModel();
+
+ $this->assertNull($model->key());
+ }
+
+ public function testLoadedKeyIsReturned() {
+ $this->cassandra->set(
+ 'user.john',
+ array(
+ 'email' => 'john@smith.com',
+ 'name' => 'John Smith',
+ 'age' => 23
+ )
+ );
+
+ $model = UserModel::load('john');
+
+ $this->assertEquals($model->key(), 'john');
+ }
+
+ public function testKeyCanBeChanged() {
+ $model = new UserModel();
+
+ $result = $model->key('test');
+
+ $this->assertEquals($result, 'test');
+ $this->assertEquals($model->key(), 'test');
+ }
+
+ public function testLoadReturnsNullIfNotFound() {
+ $model = UserModel::load('non-existing');
+
+ $this->assertNull($model);
+ }
+
+ public function testEntryCanBeAdded1() {
+ $this->assertNull(UserModel::load('jane'));
+
+ $model = new UserModel('jane');
+ $model->email = 'jane@doe.com';
+ $model->name = 'Jane Doe';
+ $model->age = 25;
+ $result = $model->save();
+
+ $this->assertTrue($result);
+
+ $jane = UserModel::load('jane');
+
+ $this->assertNotNull($jane);
+ $this->assertEquals($jane->email, 'jane@doe.com');
+ $this->assertEquals($jane->name, 'Jane Doe');
+ $this->assertEquals($jane->age, 25);
+ $this->assertEquals($jane->key(), 'jane');
+ }
+
+ public function testEntryCanBeAdded2() {
+ $this->assertNull(UserModel::load('eve'));
+
+ $model = new UserModel();
+ $model->email = 'eve@doe.com';
+ $model->name = 'Eve Doe';
+ $model->age = 23;
+ $result = $model->save('eve');
+
+ $this->assertTrue($result);
+
+ $jane = UserModel::load('eve', null, $this->cassandra2);
+
+ $this->assertNotNull($jane);
+ $this->assertEquals($jane->email, 'eve@doe.com');
+ $this->assertEquals($jane->name, 'Eve Doe');
+ $this->assertEquals($jane->age, 23);
+ $this->assertEquals($jane->key(), 'eve');
+ $this->assertEquals($jane->getConnection(), $this->cassandra2);
+ }
+
+ public function testEntryCanBeAdded3() {
+ $this->assertNull(UserModel::load('alice'));
+
+ $model = new UserModel();
+ $result = $model->save('alice', array(
+ 'email' => 'alice@wonderland.com',
+ 'name' => 'Alice',
+ 'age' => 18,
+ ));
+
+ $this->assertTrue($result);
+
+ $alice = UserModel::load('alice');
+
+ $this->assertNotNull($alice);
+ $this->assertEquals($alice->email, 'alice@wonderland.com');
+ $this->assertEquals($alice->name, 'Alice');
+ $this->assertEquals($alice->age, 18);
+ $this->assertEquals($alice->key(), 'alice');
+ }
+
+ public function testEntryCanBeInserted() {
+ $this->assertNull(UserModel::load('kate'));
+
+ $result = UserModel::insert('kate', array(
+ 'email' => 'kate@gmail.com',
+ 'name' => 'Kate',
+ 'age' => 21,
+ ));
+
+ $this->assertTrue($result);
+
+ $kate = UserModel::load('kate');
+
+ $this->assertNotNull($kate);
+ $this->assertEquals($kate->email, 'kate@gmail.com');
+ $this->assertEquals($kate->name, 'Kate');
+ $this->assertEquals($kate->age, 21);
+ $this->assertEquals($kate->key(), 'kate');
+ }
+
+ public function testEntryCanBeInserted2() {
+ $this->assertNull(UserModel::load('katy'));
+
+ $result = UserModel::insert(
+ 'katy',
+ array(
+ 'email' => 'katy@gmail.com',
+ 'name' => 'Katy',
+ 'age' => 22,
+ ),
+ null,
+ $this->cassandra2
+ );
+
+ $this->assertTrue($result);
+
+ $katy = UserModel::load('katy');
+
+ $this->assertNotNull($katy);
+ $this->assertEquals($katy->email, 'katy@gmail.com');
+ $this->assertEquals($katy->name, 'Katy');
+ $this->assertEquals($katy->age, 22);
+ $this->assertEquals($katy->key(), 'katy');
+ }
+
+ public function testEntryCanBeUpdated() {
+ $this->assertNull(UserModel::load('dave'));
+
+ $result = UserModel::insert('dave', array(
+ 'email' => 'dave@gmail.com',
+ 'name' => 'Dave',
+ 'age' => 33,
+ ));
+
+ $this->assertTrue($result);
+
+ $dave = UserModel::load('dave');
+
+ $this->assertNotNull($dave);
+ $this->assertEquals($dave->email, 'dave@gmail.com');
+ $this->assertEquals($dave->name, 'Dave');
+ $this->assertEquals($dave->age, 33);
+ $this->assertEquals($dave->key(), 'dave');
+
+ $dave->email = 'dave@hotmail.com';
+ $dave->save();
+
+ $dave2 = UserModel::load('dave');
+
+ $this->assertNotNull($dave2);
+ $this->assertEquals($dave2->email, 'dave@hotmail.com');
+ $this->assertEquals($dave2->name, 'Dave');
+ $this->assertEquals($dave2->age, 33);
+ $this->assertEquals($dave2->key(), 'dave');
+
+ $dave2->key('david');
+ $dave2->name = 'David';
+ $dave2->save();
+
+ $dave3 = UserModel::load('dave');
+
+ $this->assertNotNull($dave3);
+ $this->assertEquals($dave3->email, 'dave@hotmail.com');
+ $this->assertEquals($dave3->name, 'Dave');
+ $this->assertEquals($dave3->age, 33);
+ $this->assertEquals($dave3->key(), 'dave');
+
+ $dave4 = UserModel::load('david');
+
+ $this->assertNotNull($dave4);
+ $this->assertEquals($dave4->email, 'dave@hotmail.com');
+ $this->assertEquals($dave4->name, 'David');
+ $this->assertEquals($dave4->age, 33);
+ $this->assertEquals($dave4->key(), 'david');
+ }
+
+ public function testRowsCanBeRemoved() {
+ UserModel::insert('dave', array(
+ 'email' => 'dave@gmail.com',
+ 'name' => 'Dave',
+ 'age' => 33,
+ ));
+
+ $dave = UserModel::load('dave');
+ $this->assertEquals($dave->email, 'dave@gmail.com');
+ $this->assertEquals($dave->name, 'Dave');
+ $this->assertEquals($dave->age, 33);
+
+ UserModel::remove('dave');
+
+ $dave2 = UserModel::load('dave');
+ $this->assertNull($dave2);
+ }
+
+ public function testRowsCanBeRemoved2() {
+ UserModel::insert('dave', array(
+ 'email' => 'dave@gmail.com',
+ 'name' => 'Dave',
+ 'age' => 33,
+ ));
+
+ $dave = UserModel::load('dave');
+ $this->assertEquals($dave->email, 'dave@gmail.com');
+ $this->assertEquals($dave->name, 'Dave');
+ $this->assertEquals($dave->age, 33);
+
+ $dave->delete();
+
+ $dave2 = UserModel::load('dave');
+ $this->assertNull($dave2);
+ }
+
+ /* not yet supported by CPCL
+ public function testColumnsCanBeRemoved() {
+ UserModel::insert('dave', array(
+ 'email' => 'dave@gmail.com',
+ 'name' => 'Dave',
+ 'age' => 33,
+ ));
+
+ $dave = UserModel::load('dave');
+ $this->assertEquals($dave->email, 'dave@gmail.com');
+ $this->assertEquals($dave->name, 'Dave');
+ $this->assertEquals($dave->age, 33);
+
+ $dave->delete(array('email', 'age'));
+
+ $dave2 = UserModel::load('dave');
+ $this->assertNotNull($dave2);
+ $this->assertNull($dave->email);
+ $this->assertEquals($dave->name, 'Dave');
+ $this->assertNull($dave->age);
+ }
+
+ public function testColumnsCanBeRemoved() {
+ UserModel::insert('dave', array(
+ 'email' => 'dave@gmail.com',
+ 'name' => 'Dave',
+ 'age' => 33,
+ 'sex' => 'male'
+ ));
+
+ UserModel::remove('dave', array('email', 'sex'));
+
+ $dave = UserModel::load('dave');
+ $this->assertEquals($dave->getData(), array(
+ 'name' => 'Dave',
+ 'age' => 33
+ ));
+ }
+
+ */
+}
View
278 test/CassandraTest.php
@@ -1,32 +1,32 @@
<?php
-require '../Cassandra.php';
+require_once '../Cassandra.php';
class CassandraTest extends PHPUnit_Framework_TestCase {
-
+
protected $servers;
-
+
/**
* @var Cassandra
*/
protected $cassandra;
-
+
protected static $setupComplete = false;
-
+
public function setup() {
if (function_exists('apc_clear_cache')) {
apc_clear_cache();
}
-
+
$this->servers = array(
array(
'host' => '127.0.0.1',
'port' => 9160
)
);
-
+
$this->cassandra = Cassandra::createInstance($this->servers);
-
+
if (!self::$setupComplete) {
try {
$this->cassandra->dropKeyspace('CassandraTest');
@@ -35,7 +35,7 @@ public function setup() {
$this->cassandra->setMaxCallRetries(5);
$this->cassandra->createKeyspace('CassandraTest');
$this->cassandra->useKeyspace('CassandraTest');
-
+
$this->cassandra->createStandardColumnFamily(
'CassandraTest',
'user',
@@ -58,7 +58,7 @@ public function setup() {
)
)
);
-
+
$this->cassandra->createSuperColumnFamily(
'CassandraTest',
'cities',
@@ -80,43 +80,43 @@ public function setup() {
1000,
0.5
);
-
+
self::$setupComplete = true;
} else {
$this->cassandra->useKeyspace('CassandraTest');
}
}
-
+
public function tearDown() {
unset($this->cassandra);
-
+
if (function_exists('apc_clear_cache')) {
apc_clear_cache();
}
}
-
+
public function testKeyspaceCanBeUpdated() {
try {
$this->cassandra->dropKeyspace('CassandraTest2');
} catch (Exception $e) {}
-
+
$this->cassandra->createKeyspace('CassandraTest2');
-
+
$this->assertEquals(array(
'column-families' => array(),
'name' => 'CassandraTest2',
'placement-strategy' => 'org.apache.cassandra.locator.SimpleStrategy',
'placement-strategy-options' => array('replication_factor' => 1),
'replication-factor' => 1,
), $this->cassandra->getKeyspaceSchema('CassandraTest2', false));
-
+
$this->cassandra->updateKeyspace(
'CassandraTest2',
1,
Cassandra::PLACEMENT_NETWORK,
array('DC1' => 2, 'DC2' => 2)
);
-
+
$this->assertEquals(array(
'column-families' => array(),
'name' => 'CassandraTest2',
@@ -128,31 +128,31 @@ public function testKeyspaceCanBeUpdated() {
'replication-factor' => null
), $this->cassandra->getKeyspaceSchema('CassandraTest2', false));
}
-
+
/**
* @expectedException CassandraColumnFamilyNotFoundException
*/
public function testExceptionThrownOnGetUnexistingSchema() {
$cf = new CassandraColumnFamily($this->cassandra, 'foobar');
$cf->getSchema();
}
-
+
/**
* @expectedException CassandraConnectionClosedException
*/
public function testGetClientThrowsExceptionIfConnectionClosed() {
$connection = $this->cassandra->getConnection();
$client = $connection->getClient();
-
+
if (!($client instanceof cassandra_CassandraClient)) {
$this->fail('Instance of CassandraClient expected');
}
-
+
$this->cassandra->closeConnections();
-
+
$connection->getClient();
}
-
+
/**
* @expectedException CassandraUnsupportedException
*/
@@ -181,7 +181,7 @@ public function testExceptionThrownUsingIndexesOnSuperColumns() {
0.5
);
}
-
+
/**
* @expectedException CassandraInvalidRequestException
*/
@@ -193,7 +193,7 @@ public function testExceptionThrownOnInvalidColumnsRequest() {
'email'
);
}
-
+
/**
* @expectedException CassandraInvalidRequestException
*/
@@ -202,7 +202,7 @@ public function testExceptionThrownOnInvalidColumnsRequest2() {
array(array('age', -1, 20))
);
}
-
+
/**
* @expectedException CassandraInvalidRequestException
*/
@@ -214,7 +214,7 @@ public function testExceptionThrownOnInvalidColumnsRequest3() {
'email'
);
}
-
+
/**
* @expectedException CassandraInvalidRequestException
*/
@@ -228,7 +228,7 @@ public function testExceptionThrownOnInvalidColumnsRequest4() {
'email'
);
}
-
+
/**
* @expectedException CassandraInvalidRequestException
*/
@@ -240,7 +240,7 @@ public function testExceptionThrownOnInvalidColumnsRequest5() {
'email'
);
}
-
+
/**
* @expectedException CassandraInvalidRequestException
*/
@@ -252,40 +252,40 @@ public function testExceptionThrownOnInvalidColumnsRequest6() {
'email'
);
}
-
+
public function testFramedOrBufferedTransportIsAvailable() {
$framed = new CassandraConnection('127.0.0.1', 9160, true, 1000, 1000);
$buffered = new CassandraConnection('127.0.0.1', 9160, false);
-
+
if (!($framed->getTransport() instanceof TFramedTransport)) {
$this->fail('Expected framed transport');
}
-
+
if (!($buffered->getTransport() instanceof TBufferedTransport)) {
$this->fail('Expected buffered transport');
}
}
-
+
public function testCanAuthenticateToKeyspace() {
$this->cassandra->useKeyspace('CassandraTest', 'admin', 'qwerty');
$this->cassandra->getKeyspaceSchema();
}
-
+
/**
* @expectedException CassandraSettingKeyspaceFailedException
*/
public function testExceptionThrownIfUnabletToSelectKeyspace() {
$this->cassandra->useKeyspace('foobar');
$this->cassandra->getKeyspaceSchema();
}
-
+
public function testClusterKnowsActiveKeyspace() {
$this->assertEquals(
$this->cassandra->getCluster()->getCurrentKeyspace(),
'CassandraTest'
);
}
-
+
public function testClusterKnowsServerList() {
$servers = array(
array(
@@ -296,34 +296,34 @@ public function testClusterKnowsServerList() {
'receive-timeout-ms' => null,
)
);
-
+
$this->assertEquals(
$this->cassandra->getCluster()->getServers(),
$servers
);
}
-
+
public function testReturnsServerVersion() {
$this->assertNotEmpty($this->cassandra->getVersion());
}
-
+
/**
* @expectedException CassandraConnectionFailedException
*/
public function testClusterGetConnectionThrowsExceptionIfNoServersAdded() {
$cluster = new CassandraCluster();
-
+
$cluster->getConnection();
}
-
+
public function testClosedConnectionsAreRemovedFromCluster() {
$connection = $this->cassandra->getConnection();
-
+
$connection->close();
-
+
$this->cassandra->getConnection();
}
-
+
/**
* @expectedException CassandraConnectionFailedException
*/
@@ -334,39 +334,39 @@ public function testExceptionIsThrownIfConnectionFails() {
'port' => 9161
)
);
-
+
$cassandra = Cassandra::createInstance($servers, 'other');
$cassandra->getConnection();
}
-
+
public function testGetInstanceReturnsInstanceByName() {
$this->assertEquals(
Cassandra::getInstance(),
$this->cassandra
);
-
+
$this->assertEquals(
Cassandra::getInstance('main'),
$this->cassandra
);
}
-
+
/**
* @expectedException CassandraInvalidRequestException
*/
public function testGetInstanceThrowsExceptionIfNameNotFound() {
Cassandra::getInstance('foobar');
}
-
+
public function testUseKeyspaceCanSkipAuthOnSecondCall() {
$this->cassandra->useKeyspace('CassandraTest', 'admin', 'qwerty');
$this->cassandra->useKeyspace('CassandraTest');
}
-
+
public function testMaxCallRetriesCanBeSet() {
$this->cassandra->setMaxCallRetries(3);
$this->cassandra->setDefaultColumnCount(50);
-
+
try {
$this->cassandra->call('foobar');
} catch (Exception $e) {
@@ -376,42 +376,42 @@ public function testMaxCallRetriesCanBeSet() {
);
}
}
-
+
/**
* @expectedException CassandraInvalidRequestException
*/
public function testExceptionIsThrownIfKeyspaceNotSetOnCall() {
$cassandra = Cassandra::createInstance($this->servers, 'other');
-
+
$cassandra->call('get');
}
-
+
public function testReturnsKeyspaceDescription() {
$info = $this->cassandra->describeKeyspace();
-
+
$this->assertEquals(
$info->name,
'CassandraTest'
);
}
-
+
public function testReturnsKeyspaceSchema() {
$info = $this->cassandra->getKeyspaceSchema('CassandraTest', true);
$this->cassandra->getKeyspaceSchema(); // coverage
-
+
$this->assertEquals(
$info['column-families']['user']['name'],
'user'
);
}
-
+
public function testEscapesSpecialTokens() {
$this->assertEquals(
Cassandra::escape('./:/,/-/|'),
'\\./\\:/\\,/\\-/\\|'
);
}
-
+
public function testStoresAndFetchesStandardColumns() {
$this->cassandra->set(
'user.foobar',
@@ -421,7 +421,7 @@ public function testStoresAndFetchesStandardColumns() {
'age' => 23
)
);
-
+
$this->cassandra->set(
'user.'.'foo.bar',
array(
@@ -430,7 +430,7 @@ public function testStoresAndFetchesStandardColumns() {
'age' => 18
)
);
-
+
$this->cassandra->set(
'user.'.'foo.bar',
array(
@@ -439,7 +439,7 @@ public function testStoresAndFetchesStandardColumns() {
'age' => 18
)
);
-
+
$this->assertEquals(
array(
'email' => 'foobar@gmail.com',
@@ -448,7 +448,7 @@ public function testStoresAndFetchesStandardColumns() {
),
$this->cassandra->get('user.foobar')
);
-
+
$this->assertEquals(
array(
'email' => 'other@gmail.com',
@@ -457,30 +457,30 @@ public function testStoresAndFetchesStandardColumns() {
),
$this->cassandra->get('user.'.Cassandra::escape('foo.bar'))
);
-
+
$this->assertEquals(
array(
'email' => 'foobar@gmail.com'
),
$this->cassandra->get('user.foobar:email')
);
-
+
$this->assertEquals(
array(
'email' => 'foobar@gmail.com',
'age' => 23
),
$this->cassandra->get('user.foobar:email,age')
);
-
+
$this->assertEquals(
array(
'email' => 'foobar@gmail.com',
'age' => 23
),
$this->cassandra->get('user.foobar:email, age')
);
-
+
$this->assertEquals(
array(
'email' => 'foobar@gmail.com',
@@ -489,7 +489,7 @@ public function testStoresAndFetchesStandardColumns() {
),
$this->cassandra->get('user.foobar:age-name')
);
-
+
$this->assertEquals(
array(
'email' => 'foobar@gmail.com',
@@ -498,40 +498,40 @@ public function testStoresAndFetchesStandardColumns() {
),
$this->cassandra->get('user.foobar:a-o')
);
-
+
$this->assertEquals(
array(
'email' => 'foobar@gmail.com',
'age' => 23
),
$this->cassandra->get('user.foobar:a-f')
);
-
+
$this->assertEquals(
array(
'email' => 'foobar@gmail.com',
'age' => 23
),
$this->cassandra->get('user.foobar:a-z|2')
);
-
-
+
+
$this->assertEquals(
array(
'age' => 23,
'email' => 'foobar@gmail.com'
),
$this->cassandra->get('user.foobar|2')
);
-
+
$this->assertEquals(
array(
'name' => 'John Smith',
'email' => 'foobar@gmail.com',
),
$this->cassandra->get('user.foobar:z-a|2R')
);
-
+
$this->assertEquals(
array(
'name' => 'John Smith',
@@ -540,7 +540,7 @@ public function testStoresAndFetchesStandardColumns() {
$this->cassandra->get('user.foobar|2R')
);
}
-
+
public function testRemovesKeys() {
$this->cassandra->set(
'user.removable',
@@ -550,7 +550,7 @@ public function testRemovesKeys() {
'age' => 44
)
);
-
+
$this->assertEquals(
array(
'email' => 'john@smith.com',
@@ -559,12 +559,12 @@ public function testRemovesKeys() {
),
$this->cassandra->get('user.removable')
);
-
+
$this->cassandra->remove('user.removable');
-
+
$this->assertNull($this->cassandra->get('user.removable'));
}
-
+
public function testRemovesKeys2() {
$this->cassandra->set(
'user.removable',
@@ -574,7 +574,7 @@ public function testRemovesKeys2() {
'age' => 44
)
);
-
+
$this->assertEquals(
array(
'email' => 'john@smith.com',
@@ -583,12 +583,12 @@ public function testRemovesKeys2() {
),
$this->cassandra->get('user.removable')
);
-
+
$this->cassandra->cf('user')->remove('removable');
-
+
$this->assertNull($this->cassandra->get('user.removable'));
}
-
+
public function testRemovesColumn() {
$this->cassandra->set(
'user.removable',
@@ -598,7 +598,7 @@ public function testRemovesColumn() {
'age' => 44
)
);
-
+
$this->assertEquals(
array(
'email' => 'john@smith.com',
@@ -607,9 +607,9 @@ public function testRemovesColumn() {
),
$this->cassandra->get('user.removable')
);
-
+
$this->cassandra->remove('user.removable:email');
-
+
$this->assertEquals(
array(
'name' => 'John Smith',
@@ -618,7 +618,7 @@ public function testRemovesColumn() {
$this->cassandra->get('user.removable')
);
}
-
+
public function testSpecialCharactersAreProperlyUnpacked() {
$this->cassandra->set(
'user.special',
@@ -628,7 +628,7 @@ public function testSpecialCharactersAreProperlyUnpacked() {
'age' => 41
)
);
-
+
$this->assertEquals(
array(
'email' => 'Jèna@grùber.com',
@@ -638,39 +638,39 @@ public function testSpecialCharactersAreProperlyUnpacked() {
$this->cassandra->get('user.special')
);
}
-
+
/**
* @expectedException CassandraInvalidPatternException
*/
public function testInvalidGetPatternThrowsException() {
$this->cassandra->get('foo#bar');
}
-
+
/**
* @expectedException CassandraInvalidPatternException
*/
public function testInvalidGetPatternThrowsException2() {
$this->cassandra->get('user.foobar:a-f-z');
}
-
+
/**
* @expectedException CassandraInvalidPatternException
*/
public function testSetWithoutColumnFamilyThrowsException() {
$this->cassandra->set('foo', array('foo' => 'bar'));
}
-
+
/**
* @expectedException CassandraInvalidRequestException
*/
public function testGetWithColumnsAndColumnRangeThrowsException() {
$this->cassandra->cf('user')->get('test', array('col1', 'col2'), 'start');
}
-
+
public function testGettingNonexistingKeyReturnsNull() {
$this->assertNull($this->cassandra->get('user.x'));
}
-
+
public function testGetAllReturnsAllColumns() {
$this->assertEquals(
array(
@@ -680,7 +680,7 @@ public function testGetAllReturnsAllColumns() {
),
$this->cassandra->cf('user')->getAll('foobar')
);
-
+
$this->cassandra->set(
'cities.Estonia',
array(
@@ -696,7 +696,7 @@ public function testGetAllReturnsAllColumns() {
)
)
);
-
+
$this->assertEquals(
array(
'population' => '98589',
@@ -705,15 +705,15 @@ public function testGetAllReturnsAllColumns() {
),
$this->cassandra->cf('cities')->getAll('Estonia', 'Tartu')
);
-
+
$this->assertEquals(
array(
'email' => 'foobar@gmail.com',
'name' => 'John Smith'
),
$this->cassandra->cf('user')->getColumns('foobar', array('email', 'name'))
);
-
+
$this->assertEquals(
array(
'age' => 23,
@@ -722,7 +722,7 @@ public function testGetAllReturnsAllColumns() {
$this->cassandra->cf('user')->getColumnRange('foobar', 'age', 'email')
);
}
-
+
public function testStoresAndFetchesSuperColumns() {
$this->cassandra->set(
'cities.Estonia',
@@ -739,7 +739,7 @@ public function testStoresAndFetchesSuperColumns() {
)
)
);
-
+
$this->assertEquals(
array(
'population' => '98589',
@@ -748,15 +748,15 @@ public function testStoresAndFetchesSuperColumns() {
),
$this->cassandra->get('cities.Estonia.Tartu')
);
-
+
$this->assertEquals(
array(
'population' => '98589',
'size' => 'medium'
),
$this->cassandra->get('cities.Estonia.Tartu:population,size')
);
-
+
$this->assertEquals(
array(
'Tallinn' => array(
@@ -773,7 +773,7 @@ public function testStoresAndFetchesSuperColumns() {
$this->cassandra->get('cities.Estonia[]')
);
}
-
+
public function testDataCanBeRequestedUsingIndexes() {
$this->cassandra->set(
'user.john',
@@ -783,7 +783,7 @@ public function testDataCanBeRequestedUsingIndexes() {
'age' => 34
)
);
-
+
$this->cassandra->set(
'user.jane',
array(
@@ -792,7 +792,7 @@ public function testDataCanBeRequestedUsingIndexes() {
'age' => 24
)
);
-
+
$this->cassandra->set(
'user.chuck',
array(
@@ -801,7 +801,7 @@ public function testDataCanBeRequestedUsingIndexes() {
'age' => 34
)
);
-
+
$this->cassandra->set(
'user.sheldon',
array(
@@ -810,7 +810,7 @@ public function testDataCanBeRequestedUsingIndexes() {
'age' => 34
)
);
-
+
$this->assertEquals(
array(
'chuck' => array(
@@ -831,7 +831,7 @@ public function testDataCanBeRequestedUsingIndexes() {
),
$this->cassandra->cf('user')->getWhere(array('age' => 34))->getAll()
);
-
+
$this->assertEquals(
array(
'chuck' => array(
@@ -863,62 +863,62 @@ public function testDataCanBeRequestedUsingIndexes() {
)->getAll()
);
}
-
+
public function testGetWhereCanReturnManyRows() {
for ($i = 0; $i < 2010; $i++) {
$this->cassandra->set(
'user.test'.$i,
array(
'email' => 'test'.$i.'@test.com',
'name' => 'Test #'.$i,
- 'age' => 50
+ 'age' => 51
)
);
}
-
+
$results = array();
- $iterator = $this->cassandra->cf('user')->getWhere(array('age' => 50));
-
+ $iterator = $this->cassandra->cf('user')->getWhere(array('age' => 51));
+
foreach ($iterator as $key => $row) {
$results[$key] = $row;
}
$this->assertEquals(2010, count($results));
}
-
+
public function testGetMultipleReturnsSeveralKeysData() {
$keys = array();
$expected = array();
-
+
for ($i = 0; $i < 501; $i++) {
$key = 'user.test'.$i;
-
+
$userData = array(
'email' => 'test'.$i.'@test.com',
'name' => 'Test #'.$i,
'age' => 70
);
-
+
$this->cassandra->set(
$key,
$userData
);
-
+
$expected['test'.$i] = $userData;
$keys[] = 'test'.$i;
}
-
+
$this->assertEquals(array(
'email' => 'test0@test.com',
'name' => 'Test #0',
'age' => 70
), $this->cassandra->get('user.test0'));
-
+
$data = $this->cassandra->cf('user')->getMultiple($keys);
-
+
$this->assertEquals($expected, $data);
}
-
+
public function testRowColumnsCanBeCounted() {
$this->cassandra->set(
'user.sheldon',
@@ -928,13 +928,13 @@ public function testRowColumnsCanBeCounted() {
'age' => 34
)
);
-
+
$this->assertEquals(
3,
$this->cassandra->cf('user')->getColumnCount('sheldon')
);
}
-
+
public function testRowColumnsCanBeCounted2() {
$this->cassandra->set(
'user.sheldon',
@@ -944,7 +944,7 @@ public function testRowColumnsCanBeCounted2() {
'age' => 34
)
);
-
+
$this->cassandra->set(
'user.john',
array(
@@ -954,7 +954,7 @@ public function testRowColumnsCanBeCounted2() {
'profession' => 'actor'
)
);
-
+
$this->assertEquals(
array(
'sheldon' => 3,
@@ -963,61 +963,61 @@ public function testRowColumnsCanBeCounted2() {
$this->cassandra->cf('user')->getColumnCounts(array('sheldon', 'john'))
);
}
-
+
/* Run only with partitioner:
* org.apache.cassandra.dht.CollatingOrderPreservingPartitioner
public function testKeysCanBeFetchedByRange() {
$expected = array();
$expected2 = array();
-
+
$this->cassandra->truncate('user');
-
+
for ($i = ord('a'); $i < ord('z'); $i++) {
$testData = array(
'age' => 50,
'email' => 'test'.$i.'@test.com',
'name' => 'Test #'.$i
);
-
+
$this->cassandra->set(
'user.test-'.chr($i),
$testData
);
-
+