From 27c9ce471377ffb5b900030f4074f7249aae737b Mon Sep 17 00:00:00 2001 From: patricioe Date: Wed, 10 Nov 2010 14:56:08 -0800 Subject: [PATCH] Add addDeletion that allows a specific clock and test --- .../cassandra/model/MutatorImpl.java | 21 ++++++++++----- .../hector/api/mutation/Mutator.java | 27 +++++++++++++++++++ .../cassandra/model/MutatorTest.java | 20 +++++++++++++- 3 files changed, 60 insertions(+), 8 deletions(-) diff --git a/src/main/java/me/prettyprint/cassandra/model/MutatorImpl.java b/src/main/java/me/prettyprint/cassandra/model/MutatorImpl.java index 43f0fdab4..796b6e397 100644 --- a/src/main/java/me/prettyprint/cassandra/model/MutatorImpl.java +++ b/src/main/java/me/prettyprint/cassandra/model/MutatorImpl.java @@ -111,19 +111,26 @@ public Mutator addInsertion(K key, String cf, HSuperColumn 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 Mutator addDeletion(K key, String cf, N columnName, Serializer 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 Mutator addDeletion(K key, String cf, N columnName, Serializer 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. diff --git a/src/main/java/me/prettyprint/hector/api/mutation/Mutator.java b/src/main/java/me/prettyprint/hector/api/mutation/Mutator.java index 0ca653531..aed52ecfe 100644 --- a/src/main/java/me/prettyprint/hector/api/mutation/Mutator.java +++ b/src/main/java/me/prettyprint/hector/api/mutation/Mutator.java @@ -47,8 +47,35 @@ MutationResult subDelete(final K key, final String cf, final SN supercol */ Mutator addInsertion(K key, String cf, HSuperColumn 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 column name type + * @param key row key + * @param cf column family + * @param columnName column family name + * @param nameSerializer a name serializer + * @return a mutator + */ Mutator addDeletion(K key, String cf, N columnName, Serializer 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 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 + */ + Mutator addDeletion(K key, String cf, N columnName, Serializer nameSerializer, long clock); + /** * Batch executes all mutations scheduled to this Mutator instance by addInsertion, addDeletion etc. * May throw a HectorException which is a RuntimeException. diff --git a/src/test/java/me/prettyprint/cassandra/model/MutatorTest.java b/src/test/java/me/prettyprint/cassandra/model/MutatorTest.java index 9099fab9f..d3d88c5e2 100644 --- a/src/test/java/me/prettyprint/cassandra/model/MutatorTest.java +++ b/src/test/java/me/prettyprint/cassandra/model/MutatorTest.java @@ -1,12 +1,14 @@ 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; @@ -14,7 +16,7 @@ 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; @@ -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; @@ -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> 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) {