Skip to content
This repository has been archived by the owner on Feb 27, 2023. It is now read-only.

Commit

Permalink
Add addDeletion that allows a specific clock and test
Browse files Browse the repository at this point in the history
  • Loading branch information
patricioe committed Nov 10, 2010
1 parent bf6efae commit 27c9ce4
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 8 deletions.
21 changes: 14 additions & 7 deletions src/main/java/me/prettyprint/cassandra/model/MutatorImpl.java
Expand Up @@ -111,19 +111,26 @@ public <SN,N,V> Mutator<K> addInsertion(K key, String cf, HSuperColumn<SN,N,V> s
}

/**
* Adds a Deletion to the underlying batch_mutate call. The columnName argument can be null
* in which case Deletion is created with only the Clock, resulting in the whole row being deleted
* {@inheritDoc}
*/
@Override
public <N> Mutator<K> addDeletion(K key, String cf, N columnName, Serializer<N> nameSerializer) {
SlicePredicate sp = new SlicePredicate();
sp.addToColumn_names(nameSerializer.toByteBuffer(columnName));
Deletion d = columnName != null ? new Deletion(keyspace.createClock()).setPredicate(sp)
: new Deletion(keyspace.createClock());
getPendingMutations().addDeletion(key, Arrays.asList(cf), d);
addDeletion(key, cf, columnName, nameSerializer, keyspace.createClock());
return this;
}

/**
* {@inheritDoc}
*/
@Override
public <N> Mutator<K> addDeletion(K key, String cf, N columnName, Serializer<N> nameSerializer, long clock) {
SlicePredicate sp = new SlicePredicate();
sp.addToColumn_names(nameSerializer.toByteBuffer(columnName));
Deletion d = columnName != null ? new Deletion(clock).setPredicate(sp) : new Deletion(clock);
getPendingMutations().addDeletion(key, Arrays.asList(cf), d);
return this;
}

/**
* Batch executes all mutations scheduled to this Mutator instance by addInsertion, addDeletion etc.
* May throw a HectorException which is a RuntimeException.
Expand Down
27 changes: 27 additions & 0 deletions src/main/java/me/prettyprint/hector/api/mutation/Mutator.java
Expand Up @@ -47,8 +47,35 @@ <SN, N> MutationResult subDelete(final K key, final String cf, final SN supercol
*/
<SN, N, V> Mutator<K> addInsertion(K key, String cf, HSuperColumn<SN, N, V> sc);

/**
* Adds a Deletion to the underlying batch_mutate call. The columnName argument can be null
* in which case Deletion is created with only the Clock, in this case user defined,
* resulting in the whole row being deleted.
*
* @param <N> column name type
* @param key row key
* @param cf column family
* @param columnName column family name
* @param nameSerializer a name serializer
* @return a mutator
*/
<N> Mutator<K> addDeletion(K key, String cf, N columnName, Serializer<N> nameSerializer);

/**
* Adds a Deletion to the underlying batch_mutate call. The columnName argument can be null
* in which case Deletion is created with only the Clock, in this case user defined,
* resulting in the whole row being deleted.
*
* @param <N> column name type
* @param key row key
* @param cf column family
* @param columnName column family name
* @param nameSerializer a name serializer
* @param clock custom clock to use in the deletion
* @return a mutator
*/
<N> Mutator<K> addDeletion(K key, String cf, N columnName, Serializer<N> nameSerializer, long clock);

/**
* Batch executes all mutations scheduled to this Mutator instance by addInsertion, addDeletion etc.
* May throw a HectorException which is a RuntimeException.
Expand Down
20 changes: 19 additions & 1 deletion src/test/java/me/prettyprint/cassandra/model/MutatorTest.java
@@ -1,20 +1,22 @@
package me.prettyprint.cassandra.model;

import static me.prettyprint.hector.api.factory.HFactory.createColumn;
import static me.prettyprint.hector.api.factory.HFactory.createColumnQuery;
import static me.prettyprint.hector.api.factory.HFactory.createKeyspace;
import static me.prettyprint.hector.api.factory.HFactory.createMutator;
import static me.prettyprint.hector.api.factory.HFactory.createSuperColumn;
import static me.prettyprint.hector.api.factory.HFactory.getOrCreateCluster;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;

import java.util.ArrayList;
import java.util.List;

import me.prettyprint.cassandra.BaseEmbededServerSetupTest;
import me.prettyprint.cassandra.serializers.StringSerializer;
import me.prettyprint.cassandra.service.KeyspaceService;
import me.prettyprint.cassandra.service.ClockResolution;
import me.prettyprint.cassandra.utils.StringUtils;
import me.prettyprint.hector.api.Cluster;
import me.prettyprint.hector.api.Keyspace;
Expand All @@ -24,6 +26,7 @@
import me.prettyprint.hector.api.mutation.MutationResult;
import me.prettyprint.hector.api.mutation.Mutator;
import me.prettyprint.hector.api.query.ColumnQuery;
import me.prettyprint.hector.api.query.QueryResult;

import org.apache.cassandra.thrift.ColumnPath;
import org.junit.After;
Expand Down Expand Up @@ -114,10 +117,25 @@ public void testRowDeletion() {
}
MutationResult r = m.execute();

// Try to delete the row with key "k0" with a clock previous to the insertion.
// Cassandra will discard this operation.
m.addDeletion("k0", cf, null, se, (ClockResolution.MICROSECONDS.createClock() - 1000));

// Check that the delete was harmless
QueryResult<HColumn<String, String>> columnResult =
createColumnQuery(keyspace, se, se, se).setColumnFamily(cf).setKey("k0").
setName("name").execute();
assertEquals("value0", columnResult.get().getValue());

for (int i = 0; i < 5; i++) {
m.addDeletion("k" + i, cf, null, se);
}
m.execute();

// Check that the delete took place now
columnResult = createColumnQuery(keyspace, se, se, se).
setColumnFamily(cf).setKey("k0").setName("name").execute();
assertNull(columnResult.get());
}

private void assertColumnExists(String keyspace, String cf, String key, String column) {
Expand Down

0 comments on commit 27c9ce4

Please sign in to comment.