kallaspriit / Cassandra-PHP-Client-Library

Cassandra PHP-based client library for managing and querying your Cassandra cluster
http://cassandra-php-client-library.com
103 stars 25 forks source link

No delete available? #3

Closed LordStephen closed 12 years ago

LordStephen commented 13 years ago

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 commented 13 years ago

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 commented 12 years ago

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 commented 12 years ago

Will add these soon, thanks :)