Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

added documentation, deprecation and some tweaks dealing with clocks …

…on the template classes. for #400
  • Loading branch information...
commit 0baeefe91967cdd455fdad14eb5b039f6ba24850 1 parent 5206b08
zznate authored
View
20 core/src/main/java/me/prettyprint/cassandra/service/template/AbstractColumnFamilyTemplate.java
@@ -121,16 +121,30 @@ public MutationResult executeBatch(Mutator<K> mutator) {
return HFactory.createMutator(keyspace, keySerializer);
}
- public Long getClock() {
- return clock;
+ /**
+ * Wrapped call to keyspace.createClock
+ * To Specify a clock for a group of operations, use {@link AbstractTemplateUpdater} instead
+ * @return
+ */
+ public long getClock() {
+ return keyspace.createClock();
}
+ /**
+ * @deprecated (and not thread-safe). Set clocks on the {@link AbstractTemplateUpdater}
+ * implementation as needed.
+ * @param clock
+ */
public void setClock(Long clock) {
this.clock = clock;
}
+ /**
+ * @deprecated does the same thing as getClock() will be removed in a future release
+ * @return
+ */
public long getEffectiveClock() {
- return clock != null ? clock.longValue() : keyspace.createClock();
+ return keyspace.createClock();
}
public void setExceptionsTranslator(ExceptionsTranslator exceptionsTranslator) {
View
15 core/src/main/java/me/prettyprint/cassandra/service/template/AbstractTemplateUpdater.java
@@ -13,11 +13,13 @@
protected ColumnFactory columnFactory;
protected AbstractColumnFamilyTemplate<K,N> template;
protected Mutator<K> mutator;
+ protected long clock;
public AbstractTemplateUpdater(AbstractColumnFamilyTemplate<K, N> template, ColumnFactory columnFactory, Mutator<K> mutator) {
this.template = template;
this.columnFactory = columnFactory;
this.mutator = mutator;
+ this.clock = template.getClock();
}
public AbstractTemplateUpdater<K,N> addKey(K key) {
@@ -48,4 +50,17 @@ public void update() {
public Mutator<K> getCurrentMutator() {
return mutator;
}
+
+ /**
+ * Reset the clock used for column creation. By default, we hold a reference to a single
+ * clock value created at construction from {@link AbstractColumnFamilyTemplate#getClock()}
+ *
+ * Since updater implementations are instance-based (and thus should not be shared among threads),
+ * call this as often as you need to apply the clock to the underlying columns.
+ *
+ * @param clock
+ */
+ public void setClock(long clock) {
+ this.clock = clock;
+ }
}
View
51 core/src/main/java/me/prettyprint/cassandra/service/template/ColumnFamilyUpdater.java
@@ -4,17 +4,11 @@
import java.util.Date;
import java.util.UUID;
-import me.prettyprint.cassandra.serializers.BooleanSerializer;
-import me.prettyprint.cassandra.serializers.ByteBufferSerializer;
-import me.prettyprint.cassandra.serializers.BytesArraySerializer;
-import me.prettyprint.cassandra.serializers.DateSerializer;
-import me.prettyprint.cassandra.serializers.DoubleSerializer;
-import me.prettyprint.cassandra.serializers.IntegerSerializer;
-import me.prettyprint.cassandra.serializers.LongSerializer;
-import me.prettyprint.cassandra.serializers.StringSerializer;
-import me.prettyprint.cassandra.serializers.UUIDSerializer;
+import me.prettyprint.cassandra.serializers.*;
import me.prettyprint.hector.api.ColumnFactory;
import me.prettyprint.hector.api.Serializer;
+import me.prettyprint.hector.api.beans.Composite;
+import me.prettyprint.hector.api.beans.DynamicComposite;
import me.prettyprint.hector.api.beans.HColumn;
import me.prettyprint.hector.api.mutation.Mutator;
@@ -69,65 +63,80 @@ public void deleteColumn(N columnName) {
}
public void setString(N columnName, String value) {
- HColumn<N, String> column = columnFactory.createColumn(columnName, value,
+ HColumn<N, String> column = columnFactory.createColumn(columnName, value, clock,
template.getTopSerializer(), StringSerializer.get());
mutator.addInsertion(getCurrentKey(), template.getColumnFamily(), column);
}
public void setUUID(N columnName, UUID value) {
- HColumn<N, UUID> column = columnFactory.createColumn(columnName, value,
+ HColumn<N, UUID> column = columnFactory.createColumn(columnName, value, clock,
template.getTopSerializer(), UUIDSerializer.get());
mutator.addInsertion(getCurrentKey(), template.getColumnFamily(), column);
}
public void setLong(N columnName, Long value) {
- HColumn<N, Long> column = columnFactory.createColumn(columnName, value,
+ HColumn<N, Long> column = columnFactory.createColumn(columnName, value, clock,
template.getTopSerializer(), LongSerializer.get());
mutator.addInsertion(getCurrentKey(), template.getColumnFamily(), column);
}
public void setInteger(N columnName, Integer value) {
- HColumn<N, Integer> column = columnFactory.createColumn(columnName, value,
+ HColumn<N, Integer> column = columnFactory.createColumn(columnName, value, clock,
template.getTopSerializer(), IntegerSerializer.get());
mutator.addInsertion(getCurrentKey(), template.getColumnFamily(), column);
}
public void setDouble(N columnName, Double value) {
- HColumn<N, Double> column = columnFactory.createColumn(columnName, value,
+ HColumn<N, Double> column = columnFactory.createColumn(columnName, value, clock,
template.getTopSerializer(), DoubleSerializer.get());
mutator.addInsertion(getCurrentKey(), template.getColumnFamily(), column);
}
public void setBoolean(N columnName, Boolean value) {
- HColumn<N, Boolean> column = columnFactory.createColumn(columnName, value,
+ HColumn<N, Boolean> column = columnFactory.createColumn(columnName, value, clock,
template.getTopSerializer(), BooleanSerializer.get());
mutator.addInsertion(getCurrentKey(), template.getColumnFamily(), column);
}
public void setByteArray(N columnName, byte[] value) {
- HColumn<N, byte[]> column = columnFactory.createColumn(columnName, value,
+ HColumn<N, byte[]> column = columnFactory.createColumn(columnName, value, clock,
template.getTopSerializer(), BytesArraySerializer.get());
mutator.addInsertion(getCurrentKey(), template.getColumnFamily(), column);
}
public void setByteBuffer(N columnName, ByteBuffer value) {
- HColumn<N, ByteBuffer> column = columnFactory.createColumn(columnName, value,
+ HColumn<N, ByteBuffer> column = columnFactory.createColumn(columnName, value, clock,
template.getTopSerializer(), ByteBufferSerializer.get());
mutator.addInsertion(getCurrentKey(), template.getColumnFamily(), column);
}
public void setDate(N columnName, Date value) {
- HColumn<N, Date> column = columnFactory.createColumn(columnName, value,
+ HColumn<N, Date> column = columnFactory.createColumn(columnName, value, clock,
template.getTopSerializer(), DateSerializer.get());
mutator.addInsertion(getCurrentKey(), template.getColumnFamily(), column);
}
-
+
+ public <V> void setFloat(N columnName, float value) {
+ HColumn<N, Float> column = columnFactory.createColumn(columnName, value, clock,
+ template.getTopSerializer(), FloatSerializer.get());
+ }
+
+ public <V> void setComposite(N columnName, Composite composite) {
+ HColumn<N, Composite> column = columnFactory.createColumn(columnName, composite, clock,
+ template.getTopSerializer(), CompositeSerializer.get());
+ }
+
+ public <V> void setDynamicComposite(N columnName, DynamicComposite composite) {
+ HColumn<N, DynamicComposite> column = columnFactory.createColumn(columnName, composite, clock,
+ template.getTopSerializer(), DynamicCompositeSerializer.get());
+ }
+
public <V> void setValue(N columnName, V value, Serializer<V> serializer) {
- HColumn<N, V> column = columnFactory.createColumn(columnName, value,
+ HColumn<N, V> column = columnFactory.createColumn(columnName, value, clock,
template.getTopSerializer(), serializer);
mutator.addInsertion(getCurrentKey(), template.getColumnFamily(), column);
}
-
+
public <V> void setColumn(HColumn<N, V> column) {
mutator.addInsertion(getCurrentKey(), template.getColumnFamily(), column);
}
View
5 core/src/main/java/me/prettyprint/cassandra/service/template/SuperCfTemplate.java
@@ -44,7 +44,6 @@ public boolean isColumnsExist(K key) {
*
* @param key
* @param superColumnName
- * @param subSerializer
* the column name serializer of the child columns
* @return true if columns exist
*/
@@ -96,14 +95,12 @@ public int countColumns(K key, SN start, SN end, int max) {
/**
* Counts child columns in the specified range of a children in a specified
* super column
- *
- * @param <SUBCOL>
+ *
* @param key
* @param superColumnName
* @param start
* @param end
* @param max
- * @param subSerializer
* @return
*/
public int countSubColumns(K key, SN superColumnName, N start,
View
22 core/src/main/java/me/prettyprint/cassandra/service/template/SuperCfUpdater.java
@@ -50,8 +50,6 @@
* the standard or super column's data type
* @param <N>
* the child column name type in a super column
- * @param <V>
- * the object instance to persist
*/
public class SuperCfUpdater<K,SN,N> extends AbstractTemplateUpdater<K, N> {
private static final Logger log = LoggerFactory.getLogger(SuperCfUpdater.class);
@@ -131,52 +129,52 @@ public void deleteSubColumn(N columnName) {
}
public void setString(N subColumnName, String value) {
- subColumns.add(columnFactory.createColumn(subColumnName, value, template.getEffectiveClock(),
+ subColumns.add(columnFactory.createColumn(subColumnName, value, clock,
template.getSubSerializer(), StringSerializer.get() ));
}
public void setUUID(N subColumnName, UUID value) {
- subColumns.add(columnFactory.createColumn(subColumnName, value, template.getEffectiveClock(),
+ subColumns.add(columnFactory.createColumn(subColumnName, value, clock,
template.getSubSerializer(), UUIDSerializer.get()));
}
public void setLong(N subColumnName, Long value) {
- subColumns.add(columnFactory.createColumn(subColumnName, value, template.getEffectiveClock(),
+ subColumns.add(columnFactory.createColumn(subColumnName, value, clock,
template.getSubSerializer(), LongSerializer.get()));
}
public void setInteger(N subColumnName, Integer value) {
- subColumns.add(columnFactory.createColumn(subColumnName, value, template.getEffectiveClock(),
+ subColumns.add(columnFactory.createColumn(subColumnName, value, clock,
template.getSubSerializer(), IntegerSerializer.get()));
}
public void setBoolean(N subColumnName, Boolean value) {
- subColumns.add(columnFactory.createColumn(subColumnName, value, template.getEffectiveClock(),
+ subColumns.add(columnFactory.createColumn(subColumnName, value, clock,
template.getSubSerializer(), BooleanSerializer.get()));
}
public void setByteArray(N subColumnName, byte[] value) {
- subColumns.add(columnFactory.createColumn(subColumnName, value, template.getEffectiveClock(),
+ subColumns.add(columnFactory.createColumn(subColumnName, value, clock,
template.getSubSerializer(), BytesArraySerializer.get()));
}
public void setByteBuffer(N subColumnName, ByteBuffer value) {
- subColumns.add(columnFactory.createColumn(subColumnName, value, template.getEffectiveClock(),
+ subColumns.add(columnFactory.createColumn(subColumnName, value, clock,
template.getSubSerializer(), ByteBufferSerializer.get()));
}
public void setDate(N subColumnName, Date value) {
- subColumns.add(columnFactory.createColumn(subColumnName, value, template.getEffectiveClock(),
+ subColumns.add(columnFactory.createColumn(subColumnName, value, clock,
template.getSubSerializer(), DateSerializer.get()));
}
public void setDouble(N subColumnName, Double value) {
- subColumns.add(columnFactory.createColumn(subColumnName, value, template.getEffectiveClock(),
+ subColumns.add(columnFactory.createColumn(subColumnName, value, clock,
template.getSubSerializer(), DoubleSerializer.get()));
}
public <V> void setValue(N subColumnName, V value, Serializer<V> serializer) {
- subColumns.add(columnFactory.createColumn(subColumnName, value, template.getEffectiveClock(),
+ subColumns.add(columnFactory.createColumn(subColumnName, value, clock,
template.getSubSerializer(), serializer));
}
}
View
35 core/src/test/java/me/prettyprint/cassandra/service/template/ColumnFamilyTemplateTest.java
@@ -1,15 +1,14 @@
package me.prettyprint.cassandra.service.template;
-import static org.junit.Assert.*;
-
import java.util.Arrays;
import java.util.Date;
import me.prettyprint.cassandra.serializers.DateSerializer;
import me.prettyprint.cassandra.serializers.LongSerializer;
-
import org.apache.cassandra.thrift.IndexOperator;
import org.junit.Test;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
public class ColumnFamilyTemplateTest extends BaseColumnFamilyTemplateTest {
@@ -45,8 +44,38 @@ public void testCreateSelectMultiColumn() {
assertEquals("value1",wrapper.getString("stringval"));
assertEquals(date,wrapper.getDate("curdate"));
assertEquals(new Long(5),wrapper.getLong("longval"));
+ assertEquals(3,wrapper.getColumnNames().size());
}
+ @Test
+ public void testCompareClocks() {
+ ColumnFamilyTemplate<String, String> template = new ThriftColumnFamilyTemplate<String, String>(keyspace, "Standard1", se, se);
+ long ts1 = 1001;
+ long ts2 = 1002;
+ long ts3 = 1003;
+
+
+ ColumnFamilyUpdater<String,String> updater = template.createUpdater("compare_clock_key1");
+ updater.setClock(ts1);
+ updater.setString("stringval","value1");
+ Date date = new Date();
+ updater.setClock(ts2);
+ updater.setDate("curdate", date);
+ updater.setClock(ts3);
+ updater.setLong("longval", 5L);
+ template.update(updater);
+ template.addColumn("stringval", se);
+ template.addColumn("curdate", DateSerializer.get());
+ template.addColumn("longval", LongSerializer.get());
+
+ ColumnFamilyResult wrapper = template.queryColumns("compare_clock_key1");
+ assertEquals(ts1,wrapper.getColumn("stringval").getClock());
+ assertEquals(ts2,wrapper.getColumn("curdate").getClock());
+ assertEquals(ts3,wrapper.getColumn("longval").getClock());
+ assertEquals(3,wrapper.getColumnNames().size());
+ }
+
+
@Test
public void testCreateSelectTemplate() {
ColumnFamilyTemplate<String, String> template = new ThriftColumnFamilyTemplate<String, String>(keyspace, "Standard1", se, se);
Please sign in to comment.
Something went wrong with that request. Please try again.