Skip to content

No delete available? #3

Closed
LordStephen opened this Issue Sep 11, 2011 · 3 comments

3 participants

@LordStephen

I've been using your lib for sometimes now, everything work perfectly, i just can't find a way to use the "del" command available on the CLI.

Is Truncate and Drop the only way to delete stuff?

Thx you for your work

@LordStephen

I implemented it, if you want to integrate the change.

I skipped the support for column range and other fancy stuff.
Here is the code


/**
* Remove 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);

    $columnPath = new cassandra_ColumnPath(array(
            'column_family' => $details['column-family'],
            'column' => $details['columns'],
            'super_column' => $details['super-column']
        ));

    if ($timestamp === null) {
        $timestamp = CassandraUtil::getTimestamp();
    }

    if ($consistency === null) {
        $consistency = Cassandra::CONSISTENCY_ONE;
    }

    $this->call('remove', $details['key'], $columnPath, $timestamp, $consistency);
}

@glenkim
glenkim commented Sep 29, 2011

Here's another patch that modifies CassandraColumnFamily::set() to delete columns with the value set to null.

Index: Cassandra.php
===================================================================
--- Cassandra.php   (revision 101233)
+++ Cassandra.php   (working copy)
@@ -2661,12 +2661,34 @@
            $timestamp = CassandraUtil::getTimestamp();
        }

+       $deleted_columns = array();
+       foreach ($columns as $key => $value) {
+           if (is_null($value)) {
+               $deleted_columns[] = $key;
+               unset($columns[$key]);
+           }
+           if (is_array($value)) {
+               foreach($value as $key2 => $value2) {
+                   if (is_null($value2)) {
+                       $deleted_columns[$key][] = $key2;
+                       unset($columns[$key][$key2]);
+                   }
+               }
+           }
+       }
+       
        $columnsOrSuperColumns = $this->createColumnsOrSuperColumns(
            $columns,
            $timestamp,
            $timeToLiveSeconds
        );

+       $deletions = $this->createDeletions(
+           $deleted_columns,
+           $timestamp,
+           $timeToLiveSeconds
+       );
+       
        $mutations = array();

        foreach ($columnsOrSuperColumns as $columnOrSuperColumn) {
@@ -2676,6 +2698,13 @@
            $mutations[] = $mutation;
        }

+       foreach ($deletions as $deletion) {
+           $mutation = new cassandra_Mutation();
+           $mutation->deletion = $deletion;
+               
+           $mutations[] = $mutation;
+       }
+       
        return $mutations;
    }

@@ -2739,6 +2768,57 @@
    }

    /**
+   * Creates a list of deletions.
+   *
+   * Returns a list of {@see cassandra_Deletion}
+   *
+   * This is a low-level method used internally but kept public in case you
+   * may need it.
+   *
+   * @param array $columns Array of columns
+   * @param integer $timestamp Operation timestamp
+   * @return array List of cassandra_Deletion
+   */
+   public function createDeletions(
+   array $columns,
+   $timestamp = null
+   ) {
+       if ($timestamp === null) {
+           $timestamp = CassandraUtil::getTimestamp();
+       }
+       
+       $results = array();
+       
+       $supercolumns = array();
+       foreach ($columns as $key => $value) {
+           if (is_array($value)) {
+               $supercolumns[$key] = $value;
+               unset($columns[$key]);
+           }
+       }
+       $supercolumns[''] = $columns; 
+
+       foreach ($supercolumns as $super_column => $columns) {
+           $super_column = ($super_column) ? $super_column : NULL;
+           $deletion = new cassandra_Deletion();
+           $slicePredicate = $this->createSlicePredicate(
+               $columns,
+               null,
+               null,
+               false,
+               100
+               );
+           $slicePredicate->slice_range = null;
+           $deletion->timestamp = $timestamp;
+           $deletion->super_column = $super_column;
+       
+           $deletion->predicate = $slicePredicate;
+           $results[] = $deletion;
+       }
+       return $results;
+   }
+
+   /**
     * Creates a list of {@see cassandra_Column} from list of columns and their
     * values.
     * 
@kallaspriit
Owner

Will add these soon, thanks :)

@kallaspriit kallaspriit added a commit that referenced this issue Oct 18, 2011
@kallaspriit Implemented #3 removing keys. Right now only entire entries and singl…
…e columns can be removed, list and ranges of columns will follow.
f6f98e7
@kallaspriit kallaspriit closed this Dec 4, 2011
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.