forked from hector-client/hector
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request hector-client#555 from hector-client/row_clear
Row clear
- Loading branch information
Showing
2 changed files
with
168 additions
and
0 deletions.
There are no files selected for viewing
99 changes: 99 additions & 0 deletions
99
core/src/main/java/me/prettyprint/cassandra/service/ClearCounterRow.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 <this> | ||
*/ | ||
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(); | ||
} | ||
} |
69 changes: 69 additions & 0 deletions
69
core/src/test/java/me/prettyprint/cassandra/service/ClearCounterRowTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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()); | ||
} | ||
} | ||
} |