Permalink
Browse files

Improved the library and tests, should be mostly complete now. Will a…

…dd example and comments next.
  • Loading branch information...
1 parent 1560e10 commit 677fc5ac6de0767a68404d5a349ea425c1ecfd57 @kallaspriit committed Jun 27, 2011
Showing with 176 additions and 17 deletions.
  1. +25 −11 Cassandra.php
  2. +151 −6 test/CassandraTest.php
View
@@ -92,7 +92,7 @@ public function useKeyspace(
) {
$attempts = 3;
$success = false;
-
+
while($attempts-- > 0 && !$success) {
try {
$this->client->set_keyspace($keyspace);
@@ -196,6 +196,8 @@ public function useKeyspace($keyspace, $username = null, $password = null) {
$this->username = $username;
$this->password = $password;
+ $this->getConnection();
+
foreach ($this->connections as $connection) {
$connection->useKeyspace(
$keyspace,
@@ -785,6 +787,10 @@ public function createColumnFamily(
return $this->call('system_add_column_family', $def);
}
+ public function truncate($columnFamilyName) {
+ return $this->call('truncate', $columnFamilyName);
+ }
+
public static function getKeyspaceRequiredMethods() {
if (self::$keyspaceRequiredMethods === null) {
self::$keyspaceRequiredMethods = array(
@@ -1973,6 +1979,7 @@ public function __construct(
$this->bufferSize = $bufferSize;
$this->originalStartKey = $startKey;
$this->nextStartKey = $startKey;
+ $this->buffer = null;
if ($rowCountLimit !== null) {
$this->bufferSize = min($rowCountLimit, $bufferSize);
@@ -2061,8 +2068,15 @@ public function valid() {
public function getAll() {
$results = array();
- foreach ($this as $key => $row) {
- $results[$key] = $row;
+ $this->rewind();
+
+ while ($this->valid()) {
+ $key = $this->key();
+ $value = $this->current();
+
+ $results[$key] = $value;
+
+ $this->next();
}
return $results;
@@ -2187,11 +2201,11 @@ protected function updateBuffer() {
}
}
-class CassandraMaxRetriesException extends Exception {};
-class CassandraConnectionClosedException extends Exception {};
-class CassandraConnectionFailedException extends Exception {};
-class CassandraSettingKeyspaceFailedException extends Exception {};
-class CassandraColumnFamilyNotFoundException extends Exception {};
-class CassandraInvalidRequestException extends Exception {};
-class CassandraInvalidPatternException extends Exception {};
-class CassandraUnsupportedException extends Exception {};
+class CassandraMaxRetriesException extends Exception {};
+class CassandraConnectionClosedException extends Exception {};
+class CassandraConnectionFailedException extends Exception {};
+class CassandraSettingKeyspaceFailedException extends Exception {};
+class CassandraColumnFamilyNotFoundException extends Exception {};
+class CassandraInvalidRequestException extends Exception {};
+class CassandraInvalidPatternException extends Exception {};
+class CassandraUnsupportedException extends Exception {};
View
@@ -148,6 +148,106 @@ public function testGetClientThrowsExceptionIfConnectionClosed() {
$connection->getClient();
}
+ /**
+ * @expectedException CassandraUnsupportedException
+ */
+ public function testExceptionThrownUsingIndexesOnSuperColumns() {
+ $this->c->createSuperColumnFamily(
+ 'CassandraTest',
+ 'cities2',
+ array(
+ array(
+ 'name' => 'population',
+ 'type' => Cassandra::TYPE_INTEGER,
+ 'index-type' => Cassandra::INDEX_KEYS,
+ 'index-name' => 'PopulationIdx'
+ ),
+ array(
+ 'name' => 'comment',
+ 'type' => Cassandra::TYPE_UTF8
+ )
+ ),
+ Cassandra::TYPE_UTF8,
+ Cassandra::TYPE_UTF8,
+ Cassandra::TYPE_UTF8,
+ 'Capitals supercolumn test',
+ 1000,
+ 1000,
+ 0.5
+ );
+ }
+
+ /**
+ * @expectedException CassandraInvalidRequestException
+ */
+ public function testExceptionThrownOnInvalidColumnsRequest() {
+ $this->c->cf('user')->getWhere(
+ array('age' => 20),
+ array('name', 'email'),
+ 'name',
+ 'email'
+ );
+ }
+
+ /**
+ * @expectedException CassandraInvalidRequestException
+ */
+ public function testExceptionThrownOnInvalidColumnsRequest2() {
+ $this->c->cf('user')->getWhere(
+ array(array('age', -1, 20))
+ );
+ }
+
+ /**
+ * @expectedException CassandraInvalidRequestException
+ */
+ public function testExceptionThrownOnInvalidColumnsRequest3() {
+ $this->c->cf('user')->getMultiple(
+ array('user1', 'user2'),
+ array('name', 'email'),
+ 'name',
+ 'email'
+ );
+ }
+
+ /**
+ * @expectedException CassandraInvalidRequestException
+ */
+ public function testExceptionThrownOnInvalidColumnsRequest4() {
+ $this->c->cf('user')->getRange(
+ 'user1',
+ 'userN',
+ 100,
+ array('name', 'email'),
+ 'name',
+ 'email'
+ );
+ }
+
+ /**
+ * @expectedException CassandraInvalidRequestException
+ */
+ public function testExceptionThrownOnInvalidColumnsRequest5() {
+ $this->c->cf('user')->getColumnCount(
+ 'sheldon',
+ array('name', 'email'),
+ 'name',
+ 'email'
+ );
+ }
+
+ /**
+ * @expectedException CassandraInvalidRequestException
+ */
+ public function testExceptionThrownOnInvalidColumnsRequest6() {
+ $this->c->cf('user')->getColumnCounts(
+ array('sheldon', 'john'),
+ array('name', 'email'),
+ 'name',
+ 'email'
+ );
+ }
+
public function testFramedOrBufferedTransportIsAvailable() {
$framed = new CassandraConnection('127.0.0.1', 9160, true, 1000, 1000);
$buffered = new CassandraConnection('127.0.0.1', 9160, false);
@@ -726,6 +826,22 @@ public function testRowColumnsCanBeCounted() {
)
);
+ $this->assertEquals(
+ 3,
+ $this->c->cf('user')->getColumnCount('sheldon')
+ );
+ }
+
+ public function testRowColumnsCanBeCounted2() {
+ $this->c->set(
+ 'user.sheldon',
+ array(
+ 'email' => 'sheldon@cooper.com',
+ 'name' => 'Sheldon Cooper',
+ 'age' => 34
+ )
+ );
+
$this->c->set(
'user.john',
array(
@@ -745,16 +861,19 @@ public function testRowColumnsCanBeCounted() {
);
}
- /* Run only with
- * partitioner: org.apache.cassandra.dht.CollatingOrderPreservingPartitioner
+ //* Run only with
+ // * partitioner: org.apache.cassandra.dht.CollatingOrderPreservingPartitioner
public function testKeysCanBeFetchedByRange() {
$expected = array();
+ $expected2 = array();
+
+ $this->c->truncate('user');
for ($i = ord('a'); $i < ord('z'); $i++) {
$testData = array(
+ 'age' => 50,
'email' => 'test'.$i.'@test.com',
- 'name' => 'Test #'.$i,
- 'age' => 50
+ 'name' => 'Test #'.$i
);
$this->c->set(
@@ -768,8 +887,34 @@ public function testKeysCanBeFetchedByRange() {
}
$data = $this->c->cf('user')->getRange('test-'.chr(101), 'test-'.(chr(107)));
+ $results = $data->getAll();
+
+ $this->assertEquals($expected, $results);
+ }
+
+ public function testKeysCanBeFetchedByRange2() {
+ $expected = array();
+
+ $this->c->truncate('user');
+
+ for ($i = ord('a'); $i < ord('z'); $i++) {
+ $testData = array(
+ 'age' => 50,
+ 'email' => 'test'.$i.'@test.com',
+ 'name' => 'Test #'.$i
+ );
+
+ $this->c->set(
+ 'user.test-'.chr($i),
+ $testData
+ );
+ $expected['test-'.chr($i)] = $testData;
+ }
- $this->assertEquals($expected, $data->getAll());
+ $data = $this->c->cf('user')->getRange();
+ $results = $data->getAll();
+
+ $this->assertEquals($expected, $results);
}
- */
+
}

0 comments on commit 677fc5a

Please sign in to comment.