Skip to content

Commit

Permalink
Implemented #3 removing keys. Right now only entire entries and singl…
Browse files Browse the repository at this point in the history
…e columns can be removed, list and ranges of columns will follow.
  • Loading branch information
kallaspriit committed Oct 18, 2011
1 parent 91fc18f commit f6f98e7
Show file tree
Hide file tree
Showing 2 changed files with 192 additions and 2 deletions.
107 changes: 107 additions & 0 deletions Cassandra.php
Expand Up @@ -1189,6 +1189,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.
*
Expand Down Expand Up @@ -2037,6 +2069,50 @@ public function get(
return $this->parseSliceResponse($result);
}

/**
* 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.
*
Expand Down Expand Up @@ -2484,6 +2560,37 @@ public function createColumnParent($superColumnName = null) {
return $columnParent;
}

/**
* 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.
*
Expand Down
87 changes: 85 additions & 2 deletions test/CassandraTest.php
Expand Up @@ -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(
Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -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',
Expand Down

0 comments on commit f6f98e7

Please sign in to comment.