Skip to content
Browse files

Merge pull request #555 from hector-client/row_clear

Row clear
  • Loading branch information...
2 parents d765355 + 5880d73 commit fcde06d1a1632ae9b2198caa3cc6057cee89af3e @zznate zznate committed
View
99 core/src/main/java/me/prettyprint/cassandra/service/ClearCounterRow.java
@@ -0,0 +1,99 @@
+/*
+ * To change this template, choose Tools | Templates
+ * and open the template in the editor.
+ */
+package me.prettyprint.cassandra.service;
+
+import me.prettyprint.hector.api.Keyspace;
+import me.prettyprint.hector.api.Serializer;
+import me.prettyprint.hector.api.beans.HCounterColumn;
+import me.prettyprint.hector.api.factory.HFactory;
+import me.prettyprint.hector.api.mutation.Mutator;
+import me.prettyprint.hector.api.query.SliceCounterQuery;
+
+/**
+ * Resets each counter counter column in the row to zero. This class is subject to the limitations of counter columns.
+ * See the <a href="http://wiki.apache.org/cassandra/Counters">Cassandra Wiki</a> for more information on the
+ * limitations.
+ *
+ * @author thrykol
+ */
+public class ClearCounterRow<K, N> {
+ private Keyspace keyspace;
+ private Serializer<K> keySerializer;
+ private Serializer<N> nameSerializer;
+ private K rowKey;
+ private String cf;
+ private int mutateInterval = 100;
+ private int count = 100;
+
+ public ClearCounterRow(Keyspace keyspace, Serializer<K> keySerializer, Serializer<N> nameSerializer) {
+ this.keyspace = keyspace;
+ this.keySerializer =keySerializer;
+ this.nameSerializer = nameSerializer;
+ }
+
+ /**
+ * Column family the row is part of.
+ *
+ * @param cf Column family name
+ * @return <code>this</code>
+ */
+ public ClearCounterRow setColumnFamily(String cf) {
+ this.cf = cf;
+ return this;
+ }
+
+ /**
+ * Key of the source row.
+ *
+ * @param rowKey Row key
+ * @return <code>this</code>
+ */
+ public ClearCounterRow setRowKey(K rowKey) {
+ this.rowKey = rowKey;
+ return this;
+ }
+
+ /**
+ * Set the mutate interval.
+ *
+ * @param interval Mutation interval
+ * @return <code>this</code>
+ */
+ public ClearCounterRow setMutateInterval(int interval) {
+ this.mutateInterval = interval;
+ return this;
+ }
+
+ /**
+ * Set the number of columns to retrieve per slice.
+ *
+ * @param count The number of columns to retrieve per slice
+ * @return &lt;this&gt;
+ */
+ public ClearCounterRow setCount(int count) {
+ this.count = count;
+ return this;
+ }
+ /**
+ * Clear the counter columns.
+ */
+ public void clear() {
+ Mutator<K> mutator = HFactory.createMutator(this.keyspace, this.keySerializer, new BatchSizeHint(1, this.mutateInterval));
+
+ SliceCounterQuery<K, N> query = HFactory.createCounterSliceQuery(this.keyspace, this.keySerializer, this.nameSerializer).
+ setColumnFamily(this.cf).
+ setKey(this.rowKey);
+
+ SliceCounterIterator<K, N> iterator =
+ new SliceCounterIterator<K, N>(query, null, (N) null, false, this.count);
+
+ while(iterator.hasNext()) {
+ HCounterColumn<N> column = iterator.next();
+ mutator.incrementCounter(this.rowKey, this.cf, column.getName(), column.getValue() * -1);
+ }
+
+ mutator.execute();
+ }
+}
View
69 core/src/test/java/me/prettyprint/cassandra/service/ClearCounterRowTest.java
@@ -0,0 +1,69 @@
+package me.prettyprint.cassandra.service;
+
+import me.prettyprint.cassandra.BaseEmbededServerSetupTest;
+import me.prettyprint.cassandra.serializers.StringSerializer;
+import me.prettyprint.hector.api.Cluster;
+import me.prettyprint.hector.api.Keyspace;
+import me.prettyprint.hector.api.beans.HCounterColumn;
+import me.prettyprint.hector.api.factory.HFactory;
+import static me.prettyprint.hector.api.factory.HFactory.*;
+import me.prettyprint.hector.api.mutation.Mutator;
+import me.prettyprint.hector.api.query.SliceCounterQuery;
+import org.junit.After;
+import static org.junit.Assert.assertEquals;
+import org.junit.Before;
+import org.junit.Test;
+
+public class ClearCounterRowTest extends BaseEmbededServerSetupTest {
+
+ private static final StringSerializer ss = new StringSerializer();
+ private static final String cf = "Counter1";
+ private static final String key = "key";
+ private Cluster cluster;
+ private Keyspace keyspace;
+
+ @Before
+ public void setUp() {
+ cluster = getOrCreateCluster("Test Cluster", "127.0.0.1:9170");
+ keyspace = createKeyspace("Keyspace1", cluster);
+
+ Mutator<String> m = createMutator(keyspace, ss);
+ }
+
+ @After
+ public void tearDown() {
+ keyspace = null;
+ cluster = null;
+ }
+
+ @Test
+ public void testClear() {
+ Mutator<String> mutator = createMutator(keyspace, ss);
+ for (int i = 1; i <= 10; i++) {
+ mutator.addCounter(key, cf, createCounterColumn("" + i, i));
+ }
+ mutator.execute();
+
+ SliceCounterQuery query = HFactory.createCounterSliceQuery(keyspace, ss, ss);
+ query.setColumnFamily(cf);
+ query.setKey(key);
+
+ SliceCounterIterator<String, String> iterator = new SliceCounterIterator<String, String>(query, null, (String) null, false, 10);
+ while(iterator.hasNext()) {
+ HCounterColumn<String> column = iterator.next();
+
+ long expected = Long.parseLong(column.getName());
+ assertEquals(expected, (long) column.getValue());
+ }
+
+ new ClearCounterRow(keyspace, ss, ss).setColumnFamily(cf).setRowKey(key).clear();
+
+ iterator = new SliceCounterIterator<String, String>(query, null, (String) null, false, 10);
+ while(iterator.hasNext()) {
+ HCounterColumn<String> column = iterator.next();
+
+ long expected = 0;
+ assertEquals(expected, (long) column.getValue());
+ }
+ }
+}

0 comments on commit fcde06d

Please sign in to comment.
Something went wrong with that request. Please try again.