Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Implemented #3 removing keys. Right now only entire entries and singl…

…e columns can be removed, list and ranges of columns will follow.
  • Loading branch information...
commit f6f98e7de75a08d0ceabfa77640507762419d1b6 1 parent 91fc18f
@kallaspriit authored
Showing with 192 additions and 2 deletions.
  1. +107 −0 Cassandra.php
  2. +85 −2 test/CassandraTest.php
View
107 Cassandra.php
@@ -1190,6 +1190,38 @@ public function set($key, array $columns, $consistency = null) {
}
/**
+ * Removes a row or element of a row.
+ *
+ * Supported patterns:
+ * - family.key
+ * - family.key:col1 *
+ * - family.super.key:col1
+ *
+ * In all of the parts, the following characters shoudl be escaped (. etc)
+ * '.', ':', ',', '-', '|'.
+ *
+ * @param string $request The request string, see patterns above
+ * @param integer $consistency Consistency level to use
+ * @param integer $timestamp Optional timestamp to use.
+ * @throws Exception If something goes wrong
+ */
+ public function remove(
+ $request,
+ $consistency = null,
+ $timestamp = null
+ ) {
+ $details = $this->parseRequest($request);
+
+ $this->cf($details['column-family'])->remove(
+ $details['key'],
+ $details['columns'],
+ $details['super-column'],
+ $consistency,
+ $timestamp
+ );
+ }
+
+ /**
* Creates a new keyspace.
*
* Note that the replication factor means how many nodes hold a single
@@ -2038,6 +2070,50 @@ public function get(
}
/**
+ * Removes a row or element of a row.
+ *
+ * @param string $key Key to remove
+ * @param array|null Array of column names or null for all
+ * @param integer $consistency Consistency level to use
+ * @param integer $timestamp Optional timestamp to use
+ * @throws Exception If something goes wrong
+ */
+ public function remove(
+ $key,
+ array $columns = null,
+ $superColumn = null,
+ $consistency = null,
+ $timestamp = null
+ ) {
+ if (is_array($columns) && count($columns) > 1) {
+ throw new Exception(
+ 'Removing several columns is not yet supported'
+ );
+ }
+
+ $columnPath = $this->createColumnPath(
+ is_array($columns) && count($columns) == 1 ? $columns[0] : null,
+ $superColumn
+ );
+
+ if ($timestamp === null) {
+ $timestamp = CassandraUtil::getTimestamp();
+ }
+
+ if ($consistency === null) {
+ $consistency = $this->defaultWriteConsistency;
+ }
+
+ $this->cassandra->call(
+ 'remove',
+ $key,
+ $columnPath,
+ $timestamp,
+ $consistency
+ );
+ }
+
+ /**
* Fetch a set of rows filtered by secondary index where clause.
*
* To use this method, at least one of the columns present in the where
@@ -2485,6 +2561,37 @@ public function createColumnParent($superColumnName = null) {
}
/**
+ * Creates low-level Cassandra column-path definition.
+ *
+ * @param string $columnName Name of the column
+ * @param string $superColumName Name of the super column
+ * @return cassandra_ColumnPath Column path definition
+ */
+ public function createColumnPath($columnName, $superColumnName) {
+ $schema = $this->getSchema();
+
+ $columnPath = new cassandra_ColumnPath();
+
+ $columnPath->column_family = $this->name;
+
+ if ($columnName !== null) {
+ $columnPath->column = CassandraUtil::pack(
+ $columnName,
+ $this->getColumnNameType()
+ );
+ }
+
+ if ($superColumnName !== null) {
+ $columnPath->super_column = CassandraUtil::pack(
+ $superColumnName,
+ $schema['super-type']
+ );
+ }
+
+ return $columnPath;
+ }
+
+ /**
* Creates a slice predicate.
*
* This is a low-level method used internally but kept public in case you
View
87 test/CassandraTest.php
@@ -14,7 +14,9 @@ class CassandraTest extends PHPUnit_Framework_TestCase {
protected static $setupComplete = false;
public function setup() {
- apc_clear_cache();
+ if (function_exists('apc_clear_cache')) {
+ apc_clear_cache();
+ }
$this->servers = array(
array(
@@ -87,7 +89,10 @@ public function setup() {
public function tearDown() {
unset($this->cassandra);
- apc_clear_cache();
+
+ if (function_exists('apc_clear_cache')) {
+ apc_clear_cache();
+ }
}
public function testKeyspaceCanBeUpdated() {
@@ -536,6 +541,84 @@ public function testStoresAndFetchesStandardColumns() {
);
}
+ public function testRemovesKeys() {
+ $this->cassandra->set(
+ 'user.removable',
+ array(
+ 'email' => 'john@smith.com',
+ 'name' => 'John Smith',
+ 'age' => 44
+ )
+ );
+
+ $this->assertEquals(
+ array(
+ 'email' => 'john@smith.com',
+ 'name' => 'John Smith',
+ 'age' => 44
+ ),
+ $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',
+ array(
+ 'email' => 'john@smith.com',
+ 'name' => 'John Smith',
+ 'age' => 44
+ )
+ );
+
+ $this->assertEquals(
+ array(
+ 'email' => 'john@smith.com',
+ 'name' => 'John Smith',
+ 'age' => 44
+ ),
+ $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',
+ array(
+ 'email' => 'john@smith.com',
+ 'name' => 'John Smith',
+ 'age' => 44
+ )
+ );
+
+ $this->assertEquals(
+ array(
+ 'email' => 'john@smith.com',
+ 'name' => 'John Smith',
+ 'age' => 44
+ ),
+ $this->cassandra->get('user.removable')
+ );
+
+ $this->cassandra->remove('user.removable:email');
+
+ $this->assertEquals(
+ array(
+ 'name' => 'John Smith',
+ 'age' => 44
+ ),
+ $this->cassandra->get('user.removable')
+ );
+ }
+
public function testSpecialCharactersAreProperlyUnpacked() {
$this->cassandra->set(
'user.special',
Please sign in to comment.
Something went wrong with that request. Please try again.