From b2c213e8fd3513a2fc0f7d44219a358f7780c1a8 Mon Sep 17 00:00:00 2001 From: Tim Koop Date: Wed, 9 Mar 2011 11:24:05 -0600 Subject: [PATCH 001/144] Added KeyIterator and StringKeyIterator services. --- .../cassandra/service/KeyIterator.java | 106 ++++++++++++++++++ .../cassandra/service/StringKeyIterator.java | 21 ++++ 2 files changed, 127 insertions(+) create mode 100644 core/src/main/java/me/prettyprint/cassandra/service/KeyIterator.java create mode 100644 core/src/main/java/me/prettyprint/cassandra/service/StringKeyIterator.java diff --git a/core/src/main/java/me/prettyprint/cassandra/service/KeyIterator.java b/core/src/main/java/me/prettyprint/cassandra/service/KeyIterator.java new file mode 100644 index 000000000..34fbb9e34 --- /dev/null +++ b/core/src/main/java/me/prettyprint/cassandra/service/KeyIterator.java @@ -0,0 +1,106 @@ +package me.prettyprint.cassandra.service; + +import java.util.Iterator; + +import me.prettyprint.cassandra.serializers.AbstractSerializer; +import me.prettyprint.cassandra.serializers.StringSerializer; +import me.prettyprint.hector.api.Keyspace; +import me.prettyprint.hector.api.beans.OrderedRows; +import me.prettyprint.hector.api.beans.Row; +import me.prettyprint.hector.api.factory.HFactory; +import me.prettyprint.hector.api.query.QueryResult; +import me.prettyprint.hector.api.query.RangeSlicesQuery; + + +/** + * This class returns each key in the specified Column Family as an Iterator. You + * can use this class in a for loop without the overhead of first storing each + * key in a large array. See StringKeyIterator for a convenience class if the key + * is a String. + * @author Tim Koop + * @param the type of the row key + * @see StringKeyIterator + */ +public class KeyIterator implements Iterable { + private static StringSerializer stringSerializer = new StringSerializer(); + + private int maxRowCount = 500; + private int maxColumnCount = 2; // we only need this to tell if there are any columns in the row (to test for tombstones) + + private Iterator> rowsIterator = null; + + private RangeSlicesQuery query = null; + + private K nextValue = null; + private K lastReadValue = null; + + private Iterator keyIterator = new Iterator() { + @Override + public boolean hasNext() { + return nextValue != null; + } + + @Override + public K next() { + K next = nextValue; + findNext(false); + return next; + } + + @Override + public void remove() { + throw new UnsupportedOperationException(); + } + }; + + private void findNext(boolean fromRunQuery) { + nextValue = null; + if (rowsIterator == null) { + return; + } + while (rowsIterator.hasNext() && nextValue == null) { + Row row = rowsIterator.next(); + lastReadValue = row.getKey(); + if (!row.getColumnSlice().getColumns().isEmpty()) { + nextValue = lastReadValue; + } + } + if (!rowsIterator.hasNext() && nextValue == null) { + runQuery(lastReadValue); + } + } + + public KeyIterator(Keyspace keyspace, String columnFamily, AbstractSerializer serializer) { + query = HFactory + .createRangeSlicesQuery(keyspace, serializer, stringSerializer, stringSerializer) + .setColumnFamily(columnFamily) + .setRange(null, null, false, maxColumnCount) + .setRowCount(maxRowCount); + + runQuery(null); + } + + private void runQuery(K start) { + query.setKeys(start, null); + + rowsIterator = null; + QueryResult> result = query.execute(); + OrderedRows rows = (result != null) ? result.get() : null; + rowsIterator = (rows != null) ? rows.iterator() : null; + + // we'll skip this first one, since it is the same as the last one from previous time we executed + if (start != null && rowsIterator != null) rowsIterator.next(); + + if (!rowsIterator.hasNext()) { + nextValue = null; // all done. our iterator's hasNext() will now return false; + } else { + findNext(true); + } + } + + @Override + public Iterator iterator() { + return keyIterator; + } +} + diff --git a/core/src/main/java/me/prettyprint/cassandra/service/StringKeyIterator.java b/core/src/main/java/me/prettyprint/cassandra/service/StringKeyIterator.java new file mode 100644 index 000000000..9675d10ec --- /dev/null +++ b/core/src/main/java/me/prettyprint/cassandra/service/StringKeyIterator.java @@ -0,0 +1,21 @@ +package me.prettyprint.cassandra.service; + +import me.prettyprint.cassandra.serializers.StringSerializer; +import me.prettyprint.hector.api.Keyspace; + +/** + * This class returns each key in the specified Column Family as an Iterator. You + * can use this class in a for loop without the overhead of first storing each + * key in a large array. This is a convenience class for KeyIterator when the key + * is a String. + * @author Tim Koop + * @see KeyIterator + */ +public class StringKeyIterator extends KeyIterator { + + public StringKeyIterator(Keyspace keyspace, String columnFamily) { + super(keyspace, columnFamily, new StringSerializer()); + + } + +} From 56fe276686912a8adb0124c0bfae70886200ddbf Mon Sep 17 00:00:00 2001 From: zznate Date: Wed, 9 Mar 2011 15:57:55 -0600 Subject: [PATCH 002/144] removed shutdownHook register. was made redundant after inclusion of shutdown methods. thanks to sbridges for the tip --- .../connection/BackgroundCassandraHostService.java | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/core/src/main/java/me/prettyprint/cassandra/connection/BackgroundCassandraHostService.java b/core/src/main/java/me/prettyprint/cassandra/connection/BackgroundCassandraHostService.java index 4b15dc75a..5679c8bd1 100644 --- a/core/src/main/java/me/prettyprint/cassandra/connection/BackgroundCassandraHostService.java +++ b/core/src/main/java/me/prettyprint/cassandra/connection/BackgroundCassandraHostService.java @@ -20,12 +20,7 @@ public BackgroundCassandraHostService(HConnectionManager connectionManager, CassandraHostConfigurator cassandraHostConfigurator) { this.connectionManager = connectionManager; this.cassandraHostConfigurator = cassandraHostConfigurator; - Runtime.getRuntime().addShutdownHook(new Thread() { - @Override - public void run() { - shutdown(); - } - }); + } abstract void shutdown(); From d65a220ca3e973fa58318cde7ea893bd1c7c4dd7 Mon Sep 17 00:00:00 2001 From: zznate Date: Wed, 9 Mar 2011 16:49:08 -0600 Subject: [PATCH 003/144] cleaner handling of value and name changes on underlying columns, added access to raw BB for advanced usage --- .../cassandra/model/HColumnImpl.java | 32 +++++++++++-------- .../cassandra/model/HSlicePredicate.java | 6 ++-- .../prettyprint/hector/api/beans/HColumn.java | 14 +++++++- 3 files changed, 35 insertions(+), 17 deletions(-) diff --git a/core/src/main/java/me/prettyprint/cassandra/model/HColumnImpl.java b/core/src/main/java/me/prettyprint/cassandra/model/HColumnImpl.java index 9c0e7cb5f..1cedf751c 100644 --- a/core/src/main/java/me/prettyprint/cassandra/model/HColumnImpl.java +++ b/core/src/main/java/me/prettyprint/cassandra/model/HColumnImpl.java @@ -60,14 +60,14 @@ public HColumnImpl(N name, V value, long clock) { @Override public HColumn setName(N name) { notNull(name, "name is null"); - this.column.name = nameSerializer.toByteBuffer(name); + this.column.setName(nameSerializer.toByteBuffer(name)); return this; } @Override public HColumn setValue(V value) { notNull(value, "value is null"); - this.column.value = valueSerializer.toByteBuffer(value); + this.column.setValue(valueSerializer.toByteBuffer(value)); return this; } @@ -93,19 +93,13 @@ public int getTtl() { } @Override - public N getName() { - if ( column.name == null ) { - return null; - } - return nameSerializer.fromByteBuffer(column.name.duplicate()); + public N getName() { + return column.isSetName() ? nameSerializer.fromByteBuffer(column.name.duplicate()) : null; } @Override - public V getValue() { - if ( column.value == null ) { - return null; - } - return valueSerializer.fromByteBuffer(column.value.duplicate()); + public V getValue() { + return column.isSetValue() ? valueSerializer.fromByteBuffer(column.value.duplicate()) : null; } @@ -132,9 +126,18 @@ public Serializer getNameSerializer() { @Override public Serializer getValueSerializer() { return valueSerializer; + } + + @Override + public ByteBuffer getNameBytes() { + return column.isSetName() ? column.name.duplicate() : null; + } + + @Override + public ByteBuffer getValueBytes() { + return column.isSetValue() ? column.value.duplicate() : null; } - /** * Clear value, timestamp and ttl (the latter two set to '0') leaving only the column name */ @@ -145,6 +148,7 @@ public HColumn clear() { column.ttl = 0; column.setTimestampIsSet(false); column.setTtlIsSet(false); + column.setValueIsSet(false); return this; } @@ -152,7 +156,7 @@ public HColumn clear() { @Override public HColumn apply(V value, long clock, int ttl) { - column.value = valueSerializer.toByteBuffer(value); + setValue(value); column.setTimestamp(clock); column.setTtl(ttl); return this; diff --git a/core/src/main/java/me/prettyprint/cassandra/model/HSlicePredicate.java b/core/src/main/java/me/prettyprint/cassandra/model/HSlicePredicate.java index 96363e102..6be03341c 100644 --- a/core/src/main/java/me/prettyprint/cassandra/model/HSlicePredicate.java +++ b/core/src/main/java/me/prettyprint/cassandra/model/HSlicePredicate.java @@ -21,7 +21,7 @@ * @author zznate */ public final class HSlicePredicate { - + protected Collection columnNames; protected N start; protected N finish; @@ -33,6 +33,8 @@ public final class HSlicePredicate { protected enum PredicateType {Unknown, ColumnNames, Range}; protected PredicateType predicateType = PredicateType.Unknown; + private static final ByteBuffer EMPTY_BYTE_BUFFER = ByteBuffer.wrap(new byte[0]); + public HSlicePredicate(Serializer columnNameSerializer) { Assert.notNull(columnNameSerializer, "columnNameSerializer can't be null"); this.columnNameSerializer = columnNameSerializer; @@ -180,7 +182,7 @@ public SlicePredicate toThrift() { private ByteBuffer findBytes(N val) { ByteBuffer valBytes; if (val == null) { - valBytes = ByteBuffer.wrap(new byte[0]); + valBytes = EMPTY_BYTE_BUFFER; } else { valBytes = columnNameSerializer.toByteBuffer(val); } diff --git a/core/src/main/java/me/prettyprint/hector/api/beans/HColumn.java b/core/src/main/java/me/prettyprint/hector/api/beans/HColumn.java index 94a1c3d32..95a20a058 100644 --- a/core/src/main/java/me/prettyprint/hector/api/beans/HColumn.java +++ b/core/src/main/java/me/prettyprint/hector/api/beans/HColumn.java @@ -1,5 +1,7 @@ package me.prettyprint.hector.api.beans; +import java.nio.ByteBuffer; + import me.prettyprint.hector.api.Serializer; /** @@ -9,7 +11,7 @@ * @param The type of the column value * * @author Ran Tavory (rantav@gmail.com) - * + * @author zznate */ public interface HColumn { @@ -21,6 +23,16 @@ public interface HColumn { V getValue(); + /** + * (Advanced) Returns the underlying ByteBuffer for the value via ByteBuffer.duplicate(). + */ + ByteBuffer getValueBytes(); + + /** + * (Advanced) Returns the underlying ByteBuffer for the name via ByteBuffer.duplicate(). + */ + ByteBuffer getNameBytes(); + long getClock(); HColumn setClock(long clock); From 7d1e1ac989f7845dfe80e6a141f53332bb98520a Mon Sep 17 00:00:00 2001 From: zznate Date: Thu, 10 Mar 2011 13:29:41 -0600 Subject: [PATCH 004/144] changed back to collection on pool impls keeping steady state semantics of NBHM values for now --- .../connection/DynamicLoadBalancingPolicy.java | 2 +- .../cassandra/connection/HConnectionManager.java | 4 ++-- .../connection/LeastActiveBalancingPolicy.java | 2 +- .../cassandra/connection/LoadBalancingPolicy.java | 3 ++- .../connection/RoundRobinBalancingPolicy.java | 15 ++++++++++----- 5 files changed, 16 insertions(+), 10 deletions(-) diff --git a/core/src/main/java/me/prettyprint/cassandra/connection/DynamicLoadBalancingPolicy.java b/core/src/main/java/me/prettyprint/cassandra/connection/DynamicLoadBalancingPolicy.java index a6e9297be..a0c8f5f9b 100644 --- a/core/src/main/java/me/prettyprint/cassandra/connection/DynamicLoadBalancingPolicy.java +++ b/core/src/main/java/me/prettyprint/cassandra/connection/DynamicLoadBalancingPolicy.java @@ -62,7 +62,7 @@ public void run() { } @Override - public HClientPool getPool(List pools, Set excludeHosts) { + public HClientPool getPool(Collection pools, Set excludeHosts) { List poolList = Lists.newArrayList(pools); // remove the hosts from the list. diff --git a/core/src/main/java/me/prettyprint/cassandra/connection/HConnectionManager.java b/core/src/main/java/me/prettyprint/cassandra/connection/HConnectionManager.java index d200fabee..66f0f23ff 100644 --- a/core/src/main/java/me/prettyprint/cassandra/connection/HConnectionManager.java +++ b/core/src/main/java/me/prettyprint/cassandra/connection/HConnectionManager.java @@ -41,7 +41,7 @@ public class HConnectionManager { private final NonBlockingHashMap hostPools; private final NonBlockingHashMap suspendedHostPools; - private final List hostPoolValues; + private final Collection hostPoolValues; private final String clusterName; private CassandraHostRetryService cassandraHostRetryService; private NodeAutoDiscoverService nodeAutoDiscoverService; @@ -82,7 +82,7 @@ public HConnectionManager(String clusterName, CassandraHostConfigurator cassandr monitor = JmxMonitor.getInstance().getCassandraMonitor(this); exceptionsTranslator = new ExceptionsTranslatorImpl(); this.cassandraHostConfigurator = cassandraHostConfigurator; - hostPoolValues = new ArrayList(hostPools.values()); + hostPoolValues = hostPools.values(); if ( cassandraHostConfigurator.getAutoDiscoverHosts() ) { nodeAutoDiscoverService = new NodeAutoDiscoverService(this, cassandraHostConfigurator); if ( cassandraHostConfigurator.getRunAutoDiscoveryAtStartup() ) { diff --git a/core/src/main/java/me/prettyprint/cassandra/connection/LeastActiveBalancingPolicy.java b/core/src/main/java/me/prettyprint/cassandra/connection/LeastActiveBalancingPolicy.java index e7e0a94fd..9f1cfac96 100644 --- a/core/src/main/java/me/prettyprint/cassandra/connection/LeastActiveBalancingPolicy.java +++ b/core/src/main/java/me/prettyprint/cassandra/connection/LeastActiveBalancingPolicy.java @@ -25,7 +25,7 @@ public class LeastActiveBalancingPolicy implements LoadBalancingPolicy { private static final Logger log = LoggerFactory.getLogger(LeastActiveBalancingPolicy.class); @Override - public HClientPool getPool(List pools, Set excludeHosts) { + public HClientPool getPool(Collection pools, Set excludeHosts) { List vals = Lists.newArrayList(pools); // shuffle pools to avoid always returning the same one when we are not terribly busy Collections.shuffle(vals); diff --git a/core/src/main/java/me/prettyprint/cassandra/connection/LoadBalancingPolicy.java b/core/src/main/java/me/prettyprint/cassandra/connection/LoadBalancingPolicy.java index 49d628f8e..a18181b94 100644 --- a/core/src/main/java/me/prettyprint/cassandra/connection/LoadBalancingPolicy.java +++ b/core/src/main/java/me/prettyprint/cassandra/connection/LoadBalancingPolicy.java @@ -1,12 +1,13 @@ package me.prettyprint.cassandra.connection; import java.io.Serializable; +import java.util.Collection; import java.util.List; import java.util.Set; import me.prettyprint.cassandra.service.CassandraHost; public interface LoadBalancingPolicy extends Serializable { - HClientPool getPool(List pools, Set excludeHosts); + HClientPool getPool(Collection pools, Set excludeHosts); HClientPool createConnection(CassandraHost host); } diff --git a/core/src/main/java/me/prettyprint/cassandra/connection/RoundRobinBalancingPolicy.java b/core/src/main/java/me/prettyprint/cassandra/connection/RoundRobinBalancingPolicy.java index 450f3f750..165426f76 100644 --- a/core/src/main/java/me/prettyprint/cassandra/connection/RoundRobinBalancingPolicy.java +++ b/core/src/main/java/me/prettyprint/cassandra/connection/RoundRobinBalancingPolicy.java @@ -1,5 +1,6 @@ package me.prettyprint.cassandra.connection; +import java.util.Collection; import java.util.List; import java.util.Set; import java.util.concurrent.atomic.AtomicInteger; @@ -26,19 +27,23 @@ public RoundRobinBalancingPolicy() { } @Override - public HClientPool getPool(List pools, + public HClientPool getPool(Collection pools, Set excludeHosts) { - HClientPool pool = getPoolSafely(getAndIncrement(pools.size()), pools); + HClientPool pool = getPoolSafely(pools); if ( excludeHosts != null && excludeHosts.size() > 0 ) { while ( excludeHosts.contains(pool.getCassandraHost()) ) { - pool = getPoolSafely(getAndIncrement(pools.size()), pools); + pool = getPoolSafely(pools); } } return pool; } - private HClientPool getPoolSafely(int location, List pools) { - return Iterables.get(pools, location, pools.get(0)); + private HClientPool getPoolSafely(Collection pools) { + try { + return Iterables.get(pools, getAndIncrement(pools.size())); + } catch (IndexOutOfBoundsException e) { + return pools.iterator().next(); + } } private int getAndIncrement(int size) { From e86026c43ccd6426749980f6263ccddbfa02ff71 Mon Sep 17 00:00:00 2001 From: zznate Date: Thu, 10 Mar 2011 16:43:32 -0600 Subject: [PATCH 005/144] revived jndi plumbing to work with current infrastructure --- .../CassandraClientJndiResourceFactory.java | 78 +++++++++++++++---- ...assandraClientJndiResourceFactoryTest.java | 25 +++--- 2 files changed, 76 insertions(+), 27 deletions(-) diff --git a/core/src/main/java/me/prettyprint/cassandra/jndi/CassandraClientJndiResourceFactory.java b/core/src/main/java/me/prettyprint/cassandra/jndi/CassandraClientJndiResourceFactory.java index 3f838abb1..9d4eadf2c 100644 --- a/core/src/main/java/me/prettyprint/cassandra/jndi/CassandraClientJndiResourceFactory.java +++ b/core/src/main/java/me/prettyprint/cassandra/jndi/CassandraClientJndiResourceFactory.java @@ -8,29 +8,44 @@ import javax.naming.Reference; import javax.naming.spi.ObjectFactory; +import org.apache.commons.lang.BooleanUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import me.prettyprint.cassandra.service.CassandraHostConfigurator; +import me.prettyprint.hector.api.Cluster; +import me.prettyprint.hector.api.Keyspace; +import me.prettyprint.hector.api.factory.HFactory; + /** * A factory for JNDI Resource managed objects. Responsible for creating a - * {@link CassandraClientJndiResourcePool}. Relies on a supplied URL and - * port which Cassandra is listening on. These parameters are defined in - * a web application's context.xml file. For example: + * {@link Keyspace} references for passing to {@link HFactory}. + * A limited set of configuration parameters are supported. + * These parameters are defined in a web application's context.xml file. * *

*

  *           
  * 
* * @author Perry Hoekstra (dutchman_mn@charter.net) + * @author zznate * * @since 0.5.1-8 */ -public class CassandraClientJndiResourceFactory implements ObjectFactory -{ +public class CassandraClientJndiResourceFactory implements ObjectFactory { + private Logger log = LoggerFactory.getLogger(CassandraClientJndiResourceFactory.class); + + private CassandraHostConfigurator cassandraHostConfigurator; + private Cluster cluster; + private Keyspace keyspace; + /** * Creates an object using the location or reference information specified. * @@ -57,16 +72,51 @@ public Object getObjectInstance(Object object, Name jndiName, Context context, throw new Exception("Object provided is not a javax.naming.Reference type"); } - RefAddr urlRefAddr = resourceRef.get("url"); - - RefAddr portRefAddr = resourceRef.get("port"); + // config CassandraHostConfigurator + if ( cluster == null ) { + configure(resourceRef); + } + + return keyspace; + } - if ((urlRefAddr != null) && (portRefAddr != null)) { - return new CassandraClientJndiResourcePool((String)urlRefAddr.getContent(), - Integer.parseInt((String)portRefAddr.getContent())); - } else { + private void configure(Reference resourceRef) throws Exception { + // required + RefAddr hostsRefAddr = resourceRef.get("hosts"); + RefAddr clusterNameRef = resourceRef.get("clusterName"); + RefAddr keyspaceNameRef = resourceRef.get("keyspace"); + // optional + RefAddr maxActiveRefAddr = resourceRef.get("maxActive"); + RefAddr maxTimeWhenExhausted = resourceRef.get("maxTimeWhenExhausted"); + RefAddr autoDiscoverHosts = resourceRef.get("autoDiscoverHosts"); + RefAddr autoDiscoverAtStartup = resourceRef.get("runAutoDiscoveryAtStartup"); + RefAddr retryDownedHostDelayInSeconds = resourceRef.get("maxTimeWhenExhausted"); + + if ( hostsRefAddr == null || hostsRefAddr.getContent() == null) { throw new Exception("A url and port on which Cassandra is installed and listening " + - "on must be provided as a ResourceParams in the context.xml"); + "on must be provided as a ResourceParams in the context.xml"); + } + + cassandraHostConfigurator = new CassandraHostConfigurator((String)hostsRefAddr.getContent()); + if ( autoDiscoverHosts != null ) { + cassandraHostConfigurator.setAutoDiscoverHosts(Boolean.parseBoolean((String)autoDiscoverHosts.getContent())); + if ( autoDiscoverAtStartup != null ) + cassandraHostConfigurator.setRunAutoDiscoveryAtStartup(Boolean.parseBoolean((String)autoDiscoverHosts.getContent())); + } + if ( retryDownedHostDelayInSeconds != null ) { + int retryDelay = Integer.parseInt((String)retryDownedHostDelayInSeconds.getContent()); + // disable retry if less than 1 + if ( retryDelay < 1 ) + cassandraHostConfigurator.setRetryDownedHosts(false); + cassandraHostConfigurator.setRetryDownedHostsDelayInSeconds(retryDelay); } + if ( maxActiveRefAddr != null ) + cassandraHostConfigurator.setMaxActive(Integer.parseInt((String)maxActiveRefAddr.getContent())); + + if ( log.isDebugEnabled() ) + log.debug("JNDI resource created with CassandraHostConfiguration: {}", cassandraHostConfigurator.getAutoDiscoverHosts()); + + cluster = HFactory.createCluster((String)clusterNameRef.getContent(), cassandraHostConfigurator); + keyspace = HFactory.createKeyspace((String)keyspaceNameRef.getContent(), cluster); } } diff --git a/core/src/test/java/me/prettyprint/cassandra/jndi/CassandraClientJndiResourceFactoryTest.java b/core/src/test/java/me/prettyprint/cassandra/jndi/CassandraClientJndiResourceFactoryTest.java index 7cec0eb19..9abb2fed5 100644 --- a/core/src/test/java/me/prettyprint/cassandra/jndi/CassandraClientJndiResourceFactoryTest.java +++ b/core/src/test/java/me/prettyprint/cassandra/jndi/CassandraClientJndiResourceFactoryTest.java @@ -14,6 +14,7 @@ import javax.naming.StringRefAddr; import me.prettyprint.cassandra.BaseEmbededServerSetupTest; +import me.prettyprint.hector.api.Keyspace; import org.junit.After; import org.junit.Before; @@ -22,12 +23,12 @@ /** * @author Perry Hoekstra (dutchman_mn@charter.net) + * @author zznate */ -@Ignore public class CassandraClientJndiResourceFactoryTest extends BaseEmbededServerSetupTest { // canned data - private final static String cassandraUrl = "localhost"; - private final static int cassandraPort = 9170; + private final static String cassandraUrl = "localhost:9170"; + private CassandraClientJndiResourceFactory factory; @@ -45,21 +46,19 @@ public void teardownCase() throws IOException { public void getObjectInstance() throws Exception { Reference resource = new Reference("CassandraClientFactory"); - resource.add(new StringRefAddr("url", cassandraUrl)); - resource.add(new StringRefAddr("port", Integer.toString(cassandraPort))); + resource.add(new StringRefAddr("hosts", cassandraUrl)); + resource.add(new StringRefAddr("clusterName", clusterName)); + resource.add(new StringRefAddr("keyspace", "Keyspace1")); + resource.add(new StringRefAddr("autoDiscoverHosts", "true")); + Name jndiName = mock(Name.class); Context context = new InitialContext(); Hashtable environment = new Hashtable(); - CassandraClientJndiResourcePool cassandraClientJNDIResourcePool = - (CassandraClientJndiResourcePool) factory.getObjectInstance(resource, jndiName, context, - environment); + Keyspace keyspace = (Keyspace) factory.getObjectInstance(resource, jndiName, context, environment); - //CassandraClient cassandraClient = (CassandraClient) cassandraClientJNDIResourcePool.borrowObject(); - // TODO fix this - //assertNotNull(cassandraClient); - //assertEquals(cassandraUrl, cassandraClient.getCassandraHost().getHost()); - //assertEquals(cassandraPort, cassandraClient.getCassandraHost().getPort()); + assertNotNull(keyspace); + assertEquals("Keyspace1",keyspace.getKeyspaceName()); } } From d5e67ae49b6a178a1c43f236ca96ff6f1fc38574 Mon Sep 17 00:00:00 2001 From: zznate Date: Thu, 10 Mar 2011 16:53:35 -0600 Subject: [PATCH 006/144] added comments, cleanup up var names --- .../CassandraClientJndiResourceFactory.java | 20 +++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/core/src/main/java/me/prettyprint/cassandra/jndi/CassandraClientJndiResourceFactory.java b/core/src/main/java/me/prettyprint/cassandra/jndi/CassandraClientJndiResourceFactory.java index 9d4eadf2c..2f70cbbc1 100644 --- a/core/src/main/java/me/prettyprint/cassandra/jndi/CassandraClientJndiResourceFactory.java +++ b/core/src/main/java/me/prettyprint/cassandra/jndi/CassandraClientJndiResourceFactory.java @@ -22,6 +22,7 @@ * {@link Keyspace} references for passing to {@link HFactory}. * A limited set of configuration parameters are supported. * These parameters are defined in a web application's context.xml file. + * Parameter descriptions can be found in {@link CassandraHostConfigurator} * *

*

@@ -29,8 +30,13 @@
  *               auth="Container"
  *               type="me.prettyprint.cassandra.api.Keyspace"
  *               factory="me.prettyprint.cassandra.jndi.CassandraClientJndiResourceFactory"
- *               url="localhost"
- *               port="9160" />      
+ *               hosts="cass1:9160,cass2:9160,cass3:9160"
+ *               keyspace="Keyspace1"
+ *               clusterName="Test Cluster" 
+ *               maxActive="20"
+ *               maxWaitTimeWhenExhausted="10"
+ *               autoDiscoverHosts="true"
+ *               runAutoDiscoveryAtStartup="true"/>      
  * 
* * @author Perry Hoekstra (dutchman_mn@charter.net) @@ -87,10 +93,10 @@ private void configure(Reference resourceRef) throws Exception { RefAddr keyspaceNameRef = resourceRef.get("keyspace"); // optional RefAddr maxActiveRefAddr = resourceRef.get("maxActive"); - RefAddr maxTimeWhenExhausted = resourceRef.get("maxTimeWhenExhausted"); + RefAddr maxWaitTimeWhenExhausted = resourceRef.get("maxWaitTimeWhenExhausted"); RefAddr autoDiscoverHosts = resourceRef.get("autoDiscoverHosts"); - RefAddr autoDiscoverAtStartup = resourceRef.get("runAutoDiscoveryAtStartup"); - RefAddr retryDownedHostDelayInSeconds = resourceRef.get("maxTimeWhenExhausted"); + RefAddr runAutoDiscoverAtStartup = resourceRef.get("runAutoDiscoveryAtStartup"); + RefAddr retryDownedHostDelayInSeconds = resourceRef.get("retryDownedHostDelayInSeconds"); if ( hostsRefAddr == null || hostsRefAddr.getContent() == null) { throw new Exception("A url and port on which Cassandra is installed and listening " + @@ -100,7 +106,7 @@ private void configure(Reference resourceRef) throws Exception { cassandraHostConfigurator = new CassandraHostConfigurator((String)hostsRefAddr.getContent()); if ( autoDiscoverHosts != null ) { cassandraHostConfigurator.setAutoDiscoverHosts(Boolean.parseBoolean((String)autoDiscoverHosts.getContent())); - if ( autoDiscoverAtStartup != null ) + if ( runAutoDiscoverAtStartup != null ) cassandraHostConfigurator.setRunAutoDiscoveryAtStartup(Boolean.parseBoolean((String)autoDiscoverHosts.getContent())); } if ( retryDownedHostDelayInSeconds != null ) { @@ -112,6 +118,8 @@ private void configure(Reference resourceRef) throws Exception { } if ( maxActiveRefAddr != null ) cassandraHostConfigurator.setMaxActive(Integer.parseInt((String)maxActiveRefAddr.getContent())); + if ( maxWaitTimeWhenExhausted != null ) + cassandraHostConfigurator.setMaxWaitTimeWhenExhausted(Integer.parseInt((String)maxWaitTimeWhenExhausted.getContent())); if ( log.isDebugEnabled() ) log.debug("JNDI resource created with CassandraHostConfiguration: {}", cassandraHostConfigurator.getAutoDiscoverHosts()); From e530e3d4125fae4f10ec0095ffb95e511e5e3736 Mon Sep 17 00:00:00 2001 From: zznate Date: Mon, 14 Mar 2011 14:04:33 -0500 Subject: [PATCH 007/144] added check for excludedhosts being equal to or greater than active in pools. address GH issue 186 --- .../connection/RoundRobinBalancingPolicy.java | 2 ++ .../RoundRobinBalancingPolicyTest.java | 17 +++++++++++++++++ 2 files changed, 19 insertions(+) diff --git a/core/src/main/java/me/prettyprint/cassandra/connection/RoundRobinBalancingPolicy.java b/core/src/main/java/me/prettyprint/cassandra/connection/RoundRobinBalancingPolicy.java index 165426f76..eebe84967 100644 --- a/core/src/main/java/me/prettyprint/cassandra/connection/RoundRobinBalancingPolicy.java +++ b/core/src/main/java/me/prettyprint/cassandra/connection/RoundRobinBalancingPolicy.java @@ -33,6 +33,8 @@ public HClientPool getPool(Collection pools, if ( excludeHosts != null && excludeHosts.size() > 0 ) { while ( excludeHosts.contains(pool.getCassandraHost()) ) { pool = getPoolSafely(pools); + if ( excludeHosts.size() >= pools.size() ) + break; } } return pool; diff --git a/core/src/test/java/me/prettyprint/cassandra/connection/RoundRobinBalancingPolicyTest.java b/core/src/test/java/me/prettyprint/cassandra/connection/RoundRobinBalancingPolicyTest.java index 38d850b34..f59e2d301 100644 --- a/core/src/test/java/me/prettyprint/cassandra/connection/RoundRobinBalancingPolicyTest.java +++ b/core/src/test/java/me/prettyprint/cassandra/connection/RoundRobinBalancingPolicyTest.java @@ -1,5 +1,6 @@ package me.prettyprint.cassandra.connection; +import static org.junit.Assert.*; import static org.junit.Assert.assertEquals; import java.util.Arrays; @@ -46,4 +47,20 @@ public void testIgnoreExhausted() { assertEquals(poolWith10Active, roundRobinBalancingPolicy.getPool(pools, new HashSet(Arrays.asList(new CassandraHost("127.0.0.1:9160"))))); } + @Test + public void testIgnoreExhaustedAll() { + Mockito.when(poolWith5Active.getCassandraHost()).thenReturn(new CassandraHost("127.0.0.1:9160")); + Mockito.when(poolWith7Active.getCassandraHost()).thenReturn(new CassandraHost("127.0.0.2:9161")); + Mockito.when(poolWith10Active.getCassandraHost()).thenReturn(new CassandraHost("127.0.0.3:9162")); + + roundRobinBalancingPolicy = new RoundRobinBalancingPolicy(); + /* + assertEquals(poolWith10Active, roundRobinBalancingPolicy.getPool(pools, + new HashSet(Arrays.asList(new CassandraHost("127.0.0.1:9160"),new CassandraHost("127.0.0.2:9161"))))); + */ + assertNotNull(roundRobinBalancingPolicy.getPool(pools, + new HashSet(Arrays.asList(new CassandraHost("127.0.0.1:9160"),new CassandraHost("127.0.0.2:9161"),new CassandraHost("127.0.0.3:9162"))))); + + + } } From 006225ffc4e51a6d7c5c53632fce02f03752b20b Mon Sep 17 00:00:00 2001 From: zznate Date: Tue, 15 Mar 2011 12:15:46 -0500 Subject: [PATCH 008/144] updated hector version number --- object-mapper/pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/object-mapper/pom.xml b/object-mapper/pom.xml index 52d634838..6b7f768a1 100644 --- a/object-mapper/pom.xml +++ b/object-mapper/pom.xml @@ -39,7 +39,7 @@ me.prettyprint hector-core - 0.7.0-27-SNAPSHOT + 0.7.0-28 org.slf4j From 39b1860473be1501de570bec4268bc7d5e972f35 Mon Sep 17 00:00:00 2001 From: zznate Date: Wed, 16 Mar 2011 14:40:40 -0500 Subject: [PATCH 009/144] added template method design pattern, thx to David Cox for large contribution --- .../AbstractColumnFamilyTemplate.java | 167 +++++++++ .../template/AbstractResultWrapper.java | 75 ++++ .../template/CassandraClusterFactory.java | 30 ++ .../service/template/ColumnFamilyResult.java | 36 ++ .../template/ColumnFamilyResultWrapper.java | 104 ++++++ .../template/ColumnFamilyResultsIterator.java | 17 + .../template/ColumnFamilyRowMapper.java | 14 + .../template/ColumnFamilyStringTemplate.java | 42 +++ .../template/ColumnFamilyStringUpdater.java | 11 + .../template/ColumnFamilyTemplate.java | 282 +++++++++++++++ .../service/template/ColumnFamilyUpdater.java | 107 ++++++ .../service/template/SuperCfResult.java | 18 + .../template/SuperCfResultWrapper.java | 95 +++++ .../service/template/SuperCfRowMapper.java | 19 + .../template/SuperCfStringTemplate.java | 42 +++ .../template/SuperCfStringUpdater.java | 11 + .../service/template/SuperCfTemplate.java | 331 ++++++++++++++++++ .../service/template/SuperCfUpdater.java | 129 +++++++ .../template/ColumnFamilyTemplateTest.java | 84 +++++ 19 files changed, 1614 insertions(+) create mode 100644 core/src/main/java/me/prettyprint/cassandra/service/template/AbstractColumnFamilyTemplate.java create mode 100644 core/src/main/java/me/prettyprint/cassandra/service/template/AbstractResultWrapper.java create mode 100644 core/src/main/java/me/prettyprint/cassandra/service/template/CassandraClusterFactory.java create mode 100644 core/src/main/java/me/prettyprint/cassandra/service/template/ColumnFamilyResult.java create mode 100644 core/src/main/java/me/prettyprint/cassandra/service/template/ColumnFamilyResultWrapper.java create mode 100644 core/src/main/java/me/prettyprint/cassandra/service/template/ColumnFamilyResultsIterator.java create mode 100644 core/src/main/java/me/prettyprint/cassandra/service/template/ColumnFamilyRowMapper.java create mode 100644 core/src/main/java/me/prettyprint/cassandra/service/template/ColumnFamilyStringTemplate.java create mode 100644 core/src/main/java/me/prettyprint/cassandra/service/template/ColumnFamilyStringUpdater.java create mode 100644 core/src/main/java/me/prettyprint/cassandra/service/template/ColumnFamilyTemplate.java create mode 100644 core/src/main/java/me/prettyprint/cassandra/service/template/ColumnFamilyUpdater.java create mode 100644 core/src/main/java/me/prettyprint/cassandra/service/template/SuperCfResult.java create mode 100644 core/src/main/java/me/prettyprint/cassandra/service/template/SuperCfResultWrapper.java create mode 100644 core/src/main/java/me/prettyprint/cassandra/service/template/SuperCfRowMapper.java create mode 100644 core/src/main/java/me/prettyprint/cassandra/service/template/SuperCfStringTemplate.java create mode 100644 core/src/main/java/me/prettyprint/cassandra/service/template/SuperCfStringUpdater.java create mode 100644 core/src/main/java/me/prettyprint/cassandra/service/template/SuperCfTemplate.java create mode 100644 core/src/main/java/me/prettyprint/cassandra/service/template/SuperCfUpdater.java create mode 100644 core/src/test/java/me/prettyprint/cassandra/service/template/ColumnFamilyTemplateTest.java diff --git a/core/src/main/java/me/prettyprint/cassandra/service/template/AbstractColumnFamilyTemplate.java b/core/src/main/java/me/prettyprint/cassandra/service/template/AbstractColumnFamilyTemplate.java new file mode 100644 index 000000000..2b3a72aac --- /dev/null +++ b/core/src/main/java/me/prettyprint/cassandra/service/template/AbstractColumnFamilyTemplate.java @@ -0,0 +1,167 @@ +package me.prettyprint.cassandra.service.template; + +import java.util.HashMap; +import java.util.Map; + +import org.apache.cassandra.thrift.ColumnParent; + +import me.prettyprint.cassandra.model.ExecutingKeyspace; +import me.prettyprint.cassandra.model.HSlicePredicate; +import me.prettyprint.cassandra.service.ExceptionsTranslator; +import me.prettyprint.cassandra.service.ExceptionsTranslatorImpl; +import me.prettyprint.hector.api.Keyspace; +import me.prettyprint.hector.api.Serializer; +import me.prettyprint.hector.api.factory.HFactory; +import me.prettyprint.hector.api.mutation.MutationResult; +import me.prettyprint.hector.api.mutation.Mutator; + +public class AbstractColumnFamilyTemplate { + // Used for queries where we just ask for all columns + public static final int ALL_COLUMNS_COUNT = Integer.MAX_VALUE; + public static final Object ALL_COLUMNS_START = null; + public static final Object ALL_COLUMNS_END = null; + + protected ExecutingKeyspace keyspace; + protected String columnFamily; + protected Serializer keySerializer; + protected Map> columnValueSerializers; + protected ColumnParent columnParent; + protected HSlicePredicate activeSlicePredicate; + + /** The serializer for a standard column name or a super-column name */ + protected Serializer topSerializer; + + /** + * Used for all updates. Can but passed in the constructor/reassigned to allow + * updates between multiple column families/CassandraTemplates to be batched + */ + protected Mutator mutator; + + /** + * By default, execute updates automatically at common-sense points such as + * after queuing the updates of all an object's properties. Or, in the case of + * multiple objects, at the end of the list. No Mutator executes() will be + * called if this is set to true. This allows an arbitrary number of updates + * to be performed and executed manually. + */ + protected boolean batched; + + /** + * An optional clock value to pass to deletes. If null, the default value + * generated by Hector is used + */ + protected Long clock; + + protected ExceptionsTranslator exceptionsTranslator; + + public AbstractColumnFamilyTemplate(Keyspace keyspace, String columnFamily, + Serializer keySerializer, Serializer topSerializer) { + this(keyspace, columnFamily, keySerializer, topSerializer, HFactory + .createMutator(keyspace, keySerializer)); + } + + public AbstractColumnFamilyTemplate(Keyspace keyspace, String columnFamily, + Serializer keySerializer, Serializer topSerializer, + Mutator mutator) { + // ugly, but safe + this.keyspace = (ExecutingKeyspace)keyspace; + this.columnFamily = columnFamily; + this.keySerializer = keySerializer; + this.topSerializer = topSerializer; + this.mutator = mutator; + columnValueSerializers = new HashMap>(); + this.columnParent = new ColumnParent(columnFamily); + this.activeSlicePredicate = new HSlicePredicate(topSerializer); + exceptionsTranslator = new ExceptionsTranslatorImpl(); + } + + + /** + * Add a column to the static set of columns which will be used in constructing + * the single-argument form of slicing operations + * @param columnName + * @param valueSerializer + */ + public AbstractColumnFamilyTemplate addColumn(N columnName, Serializer valueSerializer) { + columnValueSerializers.put(columnName, valueSerializer); + return this; + } + + /** + * Get the value serializer for a given column. Returns null if none found + * @param columnName + * @return + */ + public Serializer getValueSerializer(N columnName) { + return columnValueSerializers.get(columnName); + } + + + public boolean isBatched() { + return batched; + } + + public AbstractColumnFamilyTemplate setBatched(boolean batched) { + this.batched = batched; + return this; + } + + public String getColumnFamily() { + return columnFamily; + } + + public Serializer getKeySerializer() { + return keySerializer; + } + + public Serializer getTopSerializer() { + return topSerializer; + } + + public MutationResult executeBatch() { + return mutator.execute(); + } + + public Mutator getMutator() { + return mutator; + } + + public AbstractColumnFamilyTemplate setMutator(Mutator mutator) { + this.mutator = mutator; + return this; + } + + public Long getClock() { + return clock; + } + + public void setClock(Long clock) { + this.clock = clock; + } + + public long getEffectiveClock() { + return clock != null ? clock.longValue() : keyspace.createClock(); + } + + public void setExceptionsTranslator(ExceptionsTranslator exceptionsTranslator) { + this.exceptionsTranslator = exceptionsTranslator; + } + + protected MutationResult executeIfNotBatched() { + if (!isBatched()) { + return mutator.execute(); + } + return null; + } + + public void deleteRow(K key) { + mutator.addDeletion(key, columnFamily, null, topSerializer); + executeIfNotBatched(); + } + + public void deleteColumn(K key, N columnName) { + mutator.addDeletion(key, columnFamily, columnName, topSerializer); + executeIfNotBatched(); + } + +} diff --git a/core/src/main/java/me/prettyprint/cassandra/service/template/AbstractResultWrapper.java b/core/src/main/java/me/prettyprint/cassandra/service/template/AbstractResultWrapper.java new file mode 100644 index 000000000..2fa4abeb8 --- /dev/null +++ b/core/src/main/java/me/prettyprint/cassandra/service/template/AbstractResultWrapper.java @@ -0,0 +1,75 @@ +package me.prettyprint.cassandra.service.template; + +import java.nio.ByteBuffer; +import java.util.Date; +import java.util.UUID; + +import me.prettyprint.cassandra.serializers.BooleanSerializer; +import me.prettyprint.cassandra.serializers.BytesArraySerializer; +import me.prettyprint.cassandra.serializers.DateSerializer; +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.hector.api.Serializer; + +/** + * Provides access to the current row of data during queries. There is a lot of + * overlap in needs for both standard and super queries. This class consolidates + * what they have in common. All data is read into ByteBuffers and translated to + * a primitive type when requested. + * + * This class is a non-static inner class which inherits the Java generic + * parameters of it's containing CassandraTemplate instance. This allows it to + * inherit the parameter from CassandraTemplate. + * + * The parameters allows this to be used by standard and super column + * queries + * + * @author david + * @since Mar 10, 2011 + * @param + * the standard column name type or the super column's child column + * type + */ +abstract class AbstractResultWrapper implements ColumnFamilyResult { + + protected Serializer keySerializer; + protected Serializer columnNameSerializer; + + public AbstractResultWrapper(Serializer keySerializer, Serializer columnNameSerializer) { + this.keySerializer = keySerializer; + this.columnNameSerializer = columnNameSerializer; + } + + public abstract ByteBuffer getColumnValue(N columnName); + + public UUID getUUID(N columnName) { + return UUIDSerializer.get().fromByteBuffer(getColumnValue(columnName)); + } + + public String getString(N columnName) { + return StringSerializer.get().fromByteBuffer(getColumnValue(columnName)); + } + + public Long getLong(N columnName) { + return LongSerializer.get().fromByteBuffer(getColumnValue(columnName)); + } + + public Integer getInteger(N columnName) { + return IntegerSerializer.get().fromByteBuffer(getColumnValue(columnName)); + } + + public Boolean getBoolean(N columnName) { + return BooleanSerializer.get().fromByteBuffer(getColumnValue(columnName)); + } + + public byte[] getByteArray(N columnName) { + return BytesArraySerializer.get() + .fromByteBuffer(getColumnValue(columnName)); + } + + public Date getDate(N columnName) { + return DateSerializer.get().fromByteBuffer(getColumnValue(columnName)); + } +} diff --git a/core/src/main/java/me/prettyprint/cassandra/service/template/CassandraClusterFactory.java b/core/src/main/java/me/prettyprint/cassandra/service/template/CassandraClusterFactory.java new file mode 100644 index 000000000..917342989 --- /dev/null +++ b/core/src/main/java/me/prettyprint/cassandra/service/template/CassandraClusterFactory.java @@ -0,0 +1,30 @@ +package me.prettyprint.cassandra.service.template; + +import me.prettyprint.hector.api.Cluster; +import me.prettyprint.hector.api.factory.HFactory; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * Factory to remove Cassandra configuration concerns from DAO objects. + * This is invoked via a spring factory method that allows injection of the + * Hector Cluster object into the DAO. + *

+ * + *

+ * @author david + * @since Jan 14, 2011 + * + */ +public class CassandraClusterFactory +{ + static final Logger LOGGER = LoggerFactory.getLogger( CassandraClusterFactory.class ); + + public static Cluster getInstance( String name, String host, int port ) + { + LOGGER.debug( "getInstance: creating cluster name=" + name + ", host=" + host + ", port=" + port ); + return HFactory.getOrCreateCluster( name, host + ":" + port ); + + } +} diff --git a/core/src/main/java/me/prettyprint/cassandra/service/template/ColumnFamilyResult.java b/core/src/main/java/me/prettyprint/cassandra/service/template/ColumnFamilyResult.java new file mode 100644 index 000000000..59fea6016 --- /dev/null +++ b/core/src/main/java/me/prettyprint/cassandra/service/template/ColumnFamilyResult.java @@ -0,0 +1,36 @@ +package me.prettyprint.cassandra.service.template; + +import java.util.Date; +import java.util.Iterator; +import java.util.UUID; + +import me.prettyprint.hector.api.ResultStatus; + +/** + * A commen interface for access to the resuls of a query of either a standard or super column family. + * There are different implementations of this which hide the differences requires of standar/super + * column families. + * + * @author david + * @since Mar 10, 2011 + * @param + * @param + */ +public interface ColumnFamilyResult extends Iterator>,ResultStatus { + public K getKey(); + + public UUID getUUID(N columnName); + + public String getString(N columnName); + + public Long getLong(N columnName); + + public Integer getInteger(N columnName); + + public Boolean getBoolean(N columnName); + + public byte[] getByteArray(N columnName); + + public Date getDate(N columnName); + +} diff --git a/core/src/main/java/me/prettyprint/cassandra/service/template/ColumnFamilyResultWrapper.java b/core/src/main/java/me/prettyprint/cassandra/service/template/ColumnFamilyResultWrapper.java new file mode 100644 index 000000000..4b257b078 --- /dev/null +++ b/core/src/main/java/me/prettyprint/cassandra/service/template/ColumnFamilyResultWrapper.java @@ -0,0 +1,104 @@ +package me.prettyprint.cassandra.service.template; + +import java.nio.ByteBuffer; +import java.util.HashMap; +import java.util.Iterator; +import java.util.List; +import java.util.Map; +import java.util.NoSuchElementException; + +import me.prettyprint.cassandra.model.HColumnImpl; +import me.prettyprint.cassandra.serializers.ByteBufferSerializer; +import me.prettyprint.cassandra.service.CassandraHost; +import me.prettyprint.hector.api.Serializer; +import me.prettyprint.hector.api.beans.HColumn; + +import org.apache.cassandra.thrift.ColumnOrSuperColumn; + +/** + * Wraps the results with as an Iterator. The underlying Iterator has already been advanced + * to the first row upon construction. + * + * @author zznate + */ +public class ColumnFamilyResultWrapper extends AbstractResultWrapper { + + private Map> columns = new HashMap>(); + private Iterator>> rows; + private Map.Entry> entry; + + public ColumnFamilyResultWrapper(Serializer keySerializer, + Serializer columnNameSerializer, + Map> rows) { + super(keySerializer, columnNameSerializer); + this.rows = rows.entrySet().iterator(); + next(); + } + + + public ByteBuffer getColumnValue( N columnName) { + HColumn col = getColumn( columnName ); + return col != null ? col.getValue() : null; + } + + private HColumn getColumn( N columnName ) { + return columns.get( columnName ); + } + + + private void applyToRow(K key, List cosclist) { + HColumn column; + N colName; + for (Iterator iterator = cosclist.iterator(); iterator.hasNext();) { + ColumnOrSuperColumn cosc = iterator.next(); + + colName = columnNameSerializer.fromByteBuffer(cosc.getColumn().name.duplicate()); + column = columns.get(colName); + + if ( column == null ) { + column = new HColumnImpl(cosc.getColumn(), columnNameSerializer, ByteBufferSerializer.get()); + } else { + ((HColumnImpl)column).apply(cosc.getColumn()); + } + columns.put(colName, column); + iterator.remove(); + } + } + + @Override + public K getKey() { + return keySerializer.fromByteBuffer(entry.getKey()); + } + + @Override + public ColumnFamilyResult next() { + if ( !hasNext() ) { + throw new NoSuchElementException("No more rows left on this HColumnFamily"); + } + entry = rows.next(); + applyToRow(keySerializer.fromByteBuffer(entry.getKey()), entry.getValue()); + return this; + } + + @Override + public boolean hasNext() { + return rows.hasNext(); + } + + @Override + public void remove() { + rows.remove(); + } + + @Override + public long getExecutionTimeMicro() { + // TODO Auto-generated method stub + return 0; + } + + @Override + public CassandraHost getHostUsed() { + // TODO Auto-generated method stub + return null; + } +} diff --git a/core/src/main/java/me/prettyprint/cassandra/service/template/ColumnFamilyResultsIterator.java b/core/src/main/java/me/prettyprint/cassandra/service/template/ColumnFamilyResultsIterator.java new file mode 100644 index 000000000..206917381 --- /dev/null +++ b/core/src/main/java/me/prettyprint/cassandra/service/template/ColumnFamilyResultsIterator.java @@ -0,0 +1,17 @@ +package me.prettyprint.cassandra.service.template; + +import java.util.Iterator; + +/** + * Allows iteration of results from a query with the niceties that Hector provides for direct access. + * + * @author david + * @since Mar 10, 2011 + * @param + * @param + */ +public interface ColumnFamilyResultsIterator extends Iterator { + public T getByKey(K key); + + public int getCount(); +} diff --git a/core/src/main/java/me/prettyprint/cassandra/service/template/ColumnFamilyRowMapper.java b/core/src/main/java/me/prettyprint/cassandra/service/template/ColumnFamilyRowMapper.java new file mode 100644 index 000000000..2d76144e8 --- /dev/null +++ b/core/src/main/java/me/prettyprint/cassandra/service/template/ColumnFamilyRowMapper.java @@ -0,0 +1,14 @@ +package me.prettyprint.cassandra.service.template; + +/** + * Converts the contents of a standard column family row into an object. + * + * @author david + * @since Mar 10, 2011 + * @param + * @param standard column type data type + * @param the object type being mapped into + */ +public interface ColumnFamilyRowMapper { + public V mapRow(ColumnFamilyResult results); +} diff --git a/core/src/main/java/me/prettyprint/cassandra/service/template/ColumnFamilyStringTemplate.java b/core/src/main/java/me/prettyprint/cassandra/service/template/ColumnFamilyStringTemplate.java new file mode 100644 index 000000000..eaad5234b --- /dev/null +++ b/core/src/main/java/me/prettyprint/cassandra/service/template/ColumnFamilyStringTemplate.java @@ -0,0 +1,42 @@ +package me.prettyprint.cassandra.service.template; + +import me.prettyprint.cassandra.serializers.StringSerializer; +import me.prettyprint.hector.api.Keyspace; +import me.prettyprint.hector.api.mutation.Mutator; + +/** + * A simple specialization of the generic class for the very common all-string + * column name and key type scenario. This just provided the StringSerializer at + * all the places required by the generic base type. There are method overloads + * for update/query that remove the need to pass serializers anywhere. + * + * @author david + * @since Mar 10, 2011 + */ +public class ColumnFamilyStringTemplate extends ColumnFamilyTemplate { + + public ColumnFamilyStringTemplate(Keyspace keyspace, String columnFamily) { + super(keyspace, columnFamily, StringSerializer.get(), StringSerializer + .get()); + } + + public ColumnFamilyStringTemplate(Keyspace keyspace, String columnFamily, + Mutator mutator) { + super(keyspace, columnFamily, StringSerializer.get(), StringSerializer + .get(), mutator); + } + + // Just so method chaining will return this type instead of the parent class + // for operations down the chain + public ColumnFamilyStringTemplate setBatched(boolean batched) { + super.setBatched(batched); + return this; + } + + // Just so method chaining will return this type instead of the parent class + // for operations down the chain + public ColumnFamilyStringTemplate setMutator(Mutator mutator) { + super.setMutator(mutator); + return this; + } +} diff --git a/core/src/main/java/me/prettyprint/cassandra/service/template/ColumnFamilyStringUpdater.java b/core/src/main/java/me/prettyprint/cassandra/service/template/ColumnFamilyStringUpdater.java new file mode 100644 index 000000000..ea446de3f --- /dev/null +++ b/core/src/main/java/me/prettyprint/cassandra/service/template/ColumnFamilyStringUpdater.java @@ -0,0 +1,11 @@ +package me.prettyprint.cassandra.service.template; + +/** + * A simple specialization of the generic class for the very common all-string + * column name scenario. + * + * @author david + * @since Mar 10, 2011 + */ +public abstract class ColumnFamilyStringUpdater extends ColumnFamilyUpdater { +} diff --git a/core/src/main/java/me/prettyprint/cassandra/service/template/ColumnFamilyTemplate.java b/core/src/main/java/me/prettyprint/cassandra/service/template/ColumnFamilyTemplate.java new file mode 100644 index 000000000..8b11f5a31 --- /dev/null +++ b/core/src/main/java/me/prettyprint/cassandra/service/template/ColumnFamilyTemplate.java @@ -0,0 +1,282 @@ +package me.prettyprint.cassandra.service.template; + +import java.nio.ByteBuffer; +import java.util.ArrayList; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; + +import me.prettyprint.cassandra.model.HSlicePredicate; +import me.prettyprint.cassandra.model.thrift.ThriftConverter; +import me.prettyprint.cassandra.serializers.SerializerTypeInferer; +import me.prettyprint.cassandra.service.Operation; +import me.prettyprint.cassandra.service.OperationType; +import me.prettyprint.hector.api.Keyspace; +import me.prettyprint.hector.api.Serializer; +import me.prettyprint.hector.api.beans.HColumn; +import me.prettyprint.hector.api.exceptions.HectorException; +import me.prettyprint.hector.api.factory.HFactory; +import me.prettyprint.hector.api.mutation.Mutator; +import me.prettyprint.hector.api.query.ColumnQuery; +import me.prettyprint.hector.api.query.CountQuery; +import me.prettyprint.hector.api.query.QueryResult; + +import org.apache.cassandra.thrift.Cassandra; +import org.apache.cassandra.thrift.ColumnOrSuperColumn; + +import com.google.common.collect.Iterators; + +/** + * This applies a Template Method pattern, much like Spring's JdbcTemplate, to + * Cassandra. The ColumnFamilyTemplate instance maintains many of the fields in + * common between various query/update operations so that they do not need to be + * constantly passed for every operation on the column family. These include the + * keyspace, column family name, key serializer, and the column name serializer + * (for standard column name or the super column name). + * + * The Java generic types of the ColumnFamilyTemplate class itself are limited to + * the key and column name type. It defers the generic types for super column + * child types to the individual update/query operation. + * + * @author david + * @author zznate + * @param + * The column family key type + * @param + * The column family name type + */ +public class ColumnFamilyTemplate extends AbstractColumnFamilyTemplate { + + public ColumnFamilyTemplate(Keyspace keyspace, String columnFamily, + Serializer keySerializer, Serializer topSerializer) { + super(keyspace, columnFamily, keySerializer, topSerializer); + } + + public ColumnFamilyTemplate(Keyspace keyspace, String columnFamily, + Serializer keySerializer, Serializer topSerializer, + Mutator mutator) { + super(keyspace, columnFamily, keySerializer, topSerializer, mutator); + } + + // Just so method chaining will return this type instead of the parent class + // for operations down the chain + public ColumnFamilyTemplate setBatched(boolean batched) { + super.setBatched(batched); + return this; + } + + // Just so method chaining will return this type instead of the parent class + // for operations down the chain + public ColumnFamilyTemplate setMutator(Mutator mutator) { + super.setMutator(mutator); + return this; + } + + public ColumnFamilyUpdater createUpdater(K key) { + ColumnFamilyUpdater updater = new ColumnFamilyUpdater(); + updater.key = key; + updater.template = this; + return updater; + } + + public void update(ColumnFamilyUpdater updater) { + updater.update(); + executeIfNotBatched(); + } + + /** + * Updates values in a standard column family in the row specified by key. + * + * @param key + * the row key + * @param updater + * the object performing updates of the current row + */ + public void update(K key, ColumnFamilyUpdater updater) { + updater.template = this; + updater.key = key; + update(updater); + } + + + /** + * Checks if there are any columns at a row specified by key in a standard + * column family + * + * @param key + * @return true if columns exist + */ + public boolean isColumnsExist(K key) { + return countColumns(key) > 0; + } + + /** + * @param key + * @return the number of columns in a standard column family at the specified + * row key + */ + @SuppressWarnings("unchecked") + public int countColumns(K key) { + return countColumns(key, (N) ALL_COLUMNS_START, (N) ALL_COLUMNS_END, + ALL_COLUMNS_COUNT); + } + + /** + * Counts columns in the specified range of a standard column family + * + * @param key + * @param start + * @param end + * @param max + * @return + */ + public int countColumns(K key, N start, N end, int max) { + CountQuery query = HFactory.createCountQuery(keyspace, keySerializer, + topSerializer); + query.setKey(key); + query.setColumnFamily(columnFamily); + query.setRange(start, end, max); + return query.execute().get(); + } + + public ColumnFamilyResultWrapper queryColumns(K key) { + return doExecuteSlice(key, null); + } + + + @SuppressWarnings("unchecked") + public T queryColumns(K key, ColumnFamilyRowMapper mapper) { + return queryColumns(key, (N) ALL_COLUMNS_START, (N) ALL_COLUMNS_END, mapper); + } + + /** + * Queries a range of columns at the given key and maps them to an object of + * type OBJ using the given mapping object + * + * @param + * @param key + * @param start + * @param end + * @param mapper + * @return + */ + public T queryColumns(K key, N start, N end, + ColumnFamilyRowMapper mapper) { + HSlicePredicate predicate = new HSlicePredicate(topSerializer); + predicate.setStartOn(start); + predicate.setEndOn(end); + predicate.setCount(100); + return executeSliceQuery(key, predicate, mapper); + } + + /** + * Queries all columns at a given key and maps them to an object of type OBJ + * using the given mapping object + * + * @param + * @param key + * @param columns + * @param mapper + * @return + */ + @SuppressWarnings("unchecked") + public T queryColumns(K key, List columns, + ColumnFamilyRowMapper mapper) { + HSlicePredicate predicate = new HSlicePredicate(topSerializer); + predicate.setColumnNames(columns); + return executeSliceQuery(key, predicate, mapper); + } + + private T executeSliceQuery(K key, HSlicePredicate predicate, ColumnFamilyRowMapper mapper) { + return mapper.mapRow(doExecuteSlice(key,predicate)); + } + + +/* @SuppressWarnings("unchecked") + public ColumnFamilyResultsIterator queryColumns(Iterable keys, + ColumnFamilyRowMapper mapper) { + return queryColumns(keys, (N) ALL_COLUMNS_START, (N) ALL_COLUMNS_END, + mapper); + } + + public ColumnFamilyResults queryColumns(Iterable keys, + N start, N end, ColumnFamilyRowMapper mapper) { + MultigetSliceQuery query = createMultigetSliceQuery(keys); + query.setRange(start, end, false, ALL_COLUMNS_COUNT); + return executeMultigetSliceQuery(keys, query, mapper); + } + + @SuppressWarnings("unchecked") + public ColumnFamilyResults queryColumns(Iterable key, + List columns, ColumnFamilyRowMapper mapper) { + + HSlicePredicate predicate = new HSlicePredicate(topSerializer); + predicate.setColumnNames(columns); + return mapper.mapRow(doExecuteMultigetSlice(key, predicate)); + } +*/ + + + @SuppressWarnings("unchecked") + public HColumn querySingleColumn(K key, N columnName, + Class valueClass) { + return querySingleColumn(key, columnName, + (Serializer) SerializerTypeInferer.getSerializer(valueClass)); + } + + public HColumn querySingleColumn(K key, N columnName, + Serializer valueSerializer) { + ColumnQuery query = HFactory.createColumnQuery(keyspace, + keySerializer, topSerializer, valueSerializer); + query.setColumnFamily(columnFamily); + query.setKey(key); + query.setName(columnName); + QueryResult> result = query.execute(); + return result != null ? result.get() : null; + } + + private ColumnFamilyResultWrapper doExecuteSlice(final K key, final HSlicePredicate workingSlicePredicate) { + + return keyspace.doExecuteOperation(new Operation>(OperationType.READ) { + @Override + public ColumnFamilyResultWrapper execute(Cassandra.Client cassandra) throws HectorException { + Map> cosc = new LinkedHashMap>(); + try { + + ByteBuffer sKey = keySerializer.toByteBuffer(key); + cosc.put(sKey, cassandra.get_slice(sKey, columnParent, + (workingSlicePredicate == null ? activeSlicePredicate.setColumnNames(columnValueSerializers.keySet()).toThrift() : workingSlicePredicate.toThrift()), + ThriftConverter.consistencyLevel(consistencyLevelPolicy.get(operationType)))); + + } catch (Exception e) { + throw exceptionsTranslator.translate(e); + } + + return new ColumnFamilyResultWrapper(keySerializer, topSerializer, cosc); + } + }).get(); + } + + private ColumnFamilyResultWrapper doExecuteMultigetSlice(final Iterable keys, final HSlicePredicate workingSlicePredicate) { + + return keyspace.doExecuteOperation(new Operation>(OperationType.READ) { + @Override + public ColumnFamilyResultWrapper execute(Cassandra.Client cassandra) throws HectorException { + Map> cosc; + try { + List keyList = new ArrayList(); + Iterators.addAll(keyList, keys.iterator()); + cosc = cassandra.multiget_slice(keySerializer.toBytesList(keyList), columnParent, + (workingSlicePredicate == null ? activeSlicePredicate.setColumnNames(columnValueSerializers.keySet()).toThrift() : workingSlicePredicate.toThrift()), + ThriftConverter.consistencyLevel(consistencyLevelPolicy.get(operationType))); + + } catch (Exception e) { + throw exceptionsTranslator.translate(e); + } + + return new ColumnFamilyResultWrapper(keySerializer, topSerializer, cosc); + } + }).get(); + } + +} diff --git a/core/src/main/java/me/prettyprint/cassandra/service/template/ColumnFamilyUpdater.java b/core/src/main/java/me/prettyprint/cassandra/service/template/ColumnFamilyUpdater.java new file mode 100644 index 000000000..59d9c8bcb --- /dev/null +++ b/core/src/main/java/me/prettyprint/cassandra/service/template/ColumnFamilyUpdater.java @@ -0,0 +1,107 @@ +package me.prettyprint.cassandra.service.template; + +import java.util.Date; +import java.util.UUID; + +import me.prettyprint.cassandra.serializers.BooleanSerializer; +import me.prettyprint.cassandra.serializers.BytesArraySerializer; +import me.prettyprint.cassandra.serializers.DateSerializer; +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.hector.api.Serializer; +import me.prettyprint.hector.api.beans.HColumn; +import me.prettyprint.hector.api.factory.HFactory; +import me.prettyprint.hector.api.mutation.Mutator; + +/** + * This provides an interface of updating a specified row, most likely with the + * contents of an object. This would likely by implemented as an anonymous inner + * class with access to a final object in scope. It would update the given row + * with the object's data. + * + * For more complex behaviour, subclasses should implementat update() to simply make + * consecutive calls to various set****() methods which already have the + * contextual information they need to update the correct row. + * + * The downside of this approach is that the updater is essentially stateful and + * cannot be used concurrently. The alternative is to pass an object in to + * update() as a parameter with the setter methods, leaving the updater to be + * stateless. + * + * @author david + * @author zznate + * + * @param + * the key's data type + * @param + * the standard or super column's data type + */ +public class ColumnFamilyUpdater { + // Values have package access and are assigned by CassandraTemplate + ColumnFamilyTemplate template; + K key; + + /** + * Default no-op update implementation. Sub-classes should override this to provide + * for more complex behaviour + */ + public void update() { + // TODO think about this contract in general + } + + /** + * @return Give the updater access to the current key if it needs it + */ + public K getKey() { + return key; + } + + public void deleteColumn(N columnName) { + template.getMutator().addDeletion(key, template.getColumnFamily(), + columnName, template.getTopSerializer()); + } + + public void setString(N columnName, String value) { + HColumn column = HFactory.createColumn(columnName, value, + template.getTopSerializer(), StringSerializer.get()); + template.getMutator().addInsertion(key, template.getColumnFamily(), column); + } + + public void setUUID(N columnName, UUID value) { + HColumn column = HFactory.createColumn(columnName, value, + template.getTopSerializer(), UUIDSerializer.get()); + template.getMutator().addInsertion(key, template.getColumnFamily(), column); + } + + public void setLong(N columnName, Long value) { + HColumn column = HFactory.createColumn(columnName, value, + template.getTopSerializer(), LongSerializer.get()); + template.getMutator().addInsertion(key, template.getColumnFamily(), column); + } + + public void setInteger(N columnName, Integer value) { + HColumn column = HFactory.createColumn(columnName, value, + template.getTopSerializer(), IntegerSerializer.get()); + template.getMutator().addInsertion(key, template.getColumnFamily(), column); + } + + public void setBoolean(N columnName, Boolean value) { + HColumn column = HFactory.createColumn(columnName, value, + template.getTopSerializer(), BooleanSerializer.get()); + template.getMutator().addInsertion(key, template.getColumnFamily(), column); + } + + public void setByteArray(N columnName, byte[] value) { + HColumn column = HFactory.createColumn(columnName, value, + template.getTopSerializer(), BytesArraySerializer.get()); + template.getMutator().addInsertion(key, template.getColumnFamily(), column); + } + + public void setDate(N columnName, Date value) { + HColumn column = HFactory.createColumn(columnName, value, + template.getTopSerializer(), DateSerializer.get()); + template.getMutator().addInsertion(key, template.getColumnFamily(), column); + } +} diff --git a/core/src/main/java/me/prettyprint/cassandra/service/template/SuperCfResult.java b/core/src/main/java/me/prettyprint/cassandra/service/template/SuperCfResult.java new file mode 100644 index 000000000..da851aa5f --- /dev/null +++ b/core/src/main/java/me/prettyprint/cassandra/service/template/SuperCfResult.java @@ -0,0 +1,18 @@ +package me.prettyprint.cassandra.service.template; + +/** + * Holds the result for the contents of a super column. This interface add + * access to the current super column name since this may be a property of the + * object being mapped into. + * + * @author david + * @since Mar 10, 2011 + * @param + * @param + * super column name data type + * @param + * child column name data type + */ +public interface SuperCfResult extends ColumnFamilyResult { + public SN getSuperName(); +} diff --git a/core/src/main/java/me/prettyprint/cassandra/service/template/SuperCfResultWrapper.java b/core/src/main/java/me/prettyprint/cassandra/service/template/SuperCfResultWrapper.java new file mode 100644 index 000000000..131520633 --- /dev/null +++ b/core/src/main/java/me/prettyprint/cassandra/service/template/SuperCfResultWrapper.java @@ -0,0 +1,95 @@ +package me.prettyprint.cassandra.service.template; + +import java.nio.ByteBuffer; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import me.prettyprint.cassandra.service.CassandraHost; +import me.prettyprint.hector.api.Serializer; +import me.prettyprint.hector.api.beans.HColumn; +import me.prettyprint.hector.api.beans.HSuperColumn; + +/** + * Provides access to the current row of data during super column queries. + * + * This class is a non-static inner class which inherits the Java generic parameters + * of it's containing CassandraTemplate instance. This allows it to inherit the + * and parameter and adds the type passed in from whichever + * query***() method was called. + * + * @author david + * @since Mar 10, 2011 + * @param the super column's sub column name type + */ +public class SuperCfResultWrapper extends AbstractResultWrapper implements SuperCfResult { + + + public SuperCfResultWrapper(Serializer keySerializer, + Serializer columnNameSerializer) { + super(keySerializer, columnNameSerializer); + // TODO Auto-generated constructor stub + } + + private HSuperColumn superColumn; + private List> subColumns; + private Map> subColumnsMap = new HashMap>(); + + + + /** + * Provides access to the current super column name from the mapper objects. This may be + * needed when the super columm name may be part of the object being mapped into. + */ + public SN getSuperName() + { + return superColumn.getName(); + } + + public ByteBuffer getColumnValue( N columnName) + { + HColumn col = getColumn( columnName ); + return col != null ? col.getValue() : null; + } + + private HColumn getColumn( N columnName ) + { + return subColumnsMap.get( columnName ); + } + + @Override + public K getKey() { + // TODO Auto-generated method stub + return null; + } + + @Override + public boolean hasNext() { + // TODO Auto-generated method stub + return false; + } + + @Override + public ColumnFamilyResult next() { + // TODO Auto-generated method stub + return null; + } + + @Override + public void remove() { + // TODO Auto-generated method stub + + } + + @Override + public long getExecutionTimeMicro() { + // TODO Auto-generated method stub + return 0; + } + + @Override + public CassandraHost getHostUsed() { + // TODO Auto-generated method stub + return null; + } +} diff --git a/core/src/main/java/me/prettyprint/cassandra/service/template/SuperCfRowMapper.java b/core/src/main/java/me/prettyprint/cassandra/service/template/SuperCfRowMapper.java new file mode 100644 index 000000000..97210c1cf --- /dev/null +++ b/core/src/main/java/me/prettyprint/cassandra/service/template/SuperCfRowMapper.java @@ -0,0 +1,19 @@ +package me.prettyprint.cassandra.service.template; + +/** + * Converts the contents of a given super column into an object. It is passed a + * results object with the data of the current super column. + * + * @author david + * @since Mar 10, 2011 + * @param + * @param + * the super column name data type + * @param + * the child column data type + * @param + * the object being mapped to datatype + */ +public interface SuperCfRowMapper { + public V mapRow(SuperCfResult results); +} diff --git a/core/src/main/java/me/prettyprint/cassandra/service/template/SuperCfStringTemplate.java b/core/src/main/java/me/prettyprint/cassandra/service/template/SuperCfStringTemplate.java new file mode 100644 index 000000000..6d31e2463 --- /dev/null +++ b/core/src/main/java/me/prettyprint/cassandra/service/template/SuperCfStringTemplate.java @@ -0,0 +1,42 @@ +package me.prettyprint.cassandra.service.template; + +import me.prettyprint.cassandra.serializers.StringSerializer; +import me.prettyprint.hector.api.Keyspace; +import me.prettyprint.hector.api.mutation.Mutator; + +/** + * A simple specialization of the generic class for the very common all-string + * column name and key type scenario. This just provided the StringSerializer at + * all the places required by the generic base type. There are method overloads + * for update/query that remove the need to pass serializers anywhere. + * + * @author david + * @since Mar 10, 2011 + */ +public class SuperCfStringTemplate extends SuperCfTemplate { + + public SuperCfStringTemplate(Keyspace keyspace, String columnFamily) { + super(keyspace, columnFamily, StringSerializer.get(), StringSerializer + .get(), StringSerializer.get()); + } + + public SuperCfStringTemplate(Keyspace keyspace, String columnFamily, + Mutator mutator) { + super(keyspace, columnFamily, StringSerializer.get(), StringSerializer + .get(), StringSerializer.get(), mutator); + } + + // Just so method chaining will return this type instead of the parent class + // for operations down the chain + public SuperCfStringTemplate setBatched(boolean batched) { + super.setBatched(batched); + return this; + } + + // Just so method chaining will return this type instead of the parent class + // for operations down the chain + public SuperCfStringTemplate setMutator(Mutator mutator) { + super.setMutator(mutator); + return this; + } +} diff --git a/core/src/main/java/me/prettyprint/cassandra/service/template/SuperCfStringUpdater.java b/core/src/main/java/me/prettyprint/cassandra/service/template/SuperCfStringUpdater.java new file mode 100644 index 000000000..7309a38c6 --- /dev/null +++ b/core/src/main/java/me/prettyprint/cassandra/service/template/SuperCfStringUpdater.java @@ -0,0 +1,11 @@ +package me.prettyprint.cassandra.service.template; + +/** + * A simple specialization of the generic class for the very common all-string + * column name scenario. + * + * @author david + * @since Mar 10, 2011 + */ +public abstract class SuperCfStringUpdater extends SuperCfUpdater { +} diff --git a/core/src/main/java/me/prettyprint/cassandra/service/template/SuperCfTemplate.java b/core/src/main/java/me/prettyprint/cassandra/service/template/SuperCfTemplate.java new file mode 100644 index 000000000..753381e0e --- /dev/null +++ b/core/src/main/java/me/prettyprint/cassandra/service/template/SuperCfTemplate.java @@ -0,0 +1,331 @@ +package me.prettyprint.cassandra.service.template; + +import java.nio.ByteBuffer; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.Iterator; +import java.util.List; +import java.util.Map; + +import me.prettyprint.cassandra.serializers.ByteBufferSerializer; +import me.prettyprint.cassandra.serializers.SerializerTypeInferer; +import me.prettyprint.hector.api.Keyspace; +import me.prettyprint.hector.api.Serializer; +import me.prettyprint.hector.api.beans.HColumn; +import me.prettyprint.hector.api.beans.HSuperColumn; +import me.prettyprint.hector.api.beans.SuperRow; +import me.prettyprint.hector.api.beans.SuperRows; +import me.prettyprint.hector.api.beans.SuperSlice; +import me.prettyprint.hector.api.factory.HFactory; +import me.prettyprint.hector.api.mutation.Mutator; +import me.prettyprint.hector.api.query.MultigetSuperSliceQuery; +import me.prettyprint.hector.api.query.QueryResult; +import me.prettyprint.hector.api.query.SubColumnQuery; +import me.prettyprint.hector.api.query.SubCountQuery; +import me.prettyprint.hector.api.query.SuperCountQuery; +import me.prettyprint.hector.api.query.SuperSliceQuery; + +public class SuperCfTemplate extends AbstractColumnFamilyTemplate { + private Serializer subSerializer; + + public SuperCfTemplate(Keyspace keyspace, String columnFamily, + Serializer keySerializer, Serializer topSerializer, + Serializer subSerializer) { + super(keyspace, columnFamily, keySerializer, topSerializer); + this.subSerializer = subSerializer; + } + + public SuperCfTemplate(Keyspace keyspace, String columnFamily, + Serializer keySerializer, Serializer topSerializer, + Serializer subSerializer, Mutator mutator) { + super(keyspace, columnFamily, keySerializer, topSerializer, mutator); + + } + + /** + * Checks if there are any columns at a row specified by key in a super column + * family + * + * @param key + * @return true if columns exist + */ + public boolean isColumnsExist(K key) { + return countColumns(key) > 0; + } + + /** + * Checks if there are any columns at a row specified by key in a specific + * super column + * + * @param key + * @param superColumnName + * @param subSerializer + * the column name serializer of the child columns + * @return true if columns exist + */ + public boolean isSubColumnsExist(K key, SN superColumnName) { + return countSubColumns(key, superColumnName) > 0; + } + + /** + * @param key + * @return the number of columns in a super column family at the specified row + * key + */ + @SuppressWarnings("unchecked") + public int countColumns(K key) { + return countColumns(key, (SN) ALL_COLUMNS_START, (SN) ALL_COLUMNS_END, + ALL_COLUMNS_COUNT); + } + + /** + * + * @param key + * @param superColumnName + * @return the number of child columns in a specified super column + */ + @SuppressWarnings("unchecked") + public int countSubColumns(K key, SN superColumnName) { + return countSubColumns(key, superColumnName, (N) ALL_COLUMNS_START, + (N) ALL_COLUMNS_END, ALL_COLUMNS_COUNT); + } + + /** + * Counts columns in the specified range of a super column family + * + * @param key + * @param start + * @param end + * @param max + * @return + */ + public int countColumns(K key, SN start, SN end, int max) { + SuperCountQuery query = HFactory.createSuperCountQuery(keyspace, + keySerializer, topSerializer); + query.setKey(key); + query.setColumnFamily(columnFamily); + query.setRange(start, end, max); + return query.execute().get(); + } + + /** + * Counts child columns in the specified range of a children in a specified + * super column + * + * @param + * @param key + * @param superColumnName + * @param start + * @param end + * @param max + * @param subSerializer + * @return + */ + public int countSubColumns(K key, SN superColumnName, N start, + N end, int max) { + SubCountQuery query = HFactory.createSubCountQuery( + keyspace, keySerializer, topSerializer, subSerializer); + query.setKey(key); + query.setColumnFamily(columnFamily); + query.setSuperColumn(superColumnName); + query.setRange(start, end, max); + return query.execute().get(); + } + + @SuppressWarnings("unchecked") + public HColumn querySingleSubColumn(K key, + SN columnName, N subColumnName, Class valueClass) { + return querySingleSubColumn(key, columnName, subColumnName, + (Serializer) SerializerTypeInferer.getSerializer(valueClass)); + } + + public HColumn querySingleSubColumn(K key, + SN columnName, N subColumnName, Serializer valueSerializer) { + SubColumnQuery query = HFactory + .createSubColumnQuery(keyspace, keySerializer, topSerializer, + subSerializer, valueSerializer); + query.setColumnFamily(columnFamily); + query.setKey(key); + query.setSuperColumn(columnName); + query.setColumn(subColumnName); + QueryResult> result = query.execute(); + return result != null ? result.get() : null; + } + + /** + * Writes a series of object in a row of a super column family. Each object's + * properties are written to a super-column's child columns + * + * @param + * the type of Object to be persisted + * @param + * the type of the sub column name + * @param key + * the row key + * @param objects + * list of objects to write to the db + * @param subSerializer + * the serializer for the child columns in the super column + * @param updater + * the object which performs updates on a + */ + public void update(K key, List objects, + SuperCfUpdater updater) { + if (objects == null || objects.size() == 0) { + return; + } + + updater.key = key; + updater.subSerializer = subSerializer; + + for (OBJ obj : objects) { + updater.columns = new ArrayList>(); + + SN columnName = updater.update(obj); + + HSuperColumn superColumn = HFactory + .createSuperColumn(columnName, updater.columns, topSerializer, + subSerializer, ByteBufferSerializer.get()); + mutator.addInsertion(key, columnFamily, superColumn); + + if (updater.columnsToDelete != null) { + HSuperColumn superColumnWithDeletes = HFactory + .createSuperColumn(columnName, updater.columnsToDelete, + topSerializer, subSerializer, ByteBufferSerializer.get()); + mutator.addSubDelete(key, columnFamily, superColumnWithDeletes); + updater.columnsToDelete = null; + } + } + executeIfNotBatched(); + } + + @SuppressWarnings("unchecked") + public List querySuperColumns(K key, + SuperCfRowMapper mapper) { + return querySuperColumns(key, (SN) ALL_COLUMNS_START, + (SN) ALL_COLUMNS_END, mapper); + } + + public List querySuperColumns(K key, SN start, SN end, + SuperCfRowMapper mapper) { + SuperSliceQuery query = createSuperSliceQuery(key); + query.setRange(start, end, false, ALL_COLUMNS_COUNT); + return executeSuperSliceQuery(key, query, mapper); + } + + @SuppressWarnings("unchecked") + public List querySuperColumns(K key, List columns, + SuperCfRowMapper mapper) { + SuperSliceQuery query = createSuperSliceQuery(key); + query.setColumnNames((SN[]) columns.toArray()); + return executeSuperSliceQuery(key, query, mapper); + } + + private List executeSuperSliceQuery(K key, + SuperSliceQuery query, + SuperCfRowMapper mapper) { + QueryResult> result = query.execute(); + SuperSlice slice = result.get(); + if (slice.getSuperColumns() != null && slice.getSuperColumns().size() > 0) { + List ret = new ArrayList(); + for (HSuperColumn superCol : slice + .getSuperColumns()) { + /*SuperCfResultWrapper wrapper = new SuperCfResultWrapper(key, superCol); + ret.add(mapper.mapRow(wrapper));*/ + } + return ret; + } + return null; + } + + private SuperSliceQuery createSuperSliceQuery( + K key) { + SuperSliceQuery query = HFactory + .createSuperSliceQuery(keyspace, keySerializer, topSerializer, + subSerializer, ByteBufferSerializer.get()); + query.setKey(key); + query.setColumnFamily(columnFamily); + return query; + } + + @SuppressWarnings("unchecked") + public ColumnFamilyResultsIterator> querySuperColumns( + List key, SuperCfRowMapper mapper) { + return querySuperColumns(key, (SN) ALL_COLUMNS_START, + (SN) ALL_COLUMNS_END, mapper); + } + + public ColumnFamilyResultsIterator> querySuperColumns( + List key, SN start, SN end, + SuperCfRowMapper mapper) { + MultigetSuperSliceQuery query = createMultigetSuperSliceQuery(key); + query.setRange(start, end, false, ALL_COLUMNS_COUNT); + return executeMultigetSuperSliceQuery(key, query, mapper); + } + + @SuppressWarnings("unchecked") + public ColumnFamilyResultsIterator> querySuperColumns( + List key, List columns, + SuperCfRowMapper mapper) { + MultigetSuperSliceQuery query = createMultigetSuperSliceQuery(key); + query.setColumnNames((SN[]) columns.toArray()); + return executeMultigetSuperSliceQuery(key, query, mapper); + } + + private ColumnFamilyResultsIterator> executeMultigetSuperSliceQuery( + final List key, + MultigetSuperSliceQuery query, + final SuperCfRowMapper mapper) { + QueryResult> result = query + .execute(); + final SuperRows rows = result.get(); + + final Iterator> iter = rows + .iterator(); + return new ColumnFamilyResultsIterator>() { + public boolean hasNext() { + return iter.hasNext(); + } + + public List next() { + return mapRow(iter.next()); + } + + public void remove() { + throw new UnsupportedOperationException("remove() is not supported"); + } + + public List getByKey(K key) { + return mapRow(rows.getByKey(key)); + } + + public int getCount() { + return rows.getCount(); + } + + private List mapRow(SuperRow row) { + SuperSlice slice = row.getSuperSlice(); + List ret = new ArrayList(); + for (HSuperColumn superCol : slice + .getSuperColumns()) { +/* SuperCfResultWrapper wrapper = new SuperCfResultWrapper(row.getKey(), + superCol); + ret.add(mapper.mapRow(wrapper));*/ + } + return ret; + } + }; + } + + @SuppressWarnings("unchecked") + private MultigetSuperSliceQuery createMultigetSuperSliceQuery( + List keys) { + MultigetSuperSliceQuery query = HFactory + .createMultigetSuperSliceQuery(keyspace, keySerializer, topSerializer, + subSerializer, ByteBufferSerializer.get()); + query.setKeys((K[]) keys.toArray()); + query.setColumnFamily(columnFamily); + return query; + } + +} diff --git a/core/src/main/java/me/prettyprint/cassandra/service/template/SuperCfUpdater.java b/core/src/main/java/me/prettyprint/cassandra/service/template/SuperCfUpdater.java new file mode 100644 index 000000000..69a6b038e --- /dev/null +++ b/core/src/main/java/me/prettyprint/cassandra/service/template/SuperCfUpdater.java @@ -0,0 +1,129 @@ +package me.prettyprint.cassandra.service.template; + +import java.nio.ByteBuffer; +import java.util.ArrayList; +import java.util.Date; +import java.util.List; +import java.util.UUID; + +import me.prettyprint.cassandra.model.HColumnImpl; +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.IntegerSerializer; +import me.prettyprint.cassandra.serializers.LongSerializer; +import me.prettyprint.cassandra.serializers.StringSerializer; +import me.prettyprint.cassandra.serializers.UUIDSerializer; +import me.prettyprint.hector.api.Serializer; +import me.prettyprint.hector.api.beans.HColumn; +import me.prettyprint.hector.api.beans.HSuperColumn; +import me.prettyprint.hector.api.factory.HFactory; +import me.prettyprint.hector.api.mutation.Mutator; + +/** + * This provides an interface of updating a specified row, most likely with the + * contents of an object. This would likely by implemented as an anonymous inner + * class with access to a final object in scope. It would update the given row + * with the object's data. + * + * This is currently implemented as an abstract base class instead of an + * interface. This could change in the future. Being an abstract base class + * allows CassandraTemplate to initialize this instance through package scope + * field access. This means that implementation of update() simply makes + * consecutive calls to various set****() methods which already have the + * contextual information they need to update the correct row. + * + * The downside of this approach is that the updater is essentially stateful and + * cannot be used concurrently. The alternative is to pass an object in to + * update() as a parameter with the setter methods, leaving the updater to be + * stateless. + * + * @author david + * @since Mar 10, 2011 + * @param + * the key's data type + * @param + * the standard or super column's data type + * @param + * the child column name type in a super column + * @param + * the object instance to persist + */ +public abstract class SuperCfUpdater { + // Values have package access and are assigned by CassandraTemplate + K key; + Serializer subSerializer; + List> columns; + List> columnsToDelete; + + /** + * The CassandraTemplate update methods for super columns take a list of + * objects to persist. The update() method of this updater is call once for + * every object in the collection. The appropriate internally variables are + * initialized between calls so that the correct is saved. + * + * Since the updater has intimate knowledge of the object being persisted, it + * should also know which super column this is being saved into. It conveys + * this to the CassandraTemplate by returning the value which will be the + * super column name these updates will go into. + * + * @param obj + * @return the super column name where the object contents are saved + */ + public abstract SN update(V obj); + + /** + * Give the updater access to the current key being updated + * + * @return + */ + public K getKey() { + return key; + } + + public void deleteColumn(N columnName) { + if (columnsToDelete == null) { + columnsToDelete = new ArrayList>(); + } + HColumn col = new HColumnImpl( + subSerializer, ByteBufferSerializer.get()); + col.setName(columnName); + columnsToDelete.add(col); + } + + public void setString(N columnName, String value) { + columns.add(HFactory.createColumn(columnName, StringSerializer.get() + .toByteBuffer(value), subSerializer, ByteBufferSerializer.get())); + } + + public void setUUID(N columnName, UUID value) { + columns.add(HFactory.createColumn(columnName, UUIDSerializer.get() + .toByteBuffer(value), subSerializer, ByteBufferSerializer.get())); + } + + public void setLong(N columnName, Long value) { + columns.add(HFactory.createColumn(columnName, LongSerializer.get() + .toByteBuffer(value), subSerializer, ByteBufferSerializer.get())); + } + + public void setInteger(N columnName, Integer value) { + columns.add(HFactory.createColumn(columnName, IntegerSerializer.get() + .toByteBuffer(value), subSerializer, ByteBufferSerializer.get())); + } + + public void setBoolean(N columnName, Boolean value) { + columns.add(HFactory.createColumn(columnName, BooleanSerializer.get() + .toByteBuffer(value), subSerializer, ByteBufferSerializer.get())); + } + + public void setByteArray(N columnName, byte[] value) { + columns.add(HFactory.createColumn(columnName, BytesArraySerializer.get() + .toByteBuffer(value), subSerializer, ByteBufferSerializer.get())); + } + + public void setDate(N columnName, Date value) { + columns.add(HFactory.createColumn(columnName, DateSerializer.get() + .toByteBuffer(value), subSerializer, ByteBufferSerializer.get())); + } +} diff --git a/core/src/test/java/me/prettyprint/cassandra/service/template/ColumnFamilyTemplateTest.java b/core/src/test/java/me/prettyprint/cassandra/service/template/ColumnFamilyTemplateTest.java new file mode 100644 index 000000000..eed57504f --- /dev/null +++ b/core/src/test/java/me/prettyprint/cassandra/service/template/ColumnFamilyTemplateTest.java @@ -0,0 +1,84 @@ +package me.prettyprint.cassandra.service.template; + +import static me.prettyprint.hector.api.factory.HFactory.createKeyspace; +import static me.prettyprint.hector.api.factory.HFactory.getOrCreateCluster; +import static org.junit.Assert.*; + +import org.junit.Before; +import org.junit.Test; + +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.ResultStatus; +import me.prettyprint.hector.api.factory.HFactory; + +public class ColumnFamilyTemplateTest extends BaseEmbededServerSetupTest { + + private Keyspace keyspace; + private static final StringSerializer se = StringSerializer.get(); + + @Before + public void setupLocal() { + Cluster cluster = getOrCreateCluster("MyCluster", "127.0.0.1:9170"); + keyspace = createKeyspace("Keyspace1", cluster); + } + + @Test + public void testCreateSelect() { + ColumnFamilyTemplate template = new ColumnFamilyTemplate(keyspace, "Standard1", se, se, HFactory.createMutator(keyspace, se)); + + + ColumnFamilyUpdater updater = template.createUpdater("key1"); // createStaticUpater() + updater.setString("column1","value1"); + template.update(updater); + /* + template.update("key1", new ColumnFamilyUpdater() { + @Override + public void update() { + setString("column1", "value1"); + } + }); + */ + template.addColumn("column1", se); + ColumnFamilyResultWrapper wrapper = template.queryColumns("key1"); + + assertEquals("value1",wrapper.getString("column1")); + +/* String value = template.queryColumns("key1", "", "", new ColumnFamilyRowMapper() { + @Override + public String mapRow(ColumnFamilyResult results) { + // TODO Auto-generated method stub + return results.getString("column1"); + } + }); + assertEquals("value1",value); + */ + + + } + + @Test + public void testCreateSelectTemplate() { + ColumnFamilyTemplate template = new ColumnFamilyTemplate(keyspace, "Standard1", se, se, HFactory.createMutator(keyspace, se)); + template.update("key1", new ColumnFamilyUpdater() { + @Override + public void update() { + setString("column1", "value1"); + } + }); + + String value = template.queryColumns("key1", "", "", new ColumnFamilyRowMapper() { + @Override + public String mapRow(ColumnFamilyResult results) { + // TODO Auto-generated method stub + return results.getString("column1"); + } + }); + assertEquals("value1",value); + + + } + +} From c30d4fd40c585b48b58e67415044a4f2fe3f7f31 Mon Sep 17 00:00:00 2001 From: zznate Date: Wed, 16 Mar 2011 16:25:58 -0500 Subject: [PATCH 010/144] added multi-key queryColumns --- .../cassandra/service/template/ColumnFamilyTemplate.java | 3 +++ 1 file changed, 3 insertions(+) diff --git a/core/src/main/java/me/prettyprint/cassandra/service/template/ColumnFamilyTemplate.java b/core/src/main/java/me/prettyprint/cassandra/service/template/ColumnFamilyTemplate.java index 8b11f5a31..5ab07e1ac 100644 --- a/core/src/main/java/me/prettyprint/cassandra/service/template/ColumnFamilyTemplate.java +++ b/core/src/main/java/me/prettyprint/cassandra/service/template/ColumnFamilyTemplate.java @@ -143,6 +143,9 @@ public ColumnFamilyResultWrapper queryColumns(K key) { return doExecuteSlice(key, null); } + public ColumnFamilyResultWrapper queryColumns(Iterable keys) { + return doExecuteMultigetSlice(keys, null); + } @SuppressWarnings("unchecked") public T queryColumns(K key, ColumnFamilyRowMapper mapper) { From 1af6c0fbefae01849825ea8eb4474cd8cb4fcfd1 Mon Sep 17 00:00:00 2001 From: zznate Date: Wed, 16 Mar 2011 16:37:22 -0500 Subject: [PATCH 011/144] cleaner usage of slicepredicate --- .../service/template/AbstractColumnFamilyTemplate.java | 1 + .../cassandra/service/template/ColumnFamilyTemplate.java | 6 +++--- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/core/src/main/java/me/prettyprint/cassandra/service/template/AbstractColumnFamilyTemplate.java b/core/src/main/java/me/prettyprint/cassandra/service/template/AbstractColumnFamilyTemplate.java index 2b3a72aac..e79f79d7f 100644 --- a/core/src/main/java/me/prettyprint/cassandra/service/template/AbstractColumnFamilyTemplate.java +++ b/core/src/main/java/me/prettyprint/cassandra/service/template/AbstractColumnFamilyTemplate.java @@ -84,6 +84,7 @@ public AbstractColumnFamilyTemplate(Keyspace keyspace, String columnFamily, */ public AbstractColumnFamilyTemplate addColumn(N columnName, Serializer valueSerializer) { columnValueSerializers.put(columnName, valueSerializer); + activeSlicePredicate.addColumnName(columnName); return this; } diff --git a/core/src/main/java/me/prettyprint/cassandra/service/template/ColumnFamilyTemplate.java b/core/src/main/java/me/prettyprint/cassandra/service/template/ColumnFamilyTemplate.java index 5ab07e1ac..abed9a11b 100644 --- a/core/src/main/java/me/prettyprint/cassandra/service/template/ColumnFamilyTemplate.java +++ b/core/src/main/java/me/prettyprint/cassandra/service/template/ColumnFamilyTemplate.java @@ -140,11 +140,11 @@ public int countColumns(K key, N start, N end, int max) { } public ColumnFamilyResultWrapper queryColumns(K key) { - return doExecuteSlice(key, null); + return doExecuteSlice(key, activeSlicePredicate); } public ColumnFamilyResultWrapper queryColumns(Iterable keys) { - return doExecuteMultigetSlice(keys, null); + return doExecuteMultigetSlice(keys, activeSlicePredicate); } @SuppressWarnings("unchecked") @@ -248,7 +248,7 @@ public ColumnFamilyResultWrapper execute(Cassandra.Client cassandra) throw ByteBuffer sKey = keySerializer.toByteBuffer(key); cosc.put(sKey, cassandra.get_slice(sKey, columnParent, - (workingSlicePredicate == null ? activeSlicePredicate.setColumnNames(columnValueSerializers.keySet()).toThrift() : workingSlicePredicate.toThrift()), + workingSlicePredicate.toThrift(), ThriftConverter.consistencyLevel(consistencyLevelPolicy.get(operationType)))); } catch (Exception e) { From 7c6628ef8c36daaf533081f95d081cb658c93d00 Mon Sep 17 00:00:00 2001 From: Tim Koop Date: Thu, 17 Mar 2011 09:20:32 -0500 Subject: [PATCH 012/144] Updated to two-space tabs. --- .../cassandra/service/KeyIterator.java | 148 +++++++++--------- .../cassandra/service/StringKeyIterator.java | 7 +- 2 files changed, 77 insertions(+), 78 deletions(-) diff --git a/core/src/main/java/me/prettyprint/cassandra/service/KeyIterator.java b/core/src/main/java/me/prettyprint/cassandra/service/KeyIterator.java index 34fbb9e34..b69fc5b75 100644 --- a/core/src/main/java/me/prettyprint/cassandra/service/KeyIterator.java +++ b/core/src/main/java/me/prettyprint/cassandra/service/KeyIterator.java @@ -22,85 +22,85 @@ * @see StringKeyIterator */ public class KeyIterator implements Iterable { - private static StringSerializer stringSerializer = new StringSerializer(); - - private int maxRowCount = 500; - private int maxColumnCount = 2; // we only need this to tell if there are any columns in the row (to test for tombstones) - - private Iterator> rowsIterator = null; - - private RangeSlicesQuery query = null; - - private K nextValue = null; - private K lastReadValue = null; - - private Iterator keyIterator = new Iterator() { - @Override - public boolean hasNext() { - return nextValue != null; - } - - @Override - public K next() { - K next = nextValue; - findNext(false); - return next; - } - - @Override - public void remove() { - throw new UnsupportedOperationException(); - } - }; - - private void findNext(boolean fromRunQuery) { - nextValue = null; - if (rowsIterator == null) { - return; - } - while (rowsIterator.hasNext() && nextValue == null) { - Row row = rowsIterator.next(); - lastReadValue = row.getKey(); - if (!row.getColumnSlice().getColumns().isEmpty()) { - nextValue = lastReadValue; - } - } - if (!rowsIterator.hasNext() && nextValue == null) { - runQuery(lastReadValue); - } - } + private static StringSerializer stringSerializer = new StringSerializer(); + + private int maxRowCount = 500; + private int maxColumnCount = 2; // we only need this to tell if there are any columns in the row (to test for tombstones) + + private Iterator> rowsIterator = null; - public KeyIterator(Keyspace keyspace, String columnFamily, AbstractSerializer serializer) { - query = HFactory - .createRangeSlicesQuery(keyspace, serializer, stringSerializer, stringSerializer) - .setColumnFamily(columnFamily) - .setRange(null, null, false, maxColumnCount) - .setRowCount(maxRowCount); + private RangeSlicesQuery query = null; - runQuery(null); + private K nextValue = null; + private K lastReadValue = null; + + private Iterator keyIterator = new Iterator() { + @Override + public boolean hasNext() { + return nextValue != null; } - - private void runQuery(K start) { - query.setKeys(start, null); - - rowsIterator = null; - QueryResult> result = query.execute(); - OrderedRows rows = (result != null) ? result.get() : null; - rowsIterator = (rows != null) ? rows.iterator() : null; - - // we'll skip this first one, since it is the same as the last one from previous time we executed - if (start != null && rowsIterator != null) rowsIterator.next(); - - if (!rowsIterator.hasNext()) { - nextValue = null; // all done. our iterator's hasNext() will now return false; - } else { - findNext(true); - } + + @Override + public K next() { + K next = nextValue; + findNext(false); + return next; } - + @Override - public Iterator iterator() { - return keyIterator; + public void remove() { + throw new UnsupportedOperationException(); + } + }; + + private void findNext(boolean fromRunQuery) { + nextValue = null; + if (rowsIterator == null) { + return; } + while (rowsIterator.hasNext() && nextValue == null) { + Row row = rowsIterator.next(); + lastReadValue = row.getKey(); + if (!row.getColumnSlice().getColumns().isEmpty()) { + nextValue = lastReadValue; + } + } + if (!rowsIterator.hasNext() && nextValue == null) { + runQuery(lastReadValue); + } + } + + public KeyIterator(Keyspace keyspace, String columnFamily, AbstractSerializer serializer) { + query = HFactory + .createRangeSlicesQuery(keyspace, serializer, stringSerializer, stringSerializer) + .setColumnFamily(columnFamily) + .setRange(null, null, false, maxColumnCount) + .setRowCount(maxRowCount); + + runQuery(null); + } + + private void runQuery(K start) { + query.setKeys(start, null); + + rowsIterator = null; + QueryResult> result = query.execute(); + OrderedRows rows = (result != null) ? result.get() : null; + rowsIterator = (rows != null) ? rows.iterator() : null; + + // we'll skip this first one, since it is the same as the last one from previous time we executed + if (start != null && rowsIterator != null) rowsIterator.next(); + + if (!rowsIterator.hasNext()) { + nextValue = null; // all done. our iterator's hasNext() will now return false; + } else { + findNext(true); + } + } + + @Override + public Iterator iterator() { + return keyIterator; + } } diff --git a/core/src/main/java/me/prettyprint/cassandra/service/StringKeyIterator.java b/core/src/main/java/me/prettyprint/cassandra/service/StringKeyIterator.java index 9675d10ec..1cddbd872 100644 --- a/core/src/main/java/me/prettyprint/cassandra/service/StringKeyIterator.java +++ b/core/src/main/java/me/prettyprint/cassandra/service/StringKeyIterator.java @@ -13,9 +13,8 @@ */ public class StringKeyIterator extends KeyIterator { - public StringKeyIterator(Keyspace keyspace, String columnFamily) { - super(keyspace, columnFamily, new StringSerializer()); - - } + public StringKeyIterator(Keyspace keyspace, String columnFamily) { + super(keyspace, columnFamily, new StringSerializer()); + } } From 7e30a592e89af24cd1838beef29aa09533df8bf4 Mon Sep 17 00:00:00 2001 From: zznate Date: Thu, 17 Mar 2011 11:15:43 -0500 Subject: [PATCH 013/144] fix issue with BB serializer. Addresses GH issue 185 --- .../cassandra/serializers/ByteBufferSerializer.java | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/core/src/main/java/me/prettyprint/cassandra/serializers/ByteBufferSerializer.java b/core/src/main/java/me/prettyprint/cassandra/serializers/ByteBufferSerializer.java index b138a078a..1d75618b6 100644 --- a/core/src/main/java/me/prettyprint/cassandra/serializers/ByteBufferSerializer.java +++ b/core/src/main/java/me/prettyprint/cassandra/serializers/ByteBufferSerializer.java @@ -9,10 +9,12 @@ /** * The BytesExtractor is a simple identity function. It supports the Extractor * interface and implements the fromBytes and toBytes as simple identity - * functions. + * functions. However, the from and to methods both return the results of + * {@link ByteBuffer#duplicate()} * - * @author Ran Tavory * + * @author Ran Tavory + * @author zznate */ public final class ByteBufferSerializer extends AbstractSerializer{ @@ -27,9 +29,7 @@ public ByteBuffer fromByteBuffer(ByteBuffer bytes) { if(bytes==null) { return null; } - ByteBuffer b = bytes.slice(); - bytes.position(bytes.position() + b.remaining()); - return b; + return bytes.duplicate(); } @Override @@ -37,7 +37,7 @@ public ByteBuffer toByteBuffer(ByteBuffer obj) { if(obj==null) { return null; } - return obj.slice(); + return obj.duplicate(); } @Override From db854c0b966012aa309b56bc11877d82a17334af Mon Sep 17 00:00:00 2001 From: Rustam Aliyev Date: Sat, 12 Mar 2011 03:01:07 +0000 Subject: [PATCH 014/144] Added setKeys/setColumnNames overloading with Collection type arguments in MultigetSuperSliceQuery/MultigetSubSliceQuery --- .../model/thrift/ThriftMultigetSubSliceQuery.java | 13 +++++++++++++ .../model/thrift/ThriftMultigetSuperSliceQuery.java | 12 ++++++++++++ .../hector/api/query/MultigetSubSliceQuery.java | 4 ++++ .../hector/api/query/MultigetSuperSliceQuery.java | 4 ++++ 4 files changed, 33 insertions(+) diff --git a/core/src/main/java/me/prettyprint/cassandra/model/thrift/ThriftMultigetSubSliceQuery.java b/core/src/main/java/me/prettyprint/cassandra/model/thrift/ThriftMultigetSubSliceQuery.java index 3b06575f3..6032280e2 100644 --- a/core/src/main/java/me/prettyprint/cassandra/model/thrift/ThriftMultigetSubSliceQuery.java +++ b/core/src/main/java/me/prettyprint/cassandra/model/thrift/ThriftMultigetSubSliceQuery.java @@ -45,6 +45,12 @@ public MultigetSubSliceQuery setKeys(K... keys) { return this; } + @Override + public MultigetSubSliceQuery setKeys(Collection keys) { + this.keys = keys; + return this; + } + /** * Set the supercolumn to run the slice query on */ @@ -100,4 +106,11 @@ public MultigetSubSliceQuery setRange(N start, N finish, boolean re public MultigetSubSliceQuery setColumnNames(N... columnNames) { return (MultigetSubSliceQuery) super.setColumnNames(columnNames); } + + @SuppressWarnings("unchecked") + @Override + public MultigetSubSliceQuery setColumnNames(Collection columnNames) { + return (MultigetSubSliceQuery) super.setColumnNames(columnNames); + } + } diff --git a/core/src/main/java/me/prettyprint/cassandra/model/thrift/ThriftMultigetSuperSliceQuery.java b/core/src/main/java/me/prettyprint/cassandra/model/thrift/ThriftMultigetSuperSliceQuery.java index ff35e57f0..adbc7dbf6 100644 --- a/core/src/main/java/me/prettyprint/cassandra/model/thrift/ThriftMultigetSuperSliceQuery.java +++ b/core/src/main/java/me/prettyprint/cassandra/model/thrift/ThriftMultigetSuperSliceQuery.java @@ -49,6 +49,12 @@ public MultigetSuperSliceQuery setKeys(K... keys) { return this; } + @Override + public MultigetSuperSliceQuery setKeys(Collection keys) { + this.keys = keys; + return this; + } + @Override public QueryResult> execute() { return new QueryResultImpl>( @@ -83,6 +89,12 @@ public MultigetSuperSliceQuery setColumnNames(SN... columnNames) { return (MultigetSuperSliceQuery) super.setColumnNames(columnNames); } + @SuppressWarnings("unchecked") + @Override + public MultigetSuperSliceQuery setColumnNames(Collection columnNames) { + return (MultigetSuperSliceQuery) super.setColumnNames(columnNames); + } + @SuppressWarnings("unchecked") @Override public MultigetSuperSliceQuery setColumnFamily(String cf) { diff --git a/core/src/main/java/me/prettyprint/hector/api/query/MultigetSubSliceQuery.java b/core/src/main/java/me/prettyprint/hector/api/query/MultigetSubSliceQuery.java index 3f2795b3f..227761bd1 100644 --- a/core/src/main/java/me/prettyprint/hector/api/query/MultigetSubSliceQuery.java +++ b/core/src/main/java/me/prettyprint/hector/api/query/MultigetSubSliceQuery.java @@ -13,6 +13,8 @@ public interface MultigetSubSliceQuery extends Query> MultigetSubSliceQuery setKeys(K... keys); + MultigetSubSliceQuery setKeys(Collection keys); + /** * Set the supercolumn to run the slice query on */ @@ -30,4 +32,6 @@ public interface MultigetSubSliceQuery extends Query> */ MultigetSubSliceQuery setColumnNames(N... columnNames); + MultigetSubSliceQuery setColumnNames(Collection columnNames); + } \ No newline at end of file diff --git a/core/src/main/java/me/prettyprint/hector/api/query/MultigetSuperSliceQuery.java b/core/src/main/java/me/prettyprint/hector/api/query/MultigetSuperSliceQuery.java index e958a9542..f1c201599 100644 --- a/core/src/main/java/me/prettyprint/hector/api/query/MultigetSuperSliceQuery.java +++ b/core/src/main/java/me/prettyprint/hector/api/query/MultigetSuperSliceQuery.java @@ -13,6 +13,8 @@ public interface MultigetSuperSliceQuery extends Query setKeys(K... keys); + MultigetSuperSliceQuery setKeys(Collection keys); + MultigetSuperSliceQuery setColumnFamily(String cf); MultigetSuperSliceQuery setRange(SN start, SN finish, boolean reversed, int count); @@ -25,4 +27,6 @@ public interface MultigetSuperSliceQuery extends Query setColumnNames(SN... columnNames); + MultigetSuperSliceQuery setColumnNames(Collection columnNames); + } \ No newline at end of file From dba432962a73de52774af9b234d80d62b6703e16 Mon Sep 17 00:00:00 2001 From: zznate Date: Thu, 17 Mar 2011 11:38:51 -0500 Subject: [PATCH 015/144] made trace default level for timing logger. Thanks to Chris Heron for the PR --- core/src/main/resources/hectorLog4j.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/src/main/resources/hectorLog4j.xml b/core/src/main/resources/hectorLog4j.xml index b7f66e553..a33e9b0cc 100644 --- a/core/src/main/resources/hectorLog4j.xml +++ b/core/src/main/resources/hectorLog4j.xml @@ -46,7 +46,7 @@ upstream loggers. --> - + From 8751ec479084eca0622c2f26f6c0b02577ad2a15 Mon Sep 17 00:00:00 2001 From: zznate Date: Thu, 17 Mar 2011 13:12:59 -0500 Subject: [PATCH 016/144] v0.8 specific changes for bringing up to date with latest cassandra trunk --- core/pom.xml | 9 +- .../thrift/AbstractThriftClientWrapper.java | 29 ++-- .../testutils/EmbeddedSchemaLoader.java | 126 ++++++++++++++++++ .../testutils/EmbeddedServerHelper.java | 32 +---- .../cassandra/service/CassandraAuthTest.java | 2 +- .../service/CassandraClusterTest.java | 2 +- .../hector/api/ApiV2SystemTest.java | 2 +- core/src/test/resources/cassandra-auth.yaml | 41 +----- core/src/test/resources/cassandra.yaml | 34 +---- 9 files changed, 157 insertions(+), 120 deletions(-) create mode 100644 core/src/main/java/me/prettyprint/cassandra/testutils/EmbeddedSchemaLoader.java diff --git a/core/pom.xml b/core/pom.xml index 107b47da9..ddd741545 100644 --- a/core/pom.xml +++ b/core/pom.xml @@ -104,8 +104,13 @@
org.apache.cassandra - cassandra-all - 0.8.0-20110216 + apache-cassandra + 0.8.0-20110317 + + + org.apache.cassandra + apache-cassandra-thrift + 0.8.0-20110317 org.apache.cassandra.deps diff --git a/core/src/main/java/me/prettyprint/cassandra/model/thrift/AbstractThriftClientWrapper.java b/core/src/main/java/me/prettyprint/cassandra/model/thrift/AbstractThriftClientWrapper.java index 9b2dd898d..dae7b10a4 100644 --- a/core/src/main/java/me/prettyprint/cassandra/model/thrift/AbstractThriftClientWrapper.java +++ b/core/src/main/java/me/prettyprint/cassandra/model/thrift/AbstractThriftClientWrapper.java @@ -21,6 +21,7 @@ import org.apache.cassandra.thrift.KsDef; import org.apache.cassandra.thrift.Mutation; import org.apache.cassandra.thrift.NotFoundException; +import org.apache.cassandra.thrift.SchemaDisagreementException; import org.apache.cassandra.thrift.SlicePredicate; import org.apache.cassandra.thrift.TimedOutException; import org.apache.cassandra.thrift.TokenRange; @@ -93,7 +94,7 @@ public String describe_snitch() throws TException { @Override public List describe_splits(String cfName, String start_token, - String end_token, int keys_per_split) throws TException { + String end_token, int keys_per_split) throws TException, InvalidRequestException { return client.describe_splits(cfName, start_token, end_token, keys_per_split); } @@ -232,7 +233,7 @@ public String recv_describe_snitch() throws TException { } @Override - public List recv_describe_splits() throws TException { + public List recv_describe_splits() throws TException, InvalidRequestException { return client.recv_describe_splits(); } @@ -312,37 +313,37 @@ public void recv_set_keyspace() throws InvalidRequestException, TException { @Override public String recv_system_add_column_family() throws InvalidRequestException, - TException { + TException, SchemaDisagreementException { return client.recv_system_add_column_family(); } @Override public String recv_system_add_keyspace() throws InvalidRequestException, - TException { + TException, SchemaDisagreementException { return client.recv_system_add_keyspace(); } @Override public String recv_system_drop_column_family() - throws InvalidRequestException, TException { + throws InvalidRequestException, TException, SchemaDisagreementException { return client.recv_system_drop_column_family(); } @Override public String recv_system_drop_keyspace() throws InvalidRequestException, - TException { + TException, SchemaDisagreementException { return client.recv_system_drop_keyspace(); } @Override public String recv_system_update_column_family() - throws InvalidRequestException, TException { + throws InvalidRequestException, TException, SchemaDisagreementException { return client.recv_system_update_column_family(); } @Override public String recv_system_update_keyspace() throws InvalidRequestException, - TException { + TException, SchemaDisagreementException { return client.recv_system_update_keyspace(); } @@ -530,37 +531,37 @@ public void set_keyspace(String keyspace) throws InvalidRequestException, @Override public String system_add_column_family(CfDef cf_def) - throws InvalidRequestException, TException { + throws InvalidRequestException, TException, SchemaDisagreementException { return client.system_add_column_family(cf_def); } @Override public String system_add_keyspace(KsDef ks_def) - throws InvalidRequestException, TException { + throws InvalidRequestException, TException, SchemaDisagreementException { return client.system_add_keyspace(ks_def); } @Override public String system_drop_column_family(String column_family) - throws InvalidRequestException, TException { + throws InvalidRequestException, TException, SchemaDisagreementException { return client.system_drop_column_family(column_family); } @Override public String system_drop_keyspace(String keyspace) - throws InvalidRequestException, TException { + throws InvalidRequestException, TException, SchemaDisagreementException { return client.system_drop_keyspace(keyspace); } @Override public String system_update_column_family(CfDef cf_def) - throws InvalidRequestException, TException { + throws InvalidRequestException, TException, SchemaDisagreementException { return client.system_update_column_family(cf_def); } @Override public String system_update_keyspace(KsDef ks_def) - throws InvalidRequestException, TException { + throws InvalidRequestException, TException, SchemaDisagreementException { return client.system_update_keyspace(ks_def); } diff --git a/core/src/main/java/me/prettyprint/cassandra/testutils/EmbeddedSchemaLoader.java b/core/src/main/java/me/prettyprint/cassandra/testutils/EmbeddedSchemaLoader.java new file mode 100644 index 000000000..25016acb7 --- /dev/null +++ b/core/src/main/java/me/prettyprint/cassandra/testutils/EmbeddedSchemaLoader.java @@ -0,0 +1,126 @@ +package me.prettyprint.cassandra.testutils; + +import java.nio.ByteBuffer; +import java.util.ArrayList; +import java.util.Collection; +import java.util.Collections; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import org.apache.cassandra.config.CFMetaData; +import org.apache.cassandra.config.ColumnDefinition; +import org.apache.cassandra.config.ConfigurationException; +import org.apache.cassandra.config.DatabaseDescriptor; +import org.apache.cassandra.config.KSMetaData; +import org.apache.cassandra.db.ColumnFamilyType; +import org.apache.cassandra.db.marshal.AbstractType; +import org.apache.cassandra.db.marshal.AsciiType; +import org.apache.cassandra.db.marshal.BytesType; +import org.apache.cassandra.db.marshal.CounterColumnType; +import org.apache.cassandra.db.marshal.IntegerType; +import org.apache.cassandra.db.marshal.LongType; +import org.apache.cassandra.db.marshal.TimeUUIDType; +import org.apache.cassandra.db.marshal.UTF8Type; +import org.apache.cassandra.locator.AbstractReplicationStrategy; +import org.apache.cassandra.locator.SimpleStrategy; +import org.apache.cassandra.thrift.IndexType; + +import com.google.common.base.Charsets; + +public class EmbeddedSchemaLoader { + public static void loadSchema() { + try { + for (KSMetaData ksm : schemaDefinition()) { + for (CFMetaData cfm : ksm.cfMetaData().values()) + CFMetaData.map(cfm); + DatabaseDescriptor.setTableDefinition(ksm, DatabaseDescriptor + .getDefsVersion()); + } + } catch (ConfigurationException e) { + throw new RuntimeException(e); + } + } + + public static Collection schemaDefinition() { + List schema = new ArrayList(); + + // A whole bucket of shorthand + String ks1 = "Keyspace1"; + String ks2 = "Keyspace2"; + + Class simple = SimpleStrategy.class; + Map no_opts = Collections. emptyMap(); + int rep_factor1 = 1; + int rep_factor2 = 2; + int rep_factor3 = 3; + int rep_factor5 = 5; + + ColumnFamilyType st = ColumnFamilyType.Standard; + ColumnFamilyType su = ColumnFamilyType.Super; + AbstractType bytes = BytesType.instance; + + // Keyspace 1 + schema.add(new KSMetaData( + ks1, + simple, + no_opts, + rep_factor1, + + // Column Families + standardCFMD(ks1, "Standard1"), standardCFMD(ks1, "Standard2"), + standardCFMD(ks1, "Standard3"), standardCFMD(ks1, "Standard4"), + standardCFMD(ks1, "StandardLong1"), standardCFMD(ks1, "StandardLong2"), + superCFMD(ks1, "Super1", BytesType.instance), superCFMD(ks1, "Super2", + LongType.instance), superCFMD(ks1, "Super3", LongType.instance), + superCFMD(ks1, "Super4", UTF8Type.instance), superCFMD(ks1, "Super5", + bytes), indexCFMD(ks1, "Indexed1", true), indexCFMD(ks1, + "Indexed2", false), new CFMetaData(ks1, "StandardInteger1", st, + IntegerType.instance, null).keyCacheSize(0), new CFMetaData(ks1, + "Counter1", st, bytes, null) + .defaultValidator(CounterColumnType.instance), new CFMetaData(ks1, + "SuperCounter1", su, bytes, bytes) + .defaultValidator(CounterColumnType.instance), jdbcCFMD(ks1, + "JdbcInteger", IntegerType.instance), jdbcCFMD(ks1, "JdbcUtf8", + UTF8Type.instance), jdbcCFMD(ks1, "JdbcLong", LongType.instance), + jdbcCFMD(ks1, "JdbcBytes", bytes), jdbcCFMD(ks1, "JdbcAscii", + AsciiType.instance))); + + // Keyspace 2 + + return schema; + } + + private static CFMetaData standardCFMD(String ksName, String cfName) { + return new CFMetaData(ksName, cfName, ColumnFamilyType.Standard, + BytesType.instance, null).keyCacheSize(0); + } + + private static CFMetaData superCFMD(String ksName, String cfName, + AbstractType subcc) { + return new CFMetaData(ksName, cfName, ColumnFamilyType.Super, + BytesType.instance, subcc).keyCacheSize(0); + } + + private static CFMetaData indexCFMD(String ksName, String cfName, + final Boolean withIdxType) { + return standardCFMD(ksName, cfName).columnMetadata( + Collections + .unmodifiableMap(new HashMap() { + { + ByteBuffer cName = ByteBuffer.wrap("birthyear" + .getBytes(Charsets.UTF_8)); + IndexType keys = withIdxType ? IndexType.KEYS : null; + put(cName, new ColumnDefinition(cName, LongType.instance, keys, + null)); + } + })); + } + + private static CFMetaData jdbcCFMD(String ksName, String cfName, + AbstractType comp) { + return new CFMetaData(ksName, cfName, ColumnFamilyType.Standard, comp, null) + .defaultValidator(comp); + } +} + diff --git a/core/src/main/java/me/prettyprint/cassandra/testutils/EmbeddedServerHelper.java b/core/src/main/java/me/prettyprint/cassandra/testutils/EmbeddedServerHelper.java index 05fb1b973..c38acfc1e 100644 --- a/core/src/main/java/me/prettyprint/cassandra/testutils/EmbeddedServerHelper.java +++ b/core/src/main/java/me/prettyprint/cassandra/testutils/EmbeddedServerHelper.java @@ -82,20 +82,7 @@ public void setup() throws TTransportException, IOException, } } - /** - * Manually load tables from the test configuration file. - * - * @throws ConfigurationException - */ - private void loadYamlTables() throws ConfigurationException { - for (KSMetaData table : DatabaseDescriptor.readTablesFromYaml()) { - for (CFMetaData cfm : table.cfMetaData().values()) { - CFMetaData.map(cfm); - } - DatabaseDescriptor.setTableDefinition(table, - DatabaseDescriptor.getDefsVersion()); - } - } + public static void teardown() { //if ( cassandraDaemon != null ) @@ -189,21 +176,10 @@ public static void mkdirs() } } - public static void loadSchemaFromYaml() + public static void loadSchemaFromYaml() { - try - { - for (KSMetaData ksm : DatabaseDescriptor.readTablesFromYaml()) - { - for (CFMetaData cfm : ksm.cfMetaData().values()) - CFMetaData.map(cfm); - DatabaseDescriptor.setTableDefinition(ksm, DatabaseDescriptor.getDefsVersion()); - } - } - catch (ConfigurationException e) - { - throw new RuntimeException(e); - } + EmbeddedSchemaLoader.loadSchema(); + } diff --git a/core/src/test/java/me/prettyprint/cassandra/service/CassandraAuthTest.java b/core/src/test/java/me/prettyprint/cassandra/service/CassandraAuthTest.java index 026dd36d0..79f583503 100644 --- a/core/src/test/java/me/prettyprint/cassandra/service/CassandraAuthTest.java +++ b/core/src/test/java/me/prettyprint/cassandra/service/CassandraAuthTest.java @@ -113,7 +113,7 @@ public void testDescribeKeyspace() throws Exception { ThriftCluster cassandraCluster = new ThriftCluster("Test Cluster", cassandraHostConfigurator, user1Credentials); KeyspaceDefinition keyspaceDetail = cassandraCluster.describeKeyspace("Keyspace1"); assertNotNull(keyspaceDetail); - assertEquals(7, keyspaceDetail.getCfDefs().size()); + assertEquals(21, keyspaceDetail.getCfDefs().size()); } @Test diff --git a/core/src/test/java/me/prettyprint/cassandra/service/CassandraClusterTest.java b/core/src/test/java/me/prettyprint/cassandra/service/CassandraClusterTest.java index 7e0bda8da..e3d95b80c 100644 --- a/core/src/test/java/me/prettyprint/cassandra/service/CassandraClusterTest.java +++ b/core/src/test/java/me/prettyprint/cassandra/service/CassandraClusterTest.java @@ -75,7 +75,7 @@ public void testDescribeRing() throws Exception { public void testDescribeKeyspace() throws Exception { KeyspaceDefinition keyspaceDetail = cassandraCluster.describeKeyspace("Keyspace1"); assertNotNull(keyspaceDetail); - assertEquals(4, keyspaceDetail.getCfDefs().size()); + assertEquals(21, keyspaceDetail.getCfDefs().size()); } @Test diff --git a/core/src/test/java/me/prettyprint/hector/api/ApiV2SystemTest.java b/core/src/test/java/me/prettyprint/hector/api/ApiV2SystemTest.java index 432e5a403..d35fea5df 100644 --- a/core/src/test/java/me/prettyprint/hector/api/ApiV2SystemTest.java +++ b/core/src/test/java/me/prettyprint/hector/api/ApiV2SystemTest.java @@ -77,7 +77,7 @@ public class ApiV2SystemTest extends BaseEmbededServerSetupTest { @Before public void setupCase() { - cluster = getOrCreateCluster("MyCluster", "127.0.0.1:9170"); + cluster = getOrCreateCluster("Test Cluster", "127.0.0.1:9170"); ko = createKeyspace(KEYSPACE, cluster); } diff --git a/core/src/test/resources/cassandra-auth.yaml b/core/src/test/resources/cassandra-auth.yaml index 3457db548..19fb059aa 100644 --- a/core/src/test/resources/cassandra-auth.yaml +++ b/core/src/test/resources/cassandra-auth.yaml @@ -373,43 +373,4 @@ index_interval: 128 # NOTE: this keyspace definition is for demonstration purposes only. # Cassandra will not load these definitions during startup. See # http://wiki.apache.org/cassandra/FAQ#no_keyspaces for an explanation. -keyspaces: - - name: Keyspace1 - replica_placement_strategy: org.apache.cassandra.locator.RackUnawareStrategy - replication_factor: 1 - column_families: - - name: Standard1 - compare_with: BytesType - - - name: Standard2 - compare_with: UTF8Type - read_repair_chance: 0.1 - keys_cached: 100 - gc_grace_seconds: 0 - - - name: StandardByUUID1 - compare_with: TimeUUIDType - - - - name: Super1 - column_type: Super - compare_with: BytesType - compare_subcolumns_with: BytesType - - - name: Super2 - column_type: Super - compare_subcolumns_with: UTF8Type - rows_cached: 10000 - keys_cached: 50 - comment: 'A column family with supercolumns, whose column and subcolumn names are UTF8 strings' - - - name: Super3 - column_type: Super - compare_with: LongType - comment: 'A column family with supercolumns, whose column names are Longs (8 bytes)' - - - name: Indexed1 - column_metadata: - - name: birthyear - validator_class: LongType - index_type: KEYS + diff --git a/core/src/test/resources/cassandra.yaml b/core/src/test/resources/cassandra.yaml index 279381f1a..1b14a1072 100644 --- a/core/src/test/resources/cassandra.yaml +++ b/core/src/test/resources/cassandra.yaml @@ -371,36 +371,4 @@ index_interval: 128 # NOTE: this keyspace definition is for demonstration purposes only. # Cassandra will not load these definitions during startup. See # http://wiki.apache.org/cassandra/FAQ#no_keyspaces for an explanation. -keyspaces: - - name: Keyspace1 - replica_placement_strategy: org.apache.cassandra.locator.RackUnawareStrategy - replication_factor: 1 - column_families: - - name: Standard1 - compare_with: BytesType - memtable_throughput_in_mb: 24 - key_cache_save_period_in_seconds: 0 - keys_cached: 0 - - - name: StandardByUUID1 - compare_with: TimeUUIDType - memtable_throughput_in_mb: 24 - key_cache_save_period_in_seconds: 0 - keys_cached: 0 - - - name: Super1 - column_type: Super - compare_with: BytesType - compare_subcolumns_with: BytesType - memtable_throughput_in_mb: 24 - key_cache_save_period_in_seconds: 0 - keys_cached: 0 - - - name: Indexed1 - memtable_throughput_in_mb: 24 - key_cache_save_period_in_seconds: 0 - keys_cached: 0 - column_metadata: - - name: birthyear - validator_class: LongType - index_type: KEYS + From 3b89e58de5fd844e77df6b537bf4b0158e339cf5 Mon Sep 17 00:00:00 2001 From: zznate Date: Thu, 17 Mar 2011 13:14:19 -0500 Subject: [PATCH 017/144] added CLs two and three --- .../prettyprint/cassandra/model/thrift/ThriftConverter.java | 4 ++++ .../java/me/prettyprint/hector/api/HConsistencyLevel.java | 3 ++- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/core/src/main/java/me/prettyprint/cassandra/model/thrift/ThriftConverter.java b/core/src/main/java/me/prettyprint/cassandra/model/thrift/ThriftConverter.java index 87f5d9e92..42f428d22 100644 --- a/core/src/main/java/me/prettyprint/cassandra/model/thrift/ThriftConverter.java +++ b/core/src/main/java/me/prettyprint/cassandra/model/thrift/ThriftConverter.java @@ -23,6 +23,10 @@ public static ConsistencyLevel consistencyLevel(HConsistencyLevel c) { return ConsistencyLevel.ANY; case ONE: return ConsistencyLevel.ONE; + case TWO: + return ConsistencyLevel.TWO; + case THREE: + return ConsistencyLevel.THREE; case QUORUM: return ConsistencyLevel.QUORUM; case EACH_QUORUM: diff --git a/core/src/main/java/me/prettyprint/hector/api/HConsistencyLevel.java b/core/src/main/java/me/prettyprint/hector/api/HConsistencyLevel.java index c475bfe20..af8ba4c51 100644 --- a/core/src/main/java/me/prettyprint/hector/api/HConsistencyLevel.java +++ b/core/src/main/java/me/prettyprint/hector/api/HConsistencyLevel.java @@ -4,7 +4,8 @@ * This is the Hector consistency level which is just a mirror of the thrift or avro consistency * levels. * @author: peter + * @author zznate */ public enum HConsistencyLevel { - ONE, QUORUM, ALL, ANY, EACH_QUORUM, LOCAL_QUORUM; + ONE, TWO, THREE, QUORUM, ALL, ANY, EACH_QUORUM, LOCAL_QUORUM; } From 04e86a5b9ddf87f1113eed009a592c910ce3edc0 Mon Sep 17 00:00:00 2001 From: zznate Date: Thu, 17 Mar 2011 17:06:46 -0500 Subject: [PATCH 018/144] abstract template more generic --- .../AbstractColumnFamilyTemplate.java | 9 ++-- .../template/ColumnFamilyTemplate.java | 5 +- .../template/ColumnFamilyTemplateTest.java | 54 +++---------------- 3 files changed, 15 insertions(+), 53 deletions(-) diff --git a/core/src/main/java/me/prettyprint/cassandra/service/template/AbstractColumnFamilyTemplate.java b/core/src/main/java/me/prettyprint/cassandra/service/template/AbstractColumnFamilyTemplate.java index e79f79d7f..144f86ae2 100644 --- a/core/src/main/java/me/prettyprint/cassandra/service/template/AbstractColumnFamilyTemplate.java +++ b/core/src/main/java/me/prettyprint/cassandra/service/template/AbstractColumnFamilyTemplate.java @@ -3,9 +3,6 @@ import java.util.HashMap; import java.util.Map; -import org.apache.cassandra.thrift.ColumnParent; - -import me.prettyprint.cassandra.model.ExecutingKeyspace; import me.prettyprint.cassandra.model.HSlicePredicate; import me.prettyprint.cassandra.service.ExceptionsTranslator; import me.prettyprint.cassandra.service.ExceptionsTranslatorImpl; @@ -15,13 +12,15 @@ import me.prettyprint.hector.api.mutation.MutationResult; import me.prettyprint.hector.api.mutation.Mutator; +import org.apache.cassandra.thrift.ColumnParent; + public class AbstractColumnFamilyTemplate { // Used for queries where we just ask for all columns public static final int ALL_COLUMNS_COUNT = Integer.MAX_VALUE; public static final Object ALL_COLUMNS_START = null; public static final Object ALL_COLUMNS_END = null; - protected ExecutingKeyspace keyspace; + protected Keyspace keyspace; protected String columnFamily; protected Serializer keySerializer; protected Map> columnValueSerializers; @@ -64,7 +63,7 @@ public AbstractColumnFamilyTemplate(Keyspace keyspace, String columnFamily, Serializer keySerializer, Serializer topSerializer, Mutator mutator) { // ugly, but safe - this.keyspace = (ExecutingKeyspace)keyspace; + this.keyspace = keyspace; this.columnFamily = columnFamily; this.keySerializer = keySerializer; this.topSerializer = topSerializer; diff --git a/core/src/main/java/me/prettyprint/cassandra/service/template/ColumnFamilyTemplate.java b/core/src/main/java/me/prettyprint/cassandra/service/template/ColumnFamilyTemplate.java index abed9a11b..5e56962f3 100644 --- a/core/src/main/java/me/prettyprint/cassandra/service/template/ColumnFamilyTemplate.java +++ b/core/src/main/java/me/prettyprint/cassandra/service/template/ColumnFamilyTemplate.java @@ -6,6 +6,7 @@ import java.util.List; import java.util.Map; +import me.prettyprint.cassandra.model.ExecutingKeyspace; import me.prettyprint.cassandra.model.HSlicePredicate; import me.prettyprint.cassandra.model.thrift.ThriftConverter; import me.prettyprint.cassandra.serializers.SerializerTypeInferer; @@ -240,7 +241,7 @@ public HColumn querySingleColumn(K key, N columnName, private ColumnFamilyResultWrapper doExecuteSlice(final K key, final HSlicePredicate workingSlicePredicate) { - return keyspace.doExecuteOperation(new Operation>(OperationType.READ) { + return ((ExecutingKeyspace)keyspace).doExecuteOperation(new Operation>(OperationType.READ) { @Override public ColumnFamilyResultWrapper execute(Cassandra.Client cassandra) throws HectorException { Map> cosc = new LinkedHashMap>(); @@ -262,7 +263,7 @@ public ColumnFamilyResultWrapper execute(Cassandra.Client cassandra) throw private ColumnFamilyResultWrapper doExecuteMultigetSlice(final Iterable keys, final HSlicePredicate workingSlicePredicate) { - return keyspace.doExecuteOperation(new Operation>(OperationType.READ) { + return ((ExecutingKeyspace)keyspace).doExecuteOperation(new Operation>(OperationType.READ) { @Override public ColumnFamilyResultWrapper execute(Cassandra.Client cassandra) throws HectorException { Map> cosc; diff --git a/core/src/test/java/me/prettyprint/cassandra/service/template/ColumnFamilyTemplateTest.java b/core/src/test/java/me/prettyprint/cassandra/service/template/ColumnFamilyTemplateTest.java index eed57504f..5bc0c9e06 100644 --- a/core/src/test/java/me/prettyprint/cassandra/service/template/ColumnFamilyTemplateTest.java +++ b/core/src/test/java/me/prettyprint/cassandra/service/template/ColumnFamilyTemplateTest.java @@ -1,62 +1,24 @@ package me.prettyprint.cassandra.service.template; -import static me.prettyprint.hector.api.factory.HFactory.createKeyspace; -import static me.prettyprint.hector.api.factory.HFactory.getOrCreateCluster; -import static org.junit.Assert.*; +import static org.junit.Assert.assertEquals; +import me.prettyprint.hector.api.factory.HFactory; -import org.junit.Before; import org.junit.Test; -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.ResultStatus; -import me.prettyprint.hector.api.factory.HFactory; - -public class ColumnFamilyTemplateTest extends BaseEmbededServerSetupTest { - - private Keyspace keyspace; - private static final StringSerializer se = StringSerializer.get(); - - @Before - public void setupLocal() { - Cluster cluster = getOrCreateCluster("MyCluster", "127.0.0.1:9170"); - keyspace = createKeyspace("Keyspace1", cluster); - } +public class ColumnFamilyTemplateTest extends BaseColumnFamilyTemplateTest { @Test public void testCreateSelect() { ColumnFamilyTemplate template = new ColumnFamilyTemplate(keyspace, "Standard1", se, se, HFactory.createMutator(keyspace, se)); - - - ColumnFamilyUpdater updater = template.createUpdater("key1"); // createStaticUpater() + + ColumnFamilyUpdater updater = template.createUpdater("key1"); updater.setString("column1","value1"); template.update(updater); - /* - template.update("key1", new ColumnFamilyUpdater() { - @Override - public void update() { - setString("column1", "value1"); - } - }); - */ - template.addColumn("column1", se); - ColumnFamilyResultWrapper wrapper = template.queryColumns("key1"); + template.addColumn("column1", se); + ColumnFamilyResultWrapper wrapper = template.queryColumns("key1"); assertEquals("value1",wrapper.getString("column1")); - -/* String value = template.queryColumns("key1", "", "", new ColumnFamilyRowMapper() { - @Override - public String mapRow(ColumnFamilyResult results) { - // TODO Auto-generated method stub - return results.getString("column1"); - } - }); - assertEquals("value1",value); - */ - - + } @Test From 8c303dd5649687502b13d8082831fab4232a1a3b Mon Sep 17 00:00:00 2001 From: zznate Date: Fri, 18 Mar 2011 16:01:56 -0500 Subject: [PATCH 019/144] added missing test cases --- .../BaseColumnFamilyTemplateTest.java | 23 +++++++++++++ .../service/template/SuperCfTemplateTest.java | 32 +++++++++++++++++++ 2 files changed, 55 insertions(+) create mode 100644 core/src/test/java/me/prettyprint/cassandra/service/template/BaseColumnFamilyTemplateTest.java create mode 100644 core/src/test/java/me/prettyprint/cassandra/service/template/SuperCfTemplateTest.java diff --git a/core/src/test/java/me/prettyprint/cassandra/service/template/BaseColumnFamilyTemplateTest.java b/core/src/test/java/me/prettyprint/cassandra/service/template/BaseColumnFamilyTemplateTest.java new file mode 100644 index 000000000..b82e729c5 --- /dev/null +++ b/core/src/test/java/me/prettyprint/cassandra/service/template/BaseColumnFamilyTemplateTest.java @@ -0,0 +1,23 @@ +package me.prettyprint.cassandra.service.template; + +import static me.prettyprint.hector.api.factory.HFactory.createKeyspace; +import static me.prettyprint.hector.api.factory.HFactory.getOrCreateCluster; + +import org.junit.Before; + +import me.prettyprint.cassandra.BaseEmbededServerSetupTest; +import me.prettyprint.cassandra.serializers.StringSerializer; +import me.prettyprint.hector.api.Cluster; +import me.prettyprint.hector.api.Keyspace; + +public abstract class BaseColumnFamilyTemplateTest extends BaseEmbededServerSetupTest { + + protected Keyspace keyspace; + static final StringSerializer se = StringSerializer.get(); + + @Before + public void setupLocal() { + Cluster cluster = getOrCreateCluster("MyCluster", "127.0.0.1:9170"); + keyspace = createKeyspace("Keyspace1", cluster); + } +} diff --git a/core/src/test/java/me/prettyprint/cassandra/service/template/SuperCfTemplateTest.java b/core/src/test/java/me/prettyprint/cassandra/service/template/SuperCfTemplateTest.java new file mode 100644 index 000000000..641b3d3bf --- /dev/null +++ b/core/src/test/java/me/prettyprint/cassandra/service/template/SuperCfTemplateTest.java @@ -0,0 +1,32 @@ +package me.prettyprint.cassandra.service.template; + +import static org.junit.Assert.*; + +import java.util.Arrays; + +import org.junit.Test; + +public class SuperCfTemplateTest extends BaseColumnFamilyTemplateTest { + + @Test + public void testSuperCfInsertReadTemplate() { + SuperCfTemplate sTemplate = + new SuperCfTemplate(keyspace, "Super1", se, se, se); + sTemplate.update("super_key_1", Arrays.asList("super_name_1"), new SuperCfUpdater() { + @Override + public String update(String obj) { + setString("sub_col_1", "sub_val_1"); + return obj; + } + }); + + assertEquals("sub_val_1",sTemplate.querySingleSubColumn("super_key_1", "super_name_1", "sub_col_1", se).getValue()); + } + + @Test + public void testSuperCfInsertRead() { + SuperCfTemplate sTemplate = + new SuperCfTemplate(keyspace, "Super1", se, se, se); + + } +} From 91aab2b9861a873317241e00eb2c404e77986579 Mon Sep 17 00:00:00 2001 From: zznate Date: Fri, 18 Mar 2011 17:20:51 -0500 Subject: [PATCH 020/144] added biginteger serializer --- .../serializers/BigIntegerSerializer.java | 39 +++++++++++++++++++ .../serializers/BigIntegerSerializerTest.java | 21 ++++++++++ 2 files changed, 60 insertions(+) create mode 100644 core/src/main/java/me/prettyprint/cassandra/serializers/BigIntegerSerializer.java create mode 100644 core/src/test/java/me/prettyprint/cassandra/serializers/BigIntegerSerializerTest.java diff --git a/core/src/main/java/me/prettyprint/cassandra/serializers/BigIntegerSerializer.java b/core/src/main/java/me/prettyprint/cassandra/serializers/BigIntegerSerializer.java new file mode 100644 index 000000000..df85714af --- /dev/null +++ b/core/src/main/java/me/prettyprint/cassandra/serializers/BigIntegerSerializer.java @@ -0,0 +1,39 @@ +package me.prettyprint.cassandra.serializers; + +import java.math.BigInteger; +import java.nio.ByteBuffer; + +/** + * Serializer implementation for BigInteger + * + * @author zznate + */ +public final class BigIntegerSerializer extends AbstractSerializer { + + private static final BigIntegerSerializer INSTANCE = new BigIntegerSerializer(); + + public static BigIntegerSerializer get() { + return INSTANCE; + } + + @Override + public BigInteger fromByteBuffer(ByteBuffer byteBuffer) { + if ( byteBuffer == null ) { + return null; + } + int length = byteBuffer.remaining(); + byte[] bytes = new byte[length]; + byteBuffer.duplicate().get(bytes); + return new BigInteger(bytes); + } + + @Override + public ByteBuffer toByteBuffer(BigInteger obj) { + if ( obj == null ) { + return null; + } + return ByteBuffer.wrap(obj.toByteArray()); + } + + +} diff --git a/core/src/test/java/me/prettyprint/cassandra/serializers/BigIntegerSerializerTest.java b/core/src/test/java/me/prettyprint/cassandra/serializers/BigIntegerSerializerTest.java new file mode 100644 index 000000000..51bc09d8c --- /dev/null +++ b/core/src/test/java/me/prettyprint/cassandra/serializers/BigIntegerSerializerTest.java @@ -0,0 +1,21 @@ +package me.prettyprint.cassandra.serializers; + +import static org.junit.Assert.*; + +import java.math.BigInteger; +import java.nio.ByteBuffer; + +import org.junit.Test; + +public class BigIntegerSerializerTest { + + @Test + public void testToAndFrom() { + BigInteger bi = new BigInteger(new byte[]{0,0,0,1}); + ByteBuffer bb = BigIntegerSerializer.get().toByteBuffer(bi); + assertTrue(bb != null); + assertEquals(1,bb.array().length); + assertEquals(bi, BigIntegerSerializer.get().fromByteBuffer(bb)); + } + +} From 3d9fb844ef50e52c3a19fb7e1235fbcbf978e8c2 Mon Sep 17 00:00:00 2001 From: zznate Date: Fri, 18 Mar 2011 17:36:09 -0500 Subject: [PATCH 021/144] added ascii serializer --- .../serializers/AsciiSerializer.java | 39 +++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 core/src/main/java/me/prettyprint/cassandra/serializers/AsciiSerializer.java diff --git a/core/src/main/java/me/prettyprint/cassandra/serializers/AsciiSerializer.java b/core/src/main/java/me/prettyprint/cassandra/serializers/AsciiSerializer.java new file mode 100644 index 000000000..e8fdb8ed9 --- /dev/null +++ b/core/src/main/java/me/prettyprint/cassandra/serializers/AsciiSerializer.java @@ -0,0 +1,39 @@ +package me.prettyprint.cassandra.serializers; + +import java.nio.ByteBuffer; +import java.nio.charset.Charset; + +/** + * Almost identical to StringSerializer except we use the US-ASCII + * character set code + * + * @author zznate + */ +public final class AsciiSerializer extends AbstractSerializer { + + private static final String US_ASCII = "US-ASCII"; + private static final AsciiSerializer instance = new AsciiSerializer(); + private static final Charset charset = Charset.forName(US_ASCII); + + public static AsciiSerializer get() { + return instance; + } + + @Override + public String fromByteBuffer(ByteBuffer byteBuffer) { + if (byteBuffer == null) { + return null; + } + return charset.decode(byteBuffer).toString(); + } + + @Override + public ByteBuffer toByteBuffer(String obj) { + if (obj == null) { + return null; + } + return ByteBuffer.wrap(obj.getBytes(charset)); + } + + +} From a1f50d76512d4f43f00403432c4b730b2bf533b8 Mon Sep 17 00:00:00 2001 From: zznate Date: Mon, 21 Mar 2011 11:03:11 -0500 Subject: [PATCH 022/144] added keyCacheSavePeriodInSec. was missing. GH issue 191 --- .../cassandra/model/BasicColumnFamilyDefinition.java | 11 +++++++++++ .../me/prettyprint/cassandra/service/ThriftCfDef.java | 10 ++++++++++ .../hector/api/ddl/ColumnFamilyDefinition.java | 1 + 3 files changed, 22 insertions(+) diff --git a/core/src/main/java/me/prettyprint/cassandra/model/BasicColumnFamilyDefinition.java b/core/src/main/java/me/prettyprint/cassandra/model/BasicColumnFamilyDefinition.java index 96527b38e..bc7fc5f50 100644 --- a/core/src/main/java/me/prettyprint/cassandra/model/BasicColumnFamilyDefinition.java +++ b/core/src/main/java/me/prettyprint/cassandra/model/BasicColumnFamilyDefinition.java @@ -29,6 +29,7 @@ public class BasicColumnFamilyDefinition implements ColumnFamilyDefinition { private int maxCompactionThreshold; private int minCompactionThreshold; private int rowCacheSavePeriodInSeconds; + private int keyCacheSavePeriodInSeconds; private double memtableOperationsInMillions; private int memtableThroughputInMb; private int memtableFlushAfterMins; @@ -53,6 +54,7 @@ public BasicColumnFamilyDefinition(ColumnFamilyDefinition columnFamilyDefinition rowCacheSize = columnFamilyDefinition.getRowCacheSize(); rowCacheSavePeriodInSeconds = columnFamilyDefinition.getRowCacheSavePeriodInSeconds(); keyCacheSize = columnFamilyDefinition.getKeyCacheSize(); + keyCacheSavePeriodInSeconds = columnFamilyDefinition.getKeyCacheSavePeriodInSeconds(); readRepairChance = columnFamilyDefinition.getReadRepairChance(); columnDefinitions = columnFamilyDefinition.getColumnMetadata() != null ? new ArrayList(columnFamilyDefinition.getColumnMetadata()) @@ -141,7 +143,12 @@ public void setMemtableFlushAfterMins(int memtableFlushAfterMins) { public void addColumnDefinition( ColumnDefinition columnDefinition){ this.columnDefinitions.add( columnDefinition ); + } + + public void setKeyCacheSavePeriodInSeconds(int keyCacheSavePeriodInSeconds) { + this.keyCacheSavePeriodInSeconds = keyCacheSavePeriodInSeconds; } + /** * SHOULD THIS BE HERE? A COLUMN DEFINITION IS PART OF A KEYSPACE BY VIRTUE @@ -242,5 +249,9 @@ public int getMemtableThroughputInMb() { return this.memtableThroughputInMb; } + public int getKeyCacheSavePeriodInSeconds() { + return keyCacheSavePeriodInSeconds; + } + } diff --git a/core/src/main/java/me/prettyprint/cassandra/service/ThriftCfDef.java b/core/src/main/java/me/prettyprint/cassandra/service/ThriftCfDef.java index 56ee9581e..25963c649 100644 --- a/core/src/main/java/me/prettyprint/cassandra/service/ThriftCfDef.java +++ b/core/src/main/java/me/prettyprint/cassandra/service/ThriftCfDef.java @@ -36,6 +36,7 @@ public class ThriftCfDef implements ColumnFamilyDefinition { private double memtableOperationsInMillions; private int memtableThroughputInMb; private int memtableFlushAfterMins; + private int keyCacheSavePeriodInSeconds; public ThriftCfDef(CfDef d) { Assert.notNull(d, "CfDef is null"); @@ -48,6 +49,7 @@ public ThriftCfDef(CfDef d) { rowCacheSize = d.row_cache_size; rowCacheSavePeriodInSeconds = d.row_cache_save_period_in_seconds; keyCacheSize = d.key_cache_size; + keyCacheSavePeriodInSeconds = d.key_cache_save_period_in_seconds; readRepairChance = d.read_repair_chance; columnMetadata = ThriftColumnDef.fromThriftList(d.column_metadata); gcGraceSeconds = d.gc_grace_seconds; @@ -76,6 +78,7 @@ public ThriftCfDef(ColumnFamilyDefinition columnFamilyDefinition) { rowCacheSize = columnFamilyDefinition.getRowCacheSize(); rowCacheSavePeriodInSeconds = columnFamilyDefinition.getRowCacheSavePeriodInSeconds(); keyCacheSize = columnFamilyDefinition.getKeyCacheSize(); + keyCacheSavePeriodInSeconds = columnFamilyDefinition.getKeyCacheSavePeriodInSeconds(); readRepairChance = columnFamilyDefinition.getReadRepairChance(); columnMetadata = columnFamilyDefinition.getColumnMetadata(); gcGraceSeconds = columnFamilyDefinition.getGcGraceSeconds(); @@ -102,6 +105,7 @@ public ThriftCfDef(String keyspace, String columnFamilyName) { comparatorType = ComparatorType.BYTESTYPE; readRepairChance = CFMetaData.DEFAULT_READ_REPAIR_CHANCE; keyCacheSize = CFMetaData.DEFAULT_KEY_CACHE_SIZE; + keyCacheSavePeriodInSeconds = CFMetaData.DEFAULT_KEY_CACHE_SAVE_PERIOD_IN_SECONDS; gcGraceSeconds = CFMetaData.DEFAULT_GC_GRACE_SECONDS; minCompactionThreshold = CFMetaData.DEFAULT_MIN_COMPACTION_THRESHOLD; maxCompactionThreshold = CFMetaData.DEFAULT_MAX_COMPACTION_THRESHOLD; @@ -217,6 +221,7 @@ public CfDef toThrift() { d.setGc_grace_seconds(gcGraceSeconds); d.setId(id); d.setKey_cache_size(keyCacheSize); + d.setKey_cache_save_period_in_seconds(keyCacheSavePeriodInSeconds); d.setMax_compaction_threshold(maxCompactionThreshold); d.setMin_compaction_threshold(minCompactionThreshold); d.setRead_repair_chance(readRepairChance); @@ -326,4 +331,9 @@ public double getMemtableOperationsInMillions() { public int getMemtableThroughputInMb() { return memtableThroughputInMb; } + + @Override + public int getKeyCacheSavePeriodInSeconds() { + return keyCacheSavePeriodInSeconds; + } } diff --git a/core/src/main/java/me/prettyprint/hector/api/ddl/ColumnFamilyDefinition.java b/core/src/main/java/me/prettyprint/hector/api/ddl/ColumnFamilyDefinition.java index 241e61c79..8e5bae5ab 100644 --- a/core/src/main/java/me/prettyprint/hector/api/ddl/ColumnFamilyDefinition.java +++ b/core/src/main/java/me/prettyprint/hector/api/ddl/ColumnFamilyDefinition.java @@ -18,6 +18,7 @@ public interface ColumnFamilyDefinition { String getComment(); double getRowCacheSize(); int getRowCacheSavePeriodInSeconds(); + int getKeyCacheSavePeriodInSeconds(); double getKeyCacheSize(); double getReadRepairChance(); List getColumnMetadata(); From d5ab3ccc36b647f2679a61169ba218f77125fcb3 Mon Sep 17 00:00:00 2001 From: zznate Date: Mon, 21 Mar 2011 12:02:30 -0500 Subject: [PATCH 023/144] updated doc link to point to wiki --- README | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/README b/README index 83bec4fdf..cadaeb644 100644 --- a/README +++ b/README @@ -25,9 +25,10 @@ Some features provided by this client: o simple ORM layer that works o a type-safe approach to dealing with Apache Cassandra's data model -Detailed documentation of Hector features and usage can be found in PDF form hosted by Riptano: http://www.riptano.com/sites/default/files/hector-v2-client-doc.pdf +Detailed documentation of Hector features and usage can be found on the wiki: +https://github.com/rantav/hector/wiki/User-Guide -Some interesting pages from the wiki: +Some additional pages from the wiki that may be of interest: o SLF4J fun and hijinks: https://github.com/rantav/hector/wiki/SLF4J-in-Hector- o Mailing Lists: https://github.com/rantav/hector/wiki/Mailing-Lists From 6e39d161b9156f79795944d595900cdd23867208 Mon Sep 17 00:00:00 2001 From: zznate Date: Mon, 21 Mar 2011 15:59:38 -0500 Subject: [PATCH 024/144] updated template, mapping results, for standard, cleaner iface definitions --- .../template/ColumnFamilyResultsIterator.java | 17 ---- .../template/ColumnFamilyTemplate.java | 90 ++++++++++++------- .../service/template/ColumnFamilyUpdater.java | 36 +++++--- .../template/MappedColumnFamilyResult.java | 7 ++ .../MappedColumnFamilyResultWrapper.java | 26 ++++++ .../service/template/SuperCfTemplate.java | 7 +- .../template/ColumnFamilyTemplateTest.java | 27 +++++- 7 files changed, 143 insertions(+), 67 deletions(-) delete mode 100644 core/src/main/java/me/prettyprint/cassandra/service/template/ColumnFamilyResultsIterator.java create mode 100644 core/src/main/java/me/prettyprint/cassandra/service/template/MappedColumnFamilyResult.java create mode 100644 core/src/main/java/me/prettyprint/cassandra/service/template/MappedColumnFamilyResultWrapper.java diff --git a/core/src/main/java/me/prettyprint/cassandra/service/template/ColumnFamilyResultsIterator.java b/core/src/main/java/me/prettyprint/cassandra/service/template/ColumnFamilyResultsIterator.java deleted file mode 100644 index 206917381..000000000 --- a/core/src/main/java/me/prettyprint/cassandra/service/template/ColumnFamilyResultsIterator.java +++ /dev/null @@ -1,17 +0,0 @@ -package me.prettyprint.cassandra.service.template; - -import java.util.Iterator; - -/** - * Allows iteration of results from a query with the niceties that Hector provides for direct access. - * - * @author david - * @since Mar 10, 2011 - * @param - * @param - */ -public interface ColumnFamilyResultsIterator extends Iterator { - public T getByKey(K key); - - public int getCount(); -} diff --git a/core/src/main/java/me/prettyprint/cassandra/service/template/ColumnFamilyTemplate.java b/core/src/main/java/me/prettyprint/cassandra/service/template/ColumnFamilyTemplate.java index 5e56962f3..7bcac0187 100644 --- a/core/src/main/java/me/prettyprint/cassandra/service/template/ColumnFamilyTemplate.java +++ b/core/src/main/java/me/prettyprint/cassandra/service/template/ColumnFamilyTemplate.java @@ -75,7 +75,7 @@ public ColumnFamilyTemplate setMutator(Mutator mutator) { public ColumnFamilyUpdater createUpdater(K key) { ColumnFamilyUpdater updater = new ColumnFamilyUpdater(); - updater.key = key; + updater.addKey(key); updater.template = this; return updater; } @@ -95,7 +95,7 @@ public void update(ColumnFamilyUpdater updater) { */ public void update(K key, ColumnFamilyUpdater updater) { updater.template = this; - updater.key = key; + updater.addKey(key); update(updater); } @@ -196,29 +196,26 @@ private T executeSliceQuery(K key, HSlicePredicate predicate, ColumnFamilyRo } -/* @SuppressWarnings("unchecked") - public ColumnFamilyResultsIterator queryColumns(Iterable keys, - ColumnFamilyRowMapper mapper) { - return queryColumns(keys, (N) ALL_COLUMNS_START, (N) ALL_COLUMNS_END, - mapper); + public MappedColumnFamilyResult queryColumns(Iterable keys, + ColumnFamilyRowMapper mapper) { + return doExecuteMultigetSlice(keys, activeSlicePredicate, mapper); } - public ColumnFamilyResults queryColumns(Iterable keys, - N start, N end, ColumnFamilyRowMapper mapper) { - MultigetSliceQuery query = createMultigetSliceQuery(keys); - query.setRange(start, end, false, ALL_COLUMNS_COUNT); - return executeMultigetSliceQuery(keys, query, mapper); + public MappedColumnFamilyResult queryColumns(Iterable keys, + N start, N end, ColumnFamilyRowMapper mapper) { + HSlicePredicate predicate = new HSlicePredicate(topSerializer); + predicate.setStartOn(start); + predicate.setEndOn(end); + predicate.setCount(100); + return doExecuteMultigetSlice(keys, predicate, mapper); } - @SuppressWarnings("unchecked") - public ColumnFamilyResults queryColumns(Iterable key, - List columns, ColumnFamilyRowMapper mapper) { - + public MappedColumnFamilyResult queryColumns(Iterable keys, + List columns, ColumnFamilyRowMapper mapper) { HSlicePredicate predicate = new HSlicePredicate(topSerializer); predicate.setColumnNames(columns); - return mapper.mapRow(doExecuteMultigetSlice(key, predicate)); + return doExecuteMultigetSlice(keys, predicate, mapper); } -*/ @SuppressWarnings("unchecked") @@ -239,48 +236,75 @@ public HColumn querySingleColumn(K key, N columnName, return result != null ? result.get() : null; } - private ColumnFamilyResultWrapper doExecuteSlice(final K key, final HSlicePredicate workingSlicePredicate) { - - return ((ExecutingKeyspace)keyspace).doExecuteOperation(new Operation>(OperationType.READ) { + private ColumnFamilyResultWrapper doExecuteSlice(final K key, final HSlicePredicate workingSlicePredicate) { + return new ColumnFamilyResultWrapper(keySerializer, topSerializer, + sliceInternal(key, workingSlicePredicate)); + } + + private MappedColumnFamilyResult doExecuteSlice(final K key, + final HSlicePredicate workingSlicePredicate, + final ColumnFamilyRowMapper mapper) { + return new MappedColumnFamilyResultWrapper(keySerializer, topSerializer, + sliceInternal(key, workingSlicePredicate), mapper); + } + + + private ColumnFamilyResultWrapper doExecuteMultigetSlice(final Iterable keys, final HSlicePredicate workingSlicePredicate) { + return new ColumnFamilyResultWrapper(keySerializer, topSerializer, + multigetSliceInternal(keys, workingSlicePredicate)); + } + + + private MappedColumnFamilyResult doExecuteMultigetSlice(final Iterable keys, + final HSlicePredicate workingSlicePredicate, + final ColumnFamilyRowMapper mapper) { + return new MappedColumnFamilyResultWrapper(keySerializer, topSerializer, + multigetSliceInternal(keys, workingSlicePredicate), mapper); + } + + + private Map> sliceInternal(final K key, + final HSlicePredicate workingSlicePredicate) { + return ((ExecutingKeyspace)keyspace).doExecuteOperation(new Operation>>(OperationType.READ) { @Override - public ColumnFamilyResultWrapper execute(Cassandra.Client cassandra) throws HectorException { + public Map> execute(Cassandra.Client cassandra) throws HectorException { Map> cosc = new LinkedHashMap>(); try { - + ByteBuffer sKey = keySerializer.toByteBuffer(key); cosc.put(sKey, cassandra.get_slice(sKey, columnParent, workingSlicePredicate.toThrift(), - ThriftConverter.consistencyLevel(consistencyLevelPolicy.get(operationType)))); - + ThriftConverter.consistencyLevel(consistencyLevelPolicy.get(operationType)))); + } catch (Exception e) { throw exceptionsTranslator.translate(e); } - return new ColumnFamilyResultWrapper(keySerializer, topSerializer, cosc); + return cosc; } }).get(); } - private ColumnFamilyResultWrapper doExecuteMultigetSlice(final Iterable keys, final HSlicePredicate workingSlicePredicate) { - - return ((ExecutingKeyspace)keyspace).doExecuteOperation(new Operation>(OperationType.READ) { + private Map> multigetSliceInternal(final Iterable keys, + final HSlicePredicate workingSlicePredicate) { + return ((ExecutingKeyspace)keyspace).doExecuteOperation(new Operation>>(OperationType.READ) { @Override - public ColumnFamilyResultWrapper execute(Cassandra.Client cassandra) throws HectorException { - Map> cosc; + public Map> execute(Cassandra.Client cassandra) throws HectorException { + Map> cosc = new LinkedHashMap>(); try { List keyList = new ArrayList(); Iterators.addAll(keyList, keys.iterator()); cosc = cassandra.multiget_slice(keySerializer.toBytesList(keyList), columnParent, (workingSlicePredicate == null ? activeSlicePredicate.setColumnNames(columnValueSerializers.keySet()).toThrift() : workingSlicePredicate.toThrift()), ThriftConverter.consistencyLevel(consistencyLevelPolicy.get(operationType))); - } catch (Exception e) { throw exceptionsTranslator.translate(e); } - return new ColumnFamilyResultWrapper(keySerializer, topSerializer, cosc); + return cosc; } }).get(); } + } diff --git a/core/src/main/java/me/prettyprint/cassandra/service/template/ColumnFamilyUpdater.java b/core/src/main/java/me/prettyprint/cassandra/service/template/ColumnFamilyUpdater.java index 59d9c8bcb..e9238fd25 100644 --- a/core/src/main/java/me/prettyprint/cassandra/service/template/ColumnFamilyUpdater.java +++ b/core/src/main/java/me/prettyprint/cassandra/service/template/ColumnFamilyUpdater.java @@ -1,6 +1,8 @@ package me.prettyprint.cassandra.service.template; +import java.util.ArrayList; import java.util.Date; +import java.util.List; import java.util.UUID; import me.prettyprint.cassandra.serializers.BooleanSerializer; @@ -41,7 +43,8 @@ public class ColumnFamilyUpdater { // Values have package access and are assigned by CassandraTemplate ColumnFamilyTemplate template; - K key; + List keys; + int keyPos = 0; /** * Default no-op update implementation. Sub-classes should override this to provide @@ -51,57 +54,68 @@ public void update() { // TODO think about this contract in general } + public ColumnFamilyUpdater addKey(K key) { + if ( keys == null ) { + keys = new ArrayList(); + } else { + keyPos++; + } + keys.add(key); + + return this; + } + /** * @return Give the updater access to the current key if it needs it */ - public K getKey() { - return key; + public K getCurrentKey() { + return keys.get(keyPos); } public void deleteColumn(N columnName) { - template.getMutator().addDeletion(key, template.getColumnFamily(), + template.getMutator().addDeletion(getCurrentKey(), template.getColumnFamily(), columnName, template.getTopSerializer()); } public void setString(N columnName, String value) { HColumn column = HFactory.createColumn(columnName, value, template.getTopSerializer(), StringSerializer.get()); - template.getMutator().addInsertion(key, template.getColumnFamily(), column); + template.getMutator().addInsertion(getCurrentKey(), template.getColumnFamily(), column); } public void setUUID(N columnName, UUID value) { HColumn column = HFactory.createColumn(columnName, value, template.getTopSerializer(), UUIDSerializer.get()); - template.getMutator().addInsertion(key, template.getColumnFamily(), column); + template.getMutator().addInsertion(getCurrentKey(), template.getColumnFamily(), column); } public void setLong(N columnName, Long value) { HColumn column = HFactory.createColumn(columnName, value, template.getTopSerializer(), LongSerializer.get()); - template.getMutator().addInsertion(key, template.getColumnFamily(), column); + template.getMutator().addInsertion(getCurrentKey(), template.getColumnFamily(), column); } public void setInteger(N columnName, Integer value) { HColumn column = HFactory.createColumn(columnName, value, template.getTopSerializer(), IntegerSerializer.get()); - template.getMutator().addInsertion(key, template.getColumnFamily(), column); + template.getMutator().addInsertion(getCurrentKey(), template.getColumnFamily(), column); } public void setBoolean(N columnName, Boolean value) { HColumn column = HFactory.createColumn(columnName, value, template.getTopSerializer(), BooleanSerializer.get()); - template.getMutator().addInsertion(key, template.getColumnFamily(), column); + template.getMutator().addInsertion(getCurrentKey(), template.getColumnFamily(), column); } public void setByteArray(N columnName, byte[] value) { HColumn column = HFactory.createColumn(columnName, value, template.getTopSerializer(), BytesArraySerializer.get()); - template.getMutator().addInsertion(key, template.getColumnFamily(), column); + template.getMutator().addInsertion(getCurrentKey(), template.getColumnFamily(), column); } public void setDate(N columnName, Date value) { HColumn column = HFactory.createColumn(columnName, value, template.getTopSerializer(), DateSerializer.get()); - template.getMutator().addInsertion(key, template.getColumnFamily(), column); + template.getMutator().addInsertion(getCurrentKey(), template.getColumnFamily(), column); } } diff --git a/core/src/main/java/me/prettyprint/cassandra/service/template/MappedColumnFamilyResult.java b/core/src/main/java/me/prettyprint/cassandra/service/template/MappedColumnFamilyResult.java new file mode 100644 index 000000000..f1fc54cb3 --- /dev/null +++ b/core/src/main/java/me/prettyprint/cassandra/service/template/MappedColumnFamilyResult.java @@ -0,0 +1,7 @@ +package me.prettyprint.cassandra.service.template; + +public interface MappedColumnFamilyResult extends ColumnFamilyResult { + + V getRow(); + +} diff --git a/core/src/main/java/me/prettyprint/cassandra/service/template/MappedColumnFamilyResultWrapper.java b/core/src/main/java/me/prettyprint/cassandra/service/template/MappedColumnFamilyResultWrapper.java new file mode 100644 index 000000000..aaa347109 --- /dev/null +++ b/core/src/main/java/me/prettyprint/cassandra/service/template/MappedColumnFamilyResultWrapper.java @@ -0,0 +1,26 @@ +package me.prettyprint.cassandra.service.template; + +import java.nio.ByteBuffer; +import java.util.List; +import java.util.Map; + +import me.prettyprint.hector.api.Serializer; + +import org.apache.cassandra.thrift.ColumnOrSuperColumn; + +public class MappedColumnFamilyResultWrapper extends ColumnFamilyResultWrapper implements MappedColumnFamilyResult{ + + private ColumnFamilyRowMapper rowMapper; + + public MappedColumnFamilyResultWrapper(Serializer keySerializer, + Serializer columnNameSerializer, + Map> rows, ColumnFamilyRowMapper mapper) { + super(keySerializer, columnNameSerializer, rows); + } + + @Override + public V getRow() { + return rowMapper.mapRow(this); + } + +} diff --git a/core/src/main/java/me/prettyprint/cassandra/service/template/SuperCfTemplate.java b/core/src/main/java/me/prettyprint/cassandra/service/template/SuperCfTemplate.java index 753381e0e..ab0f2341c 100644 --- a/core/src/main/java/me/prettyprint/cassandra/service/template/SuperCfTemplate.java +++ b/core/src/main/java/me/prettyprint/cassandra/service/template/SuperCfTemplate.java @@ -247,7 +247,7 @@ private SuperSliceQuery createSuperSliceQuery( query.setColumnFamily(columnFamily); return query; } - +/* @SuppressWarnings("unchecked") public ColumnFamilyResultsIterator> querySuperColumns( List key, SuperCfRowMapper mapper) { @@ -308,14 +308,15 @@ private List mapRow(SuperRow row) { List ret = new ArrayList(); for (HSuperColumn superCol : slice .getSuperColumns()) { -/* SuperCfResultWrapper wrapper = new SuperCfResultWrapper(row.getKey(), + SuperCfResultWrapper wrapper = new SuperCfResultWrapper(row.getKey(), superCol); - ret.add(mapper.mapRow(wrapper));*/ + ret.add(mapper.mapRow(wrapper)); } return ret; } }; } + */ @SuppressWarnings("unchecked") private MultigetSuperSliceQuery createMultigetSuperSliceQuery( diff --git a/core/src/test/java/me/prettyprint/cassandra/service/template/ColumnFamilyTemplateTest.java b/core/src/test/java/me/prettyprint/cassandra/service/template/ColumnFamilyTemplateTest.java index 5bc0c9e06..688f86f6d 100644 --- a/core/src/test/java/me/prettyprint/cassandra/service/template/ColumnFamilyTemplateTest.java +++ b/core/src/test/java/me/prettyprint/cassandra/service/template/ColumnFamilyTemplateTest.java @@ -1,6 +1,9 @@ package me.prettyprint.cassandra.service.template; import static org.junit.Assert.assertEquals; + +import java.util.Arrays; + import me.prettyprint.hector.api.factory.HFactory; import org.junit.Test; @@ -38,9 +41,27 @@ public String mapRow(ColumnFamilyResult results) { return results.getString("column1"); } }); - assertEquals("value1",value); - - + assertEquals("value1",value); + } + + @Test + public void testQueryMultiget() { + ColumnFamilyTemplate template = new ColumnFamilyTemplate(keyspace, "Standard1", se, se, HFactory.createMutator(keyspace, se)); + ColumnFamilyUpdater updater = template.createUpdater("mg_key1"); + updater.setString("column1","value1"); + updater.addKey("mg_key2"); + updater.setString("column1","value2"); + updater.addKey("mg_key3"); + updater.setString("column1","value3"); + template.update(updater); + + template.addColumn("column1", se); + ColumnFamilyResultWrapper wrapper = template.queryColumns(Arrays.asList("mg_key1", "mg_key2", "mg_key3")); + assertEquals("value1",wrapper.getString("column1")); + wrapper.next(); + assertEquals("value2",wrapper.getString("column1")); + wrapper.next(); + assertEquals("value3",wrapper.getString("column1")); } } From 65e1b532ab626e1f7c168ef84349f852239028c9 Mon Sep 17 00:00:00 2001 From: Ed Anuff Date: Tue, 22 Mar 2011 13:50:03 -0700 Subject: [PATCH 025/144] moved in DynamicComponent bean and support classes for support of upcoming composite columns in Cassandra --- .../utils/ByteBufferOutputStream.java | 165 +++++++++ .../hector/api/beans/DynamicComposite.java | 327 ++++++++++++++++++ .../api/beans/DynamicCompositeSerialzier.java | 32 ++ 3 files changed, 524 insertions(+) create mode 100644 core/src/main/java/me/prettyprint/cassandra/utils/ByteBufferOutputStream.java create mode 100644 core/src/main/java/me/prettyprint/hector/api/beans/DynamicComposite.java create mode 100644 core/src/main/java/me/prettyprint/hector/api/beans/DynamicCompositeSerialzier.java diff --git a/core/src/main/java/me/prettyprint/cassandra/utils/ByteBufferOutputStream.java b/core/src/main/java/me/prettyprint/cassandra/utils/ByteBufferOutputStream.java new file mode 100644 index 000000000..8e847a732 --- /dev/null +++ b/core/src/main/java/me/prettyprint/cassandra/utils/ByteBufferOutputStream.java @@ -0,0 +1,165 @@ +package me.prettyprint.cassandra.utils; + +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import java.io.IOException; +import java.io.OutputStream; +import java.nio.ByteBuffer; +import java.util.LinkedList; +import java.util.List; + +/** + * Utility to collect data written to an {@link OutputStream} in + * {@link ByteBuffer}s. + * + * Originally from org.apache.avro.util.ByteBufferOutputStream, moved into + * Hector and added getByteBuffer to return single ByteBuffer from contents. + */ +public class ByteBufferOutputStream extends OutputStream { + public static final int BUFFER_SIZE = 8192; + + private List buffers; + + public ByteBufferOutputStream() { + reset(); + } + + /** Returns all data written and resets the stream to be empty. */ + public List getBufferList() { + List result = buffers; + reset(); + for (ByteBuffer buffer : result) { + buffer.flip(); + } + return result; + } + + public ByteBuffer getByteBuffer() { + List list = getBufferList(); + // if there's just one bytebuffer in list, return it + if (list.size() == 1) { + return list.get(0); + } + int size = 0; + for (ByteBuffer buffer : list) { + size += buffer.remaining(); + } + ByteBuffer result = ByteBuffer.allocate(size); + for (ByteBuffer buffer : list) { + result.put(buffer); + } + return result; + } + + /** Prepend a list of ByteBuffers to this stream. */ + public void prepend(List lists) { + for (ByteBuffer buffer : lists) { + buffer.position(buffer.limit()); + } + buffers.addAll(0, lists); + } + + /** Append a list of ByteBuffers to this stream. */ + public void append(List lists) { + for (ByteBuffer buffer : lists) { + buffer.position(buffer.limit()); + } + buffers.addAll(lists); + } + + public void reset() { + buffers = new LinkedList(); + buffers.add(ByteBuffer.allocate(BUFFER_SIZE)); + } + + public void write(ByteBuffer buffer) { + buffers.add(buffer); + } + + private ByteBuffer getBufferWithCapacity(int capacity) { + ByteBuffer buffer = buffers.get(buffers.size() - 1); + if (buffer.remaining() < capacity) { + buffer = ByteBuffer.allocate(BUFFER_SIZE); + buffers.add(buffer); + } + return buffer; + } + + @Override + public void write(int b) { + ByteBuffer buffer = getBufferWithCapacity(1); + buffer.put((byte) b); + } + + public void writeShort(short value) { + ByteBuffer buffer = getBufferWithCapacity(2); + buffer.putShort(value); + } + + public void writeChar(char value) { + ByteBuffer buffer = getBufferWithCapacity(2); + buffer.putChar(value); + } + + public void writeInt(int value) { + ByteBuffer buffer = getBufferWithCapacity(4); + buffer.putInt(value); + } + + public void writeFloat(float value) { + ByteBuffer buffer = getBufferWithCapacity(4); + buffer.putFloat(value); + } + + public void writeLong(long value) { + ByteBuffer buffer = getBufferWithCapacity(8); + buffer.putLong(value); + } + + public void writeDouble(double value) { + ByteBuffer buffer = getBufferWithCapacity(8); + buffer.putDouble(value); + } + + @Override + public void write(byte[] b, int off, int len) { + ByteBuffer buffer = buffers.get(buffers.size() - 1); + int remaining = buffer.remaining(); + while (len > remaining) { + buffer.put(b, off, remaining); + len -= remaining; + off += remaining; + buffer = ByteBuffer.allocate(BUFFER_SIZE); + buffers.add(buffer); + remaining = buffer.remaining(); + } + buffer.put(b, off, len); + } + + /** Add a buffer to the output without copying, if possible. */ + public void writeBuffer(ByteBuffer buffer) throws IOException { + if (buffer.remaining() < BUFFER_SIZE) { + write(buffer.array(), buffer.position(), buffer.remaining()); + } else { // append w/o copying bytes + ByteBuffer dup = buffer.duplicate(); + dup.position(buffer.limit()); // ready for flip + buffers.add(dup); + } + } +} diff --git a/core/src/main/java/me/prettyprint/hector/api/beans/DynamicComposite.java b/core/src/main/java/me/prettyprint/hector/api/beans/DynamicComposite.java new file mode 100644 index 000000000..0bfde634d --- /dev/null +++ b/core/src/main/java/me/prettyprint/hector/api/beans/DynamicComposite.java @@ -0,0 +1,327 @@ +package me.prettyprint.hector.api.beans; + +import java.nio.ByteBuffer; +import java.util.AbstractList; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.logging.Logger; + +import me.prettyprint.cassandra.serializers.AsciiSerializer; +import me.prettyprint.cassandra.serializers.BigIntegerSerializer; +import me.prettyprint.cassandra.serializers.BooleanSerializer; +import me.prettyprint.cassandra.serializers.ByteBufferSerializer; +import me.prettyprint.cassandra.serializers.BytesArraySerializer; +import me.prettyprint.cassandra.serializers.LongSerializer; +import me.prettyprint.cassandra.serializers.SerializerTypeInferer; +import me.prettyprint.cassandra.serializers.ShortSerializer; +import me.prettyprint.cassandra.serializers.StringSerializer; +import me.prettyprint.cassandra.serializers.UUIDSerializer; +import me.prettyprint.cassandra.utils.ByteBufferOutputStream; +import me.prettyprint.hector.api.Serializer; + +import org.apache.cassandra.utils.ByteBufferUtil; + +@SuppressWarnings("rawtypes") +public class DynamicComposite extends AbstractList implements + Comparable { + + static final Logger logger = Logger.getLogger(DynamicComposite.class + .getName()); + + public static final Map, String> DEFAULT_SERIALIZER_TO_COMPARER_MAPPING; + + static { + DEFAULT_SERIALIZER_TO_COMPARER_MAPPING = new HashMap, String>(); + DEFAULT_SERIALIZER_TO_COMPARER_MAPPING.put(AsciiSerializer.class, + "AsciiType"); + DEFAULT_SERIALIZER_TO_COMPARER_MAPPING.put(BigIntegerSerializer.class, + "IntegerType"); + DEFAULT_SERIALIZER_TO_COMPARER_MAPPING.put(BooleanSerializer.class, + "BytesType"); + DEFAULT_SERIALIZER_TO_COMPARER_MAPPING.put(ByteBufferSerializer.class, + "BytesType"); + DEFAULT_SERIALIZER_TO_COMPARER_MAPPING.put(BytesArraySerializer.class, + "BytesType"); + DEFAULT_SERIALIZER_TO_COMPARER_MAPPING + .put(LongSerializer.class, "LongType"); + DEFAULT_SERIALIZER_TO_COMPARER_MAPPING.put(ShortSerializer.class, + "BytesType"); + DEFAULT_SERIALIZER_TO_COMPARER_MAPPING.put(StringSerializer.class, + "UTF8Type"); + DEFAULT_SERIALIZER_TO_COMPARER_MAPPING + .put(UUIDSerializer.class, "UUIDType"); + } + + public static final Map DEFAULT_COMPARER_TO_SERIALIZER_MAPPING = new HashMap(); + + static { + DEFAULT_COMPARER_TO_SERIALIZER_MAPPING.put("AsciiType", + ByteBufferSerializer.get()); + DEFAULT_COMPARER_TO_SERIALIZER_MAPPING.put("BytesType", + ByteBufferSerializer.get()); + DEFAULT_COMPARER_TO_SERIALIZER_MAPPING.put("IntegerType", + ByteBufferSerializer.get()); + DEFAULT_COMPARER_TO_SERIALIZER_MAPPING.put("LexicalUUIDType", + ByteBufferSerializer.get()); + DEFAULT_COMPARER_TO_SERIALIZER_MAPPING.put("LongType", + ByteBufferSerializer.get()); + DEFAULT_COMPARER_TO_SERIALIZER_MAPPING.put("TimeUUIDType", + ByteBufferSerializer.get()); + DEFAULT_COMPARER_TO_SERIALIZER_MAPPING.put("UTF8Type", + ByteBufferSerializer.get()); + + } + + public static final Map DEFAULT_ALIAS_TO_COMPARER_MAPPING = new HashMap(); + + static { + DEFAULT_ALIAS_TO_COMPARER_MAPPING.put((byte) 'a', "AsciiType"); + DEFAULT_ALIAS_TO_COMPARER_MAPPING.put((byte) 'b', "BytesType"); + DEFAULT_ALIAS_TO_COMPARER_MAPPING.put((byte) 'i', "IntegerType"); + DEFAULT_ALIAS_TO_COMPARER_MAPPING.put((byte) 'x', "LexicalUUIDType"); + DEFAULT_ALIAS_TO_COMPARER_MAPPING.put((byte) 'l', "LongType"); + DEFAULT_ALIAS_TO_COMPARER_MAPPING.put((byte) 't', "TimeUUIDType"); + DEFAULT_ALIAS_TO_COMPARER_MAPPING.put((byte) 's', "UTF8Type"); + + } + + public static final Map DEFAULT_COMPARER_TO_ALIAS_MAPPING = new HashMap(); + + static { + DEFAULT_COMPARER_TO_ALIAS_MAPPING.put("AsciiType", (byte) 'a'); + DEFAULT_COMPARER_TO_ALIAS_MAPPING.put("BytesType", (byte) 'b'); + DEFAULT_COMPARER_TO_ALIAS_MAPPING.put("IntegerType", (byte) 'i'); + DEFAULT_COMPARER_TO_ALIAS_MAPPING.put("LexicalUUIDType", (byte) 'x'); + DEFAULT_COMPARER_TO_ALIAS_MAPPING.put("LongType", (byte) 'l'); + DEFAULT_COMPARER_TO_ALIAS_MAPPING.put("TimeUUIDType", (byte) 't'); + DEFAULT_COMPARER_TO_ALIAS_MAPPING.put("UTF8Type", (byte) 's'); + + } + + Map, String> serializerToComparerMapping = DEFAULT_SERIALIZER_TO_COMPARER_MAPPING; + + Map comparerToSerializerMapping = DEFAULT_COMPARER_TO_SERIALIZER_MAPPING; + + Map aliasesToComparerMapping = DEFAULT_ALIAS_TO_COMPARER_MAPPING; + + Map comparerToAliasMapping = DEFAULT_COMPARER_TO_ALIAS_MAPPING; + + public class Component { + final Serializer serializer; + final T value; + final String comparer; + final boolean inclusive; + + public Component(T value, Serializer serializer, String comparer, + boolean inclusive) { + this.serializer = serializer; + this.value = value; + this.comparer = comparer; + this.inclusive = inclusive; + } + + public Serializer getSerializer() { + return serializer; + } + + public T getValue() { + return value; + } + + public String getComparer() { + return comparer; + } + + public boolean isInclusive() { + return inclusive; + } + } + + List components = new ArrayList(); + + ByteBuffer serialized = null; + + public DynamicComposite() { + + } + + public List getComponents() { + return components; + } + + public void setComponents(List components) { + serialized = null; + this.components = components; + } + + public Map, String> getSerializerToComparerMapping() { + return serializerToComparerMapping; + } + + public void setSerializerToComparerMapping( + Map, String> serializerToComparerMapping) { + serialized = null; + this.serializerToComparerMapping = serializerToComparerMapping; + } + + public Map getComparerToSerializerMapping() { + return comparerToSerializerMapping; + } + + public void setComparerToSerializerMapping( + Map comparerToSerializerMapping) { + serialized = null; + this.comparerToSerializerMapping = comparerToSerializerMapping; + } + + public Map getAliasesToComparerMapping() { + return aliasesToComparerMapping; + } + + public void setAliasesToComparerMapping( + Map aliasesToComparerMapping) { + serialized = null; + this.aliasesToComparerMapping = aliasesToComparerMapping; + } + + public Map getComparerToAliasMapping() { + return comparerToAliasMapping; + } + + public void setComparerToAliasMapping(Map comparerToAliasMapping) { + serialized = null; + this.comparerToAliasMapping = comparerToAliasMapping; + } + + @Override + public int compareTo(DynamicComposite arg0) { + // TODO Auto-generated method stub + return 0; + } + + private String comparerForSerializer(Serializer s) { + String comparer = serializerToComparerMapping.get(s); + if (comparer != null) { + return comparer; + } + return "BytesType"; + } + + public DynamicComposite add(T value, Serializer s) { + serialized = null; + + add(value, s, comparerForSerializer(s)); + + return this; + + } + + public DynamicComposite add(T value, Serializer s, String comparer) { + serialized = null; + + add(value, s, comparer, false); + + return this; + + } + + @SuppressWarnings("unchecked") + public DynamicComposite add(T value, Serializer s, String comparer, + boolean inclusive) { + serialized = null; + + components.add(new Component(value, s, comparer, inclusive)); + + return this; + + } + + @Override + public void clear() { + serialized = null; + components = new ArrayList(); + } + + @Override + public int size() { + return components.size(); + } + + @SuppressWarnings("unchecked") + @Override + public void add(int index, Object element) { + serialized = null; + Serializer s = SerializerTypeInferer.getSerializer(element); + components.add(index, new Component(element, s, comparerForSerializer(s), + false)); + } + + @Override + public Object remove(int index) { + serialized = null; + Component prev = components.remove(index); + if (prev != null) { + return prev.getValue(); + } + return null; + } + + @SuppressWarnings("unchecked") + @Override + public Object set(int index, Object element) { + serialized = null; + Serializer s = SerializerTypeInferer.getSerializer(element); + Component prev = components.set(index, new Component(element, s, + comparerForSerializer(s), false)); + if (prev != null) { + return prev.getValue(); + } + return null; + } + + @Override + public Object get(int i) { + Component c = components.get(i); + if (c != null) { + return c.getValue(); + } + return null; + } + + @SuppressWarnings("unchecked") + public ByteBuffer serialize() { + if (serialized != null) { + return serialized; + } + + ByteBufferOutputStream out = new ByteBufferOutputStream(); + + for (Component c : components) { + ByteBuffer cb = c.getSerializer().toByteBuffer(c.getValue()); + + if (comparerToAliasMapping.containsKey(c.getComparer())) { + out.writeShort((short) (0x8000 | comparerToAliasMapping.get(c + .getComparer()))); + } else { + out.writeShort((short) c.getComparer().length()); + out.write(ByteBufferUtil.bytes(c.getComparer())); + } + out.write(cb.remaining()); + out.write(cb.slice()); + out.write(c.isInclusive() ? 1 : 0); + } + + serialized = out.getByteBuffer(); + return serialized; + } + + public void deserialize(ByteBuffer b) { + serialized = b; + components = new ArrayList(); + + // TODO deserialize + } + +} diff --git a/core/src/main/java/me/prettyprint/hector/api/beans/DynamicCompositeSerialzier.java b/core/src/main/java/me/prettyprint/hector/api/beans/DynamicCompositeSerialzier.java new file mode 100644 index 000000000..634592c38 --- /dev/null +++ b/core/src/main/java/me/prettyprint/hector/api/beans/DynamicCompositeSerialzier.java @@ -0,0 +1,32 @@ +/** + * + */ +package me.prettyprint.hector.api.beans; + +import java.nio.ByteBuffer; + +import me.prettyprint.cassandra.serializers.AbstractSerializer; + +/** + * @author Todd Nine + * + */ +public class DynamicCompositeSerialzier extends AbstractSerializer{ + + @Override + public ByteBuffer toByteBuffer(DynamicComposite obj) { + + return obj.serialize(); + } + + @Override + public DynamicComposite fromByteBuffer(ByteBuffer byteBuffer) { + + DynamicComposite composite = new DynamicComposite(); + composite.deserialize(byteBuffer); + + return composite; + + } + +} From c73ec33c62af163fd1d9c2491b9ca29b79279269 Mon Sep 17 00:00:00 2001 From: Ed Anuff Date: Tue, 22 Mar 2011 14:24:43 -0700 Subject: [PATCH 026/144] added deserialization to DynamicComposite bean --- .../hector/api/beans/DynamicComposite.java | 70 +++++++++++++++++-- 1 file changed, 65 insertions(+), 5 deletions(-) diff --git a/core/src/main/java/me/prettyprint/hector/api/beans/DynamicComposite.java b/core/src/main/java/me/prettyprint/hector/api/beans/DynamicComposite.java index 0bfde634d..309ce84e7 100644 --- a/core/src/main/java/me/prettyprint/hector/api/beans/DynamicComposite.java +++ b/core/src/main/java/me/prettyprint/hector/api/beans/DynamicComposite.java @@ -1,6 +1,7 @@ package me.prettyprint.hector.api.beans; import java.nio.ByteBuffer; +import java.nio.charset.CharacterCodingException; import java.util.AbstractList; import java.util.ArrayList; import java.util.HashMap; @@ -209,6 +210,15 @@ private String comparerForSerializer(Serializer s) { return "BytesType"; } + @SuppressWarnings("unused") + private Serializer serializerForComparer(String c) { + Serializer s = comparerToSerializerMapping.get(c); + if (s != null) { + return s; + } + return ByteBufferSerializer.get(); + } + public DynamicComposite add(T value, Serializer s) { serialized = null; @@ -293,7 +303,7 @@ public Object get(int i) { @SuppressWarnings("unchecked") public ByteBuffer serialize() { if (serialized != null) { - return serialized; + return serialized.duplicate(); } ByteBufferOutputStream out = new ByteBufferOutputStream(); @@ -308,20 +318,70 @@ public ByteBuffer serialize() { out.writeShort((short) c.getComparer().length()); out.write(ByteBufferUtil.bytes(c.getComparer())); } - out.write(cb.remaining()); + out.writeShort((short) cb.remaining()); out.write(cb.slice()); out.write(c.isInclusive() ? 1 : 0); } serialized = out.getByteBuffer(); - return serialized; + return serialized.duplicate(); } + @SuppressWarnings("unchecked") public void deserialize(ByteBuffer b) { - serialized = b; + serialized = b.duplicate(); components = new ArrayList(); - // TODO deserialize + String comparer = null; + while ((comparer = getComparator(b)) != null) { + ByteBuffer data = getWithShortLength(b); + if (data != null) { + Serializer s = serializerForComparer(comparer); + Object value = s.fromByteBuffer(data); + boolean inclusive = b.get() != 0; + components.add(new Component(value, s, comparer, inclusive)); + } else { + throw new RuntimeException("Missing component data in composite type"); + } + } + + } + + protected static int getShortLength(ByteBuffer bb) { + int length = (bb.get() & 0xFF) << 8; + return length | (bb.get() & 0xFF); + } + + protected static ByteBuffer getBytes(ByteBuffer bb, int length) { + ByteBuffer copy = bb.duplicate(); + copy.limit(copy.position() + length); + bb.position(bb.position() + length); + return copy; + } + + protected static ByteBuffer getWithShortLength(ByteBuffer bb) { + int length = getShortLength(bb); + return getBytes(bb, length); + } + + private String getComparator(ByteBuffer bb) { + String name = null; + if (bb.hasRemaining()) { + try { + int header = getShortLength(bb); + if ((header & 0x8000) == 0) { + name = ByteBufferUtil.string(getBytes(bb, header)); + } else { + name = aliasesToComparerMapping.get((byte) (header & 0xFF)); + } + } catch (CharacterCodingException e) { + throw new RuntimeException(e); + } + } + if ((name != null) && (name.length() == 0)) { + name = null; + } + return name; } } From eac3c8f1a40acb41870bacadf6f8775c9b36ae12 Mon Sep 17 00:00:00 2001 From: Ed Anuff Date: Tue, 22 Mar 2011 14:29:06 -0700 Subject: [PATCH 027/144] Fixed comparer to serializer mapping --- .../hector/api/beans/DynamicComposite.java | 20 +++++++++---------- 1 file changed, 9 insertions(+), 11 deletions(-) diff --git a/core/src/main/java/me/prettyprint/hector/api/beans/DynamicComposite.java b/core/src/main/java/me/prettyprint/hector/api/beans/DynamicComposite.java index 309ce84e7..4a0371efa 100644 --- a/core/src/main/java/me/prettyprint/hector/api/beans/DynamicComposite.java +++ b/core/src/main/java/me/prettyprint/hector/api/beans/DynamicComposite.java @@ -59,19 +59,19 @@ public class DynamicComposite extends AbstractList implements static { DEFAULT_COMPARER_TO_SERIALIZER_MAPPING.put("AsciiType", - ByteBufferSerializer.get()); + AsciiSerializer.get()); DEFAULT_COMPARER_TO_SERIALIZER_MAPPING.put("BytesType", ByteBufferSerializer.get()); DEFAULT_COMPARER_TO_SERIALIZER_MAPPING.put("IntegerType", - ByteBufferSerializer.get()); + BigIntegerSerializer.get()); DEFAULT_COMPARER_TO_SERIALIZER_MAPPING.put("LexicalUUIDType", - ByteBufferSerializer.get()); - DEFAULT_COMPARER_TO_SERIALIZER_MAPPING.put("LongType", - ByteBufferSerializer.get()); + UUIDSerializer.get()); + DEFAULT_COMPARER_TO_SERIALIZER_MAPPING + .put("LongType", LongSerializer.get()); DEFAULT_COMPARER_TO_SERIALIZER_MAPPING.put("TimeUUIDType", - ByteBufferSerializer.get()); + UUIDSerializer.get()); DEFAULT_COMPARER_TO_SERIALIZER_MAPPING.put("UTF8Type", - ByteBufferSerializer.get()); + StringSerializer.get()); } @@ -197,9 +197,8 @@ public void setComparerToAliasMapping(Map comparerToAliasMapping) } @Override - public int compareTo(DynamicComposite arg0) { - // TODO Auto-generated method stub - return 0; + public int compareTo(DynamicComposite o) { + return serialize().compareTo(o.serialize()); } private String comparerForSerializer(Serializer s) { @@ -210,7 +209,6 @@ private String comparerForSerializer(Serializer s) { return "BytesType"; } - @SuppressWarnings("unused") private Serializer serializerForComparer(String c) { Serializer s = comparerToSerializerMapping.get(c); if (s != null) { From a772f77ca51c33638f4ac12b886e013233d559e4 Mon Sep 17 00:00:00 2001 From: Ed Anuff Date: Tue, 22 Mar 2011 14:31:07 -0700 Subject: [PATCH 028/144] moved dynamic composite serializer into serializers package --- .../serializers}/DynamicCompositeSerialzier.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) rename core/src/main/java/me/prettyprint/{hector/api/beans => cassandra/serializers}/DynamicCompositeSerialzier.java (81%) diff --git a/core/src/main/java/me/prettyprint/hector/api/beans/DynamicCompositeSerialzier.java b/core/src/main/java/me/prettyprint/cassandra/serializers/DynamicCompositeSerialzier.java similarity index 81% rename from core/src/main/java/me/prettyprint/hector/api/beans/DynamicCompositeSerialzier.java rename to core/src/main/java/me/prettyprint/cassandra/serializers/DynamicCompositeSerialzier.java index 634592c38..62cb81610 100644 --- a/core/src/main/java/me/prettyprint/hector/api/beans/DynamicCompositeSerialzier.java +++ b/core/src/main/java/me/prettyprint/cassandra/serializers/DynamicCompositeSerialzier.java @@ -1,11 +1,11 @@ /** * */ -package me.prettyprint.hector.api.beans; +package me.prettyprint.cassandra.serializers; import java.nio.ByteBuffer; -import me.prettyprint.cassandra.serializers.AbstractSerializer; +import me.prettyprint.hector.api.beans.DynamicComposite; /** * @author Todd Nine From 15dbc4a948d60b9403252e8c6c64b283f5a86a38 Mon Sep 17 00:00:00 2001 From: Ed Anuff Date: Tue, 22 Mar 2011 15:02:42 -0700 Subject: [PATCH 029/144] Fixes on DynamicComposite --- .../utils/ByteBufferOutputStream.java | 9 ++--- .../hector/api/beans/DynamicComposite.java | 2 +- .../hector/api/DynamicCompositeTest.java | 35 +++++++++++++++++++ 3 files changed, 38 insertions(+), 8 deletions(-) create mode 100644 core/src/test/java/me/prettyprint/hector/api/DynamicCompositeTest.java diff --git a/core/src/main/java/me/prettyprint/cassandra/utils/ByteBufferOutputStream.java b/core/src/main/java/me/prettyprint/cassandra/utils/ByteBufferOutputStream.java index 8e847a732..06a144e73 100644 --- a/core/src/main/java/me/prettyprint/cassandra/utils/ByteBufferOutputStream.java +++ b/core/src/main/java/me/prettyprint/cassandra/utils/ByteBufferOutputStream.java @@ -18,7 +18,6 @@ * limitations under the License. */ -import java.io.IOException; import java.io.OutputStream; import java.nio.ByteBuffer; import java.util.LinkedList; @@ -64,7 +63,7 @@ public ByteBuffer getByteBuffer() { for (ByteBuffer buffer : list) { result.put(buffer); } - return result; + return (ByteBuffer) result.rewind(); } /** Prepend a list of ByteBuffers to this stream. */ @@ -88,10 +87,6 @@ public void reset() { buffers.add(ByteBuffer.allocate(BUFFER_SIZE)); } - public void write(ByteBuffer buffer) { - buffers.add(buffer); - } - private ByteBuffer getBufferWithCapacity(int capacity) { ByteBuffer buffer = buffers.get(buffers.size() - 1); if (buffer.remaining() < capacity) { @@ -153,7 +148,7 @@ public void write(byte[] b, int off, int len) { } /** Add a buffer to the output without copying, if possible. */ - public void writeBuffer(ByteBuffer buffer) throws IOException { + public void write(ByteBuffer buffer) { if (buffer.remaining() < BUFFER_SIZE) { write(buffer.array(), buffer.position(), buffer.remaining()); } else { // append w/o copying bytes diff --git a/core/src/main/java/me/prettyprint/hector/api/beans/DynamicComposite.java b/core/src/main/java/me/prettyprint/hector/api/beans/DynamicComposite.java index 4a0371efa..9ce5407bc 100644 --- a/core/src/main/java/me/prettyprint/hector/api/beans/DynamicComposite.java +++ b/core/src/main/java/me/prettyprint/hector/api/beans/DynamicComposite.java @@ -202,7 +202,7 @@ public int compareTo(DynamicComposite o) { } private String comparerForSerializer(Serializer s) { - String comparer = serializerToComparerMapping.get(s); + String comparer = serializerToComparerMapping.get(s.getClass()); if (comparer != null) { return comparer; } diff --git a/core/src/test/java/me/prettyprint/hector/api/DynamicCompositeTest.java b/core/src/test/java/me/prettyprint/hector/api/DynamicCompositeTest.java new file mode 100644 index 000000000..0808eb5a7 --- /dev/null +++ b/core/src/test/java/me/prettyprint/hector/api/DynamicCompositeTest.java @@ -0,0 +1,35 @@ +package me.prettyprint.hector.api; + +import static org.junit.Assert.assertEquals; + +import java.nio.ByteBuffer; + +import me.prettyprint.hector.api.beans.DynamicComposite; + +import org.junit.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class DynamicCompositeTest { + + private static final Logger log = LoggerFactory + .getLogger(ClockResolutionTest.class); + + @Test + public void testSerialization() throws Exception { + + DynamicComposite c = new DynamicComposite(); + c.add("String1"); + ByteBuffer b = c.serialize(); + assertEquals(b.remaining(), 12); + + c.add("String2"); + b = c.serialize(); + assertEquals(b.remaining(), 24); + + c = new DynamicComposite(); + c.deserialize(b); + Object o = c.get(0); + assertEquals("String1", o); + } +} From c846609d8ad8f29bffe79aeb75a900571cbcbdc4 Mon Sep 17 00:00:00 2001 From: Ed Anuff Date: Tue, 22 Mar 2011 15:13:42 -0700 Subject: [PATCH 030/144] Unit tests for DynamicComposite --- .../hector/api/DynamicCompositeTest.java | 72 +++++++++++++++++++ 1 file changed, 72 insertions(+) diff --git a/core/src/test/java/me/prettyprint/hector/api/DynamicCompositeTest.java b/core/src/test/java/me/prettyprint/hector/api/DynamicCompositeTest.java index 0808eb5a7..7be6b587c 100644 --- a/core/src/test/java/me/prettyprint/hector/api/DynamicCompositeTest.java +++ b/core/src/test/java/me/prettyprint/hector/api/DynamicCompositeTest.java @@ -1,11 +1,16 @@ package me.prettyprint.hector.api; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; +import java.math.BigInteger; import java.nio.ByteBuffer; +import java.util.UUID; import me.prettyprint.hector.api.beans.DynamicComposite; +import org.apache.cassandra.utils.ByteBufferUtil; +import org.apache.cassandra.utils.UUIDGen; import org.junit.Test; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -29,7 +34,74 @@ public void testSerialization() throws Exception { c = new DynamicComposite(); c.deserialize(b); + assertEquals(2, c.size()); Object o = c.get(0); assertEquals("String1", o); + o = c.get(1); + assertEquals("String2", o); + + c = new DynamicComposite(); + c.add(new Long(10)); + b = c.serialize(); + c = new DynamicComposite(); + c.deserialize(b); + o = c.get(0); + assertTrue(o instanceof Long); + + b = createDynamicCompositeKey("Hello", UUID.randomUUID(), 10, false); + c = new DynamicComposite(); + c.deserialize(b); + o = c.get(0); + assertTrue(o instanceof ByteBuffer); + o = c.get(1); + assertEquals(UUID.class, o.getClass()); + o = c.get(2); + assertEquals(BigInteger.class, o.getClass()); + assertEquals(BigInteger.valueOf(10), o); + } + + // from the Casssandra DynamicCompositeType unit test + private ByteBuffer createDynamicCompositeKey(String s, UUID uuid, int i, + boolean lastIsOne) { + ByteBuffer bytes = ByteBufferUtil.bytes(s); + int totalSize = 0; + if (s != null) { + totalSize += 2 + 2 + bytes.remaining() + 1; + if (uuid != null) { + totalSize += 2 + 2 + 16 + 1; + if (i != -1) { + totalSize += 2 + "IntegerType".length() + 2 + 1 + 1; + } + } + } + + ByteBuffer bb = ByteBuffer.allocate(totalSize); + + if (s != null) { + bb.putShort((short) (0x8000 | 'b')); + bb.putShort((short) bytes.remaining()); + bb.put(bytes); + bb.put((uuid == null) && lastIsOne ? (byte) 1 : (byte) 0); + if (uuid != null) { + bb.putShort((short) (0x8000 | 't')); + bb.putShort((short) 16); + bb.put(UUIDGen.decompose(uuid)); + bb.put((i == -1) && lastIsOne ? (byte) 1 : (byte) 0); + if (i != -1) { + bb.putShort((short) "IntegerType".length()); + bb.put(ByteBufferUtil.bytes("IntegerType")); + // We are putting a byte only because our test use ints that + // fit in a byte *and* IntegerType.fromString() will + // return something compatible (i.e, putting a full int here + // would break 'fromStringTest') + bb.putShort((short) 1); + bb.put((byte) i); + bb.put(lastIsOne ? (byte) 1 : (byte) 0); + } + } + } + bb.rewind(); + return bb; } + } From 493606e7adaa93823a6aa776452b604b38522b28 Mon Sep 17 00:00:00 2001 From: Ed Anuff Date: Tue, 22 Mar 2011 15:17:06 -0700 Subject: [PATCH 031/144] Unit test for DynamicComposite bean --- .../java/me/prettyprint/hector/api/DynamicCompositeTest.java | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/core/src/test/java/me/prettyprint/hector/api/DynamicCompositeTest.java b/core/src/test/java/me/prettyprint/hector/api/DynamicCompositeTest.java index 7be6b587c..a66872f93 100644 --- a/core/src/test/java/me/prettyprint/hector/api/DynamicCompositeTest.java +++ b/core/src/test/java/me/prettyprint/hector/api/DynamicCompositeTest.java @@ -7,6 +7,7 @@ import java.nio.ByteBuffer; import java.util.UUID; +import me.prettyprint.cassandra.utils.TimeUUIDUtils; import me.prettyprint.hector.api.beans.DynamicComposite; import org.apache.cassandra.utils.ByteBufferUtil; @@ -48,7 +49,8 @@ public void testSerialization() throws Exception { o = c.get(0); assertTrue(o instanceof Long); - b = createDynamicCompositeKey("Hello", UUID.randomUUID(), 10, false); + b = createDynamicCompositeKey("Hello", + TimeUUIDUtils.getUniqueTimeUUIDinMillis(), 10, false); c = new DynamicComposite(); c.deserialize(b); o = c.get(0); From 7dcdb29625d80bb48acebeb219da6ab540401f42 Mon Sep 17 00:00:00 2001 From: Ed Anuff Date: Tue, 22 Mar 2011 15:40:46 -0700 Subject: [PATCH 032/144] Added utility method to DynamicComposite for specifying serializer when getting ByteBuffer component --- .../me/prettyprint/hector/api/beans/DynamicComposite.java | 8 ++++++++ .../me/prettyprint/hector/api/DynamicCompositeTest.java | 5 +++++ 2 files changed, 13 insertions(+) diff --git a/core/src/main/java/me/prettyprint/hector/api/beans/DynamicComposite.java b/core/src/main/java/me/prettyprint/hector/api/beans/DynamicComposite.java index 9ce5407bc..4509fa676 100644 --- a/core/src/main/java/me/prettyprint/hector/api/beans/DynamicComposite.java +++ b/core/src/main/java/me/prettyprint/hector/api/beans/DynamicComposite.java @@ -298,6 +298,14 @@ public Object get(int i) { return null; } + public T get(int i, Serializer s) throws ClassCastException { + Component c = components.get(i); + if (c != null) { + return s.fromByteBuffer((ByteBuffer) c.getValue()); + } + return null; + } + @SuppressWarnings("unchecked") public ByteBuffer serialize() { if (serialized != null) { diff --git a/core/src/test/java/me/prettyprint/hector/api/DynamicCompositeTest.java b/core/src/test/java/me/prettyprint/hector/api/DynamicCompositeTest.java index a66872f93..5ab11d559 100644 --- a/core/src/test/java/me/prettyprint/hector/api/DynamicCompositeTest.java +++ b/core/src/test/java/me/prettyprint/hector/api/DynamicCompositeTest.java @@ -7,6 +7,7 @@ import java.nio.ByteBuffer; import java.util.UUID; +import me.prettyprint.cassandra.serializers.StringSerializer; import me.prettyprint.cassandra.utils.TimeUUIDUtils; import me.prettyprint.hector.api.beans.DynamicComposite; @@ -55,11 +56,15 @@ public void testSerialization() throws Exception { c.deserialize(b); o = c.get(0); assertTrue(o instanceof ByteBuffer); + assertEquals("Hello", c.get(0, StringSerializer.get())); + o = c.get(1); assertEquals(UUID.class, o.getClass()); + o = c.get(2); assertEquals(BigInteger.class, o.getClass()); assertEquals(BigInteger.valueOf(10), o); + } // from the Casssandra DynamicCompositeType unit test From b098390b6e4dd3d09767ef9803faeb2f09d3023b Mon Sep 17 00:00:00 2001 From: Ed Anuff Date: Wed, 23 Mar 2011 07:58:59 -0700 Subject: [PATCH 033/144] Added options to override auto selection of serializers --- .../hector/api/beans/DynamicComposite.java | 66 ++++++++++++++++++- .../hector/api/DynamicCompositeTest.java | 15 ++++- 2 files changed, 78 insertions(+), 3 deletions(-) diff --git a/core/src/main/java/me/prettyprint/hector/api/beans/DynamicComposite.java b/core/src/main/java/me/prettyprint/hector/api/beans/DynamicComposite.java index 4509fa676..e11ab7368 100644 --- a/core/src/main/java/me/prettyprint/hector/api/beans/DynamicComposite.java +++ b/core/src/main/java/me/prettyprint/hector/api/beans/DynamicComposite.java @@ -4,7 +4,9 @@ import java.nio.charset.CharacterCodingException; import java.util.AbstractList; import java.util.ArrayList; +import java.util.Arrays; import java.util.HashMap; +import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.logging.Logger; @@ -109,6 +111,10 @@ public class DynamicComposite extends AbstractList implements Map comparerToAliasMapping = DEFAULT_COMPARER_TO_ALIAS_MAPPING; + boolean autoDeserialize = true; + + List> serializersByPosition = null; + public class Component { final Serializer serializer; final T value; @@ -196,6 +202,26 @@ public void setComparerToAliasMapping(Map comparerToAliasMapping) this.comparerToAliasMapping = comparerToAliasMapping; } + public boolean isAutoDeserialize() { + return autoDeserialize; + } + + public void setAutoDeserialize(boolean autoDeserialize) { + this.autoDeserialize = autoDeserialize; + } + + public List> getSerializersByPosition() { + return serializersByPosition; + } + + public void setSerializersByPosition(List> serializersByPosition) { + this.serializersByPosition = serializersByPosition; + } + + public void setSerializersByPosition(Serializer... serializers) { + serializersByPosition = Arrays.asList(serializers); + } + @Override public int compareTo(DynamicComposite o) { return serialize().compareTo(o.serialize()); @@ -217,6 +243,24 @@ private Serializer serializerForComparer(String c) { return ByteBufferSerializer.get(); } + private Serializer serializerForPosition(int i) { + if (serializersByPosition == null) { + return null; + } + if (i >= serializersByPosition.size()) { + return null; + } + return serializersByPosition.get(i); + } + + private Serializer getSerializer(int i, String c) { + Serializer s = serializerForPosition(i); + if (s != null) { + return s; + } + return serializerForComparer(c); + } + public DynamicComposite add(T value, Serializer s) { serialized = null; @@ -261,7 +305,10 @@ public int size() { @Override public void add(int index, Object element) { serialized = null; - Serializer s = SerializerTypeInferer.getSerializer(element); + Serializer s = serializerForPosition(index); + if (s == null) { + s = SerializerTypeInferer.getSerializer(element); + } components.add(index, new Component(element, s, comparerForSerializer(s), false)); } @@ -306,6 +353,18 @@ public T get(int i, Serializer s) throws ClassCastException { return null; } + public Component getComponent(int i) { + if (i >= components.size()) { + return null; + } + Component c = components.get(i); + return c; + } + + public Iterator componentsIterator() { + return components.iterator(); + } + @SuppressWarnings("unchecked") public ByteBuffer serialize() { if (serialized != null) { @@ -339,16 +398,19 @@ public void deserialize(ByteBuffer b) { components = new ArrayList(); String comparer = null; + int i = 0; while ((comparer = getComparator(b)) != null) { ByteBuffer data = getWithShortLength(b); if (data != null) { - Serializer s = serializerForComparer(comparer); + Serializer s = autoDeserialize ? getSerializer(i, comparer) + : ByteBufferSerializer.get(); Object value = s.fromByteBuffer(data); boolean inclusive = b.get() != 0; components.add(new Component(value, s, comparer, inclusive)); } else { throw new RuntimeException("Missing component data in composite type"); } + i++; } } diff --git a/core/src/test/java/me/prettyprint/hector/api/DynamicCompositeTest.java b/core/src/test/java/me/prettyprint/hector/api/DynamicCompositeTest.java index 5ab11d559..cdc633833 100644 --- a/core/src/test/java/me/prettyprint/hector/api/DynamicCompositeTest.java +++ b/core/src/test/java/me/prettyprint/hector/api/DynamicCompositeTest.java @@ -53,7 +53,7 @@ public void testSerialization() throws Exception { b = createDynamicCompositeKey("Hello", TimeUUIDUtils.getUniqueTimeUUIDinMillis(), 10, false); c = new DynamicComposite(); - c.deserialize(b); + c.deserialize(b.slice()); o = c.get(0); assertTrue(o instanceof ByteBuffer); assertEquals("Hello", c.get(0, StringSerializer.get())); @@ -65,6 +65,19 @@ public void testSerialization() throws Exception { assertEquals(BigInteger.class, o.getClass()); assertEquals(BigInteger.valueOf(10), o); + c = new DynamicComposite(); + c.setAutoDeserialize(false); + c.deserialize(b.slice()); + assertTrue(c.get(0) instanceof ByteBuffer); + assertTrue(c.get(1) instanceof ByteBuffer); + assertTrue(c.get(2) instanceof ByteBuffer); + + c = new DynamicComposite(); + c.setSerializersByPosition(StringSerializer.get()); + c.deserialize(b.slice()); + assertTrue(c.get(0) instanceof String); + assertTrue(c.get(1) instanceof UUID); + assertTrue(c.get(2) instanceof BigInteger); } // from the Casssandra DynamicCompositeType unit test From 07d53ea47923d7d9ac6709e28a10501263311417 Mon Sep 17 00:00:00 2001 From: Ed Anuff Date: Wed, 23 Mar 2011 08:43:14 -0700 Subject: [PATCH 034/144] modified DynamicCompositeTest --- .../me/prettyprint/hector/api/DynamicCompositeTest.java | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/core/src/test/java/me/prettyprint/hector/api/DynamicCompositeTest.java b/core/src/test/java/me/prettyprint/hector/api/DynamicCompositeTest.java index cdc633833..deba1ab85 100644 --- a/core/src/test/java/me/prettyprint/hector/api/DynamicCompositeTest.java +++ b/core/src/test/java/me/prettyprint/hector/api/DynamicCompositeTest.java @@ -7,6 +7,7 @@ import java.nio.ByteBuffer; import java.util.UUID; +import me.prettyprint.cassandra.serializers.ByteBufferSerializer; import me.prettyprint.cassandra.serializers.StringSerializer; import me.prettyprint.cassandra.utils.TimeUUIDUtils; import me.prettyprint.hector.api.beans.DynamicComposite; @@ -73,11 +74,12 @@ public void testSerialization() throws Exception { assertTrue(c.get(2) instanceof ByteBuffer); c = new DynamicComposite(); - c.setSerializersByPosition(StringSerializer.get()); + c.setSerializersByPosition(StringSerializer.get(), null, + ByteBufferSerializer.get()); c.deserialize(b.slice()); assertTrue(c.get(0) instanceof String); assertTrue(c.get(1) instanceof UUID); - assertTrue(c.get(2) instanceof BigInteger); + assertTrue(c.get(2) instanceof ByteBuffer); } // from the Casssandra DynamicCompositeType unit test From 65bdd1f804e8c64b91957058f85bbc8ee8d6b458 Mon Sep 17 00:00:00 2001 From: Ed Anuff Date: Wed, 23 Mar 2011 11:38:13 -0700 Subject: [PATCH 035/144] Modified DynamicComposite to handle non-dynamic composites as well Renamed DynamicComposite to Composite Fixed typo in DynamicCompositeSerializer Added changed to ComparatorType to let it reused by Composite --- .../DynamicCompositeSerializer.java | 32 ++ .../DynamicCompositeSerialzier.java | 32 -- .../hector/api/beans/Composite.java | 530 ++++++++++++++++++ .../hector/api/beans/DynamicComposite.java | 455 --------------- .../hector/api/ddl/ComparatorType.java | 42 +- .../hector/api/DynamicCompositeTest.java | 16 +- 6 files changed, 602 insertions(+), 505 deletions(-) create mode 100644 core/src/main/java/me/prettyprint/cassandra/serializers/DynamicCompositeSerializer.java delete mode 100644 core/src/main/java/me/prettyprint/cassandra/serializers/DynamicCompositeSerialzier.java create mode 100644 core/src/main/java/me/prettyprint/hector/api/beans/Composite.java delete mode 100644 core/src/main/java/me/prettyprint/hector/api/beans/DynamicComposite.java diff --git a/core/src/main/java/me/prettyprint/cassandra/serializers/DynamicCompositeSerializer.java b/core/src/main/java/me/prettyprint/cassandra/serializers/DynamicCompositeSerializer.java new file mode 100644 index 000000000..826b04c96 --- /dev/null +++ b/core/src/main/java/me/prettyprint/cassandra/serializers/DynamicCompositeSerializer.java @@ -0,0 +1,32 @@ +/** + * + */ +package me.prettyprint.cassandra.serializers; + +import java.nio.ByteBuffer; + +import me.prettyprint.hector.api.beans.Composite; + +/** + * @author Todd Nine + * + */ +public class DynamicCompositeSerializer extends AbstractSerializer{ + + @Override + public ByteBuffer toByteBuffer(Composite obj) { + + return obj.serialize(); + } + + @Override + public Composite fromByteBuffer(ByteBuffer byteBuffer) { + + Composite composite = new Composite(); + composite.deserialize(byteBuffer); + + return composite; + + } + +} diff --git a/core/src/main/java/me/prettyprint/cassandra/serializers/DynamicCompositeSerialzier.java b/core/src/main/java/me/prettyprint/cassandra/serializers/DynamicCompositeSerialzier.java deleted file mode 100644 index 62cb81610..000000000 --- a/core/src/main/java/me/prettyprint/cassandra/serializers/DynamicCompositeSerialzier.java +++ /dev/null @@ -1,32 +0,0 @@ -/** - * - */ -package me.prettyprint.cassandra.serializers; - -import java.nio.ByteBuffer; - -import me.prettyprint.hector.api.beans.DynamicComposite; - -/** - * @author Todd Nine - * - */ -public class DynamicCompositeSerialzier extends AbstractSerializer{ - - @Override - public ByteBuffer toByteBuffer(DynamicComposite obj) { - - return obj.serialize(); - } - - @Override - public DynamicComposite fromByteBuffer(ByteBuffer byteBuffer) { - - DynamicComposite composite = new DynamicComposite(); - composite.deserialize(byteBuffer); - - return composite; - - } - -} diff --git a/core/src/main/java/me/prettyprint/hector/api/beans/Composite.java b/core/src/main/java/me/prettyprint/hector/api/beans/Composite.java new file mode 100644 index 000000000..399582dc8 --- /dev/null +++ b/core/src/main/java/me/prettyprint/hector/api/beans/Composite.java @@ -0,0 +1,530 @@ +package me.prettyprint.hector.api.beans; + +import static me.prettyprint.hector.api.ddl.ComparatorType.ASCIITYPE; +import static me.prettyprint.hector.api.ddl.ComparatorType.BYTESTYPE; +import static me.prettyprint.hector.api.ddl.ComparatorType.INTEGERTYPE; +import static me.prettyprint.hector.api.ddl.ComparatorType.LEXICALUUIDTYPE; +import static me.prettyprint.hector.api.ddl.ComparatorType.LONGTYPE; +import static me.prettyprint.hector.api.ddl.ComparatorType.TIMEUUIDTYPE; +import static me.prettyprint.hector.api.ddl.ComparatorType.UTF8TYPE; + +import java.nio.ByteBuffer; +import java.nio.charset.CharacterCodingException; +import java.util.AbstractList; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.HashMap; +import java.util.Iterator; +import java.util.List; +import java.util.Map; +import java.util.logging.Logger; + +import me.prettyprint.cassandra.serializers.AsciiSerializer; +import me.prettyprint.cassandra.serializers.BigIntegerSerializer; +import me.prettyprint.cassandra.serializers.BooleanSerializer; +import me.prettyprint.cassandra.serializers.ByteBufferSerializer; +import me.prettyprint.cassandra.serializers.BytesArraySerializer; +import me.prettyprint.cassandra.serializers.LongSerializer; +import me.prettyprint.cassandra.serializers.SerializerTypeInferer; +import me.prettyprint.cassandra.serializers.ShortSerializer; +import me.prettyprint.cassandra.serializers.StringSerializer; +import me.prettyprint.cassandra.serializers.UUIDSerializer; +import me.prettyprint.cassandra.utils.ByteBufferOutputStream; +import me.prettyprint.hector.api.Serializer; + +import org.apache.cassandra.utils.ByteBufferUtil; + +@SuppressWarnings("rawtypes") +public class Composite extends AbstractList implements + Comparable { + + static final Logger logger = Logger.getLogger(Composite.class + .getName()); + + public static final Map, String> DEFAULT_SERIALIZER_TO_COMPARATOR_MAPPING; + + static { + DEFAULT_SERIALIZER_TO_COMPARATOR_MAPPING = new HashMap, String>(); + DEFAULT_SERIALIZER_TO_COMPARATOR_MAPPING.put(AsciiSerializer.class, + ASCIITYPE.getTypeName()); + DEFAULT_SERIALIZER_TO_COMPARATOR_MAPPING.put(BigIntegerSerializer.class, + INTEGERTYPE.getTypeName()); + DEFAULT_SERIALIZER_TO_COMPARATOR_MAPPING.put(BooleanSerializer.class, + BYTESTYPE.getTypeName()); + DEFAULT_SERIALIZER_TO_COMPARATOR_MAPPING.put(ByteBufferSerializer.class, + BYTESTYPE.getTypeName()); + DEFAULT_SERIALIZER_TO_COMPARATOR_MAPPING.put(BytesArraySerializer.class, + BYTESTYPE.getTypeName()); + DEFAULT_SERIALIZER_TO_COMPARATOR_MAPPING.put(LongSerializer.class, + LONGTYPE.getTypeName()); + DEFAULT_SERIALIZER_TO_COMPARATOR_MAPPING.put(ShortSerializer.class, + BYTESTYPE.getTypeName()); + DEFAULT_SERIALIZER_TO_COMPARATOR_MAPPING.put(StringSerializer.class, + UTF8TYPE.getTypeName()); + // TODO this doesn't work, there isn't a unified UUIDType yet + DEFAULT_SERIALIZER_TO_COMPARATOR_MAPPING.put(UUIDSerializer.class, + "UUIDType"); + } + + public static final Map DEFAULT_COMPARATOR_TO_SERIALIZER_MAPPING = new HashMap(); + + static { + DEFAULT_COMPARATOR_TO_SERIALIZER_MAPPING.put(ASCIITYPE.getTypeName(), + AsciiSerializer.get()); + DEFAULT_COMPARATOR_TO_SERIALIZER_MAPPING.put(BYTESTYPE.getTypeName(), + ByteBufferSerializer.get()); + DEFAULT_COMPARATOR_TO_SERIALIZER_MAPPING.put(INTEGERTYPE.getTypeName(), + BigIntegerSerializer.get()); + DEFAULT_COMPARATOR_TO_SERIALIZER_MAPPING.put(LEXICALUUIDTYPE.getTypeName(), + UUIDSerializer.get()); + DEFAULT_COMPARATOR_TO_SERIALIZER_MAPPING.put(LONGTYPE.getTypeName(), + LongSerializer.get()); + DEFAULT_COMPARATOR_TO_SERIALIZER_MAPPING.put(TIMEUUIDTYPE.getTypeName(), + UUIDSerializer.get()); + DEFAULT_COMPARATOR_TO_SERIALIZER_MAPPING.put(UTF8TYPE.getTypeName(), + StringSerializer.get()); + + } + + public static final Map DEFAULT_ALIAS_TO_COMPARATOR_MAPPING = new HashMap(); + + static { + DEFAULT_ALIAS_TO_COMPARATOR_MAPPING + .put((byte) 'a', ASCIITYPE.getTypeName()); + DEFAULT_ALIAS_TO_COMPARATOR_MAPPING + .put((byte) 'b', BYTESTYPE.getTypeName()); + DEFAULT_ALIAS_TO_COMPARATOR_MAPPING.put((byte) 'i', + INTEGERTYPE.getTypeName()); + DEFAULT_ALIAS_TO_COMPARATOR_MAPPING.put((byte) 'x', + LEXICALUUIDTYPE.getTypeName()); + DEFAULT_ALIAS_TO_COMPARATOR_MAPPING.put((byte) 'l', LONGTYPE.getTypeName()); + DEFAULT_ALIAS_TO_COMPARATOR_MAPPING.put((byte) 't', + TIMEUUIDTYPE.getTypeName()); + DEFAULT_ALIAS_TO_COMPARATOR_MAPPING.put((byte) 's', UTF8TYPE.getTypeName()); + + } + + public static final Map DEFAULT_COMPARATOR_TO_ALIAS_MAPPING = new HashMap(); + + static { + DEFAULT_COMPARATOR_TO_ALIAS_MAPPING + .put(ASCIITYPE.getTypeName(), (byte) 'a'); + DEFAULT_COMPARATOR_TO_ALIAS_MAPPING + .put(BYTESTYPE.getTypeName(), (byte) 'b'); + DEFAULT_COMPARATOR_TO_ALIAS_MAPPING.put(INTEGERTYPE.getTypeName(), + (byte) 'i'); + DEFAULT_COMPARATOR_TO_ALIAS_MAPPING.put(LEXICALUUIDTYPE.getTypeName(), + (byte) 'x'); + DEFAULT_COMPARATOR_TO_ALIAS_MAPPING.put(LONGTYPE.getTypeName(), (byte) 'l'); + DEFAULT_COMPARATOR_TO_ALIAS_MAPPING.put(TIMEUUIDTYPE.getTypeName(), + (byte) 't'); + DEFAULT_COMPARATOR_TO_ALIAS_MAPPING.put(UTF8TYPE.getTypeName(), (byte) 's'); + + } + + Map, String> serializerToComparatorMapping = DEFAULT_SERIALIZER_TO_COMPARATOR_MAPPING; + + Map comparatorToSerializerMapping = DEFAULT_COMPARATOR_TO_SERIALIZER_MAPPING; + + Map aliasesToComparatorMapping = DEFAULT_ALIAS_TO_COMPARATOR_MAPPING; + + Map comparatorToAliasMapping = DEFAULT_COMPARATOR_TO_ALIAS_MAPPING; + + boolean autoDeserialize = true; + + boolean dynamic = true; + + List> serializersByPosition = null; + List comparatorsByPosition = null; + + public class Component { + final Serializer serializer; + final T value; + final String comparator; + final boolean inclusive; + + public Component(T value, Serializer serializer, String comparator, + boolean inclusive) { + this.serializer = serializer; + this.value = value; + this.comparator = comparator; + this.inclusive = inclusive; + } + + public Serializer getSerializer() { + return serializer; + } + + public T getValue() { + return value; + } + + public String getComparator() { + return comparator; + } + + public boolean isInclusive() { + return inclusive; + } + } + + List components = new ArrayList(); + + ByteBuffer serialized = null; + + public Composite() { + + } + + public Composite(Object... o) { + this.addAll(Arrays.asList(o)); + } + + public List getComponents() { + return components; + } + + public void setComponents(List components) { + serialized = null; + this.components = components; + } + + public Map, String> getSerializerToComparatorMapping() { + return serializerToComparatorMapping; + } + + public void setSerializerToComparatorMapping( + Map, String> serializerToComparatorMapping) { + serialized = null; + this.serializerToComparatorMapping = serializerToComparatorMapping; + } + + public Map getComparatorToSerializerMapping() { + return comparatorToSerializerMapping; + } + + public void setComparatorToSerializerMapping( + Map comparatorToSerializerMapping) { + serialized = null; + this.comparatorToSerializerMapping = comparatorToSerializerMapping; + } + + public Map getAliasesToComparatorMapping() { + return aliasesToComparatorMapping; + } + + public void setAliasesToComparatorMapping( + Map aliasesToComparatorMapping) { + serialized = null; + this.aliasesToComparatorMapping = aliasesToComparatorMapping; + } + + public Map getComparatorToAliasMapping() { + return comparatorToAliasMapping; + } + + public void setComparatorToAliasMapping( + Map comparatorToAliasMapping) { + serialized = null; + this.comparatorToAliasMapping = comparatorToAliasMapping; + } + + public boolean isAutoDeserialize() { + return autoDeserialize; + } + + public void setAutoDeserialize(boolean autoDeserialize) { + this.autoDeserialize = autoDeserialize; + } + + public boolean isDynamic() { + return dynamic; + } + + public void setDynamic(boolean dynamic) { + this.dynamic = dynamic; + } + + public List> getSerializersByPosition() { + return serializersByPosition; + } + + public void setSerializersByPosition(List> serializersByPosition) { + this.serializersByPosition = serializersByPosition; + } + + public void setSerializersByPosition(Serializer... serializers) { + serializersByPosition = Arrays.asList(serializers); + } + + public List getComparatorsByPosition() { + return comparatorsByPosition; + } + + public void setComparatorsByPosition(List comparatorsByPosition) { + this.comparatorsByPosition = comparatorsByPosition; + } + + public void setComparatorsByPosition(String... comparators) { + comparatorsByPosition = Arrays.asList(comparators); + } + + @Override + public int compareTo(Composite o) { + return serialize().compareTo(o.serialize()); + } + + private String comparatorForSerializer(Serializer s) { + String comparator = serializerToComparatorMapping.get(s.getClass()); + if (comparator != null) { + return comparator; + } + return BYTESTYPE.getTypeName(); + } + + private Serializer serializerForComparator(String c) { + Serializer s = comparatorToSerializerMapping.get(c); + if (s != null) { + return s; + } + return ByteBufferSerializer.get(); + } + + private Serializer serializerForPosition(int i) { + if (serializersByPosition == null) { + return null; + } + if (i >= serializersByPosition.size()) { + return null; + } + return serializersByPosition.get(i); + } + + private Serializer getSerializer(int i, String c) { + Serializer s = serializerForPosition(i); + if (s != null) { + return s; + } + return serializerForComparator(c); + } + + private String comparatorForPosition(int i) { + if (comparatorsByPosition == null) { + return null; + } + if (i >= comparatorsByPosition.size()) { + return null; + } + return comparatorsByPosition.get(i); + } + + public Composite add(T value, Serializer s) { + serialized = null; + + add(value, s, comparatorForSerializer(s)); + + return this; + + } + + public Composite add(T value, Serializer s, String comparator) { + serialized = null; + + add(value, s, comparator, false); + + return this; + + } + + @SuppressWarnings("unchecked") + public Composite add(T value, Serializer s, String comparator, + boolean inclusive) { + serialized = null; + + components.add(new Component(value, s, comparator, inclusive)); + + return this; + + } + + @Override + public void clear() { + serialized = null; + components = new ArrayList(); + } + + @Override + public int size() { + return components.size(); + } + + @SuppressWarnings("unchecked") + @Override + public void add(int index, Object element) { + serialized = null; + Serializer s = serializerForPosition(index); + if (s == null) { + s = SerializerTypeInferer.getSerializer(element); + } + components.add(index, new Component(element, s, comparatorForSerializer(s), + false)); + } + + @Override + public Object remove(int index) { + serialized = null; + Component prev = components.remove(index); + if (prev != null) { + return prev.getValue(); + } + return null; + } + + @SuppressWarnings("unchecked") + @Override + public Object set(int index, Object element) { + serialized = null; + Serializer s = SerializerTypeInferer.getSerializer(element); + Component prev = components.set(index, new Component(element, s, + comparatorForSerializer(s), false)); + if (prev != null) { + return prev.getValue(); + } + return null; + } + + @Override + public Object get(int i) { + Component c = components.get(i); + if (c != null) { + return c.getValue(); + } + return null; + } + + public T get(int i, Serializer s) throws ClassCastException { + Component c = components.get(i); + if (c != null) { + return s.fromByteBuffer((ByteBuffer) c.getValue()); + } + return null; + } + + public Component getComponent(int i) { + if (i >= components.size()) { + return null; + } + Component c = components.get(i); + return c; + } + + public Iterator componentsIterator() { + return components.iterator(); + } + + @SuppressWarnings("unchecked") + public ByteBuffer serialize() { + if (serialized != null) { + return serialized.duplicate(); + } + + ByteBufferOutputStream out = new ByteBufferOutputStream(); + + int i = 0; + for (Component c : components) { + Serializer s = serializerForPosition(i); + if (s == null) { + s = c.getSerializer(); + } + ByteBuffer cb = s.toByteBuffer(c.getValue()); + + if (dynamic) { + String comparator = comparatorForPosition(i); + if (comparator == null) { + comparator = c.getComparator(); + } + if (comparatorToAliasMapping.containsKey(comparator)) { + out.writeShort((short) (0x8000 | comparatorToAliasMapping + .get(comparator))); + } else { + out.writeShort((short) comparator.length()); + out.write(ByteBufferUtil.bytes(comparator)); + } + } + out.writeShort((short) cb.remaining()); + out.write(cb.slice()); + out.write(c.isInclusive() ? 1 : 0); + i++; + } + + serialized = out.getByteBuffer(); + return serialized.duplicate(); + } + + @SuppressWarnings("unchecked") + public void deserialize(ByteBuffer b) { + serialized = b.duplicate(); + components = new ArrayList(); + + String comparator = null; + int i = 0; + while ((comparator = getComparator(i, b)) != null) { + ByteBuffer data = getWithShortLength(b); + if (data != null) { + Serializer s = autoDeserialize ? getSerializer(i, comparator) + : ByteBufferSerializer.get(); + Object value = s.fromByteBuffer(data); + boolean inclusive = b.get() != 0; + components.add(new Component(value, s, comparator, inclusive)); + } else { + throw new RuntimeException("Missing component data in composite type"); + } + i++; + } + + } + + protected static int getShortLength(ByteBuffer bb) { + int length = (bb.get() & 0xFF) << 8; + return length | (bb.get() & 0xFF); + } + + protected static ByteBuffer getBytes(ByteBuffer bb, int length) { + ByteBuffer copy = bb.duplicate(); + copy.limit(copy.position() + length); + bb.position(bb.position() + length); + return copy; + } + + protected static ByteBuffer getWithShortLength(ByteBuffer bb) { + int length = getShortLength(bb); + return getBytes(bb, length); + } + + private String getComparator(int i, ByteBuffer bb) { + String name = comparatorForPosition(i); + if (name != null) { + return name; + } + if (!dynamic) { + return BYTESTYPE.getTypeName(); + } + if (bb.hasRemaining()) { + try { + int header = getShortLength(bb); + if ((header & 0x8000) == 0) { + name = ByteBufferUtil.string(getBytes(bb, header)); + } else { + name = aliasesToComparatorMapping.get((byte) (header & 0xFF)); + } + } catch (CharacterCodingException e) { + throw new RuntimeException(e); + } + } + if ((name != null) && (name.length() == 0)) { + name = null; + } + return name; + } + +} diff --git a/core/src/main/java/me/prettyprint/hector/api/beans/DynamicComposite.java b/core/src/main/java/me/prettyprint/hector/api/beans/DynamicComposite.java deleted file mode 100644 index e11ab7368..000000000 --- a/core/src/main/java/me/prettyprint/hector/api/beans/DynamicComposite.java +++ /dev/null @@ -1,455 +0,0 @@ -package me.prettyprint.hector.api.beans; - -import java.nio.ByteBuffer; -import java.nio.charset.CharacterCodingException; -import java.util.AbstractList; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.HashMap; -import java.util.Iterator; -import java.util.List; -import java.util.Map; -import java.util.logging.Logger; - -import me.prettyprint.cassandra.serializers.AsciiSerializer; -import me.prettyprint.cassandra.serializers.BigIntegerSerializer; -import me.prettyprint.cassandra.serializers.BooleanSerializer; -import me.prettyprint.cassandra.serializers.ByteBufferSerializer; -import me.prettyprint.cassandra.serializers.BytesArraySerializer; -import me.prettyprint.cassandra.serializers.LongSerializer; -import me.prettyprint.cassandra.serializers.SerializerTypeInferer; -import me.prettyprint.cassandra.serializers.ShortSerializer; -import me.prettyprint.cassandra.serializers.StringSerializer; -import me.prettyprint.cassandra.serializers.UUIDSerializer; -import me.prettyprint.cassandra.utils.ByteBufferOutputStream; -import me.prettyprint.hector.api.Serializer; - -import org.apache.cassandra.utils.ByteBufferUtil; - -@SuppressWarnings("rawtypes") -public class DynamicComposite extends AbstractList implements - Comparable { - - static final Logger logger = Logger.getLogger(DynamicComposite.class - .getName()); - - public static final Map, String> DEFAULT_SERIALIZER_TO_COMPARER_MAPPING; - - static { - DEFAULT_SERIALIZER_TO_COMPARER_MAPPING = new HashMap, String>(); - DEFAULT_SERIALIZER_TO_COMPARER_MAPPING.put(AsciiSerializer.class, - "AsciiType"); - DEFAULT_SERIALIZER_TO_COMPARER_MAPPING.put(BigIntegerSerializer.class, - "IntegerType"); - DEFAULT_SERIALIZER_TO_COMPARER_MAPPING.put(BooleanSerializer.class, - "BytesType"); - DEFAULT_SERIALIZER_TO_COMPARER_MAPPING.put(ByteBufferSerializer.class, - "BytesType"); - DEFAULT_SERIALIZER_TO_COMPARER_MAPPING.put(BytesArraySerializer.class, - "BytesType"); - DEFAULT_SERIALIZER_TO_COMPARER_MAPPING - .put(LongSerializer.class, "LongType"); - DEFAULT_SERIALIZER_TO_COMPARER_MAPPING.put(ShortSerializer.class, - "BytesType"); - DEFAULT_SERIALIZER_TO_COMPARER_MAPPING.put(StringSerializer.class, - "UTF8Type"); - DEFAULT_SERIALIZER_TO_COMPARER_MAPPING - .put(UUIDSerializer.class, "UUIDType"); - } - - public static final Map DEFAULT_COMPARER_TO_SERIALIZER_MAPPING = new HashMap(); - - static { - DEFAULT_COMPARER_TO_SERIALIZER_MAPPING.put("AsciiType", - AsciiSerializer.get()); - DEFAULT_COMPARER_TO_SERIALIZER_MAPPING.put("BytesType", - ByteBufferSerializer.get()); - DEFAULT_COMPARER_TO_SERIALIZER_MAPPING.put("IntegerType", - BigIntegerSerializer.get()); - DEFAULT_COMPARER_TO_SERIALIZER_MAPPING.put("LexicalUUIDType", - UUIDSerializer.get()); - DEFAULT_COMPARER_TO_SERIALIZER_MAPPING - .put("LongType", LongSerializer.get()); - DEFAULT_COMPARER_TO_SERIALIZER_MAPPING.put("TimeUUIDType", - UUIDSerializer.get()); - DEFAULT_COMPARER_TO_SERIALIZER_MAPPING.put("UTF8Type", - StringSerializer.get()); - - } - - public static final Map DEFAULT_ALIAS_TO_COMPARER_MAPPING = new HashMap(); - - static { - DEFAULT_ALIAS_TO_COMPARER_MAPPING.put((byte) 'a', "AsciiType"); - DEFAULT_ALIAS_TO_COMPARER_MAPPING.put((byte) 'b', "BytesType"); - DEFAULT_ALIAS_TO_COMPARER_MAPPING.put((byte) 'i', "IntegerType"); - DEFAULT_ALIAS_TO_COMPARER_MAPPING.put((byte) 'x', "LexicalUUIDType"); - DEFAULT_ALIAS_TO_COMPARER_MAPPING.put((byte) 'l', "LongType"); - DEFAULT_ALIAS_TO_COMPARER_MAPPING.put((byte) 't', "TimeUUIDType"); - DEFAULT_ALIAS_TO_COMPARER_MAPPING.put((byte) 's', "UTF8Type"); - - } - - public static final Map DEFAULT_COMPARER_TO_ALIAS_MAPPING = new HashMap(); - - static { - DEFAULT_COMPARER_TO_ALIAS_MAPPING.put("AsciiType", (byte) 'a'); - DEFAULT_COMPARER_TO_ALIAS_MAPPING.put("BytesType", (byte) 'b'); - DEFAULT_COMPARER_TO_ALIAS_MAPPING.put("IntegerType", (byte) 'i'); - DEFAULT_COMPARER_TO_ALIAS_MAPPING.put("LexicalUUIDType", (byte) 'x'); - DEFAULT_COMPARER_TO_ALIAS_MAPPING.put("LongType", (byte) 'l'); - DEFAULT_COMPARER_TO_ALIAS_MAPPING.put("TimeUUIDType", (byte) 't'); - DEFAULT_COMPARER_TO_ALIAS_MAPPING.put("UTF8Type", (byte) 's'); - - } - - Map, String> serializerToComparerMapping = DEFAULT_SERIALIZER_TO_COMPARER_MAPPING; - - Map comparerToSerializerMapping = DEFAULT_COMPARER_TO_SERIALIZER_MAPPING; - - Map aliasesToComparerMapping = DEFAULT_ALIAS_TO_COMPARER_MAPPING; - - Map comparerToAliasMapping = DEFAULT_COMPARER_TO_ALIAS_MAPPING; - - boolean autoDeserialize = true; - - List> serializersByPosition = null; - - public class Component { - final Serializer serializer; - final T value; - final String comparer; - final boolean inclusive; - - public Component(T value, Serializer serializer, String comparer, - boolean inclusive) { - this.serializer = serializer; - this.value = value; - this.comparer = comparer; - this.inclusive = inclusive; - } - - public Serializer getSerializer() { - return serializer; - } - - public T getValue() { - return value; - } - - public String getComparer() { - return comparer; - } - - public boolean isInclusive() { - return inclusive; - } - } - - List components = new ArrayList(); - - ByteBuffer serialized = null; - - public DynamicComposite() { - - } - - public List getComponents() { - return components; - } - - public void setComponents(List components) { - serialized = null; - this.components = components; - } - - public Map, String> getSerializerToComparerMapping() { - return serializerToComparerMapping; - } - - public void setSerializerToComparerMapping( - Map, String> serializerToComparerMapping) { - serialized = null; - this.serializerToComparerMapping = serializerToComparerMapping; - } - - public Map getComparerToSerializerMapping() { - return comparerToSerializerMapping; - } - - public void setComparerToSerializerMapping( - Map comparerToSerializerMapping) { - serialized = null; - this.comparerToSerializerMapping = comparerToSerializerMapping; - } - - public Map getAliasesToComparerMapping() { - return aliasesToComparerMapping; - } - - public void setAliasesToComparerMapping( - Map aliasesToComparerMapping) { - serialized = null; - this.aliasesToComparerMapping = aliasesToComparerMapping; - } - - public Map getComparerToAliasMapping() { - return comparerToAliasMapping; - } - - public void setComparerToAliasMapping(Map comparerToAliasMapping) { - serialized = null; - this.comparerToAliasMapping = comparerToAliasMapping; - } - - public boolean isAutoDeserialize() { - return autoDeserialize; - } - - public void setAutoDeserialize(boolean autoDeserialize) { - this.autoDeserialize = autoDeserialize; - } - - public List> getSerializersByPosition() { - return serializersByPosition; - } - - public void setSerializersByPosition(List> serializersByPosition) { - this.serializersByPosition = serializersByPosition; - } - - public void setSerializersByPosition(Serializer... serializers) { - serializersByPosition = Arrays.asList(serializers); - } - - @Override - public int compareTo(DynamicComposite o) { - return serialize().compareTo(o.serialize()); - } - - private String comparerForSerializer(Serializer s) { - String comparer = serializerToComparerMapping.get(s.getClass()); - if (comparer != null) { - return comparer; - } - return "BytesType"; - } - - private Serializer serializerForComparer(String c) { - Serializer s = comparerToSerializerMapping.get(c); - if (s != null) { - return s; - } - return ByteBufferSerializer.get(); - } - - private Serializer serializerForPosition(int i) { - if (serializersByPosition == null) { - return null; - } - if (i >= serializersByPosition.size()) { - return null; - } - return serializersByPosition.get(i); - } - - private Serializer getSerializer(int i, String c) { - Serializer s = serializerForPosition(i); - if (s != null) { - return s; - } - return serializerForComparer(c); - } - - public DynamicComposite add(T value, Serializer s) { - serialized = null; - - add(value, s, comparerForSerializer(s)); - - return this; - - } - - public DynamicComposite add(T value, Serializer s, String comparer) { - serialized = null; - - add(value, s, comparer, false); - - return this; - - } - - @SuppressWarnings("unchecked") - public DynamicComposite add(T value, Serializer s, String comparer, - boolean inclusive) { - serialized = null; - - components.add(new Component(value, s, comparer, inclusive)); - - return this; - - } - - @Override - public void clear() { - serialized = null; - components = new ArrayList(); - } - - @Override - public int size() { - return components.size(); - } - - @SuppressWarnings("unchecked") - @Override - public void add(int index, Object element) { - serialized = null; - Serializer s = serializerForPosition(index); - if (s == null) { - s = SerializerTypeInferer.getSerializer(element); - } - components.add(index, new Component(element, s, comparerForSerializer(s), - false)); - } - - @Override - public Object remove(int index) { - serialized = null; - Component prev = components.remove(index); - if (prev != null) { - return prev.getValue(); - } - return null; - } - - @SuppressWarnings("unchecked") - @Override - public Object set(int index, Object element) { - serialized = null; - Serializer s = SerializerTypeInferer.getSerializer(element); - Component prev = components.set(index, new Component(element, s, - comparerForSerializer(s), false)); - if (prev != null) { - return prev.getValue(); - } - return null; - } - - @Override - public Object get(int i) { - Component c = components.get(i); - if (c != null) { - return c.getValue(); - } - return null; - } - - public T get(int i, Serializer s) throws ClassCastException { - Component c = components.get(i); - if (c != null) { - return s.fromByteBuffer((ByteBuffer) c.getValue()); - } - return null; - } - - public Component getComponent(int i) { - if (i >= components.size()) { - return null; - } - Component c = components.get(i); - return c; - } - - public Iterator componentsIterator() { - return components.iterator(); - } - - @SuppressWarnings("unchecked") - public ByteBuffer serialize() { - if (serialized != null) { - return serialized.duplicate(); - } - - ByteBufferOutputStream out = new ByteBufferOutputStream(); - - for (Component c : components) { - ByteBuffer cb = c.getSerializer().toByteBuffer(c.getValue()); - - if (comparerToAliasMapping.containsKey(c.getComparer())) { - out.writeShort((short) (0x8000 | comparerToAliasMapping.get(c - .getComparer()))); - } else { - out.writeShort((short) c.getComparer().length()); - out.write(ByteBufferUtil.bytes(c.getComparer())); - } - out.writeShort((short) cb.remaining()); - out.write(cb.slice()); - out.write(c.isInclusive() ? 1 : 0); - } - - serialized = out.getByteBuffer(); - return serialized.duplicate(); - } - - @SuppressWarnings("unchecked") - public void deserialize(ByteBuffer b) { - serialized = b.duplicate(); - components = new ArrayList(); - - String comparer = null; - int i = 0; - while ((comparer = getComparator(b)) != null) { - ByteBuffer data = getWithShortLength(b); - if (data != null) { - Serializer s = autoDeserialize ? getSerializer(i, comparer) - : ByteBufferSerializer.get(); - Object value = s.fromByteBuffer(data); - boolean inclusive = b.get() != 0; - components.add(new Component(value, s, comparer, inclusive)); - } else { - throw new RuntimeException("Missing component data in composite type"); - } - i++; - } - - } - - protected static int getShortLength(ByteBuffer bb) { - int length = (bb.get() & 0xFF) << 8; - return length | (bb.get() & 0xFF); - } - - protected static ByteBuffer getBytes(ByteBuffer bb, int length) { - ByteBuffer copy = bb.duplicate(); - copy.limit(copy.position() + length); - bb.position(bb.position() + length); - return copy; - } - - protected static ByteBuffer getWithShortLength(ByteBuffer bb) { - int length = getShortLength(bb); - return getBytes(bb, length); - } - - private String getComparator(ByteBuffer bb) { - String name = null; - if (bb.hasRemaining()) { - try { - int header = getShortLength(bb); - if ((header & 0x8000) == 0) { - name = ByteBufferUtil.string(getBytes(bb, header)); - } else { - name = aliasesToComparerMapping.get((byte) (header & 0xFF)); - } - } catch (CharacterCodingException e) { - throw new RuntimeException(e); - } - } - if ((name != null) && (name.length() == 0)) { - name = null; - } - return name; - } - -} diff --git a/core/src/main/java/me/prettyprint/hector/api/ddl/ComparatorType.java b/core/src/main/java/me/prettyprint/hector/api/ddl/ComparatorType.java index 8efa8ceff..7567f47fc 100644 --- a/core/src/main/java/me/prettyprint/hector/api/ddl/ComparatorType.java +++ b/core/src/main/java/me/prettyprint/hector/api/ddl/ComparatorType.java @@ -5,26 +5,47 @@ */ public class ComparatorType { - public static ComparatorType BYTESTYPE = new ComparatorType("org.apache.cassandra.db.marshal.BytesType"); - public static ComparatorType ASCIITYPE = new ComparatorType("org.apache.cassandra.db.marshal.AsciiType"); - public static ComparatorType UTF8TYPE = new ComparatorType("org.apache.cassandra.db.marshal.UTF8Type"); - public static ComparatorType LEXICALUUIDTYPE = new ComparatorType("org.apache.cassandra.db.marshal.LexicalUUIDType"); - public static ComparatorType TIMEUUIDTYPE = new ComparatorType("org.apache.cassandra.db.marshal.TimeUUIDType"); - public static ComparatorType LONGTYPE = new ComparatorType("org.apache.cassandra.db.marshal.LongType"); - public static ComparatorType INTEGERTYPE = new ComparatorType("org.apache.cassandra.db.marshal.IntegerType"); - - private static ComparatorType[] values = {BYTESTYPE, ASCIITYPE, UTF8TYPE, LEXICALUUIDTYPE, TIMEUUIDTYPE, LONGTYPE,INTEGERTYPE}; + public static ComparatorType ASCIITYPE = new ComparatorType( + "org.apache.cassandra.db.marshal.AsciiType"); + public static ComparatorType BYTESTYPE = new ComparatorType( + "org.apache.cassandra.db.marshal.BytesType"); + public static ComparatorType INTEGERTYPE = new ComparatorType( + "org.apache.cassandra.db.marshal.IntegerType"); + public static ComparatorType LEXICALUUIDTYPE = new ComparatorType( + "org.apache.cassandra.db.marshal.LexicalUUIDType"); + public static ComparatorType LOCALBYPARTITIONERTYPE = new ComparatorType( + "org.apache.cassandra.db.marshal.LocalByPartionerType"); + public static ComparatorType LONGTYPE = new ComparatorType( + "org.apache.cassandra.db.marshal.LongType"); + public static ComparatorType TIMEUUIDTYPE = new ComparatorType( + "org.apache.cassandra.db.marshal.TimeUUIDType"); + public static ComparatorType UTF8TYPE = new ComparatorType( + "org.apache.cassandra.db.marshal.UTF8Type"); + + private static ComparatorType[] values = { ASCIITYPE, BYTESTYPE, INTEGERTYPE, + LEXICALUUIDTYPE, LOCALBYPARTITIONERTYPE, LONGTYPE, TIMEUUIDTYPE, UTF8TYPE }; private final String className; + private final String typeName; private ComparatorType(String className) { this.className = className; + if (className.startsWith("org.apache.cassandra.db.marshal.")) { + typeName = className.substring("org.apache.cassandra.db.marshal." + .length()); + } else { + typeName = className; + } } public String getClassName() { return className; } + public String getTypeName() { + return typeName; + } + public static ComparatorType getByClassName(String className) { for (int a = 0; a < values.length; a++) { @@ -32,7 +53,8 @@ public static ComparatorType getByClassName(String className) { if (type.getClassName().equals(className)) { return type; } - if (type.getClassName().equals("org.apache.cassandra.db.marshal." + className)) { + if (type.getClassName().equals( + "org.apache.cassandra.db.marshal." + className)) { return type; } } diff --git a/core/src/test/java/me/prettyprint/hector/api/DynamicCompositeTest.java b/core/src/test/java/me/prettyprint/hector/api/DynamicCompositeTest.java index deba1ab85..77777c7cb 100644 --- a/core/src/test/java/me/prettyprint/hector/api/DynamicCompositeTest.java +++ b/core/src/test/java/me/prettyprint/hector/api/DynamicCompositeTest.java @@ -10,7 +10,7 @@ import me.prettyprint.cassandra.serializers.ByteBufferSerializer; import me.prettyprint.cassandra.serializers.StringSerializer; import me.prettyprint.cassandra.utils.TimeUUIDUtils; -import me.prettyprint.hector.api.beans.DynamicComposite; +import me.prettyprint.hector.api.beans.Composite; import org.apache.cassandra.utils.ByteBufferUtil; import org.apache.cassandra.utils.UUIDGen; @@ -26,7 +26,7 @@ public class DynamicCompositeTest { @Test public void testSerialization() throws Exception { - DynamicComposite c = new DynamicComposite(); + Composite c = new Composite(); c.add("String1"); ByteBuffer b = c.serialize(); assertEquals(b.remaining(), 12); @@ -35,7 +35,7 @@ public void testSerialization() throws Exception { b = c.serialize(); assertEquals(b.remaining(), 24); - c = new DynamicComposite(); + c = new Composite(); c.deserialize(b); assertEquals(2, c.size()); Object o = c.get(0); @@ -43,17 +43,17 @@ public void testSerialization() throws Exception { o = c.get(1); assertEquals("String2", o); - c = new DynamicComposite(); + c = new Composite(); c.add(new Long(10)); b = c.serialize(); - c = new DynamicComposite(); + c = new Composite(); c.deserialize(b); o = c.get(0); assertTrue(o instanceof Long); b = createDynamicCompositeKey("Hello", TimeUUIDUtils.getUniqueTimeUUIDinMillis(), 10, false); - c = new DynamicComposite(); + c = new Composite(); c.deserialize(b.slice()); o = c.get(0); assertTrue(o instanceof ByteBuffer); @@ -66,14 +66,14 @@ public void testSerialization() throws Exception { assertEquals(BigInteger.class, o.getClass()); assertEquals(BigInteger.valueOf(10), o); - c = new DynamicComposite(); + c = new Composite(); c.setAutoDeserialize(false); c.deserialize(b.slice()); assertTrue(c.get(0) instanceof ByteBuffer); assertTrue(c.get(1) instanceof ByteBuffer); assertTrue(c.get(2) instanceof ByteBuffer); - c = new DynamicComposite(); + c = new Composite(); c.setSerializersByPosition(StringSerializer.get(), null, ByteBufferSerializer.get()); c.deserialize(b.slice()); From 5d7b6d07af51a6a5c548e66ea162f94dd9794079 Mon Sep 17 00:00:00 2001 From: Ed Anuff Date: Wed, 23 Mar 2011 11:52:17 -0700 Subject: [PATCH 036/144] Added test of deserializing static composites to unit test --- .../hector/api/beans/Composite.java | 59 ++++++++++--------- .../hector/api/DynamicCompositeTest.java | 55 ++++++++++++++++- 2 files changed, 85 insertions(+), 29 deletions(-) diff --git a/core/src/main/java/me/prettyprint/hector/api/beans/Composite.java b/core/src/main/java/me/prettyprint/hector/api/beans/Composite.java index 399582dc8..9e54b747c 100644 --- a/core/src/main/java/me/prettyprint/hector/api/beans/Composite.java +++ b/core/src/main/java/me/prettyprint/hector/api/beans/Composite.java @@ -38,8 +38,7 @@ public class Composite extends AbstractList implements Comparable { - static final Logger logger = Logger.getLogger(Composite.class - .getName()); + static final Logger logger = Logger.getLogger(Composite.class.getName()); public static final Map, String> DEFAULT_SERIALIZER_TO_COMPARATOR_MAPPING; @@ -318,6 +317,36 @@ private String comparatorForPosition(int i) { return comparatorsByPosition.get(i); } + private String getComparator(int i, ByteBuffer bb) { + String name = comparatorForPosition(i); + if (name != null) { + return name; + } + if (!dynamic) { + if (bb.hasRemaining()) { + return BYTESTYPE.getTypeName(); + } else { + return null; + } + } + if (bb.hasRemaining()) { + try { + int header = getShortLength(bb); + if ((header & 0x8000) == 0) { + name = ByteBufferUtil.string(getBytes(bb, header)); + } else { + name = aliasesToComparatorMapping.get((byte) (header & 0xFF)); + } + } catch (CharacterCodingException e) { + throw new RuntimeException(e); + } + } + if ((name != null) && (name.length() == 0)) { + name = null; + } + return name; + } + public Composite add(T value, Serializer s) { serialized = null; @@ -501,30 +530,4 @@ protected static ByteBuffer getWithShortLength(ByteBuffer bb) { return getBytes(bb, length); } - private String getComparator(int i, ByteBuffer bb) { - String name = comparatorForPosition(i); - if (name != null) { - return name; - } - if (!dynamic) { - return BYTESTYPE.getTypeName(); - } - if (bb.hasRemaining()) { - try { - int header = getShortLength(bb); - if ((header & 0x8000) == 0) { - name = ByteBufferUtil.string(getBytes(bb, header)); - } else { - name = aliasesToComparatorMapping.get((byte) (header & 0xFF)); - } - } catch (CharacterCodingException e) { - throw new RuntimeException(e); - } - } - if ((name != null) && (name.length() == 0)) { - name = null; - } - return name; - } - } diff --git a/core/src/test/java/me/prettyprint/hector/api/DynamicCompositeTest.java b/core/src/test/java/me/prettyprint/hector/api/DynamicCompositeTest.java index 77777c7cb..283df8062 100644 --- a/core/src/test/java/me/prettyprint/hector/api/DynamicCompositeTest.java +++ b/core/src/test/java/me/prettyprint/hector/api/DynamicCompositeTest.java @@ -7,8 +7,10 @@ import java.nio.ByteBuffer; import java.util.UUID; +import me.prettyprint.cassandra.serializers.BigIntegerSerializer; import me.prettyprint.cassandra.serializers.ByteBufferSerializer; import me.prettyprint.cassandra.serializers.StringSerializer; +import me.prettyprint.cassandra.serializers.UUIDSerializer; import me.prettyprint.cassandra.utils.TimeUUIDUtils; import me.prettyprint.hector.api.beans.Composite; @@ -80,9 +82,20 @@ public void testSerialization() throws Exception { assertTrue(c.get(0) instanceof String); assertTrue(c.get(1) instanceof UUID); assertTrue(c.get(2) instanceof ByteBuffer); + + b = createCompositeKey("Hello", TimeUUIDUtils.getUniqueTimeUUIDinMillis(), + 10, false); + c = new Composite(); + c.setDynamic(false); + c.setSerializersByPosition(StringSerializer.get(), UUIDSerializer.get(), + BigIntegerSerializer.get()); + c.deserialize(b.slice()); + assertTrue(c.get(0) instanceof String); + assertTrue(c.get(1) instanceof UUID); + assertTrue(c.get(2) instanceof BigInteger); } - // from the Casssandra DynamicCompositeType unit test + // from the Casssandra DynamicCompositeTypeTest unit test private ByteBuffer createDynamicCompositeKey(String s, UUID uuid, int i, boolean lastIsOne) { ByteBuffer bytes = ByteBufferUtil.bytes(s); @@ -126,4 +139,44 @@ private ByteBuffer createDynamicCompositeKey(String s, UUID uuid, int i, return bb; } + // from the Casssandra CompositeTypeTest unit test + static ByteBuffer createCompositeKey(String s, UUID uuid, int i, + boolean lastIsOne) { + ByteBuffer bytes = ByteBufferUtil.bytes(s); + int totalSize = 0; + if (s != null) { + totalSize += 2 + bytes.remaining() + 1; + if (uuid != null) { + totalSize += 2 + 16 + 1; + if (i != -1) { + totalSize += 2 + 1 + 1; + } + } + } + + ByteBuffer bb = ByteBuffer.allocate(totalSize); + + if (s != null) { + bb.putShort((short) bytes.remaining()); + bb.put(bytes); + bb.put((uuid == null) && lastIsOne ? (byte) 1 : (byte) 0); + if (uuid != null) { + bb.putShort((short) 16); + bb.put(UUIDGen.decompose(uuid)); + bb.put((i == -1) && lastIsOne ? (byte) 1 : (byte) 0); + if (i != -1) { + // We are putting a byte only because our test use ints that fit in a + // byte *and* IntegerType.fromString() will + // return something compatible (i.e, putting a full int here would + // break 'fromStringTest') + bb.putShort((short) 1); + bb.put((byte) i); + bb.put(lastIsOne ? (byte) 1 : (byte) 0); + } + } + } + bb.rewind(); + return bb; + } + } From 0a44ca375c240dd967dfbb3db2e0d1f9557943a1 Mon Sep 17 00:00:00 2001 From: Ed Anuff Date: Wed, 23 Mar 2011 11:55:46 -0700 Subject: [PATCH 037/144] rename DynamicCompositeTest to CompositeTest --- .../api/{DynamicCompositeTest.java => CompositeTest.java} | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) rename core/src/test/java/me/prettyprint/hector/api/{DynamicCompositeTest.java => CompositeTest.java} (99%) diff --git a/core/src/test/java/me/prettyprint/hector/api/DynamicCompositeTest.java b/core/src/test/java/me/prettyprint/hector/api/CompositeTest.java similarity index 99% rename from core/src/test/java/me/prettyprint/hector/api/DynamicCompositeTest.java rename to core/src/test/java/me/prettyprint/hector/api/CompositeTest.java index 283df8062..c4fd53270 100644 --- a/core/src/test/java/me/prettyprint/hector/api/DynamicCompositeTest.java +++ b/core/src/test/java/me/prettyprint/hector/api/CompositeTest.java @@ -20,7 +20,7 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; -public class DynamicCompositeTest { +public class CompositeTest { private static final Logger log = LoggerFactory .getLogger(ClockResolutionTest.class); From 0ae44b5438cd60d37156d615ee1f415ebe58604a Mon Sep 17 00:00:00 2001 From: Ed Anuff Date: Wed, 23 Mar 2011 11:56:33 -0700 Subject: [PATCH 038/144] removed unused logger from test --- .../test/java/me/prettyprint/hector/api/CompositeTest.java | 5 ----- 1 file changed, 5 deletions(-) diff --git a/core/src/test/java/me/prettyprint/hector/api/CompositeTest.java b/core/src/test/java/me/prettyprint/hector/api/CompositeTest.java index c4fd53270..553b3e0a5 100644 --- a/core/src/test/java/me/prettyprint/hector/api/CompositeTest.java +++ b/core/src/test/java/me/prettyprint/hector/api/CompositeTest.java @@ -17,14 +17,9 @@ import org.apache.cassandra.utils.ByteBufferUtil; import org.apache.cassandra.utils.UUIDGen; import org.junit.Test; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; public class CompositeTest { - private static final Logger log = LoggerFactory - .getLogger(ClockResolutionTest.class); - @Test public void testSerialization() throws Exception { From 298a173bca2ee2921884350b6734aba79329324b Mon Sep 17 00:00:00 2001 From: tamalex Date: Wed, 23 Mar 2011 12:14:05 -0700 Subject: [PATCH 039/144] Edited core/src/main/java/me/prettyprint/cassandra/model/ExecutionResult.java via GitHub --- .../me/prettyprint/cassandra/model/ExecutionResult.java | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/core/src/main/java/me/prettyprint/cassandra/model/ExecutionResult.java b/core/src/main/java/me/prettyprint/cassandra/model/ExecutionResult.java index d193bf38d..22a76bb4f 100644 --- a/core/src/main/java/me/prettyprint/cassandra/model/ExecutionResult.java +++ b/core/src/main/java/me/prettyprint/cassandra/model/ExecutionResult.java @@ -34,6 +34,13 @@ public T get() { return value; } + /** + * @return the execution time, as already recorded, in nanos + */ + public long getExecutionTimeNano() { + return execTime; + } + /** * Execution time is actually recorded in nanos, so we divide this by 1000 * make the number more sensible @@ -43,6 +50,7 @@ public long getExecutionTimeMicro() { return execTime / MICRO_DENOM; } + @Override public String toString() { return formatMessage("ExecutionResult", "n/a"); From 551c12458723e1e1f0ecd770c32aa60cec5a5656 Mon Sep 17 00:00:00 2001 From: tamalex Date: Wed, 23 Mar 2011 12:15:42 -0700 Subject: [PATCH 040/144] Edited core/src/main/java/me/prettyprint/cassandra/model/AbstractSubColumnQuery.java via GitHub --- .../me/prettyprint/cassandra/model/AbstractSubColumnQuery.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/src/main/java/me/prettyprint/cassandra/model/AbstractSubColumnQuery.java b/core/src/main/java/me/prettyprint/cassandra/model/AbstractSubColumnQuery.java index 8c8bcb04a..84cabeaca 100644 --- a/core/src/main/java/me/prettyprint/cassandra/model/AbstractSubColumnQuery.java +++ b/core/src/main/java/me/prettyprint/cassandra/model/AbstractSubColumnQuery.java @@ -64,6 +64,6 @@ public QueryResult> execute() { List> columns = slice.getColumns(); HColumn column = columns.size() == 0 ? null : columns.get(0); return new QueryResultImpl>( - new ExecutionResult>(column, r.getExecutionTimeMicro(), r.getHostUsed()), this); + new ExecutionResult>(column, r.getExecutionTimeNano(), r.getHostUsed()), this); } } From 0393b6e98f36cfbe7fbc907096e7bc72487b64db Mon Sep 17 00:00:00 2001 From: Ed Anuff Date: Wed, 23 Mar 2011 12:38:08 -0700 Subject: [PATCH 041/144] Refactored Composite into AbstractComposite, Composite, and DynamicComposite, to disambiguate usage --- .../serializers/CompositeSerializer.java | 32 ++ .../DynamicCompositeSerializer.java | 39 +- .../hector/api/beans/AbstractComposite.java | 539 ++++++++++++++++++ .../hector/api/beans/Composite.java | 526 +---------------- .../hector/api/beans/DynamicComposite.java | 13 + .../prettyprint/hector/api/CompositeTest.java | 29 +- 6 files changed, 624 insertions(+), 554 deletions(-) create mode 100644 core/src/main/java/me/prettyprint/cassandra/serializers/CompositeSerializer.java create mode 100644 core/src/main/java/me/prettyprint/hector/api/beans/AbstractComposite.java create mode 100644 core/src/main/java/me/prettyprint/hector/api/beans/DynamicComposite.java diff --git a/core/src/main/java/me/prettyprint/cassandra/serializers/CompositeSerializer.java b/core/src/main/java/me/prettyprint/cassandra/serializers/CompositeSerializer.java new file mode 100644 index 000000000..c5242f21d --- /dev/null +++ b/core/src/main/java/me/prettyprint/cassandra/serializers/CompositeSerializer.java @@ -0,0 +1,32 @@ +/** + * + */ +package me.prettyprint.cassandra.serializers; + +import java.nio.ByteBuffer; + +import me.prettyprint.hector.api.beans.Composite; + +/** + * @author Todd Nine + * + */ +public class CompositeSerializer extends AbstractSerializer { + + @Override + public ByteBuffer toByteBuffer(Composite obj) { + + return obj.serialize(); + } + + @Override + public Composite fromByteBuffer(ByteBuffer byteBuffer) { + + Composite composite = new Composite(); + composite.deserialize(byteBuffer); + + return composite; + + } + +} diff --git a/core/src/main/java/me/prettyprint/cassandra/serializers/DynamicCompositeSerializer.java b/core/src/main/java/me/prettyprint/cassandra/serializers/DynamicCompositeSerializer.java index 826b04c96..ccab74477 100644 --- a/core/src/main/java/me/prettyprint/cassandra/serializers/DynamicCompositeSerializer.java +++ b/core/src/main/java/me/prettyprint/cassandra/serializers/DynamicCompositeSerializer.java @@ -5,28 +5,29 @@ import java.nio.ByteBuffer; -import me.prettyprint.hector.api.beans.Composite; +import me.prettyprint.hector.api.beans.DynamicComposite; /** * @author Todd Nine - * + * */ -public class DynamicCompositeSerializer extends AbstractSerializer{ - - @Override - public ByteBuffer toByteBuffer(Composite obj) { - - return obj.serialize(); - } - - @Override - public Composite fromByteBuffer(ByteBuffer byteBuffer) { - - Composite composite = new Composite(); - composite.deserialize(byteBuffer); - - return composite; - - } +public class DynamicCompositeSerializer extends + AbstractSerializer { + + @Override + public ByteBuffer toByteBuffer(DynamicComposite obj) { + + return obj.serialize(); + } + + @Override + public DynamicComposite fromByteBuffer(ByteBuffer byteBuffer) { + + DynamicComposite composite = new DynamicComposite(); + composite.deserialize(byteBuffer); + + return composite; + + } } diff --git a/core/src/main/java/me/prettyprint/hector/api/beans/AbstractComposite.java b/core/src/main/java/me/prettyprint/hector/api/beans/AbstractComposite.java new file mode 100644 index 000000000..2f386d7b1 --- /dev/null +++ b/core/src/main/java/me/prettyprint/hector/api/beans/AbstractComposite.java @@ -0,0 +1,539 @@ +package me.prettyprint.hector.api.beans; + +import static me.prettyprint.hector.api.ddl.ComparatorType.ASCIITYPE; +import static me.prettyprint.hector.api.ddl.ComparatorType.BYTESTYPE; +import static me.prettyprint.hector.api.ddl.ComparatorType.INTEGERTYPE; +import static me.prettyprint.hector.api.ddl.ComparatorType.LEXICALUUIDTYPE; +import static me.prettyprint.hector.api.ddl.ComparatorType.LONGTYPE; +import static me.prettyprint.hector.api.ddl.ComparatorType.TIMEUUIDTYPE; +import static me.prettyprint.hector.api.ddl.ComparatorType.UTF8TYPE; + +import java.nio.ByteBuffer; +import java.nio.charset.CharacterCodingException; +import java.util.AbstractList; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.HashMap; +import java.util.Iterator; +import java.util.List; +import java.util.Map; +import java.util.logging.Logger; + +import me.prettyprint.cassandra.serializers.AsciiSerializer; +import me.prettyprint.cassandra.serializers.BigIntegerSerializer; +import me.prettyprint.cassandra.serializers.BooleanSerializer; +import me.prettyprint.cassandra.serializers.ByteBufferSerializer; +import me.prettyprint.cassandra.serializers.BytesArraySerializer; +import me.prettyprint.cassandra.serializers.LongSerializer; +import me.prettyprint.cassandra.serializers.SerializerTypeInferer; +import me.prettyprint.cassandra.serializers.ShortSerializer; +import me.prettyprint.cassandra.serializers.StringSerializer; +import me.prettyprint.cassandra.serializers.UUIDSerializer; +import me.prettyprint.cassandra.utils.ByteBufferOutputStream; +import me.prettyprint.hector.api.Serializer; + +import org.apache.cassandra.utils.ByteBufferUtil; + +@SuppressWarnings("rawtypes") +public abstract class AbstractComposite extends AbstractList implements + Comparable { + + static final Logger logger = Logger.getLogger(AbstractComposite.class + .getName()); + + public static final Map, String> DEFAULT_SERIALIZER_TO_COMPARATOR_MAPPING; + + static { + DEFAULT_SERIALIZER_TO_COMPARATOR_MAPPING = new HashMap, String>(); + DEFAULT_SERIALIZER_TO_COMPARATOR_MAPPING.put(AsciiSerializer.class, + ASCIITYPE.getTypeName()); + DEFAULT_SERIALIZER_TO_COMPARATOR_MAPPING.put(BigIntegerSerializer.class, + INTEGERTYPE.getTypeName()); + DEFAULT_SERIALIZER_TO_COMPARATOR_MAPPING.put(BooleanSerializer.class, + BYTESTYPE.getTypeName()); + DEFAULT_SERIALIZER_TO_COMPARATOR_MAPPING.put(ByteBufferSerializer.class, + BYTESTYPE.getTypeName()); + DEFAULT_SERIALIZER_TO_COMPARATOR_MAPPING.put(BytesArraySerializer.class, + BYTESTYPE.getTypeName()); + DEFAULT_SERIALIZER_TO_COMPARATOR_MAPPING.put(LongSerializer.class, + LONGTYPE.getTypeName()); + DEFAULT_SERIALIZER_TO_COMPARATOR_MAPPING.put(ShortSerializer.class, + BYTESTYPE.getTypeName()); + DEFAULT_SERIALIZER_TO_COMPARATOR_MAPPING.put(StringSerializer.class, + UTF8TYPE.getTypeName()); + // TODO this doesn't work, there isn't a unified UUIDType yet + DEFAULT_SERIALIZER_TO_COMPARATOR_MAPPING.put(UUIDSerializer.class, + "UUIDType"); + } + + public static final Map DEFAULT_COMPARATOR_TO_SERIALIZER_MAPPING = new HashMap(); + + static { + DEFAULT_COMPARATOR_TO_SERIALIZER_MAPPING.put(ASCIITYPE.getTypeName(), + AsciiSerializer.get()); + DEFAULT_COMPARATOR_TO_SERIALIZER_MAPPING.put(BYTESTYPE.getTypeName(), + ByteBufferSerializer.get()); + DEFAULT_COMPARATOR_TO_SERIALIZER_MAPPING.put(INTEGERTYPE.getTypeName(), + BigIntegerSerializer.get()); + DEFAULT_COMPARATOR_TO_SERIALIZER_MAPPING.put(LEXICALUUIDTYPE.getTypeName(), + UUIDSerializer.get()); + DEFAULT_COMPARATOR_TO_SERIALIZER_MAPPING.put(LONGTYPE.getTypeName(), + LongSerializer.get()); + DEFAULT_COMPARATOR_TO_SERIALIZER_MAPPING.put(TIMEUUIDTYPE.getTypeName(), + UUIDSerializer.get()); + DEFAULT_COMPARATOR_TO_SERIALIZER_MAPPING.put(UTF8TYPE.getTypeName(), + StringSerializer.get()); + + } + + public static final Map DEFAULT_ALIAS_TO_COMPARATOR_MAPPING = new HashMap(); + + static { + DEFAULT_ALIAS_TO_COMPARATOR_MAPPING + .put((byte) 'a', ASCIITYPE.getTypeName()); + DEFAULT_ALIAS_TO_COMPARATOR_MAPPING + .put((byte) 'b', BYTESTYPE.getTypeName()); + DEFAULT_ALIAS_TO_COMPARATOR_MAPPING.put((byte) 'i', + INTEGERTYPE.getTypeName()); + DEFAULT_ALIAS_TO_COMPARATOR_MAPPING.put((byte) 'x', + LEXICALUUIDTYPE.getTypeName()); + DEFAULT_ALIAS_TO_COMPARATOR_MAPPING.put((byte) 'l', LONGTYPE.getTypeName()); + DEFAULT_ALIAS_TO_COMPARATOR_MAPPING.put((byte) 't', + TIMEUUIDTYPE.getTypeName()); + DEFAULT_ALIAS_TO_COMPARATOR_MAPPING.put((byte) 's', UTF8TYPE.getTypeName()); + + } + + public static final Map DEFAULT_COMPARATOR_TO_ALIAS_MAPPING = new HashMap(); + + static { + DEFAULT_COMPARATOR_TO_ALIAS_MAPPING + .put(ASCIITYPE.getTypeName(), (byte) 'a'); + DEFAULT_COMPARATOR_TO_ALIAS_MAPPING + .put(BYTESTYPE.getTypeName(), (byte) 'b'); + DEFAULT_COMPARATOR_TO_ALIAS_MAPPING.put(INTEGERTYPE.getTypeName(), + (byte) 'i'); + DEFAULT_COMPARATOR_TO_ALIAS_MAPPING.put(LEXICALUUIDTYPE.getTypeName(), + (byte) 'x'); + DEFAULT_COMPARATOR_TO_ALIAS_MAPPING.put(LONGTYPE.getTypeName(), (byte) 'l'); + DEFAULT_COMPARATOR_TO_ALIAS_MAPPING.put(TIMEUUIDTYPE.getTypeName(), + (byte) 't'); + DEFAULT_COMPARATOR_TO_ALIAS_MAPPING.put(UTF8TYPE.getTypeName(), (byte) 's'); + + } + + Map, String> serializerToComparatorMapping = DEFAULT_SERIALIZER_TO_COMPARATOR_MAPPING; + + Map comparatorToSerializerMapping = DEFAULT_COMPARATOR_TO_SERIALIZER_MAPPING; + + Map aliasesToComparatorMapping = DEFAULT_ALIAS_TO_COMPARATOR_MAPPING; + + Map comparatorToAliasMapping = DEFAULT_COMPARATOR_TO_ALIAS_MAPPING; + + boolean autoDeserialize = true; + + boolean dynamic = false; + + List> serializersByPosition = null; + List comparatorsByPosition = null; + + public class Component { + final Serializer serializer; + final T value; + final String comparator; + final boolean inclusive; + + public Component(T value, Serializer serializer, String comparator, + boolean inclusive) { + this.serializer = serializer; + this.value = value; + this.comparator = comparator; + this.inclusive = inclusive; + } + + public Serializer getSerializer() { + return serializer; + } + + public T getValue() { + return value; + } + + public String getComparator() { + return comparator; + } + + public boolean isInclusive() { + return inclusive; + } + } + + List components = new ArrayList(); + + ByteBuffer serialized = null; + + public AbstractComposite() { + + } + + public AbstractComposite(boolean dynamic) { + this.dynamic = dynamic; + } + + public AbstractComposite(Object... o) { + this.addAll(Arrays.asList(o)); + } + + public AbstractComposite(boolean dynamic, Object... o) { + this.dynamic = dynamic; + this.addAll(Arrays.asList(o)); + } + + public List getComponents() { + return components; + } + + public void setComponents(List components) { + serialized = null; + this.components = components; + } + + public Map, String> getSerializerToComparatorMapping() { + return serializerToComparatorMapping; + } + + public void setSerializerToComparatorMapping( + Map, String> serializerToComparatorMapping) { + serialized = null; + this.serializerToComparatorMapping = serializerToComparatorMapping; + } + + public Map getComparatorToSerializerMapping() { + return comparatorToSerializerMapping; + } + + public void setComparatorToSerializerMapping( + Map comparatorToSerializerMapping) { + serialized = null; + this.comparatorToSerializerMapping = comparatorToSerializerMapping; + } + + public Map getAliasesToComparatorMapping() { + return aliasesToComparatorMapping; + } + + public void setAliasesToComparatorMapping( + Map aliasesToComparatorMapping) { + serialized = null; + this.aliasesToComparatorMapping = aliasesToComparatorMapping; + } + + public Map getComparatorToAliasMapping() { + return comparatorToAliasMapping; + } + + public void setComparatorToAliasMapping( + Map comparatorToAliasMapping) { + serialized = null; + this.comparatorToAliasMapping = comparatorToAliasMapping; + } + + public boolean isAutoDeserialize() { + return autoDeserialize; + } + + public void setAutoDeserialize(boolean autoDeserialize) { + this.autoDeserialize = autoDeserialize; + } + + public boolean isDynamic() { + return dynamic; + } + + public List> getSerializersByPosition() { + return serializersByPosition; + } + + public void setSerializersByPosition(List> serializersByPosition) { + this.serializersByPosition = serializersByPosition; + } + + public void setSerializersByPosition(Serializer... serializers) { + serializersByPosition = Arrays.asList(serializers); + } + + public List getComparatorsByPosition() { + return comparatorsByPosition; + } + + public void setComparatorsByPosition(List comparatorsByPosition) { + this.comparatorsByPosition = comparatorsByPosition; + } + + public void setComparatorsByPosition(String... comparators) { + comparatorsByPosition = Arrays.asList(comparators); + } + + @Override + public int compareTo(AbstractComposite o) { + return serialize().compareTo(o.serialize()); + } + + private String comparatorForSerializer(Serializer s) { + String comparator = serializerToComparatorMapping.get(s.getClass()); + if (comparator != null) { + return comparator; + } + return BYTESTYPE.getTypeName(); + } + + private Serializer serializerForComparator(String c) { + Serializer s = comparatorToSerializerMapping.get(c); + if (s != null) { + return s; + } + return ByteBufferSerializer.get(); + } + + private Serializer serializerForPosition(int i) { + if (serializersByPosition == null) { + return null; + } + if (i >= serializersByPosition.size()) { + return null; + } + return serializersByPosition.get(i); + } + + private Serializer getSerializer(int i, String c) { + Serializer s = serializerForPosition(i); + if (s != null) { + return s; + } + return serializerForComparator(c); + } + + private String comparatorForPosition(int i) { + if (comparatorsByPosition == null) { + return null; + } + if (i >= comparatorsByPosition.size()) { + return null; + } + return comparatorsByPosition.get(i); + } + + private String getComparator(int i, ByteBuffer bb) { + String name = comparatorForPosition(i); + if (name != null) { + return name; + } + if (!dynamic) { + if (bb.hasRemaining()) { + return BYTESTYPE.getTypeName(); + } else { + return null; + } + } + if (bb.hasRemaining()) { + try { + int header = getShortLength(bb); + if ((header & 0x8000) == 0) { + name = ByteBufferUtil.string(getBytes(bb, header)); + } else { + name = aliasesToComparatorMapping.get((byte) (header & 0xFF)); + } + } catch (CharacterCodingException e) { + throw new RuntimeException(e); + } + } + if ((name != null) && (name.length() == 0)) { + name = null; + } + return name; + } + + public AbstractComposite add(T value, Serializer s) { + serialized = null; + + add(value, s, comparatorForSerializer(s)); + + return this; + + } + + public AbstractComposite add(T value, Serializer s, String comparator) { + serialized = null; + + add(value, s, comparator, false); + + return this; + + } + + @SuppressWarnings("unchecked") + public AbstractComposite add(T value, Serializer s, String comparator, + boolean inclusive) { + serialized = null; + + components.add(new Component(value, s, comparator, inclusive)); + + return this; + + } + + @Override + public void clear() { + serialized = null; + components = new ArrayList(); + } + + @Override + public int size() { + return components.size(); + } + + @SuppressWarnings("unchecked") + @Override + public void add(int index, Object element) { + serialized = null; + Serializer s = serializerForPosition(index); + if (s == null) { + s = SerializerTypeInferer.getSerializer(element); + } + components.add(index, new Component(element, s, comparatorForSerializer(s), + false)); + } + + @Override + public Object remove(int index) { + serialized = null; + Component prev = components.remove(index); + if (prev != null) { + return prev.getValue(); + } + return null; + } + + @SuppressWarnings("unchecked") + @Override + public Object set(int index, Object element) { + serialized = null; + Serializer s = SerializerTypeInferer.getSerializer(element); + Component prev = components.set(index, new Component(element, s, + comparatorForSerializer(s), false)); + if (prev != null) { + return prev.getValue(); + } + return null; + } + + @Override + public Object get(int i) { + Component c = components.get(i); + if (c != null) { + return c.getValue(); + } + return null; + } + + public T get(int i, Serializer s) throws ClassCastException { + Component c = components.get(i); + if (c != null) { + return s.fromByteBuffer((ByteBuffer) c.getValue()); + } + return null; + } + + public Component getComponent(int i) { + if (i >= components.size()) { + return null; + } + Component c = components.get(i); + return c; + } + + public Iterator componentsIterator() { + return components.iterator(); + } + + @SuppressWarnings("unchecked") + public ByteBuffer serialize() { + if (serialized != null) { + return serialized.duplicate(); + } + + ByteBufferOutputStream out = new ByteBufferOutputStream(); + + int i = 0; + for (Component c : components) { + Serializer s = serializerForPosition(i); + if (s == null) { + s = c.getSerializer(); + } + ByteBuffer cb = s.toByteBuffer(c.getValue()); + + if (dynamic) { + String comparator = comparatorForPosition(i); + if (comparator == null) { + comparator = c.getComparator(); + } + if (comparatorToAliasMapping.containsKey(comparator)) { + out.writeShort((short) (0x8000 | comparatorToAliasMapping + .get(comparator))); + } else { + out.writeShort((short) comparator.length()); + out.write(ByteBufferUtil.bytes(comparator)); + } + } + out.writeShort((short) cb.remaining()); + out.write(cb.slice()); + out.write(c.isInclusive() ? 1 : 0); + i++; + } + + serialized = out.getByteBuffer(); + return serialized.duplicate(); + } + + @SuppressWarnings("unchecked") + public void deserialize(ByteBuffer b) { + serialized = b.duplicate(); + components = new ArrayList(); + + String comparator = null; + int i = 0; + while ((comparator = getComparator(i, b)) != null) { + ByteBuffer data = getWithShortLength(b); + if (data != null) { + Serializer s = autoDeserialize ? getSerializer(i, comparator) + : ByteBufferSerializer.get(); + Object value = s.fromByteBuffer(data); + boolean inclusive = b.get() != 0; + components.add(new Component(value, s, comparator, inclusive)); + } else { + throw new RuntimeException("Missing component data in composite type"); + } + i++; + } + + } + + protected static int getShortLength(ByteBuffer bb) { + int length = (bb.get() & 0xFF) << 8; + return length | (bb.get() & 0xFF); + } + + protected static ByteBuffer getBytes(ByteBuffer bb, int length) { + ByteBuffer copy = bb.duplicate(); + copy.limit(copy.position() + length); + bb.position(bb.position() + length); + return copy; + } + + protected static ByteBuffer getWithShortLength(ByteBuffer bb) { + int length = getShortLength(bb); + return getBytes(bb, length); + } + +} diff --git a/core/src/main/java/me/prettyprint/hector/api/beans/Composite.java b/core/src/main/java/me/prettyprint/hector/api/beans/Composite.java index 9e54b747c..04109034a 100644 --- a/core/src/main/java/me/prettyprint/hector/api/beans/Composite.java +++ b/core/src/main/java/me/prettyprint/hector/api/beans/Composite.java @@ -1,533 +1,13 @@ package me.prettyprint.hector.api.beans; -import static me.prettyprint.hector.api.ddl.ComparatorType.ASCIITYPE; -import static me.prettyprint.hector.api.ddl.ComparatorType.BYTESTYPE; -import static me.prettyprint.hector.api.ddl.ComparatorType.INTEGERTYPE; -import static me.prettyprint.hector.api.ddl.ComparatorType.LEXICALUUIDTYPE; -import static me.prettyprint.hector.api.ddl.ComparatorType.LONGTYPE; -import static me.prettyprint.hector.api.ddl.ComparatorType.TIMEUUIDTYPE; -import static me.prettyprint.hector.api.ddl.ComparatorType.UTF8TYPE; - -import java.nio.ByteBuffer; -import java.nio.charset.CharacterCodingException; -import java.util.AbstractList; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.HashMap; -import java.util.Iterator; -import java.util.List; -import java.util.Map; -import java.util.logging.Logger; - -import me.prettyprint.cassandra.serializers.AsciiSerializer; -import me.prettyprint.cassandra.serializers.BigIntegerSerializer; -import me.prettyprint.cassandra.serializers.BooleanSerializer; -import me.prettyprint.cassandra.serializers.ByteBufferSerializer; -import me.prettyprint.cassandra.serializers.BytesArraySerializer; -import me.prettyprint.cassandra.serializers.LongSerializer; -import me.prettyprint.cassandra.serializers.SerializerTypeInferer; -import me.prettyprint.cassandra.serializers.ShortSerializer; -import me.prettyprint.cassandra.serializers.StringSerializer; -import me.prettyprint.cassandra.serializers.UUIDSerializer; -import me.prettyprint.cassandra.utils.ByteBufferOutputStream; -import me.prettyprint.hector.api.Serializer; - -import org.apache.cassandra.utils.ByteBufferUtil; - -@SuppressWarnings("rawtypes") -public class Composite extends AbstractList implements - Comparable { - - static final Logger logger = Logger.getLogger(Composite.class.getName()); - - public static final Map, String> DEFAULT_SERIALIZER_TO_COMPARATOR_MAPPING; - - static { - DEFAULT_SERIALIZER_TO_COMPARATOR_MAPPING = new HashMap, String>(); - DEFAULT_SERIALIZER_TO_COMPARATOR_MAPPING.put(AsciiSerializer.class, - ASCIITYPE.getTypeName()); - DEFAULT_SERIALIZER_TO_COMPARATOR_MAPPING.put(BigIntegerSerializer.class, - INTEGERTYPE.getTypeName()); - DEFAULT_SERIALIZER_TO_COMPARATOR_MAPPING.put(BooleanSerializer.class, - BYTESTYPE.getTypeName()); - DEFAULT_SERIALIZER_TO_COMPARATOR_MAPPING.put(ByteBufferSerializer.class, - BYTESTYPE.getTypeName()); - DEFAULT_SERIALIZER_TO_COMPARATOR_MAPPING.put(BytesArraySerializer.class, - BYTESTYPE.getTypeName()); - DEFAULT_SERIALIZER_TO_COMPARATOR_MAPPING.put(LongSerializer.class, - LONGTYPE.getTypeName()); - DEFAULT_SERIALIZER_TO_COMPARATOR_MAPPING.put(ShortSerializer.class, - BYTESTYPE.getTypeName()); - DEFAULT_SERIALIZER_TO_COMPARATOR_MAPPING.put(StringSerializer.class, - UTF8TYPE.getTypeName()); - // TODO this doesn't work, there isn't a unified UUIDType yet - DEFAULT_SERIALIZER_TO_COMPARATOR_MAPPING.put(UUIDSerializer.class, - "UUIDType"); - } - - public static final Map DEFAULT_COMPARATOR_TO_SERIALIZER_MAPPING = new HashMap(); - - static { - DEFAULT_COMPARATOR_TO_SERIALIZER_MAPPING.put(ASCIITYPE.getTypeName(), - AsciiSerializer.get()); - DEFAULT_COMPARATOR_TO_SERIALIZER_MAPPING.put(BYTESTYPE.getTypeName(), - ByteBufferSerializer.get()); - DEFAULT_COMPARATOR_TO_SERIALIZER_MAPPING.put(INTEGERTYPE.getTypeName(), - BigIntegerSerializer.get()); - DEFAULT_COMPARATOR_TO_SERIALIZER_MAPPING.put(LEXICALUUIDTYPE.getTypeName(), - UUIDSerializer.get()); - DEFAULT_COMPARATOR_TO_SERIALIZER_MAPPING.put(LONGTYPE.getTypeName(), - LongSerializer.get()); - DEFAULT_COMPARATOR_TO_SERIALIZER_MAPPING.put(TIMEUUIDTYPE.getTypeName(), - UUIDSerializer.get()); - DEFAULT_COMPARATOR_TO_SERIALIZER_MAPPING.put(UTF8TYPE.getTypeName(), - StringSerializer.get()); - - } - - public static final Map DEFAULT_ALIAS_TO_COMPARATOR_MAPPING = new HashMap(); - - static { - DEFAULT_ALIAS_TO_COMPARATOR_MAPPING - .put((byte) 'a', ASCIITYPE.getTypeName()); - DEFAULT_ALIAS_TO_COMPARATOR_MAPPING - .put((byte) 'b', BYTESTYPE.getTypeName()); - DEFAULT_ALIAS_TO_COMPARATOR_MAPPING.put((byte) 'i', - INTEGERTYPE.getTypeName()); - DEFAULT_ALIAS_TO_COMPARATOR_MAPPING.put((byte) 'x', - LEXICALUUIDTYPE.getTypeName()); - DEFAULT_ALIAS_TO_COMPARATOR_MAPPING.put((byte) 'l', LONGTYPE.getTypeName()); - DEFAULT_ALIAS_TO_COMPARATOR_MAPPING.put((byte) 't', - TIMEUUIDTYPE.getTypeName()); - DEFAULT_ALIAS_TO_COMPARATOR_MAPPING.put((byte) 's', UTF8TYPE.getTypeName()); - - } - - public static final Map DEFAULT_COMPARATOR_TO_ALIAS_MAPPING = new HashMap(); - - static { - DEFAULT_COMPARATOR_TO_ALIAS_MAPPING - .put(ASCIITYPE.getTypeName(), (byte) 'a'); - DEFAULT_COMPARATOR_TO_ALIAS_MAPPING - .put(BYTESTYPE.getTypeName(), (byte) 'b'); - DEFAULT_COMPARATOR_TO_ALIAS_MAPPING.put(INTEGERTYPE.getTypeName(), - (byte) 'i'); - DEFAULT_COMPARATOR_TO_ALIAS_MAPPING.put(LEXICALUUIDTYPE.getTypeName(), - (byte) 'x'); - DEFAULT_COMPARATOR_TO_ALIAS_MAPPING.put(LONGTYPE.getTypeName(), (byte) 'l'); - DEFAULT_COMPARATOR_TO_ALIAS_MAPPING.put(TIMEUUIDTYPE.getTypeName(), - (byte) 't'); - DEFAULT_COMPARATOR_TO_ALIAS_MAPPING.put(UTF8TYPE.getTypeName(), (byte) 's'); - - } - - Map, String> serializerToComparatorMapping = DEFAULT_SERIALIZER_TO_COMPARATOR_MAPPING; - - Map comparatorToSerializerMapping = DEFAULT_COMPARATOR_TO_SERIALIZER_MAPPING; - - Map aliasesToComparatorMapping = DEFAULT_ALIAS_TO_COMPARATOR_MAPPING; - - Map comparatorToAliasMapping = DEFAULT_COMPARATOR_TO_ALIAS_MAPPING; - - boolean autoDeserialize = true; - - boolean dynamic = true; - - List> serializersByPosition = null; - List comparatorsByPosition = null; - - public class Component { - final Serializer serializer; - final T value; - final String comparator; - final boolean inclusive; - - public Component(T value, Serializer serializer, String comparator, - boolean inclusive) { - this.serializer = serializer; - this.value = value; - this.comparator = comparator; - this.inclusive = inclusive; - } - - public Serializer getSerializer() { - return serializer; - } - - public T getValue() { - return value; - } - - public String getComparator() { - return comparator; - } - - public boolean isInclusive() { - return inclusive; - } - } - - List components = new ArrayList(); - - ByteBuffer serialized = null; +public class Composite extends AbstractComposite { public Composite() { - + super(false); } public Composite(Object... o) { - this.addAll(Arrays.asList(o)); - } - - public List getComponents() { - return components; - } - - public void setComponents(List components) { - serialized = null; - this.components = components; - } - - public Map, String> getSerializerToComparatorMapping() { - return serializerToComparatorMapping; - } - - public void setSerializerToComparatorMapping( - Map, String> serializerToComparatorMapping) { - serialized = null; - this.serializerToComparatorMapping = serializerToComparatorMapping; - } - - public Map getComparatorToSerializerMapping() { - return comparatorToSerializerMapping; - } - - public void setComparatorToSerializerMapping( - Map comparatorToSerializerMapping) { - serialized = null; - this.comparatorToSerializerMapping = comparatorToSerializerMapping; - } - - public Map getAliasesToComparatorMapping() { - return aliasesToComparatorMapping; - } - - public void setAliasesToComparatorMapping( - Map aliasesToComparatorMapping) { - serialized = null; - this.aliasesToComparatorMapping = aliasesToComparatorMapping; - } - - public Map getComparatorToAliasMapping() { - return comparatorToAliasMapping; - } - - public void setComparatorToAliasMapping( - Map comparatorToAliasMapping) { - serialized = null; - this.comparatorToAliasMapping = comparatorToAliasMapping; - } - - public boolean isAutoDeserialize() { - return autoDeserialize; - } - - public void setAutoDeserialize(boolean autoDeserialize) { - this.autoDeserialize = autoDeserialize; - } - - public boolean isDynamic() { - return dynamic; - } - - public void setDynamic(boolean dynamic) { - this.dynamic = dynamic; - } - - public List> getSerializersByPosition() { - return serializersByPosition; - } - - public void setSerializersByPosition(List> serializersByPosition) { - this.serializersByPosition = serializersByPosition; - } - - public void setSerializersByPosition(Serializer... serializers) { - serializersByPosition = Arrays.asList(serializers); - } - - public List getComparatorsByPosition() { - return comparatorsByPosition; - } - - public void setComparatorsByPosition(List comparatorsByPosition) { - this.comparatorsByPosition = comparatorsByPosition; - } - - public void setComparatorsByPosition(String... comparators) { - comparatorsByPosition = Arrays.asList(comparators); - } - - @Override - public int compareTo(Composite o) { - return serialize().compareTo(o.serialize()); - } - - private String comparatorForSerializer(Serializer s) { - String comparator = serializerToComparatorMapping.get(s.getClass()); - if (comparator != null) { - return comparator; - } - return BYTESTYPE.getTypeName(); - } - - private Serializer serializerForComparator(String c) { - Serializer s = comparatorToSerializerMapping.get(c); - if (s != null) { - return s; - } - return ByteBufferSerializer.get(); - } - - private Serializer serializerForPosition(int i) { - if (serializersByPosition == null) { - return null; - } - if (i >= serializersByPosition.size()) { - return null; - } - return serializersByPosition.get(i); - } - - private Serializer getSerializer(int i, String c) { - Serializer s = serializerForPosition(i); - if (s != null) { - return s; - } - return serializerForComparator(c); - } - - private String comparatorForPosition(int i) { - if (comparatorsByPosition == null) { - return null; - } - if (i >= comparatorsByPosition.size()) { - return null; - } - return comparatorsByPosition.get(i); - } - - private String getComparator(int i, ByteBuffer bb) { - String name = comparatorForPosition(i); - if (name != null) { - return name; - } - if (!dynamic) { - if (bb.hasRemaining()) { - return BYTESTYPE.getTypeName(); - } else { - return null; - } - } - if (bb.hasRemaining()) { - try { - int header = getShortLength(bb); - if ((header & 0x8000) == 0) { - name = ByteBufferUtil.string(getBytes(bb, header)); - } else { - name = aliasesToComparatorMapping.get((byte) (header & 0xFF)); - } - } catch (CharacterCodingException e) { - throw new RuntimeException(e); - } - } - if ((name != null) && (name.length() == 0)) { - name = null; - } - return name; - } - - public Composite add(T value, Serializer s) { - serialized = null; - - add(value, s, comparatorForSerializer(s)); - - return this; - - } - - public Composite add(T value, Serializer s, String comparator) { - serialized = null; - - add(value, s, comparator, false); - - return this; - - } - - @SuppressWarnings("unchecked") - public Composite add(T value, Serializer s, String comparator, - boolean inclusive) { - serialized = null; - - components.add(new Component(value, s, comparator, inclusive)); - - return this; - - } - - @Override - public void clear() { - serialized = null; - components = new ArrayList(); - } - - @Override - public int size() { - return components.size(); - } - - @SuppressWarnings("unchecked") - @Override - public void add(int index, Object element) { - serialized = null; - Serializer s = serializerForPosition(index); - if (s == null) { - s = SerializerTypeInferer.getSerializer(element); - } - components.add(index, new Component(element, s, comparatorForSerializer(s), - false)); - } - - @Override - public Object remove(int index) { - serialized = null; - Component prev = components.remove(index); - if (prev != null) { - return prev.getValue(); - } - return null; - } - - @SuppressWarnings("unchecked") - @Override - public Object set(int index, Object element) { - serialized = null; - Serializer s = SerializerTypeInferer.getSerializer(element); - Component prev = components.set(index, new Component(element, s, - comparatorForSerializer(s), false)); - if (prev != null) { - return prev.getValue(); - } - return null; - } - - @Override - public Object get(int i) { - Component c = components.get(i); - if (c != null) { - return c.getValue(); - } - return null; - } - - public T get(int i, Serializer s) throws ClassCastException { - Component c = components.get(i); - if (c != null) { - return s.fromByteBuffer((ByteBuffer) c.getValue()); - } - return null; - } - - public Component getComponent(int i) { - if (i >= components.size()) { - return null; - } - Component c = components.get(i); - return c; - } - - public Iterator componentsIterator() { - return components.iterator(); - } - - @SuppressWarnings("unchecked") - public ByteBuffer serialize() { - if (serialized != null) { - return serialized.duplicate(); - } - - ByteBufferOutputStream out = new ByteBufferOutputStream(); - - int i = 0; - for (Component c : components) { - Serializer s = serializerForPosition(i); - if (s == null) { - s = c.getSerializer(); - } - ByteBuffer cb = s.toByteBuffer(c.getValue()); - - if (dynamic) { - String comparator = comparatorForPosition(i); - if (comparator == null) { - comparator = c.getComparator(); - } - if (comparatorToAliasMapping.containsKey(comparator)) { - out.writeShort((short) (0x8000 | comparatorToAliasMapping - .get(comparator))); - } else { - out.writeShort((short) comparator.length()); - out.write(ByteBufferUtil.bytes(comparator)); - } - } - out.writeShort((short) cb.remaining()); - out.write(cb.slice()); - out.write(c.isInclusive() ? 1 : 0); - i++; - } - - serialized = out.getByteBuffer(); - return serialized.duplicate(); - } - - @SuppressWarnings("unchecked") - public void deserialize(ByteBuffer b) { - serialized = b.duplicate(); - components = new ArrayList(); - - String comparator = null; - int i = 0; - while ((comparator = getComparator(i, b)) != null) { - ByteBuffer data = getWithShortLength(b); - if (data != null) { - Serializer s = autoDeserialize ? getSerializer(i, comparator) - : ByteBufferSerializer.get(); - Object value = s.fromByteBuffer(data); - boolean inclusive = b.get() != 0; - components.add(new Component(value, s, comparator, inclusive)); - } else { - throw new RuntimeException("Missing component data in composite type"); - } - i++; - } - - } - - protected static int getShortLength(ByteBuffer bb) { - int length = (bb.get() & 0xFF) << 8; - return length | (bb.get() & 0xFF); - } - - protected static ByteBuffer getBytes(ByteBuffer bb, int length) { - ByteBuffer copy = bb.duplicate(); - copy.limit(copy.position() + length); - bb.position(bb.position() + length); - return copy; - } - - protected static ByteBuffer getWithShortLength(ByteBuffer bb) { - int length = getShortLength(bb); - return getBytes(bb, length); + super(false, o); } } diff --git a/core/src/main/java/me/prettyprint/hector/api/beans/DynamicComposite.java b/core/src/main/java/me/prettyprint/hector/api/beans/DynamicComposite.java new file mode 100644 index 000000000..21941269d --- /dev/null +++ b/core/src/main/java/me/prettyprint/hector/api/beans/DynamicComposite.java @@ -0,0 +1,13 @@ +package me.prettyprint.hector.api.beans; + +public class DynamicComposite extends AbstractComposite { + + public DynamicComposite() { + super(true); + } + + public DynamicComposite(Object... o) { + super(true, o); + } + +} diff --git a/core/src/test/java/me/prettyprint/hector/api/CompositeTest.java b/core/src/test/java/me/prettyprint/hector/api/CompositeTest.java index 553b3e0a5..d8b518b25 100644 --- a/core/src/test/java/me/prettyprint/hector/api/CompositeTest.java +++ b/core/src/test/java/me/prettyprint/hector/api/CompositeTest.java @@ -13,6 +13,7 @@ import me.prettyprint.cassandra.serializers.UUIDSerializer; import me.prettyprint.cassandra.utils.TimeUUIDUtils; import me.prettyprint.hector.api.beans.Composite; +import me.prettyprint.hector.api.beans.DynamicComposite; import org.apache.cassandra.utils.ByteBufferUtil; import org.apache.cassandra.utils.UUIDGen; @@ -21,9 +22,9 @@ public class CompositeTest { @Test - public void testSerialization() throws Exception { + public void testDynamicSerialization() throws Exception { - Composite c = new Composite(); + DynamicComposite c = new DynamicComposite(); c.add("String1"); ByteBuffer b = c.serialize(); assertEquals(b.remaining(), 12); @@ -32,7 +33,7 @@ public void testSerialization() throws Exception { b = c.serialize(); assertEquals(b.remaining(), 24); - c = new Composite(); + c = new DynamicComposite(); c.deserialize(b); assertEquals(2, c.size()); Object o = c.get(0); @@ -40,17 +41,17 @@ public void testSerialization() throws Exception { o = c.get(1); assertEquals("String2", o); - c = new Composite(); + c = new DynamicComposite(); c.add(new Long(10)); b = c.serialize(); - c = new Composite(); + c = new DynamicComposite(); c.deserialize(b); o = c.get(0); assertTrue(o instanceof Long); b = createDynamicCompositeKey("Hello", TimeUUIDUtils.getUniqueTimeUUIDinMillis(), 10, false); - c = new Composite(); + c = new DynamicComposite(); c.deserialize(b.slice()); o = c.get(0); assertTrue(o instanceof ByteBuffer); @@ -63,14 +64,14 @@ public void testSerialization() throws Exception { assertEquals(BigInteger.class, o.getClass()); assertEquals(BigInteger.valueOf(10), o); - c = new Composite(); + c = new DynamicComposite(); c.setAutoDeserialize(false); c.deserialize(b.slice()); assertTrue(c.get(0) instanceof ByteBuffer); assertTrue(c.get(1) instanceof ByteBuffer); assertTrue(c.get(2) instanceof ByteBuffer); - c = new Composite(); + c = new DynamicComposite(); c.setSerializersByPosition(StringSerializer.get(), null, ByteBufferSerializer.get()); c.deserialize(b.slice()); @@ -78,10 +79,14 @@ public void testSerialization() throws Exception { assertTrue(c.get(1) instanceof UUID); assertTrue(c.get(2) instanceof ByteBuffer); - b = createCompositeKey("Hello", TimeUUIDUtils.getUniqueTimeUUIDinMillis(), - 10, false); - c = new Composite(); - c.setDynamic(false); + } + + @Test + public void testStaticSerialization() throws Exception { + + ByteBuffer b = createCompositeKey("Hello", + TimeUUIDUtils.getUniqueTimeUUIDinMillis(), 10, false); + Composite c = new Composite(); c.setSerializersByPosition(StringSerializer.get(), UUIDSerializer.get(), BigIntegerSerializer.get()); c.deserialize(b.slice()); From 269b10bfe0100ec99f5ad3356761fe71b3a6950d Mon Sep 17 00:00:00 2001 From: Ed Anuff Date: Wed, 23 Mar 2011 13:07:47 -0700 Subject: [PATCH 042/144] simplified alias mappings by using a BiMap from Google Collections --- .../hector/api/beans/AbstractComposite.java | 83 +++++-------------- 1 file changed, 21 insertions(+), 62 deletions(-) diff --git a/core/src/main/java/me/prettyprint/hector/api/beans/AbstractComposite.java b/core/src/main/java/me/prettyprint/hector/api/beans/AbstractComposite.java index 2f386d7b1..67eab1141 100644 --- a/core/src/main/java/me/prettyprint/hector/api/beans/AbstractComposite.java +++ b/core/src/main/java/me/prettyprint/hector/api/beans/AbstractComposite.java @@ -34,6 +34,9 @@ import org.apache.cassandra.utils.ByteBufferUtil; +import com.google.common.collect.BiMap; +import com.google.common.collect.ImmutableBiMap; + @SuppressWarnings("rawtypes") public abstract class AbstractComposite extends AbstractList implements Comparable { @@ -75,10 +78,12 @@ public abstract class AbstractComposite extends AbstractList implements ByteBufferSerializer.get()); DEFAULT_COMPARATOR_TO_SERIALIZER_MAPPING.put(INTEGERTYPE.getTypeName(), BigIntegerSerializer.get()); + // Can't use a BiMap because either UUIDType maps to the same serializer DEFAULT_COMPARATOR_TO_SERIALIZER_MAPPING.put(LEXICALUUIDTYPE.getTypeName(), UUIDSerializer.get()); DEFAULT_COMPARATOR_TO_SERIALIZER_MAPPING.put(LONGTYPE.getTypeName(), LongSerializer.get()); + // Can't use a BiMap because either UUIDType maps to the same serializer DEFAULT_COMPARATOR_TO_SERIALIZER_MAPPING.put(TIMEUUIDTYPE.getTypeName(), UUIDSerializer.get()); DEFAULT_COMPARATOR_TO_SERIALIZER_MAPPING.put(UTF8TYPE.getTypeName(), @@ -86,53 +91,24 @@ public abstract class AbstractComposite extends AbstractList implements } - public static final Map DEFAULT_ALIAS_TO_COMPARATOR_MAPPING = new HashMap(); - - static { - DEFAULT_ALIAS_TO_COMPARATOR_MAPPING - .put((byte) 'a', ASCIITYPE.getTypeName()); - DEFAULT_ALIAS_TO_COMPARATOR_MAPPING - .put((byte) 'b', BYTESTYPE.getTypeName()); - DEFAULT_ALIAS_TO_COMPARATOR_MAPPING.put((byte) 'i', - INTEGERTYPE.getTypeName()); - DEFAULT_ALIAS_TO_COMPARATOR_MAPPING.put((byte) 'x', - LEXICALUUIDTYPE.getTypeName()); - DEFAULT_ALIAS_TO_COMPARATOR_MAPPING.put((byte) 'l', LONGTYPE.getTypeName()); - DEFAULT_ALIAS_TO_COMPARATOR_MAPPING.put((byte) 't', - TIMEUUIDTYPE.getTypeName()); - DEFAULT_ALIAS_TO_COMPARATOR_MAPPING.put((byte) 's', UTF8TYPE.getTypeName()); - - } - - public static final Map DEFAULT_COMPARATOR_TO_ALIAS_MAPPING = new HashMap(); - - static { - DEFAULT_COMPARATOR_TO_ALIAS_MAPPING - .put(ASCIITYPE.getTypeName(), (byte) 'a'); - DEFAULT_COMPARATOR_TO_ALIAS_MAPPING - .put(BYTESTYPE.getTypeName(), (byte) 'b'); - DEFAULT_COMPARATOR_TO_ALIAS_MAPPING.put(INTEGERTYPE.getTypeName(), - (byte) 'i'); - DEFAULT_COMPARATOR_TO_ALIAS_MAPPING.put(LEXICALUUIDTYPE.getTypeName(), - (byte) 'x'); - DEFAULT_COMPARATOR_TO_ALIAS_MAPPING.put(LONGTYPE.getTypeName(), (byte) 'l'); - DEFAULT_COMPARATOR_TO_ALIAS_MAPPING.put(TIMEUUIDTYPE.getTypeName(), - (byte) 't'); - DEFAULT_COMPARATOR_TO_ALIAS_MAPPING.put(UTF8TYPE.getTypeName(), (byte) 's'); - - } + public static final BiMap DEFAULT_ALIAS_TO_COMPARATOR_MAPPING = new ImmutableBiMap.Builder() + .put((byte) 'a', ASCIITYPE.getTypeName()) + .put((byte) 'b', BYTESTYPE.getTypeName()) + .put((byte) 'i', INTEGERTYPE.getTypeName()) + .put((byte) 'x', LEXICALUUIDTYPE.getTypeName()) + .put((byte) 'l', LONGTYPE.getTypeName()) + .put((byte) 't', TIMEUUIDTYPE.getTypeName()) + .put((byte) 's', UTF8TYPE.getTypeName()).build(); Map, String> serializerToComparatorMapping = DEFAULT_SERIALIZER_TO_COMPARATOR_MAPPING; Map comparatorToSerializerMapping = DEFAULT_COMPARATOR_TO_SERIALIZER_MAPPING; - Map aliasesToComparatorMapping = DEFAULT_ALIAS_TO_COMPARATOR_MAPPING; - - Map comparatorToAliasMapping = DEFAULT_COMPARATOR_TO_ALIAS_MAPPING; + BiMap aliasToComparatorMapping = DEFAULT_ALIAS_TO_COMPARATOR_MAPPING; boolean autoDeserialize = true; - boolean dynamic = false; + final boolean dynamic; List> serializersByPosition = null; List comparatorsByPosition = null; @@ -172,18 +148,10 @@ public boolean isInclusive() { ByteBuffer serialized = null; - public AbstractComposite() { - - } - public AbstractComposite(boolean dynamic) { this.dynamic = dynamic; } - public AbstractComposite(Object... o) { - this.addAll(Arrays.asList(o)); - } - public AbstractComposite(boolean dynamic, Object... o) { this.dynamic = dynamic; this.addAll(Arrays.asList(o)); @@ -219,23 +187,14 @@ public void setComparatorToSerializerMapping( } public Map getAliasesToComparatorMapping() { - return aliasesToComparatorMapping; + return aliasToComparatorMapping; } public void setAliasesToComparatorMapping( Map aliasesToComparatorMapping) { serialized = null; - this.aliasesToComparatorMapping = aliasesToComparatorMapping; - } - - public Map getComparatorToAliasMapping() { - return comparatorToAliasMapping; - } - - public void setComparatorToAliasMapping( - Map comparatorToAliasMapping) { - serialized = null; - this.comparatorToAliasMapping = comparatorToAliasMapping; + this.aliasToComparatorMapping = new ImmutableBiMap.Builder() + .putAll(aliasesToComparatorMapping).build(); } public boolean isAutoDeserialize() { @@ -341,7 +300,7 @@ private String getComparator(int i, ByteBuffer bb) { if ((header & 0x8000) == 0) { name = ByteBufferUtil.string(getBytes(bb, header)); } else { - name = aliasesToComparatorMapping.get((byte) (header & 0xFF)); + name = aliasToComparatorMapping.get((byte) (header & 0xFF)); } } catch (CharacterCodingException e) { throw new RuntimeException(e); @@ -478,8 +437,8 @@ public ByteBuffer serialize() { if (comparator == null) { comparator = c.getComparator(); } - if (comparatorToAliasMapping.containsKey(comparator)) { - out.writeShort((short) (0x8000 | comparatorToAliasMapping + if (aliasToComparatorMapping.inverse().containsKey(comparator)) { + out.writeShort((short) (0x8000 | aliasToComparatorMapping.inverse() .get(comparator))); } else { out.writeShort((short) comparator.length()); From 926778a35feabf4f9be51e0501b1a691a42f7da8 Mon Sep 17 00:00:00 2001 From: Ed Anuff Date: Wed, 23 Mar 2011 13:45:47 -0700 Subject: [PATCH 043/144] Cleaned mappings by using BiMap --- .../hector/api/beans/AbstractComposite.java | 125 ++++++++---------- 1 file changed, 53 insertions(+), 72 deletions(-) diff --git a/core/src/main/java/me/prettyprint/hector/api/beans/AbstractComposite.java b/core/src/main/java/me/prettyprint/hector/api/beans/AbstractComposite.java index 67eab1141..85b5da053 100644 --- a/core/src/main/java/me/prettyprint/hector/api/beans/AbstractComposite.java +++ b/core/src/main/java/me/prettyprint/hector/api/beans/AbstractComposite.java @@ -13,20 +13,17 @@ import java.util.AbstractList; import java.util.ArrayList; import java.util.Arrays; -import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.Map; +import java.util.UUID; import java.util.logging.Logger; import me.prettyprint.cassandra.serializers.AsciiSerializer; import me.prettyprint.cassandra.serializers.BigIntegerSerializer; -import me.prettyprint.cassandra.serializers.BooleanSerializer; import me.prettyprint.cassandra.serializers.ByteBufferSerializer; -import me.prettyprint.cassandra.serializers.BytesArraySerializer; import me.prettyprint.cassandra.serializers.LongSerializer; import me.prettyprint.cassandra.serializers.SerializerTypeInferer; -import me.prettyprint.cassandra.serializers.ShortSerializer; import me.prettyprint.cassandra.serializers.StringSerializer; import me.prettyprint.cassandra.serializers.UUIDSerializer; import me.prettyprint.cassandra.utils.ByteBufferOutputStream; @@ -36,6 +33,7 @@ import com.google.common.collect.BiMap; import com.google.common.collect.ImmutableBiMap; +import com.google.common.collect.ImmutableClassToInstanceMap; @SuppressWarnings("rawtypes") public abstract class AbstractComposite extends AbstractList implements @@ -44,52 +42,21 @@ public abstract class AbstractComposite extends AbstractList implements static final Logger logger = Logger.getLogger(AbstractComposite.class .getName()); - public static final Map, String> DEFAULT_SERIALIZER_TO_COMPARATOR_MAPPING; - - static { - DEFAULT_SERIALIZER_TO_COMPARATOR_MAPPING = new HashMap, String>(); - DEFAULT_SERIALIZER_TO_COMPARATOR_MAPPING.put(AsciiSerializer.class, - ASCIITYPE.getTypeName()); - DEFAULT_SERIALIZER_TO_COMPARATOR_MAPPING.put(BigIntegerSerializer.class, - INTEGERTYPE.getTypeName()); - DEFAULT_SERIALIZER_TO_COMPARATOR_MAPPING.put(BooleanSerializer.class, - BYTESTYPE.getTypeName()); - DEFAULT_SERIALIZER_TO_COMPARATOR_MAPPING.put(ByteBufferSerializer.class, - BYTESTYPE.getTypeName()); - DEFAULT_SERIALIZER_TO_COMPARATOR_MAPPING.put(BytesArraySerializer.class, - BYTESTYPE.getTypeName()); - DEFAULT_SERIALIZER_TO_COMPARATOR_MAPPING.put(LongSerializer.class, - LONGTYPE.getTypeName()); - DEFAULT_SERIALIZER_TO_COMPARATOR_MAPPING.put(ShortSerializer.class, - BYTESTYPE.getTypeName()); - DEFAULT_SERIALIZER_TO_COMPARATOR_MAPPING.put(StringSerializer.class, - UTF8TYPE.getTypeName()); - // TODO this doesn't work, there isn't a unified UUIDType yet - DEFAULT_SERIALIZER_TO_COMPARATOR_MAPPING.put(UUIDSerializer.class, - "UUIDType"); - } - - public static final Map DEFAULT_COMPARATOR_TO_SERIALIZER_MAPPING = new HashMap(); - - static { - DEFAULT_COMPARATOR_TO_SERIALIZER_MAPPING.put(ASCIITYPE.getTypeName(), - AsciiSerializer.get()); - DEFAULT_COMPARATOR_TO_SERIALIZER_MAPPING.put(BYTESTYPE.getTypeName(), - ByteBufferSerializer.get()); - DEFAULT_COMPARATOR_TO_SERIALIZER_MAPPING.put(INTEGERTYPE.getTypeName(), - BigIntegerSerializer.get()); - // Can't use a BiMap because either UUIDType maps to the same serializer - DEFAULT_COMPARATOR_TO_SERIALIZER_MAPPING.put(LEXICALUUIDTYPE.getTypeName(), - UUIDSerializer.get()); - DEFAULT_COMPARATOR_TO_SERIALIZER_MAPPING.put(LONGTYPE.getTypeName(), - LongSerializer.get()); - // Can't use a BiMap because either UUIDType maps to the same serializer - DEFAULT_COMPARATOR_TO_SERIALIZER_MAPPING.put(TIMEUUIDTYPE.getTypeName(), - UUIDSerializer.get()); - DEFAULT_COMPARATOR_TO_SERIALIZER_MAPPING.put(UTF8TYPE.getTypeName(), - StringSerializer.get()); - - } + public static final BiMap, String> DEFAULT_SERIALIZER_TO_COMPARATOR_MAPPING = new ImmutableBiMap.Builder, String>() + .put(AsciiSerializer.class, ASCIITYPE.getTypeName()) + .put(BigIntegerSerializer.class, INTEGERTYPE.getTypeName()) + .put(ByteBufferSerializer.class, BYTESTYPE.getTypeName()) + .put(LongSerializer.class, LONGTYPE.getTypeName()) + .put(StringSerializer.class, UTF8TYPE.getTypeName()) + .put(UUIDSerializer.class, "UUIDType").build(); + + static final ImmutableClassToInstanceMap SERIALIZERS = new ImmutableClassToInstanceMap.Builder() + .put(AsciiSerializer.class, AsciiSerializer.get()) + .put(BigIntegerSerializer.class, BigIntegerSerializer.get()) + .put(ByteBufferSerializer.class, ByteBufferSerializer.get()) + .put(LongSerializer.class, LongSerializer.get()) + .put(StringSerializer.class, StringSerializer.get()) + .put(UUIDSerializer.class, UUIDSerializer.get()).build(); public static final BiMap DEFAULT_ALIAS_TO_COMPARATOR_MAPPING = new ImmutableBiMap.Builder() .put((byte) 'a', ASCIITYPE.getTypeName()) @@ -100,9 +67,7 @@ public abstract class AbstractComposite extends AbstractList implements .put((byte) 't', TIMEUUIDTYPE.getTypeName()) .put((byte) 's', UTF8TYPE.getTypeName()).build(); - Map, String> serializerToComparatorMapping = DEFAULT_SERIALIZER_TO_COMPARATOR_MAPPING; - - Map comparatorToSerializerMapping = DEFAULT_COMPARATOR_TO_SERIALIZER_MAPPING; + BiMap, String> serializerToComparatorMapping = DEFAULT_SERIALIZER_TO_COMPARATOR_MAPPING; BiMap aliasToComparatorMapping = DEFAULT_ALIAS_TO_COMPARATOR_MAPPING; @@ -173,17 +138,8 @@ public Map, String> getSerializerToComparatorMapping public void setSerializerToComparatorMapping( Map, String> serializerToComparatorMapping) { serialized = null; - this.serializerToComparatorMapping = serializerToComparatorMapping; - } - - public Map getComparatorToSerializerMapping() { - return comparatorToSerializerMapping; - } - - public void setComparatorToSerializerMapping( - Map comparatorToSerializerMapping) { - serialized = null; - this.comparatorToSerializerMapping = comparatorToSerializerMapping; + this.serializerToComparatorMapping = new ImmutableBiMap.Builder, String>() + .putAll(serializerToComparatorMapping).build(); } public Map getAliasesToComparatorMapping() { @@ -193,7 +149,7 @@ public Map getAliasesToComparatorMapping() { public void setAliasesToComparatorMapping( Map aliasesToComparatorMapping) { serialized = null; - this.aliasToComparatorMapping = new ImmutableBiMap.Builder() + aliasToComparatorMapping = new ImmutableBiMap.Builder() .putAll(aliasesToComparatorMapping).build(); } @@ -246,8 +202,21 @@ private String comparatorForSerializer(Serializer s) { return BYTESTYPE.getTypeName(); } + private String comparatorForUUID(UUID uuid) { + if (uuid.version() == 1) { + return TIMEUUIDTYPE.getTypeName(); + } + return LEXICALUUIDTYPE.getTypeName(); + } + private Serializer serializerForComparator(String c) { - Serializer s = comparatorToSerializerMapping.get(c); + if (LEXICALUUIDTYPE.getTypeName().equals(c) + || TIMEUUIDTYPE.getTypeName().equals(c)) { + return UUIDSerializer.get(); + } + + Serializer s = SERIALIZERS.getInstance(serializerToComparatorMapping + .inverse().get(c)); if (s != null) { return s; } @@ -315,7 +284,8 @@ private String getComparator(int i, ByteBuffer bb) { public AbstractComposite add(T value, Serializer s) { serialized = null; - add(value, s, comparatorForSerializer(s)); + add(value, s, value instanceof UUID ? comparatorForUUID((UUID) value) + : comparatorForSerializer(s)); return this; @@ -360,8 +330,12 @@ public void add(int index, Object element) { if (s == null) { s = SerializerTypeInferer.getSerializer(element); } - components.add(index, new Component(element, s, comparatorForSerializer(s), - false)); + String c = comparatorForPosition(index); + if (c == null) { + c = element instanceof UUID ? comparatorForUUID((UUID) element) + : comparatorForSerializer(s); + } + components.add(index, new Component(element, s, c, false)); } @Override @@ -378,9 +352,16 @@ public Object remove(int index) { @Override public Object set(int index, Object element) { serialized = null; - Serializer s = SerializerTypeInferer.getSerializer(element); - Component prev = components.set(index, new Component(element, s, - comparatorForSerializer(s), false)); + Serializer s = serializerForPosition(index); + if (s == null) { + s = SerializerTypeInferer.getSerializer(element); + } + String c = comparatorForPosition(index); + if (c == null) { + c = element instanceof UUID ? comparatorForUUID((UUID) element) + : comparatorForSerializer(s); + } + Component prev = components.set(index, new Component(element, s, c, false)); if (prev != null) { return prev.getValue(); } From baa374be36f0ae974fe033704e9f1ec6123ed66d Mon Sep 17 00:00:00 2001 From: zznate Date: Wed, 23 Mar 2011 16:56:53 -0500 Subject: [PATCH 044/144] added fix to borked iface --- .../cassandra/service/HColumnFamilyImpl.java | 7 ++++++- .../AbstractColumnFamilyTemplate.java | 13 ++++++------- .../template/AbstractResultWrapper.java | 19 +++++++++++++++++++ .../template/ColumnFamilyResultWrapper.java | 11 ----------- .../prettyprint/hector/api/ResultStatus.java | 2 ++ 5 files changed, 33 insertions(+), 19 deletions(-) diff --git a/core/src/main/java/me/prettyprint/cassandra/service/HColumnFamilyImpl.java b/core/src/main/java/me/prettyprint/cassandra/service/HColumnFamilyImpl.java index ddcc0a4d8..1f8b94c01 100644 --- a/core/src/main/java/me/prettyprint/cassandra/service/HColumnFamilyImpl.java +++ b/core/src/main/java/me/prettyprint/cassandra/service/HColumnFamilyImpl.java @@ -237,7 +237,7 @@ public HColumnFamily setWriteConsistencyLevel(HConsistencyLevel writeLevel @Override public long getExecutionTimeMicro() { - return lastExecutionTime; + return lastExecutionTime / 1000; } @Override @@ -351,4 +351,9 @@ public Column execute(Cassandra.Client cassandra) throws HectorException { applyToRow(_keys.get(0), rows.get(keySerializer.toByteBuffer(_keys.get(0)))); } + @Override + public long getExecutionTimeNano() { + return lastExecutionTime; + } + } diff --git a/core/src/main/java/me/prettyprint/cassandra/service/template/AbstractColumnFamilyTemplate.java b/core/src/main/java/me/prettyprint/cassandra/service/template/AbstractColumnFamilyTemplate.java index 144f86ae2..9365dad6e 100644 --- a/core/src/main/java/me/prettyprint/cassandra/service/template/AbstractColumnFamilyTemplate.java +++ b/core/src/main/java/me/prettyprint/cassandra/service/template/AbstractColumnFamilyTemplate.java @@ -118,8 +118,10 @@ public Serializer getTopSerializer() { return topSerializer; } - public MutationResult executeBatch() { - return mutator.execute(); + public MutationResult executeBatch() { + MutationResult result = mutator.execute(); + mutator.discardPendingMutations(); + return result; } public Mutator getMutator() { @@ -147,11 +149,8 @@ public void setExceptionsTranslator(ExceptionsTranslator exceptionsTranslator) { this.exceptionsTranslator = exceptionsTranslator; } - protected MutationResult executeIfNotBatched() { - if (!isBatched()) { - return mutator.execute(); - } - return null; + protected MutationResult executeIfNotBatched() { + return isBatched() ? executeBatch() : null; } public void deleteRow(K key) { diff --git a/core/src/main/java/me/prettyprint/cassandra/service/template/AbstractResultWrapper.java b/core/src/main/java/me/prettyprint/cassandra/service/template/AbstractResultWrapper.java index 2fa4abeb8..7e5e46f0e 100644 --- a/core/src/main/java/me/prettyprint/cassandra/service/template/AbstractResultWrapper.java +++ b/core/src/main/java/me/prettyprint/cassandra/service/template/AbstractResultWrapper.java @@ -11,6 +11,7 @@ import me.prettyprint.cassandra.serializers.LongSerializer; import me.prettyprint.cassandra.serializers.StringSerializer; import me.prettyprint.cassandra.serializers.UUIDSerializer; +import me.prettyprint.cassandra.service.CassandraHost; import me.prettyprint.hector.api.Serializer; /** @@ -72,4 +73,22 @@ public byte[] getByteArray(N columnName) { public Date getDate(N columnName) { return DateSerializer.get().fromByteBuffer(getColumnValue(columnName)); } + + @Override + public long getExecutionTimeMicro() { + // TODO Auto-generated method stub + return 0; + } + + @Override + public long getExecutionTimeNano() { + // TODO Auto-generated method stub + return 0; + } + + @Override + public CassandraHost getHostUsed() { + // TODO Auto-generated method stub + return null; + } } diff --git a/core/src/main/java/me/prettyprint/cassandra/service/template/ColumnFamilyResultWrapper.java b/core/src/main/java/me/prettyprint/cassandra/service/template/ColumnFamilyResultWrapper.java index 4b257b078..85c44d60b 100644 --- a/core/src/main/java/me/prettyprint/cassandra/service/template/ColumnFamilyResultWrapper.java +++ b/core/src/main/java/me/prettyprint/cassandra/service/template/ColumnFamilyResultWrapper.java @@ -90,15 +90,4 @@ public void remove() { rows.remove(); } - @Override - public long getExecutionTimeMicro() { - // TODO Auto-generated method stub - return 0; - } - - @Override - public CassandraHost getHostUsed() { - // TODO Auto-generated method stub - return null; - } } diff --git a/core/src/main/java/me/prettyprint/hector/api/ResultStatus.java b/core/src/main/java/me/prettyprint/hector/api/ResultStatus.java index cdab103c3..1b562abf5 100644 --- a/core/src/main/java/me/prettyprint/hector/api/ResultStatus.java +++ b/core/src/main/java/me/prettyprint/hector/api/ResultStatus.java @@ -18,6 +18,8 @@ public interface ResultStatus { * @return */ long getExecutionTimeMicro(); + + long getExecutionTimeNano(); /** * The {@link CassandraHost} on which this operation From 73fd63c75dead9695bd43041ff6e9c9b6985fcbd Mon Sep 17 00:00:00 2001 From: zznate Date: Wed, 23 Mar 2011 18:02:45 -0500 Subject: [PATCH 045/144] forgot to not --- .../service/template/AbstractColumnFamilyTemplate.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/src/main/java/me/prettyprint/cassandra/service/template/AbstractColumnFamilyTemplate.java b/core/src/main/java/me/prettyprint/cassandra/service/template/AbstractColumnFamilyTemplate.java index 9365dad6e..30b188392 100644 --- a/core/src/main/java/me/prettyprint/cassandra/service/template/AbstractColumnFamilyTemplate.java +++ b/core/src/main/java/me/prettyprint/cassandra/service/template/AbstractColumnFamilyTemplate.java @@ -150,7 +150,7 @@ public void setExceptionsTranslator(ExceptionsTranslator exceptionsTranslator) { } protected MutationResult executeIfNotBatched() { - return isBatched() ? executeBatch() : null; + return !isBatched() ? executeBatch() : null; } public void deleteRow(K key) { From 3a434cb96d6bf695b04add561b010d9556444393 Mon Sep 17 00:00:00 2001 From: zznate Date: Wed, 23 Mar 2011 18:06:38 -0500 Subject: [PATCH 046/144] added null check on comparator --- .../main/java/me/prettyprint/hector/api/ddl/ComparatorType.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/src/main/java/me/prettyprint/hector/api/ddl/ComparatorType.java b/core/src/main/java/me/prettyprint/hector/api/ddl/ComparatorType.java index 7567f47fc..6db969717 100644 --- a/core/src/main/java/me/prettyprint/hector/api/ddl/ComparatorType.java +++ b/core/src/main/java/me/prettyprint/hector/api/ddl/ComparatorType.java @@ -47,7 +47,7 @@ public String getTypeName() { } public static ComparatorType getByClassName(String className) { - + if ( className == null ) return null; for (int a = 0; a < values.length; a++) { ComparatorType type = values[a]; if (type.getClassName().equals(className)) { From f22818d69b3d13c1872bc150ab0b2f843fb43a12 Mon Sep 17 00:00:00 2001 From: zznate Date: Wed, 23 Mar 2011 22:12:56 -0500 Subject: [PATCH 047/144] additional cleanup of cftemplate classes, particularly with ResultStatus info --- .../cassandra/model/ExecutionResult.java | 3 +- .../template/AbstractResultWrapper.java | 25 ++++++++------- .../service/template/ColumnFamilyResult.java | 23 ++++++------- .../template/ColumnFamilyResultWrapper.java | 8 +++-- .../template/ColumnFamilyTemplate.java | 32 ++++++++----------- .../MappedColumnFamilyResultWrapper.java | 5 +-- .../template/SuperCfResultWrapper.java | 8 +++-- 7 files changed, 55 insertions(+), 49 deletions(-) diff --git a/core/src/main/java/me/prettyprint/cassandra/model/ExecutionResult.java b/core/src/main/java/me/prettyprint/cassandra/model/ExecutionResult.java index 22a76bb4f..4cba4e828 100644 --- a/core/src/main/java/me/prettyprint/cassandra/model/ExecutionResult.java +++ b/core/src/main/java/me/prettyprint/cassandra/model/ExecutionResult.java @@ -1,6 +1,7 @@ package me.prettyprint.cassandra.model; import me.prettyprint.cassandra.service.CassandraHost; +import me.prettyprint.hector.api.ResultStatus; /** @@ -11,7 +12,7 @@ * @author Ran * @author zznate */ -public class ExecutionResult { +public class ExecutionResult implements ResultStatus { private final T value; private final long execTime; diff --git a/core/src/main/java/me/prettyprint/cassandra/service/template/AbstractResultWrapper.java b/core/src/main/java/me/prettyprint/cassandra/service/template/AbstractResultWrapper.java index 7e5e46f0e..f7aba13e0 100644 --- a/core/src/main/java/me/prettyprint/cassandra/service/template/AbstractResultWrapper.java +++ b/core/src/main/java/me/prettyprint/cassandra/service/template/AbstractResultWrapper.java @@ -12,6 +12,7 @@ import me.prettyprint.cassandra.serializers.StringSerializer; import me.prettyprint.cassandra.serializers.UUIDSerializer; import me.prettyprint.cassandra.service.CassandraHost; +import me.prettyprint.hector.api.ResultStatus; import me.prettyprint.hector.api.Serializer; /** @@ -21,14 +22,16 @@ * a primitive type when requested. * * This class is a non-static inner class which inherits the Java generic - * parameters of it's containing CassandraTemplate instance. This allows it to - * inherit the parameter from CassandraTemplate. + * parameters of it's containing ColumnFamilyTemplate instance. This allows it to + * inherit the parameter from ColumnFamilyTemplate. * - * The parameters allows this to be used by standard and super column + * The parameters allows this to be used by standard and super column * queries * * @author david - * @since Mar 10, 2011 + * @author zznate + * @param + * the type of the key * @param * the standard column name type or the super column's child column * type @@ -37,10 +40,12 @@ abstract class AbstractResultWrapper implements ColumnFamilyResult { protected Serializer keySerializer; protected Serializer columnNameSerializer; + protected ResultStatus resultStatus; - public AbstractResultWrapper(Serializer keySerializer, Serializer columnNameSerializer) { + public AbstractResultWrapper(Serializer keySerializer, Serializer columnNameSerializer, ResultStatus resultStatus) { this.keySerializer = keySerializer; this.columnNameSerializer = columnNameSerializer; + this.resultStatus = resultStatus; } public abstract ByteBuffer getColumnValue(N columnName); @@ -76,19 +81,17 @@ public Date getDate(N columnName) { @Override public long getExecutionTimeMicro() { - // TODO Auto-generated method stub - return 0; + return resultStatus.getExecutionTimeMicro(); } @Override public long getExecutionTimeNano() { - // TODO Auto-generated method stub - return 0; + return resultStatus.getExecutionTimeNano(); } @Override public CassandraHost getHostUsed() { - // TODO Auto-generated method stub - return null; + return resultStatus.getHostUsed(); } + } diff --git a/core/src/main/java/me/prettyprint/cassandra/service/template/ColumnFamilyResult.java b/core/src/main/java/me/prettyprint/cassandra/service/template/ColumnFamilyResult.java index 59fea6016..b21e35c6b 100644 --- a/core/src/main/java/me/prettyprint/cassandra/service/template/ColumnFamilyResult.java +++ b/core/src/main/java/me/prettyprint/cassandra/service/template/ColumnFamilyResult.java @@ -7,30 +7,31 @@ import me.prettyprint.hector.api.ResultStatus; /** - * A commen interface for access to the resuls of a query of either a standard or super column family. + * A common interface for access to the resuls of a query of either a standard or super column family. * There are different implementations of this which hide the differences requires of standar/super - * column families. + * column families. As this interface inherits from {@link ResultStatus}, results will also provide + * execution details. * * @author david - * @since Mar 10, 2011 + * @author zznate * @param * @param */ public interface ColumnFamilyResult extends Iterator>,ResultStatus { - public K getKey(); + K getKey(); - public UUID getUUID(N columnName); + UUID getUUID(N columnName); - public String getString(N columnName); + String getString(N columnName); - public Long getLong(N columnName); + Long getLong(N columnName); - public Integer getInteger(N columnName); + Integer getInteger(N columnName); - public Boolean getBoolean(N columnName); + Boolean getBoolean(N columnName); - public byte[] getByteArray(N columnName); + byte[] getByteArray(N columnName); - public Date getDate(N columnName); + Date getDate(N columnName); } diff --git a/core/src/main/java/me/prettyprint/cassandra/service/template/ColumnFamilyResultWrapper.java b/core/src/main/java/me/prettyprint/cassandra/service/template/ColumnFamilyResultWrapper.java index 85c44d60b..0d9a77480 100644 --- a/core/src/main/java/me/prettyprint/cassandra/service/template/ColumnFamilyResultWrapper.java +++ b/core/src/main/java/me/prettyprint/cassandra/service/template/ColumnFamilyResultWrapper.java @@ -7,6 +7,7 @@ import java.util.Map; import java.util.NoSuchElementException; +import me.prettyprint.cassandra.model.ExecutionResult; import me.prettyprint.cassandra.model.HColumnImpl; import me.prettyprint.cassandra.serializers.ByteBufferSerializer; import me.prettyprint.cassandra.service.CassandraHost; @@ -26,12 +27,13 @@ public class ColumnFamilyResultWrapper extends AbstractResultWrapper { private Map> columns = new HashMap>(); private Iterator>> rows; private Map.Entry> entry; + private ExecutionResult>> executionResult; public ColumnFamilyResultWrapper(Serializer keySerializer, Serializer columnNameSerializer, - Map> rows) { - super(keySerializer, columnNameSerializer); - this.rows = rows.entrySet().iterator(); + ExecutionResult>> executionResult) { + super(keySerializer, columnNameSerializer, executionResult); + this.rows = executionResult.get().entrySet().iterator(); next(); } diff --git a/core/src/main/java/me/prettyprint/cassandra/service/template/ColumnFamilyTemplate.java b/core/src/main/java/me/prettyprint/cassandra/service/template/ColumnFamilyTemplate.java index 7bcac0187..756fc84c2 100644 --- a/core/src/main/java/me/prettyprint/cassandra/service/template/ColumnFamilyTemplate.java +++ b/core/src/main/java/me/prettyprint/cassandra/service/template/ColumnFamilyTemplate.java @@ -7,6 +7,7 @@ import java.util.Map; import me.prettyprint.cassandra.model.ExecutingKeyspace; +import me.prettyprint.cassandra.model.ExecutionResult; import me.prettyprint.cassandra.model.HSlicePredicate; import me.prettyprint.cassandra.model.thrift.ThriftConverter; import me.prettyprint.cassandra.serializers.SerializerTypeInferer; @@ -191,10 +192,6 @@ public T queryColumns(K key, List columns, return executeSliceQuery(key, predicate, mapper); } - private T executeSliceQuery(K key, HSlicePredicate predicate, ColumnFamilyRowMapper mapper) { - return mapper.mapRow(doExecuteSlice(key,predicate)); - } - public MappedColumnFamilyResult queryColumns(Iterable keys, ColumnFamilyRowMapper mapper) { @@ -235,26 +232,23 @@ public HColumn querySingleColumn(K key, N columnName, QueryResult> result = query.execute(); return result != null ? result.get() : null; } + + //-------------------------- delegation methods ---------------------------- + private T executeSliceQuery(K key, HSlicePredicate predicate, ColumnFamilyRowMapper mapper) { + return mapper.mapRow(doExecuteSlice(key,predicate)); + } + private ColumnFamilyResultWrapper doExecuteSlice(final K key, final HSlicePredicate workingSlicePredicate) { return new ColumnFamilyResultWrapper(keySerializer, topSerializer, sliceInternal(key, workingSlicePredicate)); } - - private MappedColumnFamilyResult doExecuteSlice(final K key, - final HSlicePredicate workingSlicePredicate, - final ColumnFamilyRowMapper mapper) { - return new MappedColumnFamilyResultWrapper(keySerializer, topSerializer, - sliceInternal(key, workingSlicePredicate), mapper); - } - - + private ColumnFamilyResultWrapper doExecuteMultigetSlice(final Iterable keys, final HSlicePredicate workingSlicePredicate) { return new ColumnFamilyResultWrapper(keySerializer, topSerializer, multigetSliceInternal(keys, workingSlicePredicate)); } - private MappedColumnFamilyResult doExecuteMultigetSlice(final Iterable keys, final HSlicePredicate workingSlicePredicate, final ColumnFamilyRowMapper mapper) { @@ -263,7 +257,7 @@ private MappedColumnFamilyResult doExecuteMultigetSlice(final Itera } - private Map> sliceInternal(final K key, + private ExecutionResult>> sliceInternal(final K key, final HSlicePredicate workingSlicePredicate) { return ((ExecutingKeyspace)keyspace).doExecuteOperation(new Operation>>(OperationType.READ) { @Override @@ -282,10 +276,10 @@ public Map> execute(Cassandra.Client cassan return cosc; } - }).get(); + }); } - private Map> multigetSliceInternal(final Iterable keys, + private ExecutionResult>> multigetSliceInternal(final Iterable keys, final HSlicePredicate workingSlicePredicate) { return ((ExecutingKeyspace)keyspace).doExecuteOperation(new Operation>>(OperationType.READ) { @Override @@ -295,7 +289,7 @@ public Map> execute(Cassandra.Client cassan List keyList = new ArrayList(); Iterators.addAll(keyList, keys.iterator()); cosc = cassandra.multiget_slice(keySerializer.toBytesList(keyList), columnParent, - (workingSlicePredicate == null ? activeSlicePredicate.setColumnNames(columnValueSerializers.keySet()).toThrift() : workingSlicePredicate.toThrift()), + (workingSlicePredicate == null ? activeSlicePredicate.setColumnNames(columnValueSerializers.keySet()).toThrift() : workingSlicePredicate.toThrift()), ThriftConverter.consistencyLevel(consistencyLevelPolicy.get(operationType))); } catch (Exception e) { throw exceptionsTranslator.translate(e); @@ -303,7 +297,7 @@ public Map> execute(Cassandra.Client cassan return cosc; } - }).get(); + }); } diff --git a/core/src/main/java/me/prettyprint/cassandra/service/template/MappedColumnFamilyResultWrapper.java b/core/src/main/java/me/prettyprint/cassandra/service/template/MappedColumnFamilyResultWrapper.java index aaa347109..d191a0fca 100644 --- a/core/src/main/java/me/prettyprint/cassandra/service/template/MappedColumnFamilyResultWrapper.java +++ b/core/src/main/java/me/prettyprint/cassandra/service/template/MappedColumnFamilyResultWrapper.java @@ -4,6 +4,7 @@ import java.util.List; import java.util.Map; +import me.prettyprint.cassandra.model.ExecutionResult; import me.prettyprint.hector.api.Serializer; import org.apache.cassandra.thrift.ColumnOrSuperColumn; @@ -14,8 +15,8 @@ public class MappedColumnFamilyResultWrapper extends ColumnFamilyResult public MappedColumnFamilyResultWrapper(Serializer keySerializer, Serializer columnNameSerializer, - Map> rows, ColumnFamilyRowMapper mapper) { - super(keySerializer, columnNameSerializer, rows); + ExecutionResult>> executionResult, ColumnFamilyRowMapper mapper) { + super(keySerializer, columnNameSerializer, executionResult); } @Override diff --git a/core/src/main/java/me/prettyprint/cassandra/service/template/SuperCfResultWrapper.java b/core/src/main/java/me/prettyprint/cassandra/service/template/SuperCfResultWrapper.java index 131520633..58938b815 100644 --- a/core/src/main/java/me/prettyprint/cassandra/service/template/SuperCfResultWrapper.java +++ b/core/src/main/java/me/prettyprint/cassandra/service/template/SuperCfResultWrapper.java @@ -5,6 +5,9 @@ import java.util.List; import java.util.Map; +import org.apache.cassandra.thrift.ColumnOrSuperColumn; + +import me.prettyprint.cassandra.model.ExecutionResult; import me.prettyprint.cassandra.service.CassandraHost; import me.prettyprint.hector.api.Serializer; import me.prettyprint.hector.api.beans.HColumn; @@ -26,8 +29,9 @@ public class SuperCfResultWrapper extends AbstractResultWrapper imp public SuperCfResultWrapper(Serializer keySerializer, - Serializer columnNameSerializer) { - super(keySerializer, columnNameSerializer); + Serializer columnNameSerializer, + ExecutionResult>> executionResult) { + super(keySerializer, columnNameSerializer, executionResult); // TODO Auto-generated constructor stub } From 30559e5fa0f5f1e5e76c5ade0684a4f2268c7731 Mon Sep 17 00:00:00 2001 From: patricioe Date: Thu, 24 Mar 2011 10:51:36 -0700 Subject: [PATCH 048/144] First stab to counter. Interfaces and Abstract class added HAbstractColumnImpl might need some review as it includes value serializer and it is not part of the HCounterColumn --- .../cassandra/model/HAbstractColumnImpl.java | 86 +++++++++++++++ .../cassandra/model/HColumnImpl.java | 79 ++------------ .../hector/api/beans/HCounterColumn.java | 40 +++++++ .../hector/api/beans/HCounterSuperColumn.java | 44 ++++++++ .../hector/api/factory/HFactory.java | 14 +++ .../hector/api/mutation/CounterMutator.java | 101 ++++++++++++++++++ 6 files changed, 293 insertions(+), 71 deletions(-) create mode 100644 core/src/main/java/me/prettyprint/cassandra/model/HAbstractColumnImpl.java create mode 100644 core/src/main/java/me/prettyprint/hector/api/beans/HCounterColumn.java create mode 100644 core/src/main/java/me/prettyprint/hector/api/beans/HCounterSuperColumn.java create mode 100644 core/src/main/java/me/prettyprint/hector/api/mutation/CounterMutator.java diff --git a/core/src/main/java/me/prettyprint/cassandra/model/HAbstractColumnImpl.java b/core/src/main/java/me/prettyprint/cassandra/model/HAbstractColumnImpl.java new file mode 100644 index 000000000..06cddc88a --- /dev/null +++ b/core/src/main/java/me/prettyprint/cassandra/model/HAbstractColumnImpl.java @@ -0,0 +1,86 @@ +package me.prettyprint.cassandra.model; + +import static me.prettyprint.cassandra.utils.Assert.notNull; + +import java.nio.ByteBuffer; + +import me.prettyprint.hector.api.Serializer; +import me.prettyprint.hector.api.beans.HColumn; + +import org.apache.cassandra.thrift.Column; + +/** + * Base class for different column implementations + * + * @author patricioe (Patricio Echague - patricio@datastax.com) + * @param + * + */ +public abstract class HAbstractColumnImpl { + + protected Column column; + protected Serializer nameSerializer; + protected Serializer valueSerializer; + + public HAbstractColumnImpl(Serializer nameSerializer, Serializer valueSerializer) { + notNull(nameSerializer, "nameSerializer is null"); + notNull(valueSerializer, "valueSerializer is null"); + this.nameSerializer = nameSerializer; + this.valueSerializer = valueSerializer; + this.column = new Column(); + } + + protected abstract T getThis(); + + public T setName(N name) { + notNull(name, "name is null"); + this.column.setName(nameSerializer.toByteBuffer(name)); + return getThis(); + } + + public N getName() { + return column.isSetName() ? nameSerializer.fromByteBuffer(column.name.duplicate()) : null; + } + + public T setValue(V value) { + notNull(value, "value is null"); + this.column.setValue(valueSerializer.toByteBuffer(value)); + return getThis(); + } + + public Serializer getNameSerializer() { + return nameSerializer; + } + + public Serializer getValueSerializer() { + return valueSerializer; + } + + /** + * Set the time-to-live value for this column in seconds. The server will + * mark this column as deleted once the number of seconds has elapsed. + */ + public T setTtl(int ttl) { + this.column.setTtl(ttl); + return getThis(); + } + + public int getTtl() { + return this.column.ttl; + } + + public ByteBuffer getNameBytes() { + return column.isSetName() ? column.name.duplicate() : null; + } + + public Column toThrift() { + return column; + } + + public T fromThrift(Column c) { + notNull(c, "column is null"); + this.column = c; + return getThis(); + } + +} diff --git a/core/src/main/java/me/prettyprint/cassandra/model/HColumnImpl.java b/core/src/main/java/me/prettyprint/cassandra/model/HColumnImpl.java index 1cedf751c..624711478 100644 --- a/core/src/main/java/me/prettyprint/cassandra/model/HColumnImpl.java +++ b/core/src/main/java/me/prettyprint/cassandra/model/HColumnImpl.java @@ -21,17 +21,11 @@ * @author Ran Tavory (rantav@gmail.com) * @author zznate */ -public final class HColumnImpl implements HColumn { - - private Column column; - private Serializer nameSerializer; - private Serializer valueSerializer; +public final class HColumnImpl extends HAbstractColumnImpl> implements HColumn { public HColumnImpl(N name, V value, long clock, Serializer nameSerializer, Serializer valueSerializer) { this(nameSerializer, valueSerializer); - notNull(name, "name is null"); - notNull(value, "value is null"); this.column = new Column(nameSerializer.toByteBuffer(name), valueSerializer.toByteBuffer(value), clock); @@ -45,30 +39,17 @@ public HColumnImpl(Column thriftColumn, Serializer nameSerializer, } public HColumnImpl(Serializer nameSerializer, Serializer valueSerializer) { - notNull(nameSerializer, "nameSerializer is null"); - notNull(valueSerializer, "valueSerializer is null"); - this.nameSerializer = nameSerializer; - this.valueSerializer = valueSerializer; - this.column = new Column(); + super(nameSerializer, valueSerializer); } public HColumnImpl(N name, V value, long clock) { this(name, value, clock, SerializerTypeInferer.getSerializer(name), SerializerTypeInferer.getSerializer(value)); } - - @Override - public HColumn setName(N name) { - notNull(name, "name is null"); - this.column.setName(nameSerializer.toByteBuffer(name)); - return this; - } - + @Override - public HColumn setValue(V value) { - notNull(value, "value is null"); - this.column.setValue(valueSerializer.toByteBuffer(value)); - return this; + protected HColumn getThis() { + return this; } @Override @@ -77,61 +58,17 @@ public HColumn setClock(long clock) { return this; } - /** - * Set the time-to-live value for this column in seconds. - * The server will mark this column as deleted once the number of seconds has elapsed. - */ - @Override - public HColumn setTtl(int ttl) { - this.column.setTtl(ttl); - return this; - } - - @Override - public int getTtl() { - return this.column.ttl; - } - - @Override - public N getName() { - return column.isSetName() ? nameSerializer.fromByteBuffer(column.name.duplicate()) : null; - } @Override public V getValue() { return column.isSetValue() ? valueSerializer.fromByteBuffer(column.value.duplicate()) : null; - } - + } @Override public long getClock() { return column.timestamp; } - public Column toThrift() { - return column; - } - - public HColumn fromThrift(Column c) { - notNull(c, "column is null"); - this.column = c; - return this; - } - - @Override - public Serializer getNameSerializer() { - return nameSerializer; - } - - @Override - public Serializer getValueSerializer() { - return valueSerializer; - } - - @Override - public ByteBuffer getNameBytes() { - return column.isSetName() ? column.name.duplicate() : null; - } @Override public ByteBuffer getValueBytes() { @@ -151,8 +88,6 @@ public HColumn clear() { column.setValueIsSet(false); return this; } - - @Override public HColumn apply(V value, long clock, int ttl) { @@ -193,4 +128,6 @@ public boolean equals(Object obj) { return new EqualsBuilder().appendSuper(super.equals(obj)).append(getName(), other.getName()). append(getValue(), other.getValue()).append(getClock(), other.getClock()).isEquals(); } + + } diff --git a/core/src/main/java/me/prettyprint/hector/api/beans/HCounterColumn.java b/core/src/main/java/me/prettyprint/hector/api/beans/HCounterColumn.java new file mode 100644 index 000000000..194b14a6f --- /dev/null +++ b/core/src/main/java/me/prettyprint/hector/api/beans/HCounterColumn.java @@ -0,0 +1,40 @@ +package me.prettyprint.hector.api.beans; + +import java.nio.ByteBuffer; + +import me.prettyprint.hector.api.Serializer; + +/** + * Hector Counter Column definition. + * + * @param The type of the column name + * + * @author patricioe (patricioe@gmail.com) + */ +public interface HCounterColumn { + + HCounterColumn setName(N name); + + HCounterColumn setValue(Long value); + + N getName(); + + Long getValue(); + + + /** + * (Advanced) Returns the underlying ByteBuffer for the name via ByteBuffer.duplicate(). + */ + ByteBuffer getNameBytes(); + + HCounterColumn clear(); + + int getTtl(); + + HCounterColumn setTtl(int ttl); + + HCounterColumn apply(Long value, int ttl); + + Serializer getNameSerializer(); + +} \ No newline at end of file diff --git a/core/src/main/java/me/prettyprint/hector/api/beans/HCounterSuperColumn.java b/core/src/main/java/me/prettyprint/hector/api/beans/HCounterSuperColumn.java new file mode 100644 index 000000000..e65543f40 --- /dev/null +++ b/core/src/main/java/me/prettyprint/hector/api/beans/HCounterSuperColumn.java @@ -0,0 +1,44 @@ +package me.prettyprint.hector.api.beans; + +import java.nio.ByteBuffer; +import java.util.List; + +import me.prettyprint.hector.api.Serializer; + +/** + * Models a Counter SuperColumn. + * + * @param + * SuperColumn name type + * @param + * Column name type + * + * @author patricioe + */ +public interface HCounterSuperColumn { + + HCounterSuperColumn setName(SN name); + + HCounterSuperColumn setSubcolumns(List> subcolumns); + + int getSize(); + + SN getName(); + + /** + * + * @return an unmodifiable list of columns + */ + List> getColumns(); + + HCounterColumn get(int i); + + Serializer getNameSerializer(); + + byte[] getNameBytes(); + + ByteBuffer getNameByteBuffer(); + + Serializer getSuperNameSerializer(); + +} \ No newline at end of file diff --git a/core/src/main/java/me/prettyprint/hector/api/factory/HFactory.java b/core/src/main/java/me/prettyprint/hector/api/factory/HFactory.java index 3ea960f62..faa6fb90b 100644 --- a/core/src/main/java/me/prettyprint/hector/api/factory/HFactory.java +++ b/core/src/main/java/me/prettyprint/hector/api/factory/HFactory.java @@ -47,6 +47,7 @@ import me.prettyprint.hector.api.ddl.ColumnFamilyDefinition; import me.prettyprint.hector.api.ddl.ComparatorType; import me.prettyprint.hector.api.ddl.KeyspaceDefinition; +import me.prettyprint.hector.api.mutation.CounterMutator; import me.prettyprint.hector.api.mutation.Mutator; import me.prettyprint.hector.api.query.ColumnQuery; import me.prettyprint.hector.api.query.CountQuery; @@ -312,6 +313,19 @@ public static Mutator createMutator(Keyspace keyspace, Serializer keySerializer) { return new MutatorImpl(keyspace, keySerializer); } + + + /** + * Creates a counter mutator for updating counters in a keyspace. + * + * @param keyspace + * @param keySerializer + */ + public static CounterMutator createCounterMutator(Keyspace keyspace, + Serializer keySerializer) { + return null; // TODO (patricioe) to be implemented. + //return new CounterMutatorImpl(keyspace, keySerializer); + } public static ColumnQuery createColumnQuery( Keyspace keyspace, Serializer keySerializer, diff --git a/core/src/main/java/me/prettyprint/hector/api/mutation/CounterMutator.java b/core/src/main/java/me/prettyprint/hector/api/mutation/CounterMutator.java new file mode 100644 index 000000000..0bfdec329 --- /dev/null +++ b/core/src/main/java/me/prettyprint/hector/api/mutation/CounterMutator.java @@ -0,0 +1,101 @@ +package me.prettyprint.hector.api.mutation; + +import me.prettyprint.hector.api.Serializer; +import me.prettyprint.hector.api.beans.HColumn; +import me.prettyprint.hector.api.beans.HCounterColumn; +import me.prettyprint.hector.api.beans.HCounterSuperColumn; +import me.prettyprint.hector.api.beans.HSuperColumn; + +/** + * A Mutator inserts or deletes counter values from the cluster. + * There are two main ways to use a mutator: + * 1. Use the increment/decrement/delete methods to immediately increment, decrement or delete values. + * or 2. Use the addIncrement/addDecrement/addDeletion methods to schedule batch operations and then execute() + * all of them in batch. + * + * The class is not thread-safe. + * + * @author patricioe (patricioe@gmail.com) + */ +public interface CounterMutator { + + // Simple and immediate insertion of a column + MutationResult increment(final K key, final String cf, final HCounterColumn c); + + MutationResult decrement(final K key, final String cf, final HCounterColumn c); + + // overloaded insert-super + MutationResult increment(final K key, final String cf, final HCounterSuperColumn superColumn); + + MutationResult decrement(final K key, final String cf, final HCounterSuperColumn superColumn); + + MutationResult delete(final K key, final String cf, final N columnName, + final Serializer nameSerializer); + + /** + * Deletes a subcolumn of a supercolumn for a counter + * @param super column type + * @param subcolumn type + */ + MutationResult subDelete(final K key, final String cf, final SN supercolumnName, + final N columnName, final Serializer sNameSerializer, final Serializer nameSerializer); + + /** + * Schedule an increment of a CounterColumn to be inserted in batch mode by {@link #execute()} + */ + CounterMutator addIncrement(K key, String cf, HCounterColumn c); + + /** + * Schedule an increment of a SuperColumn to be inserted in batch mode by {@link #execute()} + */ + CounterMutator addIncrement(K key, String cf, HCounterSuperColumn sc); + + /** + * Schedule a decrement of a CounterColumn to be inserted in batch mode by {@link #execute()} + */ + CounterMutator addDecrement(K key, String cf, HCounterColumn c); + + /** + * Schedule a decrement of a SuperColumn to be inserted in batch mode by {@link #execute()} + */ + CounterMutator addDecrement(K key, String cf, HCounterSuperColumn sc); + + /** + * Adds a Deletion to the underlying batch_mutate call. The columnName argument can be null + * in which case the whole row being deleted. + * + * @param column name type + * @param key row key + * @param cf column family + * @param counterColumnName column name. Use null to delete the whole row. + * @param nameSerializer a name serializer + * @return a mutator + */ + CounterMutator addDeletion(K key, String cf, N counterColumnName, Serializer nameSerializer); + + /** + * Alternate form for easy deletion of the whole row. + * + * @param + * @param key + * @return + */ + CounterMutator addDeletion(K key, String cf); + + + CounterMutator addSubDeletion(K key, String cf, HCounterSuperColumn sc); + + + /** + * Batch executes all mutations scheduled to this Mutator instance by addIncrement, addDecrement and addDeletion etc. + * May throw a HectorException which is a RuntimeException. + * @return A MutationResult holds the status. + */ + MutationResult execute(); + + /** + * Discards all pending mutations. + */ + CounterMutator discardPendingMutations(); + +} \ No newline at end of file From f74d7e7387adc264a99cde55909b1305731fe42c Mon Sep 17 00:00:00 2001 From: patricioe Date: Thu, 24 Mar 2011 20:32:10 -0700 Subject: [PATCH 049/144] Add HCounterColumnImpl, rollback HColumnImpl and HAbstractColumnImpl --- .../cassandra/model/HAbstractColumnImpl.java | 86 --------- .../cassandra/model/HColumnImpl.java | 79 ++++++++- .../cassandra/model/HCounterColumnImpl.java | 165 ++++++++++++++++++ 3 files changed, 236 insertions(+), 94 deletions(-) delete mode 100644 core/src/main/java/me/prettyprint/cassandra/model/HAbstractColumnImpl.java create mode 100644 core/src/main/java/me/prettyprint/cassandra/model/HCounterColumnImpl.java diff --git a/core/src/main/java/me/prettyprint/cassandra/model/HAbstractColumnImpl.java b/core/src/main/java/me/prettyprint/cassandra/model/HAbstractColumnImpl.java deleted file mode 100644 index 06cddc88a..000000000 --- a/core/src/main/java/me/prettyprint/cassandra/model/HAbstractColumnImpl.java +++ /dev/null @@ -1,86 +0,0 @@ -package me.prettyprint.cassandra.model; - -import static me.prettyprint.cassandra.utils.Assert.notNull; - -import java.nio.ByteBuffer; - -import me.prettyprint.hector.api.Serializer; -import me.prettyprint.hector.api.beans.HColumn; - -import org.apache.cassandra.thrift.Column; - -/** - * Base class for different column implementations - * - * @author patricioe (Patricio Echague - patricio@datastax.com) - * @param - * - */ -public abstract class HAbstractColumnImpl { - - protected Column column; - protected Serializer nameSerializer; - protected Serializer valueSerializer; - - public HAbstractColumnImpl(Serializer nameSerializer, Serializer valueSerializer) { - notNull(nameSerializer, "nameSerializer is null"); - notNull(valueSerializer, "valueSerializer is null"); - this.nameSerializer = nameSerializer; - this.valueSerializer = valueSerializer; - this.column = new Column(); - } - - protected abstract T getThis(); - - public T setName(N name) { - notNull(name, "name is null"); - this.column.setName(nameSerializer.toByteBuffer(name)); - return getThis(); - } - - public N getName() { - return column.isSetName() ? nameSerializer.fromByteBuffer(column.name.duplicate()) : null; - } - - public T setValue(V value) { - notNull(value, "value is null"); - this.column.setValue(valueSerializer.toByteBuffer(value)); - return getThis(); - } - - public Serializer getNameSerializer() { - return nameSerializer; - } - - public Serializer getValueSerializer() { - return valueSerializer; - } - - /** - * Set the time-to-live value for this column in seconds. The server will - * mark this column as deleted once the number of seconds has elapsed. - */ - public T setTtl(int ttl) { - this.column.setTtl(ttl); - return getThis(); - } - - public int getTtl() { - return this.column.ttl; - } - - public ByteBuffer getNameBytes() { - return column.isSetName() ? column.name.duplicate() : null; - } - - public Column toThrift() { - return column; - } - - public T fromThrift(Column c) { - notNull(c, "column is null"); - this.column = c; - return getThis(); - } - -} diff --git a/core/src/main/java/me/prettyprint/cassandra/model/HColumnImpl.java b/core/src/main/java/me/prettyprint/cassandra/model/HColumnImpl.java index 624711478..1cedf751c 100644 --- a/core/src/main/java/me/prettyprint/cassandra/model/HColumnImpl.java +++ b/core/src/main/java/me/prettyprint/cassandra/model/HColumnImpl.java @@ -21,11 +21,17 @@ * @author Ran Tavory (rantav@gmail.com) * @author zznate */ -public final class HColumnImpl extends HAbstractColumnImpl> implements HColumn { +public final class HColumnImpl implements HColumn { + + private Column column; + private Serializer nameSerializer; + private Serializer valueSerializer; public HColumnImpl(N name, V value, long clock, Serializer nameSerializer, Serializer valueSerializer) { this(nameSerializer, valueSerializer); + notNull(name, "name is null"); + notNull(value, "value is null"); this.column = new Column(nameSerializer.toByteBuffer(name), valueSerializer.toByteBuffer(value), clock); @@ -39,17 +45,30 @@ public HColumnImpl(Column thriftColumn, Serializer nameSerializer, } public HColumnImpl(Serializer nameSerializer, Serializer valueSerializer) { - super(nameSerializer, valueSerializer); + notNull(nameSerializer, "nameSerializer is null"); + notNull(valueSerializer, "valueSerializer is null"); + this.nameSerializer = nameSerializer; + this.valueSerializer = valueSerializer; + this.column = new Column(); } public HColumnImpl(N name, V value, long clock) { this(name, value, clock, SerializerTypeInferer.getSerializer(name), SerializerTypeInferer.getSerializer(value)); } - + + @Override + public HColumn setName(N name) { + notNull(name, "name is null"); + this.column.setName(nameSerializer.toByteBuffer(name)); + return this; + } + @Override - protected HColumn getThis() { - return this; + public HColumn setValue(V value) { + notNull(value, "value is null"); + this.column.setValue(valueSerializer.toByteBuffer(value)); + return this; } @Override @@ -58,17 +77,61 @@ public HColumn setClock(long clock) { return this; } + /** + * Set the time-to-live value for this column in seconds. + * The server will mark this column as deleted once the number of seconds has elapsed. + */ + @Override + public HColumn setTtl(int ttl) { + this.column.setTtl(ttl); + return this; + } + + @Override + public int getTtl() { + return this.column.ttl; + } + + @Override + public N getName() { + return column.isSetName() ? nameSerializer.fromByteBuffer(column.name.duplicate()) : null; + } @Override public V getValue() { return column.isSetValue() ? valueSerializer.fromByteBuffer(column.value.duplicate()) : null; - } + } + @Override public long getClock() { return column.timestamp; } + public Column toThrift() { + return column; + } + + public HColumn fromThrift(Column c) { + notNull(c, "column is null"); + this.column = c; + return this; + } + + @Override + public Serializer getNameSerializer() { + return nameSerializer; + } + + @Override + public Serializer getValueSerializer() { + return valueSerializer; + } + + @Override + public ByteBuffer getNameBytes() { + return column.isSetName() ? column.name.duplicate() : null; + } @Override public ByteBuffer getValueBytes() { @@ -88,6 +151,8 @@ public HColumn clear() { column.setValueIsSet(false); return this; } + + @Override public HColumn apply(V value, long clock, int ttl) { @@ -128,6 +193,4 @@ public boolean equals(Object obj) { return new EqualsBuilder().appendSuper(super.equals(obj)).append(getName(), other.getName()). append(getValue(), other.getValue()).append(getClock(), other.getClock()).isEquals(); } - - } diff --git a/core/src/main/java/me/prettyprint/cassandra/model/HCounterColumnImpl.java b/core/src/main/java/me/prettyprint/cassandra/model/HCounterColumnImpl.java new file mode 100644 index 000000000..c3e49e498 --- /dev/null +++ b/core/src/main/java/me/prettyprint/cassandra/model/HCounterColumnImpl.java @@ -0,0 +1,165 @@ +package me.prettyprint.cassandra.model; + +import static me.prettyprint.cassandra.utils.Assert.notNull; + +import java.nio.ByteBuffer; + +import me.prettyprint.cassandra.serializers.SerializerTypeInferer; +import me.prettyprint.hector.api.Serializer; +import me.prettyprint.hector.api.beans.HColumn; +import me.prettyprint.hector.api.beans.HCounterColumn; + +import org.apache.cassandra.thrift.Column; +import org.apache.cassandra.thrift.CounterColumn; +import org.apache.commons.lang.builder.EqualsBuilder; +import org.apache.commons.lang.builder.HashCodeBuilder; + +/** + * Hector Counter Column definition. + * + * @param The type of the column name + * + * @author patricioe (patricioe@gmail.com) + */ +public final class HCounterColumnImpl implements HCounterColumn { + + private CounterColumn counterColumn; + private Serializer nameSerializer; + + + public HCounterColumnImpl(N name, Long value, Serializer nameSerializer) { + this(nameSerializer); + notNull(name, "name is null"); + notNull(value, "value is null"); + this.counterColumn = new CounterColumn(nameSerializer.toByteBuffer(name), value); + } + + public HCounterColumnImpl(CounterColumn thriftCounterColumn, Serializer nameSerializer) { + this(nameSerializer); + notNull(thriftCounterColumn, "thriftColumn is null"); + this.counterColumn = thriftCounterColumn; + } + + public HCounterColumnImpl(Serializer nameSerializer) { + notNull(nameSerializer, "nameSerializer is null"); + this.nameSerializer = nameSerializer; + this.counterColumn = new CounterColumn(); + } + + public HCounterColumnImpl(N name, Long value) { + this(name, value, SerializerTypeInferer.getSerializer(name)); + } + + @Override + public HCounterColumn setName(N name) { + notNull(name, "name is null"); + this.counterColumn.setName(nameSerializer.toByteBuffer(name)); + return this; + } + + @Override + public HCounterColumn setValue(Long value) { + notNull(value, "value is null"); + this.counterColumn.setValue(value); + return this; + } + + /** + * Set the time-to-live value for this column in seconds. + * The server will mark this column as deleted once the number of seconds has elapsed. + */ + @Override + public HCounterColumn setTtl(int ttl) { + //this.counterColumn.setTtl(ttl); + // TODO (patricioe) Pending on Cassandra trunk + return this; + } + + @Override + public int getTtl() { + //return this.counterColumn.ttl; + // TODO (patricioe) Pending on Cassandra trunk + return Integer.MAX_VALUE; + } + + @Override + public N getName() { + return counterColumn.isSetName() ? nameSerializer.fromByteBuffer(counterColumn.name.duplicate()) : null; + } + + @Override + public Long getValue() { + return counterColumn.value; + } + + public CounterColumn toThrift() { + return counterColumn; + } + + public HCounterColumn fromThrift(CounterColumn c) { + notNull(c, "column is null"); + this.counterColumn = c; + return this; + } + + @Override + public Serializer getNameSerializer() { + return nameSerializer; + } + + @Override + public ByteBuffer getNameBytes() { + return counterColumn.isSetName() ? counterColumn.name.duplicate() : null; + } + + /** + * Clear value, timestamp and ttl (the latter two set to '0') leaving only the column name + */ + @Override + public HCounterColumn clear() { + counterColumn.value = 0; // TODO (patricioe) Is this ok? + //counterColumn.ttl = 0; TODO (patricioe) pending on trunk + //counterColumn.setTtlIsSet(false); + counterColumn.setValueIsSet(false); + return this; + } + + @Override + public HCounterColumn apply(Long value, int ttl) { + setValue(value); + //counterColumn.setTtl(ttl); + return this; + } + + public HCounterColumn apply(CounterColumn c) { + this.counterColumn = c; + return this; + } + + @Override + public String toString() { + return String.format("HCounterColumn(%s=%s)",getName(), getValue()); + } + + @Override + public int hashCode() { + return new HashCodeBuilder().append(getName()).append(getValue()).toHashCode(); + } + + @Override + public boolean equals(Object obj) { + if (obj == null) { + return false; + } + if (obj == this) { + return true; + } + if (obj.getClass() != getClass()) { + return false; + } + @SuppressWarnings("unchecked") + HCounterColumnImpl other = (HCounterColumnImpl) obj; + return new EqualsBuilder().appendSuper(super.equals(obj)).append(getName(), other.getName()). + append(getValue(), other.getValue()).isEquals(); + } +} From ce95d2b6fc278d24bb2ffc43a43a55caee6c16d0 Mon Sep 17 00:00:00 2001 From: patricioe Date: Thu, 24 Mar 2011 23:06:18 -0700 Subject: [PATCH 050/144] Add new map of CounterMutation to BatchMutation. Add tests. --- .../cassandra/service/BatchMutation.java | 97 +++++++++++++++++-- .../cassandra/service/BatchMutationTest.java | 93 ++++++++++++++++++ 2 files changed, 183 insertions(+), 7 deletions(-) diff --git a/core/src/main/java/me/prettyprint/cassandra/service/BatchMutation.java b/core/src/main/java/me/prettyprint/cassandra/service/BatchMutation.java index eebdbd324..971dee33b 100644 --- a/core/src/main/java/me/prettyprint/cassandra/service/BatchMutation.java +++ b/core/src/main/java/me/prettyprint/cassandra/service/BatchMutation.java @@ -11,6 +11,11 @@ import org.apache.cassandra.thrift.Column; import org.apache.cassandra.thrift.ColumnOrSuperColumn; +import org.apache.cassandra.thrift.Counter; +import org.apache.cassandra.thrift.CounterColumn; +import org.apache.cassandra.thrift.CounterDeletion; +import org.apache.cassandra.thrift.CounterMutation; +import org.apache.cassandra.thrift.CounterSuperColumn; import org.apache.cassandra.thrift.Deletion; import org.apache.cassandra.thrift.Mutation; import org.apache.cassandra.thrift.SuperColumn; @@ -24,21 +29,26 @@ * * @author Ran Tavory (rantan@outbrain.com) * @author Nathan McCall (nate@riptano.com) + * @author Patricio Echague (patricioe@gmail.com) * */ public final class BatchMutation { private final Map>> mutationMap; + private Map>> countersMutationMap; private final Serializer keySerializer; public BatchMutation(Serializer serializer) { this.keySerializer = serializer; mutationMap = new HashMap>>(); + countersMutationMap = null; } - private BatchMutation(Serializer serializer, Map>> mutationMap) { + private BatchMutation(Serializer serializer, Map>> mutationMap, + Map>> countersMutationMap) { this.keySerializer = serializer; this.mutationMap = mutationMap; + this.countersMutationMap = countersMutationMap; } /** @@ -51,10 +61,9 @@ public BatchMutation addInsertion(K key, List columnFamilies, addMutation(key, columnFamilies, mutation); return this; } - - + /** - * Add an SuperColumn insertion (or update) to the batch mutation request. + * Add a SuperColumn insertion (or update) to the batch mutation request. */ public BatchMutation addSuperInsertion(K key, List columnFamilies, SuperColumn superColumn) { @@ -63,6 +72,31 @@ public BatchMutation addSuperInsertion(K key, List columnFamilies, addMutation(key, columnFamilies, mutation); return this; } + + /** + * Add a ColumnCounter insertion (or update) + */ + public BatchMutation addCounterInsertion(K key, List columnFamilies, CounterColumn counterColumn) { + Counter counter = new Counter(); + counter.setColumn(counterColumn); + CounterMutation mutation = new CounterMutation(); + mutation.setCounter(counter); + addCounterMutation(key, columnFamilies, mutation); + return this; + } + + /** + * Add a SuperColumnCounter insertion (or update) + */ + public BatchMutation addSuperCounterInsertion(K key, List columnFamilies, + CounterSuperColumn counterSuperColumn) { + Counter counter = new Counter(); + counter.setSuper_column(counterSuperColumn); + CounterMutation mutation = new CounterMutation(); + mutation.setCounter(counter); + addCounterMutation(key, columnFamilies, mutation); + return this; + } /** * Add a deletion request to the batch mutation. @@ -73,6 +107,16 @@ public BatchMutation addDeletion(K key, List columnFamilies, Deletion addMutation(key, columnFamilies, mutation); return this; } + + /** + * Add a counterDeletion request to the batch mutation. + */ + public BatchMutation addCounterDeletion(K key, List columnFamilies, CounterDeletion deletion) { + CounterMutation mutation = new CounterMutation(); + mutation.setDeletion(deletion); + addCounterMutation(key, columnFamilies, mutation); + return this; + } private void addMutation(K key, List columnFamilies, Mutation mutation) { Map> innerMutationMap = getInnerMutationMap(key); @@ -87,6 +131,20 @@ private void addMutation(K key, List columnFamilies, Mutation mutation) } mutationMap.put(keySerializer.toByteBuffer(key), innerMutationMap); } + + private void addCounterMutation(K key, List columnFamilies, CounterMutation mutation) { + Map> innerMutationMap = getCountersInnerMutationMap(key); + for (String columnFamily : columnFamilies) { + if (innerMutationMap.get(columnFamily) == null) { + innerMutationMap.put(columnFamily, Arrays.asList(mutation)); + } else { + List mutations = new ArrayList(innerMutationMap.get(columnFamily)); + mutations.add(mutation); + innerMutationMap.put(columnFamily, mutations); + } + } + getCreateCounterMutationMap().put(keySerializer.toByteBuffer(key), innerMutationMap); + } private Map> getInnerMutationMap(K key) { Map> innerMutationMap = mutationMap.get(keySerializer.toByteBuffer(key)); @@ -95,18 +153,43 @@ private Map> getInnerMutationMap(K key) { } return innerMutationMap; } + + private Map> getCountersInnerMutationMap(K key) { + Map> innerMutationMap = getCreateCounterMutationMap().get(keySerializer.toByteBuffer(key)); + if (innerMutationMap == null) { + innerMutationMap = new HashMap>(); + } + return innerMutationMap; + } + + /** + * Assuming that not all the operations are with counter, we create it on demand. + * @return + */ + private Map>> getCreateCounterMutationMap() { + if (countersMutationMap == null) { + countersMutationMap = new HashMap>>(); + } + return countersMutationMap; + } Map>> getMutationMap() { return mutationMap; } - + + /** + * Note it can be NULL ! + */ + Map>> getCounterMutationMap() { + return countersMutationMap; + } /** * Makes a shallow copy of the mutation object. * @return */ public BatchMutation makeCopy() { - return new BatchMutation(keySerializer, mutationMap); + return new BatchMutation(keySerializer, mutationMap, countersMutationMap); } /** @@ -114,6 +197,6 @@ public BatchMutation makeCopy() { * @return */ public boolean isEmpty() { - return mutationMap.isEmpty(); + return mutationMap.isEmpty() && (countersMutationMap == null || countersMutationMap.isEmpty()) ; } } diff --git a/core/src/test/java/me/prettyprint/cassandra/service/BatchMutationTest.java b/core/src/test/java/me/prettyprint/cassandra/service/BatchMutationTest.java index ce636973b..6c89cb5f8 100644 --- a/core/src/test/java/me/prettyprint/cassandra/service/BatchMutationTest.java +++ b/core/src/test/java/me/prettyprint/cassandra/service/BatchMutationTest.java @@ -1,6 +1,8 @@ package me.prettyprint.cassandra.service; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; import java.nio.ByteBuffer; import java.util.ArrayList; @@ -9,8 +11,13 @@ import java.util.Map; import me.prettyprint.cassandra.serializers.StringSerializer; +import me.prettyprint.hector.api.query.SuperCountQuery; import org.apache.cassandra.thrift.Column; +import org.apache.cassandra.thrift.CounterColumn; +import org.apache.cassandra.thrift.CounterDeletion; +import org.apache.cassandra.thrift.CounterMutation; +import org.apache.cassandra.thrift.CounterSuperColumn; import org.apache.cassandra.thrift.Deletion; import org.apache.cassandra.thrift.Mutation; import org.apache.cassandra.thrift.SlicePredicate; @@ -80,4 +87,90 @@ public void testAddDeletion() { batchMutate.addDeletion("key1", columnFamilies, deletion); assertEquals(2,batchMutate.getMutationMap().get(StringSerializer.get().toByteBuffer("key1")).get("Standard1").size()); } + + @Test + public void testIsEmpty() { + assertTrue(batchMutate.isEmpty()); + + // Insert a column + Column c1 = new Column( + StringSerializer.get().toByteBuffer("c_name"), + StringSerializer.get().toByteBuffer("c_val"), + System.currentTimeMillis()); + batchMutate.addInsertion("key1", columnFamilies, c1); + assertFalse(batchMutate.isEmpty()); + + // Insert a Counter. + CounterColumn cc1 = new CounterColumn(StringSerializer.get().toByteBuffer("c_name"), 13); + batchMutate.addCounterInsertion("key1", columnFamilies, cc1); + assertFalse(batchMutate.isEmpty()); + + // Delete the Column/SuperColumn mutations and it should be still not empty + batchMutate.getMutationMap().clear(); + assertFalse(batchMutate.isEmpty()); + + // Delete the Counter mutations. Now the batchMutate should be empty. + batchMutate.getCounterMutationMap().clear(); + assertTrue(batchMutate.isEmpty()); + } + + // ********** Test Counters related operations ****************** + + @Test + public void testAddCounterInsertion() { + + // Insert a Counter. + CounterColumn cc1 = new CounterColumn(StringSerializer.get().toByteBuffer("c_name"), 222); + + batchMutate.addCounterInsertion("key1", columnFamilies, cc1); + + // assert there is one outter map row with 'key' as the key + Map>> mutationMap = batchMutate.getCounterMutationMap(); + assertEquals(1, mutationMap.get(StringSerializer.get().toByteBuffer("key1")).size()); + + // add again with a different counter and verify there is one key and two mutations underneath + // for "standard1" + CounterColumn cc2 = new CounterColumn(StringSerializer.get().toByteBuffer("c_name2"), 44); + batchMutate.addCounterInsertion("key1", columnFamilies, cc2); + assertEquals(2, mutationMap.get(StringSerializer.get().toByteBuffer("key1")).get("Standard1").size()); + } + + @Test + public void testAddCounterDeletion() { + + CounterDeletion counterDeletion = new CounterDeletion(); + + SlicePredicate slicePredicate = new SlicePredicate(); + slicePredicate.addToColumn_names(StringSerializer.get().toByteBuffer("c_name")); + counterDeletion.setPredicate(slicePredicate); + + batchMutate.addCounterDeletion("key1", columnFamilies, counterDeletion); + + assertEquals(1, batchMutate.getCounterMutationMap().get(StringSerializer.get().toByteBuffer("key1")).size()); + + counterDeletion = new CounterDeletion(); + slicePredicate = new SlicePredicate(); + slicePredicate.addToColumn_names(StringSerializer.get().toByteBuffer("c_name2")); + counterDeletion.setPredicate(slicePredicate); + batchMutate.addCounterDeletion("key1", columnFamilies, counterDeletion); + assertEquals(2,batchMutate.getCounterMutationMap().get(StringSerializer.get().toByteBuffer("key1")).get("Standard1").size()); + } + + @Test + public void testAddSuperCounterInsertion() { + // Create 1 super counter. + CounterSuperColumn csc1 = new CounterSuperColumn(StringSerializer.get().toByteBuffer("c_name"), + Arrays.asList(new CounterColumn(StringSerializer.get().toByteBuffer("c_name"), 123))); + + batchMutate.addSuperCounterInsertion("key1", columnFamilies, csc1); + // assert there is one outter map row with 'key' as the key + assertEquals(1, batchMutate.getCounterMutationMap().get(StringSerializer.get().toByteBuffer("key1")).size()); + + // add again with a different column and verify there is one key and two mutations underneath + // for "standard1" + CounterSuperColumn csc2 = new CounterSuperColumn(StringSerializer.get().toByteBuffer("c_name2"), + Arrays.asList(new CounterColumn(StringSerializer.get().toByteBuffer("c_name"), 456))); + batchMutate.addSuperCounterInsertion("key1", columnFamilies, csc2); + assertEquals(2, batchMutate.getCounterMutationMap().get(StringSerializer.get().toByteBuffer("key1")).get("Standard1").size()); + } } From 03daa8965f61b43d0f7cf31bf885a42cc60af2ea Mon Sep 17 00:00:00 2001 From: zznate Date: Sat, 26 Mar 2011 00:23:02 -0500 Subject: [PATCH 051/144] encapsulate column creation in template classes --- .../model/thrift/ThriftColumnFactory.java | 32 +++++++++++++++++++ .../AbstractColumnFamilyTemplate.java | 8 +++++ .../template/ColumnFamilyStringUpdater.java | 7 ++++ .../template/ColumnFamilyTemplate.java | 17 +--------- .../service/template/ColumnFamilyUpdater.java | 30 ++++++++++------- .../prettyprint/hector/api/ColumnFactory.java | 11 +++++++ .../template/ColumnFamilyTemplateTest.java | 9 ++---- 7 files changed, 81 insertions(+), 33 deletions(-) create mode 100644 core/src/main/java/me/prettyprint/cassandra/model/thrift/ThriftColumnFactory.java create mode 100644 core/src/main/java/me/prettyprint/hector/api/ColumnFactory.java diff --git a/core/src/main/java/me/prettyprint/cassandra/model/thrift/ThriftColumnFactory.java b/core/src/main/java/me/prettyprint/cassandra/model/thrift/ThriftColumnFactory.java new file mode 100644 index 000000000..35e90f126 --- /dev/null +++ b/core/src/main/java/me/prettyprint/cassandra/model/thrift/ThriftColumnFactory.java @@ -0,0 +1,32 @@ +package me.prettyprint.cassandra.model.thrift; + +import me.prettyprint.cassandra.model.HColumnImpl; +import me.prettyprint.cassandra.serializers.StringSerializer; +import me.prettyprint.hector.api.ColumnFactory; +import me.prettyprint.hector.api.Serializer; +import me.prettyprint.hector.api.beans.HColumn; +import me.prettyprint.hector.api.factory.HFactory; + +public class ThriftColumnFactory implements ColumnFactory { + + @Override + public HColumn createColumn(N name, V value, + Serializer nameSerializer, Serializer valueSerializer) { + return new HColumnImpl(name, value, HFactory.createClock(), nameSerializer, + valueSerializer); + } + + @Override + public HColumn createColumn(N name, V value, long clock, + Serializer nameSerializer, Serializer valueSerializer) { + return new HColumnImpl(name, value, clock, nameSerializer, + valueSerializer); + } + + @Override + public HColumn createStringColumn(String name, String value) { + return createColumn(name, value, StringSerializer.get(), StringSerializer.get()); + } + + +} diff --git a/core/src/main/java/me/prettyprint/cassandra/service/template/AbstractColumnFamilyTemplate.java b/core/src/main/java/me/prettyprint/cassandra/service/template/AbstractColumnFamilyTemplate.java index 30b188392..9a3c959a5 100644 --- a/core/src/main/java/me/prettyprint/cassandra/service/template/AbstractColumnFamilyTemplate.java +++ b/core/src/main/java/me/prettyprint/cassandra/service/template/AbstractColumnFamilyTemplate.java @@ -4,8 +4,10 @@ import java.util.Map; import me.prettyprint.cassandra.model.HSlicePredicate; +import me.prettyprint.cassandra.model.thrift.ThriftColumnFactory; import me.prettyprint.cassandra.service.ExceptionsTranslator; import me.prettyprint.cassandra.service.ExceptionsTranslatorImpl; +import me.prettyprint.hector.api.ColumnFactory; import me.prettyprint.hector.api.Keyspace; import me.prettyprint.hector.api.Serializer; import me.prettyprint.hector.api.factory.HFactory; @@ -26,6 +28,7 @@ public class AbstractColumnFamilyTemplate { protected Map> columnValueSerializers; protected ColumnParent columnParent; protected HSlicePredicate activeSlicePredicate; + protected ColumnFactory columnFactory; /** The serializer for a standard column name or a super-column name */ protected Serializer topSerializer; @@ -72,6 +75,7 @@ public AbstractColumnFamilyTemplate(Keyspace keyspace, String columnFamily, this.columnParent = new ColumnParent(columnFamily); this.activeSlicePredicate = new HSlicePredicate(topSerializer); exceptionsTranslator = new ExceptionsTranslatorImpl(); + this.columnFactory = new ThriftColumnFactory(); } @@ -147,6 +151,10 @@ public long getEffectiveClock() { public void setExceptionsTranslator(ExceptionsTranslator exceptionsTranslator) { this.exceptionsTranslator = exceptionsTranslator; + } + + public void setColumnFactory(ColumnFactory columnFactory) { + this.columnFactory = columnFactory; } protected MutationResult executeIfNotBatched() { diff --git a/core/src/main/java/me/prettyprint/cassandra/service/template/ColumnFamilyStringUpdater.java b/core/src/main/java/me/prettyprint/cassandra/service/template/ColumnFamilyStringUpdater.java index ea446de3f..958cfee7e 100644 --- a/core/src/main/java/me/prettyprint/cassandra/service/template/ColumnFamilyStringUpdater.java +++ b/core/src/main/java/me/prettyprint/cassandra/service/template/ColumnFamilyStringUpdater.java @@ -1,5 +1,7 @@ package me.prettyprint.cassandra.service.template; +import me.prettyprint.hector.api.ColumnFactory; + /** * A simple specialization of the generic class for the very common all-string * column name scenario. @@ -8,4 +10,9 @@ * @since Mar 10, 2011 */ public abstract class ColumnFamilyStringUpdater extends ColumnFamilyUpdater { + + public ColumnFamilyStringUpdater( + ColumnFamilyTemplate template, ColumnFactory columnFactory) { + super(template, columnFactory); + } } diff --git a/core/src/main/java/me/prettyprint/cassandra/service/template/ColumnFamilyTemplate.java b/core/src/main/java/me/prettyprint/cassandra/service/template/ColumnFamilyTemplate.java index 756fc84c2..847c424f8 100644 --- a/core/src/main/java/me/prettyprint/cassandra/service/template/ColumnFamilyTemplate.java +++ b/core/src/main/java/me/prettyprint/cassandra/service/template/ColumnFamilyTemplate.java @@ -75,9 +75,8 @@ public ColumnFamilyTemplate setMutator(Mutator mutator) { } public ColumnFamilyUpdater createUpdater(K key) { - ColumnFamilyUpdater updater = new ColumnFamilyUpdater(); + ColumnFamilyUpdater updater = new ColumnFamilyUpdater(this, columnFactory); updater.addKey(key); - updater.template = this; return updater; } @@ -86,20 +85,6 @@ public void update(ColumnFamilyUpdater updater) { executeIfNotBatched(); } - /** - * Updates values in a standard column family in the row specified by key. - * - * @param key - * the row key - * @param updater - * the object performing updates of the current row - */ - public void update(K key, ColumnFamilyUpdater updater) { - updater.template = this; - updater.addKey(key); - update(updater); - } - /** * Checks if there are any columns at a row specified by key in a standard diff --git a/core/src/main/java/me/prettyprint/cassandra/service/template/ColumnFamilyUpdater.java b/core/src/main/java/me/prettyprint/cassandra/service/template/ColumnFamilyUpdater.java index e9238fd25..a78cec37c 100644 --- a/core/src/main/java/me/prettyprint/cassandra/service/template/ColumnFamilyUpdater.java +++ b/core/src/main/java/me/prettyprint/cassandra/service/template/ColumnFamilyUpdater.java @@ -5,6 +5,7 @@ import java.util.List; import java.util.UUID; +import me.prettyprint.cassandra.model.thrift.ThriftColumnFactory; import me.prettyprint.cassandra.serializers.BooleanSerializer; import me.prettyprint.cassandra.serializers.BytesArraySerializer; import me.prettyprint.cassandra.serializers.DateSerializer; @@ -12,6 +13,7 @@ import me.prettyprint.cassandra.serializers.LongSerializer; import me.prettyprint.cassandra.serializers.StringSerializer; import me.prettyprint.cassandra.serializers.UUIDSerializer; +import me.prettyprint.hector.api.ColumnFactory; import me.prettyprint.hector.api.Serializer; import me.prettyprint.hector.api.beans.HColumn; import me.prettyprint.hector.api.factory.HFactory; @@ -42,10 +44,16 @@ */ public class ColumnFamilyUpdater { // Values have package access and are assigned by CassandraTemplate - ColumnFamilyTemplate template; - List keys; - int keyPos = 0; - + private ColumnFamilyTemplate template; + private List keys; + private int keyPos = 0; + private ColumnFactory columnFactory; + + public ColumnFamilyUpdater(ColumnFamilyTemplate template, ColumnFactory columnFactory) { + this.template = template; + this.columnFactory = columnFactory; + } + /** * Default no-op update implementation. Sub-classes should override this to provide * for more complex behaviour @@ -78,43 +86,43 @@ public void deleteColumn(N columnName) { } public void setString(N columnName, String value) { - HColumn column = HFactory.createColumn(columnName, value, + HColumn column = columnFactory.createColumn(columnName, value, template.getTopSerializer(), StringSerializer.get()); template.getMutator().addInsertion(getCurrentKey(), template.getColumnFamily(), column); } public void setUUID(N columnName, UUID value) { - HColumn column = HFactory.createColumn(columnName, value, + HColumn column = columnFactory.createColumn(columnName, value, template.getTopSerializer(), UUIDSerializer.get()); template.getMutator().addInsertion(getCurrentKey(), template.getColumnFamily(), column); } public void setLong(N columnName, Long value) { - HColumn column = HFactory.createColumn(columnName, value, + HColumn column = columnFactory.createColumn(columnName, value, template.getTopSerializer(), LongSerializer.get()); template.getMutator().addInsertion(getCurrentKey(), template.getColumnFamily(), column); } public void setInteger(N columnName, Integer value) { - HColumn column = HFactory.createColumn(columnName, value, + HColumn column = columnFactory.createColumn(columnName, value, template.getTopSerializer(), IntegerSerializer.get()); template.getMutator().addInsertion(getCurrentKey(), template.getColumnFamily(), column); } public void setBoolean(N columnName, Boolean value) { - HColumn column = HFactory.createColumn(columnName, value, + HColumn column = columnFactory.createColumn(columnName, value, template.getTopSerializer(), BooleanSerializer.get()); template.getMutator().addInsertion(getCurrentKey(), template.getColumnFamily(), column); } public void setByteArray(N columnName, byte[] value) { - HColumn column = HFactory.createColumn(columnName, value, + HColumn column = columnFactory.createColumn(columnName, value, template.getTopSerializer(), BytesArraySerializer.get()); template.getMutator().addInsertion(getCurrentKey(), template.getColumnFamily(), column); } public void setDate(N columnName, Date value) { - HColumn column = HFactory.createColumn(columnName, value, + HColumn column = columnFactory.createColumn(columnName, value, template.getTopSerializer(), DateSerializer.get()); template.getMutator().addInsertion(getCurrentKey(), template.getColumnFamily(), column); } diff --git a/core/src/main/java/me/prettyprint/hector/api/ColumnFactory.java b/core/src/main/java/me/prettyprint/hector/api/ColumnFactory.java new file mode 100644 index 000000000..1863d8bf6 --- /dev/null +++ b/core/src/main/java/me/prettyprint/hector/api/ColumnFactory.java @@ -0,0 +1,11 @@ +package me.prettyprint.hector.api; + +import me.prettyprint.hector.api.beans.HColumn; + +public interface ColumnFactory { + HColumn createColumn(N name, V value, Serializer nameSerializer, Serializer valueSerializer); + + HColumn createColumn(N name, V value, long clock, Serializer nameSerializer, Serializer valueSerializer); + + HColumn createStringColumn(String name, String value); +} diff --git a/core/src/test/java/me/prettyprint/cassandra/service/template/ColumnFamilyTemplateTest.java b/core/src/test/java/me/prettyprint/cassandra/service/template/ColumnFamilyTemplateTest.java index 688f86f6d..1ee472b22 100644 --- a/core/src/test/java/me/prettyprint/cassandra/service/template/ColumnFamilyTemplateTest.java +++ b/core/src/test/java/me/prettyprint/cassandra/service/template/ColumnFamilyTemplateTest.java @@ -27,12 +27,9 @@ public void testCreateSelect() { @Test public void testCreateSelectTemplate() { ColumnFamilyTemplate template = new ColumnFamilyTemplate(keyspace, "Standard1", se, se, HFactory.createMutator(keyspace, se)); - template.update("key1", new ColumnFamilyUpdater() { - @Override - public void update() { - setString("column1", "value1"); - } - }); + ColumnFamilyUpdater updater = template.createUpdater("key1"); + updater.setString("column1","value1"); + updater.update(); String value = template.queryColumns("key1", "", "", new ColumnFamilyRowMapper() { @Override From e55b871be962110259f2b6b0277a3dd27352a894 Mon Sep 17 00:00:00 2001 From: Ed Anuff Date: Sun, 27 Mar 2011 20:36:41 -0700 Subject: [PATCH 052/144] Added support for descending sort --- .../hector/api/beans/AbstractComposite.java | 27 ++++++++++++++++--- 1 file changed, 24 insertions(+), 3 deletions(-) diff --git a/core/src/main/java/me/prettyprint/hector/api/beans/AbstractComposite.java b/core/src/main/java/me/prettyprint/hector/api/beans/AbstractComposite.java index 85b5da053..283edc880 100644 --- a/core/src/main/java/me/prettyprint/hector/api/beans/AbstractComposite.java +++ b/core/src/main/java/me/prettyprint/hector/api/beans/AbstractComposite.java @@ -210,6 +210,10 @@ private String comparatorForUUID(UUID uuid) { } private Serializer serializerForComparator(String c) { + int p = c.indexOf('('); + if (p >= 0) { + c = c.substring(0, p); + } if (LEXICALUUIDTYPE.getTypeName().equals(c) || TIMEUUIDTYPE.getTypeName().equals(c)) { return UUIDSerializer.get(); @@ -269,7 +273,15 @@ private String getComparator(int i, ByteBuffer bb) { if ((header & 0x8000) == 0) { name = ByteBufferUtil.string(getBytes(bb, header)); } else { - name = aliasToComparatorMapping.get((byte) (header & 0xFF)); + byte a = (byte) (header & 0xFF); + name = aliasToComparatorMapping.get(a); + if (name == null) { + a = (byte) Character.toUpperCase(a); + name = aliasToComparatorMapping.get(a); + if (name != null) { + name += "(sort=desc)"; + } + } } } catch (CharacterCodingException e) { throw new RuntimeException(e); @@ -418,9 +430,18 @@ public ByteBuffer serialize() { if (comparator == null) { comparator = c.getComparator(); } + int p = comparator.indexOf("(sort=desc)"); + boolean desc = false; + if (p >= 0) { + comparator = comparator.substring(0, p); + desc = true; + } if (aliasToComparatorMapping.inverse().containsKey(comparator)) { - out.writeShort((short) (0x8000 | aliasToComparatorMapping.inverse() - .get(comparator))); + byte a = aliasToComparatorMapping.inverse().get(comparator); + if (desc) { + a = (byte) Character.toUpperCase(a); + } + out.writeShort((short) (0x8000 | a)); } else { out.writeShort((short) comparator.length()); out.write(ByteBufferUtil.bytes(comparator)); From ff1a0d10eae6096c1a6ed950e6e9b3a10954536f Mon Sep 17 00:00:00 2001 From: Ed Anuff Date: Sun, 27 Mar 2011 20:44:35 -0700 Subject: [PATCH 053/144] fixed Character.toUpperCase usage in AbstractComposite --- .../me/prettyprint/hector/api/beans/AbstractComposite.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/core/src/main/java/me/prettyprint/hector/api/beans/AbstractComposite.java b/core/src/main/java/me/prettyprint/hector/api/beans/AbstractComposite.java index 283edc880..3518950f3 100644 --- a/core/src/main/java/me/prettyprint/hector/api/beans/AbstractComposite.java +++ b/core/src/main/java/me/prettyprint/hector/api/beans/AbstractComposite.java @@ -276,7 +276,7 @@ private String getComparator(int i, ByteBuffer bb) { byte a = (byte) (header & 0xFF); name = aliasToComparatorMapping.get(a); if (name == null) { - a = (byte) Character.toUpperCase(a); + a = (byte) Character.toUpperCase((char) a); name = aliasToComparatorMapping.get(a); if (name != null) { name += "(sort=desc)"; @@ -439,7 +439,7 @@ public ByteBuffer serialize() { if (aliasToComparatorMapping.inverse().containsKey(comparator)) { byte a = aliasToComparatorMapping.inverse().get(comparator); if (desc) { - a = (byte) Character.toUpperCase(a); + a = (byte) Character.toUpperCase((char) a); } out.writeShort((short) (0x8000 | a)); } else { From 74fc2678ab93eafdb20d2e2e449ae8e9c5851d56 Mon Sep 17 00:00:00 2001 From: patricioe Date: Sun, 27 Mar 2011 21:55:39 -0700 Subject: [PATCH 054/144] Add Hector implementation of CounterSuperColumn --- .../model/HCounterSuperColumnImpl.java | 147 ++++++++++++++++++ 1 file changed, 147 insertions(+) create mode 100644 core/src/main/java/me/prettyprint/cassandra/model/HCounterSuperColumnImpl.java diff --git a/core/src/main/java/me/prettyprint/cassandra/model/HCounterSuperColumnImpl.java b/core/src/main/java/me/prettyprint/cassandra/model/HCounterSuperColumnImpl.java new file mode 100644 index 000000000..f091998c8 --- /dev/null +++ b/core/src/main/java/me/prettyprint/cassandra/model/HCounterSuperColumnImpl.java @@ -0,0 +1,147 @@ +package me.prettyprint.cassandra.model; + +import static me.prettyprint.cassandra.utils.Assert.noneNull; +import static me.prettyprint.cassandra.utils.Assert.notNull; + +import java.nio.ByteBuffer; +import java.util.ArrayList; +import java.util.List; + +import me.prettyprint.hector.api.Serializer; +import me.prettyprint.hector.api.beans.HCounterColumn; +import me.prettyprint.hector.api.beans.HCounterSuperColumn; + +import org.apache.cassandra.thrift.CounterColumn; +import org.apache.cassandra.thrift.CounterSuperColumn; + +/** + * Models a CounterSuperColumn in a protocol independent manner. + * + * @param + * CounterSuperColumn name type + * @param + * CounterColumn name type + + * + * @author patricioe + */ +public final class HCounterSuperColumnImpl implements HCounterSuperColumn { + + private SN superName; + private List> counterColumns; + private final Serializer superNameSerializer; + private final Serializer nameSerializer; + + /** + * @param CounterSuperColumn name type + * @param List> CounterColumn values + * @param Serializer the serializer type + */ + public HCounterSuperColumnImpl(SN sName, List> counterColumns, + Serializer sNameSerializer, Serializer nameSerializer) { + this(sNameSerializer, nameSerializer); + notNull(sName, "Name is null"); + notNull(counterColumns, "Columns are null"); + this.superName = sName; + this.counterColumns = counterColumns; + } + + public HCounterSuperColumnImpl(CounterSuperColumn thriftCounterSuperColumn, Serializer sNameSerializer, + Serializer nameSerializer) { + this(sNameSerializer, nameSerializer); + noneNull(thriftCounterSuperColumn, sNameSerializer, nameSerializer); + superName = sNameSerializer.fromByteBuffer(ByteBuffer.wrap(thriftCounterSuperColumn.getName())); + counterColumns = fromThriftColumns(thriftCounterSuperColumn.getColumns()); + } + + /*package*/ HCounterSuperColumnImpl(Serializer sNameSerializer, Serializer nameSerializer) { + noneNull(sNameSerializer, nameSerializer); + this.superNameSerializer = sNameSerializer; + this.nameSerializer = nameSerializer; + } + + @Override + public HCounterSuperColumn setName(SN name) { + notNull(name, "name is null"); + this.superName = name; + return this; + } + + @Override + public HCounterSuperColumn setSubcolumns(List> counterSubcolumns) { + notNull(counterSubcolumns, "subcolumns are null"); + this.counterColumns = counterSubcolumns; + return this; + } + + @Override + public int getSize() { + return counterColumns == null ? 0 : counterColumns.size(); + } + + @Override + public SN getName() { + return superName; + } + + /** + * + * @return an unmodifiable list of counterColumns + */ + @Override + public List> getColumns() { + return counterColumns; + } + + @Override + public HCounterColumn get(int i) { + return counterColumns.get(i); + } + + @Override + public Serializer getNameSerializer() { + return superNameSerializer; + } + + @Override + public byte[] getNameBytes() { + return superNameSerializer.toByteBuffer(getName()).array(); + } + + public ByteBuffer getNameByteBuffer() { + return superNameSerializer.toByteBuffer(getName()); + } + + public CounterSuperColumn toThrift() { + if (superName == null || counterColumns == null) { + return null; + } + return new CounterSuperColumn(superNameSerializer.toByteBuffer(superName), toThriftColumn()); + } + + private List toThriftColumn() { + List ret = new ArrayList(counterColumns.size()); + for (HCounterColumn c: counterColumns) { + ret.add(((HCounterColumnImpl) c).toThrift()); + } + return ret; + } + + private List> fromThriftColumns(List tcolumns) { + List> cs = new ArrayList>(tcolumns.size()); + for (CounterColumn c: tcolumns) { + cs.add(new HCounterColumnImpl(c, nameSerializer)); + } + return cs; + } + + @Override + public Serializer getSuperNameSerializer() { + return superNameSerializer; + } + + @Override + public String toString() { + return String.format("HCounterSuperColumn(%s,%s)", superName, counterColumns); + } +} From 7829cad9c07062519023186d41a887e478c5d318 Mon Sep 17 00:00:00 2001 From: patricioe Date: Sun, 27 Mar 2011 21:56:35 -0700 Subject: [PATCH 055/144] Remove CounterMutator as we to send all mutation together We need the counter mutation to be part of the regular mutation because what we need to achieve is to be able to mutate regualar columns and superColumn as well as Counters. --- .../hector/api/mutation/CounterMutator.java | 101 ------------------ 1 file changed, 101 deletions(-) delete mode 100644 core/src/main/java/me/prettyprint/hector/api/mutation/CounterMutator.java diff --git a/core/src/main/java/me/prettyprint/hector/api/mutation/CounterMutator.java b/core/src/main/java/me/prettyprint/hector/api/mutation/CounterMutator.java deleted file mode 100644 index 0bfdec329..000000000 --- a/core/src/main/java/me/prettyprint/hector/api/mutation/CounterMutator.java +++ /dev/null @@ -1,101 +0,0 @@ -package me.prettyprint.hector.api.mutation; - -import me.prettyprint.hector.api.Serializer; -import me.prettyprint.hector.api.beans.HColumn; -import me.prettyprint.hector.api.beans.HCounterColumn; -import me.prettyprint.hector.api.beans.HCounterSuperColumn; -import me.prettyprint.hector.api.beans.HSuperColumn; - -/** - * A Mutator inserts or deletes counter values from the cluster. - * There are two main ways to use a mutator: - * 1. Use the increment/decrement/delete methods to immediately increment, decrement or delete values. - * or 2. Use the addIncrement/addDecrement/addDeletion methods to schedule batch operations and then execute() - * all of them in batch. - * - * The class is not thread-safe. - * - * @author patricioe (patricioe@gmail.com) - */ -public interface CounterMutator { - - // Simple and immediate insertion of a column - MutationResult increment(final K key, final String cf, final HCounterColumn c); - - MutationResult decrement(final K key, final String cf, final HCounterColumn c); - - // overloaded insert-super - MutationResult increment(final K key, final String cf, final HCounterSuperColumn superColumn); - - MutationResult decrement(final K key, final String cf, final HCounterSuperColumn superColumn); - - MutationResult delete(final K key, final String cf, final N columnName, - final Serializer nameSerializer); - - /** - * Deletes a subcolumn of a supercolumn for a counter - * @param super column type - * @param subcolumn type - */ - MutationResult subDelete(final K key, final String cf, final SN supercolumnName, - final N columnName, final Serializer sNameSerializer, final Serializer nameSerializer); - - /** - * Schedule an increment of a CounterColumn to be inserted in batch mode by {@link #execute()} - */ - CounterMutator addIncrement(K key, String cf, HCounterColumn c); - - /** - * Schedule an increment of a SuperColumn to be inserted in batch mode by {@link #execute()} - */ - CounterMutator addIncrement(K key, String cf, HCounterSuperColumn sc); - - /** - * Schedule a decrement of a CounterColumn to be inserted in batch mode by {@link #execute()} - */ - CounterMutator addDecrement(K key, String cf, HCounterColumn c); - - /** - * Schedule a decrement of a SuperColumn to be inserted in batch mode by {@link #execute()} - */ - CounterMutator addDecrement(K key, String cf, HCounterSuperColumn sc); - - /** - * Adds a Deletion to the underlying batch_mutate call. The columnName argument can be null - * in which case the whole row being deleted. - * - * @param column name type - * @param key row key - * @param cf column family - * @param counterColumnName column name. Use null to delete the whole row. - * @param nameSerializer a name serializer - * @return a mutator - */ - CounterMutator addDeletion(K key, String cf, N counterColumnName, Serializer nameSerializer); - - /** - * Alternate form for easy deletion of the whole row. - * - * @param - * @param key - * @return - */ - CounterMutator addDeletion(K key, String cf); - - - CounterMutator addSubDeletion(K key, String cf, HCounterSuperColumn sc); - - - /** - * Batch executes all mutations scheduled to this Mutator instance by addIncrement, addDecrement and addDeletion etc. - * May throw a HectorException which is a RuntimeException. - * @return A MutationResult holds the status. - */ - MutationResult execute(); - - /** - * Discards all pending mutations. - */ - CounterMutator discardPendingMutations(); - -} \ No newline at end of file From d4bcb3e1af52845233d11eef9b8512c782ecb687 Mon Sep 17 00:00:00 2001 From: patricioe Date: Sun, 27 Mar 2011 22:10:45 -0700 Subject: [PATCH 056/144] CounterMutator is no longer needed in HFactory. --- .../prettyprint/hector/api/factory/HFactory.java | 14 -------------- 1 file changed, 14 deletions(-) diff --git a/core/src/main/java/me/prettyprint/hector/api/factory/HFactory.java b/core/src/main/java/me/prettyprint/hector/api/factory/HFactory.java index faa6fb90b..3ea960f62 100644 --- a/core/src/main/java/me/prettyprint/hector/api/factory/HFactory.java +++ b/core/src/main/java/me/prettyprint/hector/api/factory/HFactory.java @@ -47,7 +47,6 @@ import me.prettyprint.hector.api.ddl.ColumnFamilyDefinition; import me.prettyprint.hector.api.ddl.ComparatorType; import me.prettyprint.hector.api.ddl.KeyspaceDefinition; -import me.prettyprint.hector.api.mutation.CounterMutator; import me.prettyprint.hector.api.mutation.Mutator; import me.prettyprint.hector.api.query.ColumnQuery; import me.prettyprint.hector.api.query.CountQuery; @@ -313,19 +312,6 @@ public static Mutator createMutator(Keyspace keyspace, Serializer keySerializer) { return new MutatorImpl(keyspace, keySerializer); } - - - /** - * Creates a counter mutator for updating counters in a keyspace. - * - * @param keyspace - * @param keySerializer - */ - public static CounterMutator createCounterMutator(Keyspace keyspace, - Serializer keySerializer) { - return null; // TODO (patricioe) to be implemented. - //return new CounterMutatorImpl(keyspace, keySerializer); - } public static ColumnQuery createColumnQuery( Keyspace keyspace, Serializer keySerializer, From 98e687aef1a0c2503069eb5c8c87be2cd0c939e1 Mon Sep 17 00:00:00 2001 From: patricioe Date: Sun, 27 Mar 2011 23:47:04 -0700 Subject: [PATCH 057/144] Add Keyspace support and tes for counter add, get and remove --- .../cassandra/service/KeyspaceService.java | 28 +++++++ .../service/KeyspaceServiceImpl.java | 80 +++++++++++++++++++ .../cassandra/service/KeyspaceTest.java | 64 +++++++++++++++ 3 files changed, 172 insertions(+) diff --git a/core/src/main/java/me/prettyprint/cassandra/service/KeyspaceService.java b/core/src/main/java/me/prettyprint/cassandra/service/KeyspaceService.java index bb7596e9c..1ab4fad68 100644 --- a/core/src/main/java/me/prettyprint/cassandra/service/KeyspaceService.java +++ b/core/src/main/java/me/prettyprint/cassandra/service/KeyspaceService.java @@ -11,6 +11,8 @@ import org.apache.cassandra.thrift.Column; import org.apache.cassandra.thrift.ColumnParent; import org.apache.cassandra.thrift.ColumnPath; +import org.apache.cassandra.thrift.Counter; +import org.apache.cassandra.thrift.CounterColumn; import org.apache.cassandra.thrift.IndexClause; import org.apache.cassandra.thrift.KeyRange; import org.apache.cassandra.thrift.Mutation; @@ -46,6 +48,18 @@ public interface KeyspaceService { Column getColumn(ByteBuffer key, ColumnPath columnPath) throws HectorException; Column getColumn(String key, ColumnPath columnPath) throws HectorException; + + /** + * Get the Counter at the given columnPath. + * + * If no value is present, NotFoundException is thrown. + * + * @throws HNotFoundException + * if no value exists for the counter + */ + Counter getCounter(ByteBuffer key, ColumnPath columnPath) throws HectorException; + + Counter getCounter(String key, ColumnPath columnPath) throws HectorException; /** * Get the SuperColumn at the given columnPath. @@ -146,6 +160,16 @@ Map> multigetSuperSlice(List keys, void insert(String key, ColumnPath columnPath, ByteBuffer value) throws HectorException; void insert(String key, ColumnPath columnPath, ByteBuffer value, long timestamp) throws HectorException; + + /** + * Add a counter with CL.ONE + */ + void addCounter(ByteBuffer key, ColumnParent columnParent, CounterColumn counterColumn) throws HectorException; + + /** + * Add a counter with CL.ONE + */ + void addCounter(String key, ColumnParent columnParent, CounterColumn counterColumn) throws HectorException; /** * Call batch mutate with the assembled mutationMap. This method is a direct pass-through @@ -169,6 +193,10 @@ Map> multigetSuperSlice(List keys, void remove(String key, ColumnPath columnPath) throws HectorException; void remove(String key, ColumnPath columnPath, long timestamp) throws HectorException; + + void removeCounter(ByteBuffer key, ColumnPath columnPath) throws HectorException; + + void removeCounter(String key, ColumnPath columnPath) throws HectorException; /** diff --git a/core/src/main/java/me/prettyprint/cassandra/service/KeyspaceServiceImpl.java b/core/src/main/java/me/prettyprint/cassandra/service/KeyspaceServiceImpl.java index 601d487dc..a6874bacf 100644 --- a/core/src/main/java/me/prettyprint/cassandra/service/KeyspaceServiceImpl.java +++ b/core/src/main/java/me/prettyprint/cassandra/service/KeyspaceServiceImpl.java @@ -22,6 +22,8 @@ import org.apache.cassandra.thrift.ColumnParent; import org.apache.cassandra.thrift.ColumnPath; import org.apache.cassandra.thrift.ConsistencyLevel; +import org.apache.cassandra.thrift.Counter; +import org.apache.cassandra.thrift.CounterColumn; import org.apache.cassandra.thrift.IndexClause; import org.apache.cassandra.thrift.KeyRange; import org.apache.cassandra.thrift.KeySlice; @@ -352,6 +354,29 @@ public Void execute(Cassandra.Client cassandra) throws HectorException { }; operateWithFailover(op); } + + @Override + public void addCounter(final ByteBuffer key, final ColumnParent columnParent, final CounterColumn counterColumn) + throws HectorException { + Operation op = new Operation(OperationType.WRITE, failoverPolicy, keyspaceName, credentials) { + + @Override + public Void execute(Cassandra.Client cassandra) throws HectorException { + try { + cassandra.add(key, columnParent, counterColumn, ThriftConverter.consistencyLevel(HConsistencyLevel.ONE)); + return null; + } catch (Exception e) { + throw xtrans.translate(e); + } + } + }; + operateWithFailover(op); + } + + @Override + public void addCounter(String key, ColumnParent columnParent, CounterColumn counterColumn) throws HectorException { + addCounter(StringSerializer.get().toByteBuffer(key), columnParent, counterColumn); + } @Override public void insert(String key, ColumnPath columnPath, ByteBuffer value) throws HectorException { @@ -559,6 +584,29 @@ public Void execute(Cassandra.Client cassandra) throws HectorException { }; operateWithFailover(op); } + + @Override + public void removeCounter(final ByteBuffer key, final ColumnPath columnPath) throws HectorException { + Operation op = new Operation(OperationType.WRITE, failoverPolicy, keyspaceName, credentials) { + + @Override + public Void execute(Cassandra.Client cassandra) throws HectorException { + try { + // NOTE" CL is ONE for counters + cassandra.remove_counter(key, columnPath, ThriftConverter.consistencyLevel(HConsistencyLevel.ONE)); + return null; + } catch (Exception e) { + throw xtrans.translate(e); + } + } + }; + operateWithFailover(op); + } + + @Override + public void removeCounter(String key, ColumnPath columnPath) throws HectorException { + removeCounter(StringSerializer.get().toByteBuffer(key), columnPath); + } @Override public void remove(String key, ColumnPath columnPath) throws HectorException { @@ -606,7 +654,37 @@ public Column execute(Cassandra.Client cassandra) throws HectorException { throw op.getException(); } return op.getResult(); + } + + @Override + public Counter getCounter(final ByteBuffer key, final ColumnPath columnPath) throws HectorException { + Operation op = new Operation(OperationType.READ, failoverPolicy, keyspaceName, credentials) { + + @Override + public Counter execute(Cassandra.Client cassandra) throws HectorException { + Counter cosc; + try { + cosc = cassandra.get_counter(key, columnPath, ThriftConverter.consistencyLevel(HConsistencyLevel.ONE)); + } catch (NotFoundException e) { + setException(xtrans.translate(e)); + return null; + } catch (Exception e) { + throw xtrans.translate(e); + } + return cosc; + } + }; + operateWithFailover(op); + if (op.hasException()) { + throw op.getException(); + } + return op.getResult(); + } + + @Override + public Counter getCounter(String key, ColumnPath columnPath) throws HectorException { + return getCounter(StringSerializer.get().toByteBuffer(key), columnPath); } @Override @@ -668,4 +746,6 @@ public String toString() { b.append(">"); return b.toString(); } + + } diff --git a/core/src/test/java/me/prettyprint/cassandra/service/KeyspaceTest.java b/core/src/test/java/me/prettyprint/cassandra/service/KeyspaceTest.java index d7ded05a1..d17121ff0 100644 --- a/core/src/test/java/me/prettyprint/cassandra/service/KeyspaceTest.java +++ b/core/src/test/java/me/prettyprint/cassandra/service/KeyspaceTest.java @@ -29,6 +29,8 @@ import org.apache.cassandra.thrift.ColumnParent; import org.apache.cassandra.thrift.ColumnPath; //import org.apache.cassandra.thrift.ConsistencyLevel; +import org.apache.cassandra.thrift.Counter; +import org.apache.cassandra.thrift.CounterColumn; import org.apache.cassandra.thrift.Deletion; import org.apache.cassandra.thrift.KeyRange; import org.apache.cassandra.thrift.Mutation; @@ -99,6 +101,68 @@ public void testInsertAndGetAndRemove() throws IllegalArgumentException, NoSuchE } } } + + @Test + public void testInsertAndGetAndRemoveCounter() throws IllegalArgumentException, NoSuchElementException, + IllegalStateException, HNotFoundException, Exception { + + StringSerializer ss = StringSerializer.get(); + // insert value + ColumnParent cp = new ColumnParent("Counter1"); + //cp.setColumn(bytes("testInsertAndGetAndRemoveCounter")); + + // Insert 3 counters for the same key + keyspace.addCounter("testInsertAndGetAndRemoveCounter_key1", cp, createCounterColumn("A", 5L)); + keyspace.addCounter("testInsertAndGetAndRemoveCounter_key1", cp, createCounterColumn("A", -1L)); + keyspace.addCounter("testInsertAndGetAndRemoveCounter_key1", cp, createCounterColumn("B", 10L)); + // Total for counter A is (5 - 1) = 4. + // Total for counter B is 10. + + ColumnPath cph = new ColumnPath("Counter1"); + cph.setColumn(ss.toByteBuffer("A")); + Counter counter = keyspace.getCounter("testInsertAndGetAndRemoveCounter_key1", cph); + assertNotNull(counter); + assertEquals(4, counter.getColumn().value); + + cph.setColumn(ss.toByteBuffer("B")); + counter = keyspace.getCounter("testInsertAndGetAndRemoveCounter_key1", cph); + assertNotNull(counter); + assertEquals(10, counter.getColumn().value); + + // Reuse the ColumnPath associated to Conter B and remove only B. + keyspace.removeCounter("testInsertAndGetAndRemoveCounter_key1", cph); + + // Fetch it and it should not exist + try { + keyspace.getCounter("testInsertAndGetAndRemoveCounter_key1", cph); + } catch (HNotFoundException e) { + // good + } + + // Fetch Counter A again to verify I did not delete it + cph.setColumn(ss.toByteBuffer("A")); + counter = keyspace.getCounter("testInsertAndGetAndRemoveCounter_key1", cph); + assertNotNull(counter); + + // Delete the whole row + cph.column = null; + keyspace.removeCounter("testInsertAndGetAndRemoveCounter_key1", cph); + + // Fetch A again to verify it is not there. + try { + cph.setColumn(ss.toByteBuffer("A")); + counter = keyspace.getCounter("testInsertAndGetAndRemoveCounter_key1", cph); + } catch (HNotFoundException e) { + // good + } + } + + private CounterColumn createCounterColumn(String name, long value) { + CounterColumn cc = new CounterColumn(); + cc.setName(StringSerializer.get().toByteBuffer(name)); + cc.setValue(value); + return cc; + } /** * Test insertion of a supercolumn using insert From a4d46ed473adc3db1011acf5770c1392692240a5 Mon Sep 17 00:00:00 2001 From: patricioe Date: Mon, 28 Mar 2011 09:23:02 -0700 Subject: [PATCH 058/144] Add convenient methods for creating a counter column --- .../hector/api/factory/HFactory.java | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/core/src/main/java/me/prettyprint/hector/api/factory/HFactory.java b/core/src/main/java/me/prettyprint/hector/api/factory/HFactory.java index 3ea960f62..06e5a6c16 100644 --- a/core/src/main/java/me/prettyprint/hector/api/factory/HFactory.java +++ b/core/src/main/java/me/prettyprint/hector/api/factory/HFactory.java @@ -7,6 +7,7 @@ import me.prettyprint.cassandra.model.ExecutingKeyspace; import me.prettyprint.cassandra.model.ExecutingVirtualKeyspace; import me.prettyprint.cassandra.model.HColumnImpl; +import me.prettyprint.cassandra.model.HCounterColumnImpl; import me.prettyprint.cassandra.model.HSuperColumnImpl; import me.prettyprint.cassandra.model.IndexedSlicesQuery; import me.prettyprint.cassandra.model.MutatorImpl; @@ -42,6 +43,7 @@ import me.prettyprint.hector.api.Keyspace; import me.prettyprint.hector.api.Serializer; import me.prettyprint.hector.api.beans.HColumn; +import me.prettyprint.hector.api.beans.HCounterColumn; import me.prettyprint.hector.api.beans.HSuperColumn; import me.prettyprint.hector.api.ddl.ColumnDefinition; import me.prettyprint.hector.api.ddl.ColumnFamilyDefinition; @@ -487,6 +489,21 @@ public static HColumn createStringColumn(String name, StringSerializer se = StringSerializer.get(); return createColumn(name, value, se, se); } + + /** + * Create a counter column with a name and long value + */ + public static HCounterColumn createCounterColumn(N name, long value, Serializer nameSerializer) { + return new HCounterColumnImpl(name, value, nameSerializer); + } + + /** + * Convenient method for creating a counter column with a String name and long value + */ + public static HCounterColumn createCounterColumn(String name, long value) { + StringSerializer se = StringSerializer.get(); + return createCounterColumn(name, value, se); + } /** * Creates a clock of now with the default clock resolution (micorosec) as From f8b40dc9990393f854cd700c0ae488d15d2bf096 Mon Sep 17 00:00:00 2001 From: patricioe Date: Mon, 28 Mar 2011 09:32:06 -0700 Subject: [PATCH 059/144] Add convenient mehtod to create a HCounterSuperColumn --- .../java/me/prettyprint/hector/api/factory/HFactory.java | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/core/src/main/java/me/prettyprint/hector/api/factory/HFactory.java b/core/src/main/java/me/prettyprint/hector/api/factory/HFactory.java index 06e5a6c16..fc0713c1c 100644 --- a/core/src/main/java/me/prettyprint/hector/api/factory/HFactory.java +++ b/core/src/main/java/me/prettyprint/hector/api/factory/HFactory.java @@ -8,6 +8,7 @@ import me.prettyprint.cassandra.model.ExecutingVirtualKeyspace; import me.prettyprint.cassandra.model.HColumnImpl; import me.prettyprint.cassandra.model.HCounterColumnImpl; +import me.prettyprint.cassandra.model.HCounterSuperColumnImpl; import me.prettyprint.cassandra.model.HSuperColumnImpl; import me.prettyprint.cassandra.model.IndexedSlicesQuery; import me.prettyprint.cassandra.model.MutatorImpl; @@ -44,6 +45,7 @@ import me.prettyprint.hector.api.Serializer; import me.prettyprint.hector.api.beans.HColumn; import me.prettyprint.hector.api.beans.HCounterColumn; +import me.prettyprint.hector.api.beans.HCounterSuperColumn; import me.prettyprint.hector.api.beans.HSuperColumn; import me.prettyprint.hector.api.ddl.ColumnDefinition; import me.prettyprint.hector.api.ddl.ColumnFamilyDefinition; @@ -464,6 +466,11 @@ public static HSuperColumn createSuperColumn(SN name, return new HSuperColumnImpl(name, columns, clock, superNameSerializer, nameSerializer, valueSerializer); } + + public static HCounterSuperColumn createCounterSuperColumn(SN name, + List> columns, Serializer superNameSerializer, Serializer nameSerializer) { + return new HCounterSuperColumnImpl(name, columns, superNameSerializer, nameSerializer); + } public static HColumn createColumn(N name, V value, long clock, Serializer nameSerializer, Serializer valueSerializer) { From 33e3dc2e620de7393a1ef4c2ee09efbb42c30959 Mon Sep 17 00:00:00 2001 From: zznate Date: Mon, 28 Mar 2011 16:15:33 -0500 Subject: [PATCH 060/144] encapsulated connection impl for template service --- .../template/ColumnFamilyStringTemplate.java | 42 ------- .../template/ColumnFamilyTemplate.java | 71 ++--------- .../template/ThriftColumnFamilyTemplate.java | 110 ++++++++++++++++++ .../template/ColumnFamilyTemplateTest.java | 6 +- 4 files changed, 121 insertions(+), 108 deletions(-) delete mode 100644 core/src/main/java/me/prettyprint/cassandra/service/template/ColumnFamilyStringTemplate.java create mode 100644 core/src/main/java/me/prettyprint/cassandra/service/template/ThriftColumnFamilyTemplate.java diff --git a/core/src/main/java/me/prettyprint/cassandra/service/template/ColumnFamilyStringTemplate.java b/core/src/main/java/me/prettyprint/cassandra/service/template/ColumnFamilyStringTemplate.java deleted file mode 100644 index eaad5234b..000000000 --- a/core/src/main/java/me/prettyprint/cassandra/service/template/ColumnFamilyStringTemplate.java +++ /dev/null @@ -1,42 +0,0 @@ -package me.prettyprint.cassandra.service.template; - -import me.prettyprint.cassandra.serializers.StringSerializer; -import me.prettyprint.hector.api.Keyspace; -import me.prettyprint.hector.api.mutation.Mutator; - -/** - * A simple specialization of the generic class for the very common all-string - * column name and key type scenario. This just provided the StringSerializer at - * all the places required by the generic base type. There are method overloads - * for update/query that remove the need to pass serializers anywhere. - * - * @author david - * @since Mar 10, 2011 - */ -public class ColumnFamilyStringTemplate extends ColumnFamilyTemplate { - - public ColumnFamilyStringTemplate(Keyspace keyspace, String columnFamily) { - super(keyspace, columnFamily, StringSerializer.get(), StringSerializer - .get()); - } - - public ColumnFamilyStringTemplate(Keyspace keyspace, String columnFamily, - Mutator mutator) { - super(keyspace, columnFamily, StringSerializer.get(), StringSerializer - .get(), mutator); - } - - // Just so method chaining will return this type instead of the parent class - // for operations down the chain - public ColumnFamilyStringTemplate setBatched(boolean batched) { - super.setBatched(batched); - return this; - } - - // Just so method chaining will return this type instead of the parent class - // for operations down the chain - public ColumnFamilyStringTemplate setMutator(Mutator mutator) { - super.setMutator(mutator); - return this; - } -} diff --git a/core/src/main/java/me/prettyprint/cassandra/service/template/ColumnFamilyTemplate.java b/core/src/main/java/me/prettyprint/cassandra/service/template/ColumnFamilyTemplate.java index 847c424f8..b8bf9c95c 100644 --- a/core/src/main/java/me/prettyprint/cassandra/service/template/ColumnFamilyTemplate.java +++ b/core/src/main/java/me/prettyprint/cassandra/service/template/ColumnFamilyTemplate.java @@ -47,7 +47,7 @@ * @param * The column family name type */ -public class ColumnFamilyTemplate extends AbstractColumnFamilyTemplate { +public abstract class ColumnFamilyTemplate extends AbstractColumnFamilyTemplate { public ColumnFamilyTemplate(Keyspace keyspace, String columnFamily, Serializer keySerializer, Serializer topSerializer) { @@ -156,7 +156,7 @@ public T queryColumns(K key, N start, N end, predicate.setStartOn(start); predicate.setEndOn(end); predicate.setCount(100); - return executeSliceQuery(key, predicate, mapper); + return doExecuteSlice(key, predicate, mapper); } /** @@ -174,7 +174,7 @@ public T queryColumns(K key, List columns, ColumnFamilyRowMapper mapper) { HSlicePredicate predicate = new HSlicePredicate(topSerializer); predicate.setColumnNames(columns); - return executeSliceQuery(key, predicate, mapper); + return doExecuteSlice(key, predicate, mapper); } @@ -220,70 +220,15 @@ public HColumn querySingleColumn(K key, N columnName, //-------------------------- delegation methods ---------------------------- - private T executeSliceQuery(K key, HSlicePredicate predicate, ColumnFamilyRowMapper mapper) { - return mapper.mapRow(doExecuteSlice(key,predicate)); - } + protected abstract T doExecuteSlice(K key, HSlicePredicate predicate, ColumnFamilyRowMapper mapper); - private ColumnFamilyResultWrapper doExecuteSlice(final K key, final HSlicePredicate workingSlicePredicate) { - return new ColumnFamilyResultWrapper(keySerializer, topSerializer, - sliceInternal(key, workingSlicePredicate)); - } + protected abstract ColumnFamilyResultWrapper doExecuteSlice(final K key, final HSlicePredicate workingSlicePredicate); - private ColumnFamilyResultWrapper doExecuteMultigetSlice(final Iterable keys, final HSlicePredicate workingSlicePredicate) { - return new ColumnFamilyResultWrapper(keySerializer, topSerializer, - multigetSliceInternal(keys, workingSlicePredicate)); - } + protected abstract ColumnFamilyResultWrapper doExecuteMultigetSlice(final Iterable keys, final HSlicePredicate workingSlicePredicate); - private MappedColumnFamilyResult doExecuteMultigetSlice(final Iterable keys, + protected abstract MappedColumnFamilyResult doExecuteMultigetSlice(final Iterable keys, final HSlicePredicate workingSlicePredicate, - final ColumnFamilyRowMapper mapper) { - return new MappedColumnFamilyResultWrapper(keySerializer, topSerializer, - multigetSliceInternal(keys, workingSlicePredicate), mapper); - } + final ColumnFamilyRowMapper mapper); - private ExecutionResult>> sliceInternal(final K key, - final HSlicePredicate workingSlicePredicate) { - return ((ExecutingKeyspace)keyspace).doExecuteOperation(new Operation>>(OperationType.READ) { - @Override - public Map> execute(Cassandra.Client cassandra) throws HectorException { - Map> cosc = new LinkedHashMap>(); - try { - - ByteBuffer sKey = keySerializer.toByteBuffer(key); - cosc.put(sKey, cassandra.get_slice(sKey, columnParent, - workingSlicePredicate.toThrift(), - ThriftConverter.consistencyLevel(consistencyLevelPolicy.get(operationType)))); - - } catch (Exception e) { - throw exceptionsTranslator.translate(e); - } - - return cosc; - } - }); - } - - private ExecutionResult>> multigetSliceInternal(final Iterable keys, - final HSlicePredicate workingSlicePredicate) { - return ((ExecutingKeyspace)keyspace).doExecuteOperation(new Operation>>(OperationType.READ) { - @Override - public Map> execute(Cassandra.Client cassandra) throws HectorException { - Map> cosc = new LinkedHashMap>(); - try { - List keyList = new ArrayList(); - Iterators.addAll(keyList, keys.iterator()); - cosc = cassandra.multiget_slice(keySerializer.toBytesList(keyList), columnParent, - (workingSlicePredicate == null ? activeSlicePredicate.setColumnNames(columnValueSerializers.keySet()).toThrift() : workingSlicePredicate.toThrift()), - ThriftConverter.consistencyLevel(consistencyLevelPolicy.get(operationType))); - } catch (Exception e) { - throw exceptionsTranslator.translate(e); - } - - return cosc; - } - }); - } - - } diff --git a/core/src/main/java/me/prettyprint/cassandra/service/template/ThriftColumnFamilyTemplate.java b/core/src/main/java/me/prettyprint/cassandra/service/template/ThriftColumnFamilyTemplate.java new file mode 100644 index 000000000..959abf635 --- /dev/null +++ b/core/src/main/java/me/prettyprint/cassandra/service/template/ThriftColumnFamilyTemplate.java @@ -0,0 +1,110 @@ +package me.prettyprint.cassandra.service.template; + +import java.nio.ByteBuffer; +import java.util.ArrayList; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; + +import me.prettyprint.cassandra.model.ExecutingKeyspace; +import me.prettyprint.cassandra.model.ExecutionResult; +import me.prettyprint.cassandra.model.HSlicePredicate; +import me.prettyprint.cassandra.model.thrift.ThriftConverter; +import me.prettyprint.cassandra.service.Operation; +import me.prettyprint.cassandra.service.OperationType; +import me.prettyprint.hector.api.Keyspace; +import me.prettyprint.hector.api.Serializer; +import me.prettyprint.hector.api.exceptions.HectorException; +import me.prettyprint.hector.api.mutation.Mutator; + +import org.apache.cassandra.thrift.Cassandra; +import org.apache.cassandra.thrift.ColumnOrSuperColumn; + +import com.google.common.collect.Iterators; + +/** + * Thrift specific implementation of {@link ColumnFamilyTemplate} + * @author nate + * + * @param + * @param + */ +public class ThriftColumnFamilyTemplate extends ColumnFamilyTemplate { + + + public ThriftColumnFamilyTemplate(Keyspace keyspace, String columnFamily, + Serializer keySerializer, Serializer topSerializer) { + super(keyspace, columnFamily, keySerializer, topSerializer); + } + + public ThriftColumnFamilyTemplate(Keyspace keyspace, String columnFamily, + Serializer keySerializer, Serializer topSerializer, + Mutator mutator) { + super(keyspace, columnFamily, keySerializer, topSerializer, mutator); + } + + public T doExecuteSlice(K key, HSlicePredicate predicate, ColumnFamilyRowMapper mapper) { + return mapper.mapRow(doExecuteSlice(key,predicate)); + } + + public ColumnFamilyResultWrapper doExecuteSlice(final K key, final HSlicePredicate workingSlicePredicate) { + return new ColumnFamilyResultWrapper(keySerializer, topSerializer, + sliceInternal(key, workingSlicePredicate)); + } + + public ColumnFamilyResultWrapper doExecuteMultigetSlice(final Iterable keys, final HSlicePredicate workingSlicePredicate) { + return new ColumnFamilyResultWrapper(keySerializer, topSerializer, + multigetSliceInternal(keys, workingSlicePredicate)); + } + + public MappedColumnFamilyResult doExecuteMultigetSlice(final Iterable keys, + final HSlicePredicate workingSlicePredicate, + final ColumnFamilyRowMapper mapper) { + return new MappedColumnFamilyResultWrapper(keySerializer, topSerializer, + multigetSliceInternal(keys, workingSlicePredicate), mapper); + } + + + private ExecutionResult>> sliceInternal(final K key, + final HSlicePredicate workingSlicePredicate) { + return ((ExecutingKeyspace)keyspace).doExecuteOperation(new Operation>>(OperationType.READ) { + @Override + public Map> execute(Cassandra.Client cassandra) throws HectorException { + Map> cosc = new LinkedHashMap>(); + try { + + ByteBuffer sKey = keySerializer.toByteBuffer(key); + cosc.put(sKey, cassandra.get_slice(sKey, columnParent, + workingSlicePredicate.toThrift(), + ThriftConverter.consistencyLevel(consistencyLevelPolicy.get(operationType)))); + + } catch (Exception e) { + throw exceptionsTranslator.translate(e); + } + + return cosc; + } + }); + } + + private ExecutionResult>> multigetSliceInternal(final Iterable keys, + final HSlicePredicate workingSlicePredicate) { + return ((ExecutingKeyspace)keyspace).doExecuteOperation(new Operation>>(OperationType.READ) { + @Override + public Map> execute(Cassandra.Client cassandra) throws HectorException { + Map> cosc = new LinkedHashMap>(); + try { + List keyList = new ArrayList(); + Iterators.addAll(keyList, keys.iterator()); + cosc = cassandra.multiget_slice(keySerializer.toBytesList(keyList), columnParent, + (workingSlicePredicate == null ? activeSlicePredicate.setColumnNames(columnValueSerializers.keySet()).toThrift() : workingSlicePredicate.toThrift()), + ThriftConverter.consistencyLevel(consistencyLevelPolicy.get(operationType))); + } catch (Exception e) { + throw exceptionsTranslator.translate(e); + } + + return cosc; + } + }); + } +} diff --git a/core/src/test/java/me/prettyprint/cassandra/service/template/ColumnFamilyTemplateTest.java b/core/src/test/java/me/prettyprint/cassandra/service/template/ColumnFamilyTemplateTest.java index 1ee472b22..da65de969 100644 --- a/core/src/test/java/me/prettyprint/cassandra/service/template/ColumnFamilyTemplateTest.java +++ b/core/src/test/java/me/prettyprint/cassandra/service/template/ColumnFamilyTemplateTest.java @@ -12,7 +12,7 @@ public class ColumnFamilyTemplateTest extends BaseColumnFamilyTemplateTest { @Test public void testCreateSelect() { - ColumnFamilyTemplate template = new ColumnFamilyTemplate(keyspace, "Standard1", se, se, HFactory.createMutator(keyspace, se)); + ColumnFamilyTemplate template = new ThriftColumnFamilyTemplate(keyspace, "Standard1", se, se, HFactory.createMutator(keyspace, se)); ColumnFamilyUpdater updater = template.createUpdater("key1"); updater.setString("column1","value1"); @@ -26,7 +26,7 @@ public void testCreateSelect() { @Test public void testCreateSelectTemplate() { - ColumnFamilyTemplate template = new ColumnFamilyTemplate(keyspace, "Standard1", se, se, HFactory.createMutator(keyspace, se)); + ColumnFamilyTemplate template = new ThriftColumnFamilyTemplate(keyspace, "Standard1", se, se, HFactory.createMutator(keyspace, se)); ColumnFamilyUpdater updater = template.createUpdater("key1"); updater.setString("column1","value1"); updater.update(); @@ -43,7 +43,7 @@ public String mapRow(ColumnFamilyResult results) { @Test public void testQueryMultiget() { - ColumnFamilyTemplate template = new ColumnFamilyTemplate(keyspace, "Standard1", se, se, HFactory.createMutator(keyspace, se)); + ColumnFamilyTemplate template = new ThriftColumnFamilyTemplate(keyspace, "Standard1", se, se, HFactory.createMutator(keyspace, se)); ColumnFamilyUpdater updater = template.createUpdater("mg_key1"); updater.setString("column1","value1"); updater.addKey("mg_key2"); From e091e0808038085ca0c15a7f0d4e2f9e38407bc8 Mon Sep 17 00:00:00 2001 From: zznate Date: Mon, 28 Mar 2011 17:29:00 -0500 Subject: [PATCH 061/144] update to ifaces for template result wrappers --- .../cassandra/service/template/ColumnFamilyTemplate.java | 8 ++++---- .../service/template/ThriftColumnFamilyTemplate.java | 4 ++-- .../service/template/ColumnFamilyTemplateTest.java | 4 ++-- 3 files changed, 8 insertions(+), 8 deletions(-) diff --git a/core/src/main/java/me/prettyprint/cassandra/service/template/ColumnFamilyTemplate.java b/core/src/main/java/me/prettyprint/cassandra/service/template/ColumnFamilyTemplate.java index b8bf9c95c..0e96941c6 100644 --- a/core/src/main/java/me/prettyprint/cassandra/service/template/ColumnFamilyTemplate.java +++ b/core/src/main/java/me/prettyprint/cassandra/service/template/ColumnFamilyTemplate.java @@ -126,11 +126,11 @@ public int countColumns(K key, N start, N end, int max) { return query.execute().get(); } - public ColumnFamilyResultWrapper queryColumns(K key) { + public ColumnFamilyResult queryColumns(K key) { return doExecuteSlice(key, activeSlicePredicate); } - public ColumnFamilyResultWrapper queryColumns(Iterable keys) { + public ColumnFamilyResult queryColumns(Iterable keys) { return doExecuteMultigetSlice(keys, activeSlicePredicate); } @@ -222,9 +222,9 @@ public HColumn querySingleColumn(K key, N columnName, protected abstract T doExecuteSlice(K key, HSlicePredicate predicate, ColumnFamilyRowMapper mapper); - protected abstract ColumnFamilyResultWrapper doExecuteSlice(final K key, final HSlicePredicate workingSlicePredicate); + protected abstract ColumnFamilyResult doExecuteSlice(final K key, final HSlicePredicate workingSlicePredicate); - protected abstract ColumnFamilyResultWrapper doExecuteMultigetSlice(final Iterable keys, final HSlicePredicate workingSlicePredicate); + protected abstract ColumnFamilyResult doExecuteMultigetSlice(final Iterable keys, final HSlicePredicate workingSlicePredicate); protected abstract MappedColumnFamilyResult doExecuteMultigetSlice(final Iterable keys, final HSlicePredicate workingSlicePredicate, diff --git a/core/src/main/java/me/prettyprint/cassandra/service/template/ThriftColumnFamilyTemplate.java b/core/src/main/java/me/prettyprint/cassandra/service/template/ThriftColumnFamilyTemplate.java index 959abf635..10595971a 100644 --- a/core/src/main/java/me/prettyprint/cassandra/service/template/ThriftColumnFamilyTemplate.java +++ b/core/src/main/java/me/prettyprint/cassandra/service/template/ThriftColumnFamilyTemplate.java @@ -47,12 +47,12 @@ public T doExecuteSlice(K key, HSlicePredicate predicate, ColumnFamilyRow return mapper.mapRow(doExecuteSlice(key,predicate)); } - public ColumnFamilyResultWrapper doExecuteSlice(final K key, final HSlicePredicate workingSlicePredicate) { + public ColumnFamilyResult doExecuteSlice(final K key, final HSlicePredicate workingSlicePredicate) { return new ColumnFamilyResultWrapper(keySerializer, topSerializer, sliceInternal(key, workingSlicePredicate)); } - public ColumnFamilyResultWrapper doExecuteMultigetSlice(final Iterable keys, final HSlicePredicate workingSlicePredicate) { + public ColumnFamilyResult doExecuteMultigetSlice(final Iterable keys, final HSlicePredicate workingSlicePredicate) { return new ColumnFamilyResultWrapper(keySerializer, topSerializer, multigetSliceInternal(keys, workingSlicePredicate)); } diff --git a/core/src/test/java/me/prettyprint/cassandra/service/template/ColumnFamilyTemplateTest.java b/core/src/test/java/me/prettyprint/cassandra/service/template/ColumnFamilyTemplateTest.java index da65de969..4e2ef948e 100644 --- a/core/src/test/java/me/prettyprint/cassandra/service/template/ColumnFamilyTemplateTest.java +++ b/core/src/test/java/me/prettyprint/cassandra/service/template/ColumnFamilyTemplateTest.java @@ -19,7 +19,7 @@ public void testCreateSelect() { template.update(updater); template.addColumn("column1", se); - ColumnFamilyResultWrapper wrapper = template.queryColumns("key1"); + ColumnFamilyResult wrapper = template.queryColumns("key1"); assertEquals("value1",wrapper.getString("column1")); } @@ -53,7 +53,7 @@ public void testQueryMultiget() { template.update(updater); template.addColumn("column1", se); - ColumnFamilyResultWrapper wrapper = template.queryColumns(Arrays.asList("mg_key1", "mg_key2", "mg_key3")); + ColumnFamilyResult wrapper = template.queryColumns(Arrays.asList("mg_key1", "mg_key2", "mg_key3")); assertEquals("value1",wrapper.getString("column1")); wrapper.next(); assertEquals("value2",wrapper.getString("column1")); From 0c097e5f02e621bffffd3573aa22d8c6649af582 Mon Sep 17 00:00:00 2001 From: zznate Date: Mon, 28 Mar 2011 21:27:13 -0500 Subject: [PATCH 062/144] made access level public for abstract class --- .../cassandra/service/template/AbstractResultWrapper.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/src/main/java/me/prettyprint/cassandra/service/template/AbstractResultWrapper.java b/core/src/main/java/me/prettyprint/cassandra/service/template/AbstractResultWrapper.java index f7aba13e0..674d46d39 100644 --- a/core/src/main/java/me/prettyprint/cassandra/service/template/AbstractResultWrapper.java +++ b/core/src/main/java/me/prettyprint/cassandra/service/template/AbstractResultWrapper.java @@ -36,7 +36,7 @@ * the standard column name type or the super column's child column * type */ -abstract class AbstractResultWrapper implements ColumnFamilyResult { +public abstract class AbstractResultWrapper implements ColumnFamilyResult { protected Serializer keySerializer; protected Serializer columnNameSerializer; From 3675c5e58f0f1e84984a114c7f595f2e37c558ea Mon Sep 17 00:00:00 2001 From: patricioe Date: Tue, 29 Mar 2011 09:41:04 -0700 Subject: [PATCH 063/144] Add CL > ONE for counters and wire Mutator with KS service --- .../cassandra/model/MutatorImpl.java | 101 ++++++++++++++++++ .../cassandra/model/thrift/ThriftFactory.java | 11 +- .../service/KeyspaceServiceImpl.java | 7 +- .../testutils/EmbeddedSchemaLoader.java | 4 +- .../hector/api/mutation/Mutator.java | 57 ++++++++++ .../cassandra/service/KeyspaceTest.java | 8 +- 6 files changed, 177 insertions(+), 11 deletions(-) diff --git a/core/src/main/java/me/prettyprint/cassandra/model/MutatorImpl.java b/core/src/main/java/me/prettyprint/cassandra/model/MutatorImpl.java index a06ea97ff..1c9fc1fa0 100644 --- a/core/src/main/java/me/prettyprint/cassandra/model/MutatorImpl.java +++ b/core/src/main/java/me/prettyprint/cassandra/model/MutatorImpl.java @@ -9,11 +9,15 @@ import me.prettyprint.hector.api.Keyspace; import me.prettyprint.hector.api.Serializer; import me.prettyprint.hector.api.beans.HColumn; +import me.prettyprint.hector.api.beans.HCounterColumn; +import me.prettyprint.hector.api.beans.HCounterSuperColumn; import me.prettyprint.hector.api.beans.HSuperColumn; import me.prettyprint.hector.api.exceptions.HectorException; import me.prettyprint.hector.api.mutation.MutationResult; import me.prettyprint.hector.api.mutation.Mutator; +import org.apache.cassandra.thrift.ColumnParent; +import org.apache.cassandra.thrift.CounterDeletion; import org.apache.cassandra.thrift.Deletion; import org.apache.cassandra.thrift.SlicePredicate; @@ -30,6 +34,7 @@ * * @author Ran Tavory * @author zznate + * @author patricioe */ public final class MutatorImpl implements Mutator { @@ -226,5 +231,101 @@ private BatchMutation getPendingMutations() { } return pendingMutations; } + + // Counters support. + + @Override + public MutationResult insertCounter(final K key, final String cf, final HCounterColumn c) { + return new MutationResultImpl(keyspace.doExecute(new KeyspaceOperationCallback() { + @Override + public Void doInKeyspace(KeyspaceService ks) throws HectorException { + ks.addCounter(keySerializer.toByteBuffer(key), new ColumnParent(cf), ((HCounterColumnImpl) c).toThrift()); + return null; + } + })); + } + + + @Override + public MutationResult insertCounter(K key, String cf, HCounterSuperColumn superColumn) { + addCounter(key, cf, superColumn); + return execute(); + } + + + @Override + public MutationResult deleteCounter(final K key, final String cf, final N counterColumnName, + final Serializer nameSerializer) { + return new MutationResultImpl(keyspace.doExecute(new KeyspaceOperationCallback() { + @Override + public Void doInKeyspace(KeyspaceService ks) throws HectorException { + ks.removeCounter(keySerializer.toByteBuffer(key), ThriftFactory.createColumnPath(cf, counterColumnName, + nameSerializer)); + return null; + } + })); + } + + @Override + public MutationResult subDeleteCounter(final K key, final String cf, final SN supercolumnName, + final N columnName, final Serializer sNameSerializer, final Serializer nameSerializer) { + return new MutationResultImpl(keyspace.doExecute(new KeyspaceOperationCallback() { + @Override + public Void doInKeyspace(KeyspaceService ks) throws HectorException { + ks.removeCounter(keySerializer.toByteBuffer(key), ThriftFactory.createSuperColumnPath(cf, + supercolumnName, columnName, sNameSerializer, nameSerializer)); + return null; + } + })); + } + + @Override + public Mutator addCounter(K key, String cf, HCounterColumn c) { + getPendingMutations().addCounterInsertion(key, Arrays.asList(cf), ((HCounterColumnImpl) c).toThrift()); + return this; + } + + @Override + public Mutator addCounter(K key, String cf, HCounterSuperColumn sc) { + getPendingMutations().addSuperCounterInsertion(key, Arrays.asList(cf), + ((HCounterSuperColumnImpl) sc).toThrift()); + return this; + } + + + @Override + public Mutator addCounterDeletion(K key, String cf, N counterColumnName, Serializer nameSerializer) { + SlicePredicate sp = new SlicePredicate(); + CounterDeletion d; + if ( counterColumnName != null ) { + sp.addToColumn_names(nameSerializer.toByteBuffer(counterColumnName)); + d = new CounterDeletion().setPredicate(sp); + } else { + d = new CounterDeletion(); + } + getPendingMutations().addCounterDeletion(key, Arrays.asList(cf), d); + return this; + } + + @Override + public Mutator addCounterDeletion(K key, String cf) { + addCounterDeletion(key, cf); + return this; + } + + @Override + public Mutator addCounterSubDeletion(K key, String cf, HCounterSuperColumn sc) { + SlicePredicate pred = new SlicePredicate(); + CounterDeletion d = new CounterDeletion(); + if ( sc.getColumns() != null ) { + for (HCounterColumn col : sc.getColumns()) { + pred.addToColumn_names(col.getNameSerializer().toByteBuffer(col.getName())); + } + d.setPredicate(pred); + } + d.setSuper_column(sc.getNameByteBuffer()); + getPendingMutations().addCounterDeletion(key, Arrays.asList(cf), d); + return this; + } } diff --git a/core/src/main/java/me/prettyprint/cassandra/model/thrift/ThriftFactory.java b/core/src/main/java/me/prettyprint/cassandra/model/thrift/ThriftFactory.java index d9d06f637..4ed577d7c 100644 --- a/core/src/main/java/me/prettyprint/cassandra/model/thrift/ThriftFactory.java +++ b/core/src/main/java/me/prettyprint/cassandra/model/thrift/ThriftFactory.java @@ -5,9 +5,11 @@ import java.nio.ByteBuffer; +import me.prettyprint.cassandra.serializers.StringSerializer; import me.prettyprint.hector.api.Serializer; import org.apache.cassandra.thrift.ColumnPath; +import org.apache.cassandra.thrift.CounterColumn; /** * Utility factory class for creating thrift objects. @@ -18,7 +20,7 @@ public class ThriftFactory { // probably should be typed for thrift vs. avro - /*package*/ static ColumnPath createColumnPath(String columnFamilyName, N columnName, + public static ColumnPath createColumnPath(String columnFamilyName, N columnName, Serializer nameSerializer) { return createColumnPath(columnFamilyName, nameSerializer.toByteBuffer(columnName)); } @@ -53,5 +55,12 @@ public static ColumnPath createSuperColumnPath(String columnFamilyName, } return columnPath; } + + public static CounterColumn createCounterColumn(String name, long value) { + CounterColumn cc = new CounterColumn(); + cc.setName(StringSerializer.get().toByteBuffer(name)); + cc.setValue(value); + return cc; + } } diff --git a/core/src/main/java/me/prettyprint/cassandra/service/KeyspaceServiceImpl.java b/core/src/main/java/me/prettyprint/cassandra/service/KeyspaceServiceImpl.java index a6874bacf..097d720fc 100644 --- a/core/src/main/java/me/prettyprint/cassandra/service/KeyspaceServiceImpl.java +++ b/core/src/main/java/me/prettyprint/cassandra/service/KeyspaceServiceImpl.java @@ -363,7 +363,7 @@ public void addCounter(final ByteBuffer key, final ColumnParent columnParent, fi @Override public Void execute(Cassandra.Client cassandra) throws HectorException { try { - cassandra.add(key, columnParent, counterColumn, ThriftConverter.consistencyLevel(HConsistencyLevel.ONE)); + cassandra.add(key, columnParent, counterColumn, getThriftCl(OperationType.WRITE)); return null; } catch (Exception e) { throw xtrans.translate(e); @@ -592,8 +592,7 @@ public void removeCounter(final ByteBuffer key, final ColumnPath columnPath) thr @Override public Void execute(Cassandra.Client cassandra) throws HectorException { try { - // NOTE" CL is ONE for counters - cassandra.remove_counter(key, columnPath, ThriftConverter.consistencyLevel(HConsistencyLevel.ONE)); + cassandra.remove_counter(key, columnPath, getThriftCl(OperationType.WRITE)); return null; } catch (Exception e) { throw xtrans.translate(e); @@ -664,7 +663,7 @@ public Counter getCounter(final ByteBuffer key, final ColumnPath columnPath) thr public Counter execute(Cassandra.Client cassandra) throws HectorException { Counter cosc; try { - cosc = cassandra.get_counter(key, columnPath, ThriftConverter.consistencyLevel(HConsistencyLevel.ONE)); + cosc = cassandra.get_counter(key, columnPath, getThriftCl(OperationType.READ)); } catch (NotFoundException e) { setException(xtrans.translate(e)); return null; diff --git a/core/src/main/java/me/prettyprint/cassandra/testutils/EmbeddedSchemaLoader.java b/core/src/main/java/me/prettyprint/cassandra/testutils/EmbeddedSchemaLoader.java index 25016acb7..f9cacd76e 100644 --- a/core/src/main/java/me/prettyprint/cassandra/testutils/EmbeddedSchemaLoader.java +++ b/core/src/main/java/me/prettyprint/cassandra/testutils/EmbeddedSchemaLoader.java @@ -77,9 +77,9 @@ public static Collection schemaDefinition() { bytes), indexCFMD(ks1, "Indexed1", true), indexCFMD(ks1, "Indexed2", false), new CFMetaData(ks1, "StandardInteger1", st, IntegerType.instance, null).keyCacheSize(0), new CFMetaData(ks1, - "Counter1", st, bytes, null) + "Counter1", st, bytes, null).replicateOnWrite(true) .defaultValidator(CounterColumnType.instance), new CFMetaData(ks1, - "SuperCounter1", su, bytes, bytes) + "SuperCounter1", su, bytes, bytes).replicateOnWrite(true) .defaultValidator(CounterColumnType.instance), jdbcCFMD(ks1, "JdbcInteger", IntegerType.instance), jdbcCFMD(ks1, "JdbcUtf8", UTF8Type.instance), jdbcCFMD(ks1, "JdbcLong", LongType.instance), diff --git a/core/src/main/java/me/prettyprint/hector/api/mutation/Mutator.java b/core/src/main/java/me/prettyprint/hector/api/mutation/Mutator.java index a78e92029..2b27f453d 100644 --- a/core/src/main/java/me/prettyprint/hector/api/mutation/Mutator.java +++ b/core/src/main/java/me/prettyprint/hector/api/mutation/Mutator.java @@ -2,6 +2,8 @@ import me.prettyprint.hector.api.Serializer; import me.prettyprint.hector.api.beans.HColumn; +import me.prettyprint.hector.api.beans.HCounterColumn; +import me.prettyprint.hector.api.beans.HCounterSuperColumn; import me.prettyprint.hector.api.beans.HSuperColumn; /** @@ -115,5 +117,60 @@ MutationResult subDelete(final K key, final String cf, final SN supercol * Discards all pending mutations. */ Mutator discardPendingMutations(); + + // Support for counters + + // Simple and immediate insertion of a column + MutationResult insertCounter(final K key, final String cf, final HCounterColumn c); + + // overloaded insert-super + MutationResult insertCounter(final K key, final String cf, final HCounterSuperColumn superColumn); + + MutationResult deleteCounter(final K key, final String cf, final N columnName, final Serializer nameSerializer); + + /** + * Deletes a subcolumn of a supercolumn for a counter + * @param super column type + * @param subcolumn type + */ + MutationResult subDeleteCounter(final K key, final String cf, final SN supercolumnName, + final N columnName, final Serializer sNameSerializer, final Serializer nameSerializer); + + /** + * Schedule an increment of a CounterColumn to be inserted in batch mode by {@link #execute()} + */ + Mutator addCounter(K key, String cf, HCounterColumn c); + + /** + * Schedule an increment of a SuperColumn to be inserted in batch mode by {@link #execute()} + */ + Mutator addCounter(K key, String cf, HCounterSuperColumn sc); + + /** + * Adds a Deletion to the underlying batch_mutate call. The columnName argument can be null + * in which case the whole row being deleted. + * + * @param column name type + * @param key row key + * @param cf column family + * @param counterColumnName column name. Use null to delete the whole row. + * @param nameSerializer a name serializer + * @return a mutator + */ + Mutator addCounterDeletion(K key, String cf, N counterColumnName, Serializer nameSerializer); + + /** + * Alternate form for easy deletion of the whole row. + * + * @param + * @param key + * @return + */ + Mutator addCounterDeletion(K key, String cf); + + /** + * Schedule a counter deletion. + */ + Mutator addCounterSubDeletion(K key, String cf, HCounterSuperColumn sc); } \ No newline at end of file diff --git a/core/src/test/java/me/prettyprint/cassandra/service/KeyspaceTest.java b/core/src/test/java/me/prettyprint/cassandra/service/KeyspaceTest.java index d17121ff0..9414a36bf 100644 --- a/core/src/test/java/me/prettyprint/cassandra/service/KeyspaceTest.java +++ b/core/src/test/java/me/prettyprint/cassandra/service/KeyspaceTest.java @@ -158,10 +158,10 @@ public void testInsertAndGetAndRemoveCounter() throws IllegalArgumentException, } private CounterColumn createCounterColumn(String name, long value) { - CounterColumn cc = new CounterColumn(); - cc.setName(StringSerializer.get().toByteBuffer(name)); - cc.setValue(value); - return cc; + CounterColumn cc = new CounterColumn(); + cc.setName(StringSerializer.get().toByteBuffer(name)); + cc.setValue(value); + return cc; } /** From 25e691782330c228c5e20876cb82626f02bc3d67 Mon Sep 17 00:00:00 2001 From: zznate Date: Tue, 29 Mar 2011 12:25:20 -0500 Subject: [PATCH 064/144] cf result using linkedhashmap for ordering --- .../cassandra/service/template/ColumnFamilyResultWrapper.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/core/src/main/java/me/prettyprint/cassandra/service/template/ColumnFamilyResultWrapper.java b/core/src/main/java/me/prettyprint/cassandra/service/template/ColumnFamilyResultWrapper.java index 0d9a77480..f677d6aee 100644 --- a/core/src/main/java/me/prettyprint/cassandra/service/template/ColumnFamilyResultWrapper.java +++ b/core/src/main/java/me/prettyprint/cassandra/service/template/ColumnFamilyResultWrapper.java @@ -3,6 +3,7 @@ import java.nio.ByteBuffer; import java.util.HashMap; import java.util.Iterator; +import java.util.LinkedHashMap; import java.util.List; import java.util.Map; import java.util.NoSuchElementException; @@ -24,7 +25,7 @@ */ public class ColumnFamilyResultWrapper extends AbstractResultWrapper { - private Map> columns = new HashMap>(); + private Map> columns = new LinkedHashMap>(); private Iterator>> rows; private Map.Entry> entry; private ExecutionResult>> executionResult; From 5e2fba832d288b4542abe4b0d06087d49d73a6ca Mon Sep 17 00:00:00 2001 From: Ed Anuff Date: Tue, 29 Mar 2011 18:04:28 -0700 Subject: [PATCH 065/144] Made it possible to set serializers, comparators, and components by position Renamed composite-specific add and set methods to addComponent and setComponent to differentiate from underlying AbstractList add and set methods that have different return types - addComponent and setComponent return the composite object to facilitate chaining --- .../hector/api/beans/AbstractComposite.java | 113 +++++++++++++++--- 1 file changed, 95 insertions(+), 18 deletions(-) diff --git a/core/src/main/java/me/prettyprint/hector/api/beans/AbstractComposite.java b/core/src/main/java/me/prettyprint/hector/api/beans/AbstractComposite.java index 3518950f3..77916adca 100644 --- a/core/src/main/java/me/prettyprint/hector/api/beans/AbstractComposite.java +++ b/core/src/main/java/me/prettyprint/hector/api/beans/AbstractComposite.java @@ -35,6 +35,14 @@ import com.google.common.collect.ImmutableBiMap; import com.google.common.collect.ImmutableClassToInstanceMap; +/** + * Parent class of Composite and DynamicComposite. Acts as a list of objects + * that get serialized into a composite column name. Unless + * setAutoDeserialize(true) is called, it's going to try to match serializers to + * Cassandra comparator types. + * + * @author edanuff + */ @SuppressWarnings("rawtypes") public abstract class AbstractComposite extends AbstractList implements Comparable { @@ -177,6 +185,16 @@ public void setSerializersByPosition(Serializer... serializers) { serializersByPosition = Arrays.asList(serializers); } + public void setSerializerByPosition(int index, Serializer s) { + if (serializersByPosition == null) { + serializersByPosition = new ArrayList>(); + } + while (serializersByPosition.size() <= index) { + serializersByPosition.add(null); + } + serializersByPosition.set(index, s); + } + public List getComparatorsByPosition() { return comparatorsByPosition; } @@ -189,6 +207,16 @@ public void setComparatorsByPosition(String... comparators) { comparatorsByPosition = Arrays.asList(comparators); } + public void setComparatorByPosition(int index, String c) { + if (comparatorsByPosition == null) { + comparatorsByPosition = new ArrayList(); + } + while (comparatorsByPosition.size() <= index) { + comparatorsByPosition.add(null); + } + comparatorsByPosition.set(index, c); + } + @Override public int compareTo(AbstractComposite o) { return serialize().compareTo(o.serialize()); @@ -293,45 +321,61 @@ private String getComparator(int i, ByteBuffer bb) { return name; } - public AbstractComposite add(T value, Serializer s) { + @Override + public void clear() { serialized = null; + components = new ArrayList(); + } - add(value, s, value instanceof UUID ? comparatorForUUID((UUID) value) - : comparatorForSerializer(s)); + @Override + public int size() { + return components.size(); + } + + public AbstractComposite addComponent(T value, Serializer s) { + + addComponent(value, s, + value instanceof UUID ? comparatorForUUID((UUID) value) + : comparatorForSerializer(s)); return this; } - public AbstractComposite add(T value, Serializer s, String comparator) { - serialized = null; + public AbstractComposite addComponent(T value, Serializer s, + String comparator) { - add(value, s, comparator, false); + addComponent(value, s, comparator, false); return this; } - @SuppressWarnings("unchecked") - public AbstractComposite add(T value, Serializer s, String comparator, - boolean inclusive) { - serialized = null; + public AbstractComposite addComponent(T value, Serializer s, + String comparator, boolean inclusive) { - components.add(new Component(value, s, comparator, inclusive)); + addComponent(-1, value, s, comparator, inclusive); return this; } - @Override - public void clear() { + @SuppressWarnings("unchecked") + public AbstractComposite addComponent(int index, T value, + Serializer s, String comparator, boolean inclusive) { serialized = null; - components = new ArrayList(); - } - @Override - public int size() { - return components.size(); + if (index < 0) { + index = components.size(); + } + + while (components.size() < index) { + components.add(null); + } + components.add(index, new Component(value, s, comparator, inclusive)); + + return this; + } @SuppressWarnings("unchecked") @@ -360,6 +404,39 @@ public Object remove(int index) { return null; } + public AbstractComposite setComponent(int index, T value, Serializer s) { + + setComponent(index, value, s, + value instanceof UUID ? comparatorForUUID((UUID) value) + : comparatorForSerializer(s)); + + return this; + + } + + public AbstractComposite setComponent(int index, T value, + Serializer s, String comparator) { + + setComponent(index, value, s, comparator, false); + + return this; + + } + + @SuppressWarnings("unchecked") + public AbstractComposite setComponent(int index, T value, + Serializer s, String comparator, boolean inclusive) { + serialized = null; + + while (components.size() <= index) { + components.add(null); + } + components.set(index, new Component(value, s, comparator, inclusive)); + + return this; + + } + @SuppressWarnings("unchecked") @Override public Object set(int index, Object element) { From 45dfbded8268a70ac4596f089720930c4e2b90d6 Mon Sep 17 00:00:00 2001 From: Ed Anuff Date: Tue, 29 Mar 2011 21:21:07 -0700 Subject: [PATCH 066/144] Option to turn off deserialization of components --- .../prettyprint/hector/api/beans/Composite.java | 9 +++++++++ .../hector/api/beans/DynamicComposite.java | 15 +++++++++++++++ 2 files changed, 24 insertions(+) diff --git a/core/src/main/java/me/prettyprint/hector/api/beans/Composite.java b/core/src/main/java/me/prettyprint/hector/api/beans/Composite.java index 04109034a..736ae564d 100644 --- a/core/src/main/java/me/prettyprint/hector/api/beans/Composite.java +++ b/core/src/main/java/me/prettyprint/hector/api/beans/Composite.java @@ -1,5 +1,7 @@ package me.prettyprint.hector.api.beans; +import java.nio.ByteBuffer; + public class Composite extends AbstractComposite { public Composite() { @@ -10,4 +12,11 @@ public Composite(Object... o) { super(false, o); } + public static Composite fromByteBuffer(ByteBuffer byteBuffer) { + + Composite composite = new Composite(); + composite.deserialize(byteBuffer); + + return composite; + } } diff --git a/core/src/main/java/me/prettyprint/hector/api/beans/DynamicComposite.java b/core/src/main/java/me/prettyprint/hector/api/beans/DynamicComposite.java index 21941269d..b917add54 100644 --- a/core/src/main/java/me/prettyprint/hector/api/beans/DynamicComposite.java +++ b/core/src/main/java/me/prettyprint/hector/api/beans/DynamicComposite.java @@ -1,5 +1,7 @@ package me.prettyprint.hector.api.beans; +import java.nio.ByteBuffer; + public class DynamicComposite extends AbstractComposite { public DynamicComposite() { @@ -10,4 +12,17 @@ public DynamicComposite(Object... o) { super(true, o); } + public static DynamicComposite fromByteBuffer(ByteBuffer byteBuffer) { + return fromByteBuffer(true, byteBuffer); + } + + public static DynamicComposite fromByteBuffer(boolean deserializeComponents, + ByteBuffer byteBuffer) { + + DynamicComposite composite = new DynamicComposite(); + composite.setAutoDeserialize(deserializeComponents); + composite.deserialize(byteBuffer); + + return composite; + } } From 7d33bc90f25383de124f237f1a23d7c2f131fd23 Mon Sep 17 00:00:00 2001 From: patricioe Date: Tue, 29 Mar 2011 22:32:17 -0700 Subject: [PATCH 067/144] Add Mutator test for insert counter column --- .../me/prettyprint/cassandra/model/MutatorTest.java | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/core/src/test/java/me/prettyprint/cassandra/model/MutatorTest.java b/core/src/test/java/me/prettyprint/cassandra/model/MutatorTest.java index 7713946f0..36d306387 100644 --- a/core/src/test/java/me/prettyprint/cassandra/model/MutatorTest.java +++ b/core/src/test/java/me/prettyprint/cassandra/model/MutatorTest.java @@ -1,6 +1,7 @@ package me.prettyprint.cassandra.model; import static me.prettyprint.hector.api.factory.HFactory.createColumn; +import static me.prettyprint.hector.api.factory.HFactory.createCounterColumn; 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; @@ -166,6 +167,15 @@ public void testRowDeletion() { setColumnFamily(cf).setKey("key0").setName("name").execute(); assertNull(columnResult.get()); } + + @Test + public void testInsertCounter() { + Mutator m = createMutator(keyspace, se); + MutationResult mr = m.insertCounter("k", "Counter1", createCounterColumn("name", 5)); + assertTrue("Execution time on single counter insert should be > 0", mr.getExecutionTimeMicro() > 0); + assertTrue("Should have operated on a host", mr.getHostUsed() != null); + assertColumnExists("Keyspace1", "Counter1", "k", "name"); + } private void assertColumnExists(String keyspace, String cf, String key, String column) { ColumnPath cp = new ColumnPath(cf); From d8b6396c154c9eabd39f2005dfe6e9dd38358db6 Mon Sep 17 00:00:00 2001 From: patricioe Date: Tue, 29 Mar 2011 23:59:52 -0700 Subject: [PATCH 068/144] Complete the basic add/get/remove simple CF counter --- .../cassandra/model/AbstractBasicQuery.java | 55 ++++++++++++ .../cassandra/model/AbstractQuery.java | 35 +------- .../thrift/ThriftCounterColumnQuery.java | 87 +++++++++++++++++++ .../hector/api/factory/HFactory.java | 7 ++ .../hector/api/query/CounterQuery.java | 27 ++++++ .../hector/api/ApiV2SystemTest.java | 44 ++++++++++ 6 files changed, 223 insertions(+), 32 deletions(-) create mode 100644 core/src/main/java/me/prettyprint/cassandra/model/AbstractBasicQuery.java create mode 100644 core/src/main/java/me/prettyprint/cassandra/model/thrift/ThriftCounterColumnQuery.java create mode 100644 core/src/main/java/me/prettyprint/hector/api/query/CounterQuery.java diff --git a/core/src/main/java/me/prettyprint/cassandra/model/AbstractBasicQuery.java b/core/src/main/java/me/prettyprint/cassandra/model/AbstractBasicQuery.java new file mode 100644 index 000000000..8dd01c667 --- /dev/null +++ b/core/src/main/java/me/prettyprint/cassandra/model/AbstractBasicQuery.java @@ -0,0 +1,55 @@ +package me.prettyprint.cassandra.model; + +import me.prettyprint.cassandra.utils.Assert; +import me.prettyprint.hector.api.Keyspace; +import me.prettyprint.hector.api.Serializer; +import me.prettyprint.hector.api.query.Query; + +/** + * + * @author patricioe (Patricio Echague - patricio@datastax.com) + * + * @param Key type + * @param column name type + * @param return type + */ +public abstract class AbstractBasicQuery implements Query { + + protected final ExecutingKeyspace keyspace; + protected String columnFamilyName; + protected Serializer keySerializer; + protected Serializer columnNameSerializer; + // add: FailoverPolicy, ConsistencyLevelPolicy, Credentials? + + protected AbstractBasicQuery(Keyspace k, Serializer keySerializer, + Serializer nameSerializer) { + Assert.noneNull(k, keySerializer, nameSerializer); + keyspace = (ExecutingKeyspace) k; + this.keySerializer = keySerializer; + this.columnNameSerializer = nameSerializer; + } + + public Query setColumnFamily(String cf) { + this.columnFamilyName = cf; + return this; + } + + public Serializer getKeySerializer() { + return keySerializer; + } + + public AbstractBasicQuery setKeySerializer(Serializer keySerializer) { + this.keySerializer = keySerializer; + return this; + } + + public Serializer getColumnNameSerializer() { + return columnNameSerializer; + } + + public AbstractBasicQuery setColumnNameSerializer(Serializer columnNameSerializer) { + this.columnNameSerializer = columnNameSerializer; + return this; + } + +} diff --git a/core/src/main/java/me/prettyprint/cassandra/model/AbstractQuery.java b/core/src/main/java/me/prettyprint/cassandra/model/AbstractQuery.java index b57fe0e7f..1566cbaf0 100644 --- a/core/src/main/java/me/prettyprint/cassandra/model/AbstractQuery.java +++ b/core/src/main/java/me/prettyprint/cassandra/model/AbstractQuery.java @@ -5,47 +5,18 @@ import me.prettyprint.hector.api.Serializer; import me.prettyprint.hector.api.query.Query; -public abstract class AbstractQuery implements Query { +public abstract class AbstractQuery extends AbstractBasicQuery implements Query { - protected final ExecutingKeyspace keyspace; - protected String columnFamilyName; - protected Serializer keySerializer; - protected Serializer columnNameSerializer; protected Serializer valueSerializer; // add: FailoverPolicy, ConsistencyLevelPolicy, Credentials? /*package*/ AbstractQuery(Keyspace k, Serializer keySerializer, Serializer nameSerializer, Serializer valueSerializer) { - Assert.noneNull(k, keySerializer, nameSerializer, valueSerializer); - keyspace = (ExecutingKeyspace) k; - this.keySerializer = keySerializer; - this.columnNameSerializer = nameSerializer; + super(k, keySerializer, nameSerializer); + Assert.noneNull(valueSerializer); this.valueSerializer = valueSerializer; } - public Query setColumnFamily(String cf) { - this.columnFamilyName = cf; - return this; - } - - public Serializer getKeySerializer() { - return keySerializer; - } - - public AbstractQuery setKeySerializer(Serializer keySerializer) { - this.keySerializer = keySerializer; - return this; - } - - public Serializer getColumnNameSerializer() { - return columnNameSerializer; - } - - public AbstractQuery setColumnNameSerializer(Serializer columnNameSerializer) { - this.columnNameSerializer = columnNameSerializer; - return this; - } - public Serializer getValueSerializer() { return valueSerializer; } diff --git a/core/src/main/java/me/prettyprint/cassandra/model/thrift/ThriftCounterColumnQuery.java b/core/src/main/java/me/prettyprint/cassandra/model/thrift/ThriftCounterColumnQuery.java new file mode 100644 index 000000000..3446ebf45 --- /dev/null +++ b/core/src/main/java/me/prettyprint/cassandra/model/thrift/ThriftCounterColumnQuery.java @@ -0,0 +1,87 @@ +package me.prettyprint.cassandra.model.thrift; + +import me.prettyprint.cassandra.model.AbstractBasicQuery; +import me.prettyprint.cassandra.model.AbstractColumnQuery; +import me.prettyprint.cassandra.model.AbstractQuery; +import me.prettyprint.cassandra.model.ExecutingKeyspace; +import me.prettyprint.cassandra.model.HColumnImpl; +import me.prettyprint.cassandra.model.HCounterColumnImpl; +import me.prettyprint.cassandra.model.KeyspaceOperationCallback; +import me.prettyprint.cassandra.model.QueryResultImpl; +import me.prettyprint.cassandra.serializers.LongSerializer; +import me.prettyprint.cassandra.serializers.StringSerializer; +import me.prettyprint.cassandra.serializers.TypeInferringSerializer; +import me.prettyprint.cassandra.service.KeyspaceService; +import me.prettyprint.hector.api.Keyspace; +import me.prettyprint.hector.api.Serializer; +import me.prettyprint.hector.api.beans.HColumn; +import me.prettyprint.hector.api.beans.HCounterColumn; +import me.prettyprint.hector.api.exceptions.HNotFoundException; +import me.prettyprint.hector.api.exceptions.HectorException; +import me.prettyprint.hector.api.query.ColumnQuery; +import me.prettyprint.hector.api.query.CounterQuery; +import me.prettyprint.hector.api.query.QueryResult; + +import org.apache.cassandra.thrift.Column; +import org.apache.cassandra.thrift.Counter; + +/** + * Thrift implementation of the ColumnQuery type. + * + * @author Ran Tavory + * + * @param + * column name type + * @param + * value type + */ +public class ThriftCounterColumnQuery extends AbstractBasicQuery> + implements CounterQuery { + + protected K key; + protected N name; + + public ThriftCounterColumnQuery(Keyspace keyspace, Serializer keySerializer, + Serializer nameSerializer) { + super(keyspace, keySerializer, nameSerializer); + } + + public ThriftCounterColumnQuery(Keyspace keyspace) { + super(keyspace, TypeInferringSerializer. get(), TypeInferringSerializer. get()); + } + + public CounterQuery setKey(K key) { + this.key = key; + return this; + } + + public CounterQuery setName(N name) { + this.name = name; + return this; + } + + @SuppressWarnings("unchecked") + @Override + public CounterQuery setColumnFamily(String cf) { + return (CounterQuery) super.setColumnFamily(cf); + } + + @Override + public QueryResult> execute() { + return new QueryResultImpl>( + keyspace.doExecute(new KeyspaceOperationCallback>() { + + @Override + public HCounterColumn doInKeyspace(KeyspaceService ks) throws HectorException { + try { + Counter thriftCounter = ks.getCounter(keySerializer.toByteBuffer(key), + ThriftFactory.createColumnPath(columnFamilyName, name, columnNameSerializer)); + return new HCounterColumnImpl(thriftCounter.getColumn(), columnNameSerializer); + } catch (HNotFoundException e) { + return null; + } + } + }), this); + } + +} diff --git a/core/src/main/java/me/prettyprint/hector/api/factory/HFactory.java b/core/src/main/java/me/prettyprint/hector/api/factory/HFactory.java index fc0713c1c..f78025ca0 100644 --- a/core/src/main/java/me/prettyprint/hector/api/factory/HFactory.java +++ b/core/src/main/java/me/prettyprint/hector/api/factory/HFactory.java @@ -15,6 +15,7 @@ import me.prettyprint.cassandra.model.QuorumAllConsistencyLevelPolicy; import me.prettyprint.cassandra.model.thrift.ThriftColumnQuery; import me.prettyprint.cassandra.model.thrift.ThriftCountQuery; +import me.prettyprint.cassandra.model.thrift.ThriftCounterColumnQuery; import me.prettyprint.cassandra.model.thrift.ThriftMultigetSliceQuery; import me.prettyprint.cassandra.model.thrift.ThriftMultigetSubSliceQuery; import me.prettyprint.cassandra.model.thrift.ThriftMultigetSuperSliceQuery; @@ -54,6 +55,7 @@ import me.prettyprint.hector.api.mutation.Mutator; import me.prettyprint.hector.api.query.ColumnQuery; import me.prettyprint.hector.api.query.CountQuery; +import me.prettyprint.hector.api.query.CounterQuery; import me.prettyprint.hector.api.query.MultigetSliceQuery; import me.prettyprint.hector.api.query.MultigetSubSliceQuery; import me.prettyprint.hector.api.query.MultigetSuperSliceQuery; @@ -323,6 +325,11 @@ public static ColumnQuery createColumnQuery( return new ThriftColumnQuery(keyspace, keySerializer, nameSerializer, valueSerializer); } + + public static CounterQuery createCounterColumnQuery( + Keyspace keyspace, Serializer keySerializer, Serializer nameSerializer) { + return new ThriftCounterColumnQuery(keyspace, keySerializer, nameSerializer); + } public static CountQuery createCountQuery(Keyspace keyspace, Serializer keySerializer, Serializer nameSerializer) { diff --git a/core/src/main/java/me/prettyprint/hector/api/query/CounterQuery.java b/core/src/main/java/me/prettyprint/hector/api/query/CounterQuery.java new file mode 100644 index 000000000..2b4d27ca9 --- /dev/null +++ b/core/src/main/java/me/prettyprint/hector/api/query/CounterQuery.java @@ -0,0 +1,27 @@ +package me.prettyprint.hector.api.query; + +import me.prettyprint.hector.api.beans.HCounterColumn; + +/** + * A CounterQuery is used for querying the value of a single and standard counter column. + *

+ * + * @author patricio + * + * @param Column name type. + */ +public interface CounterQuery extends Query>{ + + /** + * Set the row key for this query. + */ + CounterQuery setKey(K key); + + /** + * Set the column name for this query. + */ + CounterQuery setName(N name); + + CounterQuery setColumnFamily(String cf); + +} \ No newline at end of file diff --git a/core/src/test/java/me/prettyprint/hector/api/ApiV2SystemTest.java b/core/src/test/java/me/prettyprint/hector/api/ApiV2SystemTest.java index d35fea5df..d07bdd226 100644 --- a/core/src/test/java/me/prettyprint/hector/api/ApiV2SystemTest.java +++ b/core/src/test/java/me/prettyprint/hector/api/ApiV2SystemTest.java @@ -3,6 +3,8 @@ 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.createCountQuery; +import static me.prettyprint.hector.api.factory.HFactory.createCounterColumn; +import static me.prettyprint.hector.api.factory.HFactory.createCounterColumnQuery; import static me.prettyprint.hector.api.factory.HFactory.createKeyspace; import static me.prettyprint.hector.api.factory.HFactory.createMultigetSliceQuery; import static me.prettyprint.hector.api.factory.HFactory.createMultigetSubSliceQuery; @@ -34,6 +36,7 @@ import me.prettyprint.cassandra.serializers.StringSerializer; import me.prettyprint.hector.api.beans.ColumnSlice; import me.prettyprint.hector.api.beans.HColumn; +import me.prettyprint.hector.api.beans.HCounterColumn; import me.prettyprint.hector.api.beans.HSuperColumn; import me.prettyprint.hector.api.beans.OrderedRows; import me.prettyprint.hector.api.beans.OrderedSuperRows; @@ -46,6 +49,7 @@ import me.prettyprint.hector.api.mutation.Mutator; import me.prettyprint.hector.api.query.ColumnQuery; import me.prettyprint.hector.api.query.CountQuery; +import me.prettyprint.hector.api.query.CounterQuery; import me.prettyprint.hector.api.query.MultigetSliceQuery; import me.prettyprint.hector.api.query.MultigetSubSliceQuery; import me.prettyprint.hector.api.query.MultigetSuperSliceQuery; @@ -86,6 +90,46 @@ public void teardownCase() { ko = null; cluster = null; } + + @Test + public void testInsertGetRemoveCounter() { + String cf = "Counter1"; + Mutator m = createMutator(ko, se); + MutationResult mr = m.insertCounter("testInsertGetRemoveCounter", cf, + createCounterColumn("testInsertGetRemoveCounter_name", 25)); + + assertTrue("Time should be > 0", mr.getExecutionTimeMicro() > 0); + log.debug("insert execution time: {}", mr.getExecutionTimeMicro()); + + // get value + CounterQuery q = createCounterColumnQuery(ko, se, se); + q.setColumnFamily(cf).setName("testInsertGetRemoveCounter_name"); + QueryResult> r = q.setKey("testInsertGetRemoveCounter") + .execute(); + assertNotNull(r); + + HCounterColumn c = r.get(); + assertNotNull(c); + Long value = c.getValue(); + assertEquals(25, value.longValue()); + String name = c.getName(); + assertEquals("testInsertGetRemoveCounter_name", name); + assertEquals(q, r.getQuery()); + assertTrue("Time should be > 0", r.getExecutionTimeMicro() > 0); + + // remove value + m = createMutator(ko, se); + MutationResult mr2 = m.deleteCounter("testInsertGetRemoveCounter", cf, "testInsertGetRemoveCounter_name", se); + assertTrue("Time should be > 0", mr2.getExecutionTimeMicro() > 0); + + // get already removed value + CounterQuery q2 = createCounterColumnQuery(ko, se, se); + q2.setName("testInsertGetRemoveCounter_name").setColumnFamily(cf); + QueryResult> r2 = q2.setKey("testInsertGetRemoveCounter") + .execute(); + assertNotNull(r2); + assertNull("Value should have been deleted", r2.get()); + } @Test public void testInsertGetRemove() { From 952c7531e06dd73d5514f6bea8098fddec16308c Mon Sep 17 00:00:00 2001 From: patricioe Date: Wed, 30 Mar 2011 00:23:23 -0700 Subject: [PATCH 069/144] Increment and decrement counters in one step (Convenient methods) --- .../cassandra/model/MutatorImpl.java | 10 +++++++++ .../cassandra/model/thrift/ThriftFactory.java | 7 +++++++ .../hector/api/mutation/Mutator.java | 10 +++++++-- .../hector/api/ApiV2SystemTest.java | 21 +++++++++++++++++++ 4 files changed, 46 insertions(+), 2 deletions(-) diff --git a/core/src/main/java/me/prettyprint/cassandra/model/MutatorImpl.java b/core/src/main/java/me/prettyprint/cassandra/model/MutatorImpl.java index 1c9fc1fa0..f4120eb2c 100644 --- a/core/src/main/java/me/prettyprint/cassandra/model/MutatorImpl.java +++ b/core/src/main/java/me/prettyprint/cassandra/model/MutatorImpl.java @@ -244,6 +244,16 @@ public Void doInKeyspace(KeyspaceService ks) throws HectorException { } })); } + + @Override + public MutationResult incrementCounter(final K key, final String cf, final N columnName, final long increment) { + return insertCounter(key, cf, new HCounterColumnImpl(columnName, increment, TypeInferringSerializer. get())); + } + + @Override + public MutationResult decrementCounter(final K key, final String cf, final N columnName, final long increment) { + return incrementCounter(key, cf, columnName, increment * -1L); + } @Override diff --git a/core/src/main/java/me/prettyprint/cassandra/model/thrift/ThriftFactory.java b/core/src/main/java/me/prettyprint/cassandra/model/thrift/ThriftFactory.java index 4ed577d7c..76e7c4f01 100644 --- a/core/src/main/java/me/prettyprint/cassandra/model/thrift/ThriftFactory.java +++ b/core/src/main/java/me/prettyprint/cassandra/model/thrift/ThriftFactory.java @@ -62,5 +62,12 @@ public static CounterColumn createCounterColumn(String name, long value) { cc.setValue(value); return cc; } + + public static CounterColumn createCounterColumn(N name, long value, Serializer ns) { + CounterColumn cc = new CounterColumn(); + cc.setName(ns.toByteBuffer(name)); + cc.setValue(value); + return cc; + } } diff --git a/core/src/main/java/me/prettyprint/hector/api/mutation/Mutator.java b/core/src/main/java/me/prettyprint/hector/api/mutation/Mutator.java index 2b27f453d..6c699a64d 100644 --- a/core/src/main/java/me/prettyprint/hector/api/mutation/Mutator.java +++ b/core/src/main/java/me/prettyprint/hector/api/mutation/Mutator.java @@ -120,11 +120,17 @@ MutationResult subDelete(final K key, final String cf, final SN supercol // Support for counters - // Simple and immediate insertion of a column + /** Simple and immediate insertion (increment/decrement) of a counter */ MutationResult insertCounter(final K key, final String cf, final HCounterColumn c); - // overloaded insert-super + /** Simple and immediate insertion (increment/decrement) of a counter part of a super column */ MutationResult insertCounter(final K key, final String cf, final HCounterSuperColumn superColumn); + + /** Convenient method to increment a simple counter */ + MutationResult incrementCounter(final K key, final String cf, final N columnName, final long increment); + + /** Convenient method to decrement a simple counter */ + MutationResult decrementCounter(final K key, final String cf, final N columnName, final long increment); MutationResult deleteCounter(final K key, final String cf, final N columnName, final Serializer nameSerializer); diff --git a/core/src/test/java/me/prettyprint/hector/api/ApiV2SystemTest.java b/core/src/test/java/me/prettyprint/hector/api/ApiV2SystemTest.java index d07bdd226..3093a1b5c 100644 --- a/core/src/test/java/me/prettyprint/hector/api/ApiV2SystemTest.java +++ b/core/src/test/java/me/prettyprint/hector/api/ApiV2SystemTest.java @@ -130,6 +130,27 @@ public void testInsertGetRemoveCounter() { assertNotNull(r2); assertNull("Value should have been deleted", r2.get()); } + + @Test + public void testIncrementDecrementCounter() { + String cf = "Counter1"; + createMutator(ko, se).incrementCounter("testIncrementDecrementCounter", cf, "testIncrementDecrementCounter_name", 7); + createMutator(ko, se).decrementCounter("testIncrementDecrementCounter", cf, "testIncrementDecrementCounter_name", 2); + + // The total in the counter is 5. (7 - 2) + + // get value + CounterQuery q = createCounterColumnQuery(ko, se, se); + q.setColumnFamily(cf).setName("testIncrementDecrementCounter_name"); + QueryResult> r = q.setKey("testIncrementDecrementCounter") + .execute(); + assertNotNull(r); + + HCounterColumn c = r.get(); + assertNotNull(c); + Long value = c.getValue(); + assertEquals(5, value.longValue()); + } @Test public void testInsertGetRemove() { From b0a2c384e28e978bdb310c19d15e8a91eedce392 Mon Sep 17 00:00:00 2001 From: Ed Anuff Date: Wed, 30 Mar 2011 08:43:56 -0700 Subject: [PATCH 070/144] Able to serialize and deserialize null values --- .../hector/api/beans/AbstractComposite.java | 20 +++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/core/src/main/java/me/prettyprint/hector/api/beans/AbstractComposite.java b/core/src/main/java/me/prettyprint/hector/api/beans/AbstractComposite.java index 77916adca..2fd471475 100644 --- a/core/src/main/java/me/prettyprint/hector/api/beans/AbstractComposite.java +++ b/core/src/main/java/me/prettyprint/hector/api/beans/AbstractComposite.java @@ -467,11 +467,15 @@ public Object get(int i) { } public T get(int i, Serializer s) throws ClassCastException { + T value = null; Component c = components.get(i); if (c != null) { - return s.fromByteBuffer((ByteBuffer) c.getValue()); + ByteBuffer cb = ((ByteBuffer) c.getValue()).duplicate(); + if (cb.hasRemaining()) { + value = s.fromByteBuffer(cb); + } } - return null; + return value; } public Component getComponent(int i) { @@ -500,7 +504,12 @@ public ByteBuffer serialize() { if (s == null) { s = c.getSerializer(); } - ByteBuffer cb = s.toByteBuffer(c.getValue()); + ByteBuffer cb = null; + if (s != null) { + cb = s.toByteBuffer(c.getValue()); + } else { + cb = ByteBuffer.allocate(0); + } if (dynamic) { String comparator = comparatorForPosition(i); @@ -546,7 +555,10 @@ public void deserialize(ByteBuffer b) { if (data != null) { Serializer s = autoDeserialize ? getSerializer(i, comparator) : ByteBufferSerializer.get(); - Object value = s.fromByteBuffer(data); + Object value = null; + if (data.hasRemaining()) { + value = s.fromByteBuffer(data); + } boolean inclusive = b.get() != 0; components.add(new Component(value, s, comparator, inclusive)); } else { From 5f06b0441eeab670cd69676be86cf951ed13e801 Mon Sep 17 00:00:00 2001 From: zznate Date: Wed, 30 Mar 2011 13:05:18 -0500 Subject: [PATCH 071/144] added patch for disabling auto-deserialize by default. Thanks to Todd9. --- .../cassandra/serializers/DynamicCompositeSerializer.java | 1 + 1 file changed, 1 insertion(+) diff --git a/core/src/main/java/me/prettyprint/cassandra/serializers/DynamicCompositeSerializer.java b/core/src/main/java/me/prettyprint/cassandra/serializers/DynamicCompositeSerializer.java index ccab74477..a1e098ce0 100644 --- a/core/src/main/java/me/prettyprint/cassandra/serializers/DynamicCompositeSerializer.java +++ b/core/src/main/java/me/prettyprint/cassandra/serializers/DynamicCompositeSerializer.java @@ -24,6 +24,7 @@ public ByteBuffer toByteBuffer(DynamicComposite obj) { public DynamicComposite fromByteBuffer(ByteBuffer byteBuffer) { DynamicComposite composite = new DynamicComposite(); + composite.setAutoDeserialize(false); composite.deserialize(byteBuffer); return composite; From 50daddbde6d764dd15c3c1fc6b74da2ea8da240d Mon Sep 17 00:00:00 2001 From: Ed Anuff Date: Wed, 30 Mar 2011 13:21:35 -0700 Subject: [PATCH 072/144] Made deserialization of components lazy and occur at get() time --- .../hector/api/beans/AbstractComposite.java | 106 +++++++++++------- .../hector/api/beans/DynamicComposite.java | 6 - .../prettyprint/hector/api/CompositeTest.java | 7 +- 3 files changed, 66 insertions(+), 53 deletions(-) diff --git a/core/src/main/java/me/prettyprint/hector/api/beans/AbstractComposite.java b/core/src/main/java/me/prettyprint/hector/api/beans/AbstractComposite.java index 2fd471475..3ef1a8251 100644 --- a/core/src/main/java/me/prettyprint/hector/api/beans/AbstractComposite.java +++ b/core/src/main/java/me/prettyprint/hector/api/beans/AbstractComposite.java @@ -79,8 +79,6 @@ public abstract class AbstractComposite extends AbstractList implements BiMap aliasToComparatorMapping = DEFAULT_ALIAS_TO_COMPARATOR_MAPPING; - boolean autoDeserialize = true; - final boolean dynamic; List> serializersByPosition = null; @@ -89,13 +87,15 @@ public abstract class AbstractComposite extends AbstractList implements public class Component { final Serializer serializer; final T value; + final ByteBuffer bytes; final String comparator; final boolean inclusive; - public Component(T value, Serializer serializer, String comparator, - boolean inclusive) { + public Component(T value, ByteBuffer bytes, Serializer serializer, + String comparator, boolean inclusive) { this.serializer = serializer; this.value = value; + this.bytes = bytes; this.comparator = comparator; this.inclusive = inclusive; } @@ -104,8 +104,44 @@ public Serializer getSerializer() { return serializer; } + @SuppressWarnings("unchecked") + public A getValue(Serializer s) { + if (s == null) { + s = (Serializer) serializer; + } + if ((value == null) && (bytes != null) && (s != null)) { + ByteBuffer cb = bytes.duplicate(); + if (cb.hasRemaining()) { + return s.fromByteBuffer(cb); + } + } + return (A) value; + } + public T getValue() { - return value; + return getValue(serializer); + } + + @SuppressWarnings("unchecked") + public ByteBuffer getBytes(Serializer s) { + if (bytes == null) { + if (value instanceof ByteBuffer) { + return ((ByteBuffer) value).duplicate(); + } + if (value != null) { + if (s == null) { + s = (Serializer) serializer; + } + if (s != null) { + return s.toByteBuffer((A) value); + } + } + } + return bytes; + } + + public ByteBuffer getBytes() { + return getBytes(serializer); } public String getComparator() { @@ -117,7 +153,7 @@ public boolean isInclusive() { } } - List components = new ArrayList(); + List> components = new ArrayList>(); ByteBuffer serialized = null; @@ -130,11 +166,11 @@ public AbstractComposite(boolean dynamic, Object... o) { this.addAll(Arrays.asList(o)); } - public List getComponents() { + public List> getComponents() { return components; } - public void setComponents(List components) { + public void setComponents(List> components) { serialized = null; this.components = components; } @@ -161,14 +197,6 @@ public void setAliasesToComparatorMapping( .putAll(aliasesToComparatorMapping).build(); } - public boolean isAutoDeserialize() { - return autoDeserialize; - } - - public void setAutoDeserialize(boolean autoDeserialize) { - this.autoDeserialize = autoDeserialize; - } - public boolean isDynamic() { return dynamic; } @@ -324,7 +352,7 @@ private String getComparator(int i, ByteBuffer bb) { @Override public void clear() { serialized = null; - components = new ArrayList(); + components = new ArrayList>(); } @Override @@ -372,7 +400,7 @@ public AbstractComposite addComponent(int index, T value, while (components.size() < index) { components.add(null); } - components.add(index, new Component(value, s, comparator, inclusive)); + components.add(index, new Component(value, null, s, comparator, inclusive)); return this; @@ -391,7 +419,7 @@ public void add(int index, Object element) { c = element instanceof UUID ? comparatorForUUID((UUID) element) : comparatorForSerializer(s); } - components.add(index, new Component(element, s, c, false)); + components.add(index, new Component(element, null, s, c, false)); } @Override @@ -431,7 +459,7 @@ public AbstractComposite setComponent(int index, T value, while (components.size() <= index) { components.add(null); } - components.set(index, new Component(value, s, comparator, inclusive)); + components.set(index, new Component(value, null, s, comparator, inclusive)); return this; @@ -450,7 +478,8 @@ public Object set(int index, Object element) { c = element instanceof UUID ? comparatorForUUID((UUID) element) : comparatorForSerializer(s); } - Component prev = components.set(index, new Component(element, s, c, false)); + Component prev = components.set(index, new Component(element, null, s, c, + false)); if (prev != null) { return prev.getValue(); } @@ -468,12 +497,9 @@ public Object get(int i) { public T get(int i, Serializer s) throws ClassCastException { T value = null; - Component c = components.get(i); + Component c = components.get(i); if (c != null) { - ByteBuffer cb = ((ByteBuffer) c.getValue()).duplicate(); - if (cb.hasRemaining()) { - value = s.fromByteBuffer(cb); - } + value = c.getValue(s); } return value; } @@ -486,7 +512,7 @@ public Component getComponent(int i) { return c; } - public Iterator componentsIterator() { + public Iterator> componentsIterator() { return components.iterator(); } @@ -500,14 +526,9 @@ public ByteBuffer serialize() { int i = 0; for (Component c : components) { - Serializer s = serializerForPosition(i); - if (s == null) { - s = c.getSerializer(); - } - ByteBuffer cb = null; - if (s != null) { - cb = s.toByteBuffer(c.getValue()); - } else { + Serializer s = serializerForPosition(i); + ByteBuffer cb = c.getBytes(s); + if (cb == null) { cb = ByteBuffer.allocate(0); } @@ -516,6 +537,9 @@ public ByteBuffer serialize() { if (comparator == null) { comparator = c.getComparator(); } + if (comparator == null) { + comparator = BYTESTYPE.getTypeName(); + } int p = comparator.indexOf("(sort=desc)"); boolean desc = false; if (p >= 0) { @@ -546,21 +570,17 @@ public ByteBuffer serialize() { @SuppressWarnings("unchecked") public void deserialize(ByteBuffer b) { serialized = b.duplicate(); - components = new ArrayList(); + components = new ArrayList>(); String comparator = null; int i = 0; while ((comparator = getComparator(i, b)) != null) { ByteBuffer data = getWithShortLength(b); if (data != null) { - Serializer s = autoDeserialize ? getSerializer(i, comparator) - : ByteBufferSerializer.get(); - Object value = null; - if (data.hasRemaining()) { - value = s.fromByteBuffer(data); - } + Serializer s = getSerializer(i, comparator); boolean inclusive = b.get() != 0; - components.add(new Component(value, s, comparator, inclusive)); + components.add(new Component(null, data.slice(), s, comparator, + inclusive)); } else { throw new RuntimeException("Missing component data in composite type"); } diff --git a/core/src/main/java/me/prettyprint/hector/api/beans/DynamicComposite.java b/core/src/main/java/me/prettyprint/hector/api/beans/DynamicComposite.java index b917add54..f016fe521 100644 --- a/core/src/main/java/me/prettyprint/hector/api/beans/DynamicComposite.java +++ b/core/src/main/java/me/prettyprint/hector/api/beans/DynamicComposite.java @@ -13,14 +13,8 @@ public DynamicComposite(Object... o) { } public static DynamicComposite fromByteBuffer(ByteBuffer byteBuffer) { - return fromByteBuffer(true, byteBuffer); - } - - public static DynamicComposite fromByteBuffer(boolean deserializeComponents, - ByteBuffer byteBuffer) { DynamicComposite composite = new DynamicComposite(); - composite.setAutoDeserialize(deserializeComponents); composite.deserialize(byteBuffer); return composite; diff --git a/core/src/test/java/me/prettyprint/hector/api/CompositeTest.java b/core/src/test/java/me/prettyprint/hector/api/CompositeTest.java index d8b518b25..c7216fbda 100644 --- a/core/src/test/java/me/prettyprint/hector/api/CompositeTest.java +++ b/core/src/test/java/me/prettyprint/hector/api/CompositeTest.java @@ -65,11 +65,10 @@ public void testDynamicSerialization() throws Exception { assertEquals(BigInteger.valueOf(10), o); c = new DynamicComposite(); - c.setAutoDeserialize(false); c.deserialize(b.slice()); - assertTrue(c.get(0) instanceof ByteBuffer); - assertTrue(c.get(1) instanceof ByteBuffer); - assertTrue(c.get(2) instanceof ByteBuffer); + assertTrue(c.get(0, ByteBufferSerializer.get()) instanceof ByteBuffer); + assertTrue(c.get(1, ByteBufferSerializer.get()) instanceof ByteBuffer); + assertTrue(c.get(2, ByteBufferSerializer.get()) instanceof ByteBuffer); c = new DynamicComposite(); c.setSerializersByPosition(StringSerializer.get(), null, From 02b1c7cee6a620bb09bdbf18e911f0a595d087a6 Mon Sep 17 00:00:00 2001 From: Ed Anuff Date: Wed, 30 Mar 2011 13:24:22 -0700 Subject: [PATCH 073/144] Removed setAutodeserialize in DynamicCompositeSerializer as it's no longer needed --- .../cassandra/serializers/DynamicCompositeSerializer.java | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/core/src/main/java/me/prettyprint/cassandra/serializers/DynamicCompositeSerializer.java b/core/src/main/java/me/prettyprint/cassandra/serializers/DynamicCompositeSerializer.java index a1e098ce0..26f6e6e1d 100644 --- a/core/src/main/java/me/prettyprint/cassandra/serializers/DynamicCompositeSerializer.java +++ b/core/src/main/java/me/prettyprint/cassandra/serializers/DynamicCompositeSerializer.java @@ -23,11 +23,7 @@ public ByteBuffer toByteBuffer(DynamicComposite obj) { @Override public DynamicComposite fromByteBuffer(ByteBuffer byteBuffer) { - DynamicComposite composite = new DynamicComposite(); - composite.setAutoDeserialize(false); - composite.deserialize(byteBuffer); - - return composite; + return DynamicComposite.fromByteBuffer(byteBuffer); } From ab4a8e210437278d110d4377733e069814da45d4 Mon Sep 17 00:00:00 2001 From: Ed Anuff Date: Wed, 30 Mar 2011 13:42:58 -0700 Subject: [PATCH 074/144] Added a few more tests to CompositeTest --- .../prettyprint/hector/api/CompositeTest.java | 27 +++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/core/src/test/java/me/prettyprint/hector/api/CompositeTest.java b/core/src/test/java/me/prettyprint/hector/api/CompositeTest.java index c7216fbda..ebedb3973 100644 --- a/core/src/test/java/me/prettyprint/hector/api/CompositeTest.java +++ b/core/src/test/java/me/prettyprint/hector/api/CompositeTest.java @@ -1,5 +1,7 @@ package me.prettyprint.hector.api; +import static me.prettyprint.hector.api.ddl.ComparatorType.LEXICALUUIDTYPE; +import static me.prettyprint.hector.api.ddl.ComparatorType.TIMEUUIDTYPE; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertTrue; @@ -24,6 +26,7 @@ public class CompositeTest { @Test public void testDynamicSerialization() throws Exception { + // test correct serialization sizes for strings DynamicComposite c = new DynamicComposite(); c.add("String1"); ByteBuffer b = c.serialize(); @@ -33,6 +36,7 @@ public void testDynamicSerialization() throws Exception { b = c.serialize(); assertEquals(b.remaining(), 24); + // test deserialization of strings c = new DynamicComposite(); c.deserialize(b); assertEquals(2, c.size()); @@ -41,6 +45,7 @@ public void testDynamicSerialization() throws Exception { o = c.get(1); assertEquals("String2", o); + // test serialization and deserialization of longs c = new DynamicComposite(); c.add(new Long(10)); b = c.serialize(); @@ -49,6 +54,26 @@ public void testDynamicSerialization() throws Exception { o = c.get(0); assertTrue(o instanceof Long); + // test serialization and deserialization of random UUIDS + c = new DynamicComposite(); + c.add(UUID.randomUUID()); + b = c.serialize(); + c = DynamicComposite.fromByteBuffer(b); + o = c.get(0); + assertTrue(o instanceof UUID); + assertEquals(LEXICALUUIDTYPE.getTypeName(), c.getComponent(0) + .getComparator()); + + // test serialization and deserialization of time-based UUIDS + c = new DynamicComposite(); + c.add(TimeUUIDUtils.getUniqueTimeUUIDinMillis()); + b = c.serialize(); + c = DynamicComposite.fromByteBuffer(b); + o = c.get(0); + assertTrue(o instanceof UUID); + assertEquals(TIMEUUIDTYPE.getTypeName(), c.getComponent(0).getComparator()); + + // test compatibility with Cassandra unit tests b = createDynamicCompositeKey("Hello", TimeUUIDUtils.getUniqueTimeUUIDinMillis(), 10, false); c = new DynamicComposite(); @@ -64,12 +89,14 @@ public void testDynamicSerialization() throws Exception { assertEquals(BigInteger.class, o.getClass()); assertEquals(BigInteger.valueOf(10), o); + // test using supplied deserializer rather than auto-mapped c = new DynamicComposite(); c.deserialize(b.slice()); assertTrue(c.get(0, ByteBufferSerializer.get()) instanceof ByteBuffer); assertTrue(c.get(1, ByteBufferSerializer.get()) instanceof ByteBuffer); assertTrue(c.get(2, ByteBufferSerializer.get()) instanceof ByteBuffer); + // test setting a deserializer for specific components c = new DynamicComposite(); c.setSerializersByPosition(StringSerializer.get(), null, ByteBufferSerializer.get()); From 72666894c87ef4e45e0c34ef9601e5650b0d85f5 Mon Sep 17 00:00:00 2001 From: zznate Date: Wed, 30 Mar 2011 22:47:36 -0500 Subject: [PATCH 075/144] better use of predicate in template --- .../template/AbstractColumnFamilyTemplate.java | 10 +++++++++- .../service/template/ColumnFamilyTemplate.java | 17 ++++++----------- 2 files changed, 15 insertions(+), 12 deletions(-) diff --git a/core/src/main/java/me/prettyprint/cassandra/service/template/AbstractColumnFamilyTemplate.java b/core/src/main/java/me/prettyprint/cassandra/service/template/AbstractColumnFamilyTemplate.java index 9a3c959a5..b851bbd60 100644 --- a/core/src/main/java/me/prettyprint/cassandra/service/template/AbstractColumnFamilyTemplate.java +++ b/core/src/main/java/me/prettyprint/cassandra/service/template/AbstractColumnFamilyTemplate.java @@ -61,7 +61,7 @@ public AbstractColumnFamilyTemplate(Keyspace keyspace, String columnFamily, this(keyspace, columnFamily, keySerializer, topSerializer, HFactory .createMutator(keyspace, keySerializer)); } - + public AbstractColumnFamilyTemplate(Keyspace keyspace, String columnFamily, Serializer keySerializer, Serializer topSerializer, Mutator mutator) { @@ -76,6 +76,7 @@ public AbstractColumnFamilyTemplate(Keyspace keyspace, String columnFamily, this.activeSlicePredicate = new HSlicePredicate(topSerializer); exceptionsTranslator = new ExceptionsTranslatorImpl(); this.columnFactory = new ThriftColumnFactory(); + setCount(100); } @@ -171,4 +172,11 @@ public void deleteColumn(K key, N columnName) { executeIfNotBatched(); } + /** + * The number of columns to return when not doing a name-based template + * @param count + */ + public void setCount(int count) { + activeSlicePredicate.setCount(count); + } } diff --git a/core/src/main/java/me/prettyprint/cassandra/service/template/ColumnFamilyTemplate.java b/core/src/main/java/me/prettyprint/cassandra/service/template/ColumnFamilyTemplate.java index 0e96941c6..07304ede4 100644 --- a/core/src/main/java/me/prettyprint/cassandra/service/template/ColumnFamilyTemplate.java +++ b/core/src/main/java/me/prettyprint/cassandra/service/template/ColumnFamilyTemplate.java @@ -136,7 +136,10 @@ public ColumnFamilyResult queryColumns(Iterable keys) { @SuppressWarnings("unchecked") public T queryColumns(K key, ColumnFamilyRowMapper mapper) { - return queryColumns(key, (N) ALL_COLUMNS_START, (N) ALL_COLUMNS_END, mapper); + HSlicePredicate predicate = new HSlicePredicate(topSerializer); + predicate.setStartOn((N) ALL_COLUMNS_START); + predicate.setEndOn((N) ALL_COLUMNS_END); + return queryColumns(key, predicate, mapper); } /** @@ -150,12 +153,8 @@ public T queryColumns(K key, ColumnFamilyRowMapper mapper) { * @param mapper * @return */ - public T queryColumns(K key, N start, N end, + public T queryColumns(K key, HSlicePredicate predicate, ColumnFamilyRowMapper mapper) { - HSlicePredicate predicate = new HSlicePredicate(topSerializer); - predicate.setStartOn(start); - predicate.setEndOn(end); - predicate.setCount(100); return doExecuteSlice(key, predicate, mapper); } @@ -184,11 +183,7 @@ public MappedColumnFamilyResult queryColumns(Iterable keys, } public MappedColumnFamilyResult queryColumns(Iterable keys, - N start, N end, ColumnFamilyRowMapper mapper) { - HSlicePredicate predicate = new HSlicePredicate(topSerializer); - predicate.setStartOn(start); - predicate.setEndOn(end); - predicate.setCount(100); + HSlicePredicate predicate, ColumnFamilyRowMapper mapper) { return doExecuteMultigetSlice(keys, predicate, mapper); } From 4832331a42ae034168d47651e6b1f437d5da8c28 Mon Sep 17 00:00:00 2001 From: zznate Date: Thu, 31 Mar 2011 13:04:40 -0500 Subject: [PATCH 076/144] fixed issue with test on cf template, gutted scf template and test for refacetor --- .../template/ColumnFamilyTemplate.java | 5 +- .../service/template/SuperCfTemplate.java | 191 ++---------------- .../template/ColumnFamilyTemplateTest.java | 5 +- .../service/template/SuperCfTemplateTest.java | 11 +- 4 files changed, 20 insertions(+), 192 deletions(-) diff --git a/core/src/main/java/me/prettyprint/cassandra/service/template/ColumnFamilyTemplate.java b/core/src/main/java/me/prettyprint/cassandra/service/template/ColumnFamilyTemplate.java index 07304ede4..3038a7034 100644 --- a/core/src/main/java/me/prettyprint/cassandra/service/template/ColumnFamilyTemplate.java +++ b/core/src/main/java/me/prettyprint/cassandra/service/template/ColumnFamilyTemplate.java @@ -136,10 +136,7 @@ public ColumnFamilyResult queryColumns(Iterable keys) { @SuppressWarnings("unchecked") public T queryColumns(K key, ColumnFamilyRowMapper mapper) { - HSlicePredicate predicate = new HSlicePredicate(topSerializer); - predicate.setStartOn((N) ALL_COLUMNS_START); - predicate.setEndOn((N) ALL_COLUMNS_END); - return queryColumns(key, predicate, mapper); + return queryColumns(key, activeSlicePredicate, mapper); } /** diff --git a/core/src/main/java/me/prettyprint/cassandra/service/template/SuperCfTemplate.java b/core/src/main/java/me/prettyprint/cassandra/service/template/SuperCfTemplate.java index ab0f2341c..0ddcd7dc7 100644 --- a/core/src/main/java/me/prettyprint/cassandra/service/template/SuperCfTemplate.java +++ b/core/src/main/java/me/prettyprint/cassandra/service/template/SuperCfTemplate.java @@ -7,6 +7,7 @@ import java.util.List; import java.util.Map; +import me.prettyprint.cassandra.model.HSlicePredicate; import me.prettyprint.cassandra.serializers.ByteBufferSerializer; import me.prettyprint.cassandra.serializers.SerializerTypeInferer; import me.prettyprint.hector.api.Keyspace; @@ -133,200 +134,36 @@ public int countSubColumns(K key, SN superColumnName, N start, } @SuppressWarnings("unchecked") - public HColumn querySingleSubColumn(K key, - SN columnName, N subColumnName, Class valueClass) { - return querySingleSubColumn(key, columnName, subColumnName, - (Serializer) SerializerTypeInferer.getSerializer(valueClass)); + public HColumn querySingleSubColumn(K key, + SN columnName, N subColumnName, Class valueClass) { + return null; } public HColumn querySingleSubColumn(K key, SN columnName, N subColumnName, Serializer valueSerializer) { - SubColumnQuery query = HFactory - .createSubColumnQuery(keyspace, keySerializer, topSerializer, - subSerializer, valueSerializer); - query.setColumnFamily(columnFamily); - query.setKey(key); - query.setSuperColumn(columnName); - query.setColumn(subColumnName); - QueryResult> result = query.execute(); - return result != null ? result.get() : null; - } - - /** - * Writes a series of object in a row of a super column family. Each object's - * properties are written to a super-column's child columns - * - * @param - * the type of Object to be persisted - * @param - * the type of the sub column name - * @param key - * the row key - * @param objects - * list of objects to write to the db - * @param subSerializer - * the serializer for the child columns in the super column - * @param updater - * the object which performs updates on a - */ - public void update(K key, List objects, - SuperCfUpdater updater) { - if (objects == null || objects.size() == 0) { - return; - } - - updater.key = key; - updater.subSerializer = subSerializer; - - for (OBJ obj : objects) { - updater.columns = new ArrayList>(); - - SN columnName = updater.update(obj); - - HSuperColumn superColumn = HFactory - .createSuperColumn(columnName, updater.columns, topSerializer, - subSerializer, ByteBufferSerializer.get()); - mutator.addInsertion(key, columnFamily, superColumn); - - if (updater.columnsToDelete != null) { - HSuperColumn superColumnWithDeletes = HFactory - .createSuperColumn(columnName, updater.columnsToDelete, - topSerializer, subSerializer, ByteBufferSerializer.get()); - mutator.addSubDelete(key, columnFamily, superColumnWithDeletes); - updater.columnsToDelete = null; - } - } - executeIfNotBatched(); - } - - @SuppressWarnings("unchecked") - public List querySuperColumns(K key, - SuperCfRowMapper mapper) { - return querySuperColumns(key, (SN) ALL_COLUMNS_START, - (SN) ALL_COLUMNS_END, mapper); - } - - public List querySuperColumns(K key, SN start, SN end, - SuperCfRowMapper mapper) { - SuperSliceQuery query = createSuperSliceQuery(key); - query.setRange(start, end, false, ALL_COLUMNS_COUNT); - return executeSuperSliceQuery(key, query, mapper); - } - - @SuppressWarnings("unchecked") - public List querySuperColumns(K key, List columns, - SuperCfRowMapper mapper) { - SuperSliceQuery query = createSuperSliceQuery(key); - query.setColumnNames((SN[]) columns.toArray()); - return executeSuperSliceQuery(key, query, mapper); - } - - private List executeSuperSliceQuery(K key, - SuperSliceQuery query, - SuperCfRowMapper mapper) { - QueryResult> result = query.execute(); - SuperSlice slice = result.get(); - if (slice.getSuperColumns() != null && slice.getSuperColumns().size() > 0) { - List ret = new ArrayList(); - for (HSuperColumn superCol : slice - .getSuperColumns()) { - /*SuperCfResultWrapper wrapper = new SuperCfResultWrapper(key, superCol); - ret.add(mapper.mapRow(wrapper));*/ - } - return ret; - } return null; } - private SuperSliceQuery createSuperSliceQuery( - K key) { - SuperSliceQuery query = HFactory - .createSuperSliceQuery(keyspace, keySerializer, topSerializer, - subSerializer, ByteBufferSerializer.get()); - query.setKey(key); - query.setColumnFamily(columnFamily); - return query; - } -/* @SuppressWarnings("unchecked") - public ColumnFamilyResultsIterator> querySuperColumns( - List key, SuperCfRowMapper mapper) { - return querySuperColumns(key, (SN) ALL_COLUMNS_START, - (SN) ALL_COLUMNS_END, mapper); + public List querySuperColumns(K key, + SuperCfRowMapper mapper) { + return null; } - public ColumnFamilyResultsIterator> querySuperColumns( - List key, SN start, SN end, - SuperCfRowMapper mapper) { - MultigetSuperSliceQuery query = createMultigetSuperSliceQuery(key); - query.setRange(start, end, false, ALL_COLUMNS_COUNT); - return executeMultigetSuperSliceQuery(key, query, mapper); + public List querySuperColumns(K key, HSlicePredicate predicate, + SuperCfRowMapper mapper) { + return null; } @SuppressWarnings("unchecked") - public ColumnFamilyResultsIterator> querySuperColumns( - List key, List columns, - SuperCfRowMapper mapper) { - MultigetSuperSliceQuery query = createMultigetSuperSliceQuery(key); - query.setColumnNames((SN[]) columns.toArray()); - return executeMultigetSuperSliceQuery(key, query, mapper); + public List querySuperColumns(K key, List columns, + SuperCfRowMapper mapper) { + return null; } - private ColumnFamilyResultsIterator> executeMultigetSuperSliceQuery( - final List key, - MultigetSuperSliceQuery query, - final SuperCfRowMapper mapper) { - QueryResult> result = query - .execute(); - final SuperRows rows = result.get(); - - final Iterator> iter = rows - .iterator(); - return new ColumnFamilyResultsIterator>() { - public boolean hasNext() { - return iter.hasNext(); - } - - public List next() { - return mapRow(iter.next()); - } - - public void remove() { - throw new UnsupportedOperationException("remove() is not supported"); - } - public List getByKey(K key) { - return mapRow(rows.getByKey(key)); - } - public int getCount() { - return rows.getCount(); - } - private List mapRow(SuperRow row) { - SuperSlice slice = row.getSuperSlice(); - List ret = new ArrayList(); - for (HSuperColumn superCol : slice - .getSuperColumns()) { - SuperCfResultWrapper wrapper = new SuperCfResultWrapper(row.getKey(), - superCol); - ret.add(mapper.mapRow(wrapper)); - } - return ret; - } - }; - } - */ - - @SuppressWarnings("unchecked") - private MultigetSuperSliceQuery createMultigetSuperSliceQuery( - List keys) { - MultigetSuperSliceQuery query = HFactory - .createMultigetSuperSliceQuery(keyspace, keySerializer, topSerializer, - subSerializer, ByteBufferSerializer.get()); - query.setKeys((K[]) keys.toArray()); - query.setColumnFamily(columnFamily); - return query; - } + } diff --git a/core/src/test/java/me/prettyprint/cassandra/service/template/ColumnFamilyTemplateTest.java b/core/src/test/java/me/prettyprint/cassandra/service/template/ColumnFamilyTemplateTest.java index 4e2ef948e..12f709d7d 100644 --- a/core/src/test/java/me/prettyprint/cassandra/service/template/ColumnFamilyTemplateTest.java +++ b/core/src/test/java/me/prettyprint/cassandra/service/template/ColumnFamilyTemplateTest.java @@ -4,6 +4,7 @@ import java.util.Arrays; +import me.prettyprint.cassandra.model.HSlicePredicate; import me.prettyprint.hector.api.factory.HFactory; import org.junit.Test; @@ -30,8 +31,8 @@ public void testCreateSelectTemplate() { ColumnFamilyUpdater updater = template.createUpdater("key1"); updater.setString("column1","value1"); updater.update(); - - String value = template.queryColumns("key1", "", "", new ColumnFamilyRowMapper() { + template.setCount(10); + String value = template.queryColumns("key1", new ColumnFamilyRowMapper() { @Override public String mapRow(ColumnFamilyResult results) { // TODO Auto-generated method stub diff --git a/core/src/test/java/me/prettyprint/cassandra/service/template/SuperCfTemplateTest.java b/core/src/test/java/me/prettyprint/cassandra/service/template/SuperCfTemplateTest.java index 641b3d3bf..719154855 100644 --- a/core/src/test/java/me/prettyprint/cassandra/service/template/SuperCfTemplateTest.java +++ b/core/src/test/java/me/prettyprint/cassandra/service/template/SuperCfTemplateTest.java @@ -4,23 +4,16 @@ import java.util.Arrays; +import org.junit.Ignore; import org.junit.Test; +@Ignore public class SuperCfTemplateTest extends BaseColumnFamilyTemplateTest { @Test public void testSuperCfInsertReadTemplate() { SuperCfTemplate sTemplate = new SuperCfTemplate(keyspace, "Super1", se, se, se); - sTemplate.update("super_key_1", Arrays.asList("super_name_1"), new SuperCfUpdater() { - @Override - public String update(String obj) { - setString("sub_col_1", "sub_val_1"); - return obj; - } - }); - - assertEquals("sub_val_1",sTemplate.querySingleSubColumn("super_key_1", "super_name_1", "sub_col_1", se).getValue()); } @Test From 192e676aa70f463fe9d5f5c648d00807b62b27e4 Mon Sep 17 00:00:00 2001 From: zznate Date: Thu, 31 Mar 2011 18:18:40 -0500 Subject: [PATCH 077/144] supercol inserts working on template innards --- .../template/AbstractTemplateUpdater.java | 44 ++++++ .../template/ColumnFamilyStringUpdater.java | 18 --- .../service/template/ColumnFamilyUpdater.java | 64 +++----- .../template/SuperCfResultWrapper.java | 105 ++++++++----- .../template/SuperCfStringTemplate.java | 42 ----- .../template/SuperCfStringUpdater.java | 11 -- .../service/template/SuperCfTemplate.java | 72 ++++++++- .../service/template/SuperCfUpdater.java | 147 +++++++++++------- 8 files changed, 289 insertions(+), 214 deletions(-) create mode 100644 core/src/main/java/me/prettyprint/cassandra/service/template/AbstractTemplateUpdater.java delete mode 100644 core/src/main/java/me/prettyprint/cassandra/service/template/ColumnFamilyStringUpdater.java delete mode 100644 core/src/main/java/me/prettyprint/cassandra/service/template/SuperCfStringTemplate.java delete mode 100644 core/src/main/java/me/prettyprint/cassandra/service/template/SuperCfStringUpdater.java diff --git a/core/src/main/java/me/prettyprint/cassandra/service/template/AbstractTemplateUpdater.java b/core/src/main/java/me/prettyprint/cassandra/service/template/AbstractTemplateUpdater.java new file mode 100644 index 000000000..74d94afd6 --- /dev/null +++ b/core/src/main/java/me/prettyprint/cassandra/service/template/AbstractTemplateUpdater.java @@ -0,0 +1,44 @@ +package me.prettyprint.cassandra.service.template; + +import java.util.ArrayList; +import java.util.List; + +import me.prettyprint.hector.api.ColumnFactory; + +public abstract class AbstractTemplateUpdater { + + private List keys; + private int keyPos = 0; + protected ColumnFactory columnFactory; + protected AbstractColumnFamilyTemplate template; + + public AbstractTemplateUpdater(AbstractColumnFamilyTemplate template, ColumnFactory columnFactory) { + this.template = template; + this.columnFactory = columnFactory; + } + + public AbstractTemplateUpdater addKey(K key) { + if ( keys == null ) { + keys = new ArrayList(); + } else { + keyPos++; + } + keys.add(key); + + return this; + } + + /** + * @return Give the updater access to the current key if it needs it + */ + public K getCurrentKey() { + return keys.get(keyPos); + } + + /** + * To be overridden by folks choosing to add their own functionality. Default is a no-op. + */ + public void update() { + + } +} diff --git a/core/src/main/java/me/prettyprint/cassandra/service/template/ColumnFamilyStringUpdater.java b/core/src/main/java/me/prettyprint/cassandra/service/template/ColumnFamilyStringUpdater.java deleted file mode 100644 index 958cfee7e..000000000 --- a/core/src/main/java/me/prettyprint/cassandra/service/template/ColumnFamilyStringUpdater.java +++ /dev/null @@ -1,18 +0,0 @@ -package me.prettyprint.cassandra.service.template; - -import me.prettyprint.hector.api.ColumnFactory; - -/** - * A simple specialization of the generic class for the very common all-string - * column name scenario. - * - * @author david - * @since Mar 10, 2011 - */ -public abstract class ColumnFamilyStringUpdater extends ColumnFamilyUpdater { - - public ColumnFamilyStringUpdater( - ColumnFamilyTemplate template, ColumnFactory columnFactory) { - super(template, columnFactory); - } -} diff --git a/core/src/main/java/me/prettyprint/cassandra/service/template/ColumnFamilyUpdater.java b/core/src/main/java/me/prettyprint/cassandra/service/template/ColumnFamilyUpdater.java index a78cec37c..e75c37376 100644 --- a/core/src/main/java/me/prettyprint/cassandra/service/template/ColumnFamilyUpdater.java +++ b/core/src/main/java/me/prettyprint/cassandra/service/template/ColumnFamilyUpdater.java @@ -1,14 +1,14 @@ package me.prettyprint.cassandra.service.template; -import java.util.ArrayList; +import java.nio.ByteBuffer; import java.util.Date; -import java.util.List; import java.util.UUID; -import me.prettyprint.cassandra.model.thrift.ThriftColumnFactory; 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; @@ -16,8 +16,6 @@ import me.prettyprint.hector.api.ColumnFactory; import me.prettyprint.hector.api.Serializer; import me.prettyprint.hector.api.beans.HColumn; -import me.prettyprint.hector.api.factory.HFactory; -import me.prettyprint.hector.api.mutation.Mutator; /** * This provides an interface of updating a specified row, most likely with the @@ -42,44 +40,12 @@ * @param * the standard or super column's data type */ -public class ColumnFamilyUpdater { - // Values have package access and are assigned by CassandraTemplate - private ColumnFamilyTemplate template; - private List keys; - private int keyPos = 0; - private ColumnFactory columnFactory; +public class ColumnFamilyUpdater extends AbstractTemplateUpdater { public ColumnFamilyUpdater(ColumnFamilyTemplate template, ColumnFactory columnFactory) { - this.template = template; - this.columnFactory = columnFactory; + super(template, columnFactory); } - /** - * Default no-op update implementation. Sub-classes should override this to provide - * for more complex behaviour - */ - public void update() { - // TODO think about this contract in general - } - - public ColumnFamilyUpdater addKey(K key) { - if ( keys == null ) { - keys = new ArrayList(); - } else { - keyPos++; - } - keys.add(key); - - return this; - } - - /** - * @return Give the updater access to the current key if it needs it - */ - public K getCurrentKey() { - return keys.get(keyPos); - } - public void deleteColumn(N columnName) { template.getMutator().addDeletion(getCurrentKey(), template.getColumnFamily(), columnName, template.getTopSerializer()); @@ -108,7 +74,13 @@ public void setInteger(N columnName, Integer value) { template.getTopSerializer(), IntegerSerializer.get()); template.getMutator().addInsertion(getCurrentKey(), template.getColumnFamily(), column); } - + + public void setDouble(N columnName, Double value) { + HColumn column = columnFactory.createColumn(columnName, value, + template.getTopSerializer(), DoubleSerializer.get()); + template.getMutator().addInsertion(getCurrentKey(), template.getColumnFamily(), column); + } + public void setBoolean(N columnName, Boolean value) { HColumn column = columnFactory.createColumn(columnName, value, template.getTopSerializer(), BooleanSerializer.get()); @@ -121,9 +93,21 @@ public void setByteArray(N columnName, byte[] value) { template.getMutator().addInsertion(getCurrentKey(), template.getColumnFamily(), column); } + public void setByteBuffer(N columnName, ByteBuffer value) { + HColumn column = columnFactory.createColumn(columnName, value, + template.getTopSerializer(), ByteBufferSerializer.get()); + template.getMutator().addInsertion(getCurrentKey(), template.getColumnFamily(), column); + } + public void setDate(N columnName, Date value) { HColumn column = columnFactory.createColumn(columnName, value, template.getTopSerializer(), DateSerializer.get()); template.getMutator().addInsertion(getCurrentKey(), template.getColumnFamily(), column); } + + public void setValue(N columnName, V value, Serializer serializer) { + HColumn column = columnFactory.createColumn(columnName, value, + template.getTopSerializer(), serializer); + template.getMutator().addInsertion(getCurrentKey(), template.getColumnFamily(), column); + } } diff --git a/core/src/main/java/me/prettyprint/cassandra/service/template/SuperCfResultWrapper.java b/core/src/main/java/me/prettyprint/cassandra/service/template/SuperCfResultWrapper.java index 58938b815..ef4fb4f23 100644 --- a/core/src/main/java/me/prettyprint/cassandra/service/template/SuperCfResultWrapper.java +++ b/core/src/main/java/me/prettyprint/cassandra/service/template/SuperCfResultWrapper.java @@ -2,12 +2,18 @@ import java.nio.ByteBuffer; import java.util.HashMap; +import java.util.Iterator; +import java.util.LinkedHashMap; import java.util.List; import java.util.Map; +import java.util.NoSuchElementException; import org.apache.cassandra.thrift.ColumnOrSuperColumn; import me.prettyprint.cassandra.model.ExecutionResult; +import me.prettyprint.cassandra.model.HColumnImpl; +import me.prettyprint.cassandra.model.HSuperColumnImpl; +import me.prettyprint.cassandra.serializers.ByteBufferSerializer; import me.prettyprint.cassandra.service.CassandraHost; import me.prettyprint.hector.api.Serializer; import me.prettyprint.hector.api.beans.HColumn; @@ -27,64 +33,79 @@ */ public class SuperCfResultWrapper extends AbstractResultWrapper implements SuperCfResult { - + private Map> columns = new LinkedHashMap>(); + private Iterator>> rows; + private Map.Entry> entry; + private ExecutionResult>> executionResult; + private Serializer subSerializer; + protected Serializer columnNameSerializer; + public SuperCfResultWrapper(Serializer keySerializer, - Serializer columnNameSerializer, + Serializer topSerializer, + Serializer subSerializer, ExecutionResult>> executionResult) { - super(keySerializer, columnNameSerializer, executionResult); - // TODO Auto-generated constructor stub + super(keySerializer, null, executionResult); + this.columnNameSerializer = topSerializer; + this.subSerializer = subSerializer; + this.rows = executionResult.get().entrySet().iterator(); + next(); } - private HSuperColumn superColumn; - private List> subColumns; - private Map> subColumnsMap = new HashMap>(); - + @Override + public SuperCfResult next() { + if ( !hasNext() ) { + throw new NoSuchElementException("No more rows left on this HColumnFamily"); + } + entry = rows.next(); + applyToRow(keySerializer.fromByteBuffer(entry.getKey()), entry.getValue()); + return this; + } - - /** - * Provides access to the current super column name from the mapper objects. This may be - * needed when the super columm name may be part of the object being mapped into. - */ - public SN getSuperName() - { - return superColumn.getName(); + @Override + public boolean hasNext() { + return rows.hasNext(); } - public ByteBuffer getColumnValue( N columnName) - { - HColumn col = getColumn( columnName ); - return col != null ? col.getValue() : null; + @Override + public void remove() { + rows.remove(); } - private HColumn getColumn( N columnName ) - { - return subColumnsMap.get( columnName ); + private void applyToRow(K key, List cosclist) { + HSuperColumn column; + SN colName; + for (Iterator iterator = cosclist.iterator(); iterator.hasNext();) { + ColumnOrSuperColumn cosc = iterator.next(); + + colName = columnNameSerializer.fromByteBuffer(cosc.getSuper_column().name.duplicate()); + column = new HSuperColumnImpl(cosc.getSuper_column(), columnNameSerializer, subSerializer, ByteBufferSerializer.get()); + // TODO cache columns + // TODO add clear() on HSuperColumnImpl + columns.put(colName, column); + iterator.remove(); + } } @Override - public K getKey() { - // TODO Auto-generated method stub - return null; + public K getKey() { + return keySerializer.fromByteBuffer(entry.getKey()); } + - @Override - public boolean hasNext() { - // TODO Auto-generated method stub - return false; + /** + * Provides access to the current super column name from the mapper objects. This may be + * needed when the super columm name may be part of the object being mapped into. + */ + public SN getSuperName() { + return columnNameSerializer.fromByteBuffer(entry.getKey()); } - @Override - public ColumnFamilyResult next() { - // TODO Auto-generated method stub - return null; - } - @Override - public void remove() { - // TODO Auto-generated method stub - + private HSuperColumn getColumn( SN columnName ) { + return columns.get(columnName); } + @Override public long getExecutionTimeMicro() { // TODO Auto-generated method stub @@ -96,4 +117,10 @@ public CassandraHost getHostUsed() { // TODO Auto-generated method stub return null; } + + @Override + public ByteBuffer getColumnValue(N columnName) { + // TODO Auto-generated method stub + return null; + } } diff --git a/core/src/main/java/me/prettyprint/cassandra/service/template/SuperCfStringTemplate.java b/core/src/main/java/me/prettyprint/cassandra/service/template/SuperCfStringTemplate.java deleted file mode 100644 index 6d31e2463..000000000 --- a/core/src/main/java/me/prettyprint/cassandra/service/template/SuperCfStringTemplate.java +++ /dev/null @@ -1,42 +0,0 @@ -package me.prettyprint.cassandra.service.template; - -import me.prettyprint.cassandra.serializers.StringSerializer; -import me.prettyprint.hector.api.Keyspace; -import me.prettyprint.hector.api.mutation.Mutator; - -/** - * A simple specialization of the generic class for the very common all-string - * column name and key type scenario. This just provided the StringSerializer at - * all the places required by the generic base type. There are method overloads - * for update/query that remove the need to pass serializers anywhere. - * - * @author david - * @since Mar 10, 2011 - */ -public class SuperCfStringTemplate extends SuperCfTemplate { - - public SuperCfStringTemplate(Keyspace keyspace, String columnFamily) { - super(keyspace, columnFamily, StringSerializer.get(), StringSerializer - .get(), StringSerializer.get()); - } - - public SuperCfStringTemplate(Keyspace keyspace, String columnFamily, - Mutator mutator) { - super(keyspace, columnFamily, StringSerializer.get(), StringSerializer - .get(), StringSerializer.get(), mutator); - } - - // Just so method chaining will return this type instead of the parent class - // for operations down the chain - public SuperCfStringTemplate setBatched(boolean batched) { - super.setBatched(batched); - return this; - } - - // Just so method chaining will return this type instead of the parent class - // for operations down the chain - public SuperCfStringTemplate setMutator(Mutator mutator) { - super.setMutator(mutator); - return this; - } -} diff --git a/core/src/main/java/me/prettyprint/cassandra/service/template/SuperCfStringUpdater.java b/core/src/main/java/me/prettyprint/cassandra/service/template/SuperCfStringUpdater.java deleted file mode 100644 index 7309a38c6..000000000 --- a/core/src/main/java/me/prettyprint/cassandra/service/template/SuperCfStringUpdater.java +++ /dev/null @@ -1,11 +0,0 @@ -package me.prettyprint.cassandra.service.template; - -/** - * A simple specialization of the generic class for the very common all-string - * column name scenario. - * - * @author david - * @since Mar 10, 2011 - */ -public abstract class SuperCfStringUpdater extends SuperCfUpdater { -} diff --git a/core/src/main/java/me/prettyprint/cassandra/service/template/SuperCfTemplate.java b/core/src/main/java/me/prettyprint/cassandra/service/template/SuperCfTemplate.java index 0ddcd7dc7..d8b77b445 100644 --- a/core/src/main/java/me/prettyprint/cassandra/service/template/SuperCfTemplate.java +++ b/core/src/main/java/me/prettyprint/cassandra/service/template/SuperCfTemplate.java @@ -4,12 +4,22 @@ import java.util.ArrayList; import java.util.HashMap; import java.util.Iterator; +import java.util.LinkedHashMap; import java.util.List; import java.util.Map; +import org.apache.cassandra.thrift.Cassandra; +import org.apache.cassandra.thrift.ColumnOrSuperColumn; +import org.apache.cassandra.thrift.ColumnParent; + +import me.prettyprint.cassandra.model.ExecutingKeyspace; +import me.prettyprint.cassandra.model.ExecutionResult; import me.prettyprint.cassandra.model.HSlicePredicate; +import me.prettyprint.cassandra.model.thrift.ThriftConverter; import me.prettyprint.cassandra.serializers.ByteBufferSerializer; import me.prettyprint.cassandra.serializers.SerializerTypeInferer; +import me.prettyprint.cassandra.service.Operation; +import me.prettyprint.cassandra.service.OperationType; import me.prettyprint.hector.api.Keyspace; import me.prettyprint.hector.api.Serializer; import me.prettyprint.hector.api.beans.HColumn; @@ -17,6 +27,7 @@ import me.prettyprint.hector.api.beans.SuperRow; import me.prettyprint.hector.api.beans.SuperRows; import me.prettyprint.hector.api.beans.SuperSlice; +import me.prettyprint.hector.api.exceptions.HectorException; import me.prettyprint.hector.api.factory.HFactory; import me.prettyprint.hector.api.mutation.Mutator; import me.prettyprint.hector.api.query.MultigetSuperSliceQuery; @@ -40,9 +51,13 @@ public SuperCfTemplate(Keyspace keyspace, String columnFamily, Serializer keySerializer, Serializer topSerializer, Serializer subSerializer, Mutator mutator) { super(keyspace, columnFamily, keySerializer, topSerializer, mutator); - + this.subSerializer = subSerializer; } + public Serializer getSubSerializer() { + return subSerializer; + } + /** * Checks if there are any columns at a row specified by key in a super column * family @@ -138,12 +153,14 @@ public HColumn querySingleSubColumn(K key, SN columnName, N subColumnName, Class valueClass) { return null; } + + public HColumn querySingleSubColumn(K key, SN columnName, N subColumnName, Serializer valueSerializer) { return null; } - + @SuppressWarnings("unchecked") public List querySuperColumns(K key, SuperCfRowMapper mapper) { @@ -160,10 +177,55 @@ public List querySuperColumns(K key, List columns, SuperCfRowMapper mapper) { return null; } + + public SuperCfUpdater createUpdater(K key, SN sColumnName) { + SuperCfUpdater updater = new SuperCfUpdater(this, columnFactory); + updater.addKey(key); + updater.addSuperColumn(sColumnName); + return updater; + } - - - + public void update(SuperCfUpdater updater) { + updater.updateInternal(); + updater.update(); + executeIfNotBatched(); + } + + public SuperCfResult querySuperColumn(K key, SN sColumnName) { + ColumnParent workingColumnParent = columnParent.deepCopy(); + workingColumnParent.setSuper_column(topSerializer.toByteBuffer(sColumnName)); + return doExecuteSlice(key, workingColumnParent, activeSlicePredicate); + } + + protected SuperCfResult doExecuteSlice(K key, ColumnParent workingColumnParent, HSlicePredicate predicate) { + SuperCfResultWrapper wrapper = + new SuperCfResultWrapper(keySerializer, topSerializer, subSerializer, + sliceInternal(key, workingColumnParent, predicate)); + return wrapper; + } + + private ExecutionResult>> sliceInternal(final K key, + final ColumnParent workingColumnParent, + final HSlicePredicate workingSlicePredicate) { + return ((ExecutingKeyspace)keyspace).doExecuteOperation(new Operation>>(OperationType.READ) { + @Override + public Map> execute(Cassandra.Client cassandra) throws HectorException { + Map> cosc = new LinkedHashMap>(); + try { + + ByteBuffer sKey = keySerializer.toByteBuffer(key); + cosc.put(sKey, cassandra.get_slice(sKey, workingColumnParent, + workingSlicePredicate.toThrift(), + ThriftConverter.consistencyLevel(consistencyLevelPolicy.get(operationType)))); + + } catch (Exception e) { + throw exceptionsTranslator.translate(e); + } + + return cosc; + } + }); + } } diff --git a/core/src/main/java/me/prettyprint/cassandra/service/template/SuperCfUpdater.java b/core/src/main/java/me/prettyprint/cassandra/service/template/SuperCfUpdater.java index 69a6b038e..dd185abbd 100644 --- a/core/src/main/java/me/prettyprint/cassandra/service/template/SuperCfUpdater.java +++ b/core/src/main/java/me/prettyprint/cassandra/service/template/SuperCfUpdater.java @@ -2,24 +2,25 @@ import java.nio.ByteBuffer; import java.util.ArrayList; +import java.util.Arrays; import java.util.Date; import java.util.List; import java.util.UUID; -import me.prettyprint.cassandra.model.HColumnImpl; +import me.prettyprint.cassandra.model.HSuperColumnImpl; 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.TypeInferringSerializer; import me.prettyprint.cassandra.serializers.UUIDSerializer; +import me.prettyprint.hector.api.ColumnFactory; import me.prettyprint.hector.api.Serializer; import me.prettyprint.hector.api.beans.HColumn; -import me.prettyprint.hector.api.beans.HSuperColumn; -import me.prettyprint.hector.api.factory.HFactory; -import me.prettyprint.hector.api.mutation.Mutator; /** * This provides an interface of updating a specified row, most likely with the @@ -50,80 +51,108 @@ * @param * the object instance to persist */ -public abstract class SuperCfUpdater { - // Values have package access and are assigned by CassandraTemplate - K key; - Serializer subSerializer; - List> columns; - List> columnsToDelete; - +public class SuperCfUpdater extends AbstractTemplateUpdater { + + protected SuperCfTemplate template; + private List sColumnNames; + private int sColPos; + private HSuperColumnImpl activeColumn; + private List subColumns; + + public SuperCfUpdater(SuperCfTemplate sTemplate, ColumnFactory columnFactory) { + super((AbstractColumnFamilyTemplate) sTemplate, columnFactory); + this.template = sTemplate; + } + + public SuperCfUpdater addSuperColumn(SN sColumnName) { + if ( sColumnNames == null ) { + sColumnNames = new ArrayList(); + subColumns = new ArrayList(); + } else { + sColPos++; + } + sColumnNames.add(sColumnName); + + return this; + } + + public SN getCurrentSuperColumn() { + return sColumnNames.get(sColPos); + } + /** - * The CassandraTemplate update methods for super columns take a list of - * objects to persist. The update() method of this updater is call once for - * every object in the collection. The appropriate internally variables are - * initialized between calls so that the correct is saved. - * - * Since the updater has intimate knowledge of the object being persisted, it - * should also know which super column this is being saved into. It conveys - * this to the CassandraTemplate by returning the value which will be the - * super column name these updates will go into. - * - * @param obj - * @return the super column name where the object contents are saved + * collapse the state of the active HSuperColumn */ - public abstract SN update(V obj); + void updateInternal() { + // HSuperColumnImpl needs a refactor, this construction is lame. + // the value serializer is not used in HSuperColumnImpl, so this is safe for name + HSuperColumnImpl column = new HSuperColumnImpl(getCurrentSuperColumn(), subColumns, + template.getEffectiveClock(), template.getTopSerializer(), template.getSubSerializer(), TypeInferringSerializer.get()); + template.getMutator().addInsertion(getCurrentKey(), template.getColumnFamily(), column); + } /** - * Give the updater access to the current key being updated - * - * @return + * Deletes the super column and all of its sub columns */ - public K getKey() { - return key; + public void deleteSuperColumn(SN sColumnName) { + template.getMutator().addDeletion(getCurrentKey(), template.getColumnFamily(), + sColumnName, template.getTopSerializer()); } - - public void deleteColumn(N columnName) { - if (columnsToDelete == null) { - columnsToDelete = new ArrayList>(); - } - HColumn col = new HColumnImpl( - subSerializer, ByteBufferSerializer.get()); - col.setName(columnName); - columnsToDelete.add(col); + + public void deleteSubColumn(SN sColumnName, N columnName) { + template.getMutator().addSubDelete(getCurrentKey(), template.getColumnFamily(), + new HSuperColumnImpl(sColumnName, (List>)Arrays.asList(columnFactory.createColumn(columnName, ByteBuffer.wrap(new byte[]{}), + template.getSubSerializer(), ByteBufferSerializer.get())), template.getEffectiveClock(), + template.getTopSerializer(), template.getSubSerializer(), ByteBufferSerializer.get())); } - public void setString(N columnName, String value) { - columns.add(HFactory.createColumn(columnName, StringSerializer.get() - .toByteBuffer(value), subSerializer, ByteBufferSerializer.get())); + public void setString(N subColumnName, String value) { + subColumns.add(columnFactory.createColumn(subColumnName, value, + template.getSubSerializer(), StringSerializer.get())); } - public void setUUID(N columnName, UUID value) { - columns.add(HFactory.createColumn(columnName, UUIDSerializer.get() - .toByteBuffer(value), subSerializer, ByteBufferSerializer.get())); + public void setUUID(N subColumnName, UUID value) { + subColumns.add(columnFactory.createColumn(subColumnName, value, + template.getSubSerializer(), UUIDSerializer.get())); } - public void setLong(N columnName, Long value) { - columns.add(HFactory.createColumn(columnName, LongSerializer.get() - .toByteBuffer(value), subSerializer, ByteBufferSerializer.get())); + public void setLong(N subColumnName, Long value) { + subColumns.add(columnFactory.createColumn(subColumnName, value, + template.getSubSerializer(), LongSerializer.get())); } - public void setInteger(N columnName, Integer value) { - columns.add(HFactory.createColumn(columnName, IntegerSerializer.get() - .toByteBuffer(value), subSerializer, ByteBufferSerializer.get())); + public void setInteger(N subColumnName, Integer value) { + subColumns.add(columnFactory.createColumn(subColumnName, value, + template.getSubSerializer(), IntegerSerializer.get())); } - public void setBoolean(N columnName, Boolean value) { - columns.add(HFactory.createColumn(columnName, BooleanSerializer.get() - .toByteBuffer(value), subSerializer, ByteBufferSerializer.get())); + public void setBoolean(N subColumnName, Boolean value) { + subColumns.add(columnFactory.createColumn(subColumnName, value, + template.getSubSerializer(), BooleanSerializer.get())); } - public void setByteArray(N columnName, byte[] value) { - columns.add(HFactory.createColumn(columnName, BytesArraySerializer.get() - .toByteBuffer(value), subSerializer, ByteBufferSerializer.get())); + public void setByteArray(N subColumnName, byte[] value) { + subColumns.add(columnFactory.createColumn(subColumnName, value, + template.getSubSerializer(), BytesArraySerializer.get())); } - public void setDate(N columnName, Date value) { - columns.add(HFactory.createColumn(columnName, DateSerializer.get() - .toByteBuffer(value), subSerializer, ByteBufferSerializer.get())); + public void setByteBuffer(N subColumnName, ByteBuffer value) { + subColumns.add(columnFactory.createColumn(subColumnName, value, + template.getSubSerializer(), ByteBufferSerializer.get())); + } + + public void setDate(N subColumnName, Date value) { + subColumns.add(columnFactory.createColumn(subColumnName, value, + template.getSubSerializer(), DateSerializer.get())); + } + + public void setDouble(N subColumnName, Double value) { + subColumns.add(columnFactory.createColumn(subColumnName, value, + template.getSubSerializer(), DoubleSerializer.get())); + } + + public void setValue(N subColumnName, V value, Serializer serializer) { + subColumns.add(columnFactory.createColumn(subColumnName, value, + template.getSubSerializer(), serializer)); } } From f41c07f7f13bd13da9eca23c0751a639dee06100 Mon Sep 17 00:00:00 2001 From: zznate Date: Thu, 31 Mar 2011 18:19:16 -0500 Subject: [PATCH 078/144] supercol inserts working on template innards --- .../service/template/SuperCfTemplateTest.java | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/core/src/test/java/me/prettyprint/cassandra/service/template/SuperCfTemplateTest.java b/core/src/test/java/me/prettyprint/cassandra/service/template/SuperCfTemplateTest.java index 719154855..23eb5d368 100644 --- a/core/src/test/java/me/prettyprint/cassandra/service/template/SuperCfTemplateTest.java +++ b/core/src/test/java/me/prettyprint/cassandra/service/template/SuperCfTemplateTest.java @@ -2,8 +2,6 @@ import static org.junit.Assert.*; -import java.util.Arrays; - import org.junit.Ignore; import org.junit.Test; @@ -14,12 +12,13 @@ public class SuperCfTemplateTest extends BaseColumnFamilyTemplateTest { public void testSuperCfInsertReadTemplate() { SuperCfTemplate sTemplate = new SuperCfTemplate(keyspace, "Super1", se, se, se); + SuperCfUpdater sUpdater = sTemplate.createUpdater("skey1","super1"); + sUpdater.setString("sub_col_1", "sub_val_1"); + sTemplate.update(sUpdater); + + SuperCfResult result = sTemplate.querySuperColumn("skey1", "super1"); + assertEquals("sub_val_1",result.getString("sub_col_1")); } - @Test - public void testSuperCfInsertRead() { - SuperCfTemplate sTemplate = - new SuperCfTemplate(keyspace, "Super1", se, se, se); - - } + } From fac6141c11c58bdbb7e92dddf87daf61a636754d Mon Sep 17 00:00:00 2001 From: zznate Date: Thu, 31 Mar 2011 19:02:20 -0500 Subject: [PATCH 079/144] cleaner approach to selection of supercf in templates --- .../template/ColumnFamilyResultWrapper.java | 4 +- .../template/SuperCfResultWrapper.java | 111 +++--------------- .../service/template/SuperCfTemplate.java | 65 +--------- .../template/ThriftSuperCfTemplate.java | 63 ++++++++++ .../service/template/SuperCfTemplateTest.java | 7 +- 5 files changed, 86 insertions(+), 164 deletions(-) create mode 100644 core/src/main/java/me/prettyprint/cassandra/service/template/ThriftSuperCfTemplate.java diff --git a/core/src/main/java/me/prettyprint/cassandra/service/template/ColumnFamilyResultWrapper.java b/core/src/main/java/me/prettyprint/cassandra/service/template/ColumnFamilyResultWrapper.java index f677d6aee..0b06ce860 100644 --- a/core/src/main/java/me/prettyprint/cassandra/service/template/ColumnFamilyResultWrapper.java +++ b/core/src/main/java/me/prettyprint/cassandra/service/template/ColumnFamilyResultWrapper.java @@ -49,7 +49,7 @@ private HColumn getColumn( N columnName ) { } - private void applyToRow(K key, List cosclist) { + private void applyToRow(List cosclist) { HColumn column; N colName; for (Iterator iterator = cosclist.iterator(); iterator.hasNext();) { @@ -79,7 +79,7 @@ public ColumnFamilyResult next() { throw new NoSuchElementException("No more rows left on this HColumnFamily"); } entry = rows.next(); - applyToRow(keySerializer.fromByteBuffer(entry.getKey()), entry.getValue()); + applyToRow(entry.getValue()); return this; } diff --git a/core/src/main/java/me/prettyprint/cassandra/service/template/SuperCfResultWrapper.java b/core/src/main/java/me/prettyprint/cassandra/service/template/SuperCfResultWrapper.java index ef4fb4f23..f4a093d6e 100644 --- a/core/src/main/java/me/prettyprint/cassandra/service/template/SuperCfResultWrapper.java +++ b/core/src/main/java/me/prettyprint/cassandra/service/template/SuperCfResultWrapper.java @@ -1,94 +1,34 @@ package me.prettyprint.cassandra.service.template; import java.nio.ByteBuffer; -import java.util.HashMap; -import java.util.Iterator; -import java.util.LinkedHashMap; import java.util.List; import java.util.Map; -import java.util.NoSuchElementException; - -import org.apache.cassandra.thrift.ColumnOrSuperColumn; import me.prettyprint.cassandra.model.ExecutionResult; -import me.prettyprint.cassandra.model.HColumnImpl; -import me.prettyprint.cassandra.model.HSuperColumnImpl; -import me.prettyprint.cassandra.serializers.ByteBufferSerializer; -import me.prettyprint.cassandra.service.CassandraHost; import me.prettyprint.hector.api.Serializer; -import me.prettyprint.hector.api.beans.HColumn; -import me.prettyprint.hector.api.beans.HSuperColumn; + +import org.apache.cassandra.thrift.ColumnOrSuperColumn; /** * Provides access to the current row of data during super column queries. * - * This class is a non-static inner class which inherits the Java generic parameters - * of it's containing CassandraTemplate instance. This allows it to inherit the - * and parameter and adds the type passed in from whichever - * query***() method was called. - * - * @author david - * @since Mar 10, 2011 + * @author zznate * @param the super column's sub column name type */ -public class SuperCfResultWrapper extends AbstractResultWrapper implements SuperCfResult { +public class SuperCfResultWrapper extends ColumnFamilyResultWrapper implements SuperCfResult { + + - private Map> columns = new LinkedHashMap>(); - private Iterator>> rows; - private Map.Entry> entry; - private ExecutionResult>> executionResult; - private Serializer subSerializer; - protected Serializer columnNameSerializer; + private Serializer sNameSerializer; + private SN sColumnName; public SuperCfResultWrapper(Serializer keySerializer, - Serializer topSerializer, + Serializer sNameSerializer, Serializer subSerializer, - ExecutionResult>> executionResult) { - super(keySerializer, null, executionResult); - this.columnNameSerializer = topSerializer; - this.subSerializer = subSerializer; - this.rows = executionResult.get().entrySet().iterator(); - next(); - } - - @Override - public SuperCfResult next() { - if ( !hasNext() ) { - throw new NoSuchElementException("No more rows left on this HColumnFamily"); - } - entry = rows.next(); - applyToRow(keySerializer.fromByteBuffer(entry.getKey()), entry.getValue()); - return this; - } - - @Override - public boolean hasNext() { - return rows.hasNext(); - } - - @Override - public void remove() { - rows.remove(); - } - - private void applyToRow(K key, List cosclist) { - HSuperColumn column; - SN colName; - for (Iterator iterator = cosclist.iterator(); iterator.hasNext();) { - ColumnOrSuperColumn cosc = iterator.next(); - - colName = columnNameSerializer.fromByteBuffer(cosc.getSuper_column().name.duplicate()); - column = new HSuperColumnImpl(cosc.getSuper_column(), columnNameSerializer, subSerializer, ByteBufferSerializer.get()); - // TODO cache columns - // TODO add clear() on HSuperColumnImpl - columns.put(colName, column); - iterator.remove(); - } - } - - @Override - public K getKey() { - return keySerializer.fromByteBuffer(entry.getKey()); + ExecutionResult>> executionResult, + SN sColumnName) { + super(keySerializer, subSerializer, executionResult); + this.sNameSerializer = sNameSerializer; } @@ -97,30 +37,7 @@ public K getKey() { * needed when the super columm name may be part of the object being mapped into. */ public SN getSuperName() { - return columnNameSerializer.fromByteBuffer(entry.getKey()); - } - - - private HSuperColumn getColumn( SN columnName ) { - return columns.get(columnName); + return sColumnName; } - - @Override - public long getExecutionTimeMicro() { - // TODO Auto-generated method stub - return 0; - } - - @Override - public CassandraHost getHostUsed() { - // TODO Auto-generated method stub - return null; - } - - @Override - public ByteBuffer getColumnValue(N columnName) { - // TODO Auto-generated method stub - return null; - } } diff --git a/core/src/main/java/me/prettyprint/cassandra/service/template/SuperCfTemplate.java b/core/src/main/java/me/prettyprint/cassandra/service/template/SuperCfTemplate.java index d8b77b445..3092ad580 100644 --- a/core/src/main/java/me/prettyprint/cassandra/service/template/SuperCfTemplate.java +++ b/core/src/main/java/me/prettyprint/cassandra/service/template/SuperCfTemplate.java @@ -1,44 +1,18 @@ package me.prettyprint.cassandra.service.template; -import java.nio.ByteBuffer; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.Iterator; -import java.util.LinkedHashMap; import java.util.List; -import java.util.Map; -import org.apache.cassandra.thrift.Cassandra; -import org.apache.cassandra.thrift.ColumnOrSuperColumn; -import org.apache.cassandra.thrift.ColumnParent; - -import me.prettyprint.cassandra.model.ExecutingKeyspace; -import me.prettyprint.cassandra.model.ExecutionResult; import me.prettyprint.cassandra.model.HSlicePredicate; -import me.prettyprint.cassandra.model.thrift.ThriftConverter; -import me.prettyprint.cassandra.serializers.ByteBufferSerializer; -import me.prettyprint.cassandra.serializers.SerializerTypeInferer; -import me.prettyprint.cassandra.service.Operation; -import me.prettyprint.cassandra.service.OperationType; import me.prettyprint.hector.api.Keyspace; import me.prettyprint.hector.api.Serializer; import me.prettyprint.hector.api.beans.HColumn; -import me.prettyprint.hector.api.beans.HSuperColumn; -import me.prettyprint.hector.api.beans.SuperRow; -import me.prettyprint.hector.api.beans.SuperRows; -import me.prettyprint.hector.api.beans.SuperSlice; -import me.prettyprint.hector.api.exceptions.HectorException; import me.prettyprint.hector.api.factory.HFactory; import me.prettyprint.hector.api.mutation.Mutator; -import me.prettyprint.hector.api.query.MultigetSuperSliceQuery; -import me.prettyprint.hector.api.query.QueryResult; -import me.prettyprint.hector.api.query.SubColumnQuery; import me.prettyprint.hector.api.query.SubCountQuery; import me.prettyprint.hector.api.query.SuperCountQuery; -import me.prettyprint.hector.api.query.SuperSliceQuery; -public class SuperCfTemplate extends AbstractColumnFamilyTemplate { - private Serializer subSerializer; +public abstract class SuperCfTemplate extends AbstractColumnFamilyTemplate { + protected Serializer subSerializer; public SuperCfTemplate(Keyspace keyspace, String columnFamily, Serializer keySerializer, Serializer topSerializer, @@ -192,40 +166,9 @@ public void update(SuperCfUpdater updater) { } public SuperCfResult querySuperColumn(K key, SN sColumnName) { - ColumnParent workingColumnParent = columnParent.deepCopy(); - workingColumnParent.setSuper_column(topSerializer.toByteBuffer(sColumnName)); - return doExecuteSlice(key, workingColumnParent, activeSlicePredicate); - } - - protected SuperCfResult doExecuteSlice(K key, ColumnParent workingColumnParent, HSlicePredicate predicate) { - SuperCfResultWrapper wrapper = - new SuperCfResultWrapper(keySerializer, topSerializer, subSerializer, - sliceInternal(key, workingColumnParent, predicate)); - return wrapper; - } - - private ExecutionResult>> sliceInternal(final K key, - final ColumnParent workingColumnParent, - final HSlicePredicate workingSlicePredicate) { - return ((ExecutingKeyspace)keyspace).doExecuteOperation(new Operation>>(OperationType.READ) { - @Override - public Map> execute(Cassandra.Client cassandra) throws HectorException { - Map> cosc = new LinkedHashMap>(); - try { - - ByteBuffer sKey = keySerializer.toByteBuffer(key); - cosc.put(sKey, cassandra.get_slice(sKey, workingColumnParent, - workingSlicePredicate.toThrift(), - ThriftConverter.consistencyLevel(consistencyLevelPolicy.get(operationType)))); - - } catch (Exception e) { - throw exceptionsTranslator.translate(e); - } - - return cosc; - } - }); + return doExecuteSlice(key, sColumnName, activeSlicePredicate); } + protected abstract SuperCfResult doExecuteSlice(K key, SN sColumnName, HSlicePredicate predicate); } diff --git a/core/src/main/java/me/prettyprint/cassandra/service/template/ThriftSuperCfTemplate.java b/core/src/main/java/me/prettyprint/cassandra/service/template/ThriftSuperCfTemplate.java new file mode 100644 index 000000000..9a3adda4b --- /dev/null +++ b/core/src/main/java/me/prettyprint/cassandra/service/template/ThriftSuperCfTemplate.java @@ -0,0 +1,63 @@ +package me.prettyprint.cassandra.service.template; + +import java.nio.ByteBuffer; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; + +import org.apache.cassandra.thrift.Cassandra; +import org.apache.cassandra.thrift.ColumnOrSuperColumn; +import org.apache.cassandra.thrift.ColumnParent; + +import me.prettyprint.cassandra.model.ExecutingKeyspace; +import me.prettyprint.cassandra.model.ExecutionResult; +import me.prettyprint.cassandra.model.HSlicePredicate; +import me.prettyprint.cassandra.model.thrift.ThriftConverter; +import me.prettyprint.cassandra.service.Operation; +import me.prettyprint.cassandra.service.OperationType; +import me.prettyprint.hector.api.Keyspace; +import me.prettyprint.hector.api.Serializer; +import me.prettyprint.hector.api.exceptions.HectorException; + +public class ThriftSuperCfTemplate extends SuperCfTemplate { + + public ThriftSuperCfTemplate(Keyspace keyspace, String columnFamily, + Serializer keySerializer, Serializer topSerializer, + Serializer subSerializer) { + super(keyspace, columnFamily, keySerializer, topSerializer, subSerializer); + } + + protected SuperCfResult doExecuteSlice(K key, SN sColumnName, HSlicePredicate predicate) { + ColumnParent workingColumnParent = columnParent.deepCopy(); + workingColumnParent.setSuper_column(topSerializer.toByteBuffer(sColumnName)); + SuperCfResultWrapper wrapper = + new SuperCfResultWrapper(keySerializer, topSerializer, subSerializer, + sliceInternal(key, workingColumnParent, predicate), + sColumnName); + return wrapper; + } + + private ExecutionResult>> sliceInternal(final K key, + final ColumnParent workingColumnParent, + final HSlicePredicate workingSlicePredicate) { + return ((ExecutingKeyspace)keyspace).doExecuteOperation(new Operation>>(OperationType.READ) { + @Override + public Map> execute(Cassandra.Client cassandra) throws HectorException { + Map> cosc = new LinkedHashMap>(); + try { + + ByteBuffer sKey = keySerializer.toByteBuffer(key); + cosc.put(sKey, cassandra.get_slice(sKey, workingColumnParent, + workingSlicePredicate.toThrift(), + ThriftConverter.consistencyLevel(consistencyLevelPolicy.get(operationType)))); + + } catch (Exception e) { + throw exceptionsTranslator.translate(e); + } + + return cosc; + } + }); + } + +} diff --git a/core/src/test/java/me/prettyprint/cassandra/service/template/SuperCfTemplateTest.java b/core/src/test/java/me/prettyprint/cassandra/service/template/SuperCfTemplateTest.java index 23eb5d368..4525eebcd 100644 --- a/core/src/test/java/me/prettyprint/cassandra/service/template/SuperCfTemplateTest.java +++ b/core/src/test/java/me/prettyprint/cassandra/service/template/SuperCfTemplateTest.java @@ -1,17 +1,16 @@ package me.prettyprint.cassandra.service.template; -import static org.junit.Assert.*; +import static org.junit.Assert.assertEquals; -import org.junit.Ignore; import org.junit.Test; -@Ignore + public class SuperCfTemplateTest extends BaseColumnFamilyTemplateTest { @Test public void testSuperCfInsertReadTemplate() { SuperCfTemplate sTemplate = - new SuperCfTemplate(keyspace, "Super1", se, se, se); + new ThriftSuperCfTemplate(keyspace, "Super1", se, se, se); SuperCfUpdater sUpdater = sTemplate.createUpdater("skey1","super1"); sUpdater.setString("sub_col_1", "sub_val_1"); sTemplate.update(sUpdater); From e1346edf872e0489c8160a117fe1f9ee32773202 Mon Sep 17 00:00:00 2001 From: Ed Anuff Date: Thu, 31 Mar 2011 17:12:09 -0700 Subject: [PATCH 080/144] Support for "tiebreaker" value to specify that individual component values in a composite should compare as less-than-if-equal or greater-than-if-equal --- .../hector/api/beans/AbstractComposite.java | 61 +++++++++++++------ 1 file changed, 43 insertions(+), 18 deletions(-) diff --git a/core/src/main/java/me/prettyprint/hector/api/beans/AbstractComposite.java b/core/src/main/java/me/prettyprint/hector/api/beans/AbstractComposite.java index 3ef1a8251..aff6f5e6d 100644 --- a/core/src/main/java/me/prettyprint/hector/api/beans/AbstractComposite.java +++ b/core/src/main/java/me/prettyprint/hector/api/beans/AbstractComposite.java @@ -47,6 +47,30 @@ public abstract class AbstractComposite extends AbstractList implements Comparable { + public enum ComponentEquality { + LESS_THAN_EQUAL((byte) -1), EQUAL((byte) 0), GREATER_THAN_EQUAL((byte) 1); + + private final byte equality; + + ComponentEquality(byte equality) { + this.equality = equality; + } + + public byte toByte() { + return equality; + } + + public static ComponentEquality fromByte(byte equality) { + if (equality > 0) { + return GREATER_THAN_EQUAL; + } + if (equality < 0) { + return LESS_THAN_EQUAL; + } + return EQUAL; + } + } + static final Logger logger = Logger.getLogger(AbstractComposite.class .getName()); @@ -89,15 +113,15 @@ public class Component { final T value; final ByteBuffer bytes; final String comparator; - final boolean inclusive; + final ComponentEquality equality; public Component(T value, ByteBuffer bytes, Serializer serializer, - String comparator, boolean inclusive) { + String comparator, ComponentEquality equality) { this.serializer = serializer; this.value = value; this.bytes = bytes; this.comparator = comparator; - this.inclusive = inclusive; + this.equality = equality; } public Serializer getSerializer() { @@ -148,8 +172,8 @@ public String getComparator() { return comparator; } - public boolean isInclusive() { - return inclusive; + public ComponentEquality getEquality() { + return equality; } } @@ -373,16 +397,16 @@ public AbstractComposite addComponent(T value, Serializer s) { public AbstractComposite addComponent(T value, Serializer s, String comparator) { - addComponent(value, s, comparator, false); + addComponent(value, s, comparator, ComponentEquality.EQUAL); return this; } public AbstractComposite addComponent(T value, Serializer s, - String comparator, boolean inclusive) { + String comparator, ComponentEquality equality) { - addComponent(-1, value, s, comparator, inclusive); + addComponent(-1, value, s, comparator, equality); return this; @@ -390,7 +414,7 @@ public AbstractComposite addComponent(T value, Serializer s, @SuppressWarnings("unchecked") public AbstractComposite addComponent(int index, T value, - Serializer s, String comparator, boolean inclusive) { + Serializer s, String comparator, ComponentEquality equality) { serialized = null; if (index < 0) { @@ -400,7 +424,7 @@ public AbstractComposite addComponent(int index, T value, while (components.size() < index) { components.add(null); } - components.add(index, new Component(value, null, s, comparator, inclusive)); + components.add(index, new Component(value, null, s, comparator, equality)); return this; @@ -419,7 +443,8 @@ public void add(int index, Object element) { c = element instanceof UUID ? comparatorForUUID((UUID) element) : comparatorForSerializer(s); } - components.add(index, new Component(element, null, s, c, false)); + components.add(index, new Component(element, null, s, c, + ComponentEquality.EQUAL)); } @Override @@ -445,7 +470,7 @@ public AbstractComposite setComponent(int index, T value, Serializer s) { public AbstractComposite setComponent(int index, T value, Serializer s, String comparator) { - setComponent(index, value, s, comparator, false); + setComponent(index, value, s, comparator, ComponentEquality.EQUAL); return this; @@ -453,13 +478,13 @@ public AbstractComposite setComponent(int index, T value, @SuppressWarnings("unchecked") public AbstractComposite setComponent(int index, T value, - Serializer s, String comparator, boolean inclusive) { + Serializer s, String comparator, ComponentEquality equality) { serialized = null; while (components.size() <= index) { components.add(null); } - components.set(index, new Component(value, null, s, comparator, inclusive)); + components.set(index, new Component(value, null, s, comparator, equality)); return this; @@ -479,7 +504,7 @@ public Object set(int index, Object element) { : comparatorForSerializer(s); } Component prev = components.set(index, new Component(element, null, s, c, - false)); + ComponentEquality.EQUAL)); if (prev != null) { return prev.getValue(); } @@ -559,7 +584,7 @@ public ByteBuffer serialize() { } out.writeShort((short) cb.remaining()); out.write(cb.slice()); - out.write(c.isInclusive() ? 1 : 0); + out.write(c.getEquality().toByte()); i++; } @@ -578,9 +603,9 @@ public void deserialize(ByteBuffer b) { ByteBuffer data = getWithShortLength(b); if (data != null) { Serializer s = getSerializer(i, comparator); - boolean inclusive = b.get() != 0; + ComponentEquality equality = ComponentEquality.fromByte(b.get()); components.add(new Component(null, data.slice(), s, comparator, - inclusive)); + equality)); } else { throw new RuntimeException("Missing component data in composite type"); } From 5316e503de37b461bf8222f2c7f95f25be0515ed Mon Sep 17 00:00:00 2001 From: zznate Date: Thu, 31 Mar 2011 19:30:41 -0500 Subject: [PATCH 081/144] added get for columnnames on result set --- .../service/template/ColumnFamilyResultWrapper.java | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/core/src/main/java/me/prettyprint/cassandra/service/template/ColumnFamilyResultWrapper.java b/core/src/main/java/me/prettyprint/cassandra/service/template/ColumnFamilyResultWrapper.java index 0b06ce860..c3326bc55 100644 --- a/core/src/main/java/me/prettyprint/cassandra/service/template/ColumnFamilyResultWrapper.java +++ b/core/src/main/java/me/prettyprint/cassandra/service/template/ColumnFamilyResultWrapper.java @@ -1,6 +1,7 @@ package me.prettyprint.cassandra.service.template; import java.nio.ByteBuffer; +import java.util.Collection; import java.util.HashMap; import java.util.Iterator; import java.util.LinkedHashMap; @@ -37,8 +38,15 @@ public ColumnFamilyResultWrapper(Serializer keySerializer, this.rows = executionResult.get().entrySet().iterator(); next(); } - - + + /** + * All the column names we know about in the current iterator position + * @return + */ + public Collection getColumnNames() { + return columns.keySet(); + } + public ByteBuffer getColumnValue( N columnName) { HColumn col = getColumn( columnName ); return col != null ? col.getValue() : null; From e0779b9cc5d9cead801c0a793a633350188efc7e Mon Sep 17 00:00:00 2001 From: Ed Anuff Date: Thu, 31 Mar 2011 17:52:37 -0700 Subject: [PATCH 082/144] Change to make reverse sort consistent with CASSANDRA-2355 patch --- .../me/prettyprint/hector/api/beans/AbstractComposite.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/core/src/main/java/me/prettyprint/hector/api/beans/AbstractComposite.java b/core/src/main/java/me/prettyprint/hector/api/beans/AbstractComposite.java index aff6f5e6d..a1c945571 100644 --- a/core/src/main/java/me/prettyprint/hector/api/beans/AbstractComposite.java +++ b/core/src/main/java/me/prettyprint/hector/api/beans/AbstractComposite.java @@ -359,7 +359,7 @@ private String getComparator(int i, ByteBuffer bb) { a = (byte) Character.toUpperCase((char) a); name = aliasToComparatorMapping.get(a); if (name != null) { - name += "(sort=desc)"; + name += "(reversed=true)"; } } } @@ -565,7 +565,7 @@ public ByteBuffer serialize() { if (comparator == null) { comparator = BYTESTYPE.getTypeName(); } - int p = comparator.indexOf("(sort=desc)"); + int p = comparator.indexOf("(reversed=true)"); boolean desc = false; if (p >= 0) { comparator = comparator.substring(0, p); From 4dc3c779879ed995b258b0b97202dfd5bd223c62 Mon Sep 17 00:00:00 2001 From: zznate Date: Thu, 31 Mar 2011 20:41:52 -0500 Subject: [PATCH 083/144] added method to iface for template --- .../cassandra/service/template/ColumnFamilyResult.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/core/src/main/java/me/prettyprint/cassandra/service/template/ColumnFamilyResult.java b/core/src/main/java/me/prettyprint/cassandra/service/template/ColumnFamilyResult.java index b21e35c6b..689845473 100644 --- a/core/src/main/java/me/prettyprint/cassandra/service/template/ColumnFamilyResult.java +++ b/core/src/main/java/me/prettyprint/cassandra/service/template/ColumnFamilyResult.java @@ -1,5 +1,6 @@ package me.prettyprint.cassandra.service.template; +import java.util.Collection; import java.util.Date; import java.util.Iterator; import java.util.UUID; @@ -34,4 +35,5 @@ public interface ColumnFamilyResult extends Iterator getColumnNames(); } From 9309e109f0d4a496406081e23d117b654ba7a50c Mon Sep 17 00:00:00 2001 From: zznate Date: Thu, 31 Mar 2011 23:39:06 -0500 Subject: [PATCH 084/144] added two new primitive type serializers --- .../cassandra/serializers/CharSerializer.java | 37 +++++++++++++++++++ .../serializers/FloatSerializer.java | 28 ++++++++++++++ 2 files changed, 65 insertions(+) create mode 100644 core/src/main/java/me/prettyprint/cassandra/serializers/CharSerializer.java create mode 100644 core/src/main/java/me/prettyprint/cassandra/serializers/FloatSerializer.java diff --git a/core/src/main/java/me/prettyprint/cassandra/serializers/CharSerializer.java b/core/src/main/java/me/prettyprint/cassandra/serializers/CharSerializer.java new file mode 100644 index 000000000..a18bfa7e6 --- /dev/null +++ b/core/src/main/java/me/prettyprint/cassandra/serializers/CharSerializer.java @@ -0,0 +1,37 @@ +package me.prettyprint.cassandra.serializers; + +import java.nio.ByteBuffer; + +/** + * Uses Char Serializer + * + * @author Todd Nine + */ +public class CharSerializer extends AbstractSerializer { + + private static final CharSerializer instance = new CharSerializer(); + + public static CharSerializer get() { + return instance; + } + + @Override + public ByteBuffer toByteBuffer(Character obj) { + ByteBuffer buffer = ByteBuffer.allocate(Character.SIZE / Byte.SIZE); + + buffer.putChar(obj); + buffer.rewind(); + + return buffer; + } + + @Override + public Character fromByteBuffer(ByteBuffer bytes) { + if (bytes == null) { + return null; + } + return bytes.getChar(); + + } + +} \ No newline at end of file diff --git a/core/src/main/java/me/prettyprint/cassandra/serializers/FloatSerializer.java b/core/src/main/java/me/prettyprint/cassandra/serializers/FloatSerializer.java new file mode 100644 index 000000000..95c768958 --- /dev/null +++ b/core/src/main/java/me/prettyprint/cassandra/serializers/FloatSerializer.java @@ -0,0 +1,28 @@ +package me.prettyprint.cassandra.serializers; + +import java.nio.ByteBuffer; + +/** + * Uses IntSerializer via translating Float objects to and from raw long bytes form. + * + * @author Todd Nine + */ +public class FloatSerializer extends AbstractSerializer { + + private static final FloatSerializer instance = new FloatSerializer(); + + public static FloatSerializer get() { + return instance; + } + + @Override + public ByteBuffer toByteBuffer(Float obj) { + return IntegerSerializer.get().toByteBuffer(Float.floatToRawIntBits(obj)); + } + + @Override + public Float fromByteBuffer(ByteBuffer bytes) { + return Float.intBitsToFloat(IntegerSerializer.get().fromByteBuffer(bytes)); + } + +} From 26641ce26af5c5a48c8a66c0bdc161b0f52e6293 Mon Sep 17 00:00:00 2001 From: Ed Anuff Date: Fri, 1 Apr 2011 19:39:47 -0700 Subject: [PATCH 085/144] Fixed mapping of numbers into BigInteger for proper comparison by Cassandra IntegerType --- .../serializers/SerializerTypeInferer.java | 10 +++++++--- .../hector/api/beans/AbstractComposite.java | 10 ++++++++++ .../hector/api/beans/Composite.java | 6 ++++++ .../hector/api/beans/DynamicComposite.java | 5 +++++ .../prettyprint/hector/api/CompositeTest.java | 20 +++++++++++++++++++ 5 files changed, 48 insertions(+), 3 deletions(-) diff --git a/core/src/main/java/me/prettyprint/cassandra/serializers/SerializerTypeInferer.java b/core/src/main/java/me/prettyprint/cassandra/serializers/SerializerTypeInferer.java index 88cad9fcc..b2ab8e078 100644 --- a/core/src/main/java/me/prettyprint/cassandra/serializers/SerializerTypeInferer.java +++ b/core/src/main/java/me/prettyprint/cassandra/serializers/SerializerTypeInferer.java @@ -1,5 +1,6 @@ package me.prettyprint.cassandra.serializers; +import java.math.BigInteger; import java.util.Date; import java.util.UUID; @@ -8,9 +9,9 @@ /** * Utility class that infers the concrete Serializer needed to turn a value into * its binary representation - * + * * @author Bozhidar Bozhanov - * + * */ public class SerializerTypeInferer { @@ -27,6 +28,8 @@ public static Serializer getSerializer(Object value) { serializer = LongSerializer.get(); } else if (value instanceof Integer) { serializer = IntegerSerializer.get(); + } else if (value instanceof BigInteger) { + serializer = BigIntegerSerializer.get(); } else if (value instanceof Boolean) { serializer = BooleanSerializer.get(); } else if (value instanceof byte[]) { @@ -52,7 +55,8 @@ public static Serializer getSerializer(Class valueClass) { serializer = LongSerializer.get(); } else if (valueClass.equals(Integer.class) || valueClass.equals(int.class)) { serializer = IntegerSerializer.get(); - } else if (valueClass.equals(Boolean.class) || valueClass.equals(boolean.class)) { + } else if (valueClass.equals(Boolean.class) + || valueClass.equals(boolean.class)) { serializer = BooleanSerializer.get(); } else if (valueClass.equals(byte[].class)) { serializer = ByteBufferSerializer.get(); diff --git a/core/src/main/java/me/prettyprint/hector/api/beans/AbstractComposite.java b/core/src/main/java/me/prettyprint/hector/api/beans/AbstractComposite.java index a1c945571..7927bc801 100644 --- a/core/src/main/java/me/prettyprint/hector/api/beans/AbstractComposite.java +++ b/core/src/main/java/me/prettyprint/hector/api/beans/AbstractComposite.java @@ -8,6 +8,7 @@ import static me.prettyprint.hector.api.ddl.ComparatorType.TIMEUUIDTYPE; import static me.prettyprint.hector.api.ddl.ComparatorType.UTF8TYPE; +import java.math.BigInteger; import java.nio.ByteBuffer; import java.nio.charset.CharacterCodingException; import java.util.AbstractList; @@ -430,10 +431,18 @@ public AbstractComposite addComponent(int index, T value, } + public static Object mapIfNumber(Object o) { + if ((o instanceof Byte) || (o instanceof Integer) || (o instanceof Short)) { + return BigInteger.valueOf(((Number) o).longValue()); + } + return o; + } + @SuppressWarnings("unchecked") @Override public void add(int index, Object element) { serialized = null; + element = mapIfNumber(element); Serializer s = serializerForPosition(index); if (s == null) { s = SerializerTypeInferer.getSerializer(element); @@ -494,6 +503,7 @@ public AbstractComposite setComponent(int index, T value, @Override public Object set(int index, Object element) { serialized = null; + element = mapIfNumber(element); Serializer s = serializerForPosition(index); if (s == null) { s = SerializerTypeInferer.getSerializer(element); diff --git a/core/src/main/java/me/prettyprint/hector/api/beans/Composite.java b/core/src/main/java/me/prettyprint/hector/api/beans/Composite.java index 736ae564d..e63bde883 100644 --- a/core/src/main/java/me/prettyprint/hector/api/beans/Composite.java +++ b/core/src/main/java/me/prettyprint/hector/api/beans/Composite.java @@ -19,4 +19,10 @@ public static Composite fromByteBuffer(ByteBuffer byteBuffer) { return composite; } + + public static ByteBuffer toByteBuffer(Object... o) { + Composite composite = new Composite(o); + return composite.serialize(); + } + } diff --git a/core/src/main/java/me/prettyprint/hector/api/beans/DynamicComposite.java b/core/src/main/java/me/prettyprint/hector/api/beans/DynamicComposite.java index f016fe521..3b4019691 100644 --- a/core/src/main/java/me/prettyprint/hector/api/beans/DynamicComposite.java +++ b/core/src/main/java/me/prettyprint/hector/api/beans/DynamicComposite.java @@ -19,4 +19,9 @@ public static DynamicComposite fromByteBuffer(ByteBuffer byteBuffer) { return composite; } + + public static ByteBuffer toByteBuffer(Object... o) { + DynamicComposite composite = new DynamicComposite(o); + return composite.serialize(); + } } diff --git a/core/src/test/java/me/prettyprint/hector/api/CompositeTest.java b/core/src/test/java/me/prettyprint/hector/api/CompositeTest.java index ebedb3973..3f8ef0e73 100644 --- a/core/src/test/java/me/prettyprint/hector/api/CompositeTest.java +++ b/core/src/test/java/me/prettyprint/hector/api/CompositeTest.java @@ -105,6 +105,26 @@ public void testDynamicSerialization() throws Exception { assertTrue(c.get(1) instanceof UUID); assertTrue(c.get(2) instanceof ByteBuffer); + b = DynamicComposite.toByteBuffer(1, "string", + TimeUUIDUtils.getUniqueTimeUUIDinMillis()); + c = DynamicComposite.fromByteBuffer(b); + assertTrue(c.get(0) instanceof BigInteger); + assertTrue(c.get(1) instanceof String); + assertTrue(c.get(2) instanceof UUID); + + b = DynamicComposite.toByteBuffer((long) 1, "string", + TimeUUIDUtils.getUniqueTimeUUIDinMillis()); + c = DynamicComposite.fromByteBuffer(b); + assertTrue(c.get(0) instanceof Long); + assertTrue(c.get(1) instanceof String); + assertTrue(c.get(2) instanceof UUID); + + b = DynamicComposite.toByteBuffer((byte) 1, "string", UUID.randomUUID()); + c = DynamicComposite.fromByteBuffer(b); + assertTrue(c.get(0) instanceof BigInteger); + assertTrue(c.get(1) instanceof String); + assertTrue(c.get(2) instanceof UUID); + } @Test From 74c25837358950ba834a0223bb97946abeb00d02 Mon Sep 17 00:00:00 2001 From: Ed Anuff Date: Fri, 1 Apr 2011 20:13:13 -0700 Subject: [PATCH 086/144] Added getComparatorType() to serializers to return Cassandra comparator type which is compatible with serializer format --- .../serializers/AbstractSerializer.java | 7 ++++++ .../serializers/AsciiSerializer.java | 24 ++++++++++++------- .../serializers/BigIntegerSerializer.java | 20 +++++++++++----- .../serializers/CompositeSerializer.java | 8 +++++++ .../DynamicCompositeSerializer.java | 8 +++++++ .../cassandra/serializers/LongSerializer.java | 11 ++++++++- .../serializers/StringSerializer.java | 19 +++++++++++---- .../cassandra/serializers/UUIDSerializer.java | 16 +++++++++++-- .../hector/api/beans/AbstractComposite.java | 16 ++++++++----- .../hector/api/ddl/ComparatorType.java | 11 +++++++-- 10 files changed, 110 insertions(+), 30 deletions(-) diff --git a/core/src/main/java/me/prettyprint/cassandra/serializers/AbstractSerializer.java b/core/src/main/java/me/prettyprint/cassandra/serializers/AbstractSerializer.java index 49aa23114..412c2705b 100644 --- a/core/src/main/java/me/prettyprint/cassandra/serializers/AbstractSerializer.java +++ b/core/src/main/java/me/prettyprint/cassandra/serializers/AbstractSerializer.java @@ -1,5 +1,7 @@ package me.prettyprint.cassandra.serializers; +import static me.prettyprint.hector.api.ddl.ComparatorType.BYTESTYPE; + import java.nio.ByteBuffer; import java.util.ArrayList; import java.util.HashSet; @@ -10,6 +12,7 @@ import java.util.Set; import me.prettyprint.hector.api.Serializer; +import me.prettyprint.hector.api.ddl.ComparatorType; /** * A base class for serializer implementations. Takes care of the default @@ -118,4 +121,8 @@ public Map fromBytesMap(Map map) { public int computeInitialHashSize(int initialSize) { return Double.valueOf(Math.floor(initialSize / 0.75)).intValue() + 1; } + + public ComparatorType getComparatorType() { + return BYTESTYPE; + } } diff --git a/core/src/main/java/me/prettyprint/cassandra/serializers/AsciiSerializer.java b/core/src/main/java/me/prettyprint/cassandra/serializers/AsciiSerializer.java index e8fdb8ed9..21e8eb4aa 100644 --- a/core/src/main/java/me/prettyprint/cassandra/serializers/AsciiSerializer.java +++ b/core/src/main/java/me/prettyprint/cassandra/serializers/AsciiSerializer.java @@ -1,11 +1,15 @@ package me.prettyprint.cassandra.serializers; +import static me.prettyprint.hector.api.ddl.ComparatorType.ASCIITYPE; + import java.nio.ByteBuffer; import java.nio.charset.Charset; +import me.prettyprint.hector.api.ddl.ComparatorType; + /** - * Almost identical to StringSerializer except we use the US-ASCII - * character set code + * Almost identical to StringSerializer except we use the US-ASCII character set + * code * * @author zznate */ @@ -18,22 +22,26 @@ public final class AsciiSerializer extends AbstractSerializer { public static AsciiSerializer get() { return instance; } - + @Override public String fromByteBuffer(ByteBuffer byteBuffer) { if (byteBuffer == null) { return null; - } - return charset.decode(byteBuffer).toString(); + } + return charset.decode(byteBuffer).toString(); } @Override public ByteBuffer toByteBuffer(String obj) { if (obj == null) { return null; - } - return ByteBuffer.wrap(obj.getBytes(charset)); + } + return ByteBuffer.wrap(obj.getBytes(charset)); + } + + @Override + public ComparatorType getComparatorType() { + return ASCIITYPE; } - } diff --git a/core/src/main/java/me/prettyprint/cassandra/serializers/BigIntegerSerializer.java b/core/src/main/java/me/prettyprint/cassandra/serializers/BigIntegerSerializer.java index df85714af..f2602d234 100644 --- a/core/src/main/java/me/prettyprint/cassandra/serializers/BigIntegerSerializer.java +++ b/core/src/main/java/me/prettyprint/cassandra/serializers/BigIntegerSerializer.java @@ -1,8 +1,12 @@ package me.prettyprint.cassandra.serializers; +import static me.prettyprint.hector.api.ddl.ComparatorType.INTEGERTYPE; + import java.math.BigInteger; import java.nio.ByteBuffer; +import me.prettyprint.hector.api.ddl.ComparatorType; + /** * Serializer implementation for BigInteger * @@ -11,14 +15,14 @@ public final class BigIntegerSerializer extends AbstractSerializer { private static final BigIntegerSerializer INSTANCE = new BigIntegerSerializer(); - + public static BigIntegerSerializer get() { return INSTANCE; } - + @Override public BigInteger fromByteBuffer(ByteBuffer byteBuffer) { - if ( byteBuffer == null ) { + if (byteBuffer == null) { return null; } int length = byteBuffer.remaining(); @@ -29,11 +33,15 @@ public BigInteger fromByteBuffer(ByteBuffer byteBuffer) { @Override public ByteBuffer toByteBuffer(BigInteger obj) { - if ( obj == null ) { + if (obj == null) { return null; } - return ByteBuffer.wrap(obj.toByteArray()); + return ByteBuffer.wrap(obj.toByteArray()); + } + + @Override + public ComparatorType getComparatorType() { + return INTEGERTYPE; } - } diff --git a/core/src/main/java/me/prettyprint/cassandra/serializers/CompositeSerializer.java b/core/src/main/java/me/prettyprint/cassandra/serializers/CompositeSerializer.java index c5242f21d..9065c8408 100644 --- a/core/src/main/java/me/prettyprint/cassandra/serializers/CompositeSerializer.java +++ b/core/src/main/java/me/prettyprint/cassandra/serializers/CompositeSerializer.java @@ -3,9 +3,12 @@ */ package me.prettyprint.cassandra.serializers; +import static me.prettyprint.hector.api.ddl.ComparatorType.COMPOSITETYPE; + import java.nio.ByteBuffer; import me.prettyprint.hector.api.beans.Composite; +import me.prettyprint.hector.api.ddl.ComparatorType; /** * @author Todd Nine @@ -29,4 +32,9 @@ public Composite fromByteBuffer(ByteBuffer byteBuffer) { } + @Override + public ComparatorType getComparatorType() { + return COMPOSITETYPE; + } + } diff --git a/core/src/main/java/me/prettyprint/cassandra/serializers/DynamicCompositeSerializer.java b/core/src/main/java/me/prettyprint/cassandra/serializers/DynamicCompositeSerializer.java index 26f6e6e1d..93c4be43d 100644 --- a/core/src/main/java/me/prettyprint/cassandra/serializers/DynamicCompositeSerializer.java +++ b/core/src/main/java/me/prettyprint/cassandra/serializers/DynamicCompositeSerializer.java @@ -3,9 +3,12 @@ */ package me.prettyprint.cassandra.serializers; +import static me.prettyprint.hector.api.ddl.ComparatorType.DYNAMICCOMPOSITETYPE; + import java.nio.ByteBuffer; import me.prettyprint.hector.api.beans.DynamicComposite; +import me.prettyprint.hector.api.ddl.ComparatorType; /** * @author Todd Nine @@ -27,4 +30,9 @@ public DynamicComposite fromByteBuffer(ByteBuffer byteBuffer) { } + @Override + public ComparatorType getComparatorType() { + return DYNAMICCOMPOSITETYPE; + } + } diff --git a/core/src/main/java/me/prettyprint/cassandra/serializers/LongSerializer.java b/core/src/main/java/me/prettyprint/cassandra/serializers/LongSerializer.java index 4c6686cfd..2bb23b969 100644 --- a/core/src/main/java/me/prettyprint/cassandra/serializers/LongSerializer.java +++ b/core/src/main/java/me/prettyprint/cassandra/serializers/LongSerializer.java @@ -1,7 +1,11 @@ package me.prettyprint.cassandra.serializers; +import static me.prettyprint.hector.api.ddl.ComparatorType.LONGTYPE; + import java.nio.ByteBuffer; +import me.prettyprint.hector.api.ddl.ComparatorType; + /** * Converts bytes to Long and vise a versa * @@ -21,7 +25,7 @@ public ByteBuffer toByteBuffer(Long obj) { if (obj == null) { return null; } - return ByteBuffer.allocate(8).putLong(0, obj); + return ByteBuffer.allocate(8).putLong(0, obj); } @Override @@ -33,4 +37,9 @@ public Long fromByteBuffer(ByteBuffer byteBuffer) { return l; } + @Override + public ComparatorType getComparatorType() { + return LONGTYPE; + } + } diff --git a/core/src/main/java/me/prettyprint/cassandra/serializers/StringSerializer.java b/core/src/main/java/me/prettyprint/cassandra/serializers/StringSerializer.java index 22e4786f8..9e84c0eb4 100644 --- a/core/src/main/java/me/prettyprint/cassandra/serializers/StringSerializer.java +++ b/core/src/main/java/me/prettyprint/cassandra/serializers/StringSerializer.java @@ -1,9 +1,12 @@ package me.prettyprint.cassandra.serializers; -import java.io.UnsupportedEncodingException; +import static me.prettyprint.hector.api.ddl.ComparatorType.UTF8TYPE; + import java.nio.ByteBuffer; import java.nio.charset.Charset; +import me.prettyprint.hector.api.ddl.ComparatorType; + /** * A StringSerializer translates the byte[] to and from string using utf-8 * encoding. @@ -25,15 +28,21 @@ public static StringSerializer get() { public ByteBuffer toByteBuffer(String obj) { if (obj == null) { return null; - } - return ByteBuffer.wrap(obj.getBytes(charset)); + } + return ByteBuffer.wrap(obj.getBytes(charset)); } @Override public String fromByteBuffer(ByteBuffer byteBuffer) { if (byteBuffer == null) { return null; - } - return charset.decode(byteBuffer).toString(); + } + return charset.decode(byteBuffer).toString(); + } + + @Override + public ComparatorType getComparatorType() { + return UTF8TYPE; } + } diff --git a/core/src/main/java/me/prettyprint/cassandra/serializers/UUIDSerializer.java b/core/src/main/java/me/prettyprint/cassandra/serializers/UUIDSerializer.java index f4309f867..198c57db5 100644 --- a/core/src/main/java/me/prettyprint/cassandra/serializers/UUIDSerializer.java +++ b/core/src/main/java/me/prettyprint/cassandra/serializers/UUIDSerializer.java @@ -1,12 +1,17 @@ package me.prettyprint.cassandra.serializers; +import static me.prettyprint.hector.api.ddl.ComparatorType.LEXICALUUIDTYPE; + import java.nio.ByteBuffer; import java.util.UUID; +import me.prettyprint.hector.api.ddl.ComparatorType; + /** * A UUIDSerializer translates the byte[] to and from UUID types. + * * @author Ed Anuff - * + * */ public final class UUIDSerializer extends AbstractSerializer { @@ -16,6 +21,7 @@ public static UUIDSerializer get() { return instance; } + @Override public ByteBuffer toByteBuffer(UUID uuid) { if (uuid == null) { return null; @@ -34,11 +40,17 @@ public ByteBuffer toByteBuffer(UUID uuid) { return ByteBuffer.wrap(buffer); } + @Override public UUID fromByteBuffer(ByteBuffer bytes) { if (bytes == null) { return null; - } + } return new UUID(bytes.getLong(), bytes.getLong()); } + @Override + public ComparatorType getComparatorType() { + return LEXICALUUIDTYPE; + } + } diff --git a/core/src/main/java/me/prettyprint/hector/api/beans/AbstractComposite.java b/core/src/main/java/me/prettyprint/hector/api/beans/AbstractComposite.java index 7927bc801..fd4c6b1c8 100644 --- a/core/src/main/java/me/prettyprint/hector/api/beans/AbstractComposite.java +++ b/core/src/main/java/me/prettyprint/hector/api/beans/AbstractComposite.java @@ -76,12 +76,16 @@ public static ComponentEquality fromByte(byte equality) { .getName()); public static final BiMap, String> DEFAULT_SERIALIZER_TO_COMPARATOR_MAPPING = new ImmutableBiMap.Builder, String>() - .put(AsciiSerializer.class, ASCIITYPE.getTypeName()) - .put(BigIntegerSerializer.class, INTEGERTYPE.getTypeName()) - .put(ByteBufferSerializer.class, BYTESTYPE.getTypeName()) - .put(LongSerializer.class, LONGTYPE.getTypeName()) - .put(StringSerializer.class, UTF8TYPE.getTypeName()) - .put(UUIDSerializer.class, "UUIDType").build(); + .put(AsciiSerializer.class, + AsciiSerializer.get().getComparatorType().getTypeName()) + .put(BigIntegerSerializer.class, + BigIntegerSerializer.get().getComparatorType().getTypeName()) + .put(LongSerializer.class, + LongSerializer.get().getComparatorType().getTypeName()) + .put(StringSerializer.class, + StringSerializer.get().getComparatorType().getTypeName()) + .put(UUIDSerializer.class, + UUIDSerializer.get().getComparatorType().getTypeName()).build(); static final ImmutableClassToInstanceMap SERIALIZERS = new ImmutableClassToInstanceMap.Builder() .put(AsciiSerializer.class, AsciiSerializer.get()) diff --git a/core/src/main/java/me/prettyprint/hector/api/ddl/ComparatorType.java b/core/src/main/java/me/prettyprint/hector/api/ddl/ComparatorType.java index 6db969717..fac4ef6da 100644 --- a/core/src/main/java/me/prettyprint/hector/api/ddl/ComparatorType.java +++ b/core/src/main/java/me/prettyprint/hector/api/ddl/ComparatorType.java @@ -21,9 +21,14 @@ public class ComparatorType { "org.apache.cassandra.db.marshal.TimeUUIDType"); public static ComparatorType UTF8TYPE = new ComparatorType( "org.apache.cassandra.db.marshal.UTF8Type"); + public static ComparatorType COMPOSITETYPE = new ComparatorType( + "org.apache.cassandra.db.marshal.CompositeType"); + public static ComparatorType DYNAMICCOMPOSITETYPE = new ComparatorType( + "org.apache.cassandra.db.marshal.DynamicCompositeType"); private static ComparatorType[] values = { ASCIITYPE, BYTESTYPE, INTEGERTYPE, - LEXICALUUIDTYPE, LOCALBYPARTITIONERTYPE, LONGTYPE, TIMEUUIDTYPE, UTF8TYPE }; + LEXICALUUIDTYPE, LOCALBYPARTITIONERTYPE, LONGTYPE, TIMEUUIDTYPE, + UTF8TYPE, COMPOSITETYPE, DYNAMICCOMPOSITETYPE }; private final String className; private final String typeName; @@ -47,7 +52,9 @@ public String getTypeName() { } public static ComparatorType getByClassName(String className) { - if ( className == null ) return null; + if (className == null) { + return null; + } for (int a = 0; a < values.length; a++) { ComparatorType type = values[a]; if (type.getClassName().equals(className)) { From 0319d6276db2aadcb0bb36ffd88c36028fb0a29d Mon Sep 17 00:00:00 2001 From: Ed Anuff Date: Sat, 2 Apr 2011 21:56:11 -0700 Subject: [PATCH 087/144] convenience methods for creating composite from list --- .../hector/api/beans/AbstractComposite.java | 5 +++++ .../me/prettyprint/hector/api/beans/Composite.java | 10 ++++++++++ .../prettyprint/hector/api/beans/DynamicComposite.java | 10 ++++++++++ 3 files changed, 25 insertions(+) diff --git a/core/src/main/java/me/prettyprint/hector/api/beans/AbstractComposite.java b/core/src/main/java/me/prettyprint/hector/api/beans/AbstractComposite.java index fd4c6b1c8..103b40c12 100644 --- a/core/src/main/java/me/prettyprint/hector/api/beans/AbstractComposite.java +++ b/core/src/main/java/me/prettyprint/hector/api/beans/AbstractComposite.java @@ -195,6 +195,11 @@ public AbstractComposite(boolean dynamic, Object... o) { this.addAll(Arrays.asList(o)); } + public AbstractComposite(boolean dynamic, List l) { + this.dynamic = dynamic; + this.addAll(l); + } + public List> getComponents() { return components; } diff --git a/core/src/main/java/me/prettyprint/hector/api/beans/Composite.java b/core/src/main/java/me/prettyprint/hector/api/beans/Composite.java index e63bde883..8b7e089e4 100644 --- a/core/src/main/java/me/prettyprint/hector/api/beans/Composite.java +++ b/core/src/main/java/me/prettyprint/hector/api/beans/Composite.java @@ -1,6 +1,7 @@ package me.prettyprint.hector.api.beans; import java.nio.ByteBuffer; +import java.util.List; public class Composite extends AbstractComposite { @@ -12,6 +13,10 @@ public Composite(Object... o) { super(false, o); } + public Composite(List l) { + super(false, l); + } + public static Composite fromByteBuffer(ByteBuffer byteBuffer) { Composite composite = new Composite(); @@ -25,4 +30,9 @@ public static ByteBuffer toByteBuffer(Object... o) { return composite.serialize(); } + public static ByteBuffer toByteBuffer(List l) { + Composite composite = new Composite(l); + return composite.serialize(); + } + } diff --git a/core/src/main/java/me/prettyprint/hector/api/beans/DynamicComposite.java b/core/src/main/java/me/prettyprint/hector/api/beans/DynamicComposite.java index 3b4019691..2d06b77e5 100644 --- a/core/src/main/java/me/prettyprint/hector/api/beans/DynamicComposite.java +++ b/core/src/main/java/me/prettyprint/hector/api/beans/DynamicComposite.java @@ -1,6 +1,7 @@ package me.prettyprint.hector.api.beans; import java.nio.ByteBuffer; +import java.util.List; public class DynamicComposite extends AbstractComposite { @@ -12,6 +13,10 @@ public DynamicComposite(Object... o) { super(true, o); } + public DynamicComposite(List l) { + super(true, l); + } + public static DynamicComposite fromByteBuffer(ByteBuffer byteBuffer) { DynamicComposite composite = new DynamicComposite(); @@ -24,4 +29,9 @@ public static ByteBuffer toByteBuffer(Object... o) { DynamicComposite composite = new DynamicComposite(o); return composite.serialize(); } + + public static ByteBuffer toByteBuffer(List l) { + DynamicComposite composite = new DynamicComposite(l); + return composite.serialize(); + } } From 5db79d3033d2f3757db1eddfe3f01c805ef160a7 Mon Sep 17 00:00:00 2001 From: Ed Anuff Date: Sun, 3 Apr 2011 17:51:31 -0700 Subject: [PATCH 088/144] Made AbstractComposite flatten collections passed to addAll, etc. to be compatible with behavior of original Composite class --- .../hector/api/beans/AbstractComposite.java | 51 ++++++++++++++++++- .../prettyprint/hector/api/CompositeTest.java | 10 ++++ 2 files changed, 60 insertions(+), 1 deletion(-) diff --git a/core/src/main/java/me/prettyprint/hector/api/beans/AbstractComposite.java b/core/src/main/java/me/prettyprint/hector/api/beans/AbstractComposite.java index 103b40c12..70e3049ef 100644 --- a/core/src/main/java/me/prettyprint/hector/api/beans/AbstractComposite.java +++ b/core/src/main/java/me/prettyprint/hector/api/beans/AbstractComposite.java @@ -14,6 +14,7 @@ import java.util.AbstractList; import java.util.ArrayList; import java.util.Arrays; +import java.util.Collection; import java.util.Iterator; import java.util.List; import java.util.Map; @@ -440,13 +441,61 @@ public AbstractComposite addComponent(int index, T value, } - public static Object mapIfNumber(Object o) { + private static Object mapIfNumber(Object o) { if ((o instanceof Byte) || (o instanceof Integer) || (o instanceof Short)) { return BigInteger.valueOf(((Number) o).longValue()); } return o; } + @SuppressWarnings({ "unchecked" }) + private static Collection flatten(Collection c) { + boolean hasCollection = false; + for (Object o : c) { + if (o instanceof Collection) { + hasCollection = true; + break; + } + } + if (!hasCollection) { + return c; + } + List newList = new ArrayList(); + for (Object o : c) { + if (o instanceof Collection) { + newList.addAll(flatten((Collection) o)); + } else { + newList.add(o); + } + } + return newList; + } + + @Override + public boolean addAll(Collection c) { + return super.addAll(flatten(c)); + } + + @Override + public boolean containsAll(Collection c) { + return super.containsAll(flatten(c)); + } + + @Override + public boolean removeAll(Collection c) { + return super.removeAll(flatten(c)); + } + + @Override + public boolean retainAll(Collection c) { + return super.retainAll(flatten(c)); + } + + @Override + public boolean addAll(int i, Collection c) { + return super.addAll(i, flatten(c)); + } + @SuppressWarnings("unchecked") @Override public void add(int index, Object element) { diff --git a/core/src/test/java/me/prettyprint/hector/api/CompositeTest.java b/core/src/test/java/me/prettyprint/hector/api/CompositeTest.java index 3f8ef0e73..84032f24f 100644 --- a/core/src/test/java/me/prettyprint/hector/api/CompositeTest.java +++ b/core/src/test/java/me/prettyprint/hector/api/CompositeTest.java @@ -7,6 +7,7 @@ import java.math.BigInteger; import java.nio.ByteBuffer; +import java.util.Arrays; import java.util.UUID; import me.prettyprint.cassandra.serializers.BigIntegerSerializer; @@ -125,6 +126,15 @@ public void testDynamicSerialization() throws Exception { assertTrue(c.get(1) instanceof String); assertTrue(c.get(2) instanceof UUID); + b = DynamicComposite.toByteBuffer(Arrays.asList(Arrays.asList(0, 1, 2), 3, + 4, 5, Arrays.asList(6, 7, 8))); + c = DynamicComposite.fromByteBuffer(b); + for (int i = 0; i < 9; i++) { + o = c.get(i); + assertTrue(o instanceof BigInteger); + assertEquals(i, ((BigInteger) o).intValue()); + } + } @Test From 7f37a03f667243074fd6e498d1d921b9123fdbd2 Mon Sep 17 00:00:00 2001 From: Ed Anuff Date: Sun, 3 Apr 2011 20:08:55 -0700 Subject: [PATCH 089/144] In AbstractComposite, do the correct thing with add() or set() is called with a Component object --- .../hector/api/beans/AbstractComposite.java | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/core/src/main/java/me/prettyprint/hector/api/beans/AbstractComposite.java b/core/src/main/java/me/prettyprint/hector/api/beans/AbstractComposite.java index 70e3049ef..8ad6a7ce1 100644 --- a/core/src/main/java/me/prettyprint/hector/api/beans/AbstractComposite.java +++ b/core/src/main/java/me/prettyprint/hector/api/beans/AbstractComposite.java @@ -500,6 +500,12 @@ public boolean addAll(int i, Collection c) { @Override public void add(int index, Object element) { serialized = null; + + if (element instanceof Component) { + components.add((Component) element); + return; + } + element = mapIfNumber(element); Serializer s = serializerForPosition(index); if (s == null) { @@ -561,6 +567,15 @@ public AbstractComposite setComponent(int index, T value, @Override public Object set(int index, Object element) { serialized = null; + + if (element instanceof Component) { + Component prev = components.set(index, (Component) element); + if (prev != null) { + return prev.getValue(); + } + return null; + } + element = mapIfNumber(element); Serializer s = serializerForPosition(index); if (s == null) { From a4a896d30f91a0e48cd193d01855904cce28f63f Mon Sep 17 00:00:00 2001 From: Ed Anuff Date: Sun, 3 Apr 2011 20:18:15 -0700 Subject: [PATCH 090/144] minor fix in AbstractComposite.add() --- .../java/me/prettyprint/hector/api/beans/AbstractComposite.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/src/main/java/me/prettyprint/hector/api/beans/AbstractComposite.java b/core/src/main/java/me/prettyprint/hector/api/beans/AbstractComposite.java index 8ad6a7ce1..ce04e8182 100644 --- a/core/src/main/java/me/prettyprint/hector/api/beans/AbstractComposite.java +++ b/core/src/main/java/me/prettyprint/hector/api/beans/AbstractComposite.java @@ -502,7 +502,7 @@ public void add(int index, Object element) { serialized = null; if (element instanceof Component) { - components.add((Component) element); + components.add(index, (Component) element); return; } From 3f20ed476db346abc74b44cd5fb6ced5ed342808 Mon Sep 17 00:00:00 2001 From: zznate Date: Mon, 4 Apr 2011 10:25:58 -0500 Subject: [PATCH 091/144] changes for smoother supercol integration w. other protocols --- .../java/me/prettyprint/cassandra/model/HSuperColumnImpl.java | 4 ++-- .../cassandra/service/template/SuperCfUpdater.java | 4 +++- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/core/src/main/java/me/prettyprint/cassandra/model/HSuperColumnImpl.java b/core/src/main/java/me/prettyprint/cassandra/model/HSuperColumnImpl.java index c4e1d47e6..ca9a25414 100644 --- a/core/src/main/java/me/prettyprint/cassandra/model/HSuperColumnImpl.java +++ b/core/src/main/java/me/prettyprint/cassandra/model/HSuperColumnImpl.java @@ -55,14 +55,14 @@ public HSuperColumnImpl(SN sName, List> columns, long clock, public HSuperColumnImpl(SuperColumn thriftSuperColumn, Serializer sNameSerializer, Serializer nameSerializer, Serializer valueSerializer) { this(sNameSerializer, nameSerializer, valueSerializer); - noneNull(thriftSuperColumn, sNameSerializer, nameSerializer, valueSerializer); + noneNull(thriftSuperColumn, sNameSerializer, nameSerializer); superName = sNameSerializer.fromByteBuffer(ByteBuffer.wrap(thriftSuperColumn.getName())); columns = fromThriftColumns(thriftSuperColumn.getColumns()); } /*package*/ HSuperColumnImpl(Serializer sNameSerializer, Serializer nameSerializer, Serializer valueSerializer) { - noneNull(sNameSerializer, nameSerializer, valueSerializer); + noneNull(sNameSerializer, nameSerializer); this.superNameSerializer = sNameSerializer; this.nameSerializer = nameSerializer; this.valueSerializer = valueSerializer; diff --git a/core/src/main/java/me/prettyprint/cassandra/service/template/SuperCfUpdater.java b/core/src/main/java/me/prettyprint/cassandra/service/template/SuperCfUpdater.java index dd185abbd..d24da7118 100644 --- a/core/src/main/java/me/prettyprint/cassandra/service/template/SuperCfUpdater.java +++ b/core/src/main/java/me/prettyprint/cassandra/service/template/SuperCfUpdater.java @@ -21,6 +21,7 @@ import me.prettyprint.hector.api.ColumnFactory; import me.prettyprint.hector.api.Serializer; import me.prettyprint.hector.api.beans.HColumn; +import me.prettyprint.hector.api.factory.HFactory; /** * This provides an interface of updating a specified row, most likely with the @@ -86,8 +87,9 @@ public SN getCurrentSuperColumn() { void updateInternal() { // HSuperColumnImpl needs a refactor, this construction is lame. // the value serializer is not used in HSuperColumnImpl, so this is safe for name + // TODO need to mod to work with 0 timestamp HSuperColumnImpl column = new HSuperColumnImpl(getCurrentSuperColumn(), subColumns, - template.getEffectiveClock(), template.getTopSerializer(), template.getSubSerializer(), TypeInferringSerializer.get()); + 0, template.getTopSerializer(), template.getSubSerializer(), TypeInferringSerializer.get()); template.getMutator().addInsertion(getCurrentKey(), template.getColumnFamily(), column); } From 2d02c07071ae847d53fb8b22f48b8f828d5b7ba8 Mon Sep 17 00:00:00 2001 From: Ed Anuff Date: Mon, 4 Apr 2011 10:18:54 -0700 Subject: [PATCH 092/144] AbstractComponent.addAll() etc. now do the right thing if called with a composite --- .../me/prettyprint/hector/api/beans/AbstractComposite.java | 3 +++ 1 file changed, 3 insertions(+) diff --git a/core/src/main/java/me/prettyprint/hector/api/beans/AbstractComposite.java b/core/src/main/java/me/prettyprint/hector/api/beans/AbstractComposite.java index ce04e8182..2bc2948d7 100644 --- a/core/src/main/java/me/prettyprint/hector/api/beans/AbstractComposite.java +++ b/core/src/main/java/me/prettyprint/hector/api/beans/AbstractComposite.java @@ -450,6 +450,9 @@ private static Object mapIfNumber(Object o) { @SuppressWarnings({ "unchecked" }) private static Collection flatten(Collection c) { + if (c instanceof AbstractComposite) { + return ((AbstractComposite) c).getComponents(); + } boolean hasCollection = false; for (Object o : c) { if (o instanceof Collection) { From 14b6b5f2beaf23b6ee96db0e5d42c8de910282e0 Mon Sep 17 00:00:00 2001 From: zznate Date: Mon, 4 Apr 2011 16:12:35 -0500 Subject: [PATCH 093/144] build out of supercol methods on template --- .../template/ColumnFamilyResultWrapper.java | 44 +++++++++++------ .../template/SuperCfResultWrapper.java | 2 + .../service/template/SuperCfTemplate.java | 49 ++++++++++--------- .../template/ThriftSuperCfTemplate.java | 4 +- .../service/template/SuperCfTemplateTest.java | 15 ++++++ 5 files changed, 75 insertions(+), 39 deletions(-) diff --git a/core/src/main/java/me/prettyprint/cassandra/service/template/ColumnFamilyResultWrapper.java b/core/src/main/java/me/prettyprint/cassandra/service/template/ColumnFamilyResultWrapper.java index c3326bc55..7183d7a1b 100644 --- a/core/src/main/java/me/prettyprint/cassandra/service/template/ColumnFamilyResultWrapper.java +++ b/core/src/main/java/me/prettyprint/cassandra/service/template/ColumnFamilyResultWrapper.java @@ -16,6 +16,7 @@ import me.prettyprint.hector.api.Serializer; import me.prettyprint.hector.api.beans.HColumn; +import org.apache.cassandra.thrift.Column; import org.apache.cassandra.thrift.ColumnOrSuperColumn; /** @@ -26,10 +27,10 @@ */ public class ColumnFamilyResultWrapper extends AbstractResultWrapper { - private Map> columns = new LinkedHashMap>(); - private Iterator>> rows; - private Map.Entry> entry; - private ExecutionResult>> executionResult; + protected Map> columns = new LinkedHashMap>(); + protected Iterator>> rows; + protected Map.Entry> entry; + protected ExecutionResult>> executionResult; public ColumnFamilyResultWrapper(Serializer keySerializer, Serializer columnNameSerializer, @@ -58,23 +59,38 @@ private HColumn getColumn( N columnName ) { private void applyToRow(List cosclist) { - HColumn column; - N colName; + for (Iterator iterator = cosclist.iterator(); iterator.hasNext();) { ColumnOrSuperColumn cosc = iterator.next(); - - colName = columnNameSerializer.fromByteBuffer(cosc.getColumn().name.duplicate()); - column = columns.get(colName); - - if ( column == null ) { - column = new HColumnImpl(cosc.getColumn(), columnNameSerializer, ByteBufferSerializer.get()); + if ( cosc.isSetSuper_column() ) { + applySuper(cosc); } else { - ((HColumnImpl)column).apply(cosc.getColumn()); + applyStandard(cosc.getColumn()); } - columns.put(colName, column); + iterator.remove(); } } + + private void applySuper(ColumnOrSuperColumn cosc) { + Iterator tcolumns = cosc.getSuper_column().getColumnsIterator(); + while ( tcolumns.hasNext() ) { + applyStandard(tcolumns.next()); + } + } + + + private void applyStandard(Column cosc) { + N colName = columnNameSerializer.fromByteBuffer(cosc.name.duplicate()); + HColumn column = columns.get(colName); + + if ( column == null ) { + column = new HColumnImpl(cosc, columnNameSerializer, ByteBufferSerializer.get()); + } else { + ((HColumnImpl)column).apply(cosc); + } + columns.put(colName, column); + } @Override public K getKey() { diff --git a/core/src/main/java/me/prettyprint/cassandra/service/template/SuperCfResultWrapper.java b/core/src/main/java/me/prettyprint/cassandra/service/template/SuperCfResultWrapper.java index f4a093d6e..1aa5b421d 100644 --- a/core/src/main/java/me/prettyprint/cassandra/service/template/SuperCfResultWrapper.java +++ b/core/src/main/java/me/prettyprint/cassandra/service/template/SuperCfResultWrapper.java @@ -29,6 +29,8 @@ public SuperCfResultWrapper(Serializer keySerializer, SN sColumnName) { super(keySerializer, subSerializer, executionResult); this.sNameSerializer = sNameSerializer; + this.sColumnName = sColumnName; + } diff --git a/core/src/main/java/me/prettyprint/cassandra/service/template/SuperCfTemplate.java b/core/src/main/java/me/prettyprint/cassandra/service/template/SuperCfTemplate.java index 3092ad580..c1466e710 100644 --- a/core/src/main/java/me/prettyprint/cassandra/service/template/SuperCfTemplate.java +++ b/core/src/main/java/me/prettyprint/cassandra/service/template/SuperCfTemplate.java @@ -1,5 +1,6 @@ package me.prettyprint.cassandra.service.template; +import java.util.Collection; import java.util.List; import me.prettyprint.cassandra.model.HSlicePredicate; @@ -122,53 +123,53 @@ public int countSubColumns(K key, SN superColumnName, N start, return query.execute().get(); } - @SuppressWarnings("unchecked") - public HColumn querySingleSubColumn(K key, - SN columnName, N subColumnName, Class valueClass) { - return null; - } - - - public HColumn querySingleSubColumn(K key, - SN columnName, N subColumnName, Serializer valueSerializer) { + + public HColumn querySingleSubColumn(K key, + SN columnName, N subColumnName, Serializer valueSerializer) { return null; } @SuppressWarnings("unchecked") - public List querySuperColumns(K key, - SuperCfRowMapper mapper) { - return null; + public SuperCfResult querySuperColumns(K key, List sColumnNames) { + HSlicePredicate workingSlicePredicate = new HSlicePredicate(topSerializer); + workingSlicePredicate.setColumnNames(sColumnNames); + return doExecuteSlice(key, null, workingSlicePredicate); } - public List querySuperColumns(K key, HSlicePredicate predicate, + + + public T querySuperColumns(K key, List sColumnNames, SuperCfRowMapper mapper) { - return null; + + HSlicePredicate workingSlicePredicate = new HSlicePredicate(topSerializer); + workingSlicePredicate.setColumnNames(sColumnNames); + return mapper.mapRow(doExecuteSlice(key, null, workingSlicePredicate)); } - @SuppressWarnings("unchecked") - public List querySuperColumns(K key, List columns, - SuperCfRowMapper mapper) { - return null; + public SuperCfResult querySuperColumn(K key, SN sColumnName) { + return doExecuteSlice(key, sColumnName, activeSlicePredicate); } - public SuperCfUpdater createUpdater(K key, SN sColumnName) { + public SuperCfUpdater createUpdater(K key, SN sColumnName) { + return createUpdater(key).addSuperColumn(sColumnName); + } + + public SuperCfUpdater createUpdater(K key) { SuperCfUpdater updater = new SuperCfUpdater(this, columnFactory); updater.addKey(key); - updater.addSuperColumn(sColumnName); return updater; } - + public void update(SuperCfUpdater updater) { updater.updateInternal(); updater.update(); executeIfNotBatched(); } - public SuperCfResult querySuperColumn(K key, SN sColumnName) { - return doExecuteSlice(key, sColumnName, activeSlicePredicate); - } + protected abstract SuperCfResult doExecuteSlice(K key, SN sColumnName, HSlicePredicate predicate); + } diff --git a/core/src/main/java/me/prettyprint/cassandra/service/template/ThriftSuperCfTemplate.java b/core/src/main/java/me/prettyprint/cassandra/service/template/ThriftSuperCfTemplate.java index 9a3adda4b..f0b979fe2 100644 --- a/core/src/main/java/me/prettyprint/cassandra/service/template/ThriftSuperCfTemplate.java +++ b/core/src/main/java/me/prettyprint/cassandra/service/template/ThriftSuperCfTemplate.java @@ -29,7 +29,9 @@ public ThriftSuperCfTemplate(Keyspace keyspace, String columnFamily, protected SuperCfResult doExecuteSlice(K key, SN sColumnName, HSlicePredicate predicate) { ColumnParent workingColumnParent = columnParent.deepCopy(); - workingColumnParent.setSuper_column(topSerializer.toByteBuffer(sColumnName)); + if ( sColumnName != null ) { + workingColumnParent.setSuper_column(topSerializer.toByteBuffer(sColumnName)); + } SuperCfResultWrapper wrapper = new SuperCfResultWrapper(keySerializer, topSerializer, subSerializer, sliceInternal(key, workingColumnParent, predicate), diff --git a/core/src/test/java/me/prettyprint/cassandra/service/template/SuperCfTemplateTest.java b/core/src/test/java/me/prettyprint/cassandra/service/template/SuperCfTemplateTest.java index 4525eebcd..796cbd3e7 100644 --- a/core/src/test/java/me/prettyprint/cassandra/service/template/SuperCfTemplateTest.java +++ b/core/src/test/java/me/prettyprint/cassandra/service/template/SuperCfTemplateTest.java @@ -2,6 +2,8 @@ import static org.junit.Assert.assertEquals; +import java.util.Arrays; + import org.junit.Test; @@ -19,5 +21,18 @@ public void testSuperCfInsertReadTemplate() { assertEquals("sub_val_1",result.getString("sub_col_1")); } + @Test + public void testSuperCfMultiSc() { + SuperCfTemplate sTemplate = + new ThriftSuperCfTemplate(keyspace, "Super1", se, se, se); + SuperCfUpdater sUpdater = sTemplate.createUpdater("skey2","super1"); + sUpdater.setString("sub1_col_1", "sub1_val_1"); + //sUpdater.addSuperColumn("super2"); + //sUpdater.setString("sub2_col_1", "sub2_val_1"); + sTemplate.update(sUpdater); + + SuperCfResult result = sTemplate.querySuperColumns("skey2", Arrays.asList("super1")); + //assertEquals("sub_val_1",result.getString("sub_col_1")); + } } From f41f210e4ea4261084f01a17dd66788ceba24721 Mon Sep 17 00:00:00 2001 From: zznate Date: Mon, 4 Apr 2011 16:35:57 -0500 Subject: [PATCH 094/144] added new mapped results for supercf --- .../service/template/MappedSuperCfResult.java | 6 ++++ .../template/MappedSuperCfResultWrapper.java | 36 +++++++++++++++++++ 2 files changed, 42 insertions(+) create mode 100644 core/src/main/java/me/prettyprint/cassandra/service/template/MappedSuperCfResult.java create mode 100644 core/src/main/java/me/prettyprint/cassandra/service/template/MappedSuperCfResultWrapper.java diff --git a/core/src/main/java/me/prettyprint/cassandra/service/template/MappedSuperCfResult.java b/core/src/main/java/me/prettyprint/cassandra/service/template/MappedSuperCfResult.java new file mode 100644 index 000000000..a94758a35 --- /dev/null +++ b/core/src/main/java/me/prettyprint/cassandra/service/template/MappedSuperCfResult.java @@ -0,0 +1,6 @@ +package me.prettyprint.cassandra.service.template; + +public interface MappedSuperCfResult extends SuperCfResult { + + V getRow(); +} diff --git a/core/src/main/java/me/prettyprint/cassandra/service/template/MappedSuperCfResultWrapper.java b/core/src/main/java/me/prettyprint/cassandra/service/template/MappedSuperCfResultWrapper.java new file mode 100644 index 000000000..3e7d8c4bf --- /dev/null +++ b/core/src/main/java/me/prettyprint/cassandra/service/template/MappedSuperCfResultWrapper.java @@ -0,0 +1,36 @@ +package me.prettyprint.cassandra.service.template; + +import java.nio.ByteBuffer; +import java.util.List; +import java.util.Map; + +import me.prettyprint.cassandra.model.ExecutionResult; +import me.prettyprint.hector.api.Serializer; + +import org.apache.cassandra.thrift.ColumnOrSuperColumn; + +public class MappedSuperCfResultWrapper extends SuperCfResultWrapper implements + MappedSuperCfResult { + + private SuperCfRowMapper rowMapper; + + public MappedSuperCfResultWrapper( + Serializer keySerializer, + Serializer sNameSerializer, + Serializer subSerializer, + ExecutionResult>> executionResult, + SN sColumnName, + SuperCfRowMapper mapper) { + super(keySerializer, sNameSerializer, subSerializer, executionResult, + sColumnName); + this.rowMapper = mapper; + } + + @Override + public V getRow() { + return rowMapper.mapRow(this); + } + + + +} From 9125040290659e8ab3f2014c9c6f0c136c4d9c23 Mon Sep 17 00:00:00 2001 From: zznate Date: Mon, 4 Apr 2011 17:00:36 -0500 Subject: [PATCH 095/144] added querysinglecol for supercol template --- .../service/template/ColumnFamilyResult.java | 6 +++++ .../template/ColumnFamilyResultWrapper.java | 2 +- .../service/template/SuperCfResult.java | 3 ++- .../service/template/SuperCfTemplate.java | 10 +++++++- .../service/template/SuperCfTemplateTest.java | 24 ++++++++++++++++--- 5 files changed, 39 insertions(+), 6 deletions(-) diff --git a/core/src/main/java/me/prettyprint/cassandra/service/template/ColumnFamilyResult.java b/core/src/main/java/me/prettyprint/cassandra/service/template/ColumnFamilyResult.java index 689845473..a081cb714 100644 --- a/core/src/main/java/me/prettyprint/cassandra/service/template/ColumnFamilyResult.java +++ b/core/src/main/java/me/prettyprint/cassandra/service/template/ColumnFamilyResult.java @@ -1,11 +1,14 @@ package me.prettyprint.cassandra.service.template; +import java.nio.ByteBuffer; import java.util.Collection; import java.util.Date; import java.util.Iterator; import java.util.UUID; import me.prettyprint.hector.api.ResultStatus; +import me.prettyprint.hector.api.Serializer; +import me.prettyprint.hector.api.beans.HColumn; /** * A common interface for access to the resuls of a query of either a standard or super column family. @@ -36,4 +39,7 @@ public interface ColumnFamilyResult extends Iterator getColumnNames(); + + HColumn getColumn(N columnName); + } diff --git a/core/src/main/java/me/prettyprint/cassandra/service/template/ColumnFamilyResultWrapper.java b/core/src/main/java/me/prettyprint/cassandra/service/template/ColumnFamilyResultWrapper.java index 7183d7a1b..05004b016 100644 --- a/core/src/main/java/me/prettyprint/cassandra/service/template/ColumnFamilyResultWrapper.java +++ b/core/src/main/java/me/prettyprint/cassandra/service/template/ColumnFamilyResultWrapper.java @@ -53,7 +53,7 @@ public ByteBuffer getColumnValue( N columnName) { return col != null ? col.getValue() : null; } - private HColumn getColumn( N columnName ) { + public HColumn getColumn( N columnName ) { return columns.get( columnName ); } diff --git a/core/src/main/java/me/prettyprint/cassandra/service/template/SuperCfResult.java b/core/src/main/java/me/prettyprint/cassandra/service/template/SuperCfResult.java index da851aa5f..e3fd5cae1 100644 --- a/core/src/main/java/me/prettyprint/cassandra/service/template/SuperCfResult.java +++ b/core/src/main/java/me/prettyprint/cassandra/service/template/SuperCfResult.java @@ -14,5 +14,6 @@ * child column name data type */ public interface SuperCfResult extends ColumnFamilyResult { - public SN getSuperName(); + SN getSuperName(); + } diff --git a/core/src/main/java/me/prettyprint/cassandra/service/template/SuperCfTemplate.java b/core/src/main/java/me/prettyprint/cassandra/service/template/SuperCfTemplate.java index c1466e710..af30f7d91 100644 --- a/core/src/main/java/me/prettyprint/cassandra/service/template/SuperCfTemplate.java +++ b/core/src/main/java/me/prettyprint/cassandra/service/template/SuperCfTemplate.java @@ -1,8 +1,10 @@ package me.prettyprint.cassandra.service.template; +import java.nio.ByteBuffer; import java.util.Collection; import java.util.List; +import me.prettyprint.cassandra.model.HColumnImpl; import me.prettyprint.cassandra.model.HSlicePredicate; import me.prettyprint.hector.api.Keyspace; import me.prettyprint.hector.api.Serializer; @@ -127,7 +129,13 @@ public int countSubColumns(K key, SN superColumnName, N start, public HColumn querySingleSubColumn(K key, SN columnName, N subColumnName, Serializer valueSerializer) { - return null; + + SuperCfResult result = doExecuteSlice(key, columnName, activeSlicePredicate); + HColumn origCol = result.getColumn(subColumnName); + // TODO make this far less hacky + return new HColumnImpl(subColumnName, + valueSerializer.fromByteBuffer(origCol.getValue()), origCol.getClock(), + subSerializer, valueSerializer); } @SuppressWarnings("unchecked") diff --git a/core/src/test/java/me/prettyprint/cassandra/service/template/SuperCfTemplateTest.java b/core/src/test/java/me/prettyprint/cassandra/service/template/SuperCfTemplateTest.java index 796cbd3e7..5fb0ef9a1 100644 --- a/core/src/test/java/me/prettyprint/cassandra/service/template/SuperCfTemplateTest.java +++ b/core/src/test/java/me/prettyprint/cassandra/service/template/SuperCfTemplateTest.java @@ -4,6 +4,8 @@ import java.util.Arrays; +import me.prettyprint.hector.api.beans.HColumn; + import org.junit.Test; @@ -21,18 +23,34 @@ public void testSuperCfInsertReadTemplate() { assertEquals("sub_val_1",result.getString("sub_col_1")); } + @Test public void testSuperCfMultiSc() { SuperCfTemplate sTemplate = new ThriftSuperCfTemplate(keyspace, "Super1", se, se, se); SuperCfUpdater sUpdater = sTemplate.createUpdater("skey2","super1"); sUpdater.setString("sub1_col_1", "sub1_val_1"); - //sUpdater.addSuperColumn("super2"); - //sUpdater.setString("sub2_col_1", "sub2_val_1"); + sUpdater.addSuperColumn("super2"); + sUpdater.setString("sub2_col_1", "sub2_val_1"); sTemplate.update(sUpdater); SuperCfResult result = sTemplate.querySuperColumns("skey2", Arrays.asList("super1")); - //assertEquals("sub_val_1",result.getString("sub_col_1")); + assertEquals("sub1_val_1",result.getString("sub1_col_1")); + //assertEquals("sub2_val_1",result.next().getString("sub2_col_1")); + + } + + @Test + public void testQuerySingleSubColumn() { + SuperCfTemplate sTemplate = + new ThriftSuperCfTemplate(keyspace, "Super1", se, se, se); + SuperCfUpdater sUpdater = sTemplate.createUpdater("skey3","super1"); + sUpdater.setString("sub1_col_1", "sub1_val_1"); + sTemplate.update(sUpdater); + + HColumn myCol = sTemplate.querySingleSubColumn("skey3", "super1", "sub1_col_1", se); + assertEquals("sub1_val_1", myCol.getValue()); } + } From b7735ef98db18e8f7e793440ff64c6799448b247 Mon Sep 17 00:00:00 2001 From: zznate Date: Mon, 4 Apr 2011 17:37:43 -0500 Subject: [PATCH 096/144] fixed issue with pointing at correct update --- .../cassandra/service/template/SuperCfUpdater.java | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/core/src/main/java/me/prettyprint/cassandra/service/template/SuperCfUpdater.java b/core/src/main/java/me/prettyprint/cassandra/service/template/SuperCfUpdater.java index d24da7118..258556b3e 100644 --- a/core/src/main/java/me/prettyprint/cassandra/service/template/SuperCfUpdater.java +++ b/core/src/main/java/me/prettyprint/cassandra/service/template/SuperCfUpdater.java @@ -67,11 +67,12 @@ public SuperCfUpdater(SuperCfTemplate sTemplate, ColumnFactory columnFac public SuperCfUpdater addSuperColumn(SN sColumnName) { if ( sColumnNames == null ) { - sColumnNames = new ArrayList(); - subColumns = new ArrayList(); + sColumnNames = new ArrayList(); } else { - sColPos++; + updateInternal(); + sColPos++; } + subColumns = new ArrayList(); sColumnNames.add(sColumnName); return this; From de78ca887b6096f488425b5a43afe2ad1f35a38e Mon Sep 17 00:00:00 2001 From: zznate Date: Mon, 4 Apr 2011 17:56:04 -0500 Subject: [PATCH 097/144] added null check on sc hit --- .../cassandra/service/template/SuperCfTemplate.java | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/core/src/main/java/me/prettyprint/cassandra/service/template/SuperCfTemplate.java b/core/src/main/java/me/prettyprint/cassandra/service/template/SuperCfTemplate.java index af30f7d91..0a15c41e8 100644 --- a/core/src/main/java/me/prettyprint/cassandra/service/template/SuperCfTemplate.java +++ b/core/src/main/java/me/prettyprint/cassandra/service/template/SuperCfTemplate.java @@ -130,9 +130,12 @@ public int countSubColumns(K key, SN superColumnName, N start, public HColumn querySingleSubColumn(K key, SN columnName, N subColumnName, Serializer valueSerializer) { - SuperCfResult result = doExecuteSlice(key, columnName, activeSlicePredicate); + SuperCfResult result = doExecuteSlice(key, columnName, activeSlicePredicate); HColumn origCol = result.getColumn(subColumnName); // TODO make this far less hacky + if ( columnName == null || origCol == null ) { + return null; + } return new HColumnImpl(subColumnName, valueSerializer.fromByteBuffer(origCol.getValue()), origCol.getClock(), subSerializer, valueSerializer); From 358b15d2a23df37c5aed77f3d7692dbe401cf5eb Mon Sep 17 00:00:00 2001 From: Ed Anuff Date: Mon, 4 Apr 2011 21:10:29 -0700 Subject: [PATCH 098/144] fixed bug causing corrupt composite serialization --- .../utils/ByteBufferOutputStream.java | 3 +- .../hector/api/beans/AbstractComposite.java | 12 ++++++-- .../serializers/StringSerializerTest.java | 28 +++++++++---------- .../prettyprint/hector/api/CompositeTest.java | 5 ++++ 4 files changed, 31 insertions(+), 17 deletions(-) diff --git a/core/src/main/java/me/prettyprint/cassandra/utils/ByteBufferOutputStream.java b/core/src/main/java/me/prettyprint/cassandra/utils/ByteBufferOutputStream.java index 06a144e73..423cc40be 100644 --- a/core/src/main/java/me/prettyprint/cassandra/utils/ByteBufferOutputStream.java +++ b/core/src/main/java/me/prettyprint/cassandra/utils/ByteBufferOutputStream.java @@ -150,7 +150,8 @@ public void write(byte[] b, int off, int len) { /** Add a buffer to the output without copying, if possible. */ public void write(ByteBuffer buffer) { if (buffer.remaining() < BUFFER_SIZE) { - write(buffer.array(), buffer.position(), buffer.remaining()); + write(buffer.array(), buffer.arrayOffset() + buffer.position(), + buffer.remaining()); } else { // append w/o copying bytes ByteBuffer dup = buffer.duplicate(); dup.position(buffer.limit()); // ready for flip diff --git a/core/src/main/java/me/prettyprint/hector/api/beans/AbstractComposite.java b/core/src/main/java/me/prettyprint/hector/api/beans/AbstractComposite.java index 2bc2948d7..4c95a5869 100644 --- a/core/src/main/java/me/prettyprint/hector/api/beans/AbstractComposite.java +++ b/core/src/main/java/me/prettyprint/hector/api/beans/AbstractComposite.java @@ -145,6 +145,9 @@ public A getValue(Serializer s) { return s.fromByteBuffer(cb); } } + if (value instanceof ByteBuffer) { + return (A) ((ByteBuffer) value).duplicate(); + } return (A) value; } @@ -163,11 +166,11 @@ public ByteBuffer getBytes(Serializer s) { s = (Serializer) serializer; } if (s != null) { - return s.toByteBuffer((A) value); + return s.toByteBuffer((A) value).duplicate(); } } } - return bytes; + return bytes.duplicate(); } public ByteBuffer getBytes() { @@ -181,6 +184,11 @@ public String getComparator() { public ComponentEquality getEquality() { return equality; } + + @Override + public String toString() { + return "Component [" + getValue() + "]"; + } } List> components = new ArrayList>(); diff --git a/core/src/test/java/me/prettyprint/cassandra/serializers/StringSerializerTest.java b/core/src/test/java/me/prettyprint/cassandra/serializers/StringSerializerTest.java index ff11cf315..774d96a85 100644 --- a/core/src/test/java/me/prettyprint/cassandra/serializers/StringSerializerTest.java +++ b/core/src/test/java/me/prettyprint/cassandra/serializers/StringSerializerTest.java @@ -1,10 +1,10 @@ package me.prettyprint.cassandra.serializers; - import java.io.UnsupportedEncodingException; import java.util.Arrays; import java.util.Collection; +import org.apache.cassandra.db.marshal.UTF8Type; import org.apache.cassandra.utils.ByteBufferUtil; import org.apache.commons.lang.RandomStringUtils; import org.junit.Assert; @@ -25,22 +25,22 @@ public StringSerializerTest(String str) { @Parameters public static Collection data() throws UnsupportedEncodingException { - Object[][] data = new Object[][] { - {"" }, - {null}, - {"123"}, - {"QWER"}, - {"!@#$#$^%&^*fdghdfghdfgh%^&*"}, - {new String("\u05E9".getBytes(), "utf-8")}, - {RandomStringUtils.random(256*256)} - }; + Object[][] data = new Object[][] { { "" }, { null }, { "123" }, { "QWER" }, + { "!@#$#$^%&^*fdghdfghdfgh%^&*" }, + { new String("\u05E9".getBytes(), "utf-8") }, + { RandomStringUtils.random(256 * 256) } }; return Arrays.asList(data); } @Test - public void test() throws Exception { - Assert.assertEquals(str, s.fromByteBuffer(s.toByteBuffer(str))) ; - if ( str != null) - Assert.assertEquals(str, ByteBufferUtil.string(ByteBufferUtil.bytes(str))); + public void test() throws Exception { + Assert.assertEquals(str, s.fromByteBuffer(s.toByteBuffer(str))); + if (str != null) { + Assert + .assertEquals(str, ByteBufferUtil.string(ByteBufferUtil.bytes(str))); + } + if (str != null) { + UTF8Type.instance.validate(s.toByteBuffer(str)); + } } } diff --git a/core/src/test/java/me/prettyprint/hector/api/CompositeTest.java b/core/src/test/java/me/prettyprint/hector/api/CompositeTest.java index 84032f24f..aad265731 100644 --- a/core/src/test/java/me/prettyprint/hector/api/CompositeTest.java +++ b/core/src/test/java/me/prettyprint/hector/api/CompositeTest.java @@ -18,6 +18,7 @@ import me.prettyprint.hector.api.beans.Composite; import me.prettyprint.hector.api.beans.DynamicComposite; +import org.apache.cassandra.db.marshal.UTF8Type; import org.apache.cassandra.utils.ByteBufferUtil; import org.apache.cassandra.utils.UUIDGen; import org.junit.Test; @@ -135,6 +136,10 @@ public void testDynamicSerialization() throws Exception { assertEquals(i, ((BigInteger) o).intValue()); } + b = DynamicComposite.toByteBuffer("foo"); + c = DynamicComposite.fromByteBuffer(b); + b = c.getComponent(0).getBytes(); + UTF8Type.instance.validate(b); } @Test From 832947c0bbc1afce7aace3dccfb3329ccb2ff529 Mon Sep 17 00:00:00 2001 From: zznate Date: Mon, 4 Apr 2011 23:53:41 -0500 Subject: [PATCH 099/144] super col result wrapping smoothed out, coverage for such --- .../template/ColumnFamilyResultWrapper.java | 8 +- .../template/MappedSuperCfResultWrapper.java | 4 +- .../service/template/SuperCfResult.java | 32 +++- .../template/SuperCfResultWrapper.java | 181 ++++++++++++++++-- .../service/template/SuperCfTemplate.java | 4 +- .../template/ThriftSuperCfTemplate.java | 12 +- .../service/template/SuperCfTemplateTest.java | 26 ++- 7 files changed, 233 insertions(+), 34 deletions(-) diff --git a/core/src/main/java/me/prettyprint/cassandra/service/template/ColumnFamilyResultWrapper.java b/core/src/main/java/me/prettyprint/cassandra/service/template/ColumnFamilyResultWrapper.java index 05004b016..f519d1684 100644 --- a/core/src/main/java/me/prettyprint/cassandra/service/template/ColumnFamilyResultWrapper.java +++ b/core/src/main/java/me/prettyprint/cassandra/service/template/ColumnFamilyResultWrapper.java @@ -27,10 +27,10 @@ */ public class ColumnFamilyResultWrapper extends AbstractResultWrapper { - protected Map> columns = new LinkedHashMap>(); - protected Iterator>> rows; - protected Map.Entry> entry; - protected ExecutionResult>> executionResult; + private Map> columns = new LinkedHashMap>(); + private Iterator>> rows; + private Map.Entry> entry; + private ExecutionResult>> executionResult; public ColumnFamilyResultWrapper(Serializer keySerializer, Serializer columnNameSerializer, diff --git a/core/src/main/java/me/prettyprint/cassandra/service/template/MappedSuperCfResultWrapper.java b/core/src/main/java/me/prettyprint/cassandra/service/template/MappedSuperCfResultWrapper.java index 3e7d8c4bf..d07612ecf 100644 --- a/core/src/main/java/me/prettyprint/cassandra/service/template/MappedSuperCfResultWrapper.java +++ b/core/src/main/java/me/prettyprint/cassandra/service/template/MappedSuperCfResultWrapper.java @@ -19,10 +19,8 @@ public MappedSuperCfResultWrapper( Serializer sNameSerializer, Serializer subSerializer, ExecutionResult>> executionResult, - SN sColumnName, SuperCfRowMapper mapper) { - super(keySerializer, sNameSerializer, subSerializer, executionResult, - sColumnName); + super(keySerializer, sNameSerializer, subSerializer, executionResult); this.rowMapper = mapper; } diff --git a/core/src/main/java/me/prettyprint/cassandra/service/template/SuperCfResult.java b/core/src/main/java/me/prettyprint/cassandra/service/template/SuperCfResult.java index e3fd5cae1..45a0bb0bc 100644 --- a/core/src/main/java/me/prettyprint/cassandra/service/template/SuperCfResult.java +++ b/core/src/main/java/me/prettyprint/cassandra/service/template/SuperCfResult.java @@ -1,5 +1,14 @@ package me.prettyprint.cassandra.service.template; +import java.nio.ByteBuffer; +import java.util.Collection; +import java.util.Date; +import java.util.List; +import java.util.UUID; + +import me.prettyprint.hector.api.beans.HColumn; +import me.prettyprint.hector.api.beans.HSuperColumn; + /** * Holds the result for the contents of a super column. This interface add * access to the current super column name since this may be a property of the @@ -14,6 +23,27 @@ * child column name data type */ public interface SuperCfResult extends ColumnFamilyResult { - SN getSuperName(); + // TODO remove. this no loger makes sense with many-to-one on a row + + Collection getSuperColumns(); + + K getKey(); + + UUID getUUID(SN sColumnName, N columnName); + + String getString(SN sColumnName, N columnName); + + Long getLong(SN sColumnName, N columnName); + + Integer getInteger(SN sColumnName, N columnName); + + Boolean getBoolean(SN sColumnName, N columnName); + + byte[] getByteArray(SN sColumnName, N columnName); + + ByteBuffer getByteBuffer(SN sColumnName, N columnName); + + Date getDate(SN sColumnName, N columnName); + void applySuperColumn(SN sColumnName); } diff --git a/core/src/main/java/me/prettyprint/cassandra/service/template/SuperCfResultWrapper.java b/core/src/main/java/me/prettyprint/cassandra/service/template/SuperCfResultWrapper.java index 1aa5b421d..95dae8380 100644 --- a/core/src/main/java/me/prettyprint/cassandra/service/template/SuperCfResultWrapper.java +++ b/core/src/main/java/me/prettyprint/cassandra/service/template/SuperCfResultWrapper.java @@ -1,13 +1,35 @@ package me.prettyprint.cassandra.service.template; import java.nio.ByteBuffer; +import java.util.ArrayList; +import java.util.Collection; +import java.util.Date; +import java.util.Iterator; +import java.util.LinkedHashMap; import java.util.List; import java.util.Map; +import java.util.NoSuchElementException; +import java.util.UUID; import me.prettyprint.cassandra.model.ExecutionResult; +import me.prettyprint.cassandra.model.HColumnImpl; +import me.prettyprint.cassandra.model.HSuperColumnImpl; +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.IntegerSerializer; +import me.prettyprint.cassandra.serializers.LongSerializer; +import me.prettyprint.cassandra.serializers.StringSerializer; +import me.prettyprint.cassandra.serializers.UUIDSerializer; import me.prettyprint.hector.api.Serializer; +import me.prettyprint.hector.api.beans.HColumn; +import me.prettyprint.hector.api.beans.HSuperColumn; +import org.apache.cassandra.thrift.Column; import org.apache.cassandra.thrift.ColumnOrSuperColumn; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; /** * Provides access to the current row of data during super column queries. @@ -15,31 +37,162 @@ * @author zznate * @param the super column's sub column name type */ -public class SuperCfResultWrapper extends ColumnFamilyResultWrapper implements SuperCfResult { - - +public class SuperCfResultWrapper extends AbstractResultWrapper implements SuperCfResult { + private static final Logger log = LoggerFactory.getLogger(SuperCfResultWrapper.class); + + private Map>> columns = new LinkedHashMap>>(); + private Iterator>> rows; + private Map.Entry> entry; + private ExecutionResult>> executionResult; + private List superColumns; + private Map> subColumns = new LinkedHashMap>(); + private SN currentSuperColumn; private Serializer sNameSerializer; - private SN sColumnName; public SuperCfResultWrapper(Serializer keySerializer, Serializer sNameSerializer, Serializer subSerializer, - ExecutionResult>> executionResult, - SN sColumnName) { + ExecutionResult>> executionResult) { super(keySerializer, subSerializer, executionResult); this.sNameSerializer = sNameSerializer; - this.sColumnName = sColumnName; - + this.rows = executionResult.get().entrySet().iterator(); + next(); + } + + @Override + public SuperCfResult next() { + if ( !hasNext() ) { + throw new NoSuchElementException("No more rows left on this HColumnFamily"); + } + entry = rows.next(); + log.debug("found entry{}", getKey()); + applyToRow(entry.getValue()); + return this; + } + + private void applyToRow(List cosclist) { + superColumns = new ArrayList(cosclist.size()); + for (Iterator iterator = cosclist.iterator(); iterator.hasNext();) { + ColumnOrSuperColumn cosc = iterator.next(); + SN sColName = sNameSerializer.fromByteBuffer(cosc.super_column.name); + log.debug("cosc {}", cosc.super_column); + + superColumns.add(sColName); + Iterator tcolumns = cosc.getSuper_column().getColumnsIterator(); + Map> subColMap = new LinkedHashMap>(); + while ( tcolumns.hasNext() ) { + Column col = tcolumns.next(); + subColMap.put(columnNameSerializer.fromByteBuffer(col.name), new HColumnImpl(col, columnNameSerializer, ByteBufferSerializer.get())); + } + columns.put(sColName, subColMap); + } + } + + + public List getSuperColumns() { + return superColumns; } + + @Override + public ByteBuffer getColumnValue(N columnName) { + HColumn col = getColumn( columnName ); + return col != null ? col.getValue() : null; + } + + + @Override + public K getKey() { + return keySerializer.fromByteBuffer(entry.getKey()); + } - /** - * Provides access to the current super column name from the mapper objects. This may be - * needed when the super columm name may be part of the object being mapped into. - */ - public SN getSuperName() { - return sColumnName; + + @Override + public HColumn getColumn(N columnName) { + return subColumns == null ? null : subColumns.get( columnName ); + } + + + @Override + public Collection getColumnNames() { + return subColumns.keySet(); + } + + + @Override + public boolean hasNext() { + return rows.hasNext(); } + @Override + public void remove() { + rows.remove(); + } + + private V extractType(SN sColumnName, N columnName, Serializer valueSerializer) { + Map> map = columns.get(sColumnName); + if ( map != null && map.get(columnName) != null ) + return valueSerializer.fromByteBuffer(map.get(columnName).getValue()); + return null; + } + + @Override + public Boolean getBoolean(SN sColumnName, N columnName) { + return extractType(sColumnName, columnName, BooleanSerializer.get()); + } + + + @Override + public byte[] getByteArray(SN sColumnName, N columnName) { + return extractType(sColumnName, columnName, BytesArraySerializer.get()); + } + + + @Override + public Date getDate(SN sColumnName, N columnName) { + return extractType(sColumnName, columnName, DateSerializer.get()); + } + + + @Override + public Integer getInteger(SN sColumnName, N columnName) { + return extractType(sColumnName, columnName, IntegerSerializer.get()); + } + + + @Override + public Long getLong(SN sColumnName, N columnName) { + return extractType(sColumnName, columnName, LongSerializer.get()); + } + + + @Override + public String getString(SN sColumnName, N columnName) { + return extractType(sColumnName, columnName, StringSerializer.get()); + } + + + + @Override + public UUID getUUID(SN sColumnName, N columnName) { + return extractType(sColumnName, columnName, UUIDSerializer.get()); + } + + @Override + public ByteBuffer getByteBuffer(SN sColumnName, N columnName) { + return extractType(sColumnName, columnName, ByteBufferSerializer.get()); + } + + + + @Override + public void applySuperColumn(SN sColumnName) { + this.currentSuperColumn = sColumnName; + this.subColumns = columns.get(currentSuperColumn); + + } + + + } diff --git a/core/src/main/java/me/prettyprint/cassandra/service/template/SuperCfTemplate.java b/core/src/main/java/me/prettyprint/cassandra/service/template/SuperCfTemplate.java index 0a15c41e8..dc8fae4c3 100644 --- a/core/src/main/java/me/prettyprint/cassandra/service/template/SuperCfTemplate.java +++ b/core/src/main/java/me/prettyprint/cassandra/service/template/SuperCfTemplate.java @@ -159,7 +159,9 @@ public T querySuperColumns(K key, List sColumnNames, } public SuperCfResult querySuperColumn(K key, SN sColumnName) { - return doExecuteSlice(key, sColumnName, activeSlicePredicate); + HSlicePredicate workingSlicePredicate = new HSlicePredicate(topSerializer); + workingSlicePredicate.addColumnName(sColumnName); + return doExecuteSlice(key, sColumnName, workingSlicePredicate); } public SuperCfUpdater createUpdater(K key, SN sColumnName) { diff --git a/core/src/main/java/me/prettyprint/cassandra/service/template/ThriftSuperCfTemplate.java b/core/src/main/java/me/prettyprint/cassandra/service/template/ThriftSuperCfTemplate.java index f0b979fe2..5897086fb 100644 --- a/core/src/main/java/me/prettyprint/cassandra/service/template/ThriftSuperCfTemplate.java +++ b/core/src/main/java/me/prettyprint/cassandra/service/template/ThriftSuperCfTemplate.java @@ -27,15 +27,13 @@ public ThriftSuperCfTemplate(Keyspace keyspace, String columnFamily, super(keyspace, columnFamily, keySerializer, topSerializer, subSerializer); } - protected SuperCfResult doExecuteSlice(K key, SN sColumnName, HSlicePredicate predicate) { - ColumnParent workingColumnParent = columnParent.deepCopy(); - if ( sColumnName != null ) { - workingColumnParent.setSuper_column(topSerializer.toByteBuffer(sColumnName)); - } + protected SuperCfResult doExecuteSlice(K key, SN sColumnName, HSlicePredicate predicate) { SuperCfResultWrapper wrapper = new SuperCfResultWrapper(keySerializer, topSerializer, subSerializer, - sliceInternal(key, workingColumnParent, predicate), - sColumnName); + sliceInternal(key, columnParent, predicate)); + if ( sColumnName != null ) { + wrapper.applySuperColumn(sColumnName); + } return wrapper; } diff --git a/core/src/test/java/me/prettyprint/cassandra/service/template/SuperCfTemplateTest.java b/core/src/test/java/me/prettyprint/cassandra/service/template/SuperCfTemplateTest.java index 5fb0ef9a1..52c04acad 100644 --- a/core/src/test/java/me/prettyprint/cassandra/service/template/SuperCfTemplateTest.java +++ b/core/src/test/java/me/prettyprint/cassandra/service/template/SuperCfTemplateTest.java @@ -1,6 +1,6 @@ package me.prettyprint.cassandra.service.template; -import static org.junit.Assert.assertEquals; +import static org.junit.Assert.*; import java.util.Arrays; @@ -20,7 +20,8 @@ public void testSuperCfInsertReadTemplate() { sTemplate.update(sUpdater); SuperCfResult result = sTemplate.querySuperColumn("skey1", "super1"); - assertEquals("sub_val_1",result.getString("sub_col_1")); + + assertEquals("sub_val_1",result.getString("super1","sub_col_1")); } @@ -34,8 +35,13 @@ public void testSuperCfMultiSc() { sUpdater.setString("sub2_col_1", "sub2_val_1"); sTemplate.update(sUpdater); - SuperCfResult result = sTemplate.querySuperColumns("skey2", Arrays.asList("super1")); - assertEquals("sub1_val_1",result.getString("sub1_col_1")); + SuperCfResult result = sTemplate.querySuperColumns("skey2", Arrays.asList("super1","super2")); + assertEquals(2,result.getSuperColumns().size()); + /*for (String sName : result.getSuperColumns() ) { + result.getString(sName,"sub1_col_1"); + }*/ + + //assertEquals("sub1_val_1",result.getString("sub1_col_1")); //assertEquals("sub2_val_1",result.next().getString("sub2_col_1")); } @@ -53,4 +59,16 @@ public void testQuerySingleSubColumn() { } + @Test + public void testQuerySingleSubColumnEmpty() { + SuperCfTemplate sTemplate = + new ThriftSuperCfTemplate(keyspace, "Super1", se, se, se); + SuperCfUpdater sUpdater = sTemplate.createUpdater("skey3","super1"); + sUpdater.setString("sub1_col_1", "sub1_val_1"); + sTemplate.update(sUpdater); + + HColumn myCol = sTemplate.querySingleSubColumn("skey3", "super2", "sub1_col_1", se); + assertNull(myCol); + } + } From 6bea28cd1c7a944107f79cfa6151b8a502ec822b Mon Sep 17 00:00:00 2001 From: zznate Date: Tue, 5 Apr 2011 00:12:43 -0500 Subject: [PATCH 100/144] added multikey, multi-sf method --- .../service/template/SuperCfResult.java | 3 ++ .../service/template/SuperCfTemplate.java | 9 ++++- .../template/ThriftSuperCfTemplate.java | 35 +++++++++++++++++-- .../service/template/SuperCfTemplateTest.java | 18 ++++++++++ 4 files changed, 61 insertions(+), 4 deletions(-) diff --git a/core/src/main/java/me/prettyprint/cassandra/service/template/SuperCfResult.java b/core/src/main/java/me/prettyprint/cassandra/service/template/SuperCfResult.java index 45a0bb0bc..1bcce8178 100644 --- a/core/src/main/java/me/prettyprint/cassandra/service/template/SuperCfResult.java +++ b/core/src/main/java/me/prettyprint/cassandra/service/template/SuperCfResult.java @@ -46,4 +46,7 @@ public interface SuperCfResult extends ColumnFamilyResult { Date getDate(SN sColumnName, N columnName); void applySuperColumn(SN sColumnName); + + @Override + SuperCfResult next(); } diff --git a/core/src/main/java/me/prettyprint/cassandra/service/template/SuperCfTemplate.java b/core/src/main/java/me/prettyprint/cassandra/service/template/SuperCfTemplate.java index dc8fae4c3..e805dd8b9 100644 --- a/core/src/main/java/me/prettyprint/cassandra/service/template/SuperCfTemplate.java +++ b/core/src/main/java/me/prettyprint/cassandra/service/template/SuperCfTemplate.java @@ -148,7 +148,12 @@ public SuperCfResult querySuperColumns(K key, List sColumnNames) { return doExecuteSlice(key, null, workingSlicePredicate); } - + @SuppressWarnings("unchecked") + public SuperCfResult querySuperColumns(List keys, List sColumnNames) { + HSlicePredicate workingSlicePredicate = new HSlicePredicate(topSerializer); + workingSlicePredicate.setColumnNames(sColumnNames); + return doExecuteMultigetSlice(keys, workingSlicePredicate); + } public T querySuperColumns(K key, List sColumnNames, SuperCfRowMapper mapper) { @@ -184,5 +189,7 @@ public void update(SuperCfUpdater updater) { protected abstract SuperCfResult doExecuteSlice(K key, SN sColumnName, HSlicePredicate predicate); + protected abstract SuperCfResult doExecuteMultigetSlice(List keys, HSlicePredicate predicate); + } diff --git a/core/src/main/java/me/prettyprint/cassandra/service/template/ThriftSuperCfTemplate.java b/core/src/main/java/me/prettyprint/cassandra/service/template/ThriftSuperCfTemplate.java index 5897086fb..63db9480c 100644 --- a/core/src/main/java/me/prettyprint/cassandra/service/template/ThriftSuperCfTemplate.java +++ b/core/src/main/java/me/prettyprint/cassandra/service/template/ThriftSuperCfTemplate.java @@ -30,15 +30,21 @@ public ThriftSuperCfTemplate(Keyspace keyspace, String columnFamily, protected SuperCfResult doExecuteSlice(K key, SN sColumnName, HSlicePredicate predicate) { SuperCfResultWrapper wrapper = new SuperCfResultWrapper(keySerializer, topSerializer, subSerializer, - sliceInternal(key, columnParent, predicate)); + sliceInternal(key, predicate)); if ( sColumnName != null ) { wrapper.applySuperColumn(sColumnName); } return wrapper; } + protected SuperCfResult doExecuteMultigetSlice(List keys, HSlicePredicate predicate) { + SuperCfResultWrapper wrapper = + new SuperCfResultWrapper(keySerializer, topSerializer, subSerializer, + multigetSliceInternal(keys, columnParent, predicate)); + return wrapper; + } + private ExecutionResult>> sliceInternal(final K key, - final ColumnParent workingColumnParent, final HSlicePredicate workingSlicePredicate) { return ((ExecutingKeyspace)keyspace).doExecuteOperation(new Operation>>(OperationType.READ) { @Override @@ -47,7 +53,7 @@ public Map> execute(Cassandra.Client cassan try { ByteBuffer sKey = keySerializer.toByteBuffer(key); - cosc.put(sKey, cassandra.get_slice(sKey, workingColumnParent, + cosc.put(sKey, cassandra.get_slice(sKey, columnParent, workingSlicePredicate.toThrift(), ThriftConverter.consistencyLevel(consistencyLevelPolicy.get(operationType)))); @@ -59,5 +65,28 @@ public Map> execute(Cassandra.Client cassan } }); } + + private ExecutionResult>> multigetSliceInternal(final List keys, + final ColumnParent workingColumnParent, + final HSlicePredicate workingSlicePredicate) { + return ((ExecutingKeyspace)keyspace).doExecuteOperation(new Operation>>(OperationType.READ) { + @Override + public Map> execute(Cassandra.Client cassandra) throws HectorException { + Map> cosc; + try { + + List sKeys = keySerializer.toBytesList(keys); + cosc = cassandra.multiget_slice(sKeys, workingColumnParent, + workingSlicePredicate.toThrift(), + ThriftConverter.consistencyLevel(consistencyLevelPolicy.get(operationType))); + + } catch (Exception e) { + throw exceptionsTranslator.translate(e); + } + + return cosc; + } + }); + } } diff --git a/core/src/test/java/me/prettyprint/cassandra/service/template/SuperCfTemplateTest.java b/core/src/test/java/me/prettyprint/cassandra/service/template/SuperCfTemplateTest.java index 52c04acad..64037cf85 100644 --- a/core/src/test/java/me/prettyprint/cassandra/service/template/SuperCfTemplateTest.java +++ b/core/src/test/java/me/prettyprint/cassandra/service/template/SuperCfTemplateTest.java @@ -71,4 +71,22 @@ public void testQuerySingleSubColumnEmpty() { assertNull(myCol); } + @Test + public void testSuperCfInsertReadMultiKey() { + SuperCfTemplate sTemplate = + new ThriftSuperCfTemplate(keyspace, "Super1", se, se, se); + SuperCfUpdater sUpdater = sTemplate.createUpdater("skey1","super1"); + sUpdater.setString("sub_col_1", "sub_val_1"); + sUpdater.addKey("skey2"); + sUpdater.addSuperColumn("super1"); + sUpdater.setString("sub_col_1", "sub_val_2"); + sTemplate.update(sUpdater); + + SuperCfResult result = sTemplate.querySuperColumns(Arrays.asList("skey1","skey2"), Arrays.asList("super1")); + + assertEquals("sub_val_1",result.getString("super1","sub_col_1")); + assertEquals("sub_val_2",result.next().getString("super1","sub_col_1")); + + } + } From dc2d226954cb6ed82b1eea7cae88beaed8e2fca2 Mon Sep 17 00:00:00 2001 From: zznate Date: Tue, 5 Apr 2011 00:15:20 -0500 Subject: [PATCH 101/144] added multikey only method --- .../service/template/SuperCfTemplate.java | 10 ++++++---- .../service/template/SuperCfTemplateTest.java | 18 ++++++++++++++++++ 2 files changed, 24 insertions(+), 4 deletions(-) diff --git a/core/src/main/java/me/prettyprint/cassandra/service/template/SuperCfTemplate.java b/core/src/main/java/me/prettyprint/cassandra/service/template/SuperCfTemplate.java index e805dd8b9..45f68e2bb 100644 --- a/core/src/main/java/me/prettyprint/cassandra/service/template/SuperCfTemplate.java +++ b/core/src/main/java/me/prettyprint/cassandra/service/template/SuperCfTemplate.java @@ -140,20 +140,22 @@ public HColumn querySingleSubColumn(K key, valueSerializer.fromByteBuffer(origCol.getValue()), origCol.getClock(), subSerializer, valueSerializer); } - - @SuppressWarnings("unchecked") + public SuperCfResult querySuperColumns(K key, List sColumnNames) { HSlicePredicate workingSlicePredicate = new HSlicePredicate(topSerializer); workingSlicePredicate.setColumnNames(sColumnNames); return doExecuteSlice(key, null, workingSlicePredicate); } - - @SuppressWarnings("unchecked") + public SuperCfResult querySuperColumns(List keys, List sColumnNames) { HSlicePredicate workingSlicePredicate = new HSlicePredicate(topSerializer); workingSlicePredicate.setColumnNames(sColumnNames); return doExecuteMultigetSlice(keys, workingSlicePredicate); } + + public SuperCfResult querySuperColumns(List keys) { + return doExecuteMultigetSlice(keys, activeSlicePredicate); + } public T querySuperColumns(K key, List sColumnNames, SuperCfRowMapper mapper) { diff --git a/core/src/test/java/me/prettyprint/cassandra/service/template/SuperCfTemplateTest.java b/core/src/test/java/me/prettyprint/cassandra/service/template/SuperCfTemplateTest.java index 64037cf85..33917f9d9 100644 --- a/core/src/test/java/me/prettyprint/cassandra/service/template/SuperCfTemplateTest.java +++ b/core/src/test/java/me/prettyprint/cassandra/service/template/SuperCfTemplateTest.java @@ -89,4 +89,22 @@ public void testSuperCfInsertReadMultiKey() { } + @Test + public void testSuperCfInsertReadMultiKeyNoSc() { + SuperCfTemplate sTemplate = + new ThriftSuperCfTemplate(keyspace, "Super1", se, se, se); + SuperCfUpdater sUpdater = sTemplate.createUpdater("skey1","super1"); + sUpdater.setString("sub_col_1", "sub_val_1"); + sUpdater.addKey("skey2"); + sUpdater.addSuperColumn("super1"); + sUpdater.setString("sub_col_1", "sub_val_2"); + sTemplate.update(sUpdater); + + SuperCfResult result = sTemplate.querySuperColumns(Arrays.asList("skey1","skey2")); + + assertEquals("sub_val_1",result.getString("super1","sub_col_1")); + assertEquals("sub_val_2",result.next().getString("super1","sub_col_1")); + + } + } From b59368fd47e8c58caa712c12c5ac36eb25783edc Mon Sep 17 00:00:00 2001 From: zznate Date: Tue, 5 Apr 2011 11:59:28 -0500 Subject: [PATCH 102/144] added overloaded form of single key for all supercols --- .../service/template/SuperCfTemplate.java | 4 ++++ .../service/template/SuperCfTemplateTest.java | 15 +++++++++++++++ 2 files changed, 19 insertions(+) diff --git a/core/src/main/java/me/prettyprint/cassandra/service/template/SuperCfTemplate.java b/core/src/main/java/me/prettyprint/cassandra/service/template/SuperCfTemplate.java index 45f68e2bb..53a994400 100644 --- a/core/src/main/java/me/prettyprint/cassandra/service/template/SuperCfTemplate.java +++ b/core/src/main/java/me/prettyprint/cassandra/service/template/SuperCfTemplate.java @@ -165,6 +165,10 @@ public T querySuperColumns(K key, List sColumnNames, return mapper.mapRow(doExecuteSlice(key, null, workingSlicePredicate)); } + public SuperCfResult querySuperColumns(K key) { + return doExecuteSlice(key, null, activeSlicePredicate); + } + public SuperCfResult querySuperColumn(K key, SN sColumnName) { HSlicePredicate workingSlicePredicate = new HSlicePredicate(topSerializer); workingSlicePredicate.addColumnName(sColumnName); diff --git a/core/src/test/java/me/prettyprint/cassandra/service/template/SuperCfTemplateTest.java b/core/src/test/java/me/prettyprint/cassandra/service/template/SuperCfTemplateTest.java index 33917f9d9..ed5f74044 100644 --- a/core/src/test/java/me/prettyprint/cassandra/service/template/SuperCfTemplateTest.java +++ b/core/src/test/java/me/prettyprint/cassandra/service/template/SuperCfTemplateTest.java @@ -106,5 +106,20 @@ public void testSuperCfInsertReadMultiKeyNoSc() { assertEquals("sub_val_2",result.next().getString("super1","sub_col_1")); } + + @Test + public void testSuperCfKeyOnly() { + SuperCfTemplate sTemplate = + new ThriftSuperCfTemplate(keyspace, "Super1", se, se, se); + SuperCfUpdater sUpdater = sTemplate.createUpdater("skey1","super1"); + sUpdater.setString("sub_col_1", "sub_val_1"); + sUpdater.addSuperColumn("super2"); + sUpdater.setString("sub_col_1", "sub_val_2"); + sTemplate.update(sUpdater); + + SuperCfResult result = sTemplate.querySuperColumns("skey1"); + assertEquals(2, result.getSuperColumns().size()); + + } } From 4b34c5b61fb6dc3a5f381e67d8eb9d33c7b3ed34 Mon Sep 17 00:00:00 2001 From: zznate Date: Tue, 5 Apr 2011 13:47:37 -0500 Subject: [PATCH 103/144] added getActiveSuperCOlumn --- .../cassandra/service/template/SuperCfResult.java | 2 ++ .../cassandra/service/template/SuperCfResultWrapper.java | 6 +++++- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/core/src/main/java/me/prettyprint/cassandra/service/template/SuperCfResult.java b/core/src/main/java/me/prettyprint/cassandra/service/template/SuperCfResult.java index 1bcce8178..c647a5739 100644 --- a/core/src/main/java/me/prettyprint/cassandra/service/template/SuperCfResult.java +++ b/core/src/main/java/me/prettyprint/cassandra/service/template/SuperCfResult.java @@ -47,6 +47,8 @@ public interface SuperCfResult extends ColumnFamilyResult { void applySuperColumn(SN sColumnName); + SN getActiveSuperColumn(); + @Override SuperCfResult next(); } diff --git a/core/src/main/java/me/prettyprint/cassandra/service/template/SuperCfResultWrapper.java b/core/src/main/java/me/prettyprint/cassandra/service/template/SuperCfResultWrapper.java index 95dae8380..2f9079014 100644 --- a/core/src/main/java/me/prettyprint/cassandra/service/template/SuperCfResultWrapper.java +++ b/core/src/main/java/me/prettyprint/cassandra/service/template/SuperCfResultWrapper.java @@ -184,7 +184,11 @@ public ByteBuffer getByteBuffer(SN sColumnName, N columnName) { return extractType(sColumnName, columnName, ByteBufferSerializer.get()); } - + + @Override + public SN getActiveSuperColumn() { + return currentSuperColumn; + } @Override public void applySuperColumn(SN sColumnName) { From ee5e80716dafed38b047661b34a7124af9723a8b Mon Sep 17 00:00:00 2001 From: patricioe Date: Tue, 5 Apr 2011 13:00:35 -0700 Subject: [PATCH 104/144] Add null check for querySingleSubColumn --- .../cassandra/service/template/SuperCfTemplate.java | 9 ++++++++- .../cassandra/service/template/SuperCfTemplateTest.java | 2 ++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/core/src/main/java/me/prettyprint/cassandra/service/template/SuperCfTemplate.java b/core/src/main/java/me/prettyprint/cassandra/service/template/SuperCfTemplate.java index 53a994400..4c0fc7db7 100644 --- a/core/src/main/java/me/prettyprint/cassandra/service/template/SuperCfTemplate.java +++ b/core/src/main/java/me/prettyprint/cassandra/service/template/SuperCfTemplate.java @@ -130,12 +130,19 @@ public int countSubColumns(K key, SN superColumnName, N start, public HColumn querySingleSubColumn(K key, SN columnName, N subColumnName, Serializer valueSerializer) { - SuperCfResult result = doExecuteSlice(key, columnName, activeSlicePredicate); + SuperCfResult result = doExecuteSlice(key, columnName, activeSlicePredicate); + + if (result == null) { + return null; + } + HColumn origCol = result.getColumn(subColumnName); + // TODO make this far less hacky if ( columnName == null || origCol == null ) { return null; } + return new HColumnImpl(subColumnName, valueSerializer.fromByteBuffer(origCol.getValue()), origCol.getClock(), subSerializer, valueSerializer); diff --git a/core/src/test/java/me/prettyprint/cassandra/service/template/SuperCfTemplateTest.java b/core/src/test/java/me/prettyprint/cassandra/service/template/SuperCfTemplateTest.java index ed5f74044..adcaf1459 100644 --- a/core/src/test/java/me/prettyprint/cassandra/service/template/SuperCfTemplateTest.java +++ b/core/src/test/java/me/prettyprint/cassandra/service/template/SuperCfTemplateTest.java @@ -120,6 +120,8 @@ public void testSuperCfKeyOnly() { SuperCfResult result = sTemplate.querySuperColumns("skey1"); assertEquals(2, result.getSuperColumns().size()); + result = sTemplate.querySuperColumns("skey1-non-existing-key"); + assertNull(result.getActiveSuperColumn()); } } From 963314add4cdde45e9541fa18a8ff3ad0d3db47f Mon Sep 17 00:00:00 2001 From: zznate Date: Tue, 5 Apr 2011 15:40:21 -0500 Subject: [PATCH 105/144] added hasResults method to return false on no results --- .../service/template/ColumnFamilyResult.java | 2 ++ .../template/ColumnFamilyResultWrapper.java | 9 +++++++++ .../service/template/SuperCfResultWrapper.java | 9 +++++++-- .../service/template/ColumnFamilyTemplateTest.java | 9 ++++++++- .../service/template/SuperCfTemplateTest.java | 14 +++++++++++--- 5 files changed, 37 insertions(+), 6 deletions(-) diff --git a/core/src/main/java/me/prettyprint/cassandra/service/template/ColumnFamilyResult.java b/core/src/main/java/me/prettyprint/cassandra/service/template/ColumnFamilyResult.java index a081cb714..e81dac7a9 100644 --- a/core/src/main/java/me/prettyprint/cassandra/service/template/ColumnFamilyResult.java +++ b/core/src/main/java/me/prettyprint/cassandra/service/template/ColumnFamilyResult.java @@ -41,5 +41,7 @@ public interface ColumnFamilyResult extends Iterator getColumnNames(); HColumn getColumn(N columnName); + + boolean hasResults(); } diff --git a/core/src/main/java/me/prettyprint/cassandra/service/template/ColumnFamilyResultWrapper.java b/core/src/main/java/me/prettyprint/cassandra/service/template/ColumnFamilyResultWrapper.java index f519d1684..b596eb967 100644 --- a/core/src/main/java/me/prettyprint/cassandra/service/template/ColumnFamilyResultWrapper.java +++ b/core/src/main/java/me/prettyprint/cassandra/service/template/ColumnFamilyResultWrapper.java @@ -31,6 +31,7 @@ public class ColumnFamilyResultWrapper extends AbstractResultWrapper { private Iterator>> rows; private Map.Entry> entry; private ExecutionResult>> executionResult; + private boolean hasEntries; public ColumnFamilyResultWrapper(Serializer keySerializer, Serializer columnNameSerializer, @@ -38,6 +39,7 @@ public ColumnFamilyResultWrapper(Serializer keySerializer, super(keySerializer, columnNameSerializer, executionResult); this.rows = executionResult.get().entrySet().iterator(); next(); + hasEntries = getColumnNames() != null && getColumnNames().size() > 0; } /** @@ -117,4 +119,11 @@ public void remove() { rows.remove(); } + @Override + public boolean hasResults() { + return hasEntries; + } + + + } diff --git a/core/src/main/java/me/prettyprint/cassandra/service/template/SuperCfResultWrapper.java b/core/src/main/java/me/prettyprint/cassandra/service/template/SuperCfResultWrapper.java index 2f9079014..399fefa82 100644 --- a/core/src/main/java/me/prettyprint/cassandra/service/template/SuperCfResultWrapper.java +++ b/core/src/main/java/me/prettyprint/cassandra/service/template/SuperCfResultWrapper.java @@ -13,7 +13,6 @@ import me.prettyprint.cassandra.model.ExecutionResult; import me.prettyprint.cassandra.model.HColumnImpl; -import me.prettyprint.cassandra.model.HSuperColumnImpl; import me.prettyprint.cassandra.serializers.BooleanSerializer; import me.prettyprint.cassandra.serializers.ByteBufferSerializer; import me.prettyprint.cassandra.serializers.BytesArraySerializer; @@ -24,7 +23,6 @@ import me.prettyprint.cassandra.serializers.UUIDSerializer; import me.prettyprint.hector.api.Serializer; import me.prettyprint.hector.api.beans.HColumn; -import me.prettyprint.hector.api.beans.HSuperColumn; import org.apache.cassandra.thrift.Column; import org.apache.cassandra.thrift.ColumnOrSuperColumn; @@ -47,6 +45,7 @@ public class SuperCfResultWrapper extends AbstractResultWrapper imp private List superColumns; private Map> subColumns = new LinkedHashMap>(); private SN currentSuperColumn; + private boolean hasEntries; private Serializer sNameSerializer; @@ -58,6 +57,7 @@ public SuperCfResultWrapper(Serializer keySerializer, this.sNameSerializer = sNameSerializer; this.rows = executionResult.get().entrySet().iterator(); next(); + hasEntries = getSuperColumns() != null && getSuperColumns().size() > 0; } @Override @@ -196,6 +196,11 @@ public void applySuperColumn(SN sColumnName) { this.subColumns = columns.get(currentSuperColumn); } + + @Override + public boolean hasResults() { + return hasEntries; + } diff --git a/core/src/test/java/me/prettyprint/cassandra/service/template/ColumnFamilyTemplateTest.java b/core/src/test/java/me/prettyprint/cassandra/service/template/ColumnFamilyTemplateTest.java index 12f709d7d..665b7807d 100644 --- a/core/src/test/java/me/prettyprint/cassandra/service/template/ColumnFamilyTemplateTest.java +++ b/core/src/test/java/me/prettyprint/cassandra/service/template/ColumnFamilyTemplateTest.java @@ -1,6 +1,6 @@ package me.prettyprint.cassandra.service.template; -import static org.junit.Assert.assertEquals; +import static org.junit.Assert.*; import java.util.Arrays; @@ -61,5 +61,12 @@ public void testQueryMultiget() { wrapper.next(); assertEquals("value3",wrapper.getString("column1")); } + + @Test + public void testHasNoResults() { + ColumnFamilyTemplate template = new ThriftColumnFamilyTemplate(keyspace, "Standard1", se, se, HFactory.createMutator(keyspace, se)); + assertFalse(template.queryColumns("noresults").hasResults()); + + } } diff --git a/core/src/test/java/me/prettyprint/cassandra/service/template/SuperCfTemplateTest.java b/core/src/test/java/me/prettyprint/cassandra/service/template/SuperCfTemplateTest.java index adcaf1459..3087c800d 100644 --- a/core/src/test/java/me/prettyprint/cassandra/service/template/SuperCfTemplateTest.java +++ b/core/src/test/java/me/prettyprint/cassandra/service/template/SuperCfTemplateTest.java @@ -83,7 +83,7 @@ public void testSuperCfInsertReadMultiKey() { sTemplate.update(sUpdater); SuperCfResult result = sTemplate.querySuperColumns(Arrays.asList("skey1","skey2"), Arrays.asList("super1")); - + assertTrue(result.hasResults()); assertEquals("sub_val_1",result.getString("super1","sub_col_1")); assertEquals("sub_val_2",result.next().getString("super1","sub_col_1")); @@ -101,7 +101,7 @@ public void testSuperCfInsertReadMultiKeyNoSc() { sTemplate.update(sUpdater); SuperCfResult result = sTemplate.querySuperColumns(Arrays.asList("skey1","skey2")); - + assertTrue(result.hasResults()); assertEquals("sub_val_1",result.getString("super1","sub_col_1")); assertEquals("sub_val_2",result.next().getString("super1","sub_col_1")); @@ -119,9 +119,17 @@ public void testSuperCfKeyOnly() { SuperCfResult result = sTemplate.querySuperColumns("skey1"); assertEquals(2, result.getSuperColumns().size()); - + assertTrue(result.hasResults()); result = sTemplate.querySuperColumns("skey1-non-existing-key"); assertNull(result.getActiveSuperColumn()); } + @Test + public void testSuperCfNoResults() { + SuperCfTemplate sTemplate = + new ThriftSuperCfTemplate(keyspace, "Super1", se, se, se); + + assertFalse(sTemplate.querySuperColumns("no_results").hasResults()); + } + } From 71bc15fadc629bc821a8ddd3daf1547d5200c380 Mon Sep 17 00:00:00 2001 From: zznate Date: Tue, 5 Apr 2011 17:09:07 -0500 Subject: [PATCH 106/144] removed executiontime checks --- .../me/prettyprint/hector/api/ApiV2SystemTest.java | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/core/src/test/java/me/prettyprint/hector/api/ApiV2SystemTest.java b/core/src/test/java/me/prettyprint/hector/api/ApiV2SystemTest.java index 3093a1b5c..f35da870d 100644 --- a/core/src/test/java/me/prettyprint/hector/api/ApiV2SystemTest.java +++ b/core/src/test/java/me/prettyprint/hector/api/ApiV2SystemTest.java @@ -98,7 +98,6 @@ public void testInsertGetRemoveCounter() { MutationResult mr = m.insertCounter("testInsertGetRemoveCounter", cf, createCounterColumn("testInsertGetRemoveCounter_name", 25)); - assertTrue("Time should be > 0", mr.getExecutionTimeMicro() > 0); log.debug("insert execution time: {}", mr.getExecutionTimeMicro()); // get value @@ -114,13 +113,11 @@ public void testInsertGetRemoveCounter() { assertEquals(25, value.longValue()); String name = c.getName(); assertEquals("testInsertGetRemoveCounter_name", name); - assertEquals(q, r.getQuery()); - assertTrue("Time should be > 0", r.getExecutionTimeMicro() > 0); + assertEquals(q, r.getQuery()); // remove value m = createMutator(ko, se); MutationResult mr2 = m.deleteCounter("testInsertGetRemoveCounter", cf, "testInsertGetRemoveCounter_name", se); - assertTrue("Time should be > 0", mr2.getExecutionTimeMicro() > 0); // get already removed value CounterQuery q2 = createCounterColumnQuery(ko, se, se); @@ -165,7 +162,6 @@ public void testInsertGetRemove() { // Check the mutation result metadata // assertEquals("127.0.0.1:9170", mr.getHostUsed()); - assertTrue("Time should be > 0", mr.getExecutionTimeMicro() > 0); log.debug("insert execution time: {}", mr.getExecutionTimeMicro()); @@ -183,13 +179,13 @@ public void testInsertGetRemove() { String name = c.getName(); assertEquals("testInsertGetRemove", name); assertEquals(q, r.getQuery()); - assertTrue("Time should be > 0", r.getExecutionTimeMicro() > 0); + // remove value m = createMutator(ko, se); MutationResult mr2 = m.delete("testInsertGetRemove", cf, "testInsertGetRemove", se); - assertTrue("Time should be > 0", mr2.getExecutionTimeMicro() > 0); + // get already removed value ColumnQuery q2 = createColumnQuery(ko, se, se, se); From c0e2f7c7fc7823ae9702f8b2b44729ce57b9ba37 Mon Sep 17 00:00:00 2001 From: patricioe Date: Tue, 5 Apr 2011 17:57:06 -0700 Subject: [PATCH 107/144] Add Mutator.superDelete to delte super columns and test --- .../cassandra/model/MutatorImpl.java | 16 ++++++++++++ .../cassandra/model/thrift/ThriftFactory.java | 2 +- .../hector/api/mutation/Mutator.java | 7 ++++++ .../cassandra/model/MutatorTest.java | 25 ++++++++++++++++--- 4 files changed, 46 insertions(+), 4 deletions(-) diff --git a/core/src/main/java/me/prettyprint/cassandra/model/MutatorImpl.java b/core/src/main/java/me/prettyprint/cassandra/model/MutatorImpl.java index f4120eb2c..1ac99400f 100644 --- a/core/src/main/java/me/prettyprint/cassandra/model/MutatorImpl.java +++ b/core/src/main/java/me/prettyprint/cassandra/model/MutatorImpl.java @@ -101,6 +101,21 @@ public Void doInKeyspace(KeyspaceService ks) throws HectorException { })); } + @Override + public MutationResult superDelete(final K key, final String cf, final SN supercolumnName, + final Serializer sNameSerializer) { + return new MutationResultImpl(keyspace.doExecute(new KeyspaceOperationCallback() { + @Override + public Void doInKeyspace(KeyspaceService ks) throws HectorException { + // Remove a Super Column. + ks.remove( + keySerializer.toByteBuffer(key), + ThriftFactory.createSuperColumnPath(cf, supercolumnName, sNameSerializer)); + return null; + } + })); + } + /** * Deletes the columns defined in the HSuperColumn. If there are no HColumns attached, * we delete the whole thing. @@ -338,4 +353,5 @@ public Mutator addCounterSubDeletion(K key, String cf, HCounterSuperC return this; } + } diff --git a/core/src/main/java/me/prettyprint/cassandra/model/thrift/ThriftFactory.java b/core/src/main/java/me/prettyprint/cassandra/model/thrift/ThriftFactory.java index 76e7c4f01..c121a3f57 100644 --- a/core/src/main/java/me/prettyprint/cassandra/model/thrift/ThriftFactory.java +++ b/core/src/main/java/me/prettyprint/cassandra/model/thrift/ThriftFactory.java @@ -46,7 +46,7 @@ public static ColumnPath createSuperColumnPath(String columnFamilyName, return columnPath; } - /*package*/ static ColumnPath createSuperColumnPath(String columnFamilyName, + public static ColumnPath createSuperColumnPath(String columnFamilyName, SN superColumnName, Serializer superNameSerializer) { noneNull(columnFamilyName, superNameSerializer); ColumnPath columnPath = createColumnPath(columnFamilyName, null); diff --git a/core/src/main/java/me/prettyprint/hector/api/mutation/Mutator.java b/core/src/main/java/me/prettyprint/hector/api/mutation/Mutator.java index 6c699a64d..1b35a3f71 100644 --- a/core/src/main/java/me/prettyprint/hector/api/mutation/Mutator.java +++ b/core/src/main/java/me/prettyprint/hector/api/mutation/Mutator.java @@ -39,6 +39,13 @@ MutationResult delete(final K key, final String cf, final N columnName, */ MutationResult subDelete(final K key, final String cf, final SN supercolumnName, final N columnName, final Serializer sNameSerializer, final Serializer nameSerializer); + + /** + * Deletes a supercolumn + * @param super column type + */ + MutationResult superDelete(final K key, final String cf, final SN supercolumnName, + final Serializer sNameSerializer); // schedule an insertion to be executed in batch by the execute method diff --git a/core/src/test/java/me/prettyprint/cassandra/model/MutatorTest.java b/core/src/test/java/me/prettyprint/cassandra/model/MutatorTest.java index 36d306387..8c81b01c8 100644 --- a/core/src/test/java/me/prettyprint/cassandra/model/MutatorTest.java +++ b/core/src/test/java/me/prettyprint/cassandra/model/MutatorTest.java @@ -62,15 +62,34 @@ public void testInsert() { } @Test - public void testInsertSuper() { + public void testInsertAndDeleteSuper() { Mutator m = createMutator(keyspace, se); List> columnList = new ArrayList>(); columnList.add(createColumn("name","value",se,se)); - HSuperColumn superColumn = - createSuperColumn("super_name", columnList, se, se, se); + HSuperColumn superColumn = createSuperColumn("super_name", columnList, se, se, se); + + // Insert Super Column MutationResult r = m.insert("sk", "Super1", superColumn); + assertTrue("Execute time should be > 0", r.getExecutionTimeMicro() > 0); assertTrue("Should have operated on a host", r.getHostUsed() != null); + + // Fetch and verify it exists. + SuperColumnQuery scq = HFactory.createSuperColumnQuery(keyspace, se, se, se, se); + scq.setColumnFamily("Super1"); + scq.setKey("sk"); + scq.setSuperName("super_name"); + assertEquals("super_name", scq.execute().get().getName()); + + // Remove the Super Column + m.superDelete("sk", "Super1", "super_name", se); + + // Fetch and verify it exists. + scq = HFactory.createSuperColumnQuery(keyspace, se, se, se, se); + scq.setColumnFamily("Super1"); + scq.setKey("sk"); + scq.setSuperName("super_name"); + assertNull("super_name", scq.execute().get()); } @Test From 05b38abcde9c2fb6e527238a27451e755d25c921 Mon Sep 17 00:00:00 2001 From: Ed Anuff Date: Tue, 5 Apr 2011 21:15:44 -0700 Subject: [PATCH 108/144] set -Dfile.encoding=UTF-8 in pom.xml so surefire runs tests with utf8 set as default character encoding --- core/pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/pom.xml b/core/pom.xml index ddd741545..12a044abd 100644 --- a/core/pom.xml +++ b/core/pom.xml @@ -86,7 +86,7 @@ maven-surefire-plugin always - -Xmx512M -Xms512M + -Xmx512M -Xms512M -Dfile.encoding=UTF-8 From d00d820e816411891fa8a74cd51c44b53b16ab80 Mon Sep 17 00:00:00 2001 From: Ed Anuff Date: Tue, 5 Apr 2011 21:32:45 -0700 Subject: [PATCH 109/144] set -Dsun.jnu.encoding=UTF-8 in pom.xml --- core/pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/pom.xml b/core/pom.xml index 12a044abd..71f7a9972 100644 --- a/core/pom.xml +++ b/core/pom.xml @@ -86,7 +86,7 @@ maven-surefire-plugin always - -Xmx512M -Xms512M -Dfile.encoding=UTF-8 + -Xmx512M -Xms512M -Dfile.encoding=UTF-8 -Dsun.jnu.encoding=UTF-8 From 9bac88a3cabfa8e27d48a29496e9f132795b68b3 Mon Sep 17 00:00:00 2001 From: Ed Anuff Date: Tue, 5 Apr 2011 21:57:53 -0700 Subject: [PATCH 110/144] added public ComparatorType getComparatorType() to serializer --- .../me/prettyprint/hector/api/Serializer.java | 38 ++++++++++--------- 1 file changed, 21 insertions(+), 17 deletions(-) diff --git a/core/src/main/java/me/prettyprint/hector/api/Serializer.java b/core/src/main/java/me/prettyprint/hector/api/Serializer.java index 1306608a5..dc3bc9cec 100644 --- a/core/src/main/java/me/prettyprint/hector/api/Serializer.java +++ b/core/src/main/java/me/prettyprint/hector/api/Serializer.java @@ -5,41 +5,43 @@ import java.util.Map; import java.util.Set; -import me.prettyprint.cassandra.serializers.StringSerializer; - +import me.prettyprint.hector.api.ddl.ComparatorType; /** * Serializes a type T from the given bytes, or vice a versa. - * - * In cassandra column names and column values (and starting with 0.7.0 row keys) are all byte[]. - * To allow type safe conversion in java and keep all conversion code in one place we define the - * Extractor interface. - * Implementors of the interface define type conversion according to their domains. A predefined - * set of common extractors can be found in the extractors package, for example - * {@link StringSerializer}. - * + * + * In cassandra column names and column values (and starting with 0.7.0 row + * keys) are all byte[]. To allow type safe conversion in java and keep all + * conversion code in one place we define the Extractor interface. Implementors + * of the interface define type conversion according to their domains. A + * predefined set of common extractors can be found in the extractors package, + * for example {@link StringSerializer}. + * * @author Ran Tavory - * - * @param The type to which data extraction should work. + * + * @param + * The type to which data extraction should work. */ public interface Serializer { /** * Extract bytes from the obj of type T + * * @param obj * @return - */ - public ByteBuffer toByteBuffer(T obj); + */ + public ByteBuffer toByteBuffer(T obj); public byte[] toBytes(T obj); - + public T fromBytes(byte[] bytes); - + /** * Extract an object of type T from the bytes. + * * @param bytes * @return - */ + */ public T fromByteBuffer(ByteBuffer byteBuffer); public Set toBytesSet(List list); @@ -54,4 +56,6 @@ public interface Serializer { public List fromBytesList(List list); + public ComparatorType getComparatorType(); + } From 05d108e11471dabbf5a1a2aa4aba71dd4b348921 Mon Sep 17 00:00:00 2001 From: zznate Date: Wed, 6 Apr 2011 03:20:50 -0500 Subject: [PATCH 111/144] added additional deletion signatures for deletion in mutator, usage of such in template --- .../cassandra/model/MutatorImpl.java | 30 +++++++++++++ .../template/AbstractTemplateUpdater.java | 4 +- .../template/SuperCfResultWrapper.java | 2 +- .../service/template/SuperCfUpdater.java | 44 ++++++++++++++----- .../hector/api/mutation/Mutator.java | 19 ++++++-- .../cassandra/model/MutatorTest.java | 25 +++++++++++ .../service/template/SuperCfTemplateTest.java | 26 ++++++----- 7 files changed, 121 insertions(+), 29 deletions(-) diff --git a/core/src/main/java/me/prettyprint/cassandra/model/MutatorImpl.java b/core/src/main/java/me/prettyprint/cassandra/model/MutatorImpl.java index 1ac99400f..c1cd00f0d 100644 --- a/core/src/main/java/me/prettyprint/cassandra/model/MutatorImpl.java +++ b/core/src/main/java/me/prettyprint/cassandra/model/MutatorImpl.java @@ -353,5 +353,35 @@ public Mutator addCounterSubDeletion(K key, String cf, HCounterSuperC return this; } + @Override + public Mutator addSubDelete(K key, String cf, SN sColumnName, + N columnName, Serializer sNameSerializer, Serializer nameSerializer) { + return addSubDelete(key, cf, sColumnName, columnName, sNameSerializer, nameSerializer, keyspace.createClock()); + } + + @Override + public Mutator addSubDelete(K key, String cf, SN sColumnName, + N columnName, Serializer sNameSerializer, Serializer nameSerializer, long clock) { + Deletion d = new Deletion(clock); + SlicePredicate predicate = new SlicePredicate(); + predicate.addToColumn_names(nameSerializer.toByteBuffer(columnName)); + d.setPredicate(predicate); + d.setSuper_column(sNameSerializer.toByteBuffer(sColumnName)); + getPendingMutations().addDeletion(key, Arrays.asList(cf), d); + return this; + } + + + + @Override + public Mutator addSuperDelete(K key, String cf, SN sColumnName, + Serializer sNameSerializer) { + Deletion d = new Deletion(); + d.setSuper_column(sNameSerializer.toByteBuffer(sColumnName)); + getPendingMutations().addDeletion(key, Arrays.asList(cf), d); + return this; + } + + } diff --git a/core/src/main/java/me/prettyprint/cassandra/service/template/AbstractTemplateUpdater.java b/core/src/main/java/me/prettyprint/cassandra/service/template/AbstractTemplateUpdater.java index 74d94afd6..f23ef0938 100644 --- a/core/src/main/java/me/prettyprint/cassandra/service/template/AbstractTemplateUpdater.java +++ b/core/src/main/java/me/prettyprint/cassandra/service/template/AbstractTemplateUpdater.java @@ -7,8 +7,8 @@ public abstract class AbstractTemplateUpdater { - private List keys; - private int keyPos = 0; + protected List keys; + protected int keyPos = 0; protected ColumnFactory columnFactory; protected AbstractColumnFamilyTemplate template; diff --git a/core/src/main/java/me/prettyprint/cassandra/service/template/SuperCfResultWrapper.java b/core/src/main/java/me/prettyprint/cassandra/service/template/SuperCfResultWrapper.java index 399fefa82..6788eade0 100644 --- a/core/src/main/java/me/prettyprint/cassandra/service/template/SuperCfResultWrapper.java +++ b/core/src/main/java/me/prettyprint/cassandra/service/template/SuperCfResultWrapper.java @@ -66,7 +66,7 @@ public SuperCfResult next() { throw new NoSuchElementException("No more rows left on this HColumnFamily"); } entry = rows.next(); - log.debug("found entry{}", getKey()); + log.debug("found entry {} with value {}", getKey(), entry.getValue()); applyToRow(entry.getValue()); return this; } diff --git a/core/src/main/java/me/prettyprint/cassandra/service/template/SuperCfUpdater.java b/core/src/main/java/me/prettyprint/cassandra/service/template/SuperCfUpdater.java index 258556b3e..07cb4e4cb 100644 --- a/core/src/main/java/me/prettyprint/cassandra/service/template/SuperCfUpdater.java +++ b/core/src/main/java/me/prettyprint/cassandra/service/template/SuperCfUpdater.java @@ -7,6 +7,9 @@ import java.util.List; import java.util.UUID; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + import me.prettyprint.cassandra.model.HSuperColumnImpl; import me.prettyprint.cassandra.serializers.BooleanSerializer; import me.prettyprint.cassandra.serializers.ByteBufferSerializer; @@ -53,7 +56,7 @@ * the object instance to persist */ public class SuperCfUpdater extends AbstractTemplateUpdater { - + private static final Logger log = LoggerFactory.getLogger(SuperCfUpdater.class); protected SuperCfTemplate template; private List sColumnNames; private int sColPos; @@ -65,16 +68,33 @@ public SuperCfUpdater(SuperCfTemplate sTemplate, ColumnFactory columnFac this.template = sTemplate; } + + + @Override + public SuperCfUpdater addKey(K key) { + + if ( keys != null && keys.size() > 0 ) { + updateInternal(); + } + super.addKey(key); + sColumnNames = new ArrayList(); + sColPos = 0; + return this; + } + + + public SuperCfUpdater addSuperColumn(SN sColumnName) { - if ( sColumnNames == null ) { - sColumnNames = new ArrayList(); - } else { + if ( sColumnNames.size() > 0 ) { updateInternal(); sColPos++; } + subColumns = new ArrayList(); + sColumnNames.add(sColumnName); + return this; } @@ -85,28 +105,28 @@ public SN getCurrentSuperColumn() { /** * collapse the state of the active HSuperColumn */ - void updateInternal() { + void updateInternal() { // HSuperColumnImpl needs a refactor, this construction is lame. // the value serializer is not used in HSuperColumnImpl, so this is safe for name // TODO need to mod to work with 0 timestamp + log.debug("Adding column {} for key {} and cols {}", new Object[]{getCurrentSuperColumn(), getCurrentKey(), subColumns}); HSuperColumnImpl column = new HSuperColumnImpl(getCurrentSuperColumn(), subColumns, 0, template.getTopSerializer(), template.getSubSerializer(), TypeInferringSerializer.get()); - template.getMutator().addInsertion(getCurrentKey(), template.getColumnFamily(), column); + template.getMutator().addInsertion(getCurrentKey(), template.getColumnFamily(), column); + } /** * Deletes the super column and all of its sub columns */ - public void deleteSuperColumn(SN sColumnName) { + public void deleteSuperColumn() { template.getMutator().addDeletion(getCurrentKey(), template.getColumnFamily(), - sColumnName, template.getTopSerializer()); + getCurrentSuperColumn(), template.getTopSerializer()); } - public void deleteSubColumn(SN sColumnName, N columnName) { + public void deleteSubColumn(N columnName) { template.getMutator().addSubDelete(getCurrentKey(), template.getColumnFamily(), - new HSuperColumnImpl(sColumnName, (List>)Arrays.asList(columnFactory.createColumn(columnName, ByteBuffer.wrap(new byte[]{}), - template.getSubSerializer(), ByteBufferSerializer.get())), template.getEffectiveClock(), - template.getTopSerializer(), template.getSubSerializer(), ByteBufferSerializer.get())); + getCurrentSuperColumn(), columnName, template.getTopSerializer(), template.getSubSerializer()); } public void setString(N subColumnName, String value) { diff --git a/core/src/main/java/me/prettyprint/hector/api/mutation/Mutator.java b/core/src/main/java/me/prettyprint/hector/api/mutation/Mutator.java index 1b35a3f71..fd3a7e01a 100644 --- a/core/src/main/java/me/prettyprint/hector/api/mutation/Mutator.java +++ b/core/src/main/java/me/prettyprint/hector/api/mutation/Mutator.java @@ -41,12 +41,18 @@ MutationResult subDelete(final K key, final String cf, final SN supercol final N columnName, final Serializer sNameSerializer, final Serializer nameSerializer); /** - * Deletes a supercolumn + * Deletes a supercolumn immediately * @param super column type */ - MutationResult superDelete(final K key, final String cf, final SN supercolumnName, - final Serializer sNameSerializer); - + MutationResult superDelete(K key, String cf, SN supercolumnName, + Serializer sNameSerializer); + + /** + * batches a super column for deletion + * + */ + Mutator addSuperDelete(K key, String cf, SN sColumnName, Serializer sNameSerializer); + // schedule an insertion to be executed in batch by the execute method // CAVEAT: a large number of calls with a typo in one of them will leave things in an @@ -112,6 +118,11 @@ MutationResult superDelete(final K key, final String cf, final SN supercolu Mutator addSubDelete(K key, String cf, HSuperColumn sc, long clock); + Mutator addSubDelete(K key, String cf, SN sColumnName, N columnName, Serializer sNameSerializer, Serializer nameSerialer); + + Mutator addSubDelete(K key, String cf, SN sColumnName, N columnName, Serializer sNameSerializer, Serializer nameSerialer, long clock); + + /** * Batch executes all mutations scheduled to this Mutator instance by addInsertion, addDeletion etc. diff --git a/core/src/test/java/me/prettyprint/cassandra/model/MutatorTest.java b/core/src/test/java/me/prettyprint/cassandra/model/MutatorTest.java index 8c81b01c8..6e6785b15 100644 --- a/core/src/test/java/me/prettyprint/cassandra/model/MutatorTest.java +++ b/core/src/test/java/me/prettyprint/cassandra/model/MutatorTest.java @@ -109,6 +109,31 @@ public void testSubDelete() { scq.setSuperName("super_name"); assertEquals(3,scq.execute().get().getColumns().size()); + m.discardPendingMutations(); + + m.addSubDelete("sk1", "Super1", "super_name", "col_1", se, se); + m.execute(); + + assertEquals(2,scq.execute().get().getColumns().size()); + } + + @Test + public void testSubDeleteHSuperColumn() { + Mutator m = createMutator(keyspace, se); + List> columnList = new ArrayList>(); + columnList.add(createColumn("col_1","val_1",se,se)); + columnList.add(createColumn("col_2","val_2",se,se)); + columnList.add(createColumn("col_3","val_3",se,se)); + HSuperColumn superColumn = + createSuperColumn("super_name", columnList, se, se, se); + MutationResult r = m.insert("sk1", "Super1", superColumn); + + SuperColumnQuery scq = HFactory.createSuperColumnQuery(keyspace, se, se, se, se); + scq.setColumnFamily("Super1"); + scq.setKey("sk1"); + scq.setSuperName("super_name"); + assertEquals(3,scq.execute().get().getColumns().size()); + m.discardPendingMutations(); columnList.remove(1); columnList.remove(0); diff --git a/core/src/test/java/me/prettyprint/cassandra/service/template/SuperCfTemplateTest.java b/core/src/test/java/me/prettyprint/cassandra/service/template/SuperCfTemplateTest.java index 3087c800d..6d33042ae 100644 --- a/core/src/test/java/me/prettyprint/cassandra/service/template/SuperCfTemplateTest.java +++ b/core/src/test/java/me/prettyprint/cassandra/service/template/SuperCfTemplateTest.java @@ -22,6 +22,12 @@ public void testSuperCfInsertReadTemplate() { SuperCfResult result = sTemplate.querySuperColumn("skey1", "super1"); assertEquals("sub_val_1",result.getString("super1","sub_col_1")); + + sUpdater.deleteSuperColumn(); + sTemplate.update(sUpdater); + assertEquals("super1",sUpdater.getCurrentSuperColumn()); + result = sTemplate.querySuperColumn("skey1", "super1"); + assertFalse(result.hasResults()); } @@ -75,17 +81,17 @@ public void testQuerySingleSubColumnEmpty() { public void testSuperCfInsertReadMultiKey() { SuperCfTemplate sTemplate = new ThriftSuperCfTemplate(keyspace, "Super1", se, se, se); - SuperCfUpdater sUpdater = sTemplate.createUpdater("skey1","super1"); + SuperCfUpdater sUpdater = sTemplate.createUpdater("s_multi_key1","super1"); sUpdater.setString("sub_col_1", "sub_val_1"); - sUpdater.addKey("skey2"); + sUpdater.addKey("s_multi_key2"); sUpdater.addSuperColumn("super1"); sUpdater.setString("sub_col_1", "sub_val_2"); sTemplate.update(sUpdater); - SuperCfResult result = sTemplate.querySuperColumns(Arrays.asList("skey1","skey2"), Arrays.asList("super1")); + SuperCfResult result = sTemplate.querySuperColumns(Arrays.asList("s_multi_key1","s_multi_key2"), Arrays.asList("super1")); assertTrue(result.hasResults()); - assertEquals("sub_val_1",result.getString("super1","sub_col_1")); - assertEquals("sub_val_2",result.next().getString("super1","sub_col_1")); + assertEquals("sub_val_2",result.getString("super1","sub_col_1")); + assertEquals("sub_val_1",result.next().getString("super1","sub_col_1")); } @@ -93,17 +99,17 @@ public void testSuperCfInsertReadMultiKey() { public void testSuperCfInsertReadMultiKeyNoSc() { SuperCfTemplate sTemplate = new ThriftSuperCfTemplate(keyspace, "Super1", se, se, se); - SuperCfUpdater sUpdater = sTemplate.createUpdater("skey1","super1"); + SuperCfUpdater sUpdater = sTemplate.createUpdater("s_multi_key1","super1"); sUpdater.setString("sub_col_1", "sub_val_1"); - sUpdater.addKey("skey2"); + sUpdater.addKey("s_multi_key2"); sUpdater.addSuperColumn("super1"); sUpdater.setString("sub_col_1", "sub_val_2"); sTemplate.update(sUpdater); - SuperCfResult result = sTemplate.querySuperColumns(Arrays.asList("skey1","skey2")); + SuperCfResult result = sTemplate.querySuperColumns(Arrays.asList("s_multi_key1","s_multi_key2")); assertTrue(result.hasResults()); - assertEquals("sub_val_1",result.getString("super1","sub_col_1")); - assertEquals("sub_val_2",result.next().getString("super1","sub_col_1")); + assertEquals("sub_val_2",result.getString("super1","sub_col_1")); + assertEquals("sub_val_1",result.next().getString("super1","sub_col_1")); } From 0059ab70a92d4336e570fcc6cd5375f1edb6401d Mon Sep 17 00:00:00 2001 From: zznate Date: Wed, 6 Apr 2011 14:16:47 -0500 Subject: [PATCH 112/144] more test coverage of update SC, added timestamps to subcols --- .../service/template/SuperCfUpdater.java | 33 ++++++++++--------- .../service/template/SuperCfTemplateTest.java | 20 +++++++++++ 2 files changed, 37 insertions(+), 16 deletions(-) diff --git a/core/src/main/java/me/prettyprint/cassandra/service/template/SuperCfUpdater.java b/core/src/main/java/me/prettyprint/cassandra/service/template/SuperCfUpdater.java index 07cb4e4cb..3c338f354 100644 --- a/core/src/main/java/me/prettyprint/cassandra/service/template/SuperCfUpdater.java +++ b/core/src/main/java/me/prettyprint/cassandra/service/template/SuperCfUpdater.java @@ -108,11 +108,12 @@ public SN getCurrentSuperColumn() { void updateInternal() { // HSuperColumnImpl needs a refactor, this construction is lame. // the value serializer is not used in HSuperColumnImpl, so this is safe for name - // TODO need to mod to work with 0 timestamp - log.debug("Adding column {} for key {} and cols {}", new Object[]{getCurrentSuperColumn(), getCurrentKey(), subColumns}); - HSuperColumnImpl column = new HSuperColumnImpl(getCurrentSuperColumn(), subColumns, - 0, template.getTopSerializer(), template.getSubSerializer(), TypeInferringSerializer.get()); - template.getMutator().addInsertion(getCurrentKey(), template.getColumnFamily(), column); + if ( !subColumns.isEmpty() ) { + log.debug("Adding column {} for key {} and cols {}", new Object[]{getCurrentSuperColumn(), getCurrentKey(), subColumns}); + HSuperColumnImpl column = new HSuperColumnImpl(getCurrentSuperColumn(), subColumns, + 0, template.getTopSerializer(), template.getSubSerializer(), TypeInferringSerializer.get()); + template.getMutator().addInsertion(getCurrentKey(), template.getColumnFamily(), column); + } } @@ -130,52 +131,52 @@ public void deleteSubColumn(N columnName) { } public void setString(N subColumnName, String value) { - subColumns.add(columnFactory.createColumn(subColumnName, value, - template.getSubSerializer(), StringSerializer.get())); + subColumns.add(columnFactory.createColumn(subColumnName, value, template.getEffectiveClock(), + template.getSubSerializer(), StringSerializer.get() )); } public void setUUID(N subColumnName, UUID value) { - subColumns.add(columnFactory.createColumn(subColumnName, value, + subColumns.add(columnFactory.createColumn(subColumnName, value, template.getEffectiveClock(), template.getSubSerializer(), UUIDSerializer.get())); } public void setLong(N subColumnName, Long value) { - subColumns.add(columnFactory.createColumn(subColumnName, value, + subColumns.add(columnFactory.createColumn(subColumnName, value, template.getEffectiveClock(), template.getSubSerializer(), LongSerializer.get())); } public void setInteger(N subColumnName, Integer value) { - subColumns.add(columnFactory.createColumn(subColumnName, value, + subColumns.add(columnFactory.createColumn(subColumnName, value, template.getEffectiveClock(), template.getSubSerializer(), IntegerSerializer.get())); } public void setBoolean(N subColumnName, Boolean value) { - subColumns.add(columnFactory.createColumn(subColumnName, value, + subColumns.add(columnFactory.createColumn(subColumnName, value, template.getEffectiveClock(), template.getSubSerializer(), BooleanSerializer.get())); } public void setByteArray(N subColumnName, byte[] value) { - subColumns.add(columnFactory.createColumn(subColumnName, value, + subColumns.add(columnFactory.createColumn(subColumnName, value, template.getEffectiveClock(), template.getSubSerializer(), BytesArraySerializer.get())); } public void setByteBuffer(N subColumnName, ByteBuffer value) { - subColumns.add(columnFactory.createColumn(subColumnName, value, + subColumns.add(columnFactory.createColumn(subColumnName, value, template.getEffectiveClock(), template.getSubSerializer(), ByteBufferSerializer.get())); } public void setDate(N subColumnName, Date value) { - subColumns.add(columnFactory.createColumn(subColumnName, value, + subColumns.add(columnFactory.createColumn(subColumnName, value, template.getEffectiveClock(), template.getSubSerializer(), DateSerializer.get())); } public void setDouble(N subColumnName, Double value) { - subColumns.add(columnFactory.createColumn(subColumnName, value, + subColumns.add(columnFactory.createColumn(subColumnName, value, template.getEffectiveClock(), template.getSubSerializer(), DoubleSerializer.get())); } public void setValue(N subColumnName, V value, Serializer serializer) { - subColumns.add(columnFactory.createColumn(subColumnName, value, + subColumns.add(columnFactory.createColumn(subColumnName, value, template.getEffectiveClock(), template.getSubSerializer(), serializer)); } } diff --git a/core/src/test/java/me/prettyprint/cassandra/service/template/SuperCfTemplateTest.java b/core/src/test/java/me/prettyprint/cassandra/service/template/SuperCfTemplateTest.java index 6d33042ae..38e25f3e1 100644 --- a/core/src/test/java/me/prettyprint/cassandra/service/template/SuperCfTemplateTest.java +++ b/core/src/test/java/me/prettyprint/cassandra/service/template/SuperCfTemplateTest.java @@ -138,4 +138,24 @@ public void testSuperCfNoResults() { assertFalse(sTemplate.querySuperColumns("no_results").hasResults()); } + @Test + public void testDeleteSubColumns() { + SuperCfTemplate sTemplate = + new ThriftSuperCfTemplate(keyspace, "Super1", se, se, se); + SuperCfUpdater sUpdater = sTemplate.createUpdater("skey3","super1"); + sUpdater.setString("sub1_col_1", "sub1_val_1"); + sUpdater.setString("sub1_col_2", "sub1_val_2"); + sUpdater.setString("sub1_col_3", "sub1_val_3"); + sTemplate.update(sUpdater); + + SuperCfResult result = sTemplate.querySuperColumn("skey3","super1"); + assertEquals(3, result.getColumnNames().size()); + + sUpdater.deleteSubColumn("sub1_col_1"); + sTemplate.update(sUpdater); + + result = sTemplate.querySuperColumn("skey3","super1"); + assertEquals(2, result.getColumnNames().size()); + } + } From 5a68930a1dece15c7f0ea4d968b0ef8fb83efe1f Mon Sep 17 00:00:00 2001 From: zznate Date: Wed, 6 Apr 2011 15:16:51 -0500 Subject: [PATCH 113/144] tweak to clock on superDelete --- .../java/me/prettyprint/cassandra/model/MutatorImpl.java | 6 ++++-- .../cassandra/service/template/SuperCfUpdater.java | 6 ++++-- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/core/src/main/java/me/prettyprint/cassandra/model/MutatorImpl.java b/core/src/main/java/me/prettyprint/cassandra/model/MutatorImpl.java index c1cd00f0d..92d32b529 100644 --- a/core/src/main/java/me/prettyprint/cassandra/model/MutatorImpl.java +++ b/core/src/main/java/me/prettyprint/cassandra/model/MutatorImpl.java @@ -376,10 +376,12 @@ public Mutator addSubDelete(K key, String cf, SN sColumnName, @Override public Mutator addSuperDelete(K key, String cf, SN sColumnName, Serializer sNameSerializer) { - Deletion d = new Deletion(); + Deletion d = new Deletion(keyspace.createClock()); d.setSuper_column(sNameSerializer.toByteBuffer(sColumnName)); - getPendingMutations().addDeletion(key, Arrays.asList(cf), d); + getPendingMutations().addDeletion(key, Arrays.asList(cf), d); + return this; + } diff --git a/core/src/main/java/me/prettyprint/cassandra/service/template/SuperCfUpdater.java b/core/src/main/java/me/prettyprint/cassandra/service/template/SuperCfUpdater.java index 3c338f354..61d70c035 100644 --- a/core/src/main/java/me/prettyprint/cassandra/service/template/SuperCfUpdater.java +++ b/core/src/main/java/me/prettyprint/cassandra/service/template/SuperCfUpdater.java @@ -121,8 +121,10 @@ void updateInternal() { * Deletes the super column and all of its sub columns */ public void deleteSuperColumn() { - template.getMutator().addDeletion(getCurrentKey(), template.getColumnFamily(), - getCurrentSuperColumn(), template.getTopSerializer()); + //template.getMutator().addDeletion(getCurrentKey(), template.getColumnFamily(), + // getCurrentSuperColumn(), template.getTopSerializer()); + template.getMutator().addSuperDelete(getCurrentKey(), template.getColumnFamily(), + getCurrentSuperColumn(), template.getTopSerializer()); } public void deleteSubColumn(N columnName) { From c2bcc0caad3df3e919828e4afbea4c08b98f84f0 Mon Sep 17 00:00:00 2001 From: zznate Date: Wed, 6 Apr 2011 15:44:14 -0500 Subject: [PATCH 114/144] override template level deletes for supercols, coverage for such --- .../template/SuperCfResultWrapper.java | 2 +- .../service/template/SuperCfTemplate.java | 18 +++++- .../service/template/SuperCfTemplateTest.java | 55 +++++++++++++++++++ 3 files changed, 72 insertions(+), 3 deletions(-) diff --git a/core/src/main/java/me/prettyprint/cassandra/service/template/SuperCfResultWrapper.java b/core/src/main/java/me/prettyprint/cassandra/service/template/SuperCfResultWrapper.java index 6788eade0..c0bce21d7 100644 --- a/core/src/main/java/me/prettyprint/cassandra/service/template/SuperCfResultWrapper.java +++ b/core/src/main/java/me/prettyprint/cassandra/service/template/SuperCfResultWrapper.java @@ -116,7 +116,7 @@ public HColumn getColumn(N columnName) { @Override public Collection getColumnNames() { - return subColumns.keySet(); + return subColumns != null ? subColumns.keySet() : new ArrayList(); } diff --git a/core/src/main/java/me/prettyprint/cassandra/service/template/SuperCfTemplate.java b/core/src/main/java/me/prettyprint/cassandra/service/template/SuperCfTemplate.java index 4c0fc7db7..8e9e25fc7 100644 --- a/core/src/main/java/me/prettyprint/cassandra/service/template/SuperCfTemplate.java +++ b/core/src/main/java/me/prettyprint/cassandra/service/template/SuperCfTemplate.java @@ -197,9 +197,23 @@ public void update(SuperCfUpdater updater) { updater.update(); executeIfNotBatched(); } - + + /** + * Immediately delete the key and superColumn combination + */ + @Override + public void deleteColumn(K key, SN sColumnName) { + mutator.superDelete(key, getColumnFamily(), sColumnName, topSerializer); + } + + /** + * Immediately delete the row + */ + @Override + public void deleteRow(K key) { + mutator.delete(key, getColumnFamily(), null, null); + } - protected abstract SuperCfResult doExecuteSlice(K key, SN sColumnName, HSlicePredicate predicate); protected abstract SuperCfResult doExecuteMultigetSlice(List keys, HSlicePredicate predicate); diff --git a/core/src/test/java/me/prettyprint/cassandra/service/template/SuperCfTemplateTest.java b/core/src/test/java/me/prettyprint/cassandra/service/template/SuperCfTemplateTest.java index 38e25f3e1..31d04e24a 100644 --- a/core/src/test/java/me/prettyprint/cassandra/service/template/SuperCfTemplateTest.java +++ b/core/src/test/java/me/prettyprint/cassandra/service/template/SuperCfTemplateTest.java @@ -158,4 +158,59 @@ public void testDeleteSubColumns() { assertEquals(2, result.getColumnNames().size()); } + @Test + public void testTemplateLevelDeleteSuper() { + SuperCfTemplate sTemplate = + new ThriftSuperCfTemplate(keyspace, "Super1", se, se, se); + SuperCfUpdater sUpdater = sTemplate.createUpdater("skey_del_super","super1"); + sUpdater.setString("sub1_col_1", "sub1_val_1"); + sTemplate.update(sUpdater); + + SuperCfResult result = sTemplate.querySuperColumn("skey_del_super","super1"); + assertEquals(1, result.getColumnNames().size()); + + sTemplate.deleteColumn("skey_del_super", "super1"); + + result = sTemplate.querySuperColumn("skey_del_super","super1"); + assertFalse(result.hasResults()); + assertEquals(0, result.getColumnNames().size()); + } + + @Test + public void testTemplateLevelDeleteRow() { + SuperCfTemplate sTemplate = + new ThriftSuperCfTemplate(keyspace, "Super1", se, se, se); + SuperCfUpdater sUpdater = sTemplate.createUpdater("skey_row_del","super1"); + sUpdater.setString("sub1_col_1", "sub1_val_1"); + sTemplate.update(sUpdater); + + SuperCfResult result = sTemplate.querySuperColumn("skey_row_del","super1"); + assertEquals(1, result.getColumnNames().size()); + + sTemplate.deleteRow("skey_row_del"); + + result = sTemplate.querySuperColumns("skey_row_del"); + assertFalse(result.hasResults()); + assertEquals(0, result.getSuperColumns().size()); + } + + @Test + public void testTemplateLevelDeleteMiss() { + SuperCfTemplate sTemplate = + new ThriftSuperCfTemplate(keyspace, "Super1", se, se, se); + SuperCfUpdater sUpdater = sTemplate.createUpdater("skey_row_del_miss","super1"); + sUpdater.setString("sub1_col_1", "sub1_val_1"); + sTemplate.update(sUpdater); + + SuperCfResult result = sTemplate.querySuperColumn("skey_row_del_miss","super1"); + assertEquals(1, result.getColumnNames().size()); + + sTemplate.deleteRow("skey_row_miss_foo"); + sTemplate.deleteColumn("skey_row_del", "foo"); + + result = sTemplate.querySuperColumns("skey_row_del_miss"); + assertTrue(result.hasResults()); + assertEquals(1, result.getSuperColumns().size()); + } + } From d555ecf6c85c17eb01a5037abd06cf3a39f178ac Mon Sep 17 00:00:00 2001 From: zznate Date: Fri, 8 Apr 2011 09:53:23 -0500 Subject: [PATCH 115/144] added describeSchemaVersions --- .../cassandra/service/ThriftCluster.java | 14 ++++++++++++++ .../java/me/prettyprint/hector/api/Cluster.java | 2 ++ 2 files changed, 16 insertions(+) diff --git a/core/src/main/java/me/prettyprint/cassandra/service/ThriftCluster.java b/core/src/main/java/me/prettyprint/cassandra/service/ThriftCluster.java index c6663704a..b47e3100e 100644 --- a/core/src/main/java/me/prettyprint/cassandra/service/ThriftCluster.java +++ b/core/src/main/java/me/prettyprint/cassandra/service/ThriftCluster.java @@ -36,6 +36,20 @@ public List execute(Cassandra.Client cassandra) throws HectorExcepti return op.getResult(); } + public Map> describeSchemaVersions() throws HectorException { + Operation>> op = new Operation>>(OperationType.META_READ, getCredentials()) { + @Override + public Map> execute(Cassandra.Client cassandra) throws HectorException { + try { + return cassandra.describe_schema_versions(); + } catch (Exception e) { + throw xtrans.translate(e); + } + } + }; + connectionManager.operateWithFailover(op); + return op.getResult(); + } @Override diff --git a/core/src/main/java/me/prettyprint/hector/api/Cluster.java b/core/src/main/java/me/prettyprint/hector/api/Cluster.java index e95b43a0e..fa0a9fd63 100644 --- a/core/src/main/java/me/prettyprint/hector/api/Cluster.java +++ b/core/src/main/java/me/prettyprint/hector/api/Cluster.java @@ -41,6 +41,8 @@ public interface Cluster { String describeClusterName() throws HectorException; String describeThriftVersion() throws HectorException; + + Map> describeSchemaVersions() throws HectorException; KeyspaceDefinition describeKeyspace(final String keyspace) throws HectorException; From a819b82b8f62bec6a5471abf24c89a72aa0b94ff Mon Sep 17 00:00:00 2001 From: Ed Anuff Date: Sat, 9 Apr 2011 16:29:05 -0700 Subject: [PATCH 116/144] Added support for new UUIDType from https://issues.apache.org/jira/browse/CASSANDRA-2233 --- .../cassandra/serializers/UUIDSerializer.java | 4 +-- .../hector/api/beans/AbstractComposite.java | 26 +++++-------------- .../hector/api/ddl/ComparatorType.java | 4 ++- .../prettyprint/hector/api/CompositeTest.java | 8 +++--- 4 files changed, 15 insertions(+), 27 deletions(-) diff --git a/core/src/main/java/me/prettyprint/cassandra/serializers/UUIDSerializer.java b/core/src/main/java/me/prettyprint/cassandra/serializers/UUIDSerializer.java index 198c57db5..ab25a9408 100644 --- a/core/src/main/java/me/prettyprint/cassandra/serializers/UUIDSerializer.java +++ b/core/src/main/java/me/prettyprint/cassandra/serializers/UUIDSerializer.java @@ -1,6 +1,6 @@ package me.prettyprint.cassandra.serializers; -import static me.prettyprint.hector.api.ddl.ComparatorType.LEXICALUUIDTYPE; +import static me.prettyprint.hector.api.ddl.ComparatorType.UUIDTYPE; import java.nio.ByteBuffer; import java.util.UUID; @@ -50,7 +50,7 @@ public UUID fromByteBuffer(ByteBuffer bytes) { @Override public ComparatorType getComparatorType() { - return LEXICALUUIDTYPE; + return UUIDTYPE; } } diff --git a/core/src/main/java/me/prettyprint/hector/api/beans/AbstractComposite.java b/core/src/main/java/me/prettyprint/hector/api/beans/AbstractComposite.java index 4c95a5869..ef3f41475 100644 --- a/core/src/main/java/me/prettyprint/hector/api/beans/AbstractComposite.java +++ b/core/src/main/java/me/prettyprint/hector/api/beans/AbstractComposite.java @@ -7,6 +7,7 @@ import static me.prettyprint.hector.api.ddl.ComparatorType.LONGTYPE; import static me.prettyprint.hector.api.ddl.ComparatorType.TIMEUUIDTYPE; import static me.prettyprint.hector.api.ddl.ComparatorType.UTF8TYPE; +import static me.prettyprint.hector.api.ddl.ComparatorType.UUIDTYPE; import java.math.BigInteger; import java.nio.ByteBuffer; @@ -18,7 +19,6 @@ import java.util.Iterator; import java.util.List; import java.util.Map; -import java.util.UUID; import java.util.logging.Logger; import me.prettyprint.cassandra.serializers.AsciiSerializer; @@ -103,7 +103,8 @@ public static ComponentEquality fromByte(byte equality) { .put((byte) 'x', LEXICALUUIDTYPE.getTypeName()) .put((byte) 'l', LONGTYPE.getTypeName()) .put((byte) 't', TIMEUUIDTYPE.getTypeName()) - .put((byte) 's', UTF8TYPE.getTypeName()).build(); + .put((byte) 's', UTF8TYPE.getTypeName()) + .put((byte) 'u', UUIDTYPE.getTypeName()).build(); BiMap, String> serializerToComparatorMapping = DEFAULT_SERIALIZER_TO_COMPARATOR_MAPPING; @@ -301,13 +302,6 @@ private String comparatorForSerializer(Serializer s) { return BYTESTYPE.getTypeName(); } - private String comparatorForUUID(UUID uuid) { - if (uuid.version() == 1) { - return TIMEUUIDTYPE.getTypeName(); - } - return LEXICALUUIDTYPE.getTypeName(); - } - private Serializer serializerForComparator(String c) { int p = c.indexOf('('); if (p >= 0) { @@ -405,9 +399,7 @@ public int size() { public AbstractComposite addComponent(T value, Serializer s) { - addComponent(value, s, - value instanceof UUID ? comparatorForUUID((UUID) value) - : comparatorForSerializer(s)); + addComponent(value, s, comparatorForSerializer(s)); return this; @@ -524,8 +516,7 @@ public void add(int index, Object element) { } String c = comparatorForPosition(index); if (c == null) { - c = element instanceof UUID ? comparatorForUUID((UUID) element) - : comparatorForSerializer(s); + c = comparatorForSerializer(s); } components.add(index, new Component(element, null, s, c, ComponentEquality.EQUAL)); @@ -543,9 +534,7 @@ public Object remove(int index) { public AbstractComposite setComponent(int index, T value, Serializer s) { - setComponent(index, value, s, - value instanceof UUID ? comparatorForUUID((UUID) value) - : comparatorForSerializer(s)); + setComponent(index, value, s, comparatorForSerializer(s)); return this; @@ -594,8 +583,7 @@ public Object set(int index, Object element) { } String c = comparatorForPosition(index); if (c == null) { - c = element instanceof UUID ? comparatorForUUID((UUID) element) - : comparatorForSerializer(s); + c = comparatorForSerializer(s); } Component prev = components.set(index, new Component(element, null, s, c, ComponentEquality.EQUAL)); diff --git a/core/src/main/java/me/prettyprint/hector/api/ddl/ComparatorType.java b/core/src/main/java/me/prettyprint/hector/api/ddl/ComparatorType.java index fac4ef6da..62130693e 100644 --- a/core/src/main/java/me/prettyprint/hector/api/ddl/ComparatorType.java +++ b/core/src/main/java/me/prettyprint/hector/api/ddl/ComparatorType.java @@ -25,10 +25,12 @@ public class ComparatorType { "org.apache.cassandra.db.marshal.CompositeType"); public static ComparatorType DYNAMICCOMPOSITETYPE = new ComparatorType( "org.apache.cassandra.db.marshal.DynamicCompositeType"); + public static ComparatorType UUIDTYPE = new ComparatorType( + "org.apache.cassandra.db.marshal.UUIDType"); private static ComparatorType[] values = { ASCIITYPE, BYTESTYPE, INTEGERTYPE, LEXICALUUIDTYPE, LOCALBYPARTITIONERTYPE, LONGTYPE, TIMEUUIDTYPE, - UTF8TYPE, COMPOSITETYPE, DYNAMICCOMPOSITETYPE }; + UTF8TYPE, COMPOSITETYPE, DYNAMICCOMPOSITETYPE, UUIDTYPE }; private final String className; private final String typeName; diff --git a/core/src/test/java/me/prettyprint/hector/api/CompositeTest.java b/core/src/test/java/me/prettyprint/hector/api/CompositeTest.java index aad265731..d7eab4fa0 100644 --- a/core/src/test/java/me/prettyprint/hector/api/CompositeTest.java +++ b/core/src/test/java/me/prettyprint/hector/api/CompositeTest.java @@ -1,7 +1,6 @@ package me.prettyprint.hector.api; -import static me.prettyprint.hector.api.ddl.ComparatorType.LEXICALUUIDTYPE; -import static me.prettyprint.hector.api.ddl.ComparatorType.TIMEUUIDTYPE; +import static me.prettyprint.hector.api.ddl.ComparatorType.UUIDTYPE; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertTrue; @@ -63,8 +62,7 @@ public void testDynamicSerialization() throws Exception { c = DynamicComposite.fromByteBuffer(b); o = c.get(0); assertTrue(o instanceof UUID); - assertEquals(LEXICALUUIDTYPE.getTypeName(), c.getComponent(0) - .getComparator()); + assertEquals(UUIDTYPE.getTypeName(), c.getComponent(0).getComparator()); // test serialization and deserialization of time-based UUIDS c = new DynamicComposite(); @@ -73,7 +71,7 @@ public void testDynamicSerialization() throws Exception { c = DynamicComposite.fromByteBuffer(b); o = c.get(0); assertTrue(o instanceof UUID); - assertEquals(TIMEUUIDTYPE.getTypeName(), c.getComponent(0).getComparator()); + assertEquals(UUIDTYPE.getTypeName(), c.getComponent(0).getComparator()); // test compatibility with Cassandra unit tests b = createDynamicCompositeKey("Hello", From 481b2ba2e966c9bc2554843e0155ccc546fb66db Mon Sep 17 00:00:00 2001 From: Ed Anuff Date: Sat, 9 Apr 2011 18:57:31 -0700 Subject: [PATCH 117/144] bug fix for null composite value --- .../hector/api/beans/AbstractComposite.java | 19 ++++++++++++------- .../prettyprint/hector/api/CompositeTest.java | 19 +++++++++++++++++++ 2 files changed, 31 insertions(+), 7 deletions(-) diff --git a/core/src/main/java/me/prettyprint/hector/api/beans/AbstractComposite.java b/core/src/main/java/me/prettyprint/hector/api/beans/AbstractComposite.java index ef3f41475..e50da8c69 100644 --- a/core/src/main/java/me/prettyprint/hector/api/beans/AbstractComposite.java +++ b/core/src/main/java/me/prettyprint/hector/api/beans/AbstractComposite.java @@ -162,15 +162,20 @@ public ByteBuffer getBytes(Serializer s) { if (value instanceof ByteBuffer) { return ((ByteBuffer) value).duplicate(); } - if (value != null) { - if (s == null) { - s = (Serializer) serializer; - } - if (s != null) { - return s.toByteBuffer((A) value).duplicate(); - } + + if (value == null) { + return null; + } + + if (s == null) { + s = (Serializer) serializer; + } + if (s != null) { + return s.toByteBuffer((A) value).duplicate(); } + } + return bytes.duplicate(); } diff --git a/core/src/test/java/me/prettyprint/hector/api/CompositeTest.java b/core/src/test/java/me/prettyprint/hector/api/CompositeTest.java index d7eab4fa0..72128199a 100644 --- a/core/src/test/java/me/prettyprint/hector/api/CompositeTest.java +++ b/core/src/test/java/me/prettyprint/hector/api/CompositeTest.java @@ -2,6 +2,7 @@ import static me.prettyprint.hector.api.ddl.ComparatorType.UUIDTYPE; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; import static org.junit.Assert.assertTrue; import java.math.BigInteger; @@ -11,6 +12,7 @@ import me.prettyprint.cassandra.serializers.BigIntegerSerializer; import me.prettyprint.cassandra.serializers.ByteBufferSerializer; +import me.prettyprint.cassandra.serializers.DynamicCompositeSerializer; import me.prettyprint.cassandra.serializers.StringSerializer; import me.prettyprint.cassandra.serializers.UUIDSerializer; import me.prettyprint.cassandra.utils.TimeUUIDUtils; @@ -140,6 +142,23 @@ public void testDynamicSerialization() throws Exception { UTF8Type.instance.validate(b); } + @Test + public void testNullValueSerialization() throws Exception { + + // test correct serialization with null values and user specified + // serialization + DynamicComposite c = new DynamicComposite(); + c.addComponent(null, StringSerializer.get()); + + DynamicCompositeSerializer serializer = new DynamicCompositeSerializer(); + + ByteBuffer buff = serializer.toByteBuffer(c); + + DynamicComposite result = serializer.fromByteBuffer(buff); + + assertNull(result.get(0)); + } + @Test public void testStaticSerialization() throws Exception { From 15ddb03463e7bc1863cf75e7c25290e82009e749 Mon Sep 17 00:00:00 2001 From: zznate Date: Thu, 14 Apr 2011 16:46:54 -0500 Subject: [PATCH 118/144] fix getExhaustedPoolNames, was returning an empty set --- .../prettyprint/cassandra/service/CassandraClientMonitor.java | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/core/src/main/java/me/prettyprint/cassandra/service/CassandraClientMonitor.java b/core/src/main/java/me/prettyprint/cassandra/service/CassandraClientMonitor.java index 427bc4c7e..5c8b9068a 100644 --- a/core/src/main/java/me/prettyprint/cassandra/service/CassandraClientMonitor.java +++ b/core/src/main/java/me/prettyprint/cassandra/service/CassandraClientMonitor.java @@ -110,7 +110,9 @@ public long getNumPoolExhaustedEventCount() { @Override public Set getExhaustedPoolNames() { Set ret = new HashSet(); - + for ( CassandraHost host : connectionManager.getDownedHosts() ) { + ret.add(host.toString()); + } return ret; } From 1e917aa46a0fa7e49af2ea57782f4e98d17f0058 Mon Sep 17 00:00:00 2001 From: zznate Date: Fri, 15 Apr 2011 12:57:33 -0500 Subject: [PATCH 119/144] updated to latest 0.8 rev, converted mutators to simpler API, fixed KsDef issue from CASSANDRA-2486 --- core/pom.xml | 5 +- .../cassandra/model/MutatorImpl.java | 23 +++--- .../thrift/ThriftCounterColumnQuery.java | 15 +--- .../cassandra/service/BatchMutation.java | 78 +++---------------- .../cassandra/service/KeyspaceService.java | 5 +- .../service/KeyspaceServiceImpl.java | 15 ++-- .../cassandra/service/ThriftKsDef.java | 25 +++--- .../testutils/EmbeddedSchemaLoader.java | 12 +-- .../prettyprint/cassandra/io/StreamTest.java | 5 +- .../cassandra/model/MutatorTest.java | 12 +-- .../cassandra/service/BatchMutationTest.java | 32 +++----- .../service/CassandraClusterTest.java | 2 +- .../cassandra/service/KeyspaceTest.java | 14 ++-- 13 files changed, 83 insertions(+), 160 deletions(-) diff --git a/core/pom.xml b/core/pom.xml index 71f7a9972..b21b6314f 100644 --- a/core/pom.xml +++ b/core/pom.xml @@ -105,16 +105,17 @@ org.apache.cassandra apache-cassandra - 0.8.0-20110317 + 0.8.0-20110415 org.apache.cassandra apache-cassandra-thrift - 0.8.0-20110317 + 0.8.0-20110415 org.apache.cassandra.deps libthrift + 0.6.0 org.yaml diff --git a/core/src/main/java/me/prettyprint/cassandra/model/MutatorImpl.java b/core/src/main/java/me/prettyprint/cassandra/model/MutatorImpl.java index 92d32b529..9b88c9978 100644 --- a/core/src/main/java/me/prettyprint/cassandra/model/MutatorImpl.java +++ b/core/src/main/java/me/prettyprint/cassandra/model/MutatorImpl.java @@ -17,7 +17,6 @@ import me.prettyprint.hector.api.mutation.Mutator; import org.apache.cassandra.thrift.ColumnParent; -import org.apache.cassandra.thrift.CounterDeletion; import org.apache.cassandra.thrift.Deletion; import org.apache.cassandra.thrift.SlicePredicate; @@ -127,7 +126,7 @@ public Mutator addSubDelete(K key, String cf, HSuperColumn s public Mutator addSubDelete(K key, String cf, HSuperColumn sc, long clock) { SlicePredicate pred = new SlicePredicate(); - Deletion d = new Deletion(clock); + Deletion d = new Deletion().setTimestamp(clock); if ( sc.getColumns() != null ) { for (HColumn col : sc.getColumns()) { pred.addToColumn_names(col.getNameSerializer().toByteBuffer(col.getName())); @@ -197,9 +196,9 @@ public Mutator addDeletion(K key, String cf, N columnName, Serializer Deletion d; if ( columnName != null ) { sp.addToColumn_names(nameSerializer.toByteBuffer(columnName)); - d = new Deletion(clock).setPredicate(sp); + d = new Deletion().setTimestamp(clock).setPredicate(sp); } else { - d = new Deletion(clock); + d = new Deletion().setTimestamp(clock); } getPendingMutations().addDeletion(key, Arrays.asList(cf), d); return this; @@ -321,14 +320,14 @@ public Mutator addCounter(K key, String cf, HCounterSuperColumn Mutator addCounterDeletion(K key, String cf, N counterColumnName, Serializer nameSerializer) { SlicePredicate sp = new SlicePredicate(); - CounterDeletion d; + Deletion d; if ( counterColumnName != null ) { sp.addToColumn_names(nameSerializer.toByteBuffer(counterColumnName)); - d = new CounterDeletion().setPredicate(sp); + d = new Deletion().setPredicate(sp); } else { - d = new CounterDeletion(); + d = new Deletion(); } - getPendingMutations().addCounterDeletion(key, Arrays.asList(cf), d); + getPendingMutations().addDeletion(key, Arrays.asList(cf), d); return this; } @@ -341,7 +340,7 @@ public Mutator addCounterDeletion(K key, String cf) { @Override public Mutator addCounterSubDeletion(K key, String cf, HCounterSuperColumn sc) { SlicePredicate pred = new SlicePredicate(); - CounterDeletion d = new CounterDeletion(); + Deletion d = new Deletion(); if ( sc.getColumns() != null ) { for (HCounterColumn col : sc.getColumns()) { pred.addToColumn_names(col.getNameSerializer().toByteBuffer(col.getName())); @@ -349,7 +348,7 @@ public Mutator addCounterSubDeletion(K key, String cf, HCounterSuperC d.setPredicate(pred); } d.setSuper_column(sc.getNameByteBuffer()); - getPendingMutations().addCounterDeletion(key, Arrays.asList(cf), d); + getPendingMutations().addDeletion(key, Arrays.asList(cf), d); return this; } @@ -362,7 +361,7 @@ public Mutator addSubDelete(K key, String cf, SN sColumnName, @Override public Mutator addSubDelete(K key, String cf, SN sColumnName, N columnName, Serializer sNameSerializer, Serializer nameSerializer, long clock) { - Deletion d = new Deletion(clock); + Deletion d = new Deletion().setTimestamp(clock); SlicePredicate predicate = new SlicePredicate(); predicate.addToColumn_names(nameSerializer.toByteBuffer(columnName)); d.setPredicate(predicate); @@ -376,7 +375,7 @@ public Mutator addSubDelete(K key, String cf, SN sColumnName, @Override public Mutator addSuperDelete(K key, String cf, SN sColumnName, Serializer sNameSerializer) { - Deletion d = new Deletion(keyspace.createClock()); + Deletion d = new Deletion().setTimestamp(keyspace.createClock()); d.setSuper_column(sNameSerializer.toByteBuffer(sColumnName)); getPendingMutations().addDeletion(key, Arrays.asList(cf), d); diff --git a/core/src/main/java/me/prettyprint/cassandra/model/thrift/ThriftCounterColumnQuery.java b/core/src/main/java/me/prettyprint/cassandra/model/thrift/ThriftCounterColumnQuery.java index 3446ebf45..6fef3e8d7 100644 --- a/core/src/main/java/me/prettyprint/cassandra/model/thrift/ThriftCounterColumnQuery.java +++ b/core/src/main/java/me/prettyprint/cassandra/model/thrift/ThriftCounterColumnQuery.java @@ -1,29 +1,20 @@ package me.prettyprint.cassandra.model.thrift; import me.prettyprint.cassandra.model.AbstractBasicQuery; -import me.prettyprint.cassandra.model.AbstractColumnQuery; -import me.prettyprint.cassandra.model.AbstractQuery; -import me.prettyprint.cassandra.model.ExecutingKeyspace; -import me.prettyprint.cassandra.model.HColumnImpl; import me.prettyprint.cassandra.model.HCounterColumnImpl; import me.prettyprint.cassandra.model.KeyspaceOperationCallback; import me.prettyprint.cassandra.model.QueryResultImpl; -import me.prettyprint.cassandra.serializers.LongSerializer; -import me.prettyprint.cassandra.serializers.StringSerializer; import me.prettyprint.cassandra.serializers.TypeInferringSerializer; import me.prettyprint.cassandra.service.KeyspaceService; import me.prettyprint.hector.api.Keyspace; import me.prettyprint.hector.api.Serializer; -import me.prettyprint.hector.api.beans.HColumn; import me.prettyprint.hector.api.beans.HCounterColumn; import me.prettyprint.hector.api.exceptions.HNotFoundException; import me.prettyprint.hector.api.exceptions.HectorException; -import me.prettyprint.hector.api.query.ColumnQuery; import me.prettyprint.hector.api.query.CounterQuery; import me.prettyprint.hector.api.query.QueryResult; -import org.apache.cassandra.thrift.Column; -import org.apache.cassandra.thrift.Counter; +import org.apache.cassandra.thrift.CounterColumn; /** * Thrift implementation of the ColumnQuery type. @@ -74,9 +65,9 @@ public QueryResult> execute() { @Override public HCounterColumn doInKeyspace(KeyspaceService ks) throws HectorException { try { - Counter thriftCounter = ks.getCounter(keySerializer.toByteBuffer(key), + CounterColumn thriftCounter = ks.getCounter(keySerializer.toByteBuffer(key), ThriftFactory.createColumnPath(columnFamilyName, name, columnNameSerializer)); - return new HCounterColumnImpl(thriftCounter.getColumn(), columnNameSerializer); + return new HCounterColumnImpl(thriftCounter, columnNameSerializer); } catch (HNotFoundException e) { return null; } diff --git a/core/src/main/java/me/prettyprint/cassandra/service/BatchMutation.java b/core/src/main/java/me/prettyprint/cassandra/service/BatchMutation.java index 971dee33b..e15ffd412 100644 --- a/core/src/main/java/me/prettyprint/cassandra/service/BatchMutation.java +++ b/core/src/main/java/me/prettyprint/cassandra/service/BatchMutation.java @@ -11,10 +11,7 @@ import org.apache.cassandra.thrift.Column; import org.apache.cassandra.thrift.ColumnOrSuperColumn; -import org.apache.cassandra.thrift.Counter; import org.apache.cassandra.thrift.CounterColumn; -import org.apache.cassandra.thrift.CounterDeletion; -import org.apache.cassandra.thrift.CounterMutation; import org.apache.cassandra.thrift.CounterSuperColumn; import org.apache.cassandra.thrift.Deletion; import org.apache.cassandra.thrift.Mutation; @@ -35,20 +32,16 @@ public final class BatchMutation { private final Map>> mutationMap; - private Map>> countersMutationMap; private final Serializer keySerializer; public BatchMutation(Serializer serializer) { this.keySerializer = serializer; mutationMap = new HashMap>>(); - countersMutationMap = null; } - private BatchMutation(Serializer serializer, Map>> mutationMap, - Map>> countersMutationMap) { + private BatchMutation(Serializer serializer, Map>> mutationMap) { this.keySerializer = serializer; this.mutationMap = mutationMap; - this.countersMutationMap = countersMutationMap; } /** @@ -77,11 +70,9 @@ public BatchMutation addSuperInsertion(K key, List columnFamilies, * Add a ColumnCounter insertion (or update) */ public BatchMutation addCounterInsertion(K key, List columnFamilies, CounterColumn counterColumn) { - Counter counter = new Counter(); - counter.setColumn(counterColumn); - CounterMutation mutation = new CounterMutation(); - mutation.setCounter(counter); - addCounterMutation(key, columnFamilies, mutation); + Mutation mutation = new Mutation(); + mutation.setColumn_or_supercolumn(new ColumnOrSuperColumn().setCounter_column(counterColumn)); + addMutation(key, columnFamilies, mutation); return this; } @@ -90,11 +81,9 @@ public BatchMutation addCounterInsertion(K key, List columnFamilies, */ public BatchMutation addSuperCounterInsertion(K key, List columnFamilies, CounterSuperColumn counterSuperColumn) { - Counter counter = new Counter(); - counter.setSuper_column(counterSuperColumn); - CounterMutation mutation = new CounterMutation(); - mutation.setCounter(counter); - addCounterMutation(key, columnFamilies, mutation); + Mutation mutation = new Mutation(); + mutation.setColumn_or_supercolumn(new ColumnOrSuperColumn().setCounter_super_column(counterSuperColumn)); + addMutation(key, columnFamilies, mutation); return this; } @@ -108,15 +97,6 @@ public BatchMutation addDeletion(K key, List columnFamilies, Deletion return this; } - /** - * Add a counterDeletion request to the batch mutation. - */ - public BatchMutation addCounterDeletion(K key, List columnFamilies, CounterDeletion deletion) { - CounterMutation mutation = new CounterMutation(); - mutation.setDeletion(deletion); - addCounterMutation(key, columnFamilies, mutation); - return this; - } private void addMutation(K key, List columnFamilies, Mutation mutation) { Map> innerMutationMap = getInnerMutationMap(key); @@ -132,19 +112,7 @@ private void addMutation(K key, List columnFamilies, Mutation mutation) mutationMap.put(keySerializer.toByteBuffer(key), innerMutationMap); } - private void addCounterMutation(K key, List columnFamilies, CounterMutation mutation) { - Map> innerMutationMap = getCountersInnerMutationMap(key); - for (String columnFamily : columnFamilies) { - if (innerMutationMap.get(columnFamily) == null) { - innerMutationMap.put(columnFamily, Arrays.asList(mutation)); - } else { - List mutations = new ArrayList(innerMutationMap.get(columnFamily)); - mutations.add(mutation); - innerMutationMap.put(columnFamily, mutations); - } - } - getCreateCounterMutationMap().put(keySerializer.toByteBuffer(key), innerMutationMap); - } + private Map> getInnerMutationMap(K key) { Map> innerMutationMap = mutationMap.get(keySerializer.toByteBuffer(key)); @@ -154,42 +122,16 @@ private Map> getInnerMutationMap(K key) { return innerMutationMap; } - private Map> getCountersInnerMutationMap(K key) { - Map> innerMutationMap = getCreateCounterMutationMap().get(keySerializer.toByteBuffer(key)); - if (innerMutationMap == null) { - innerMutationMap = new HashMap>(); - } - return innerMutationMap; - } - - /** - * Assuming that not all the operations are with counter, we create it on demand. - * @return - */ - private Map>> getCreateCounterMutationMap() { - if (countersMutationMap == null) { - countersMutationMap = new HashMap>>(); - } - return countersMutationMap; - } - Map>> getMutationMap() { return mutationMap; } - - /** - * Note it can be NULL ! - */ - Map>> getCounterMutationMap() { - return countersMutationMap; - } /** * Makes a shallow copy of the mutation object. * @return */ public BatchMutation makeCopy() { - return new BatchMutation(keySerializer, mutationMap, countersMutationMap); + return new BatchMutation(keySerializer, mutationMap); } /** @@ -197,6 +139,6 @@ public BatchMutation makeCopy() { * @return */ public boolean isEmpty() { - return mutationMap.isEmpty() && (countersMutationMap == null || countersMutationMap.isEmpty()) ; + return mutationMap.isEmpty() ; } } diff --git a/core/src/main/java/me/prettyprint/cassandra/service/KeyspaceService.java b/core/src/main/java/me/prettyprint/cassandra/service/KeyspaceService.java index 1ab4fad68..d22ae55df 100644 --- a/core/src/main/java/me/prettyprint/cassandra/service/KeyspaceService.java +++ b/core/src/main/java/me/prettyprint/cassandra/service/KeyspaceService.java @@ -11,7 +11,6 @@ import org.apache.cassandra.thrift.Column; import org.apache.cassandra.thrift.ColumnParent; import org.apache.cassandra.thrift.ColumnPath; -import org.apache.cassandra.thrift.Counter; import org.apache.cassandra.thrift.CounterColumn; import org.apache.cassandra.thrift.IndexClause; import org.apache.cassandra.thrift.KeyRange; @@ -57,9 +56,9 @@ public interface KeyspaceService { * @throws HNotFoundException * if no value exists for the counter */ - Counter getCounter(ByteBuffer key, ColumnPath columnPath) throws HectorException; + CounterColumn getCounter(ByteBuffer key, ColumnPath columnPath) throws HectorException; - Counter getCounter(String key, ColumnPath columnPath) throws HectorException; + CounterColumn getCounter(String key, ColumnPath columnPath) throws HectorException; /** * Get the SuperColumn at the given columnPath. diff --git a/core/src/main/java/me/prettyprint/cassandra/service/KeyspaceServiceImpl.java b/core/src/main/java/me/prettyprint/cassandra/service/KeyspaceServiceImpl.java index 097d720fc..c15cab68b 100644 --- a/core/src/main/java/me/prettyprint/cassandra/service/KeyspaceServiceImpl.java +++ b/core/src/main/java/me/prettyprint/cassandra/service/KeyspaceServiceImpl.java @@ -22,7 +22,6 @@ import org.apache.cassandra.thrift.ColumnParent; import org.apache.cassandra.thrift.ColumnPath; import org.apache.cassandra.thrift.ConsistencyLevel; -import org.apache.cassandra.thrift.Counter; import org.apache.cassandra.thrift.CounterColumn; import org.apache.cassandra.thrift.IndexClause; import org.apache.cassandra.thrift.KeyRange; @@ -656,21 +655,21 @@ public Column execute(Cassandra.Client cassandra) throws HectorException { } @Override - public Counter getCounter(final ByteBuffer key, final ColumnPath columnPath) throws HectorException { - Operation op = new Operation(OperationType.READ, failoverPolicy, keyspaceName, credentials) { + public CounterColumn getCounter(final ByteBuffer key, final ColumnPath columnPath) throws HectorException { + Operation op = new Operation(OperationType.READ, failoverPolicy, keyspaceName, credentials) { @Override - public Counter execute(Cassandra.Client cassandra) throws HectorException { - Counter cosc; + public CounterColumn execute(Cassandra.Client cassandra) throws HectorException { + ColumnOrSuperColumn cosc; try { - cosc = cassandra.get_counter(key, columnPath, getThriftCl(OperationType.READ)); + cosc = cassandra.get(key, columnPath, getThriftCl(OperationType.READ)); } catch (NotFoundException e) { setException(xtrans.translate(e)); return null; } catch (Exception e) { throw xtrans.translate(e); } - return cosc; + return cosc.getCounter_column(); } }; @@ -682,7 +681,7 @@ public Counter execute(Cassandra.Client cassandra) throws HectorException { } @Override - public Counter getCounter(String key, ColumnPath columnPath) throws HectorException { + public CounterColumn getCounter(String key, ColumnPath columnPath) throws HectorException { return getCounter(StringSerializer.get().toByteBuffer(key), columnPath); } diff --git a/core/src/main/java/me/prettyprint/cassandra/service/ThriftKsDef.java b/core/src/main/java/me/prettyprint/cassandra/service/ThriftKsDef.java index 489e47ccd..8904f7e86 100644 --- a/core/src/main/java/me/prettyprint/cassandra/service/ThriftKsDef.java +++ b/core/src/main/java/me/prettyprint/cassandra/service/ThriftKsDef.java @@ -2,6 +2,7 @@ import java.util.ArrayList; import java.util.Collections; +import java.util.HashMap; import java.util.List; import java.util.Map; @@ -12,22 +13,23 @@ import org.apache.cassandra.thrift.KsDef; import org.apache.commons.lang.builder.ToStringBuilder; import org.apache.commons.lang.builder.ToStringStyle; +import org.apache.commons.lang.math.NumberUtils; public class ThriftKsDef implements KeyspaceDefinition { + private static final String REPLICATION_FACTOR = "replication_factor"; public static final String DEF_STRATEGY_CLASS = "org.apache.cassandra.locator.SimpleStrategy"; private final String name; private String strategyClass; - private Map strategyOptions; - private int replicationFactor; + private Map strategyOptions = new HashMap(); private final List cfDefs; public ThriftKsDef(KsDef k) { Assert.notNull(k, "KsDef is null"); name = k.name; strategyClass = k.strategy_class; - strategyOptions = k.strategy_options; - replicationFactor = k.replication_factor; + strategyOptions = k.strategy_options != null ? k.strategy_options : new HashMap(); + setReplicationFactor(NumberUtils.toInt(strategyOptions.get(REPLICATION_FACTOR), 1)); cfDefs = ThriftCfDef.fromThriftList(k.cf_defs); } @@ -35,14 +37,14 @@ public ThriftKsDef(String keyspaceName, String strategyClass, int replicationFac List cfDefs) { this.name = keyspaceName; this.strategyClass = strategyClass; - this.replicationFactor = replicationFactor; + setReplicationFactor(replicationFactor); this.cfDefs = cfDefs; } public ThriftKsDef(String keyspaceName) { this.name = keyspaceName; this.cfDefs = new ArrayList(); - this.replicationFactor = 1; + setReplicationFactor(1); this.strategyClass = DEF_STRATEGY_CLASS; } @@ -50,7 +52,7 @@ public ThriftKsDef(KeyspaceDefinition keyspaceDefinition) { name = keyspaceDefinition.getName(); strategyClass = keyspaceDefinition.getStrategyClass(); strategyOptions = keyspaceDefinition.getStrategyOptions(); - replicationFactor = keyspaceDefinition.getReplicationFactor(); + setReplicationFactor(keyspaceDefinition.getReplicationFactor()); cfDefs = keyspaceDefinition.getCfDefs(); } @@ -82,7 +84,7 @@ public Map getStrategyOptions() { @Override public int getReplicationFactor() { - return replicationFactor; + return NumberUtils.toInt(strategyOptions.get(REPLICATION_FACTOR), 1); } @Override @@ -91,7 +93,9 @@ public List getCfDefs() { } public KsDef toThrift() { - return new KsDef(name, strategyClass, replicationFactor, ThriftCfDef.toThriftList(cfDefs)); + KsDef def = new KsDef(name, strategyClass, ThriftCfDef.toThriftList(cfDefs)); + def.setStrategy_options(strategyOptions); + return def; } public void setStrategyClass(String strategyClass) { @@ -103,7 +107,8 @@ public void setStrategyOptions(Map strategyOptions) { } public void setReplicationFactor(int replicationFactor) { - this.replicationFactor = replicationFactor; + // Compensate for CASSANDRA-1263 (wasnt my idea) + strategyOptions.put(REPLICATION_FACTOR,Integer.toString(replicationFactor)); } @Override diff --git a/core/src/main/java/me/prettyprint/cassandra/testutils/EmbeddedSchemaLoader.java b/core/src/main/java/me/prettyprint/cassandra/testutils/EmbeddedSchemaLoader.java index f9cacd76e..c0b3da05d 100644 --- a/core/src/main/java/me/prettyprint/cassandra/testutils/EmbeddedSchemaLoader.java +++ b/core/src/main/java/me/prettyprint/cassandra/testutils/EmbeddedSchemaLoader.java @@ -50,12 +50,9 @@ public static Collection schemaDefinition() { String ks2 = "Keyspace2"; Class simple = SimpleStrategy.class; - Map no_opts = Collections. emptyMap(); - int rep_factor1 = 1; - int rep_factor2 = 2; - int rep_factor3 = 3; - int rep_factor5 = 5; - + Map opts = new HashMap(); + opts.put("replication_factor", Integer.toString(1)); + ColumnFamilyType st = ColumnFamilyType.Standard; ColumnFamilyType su = ColumnFamilyType.Super; AbstractType bytes = BytesType.instance; @@ -64,8 +61,7 @@ public static Collection schemaDefinition() { schema.add(new KSMetaData( ks1, simple, - no_opts, - rep_factor1, + opts, // Column Families standardCFMD(ks1, "Standard1"), standardCFMD(ks1, "Standard2"), diff --git a/core/src/test/java/me/prettyprint/cassandra/io/StreamTest.java b/core/src/test/java/me/prettyprint/cassandra/io/StreamTest.java index c1e83778a..19d67217b 100644 --- a/core/src/test/java/me/prettyprint/cassandra/io/StreamTest.java +++ b/core/src/test/java/me/prettyprint/cassandra/io/StreamTest.java @@ -31,7 +31,7 @@ public class StreamTest extends BaseEmbededServerSetupTest { BLOB_CF_DEF.comparator_type = "IntegerType"; } - public final static KeyspaceDefinition KEYSPACE_DEV = new ThriftKsDef(new KsDef(KEYSPACE, "org.apache.cassandra.locator.SimpleStrategy", 1, Arrays.asList(new CfDef[] { BLOB_CF_DEF }))); + public static KeyspaceDefinition KEYSPACE_DEV; private Keyspace keyspace; private ThriftCluster cassandraCluster; @@ -40,7 +40,8 @@ public class StreamTest extends BaseEmbededServerSetupTest { @Before public void setUp() throws Exception { super.setupClient(); - + KEYSPACE_DEV = new ThriftKsDef(new KsDef(KEYSPACE, "org.apache.cassandra.locator.SimpleStrategy", Arrays.asList(new CfDef[] { BLOB_CF_DEF }))); + ((ThriftKsDef)KEYSPACE_DEV).setReplicationFactor(1); cassandraHostConfigurator = new CassandraHostConfigurator("localhost:9170"); cassandraCluster = new ThriftCluster("Test Cluster", cassandraHostConfigurator); diff --git a/core/src/test/java/me/prettyprint/cassandra/model/MutatorTest.java b/core/src/test/java/me/prettyprint/cassandra/model/MutatorTest.java index 6e6785b15..b5f8c9f57 100644 --- a/core/src/test/java/me/prettyprint/cassandra/model/MutatorTest.java +++ b/core/src/test/java/me/prettyprint/cassandra/model/MutatorTest.java @@ -7,15 +7,12 @@ 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 static org.junit.Assert.*; import java.util.ArrayList; import java.util.List; import me.prettyprint.cassandra.BaseEmbededServerSetupTest; +import me.prettyprint.cassandra.model.thrift.ThriftCounterColumnQuery; import me.prettyprint.cassandra.serializers.StringSerializer; import me.prettyprint.cassandra.utils.StringUtils; import me.prettyprint.hector.api.Cluster; @@ -26,6 +23,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.CounterQuery; import me.prettyprint.hector.api.query.QueryResult; import me.prettyprint.hector.api.query.SuperColumnQuery; @@ -218,7 +216,9 @@ public void testInsertCounter() { MutationResult mr = m.insertCounter("k", "Counter1", createCounterColumn("name", 5)); assertTrue("Execution time on single counter insert should be > 0", mr.getExecutionTimeMicro() > 0); assertTrue("Should have operated on a host", mr.getHostUsed() != null); - assertColumnExists("Keyspace1", "Counter1", "k", "name"); + CounterQuery counter = new ThriftCounterColumnQuery(keyspace, se, se); + counter.setColumnFamily("Counter1").setKey("k").setName("name"); + assertEquals(new Long(5), counter.execute().get().getValue()); } private void assertColumnExists(String keyspace, String cf, String key, String column) { diff --git a/core/src/test/java/me/prettyprint/cassandra/service/BatchMutationTest.java b/core/src/test/java/me/prettyprint/cassandra/service/BatchMutationTest.java index 6c89cb5f8..af527df5f 100644 --- a/core/src/test/java/me/prettyprint/cassandra/service/BatchMutationTest.java +++ b/core/src/test/java/me/prettyprint/cassandra/service/BatchMutationTest.java @@ -11,12 +11,9 @@ import java.util.Map; import me.prettyprint.cassandra.serializers.StringSerializer; -import me.prettyprint.hector.api.query.SuperCountQuery; import org.apache.cassandra.thrift.Column; import org.apache.cassandra.thrift.CounterColumn; -import org.apache.cassandra.thrift.CounterDeletion; -import org.apache.cassandra.thrift.CounterMutation; import org.apache.cassandra.thrift.CounterSuperColumn; import org.apache.cassandra.thrift.Deletion; import org.apache.cassandra.thrift.Mutation; @@ -72,7 +69,7 @@ public void testAddSuperInsertion() { @Test public void testAddDeletion() { - Deletion deletion = new Deletion(System.currentTimeMillis()); + Deletion deletion = new Deletion().setTimestamp(System.currentTimeMillis()); SlicePredicate slicePredicate = new SlicePredicate(); slicePredicate.addToColumn_names(StringSerializer.get().toByteBuffer("c_name")); deletion.setPredicate(slicePredicate); @@ -80,7 +77,7 @@ public void testAddDeletion() { assertEquals(1,batchMutate.getMutationMap().get(StringSerializer.get().toByteBuffer("key1")).size()); - deletion = new Deletion(System.currentTimeMillis()); + deletion = new Deletion().setTimestamp(System.currentTimeMillis()); slicePredicate = new SlicePredicate(); slicePredicate.addToColumn_names(StringSerializer.get().toByteBuffer("c_name2")); deletion.setPredicate(slicePredicate); @@ -105,13 +102,8 @@ public void testIsEmpty() { batchMutate.addCounterInsertion("key1", columnFamilies, cc1); assertFalse(batchMutate.isEmpty()); - // Delete the Column/SuperColumn mutations and it should be still not empty - batchMutate.getMutationMap().clear(); - assertFalse(batchMutate.isEmpty()); - // Delete the Counter mutations. Now the batchMutate should be empty. - batchMutate.getCounterMutationMap().clear(); - assertTrue(batchMutate.isEmpty()); + } // ********** Test Counters related operations ****************** @@ -125,7 +117,7 @@ public void testAddCounterInsertion() { batchMutate.addCounterInsertion("key1", columnFamilies, cc1); // assert there is one outter map row with 'key' as the key - Map>> mutationMap = batchMutate.getCounterMutationMap(); + Map>> mutationMap = batchMutate.getMutationMap(); assertEquals(1, mutationMap.get(StringSerializer.get().toByteBuffer("key1")).size()); // add again with a different counter and verify there is one key and two mutations underneath @@ -138,22 +130,22 @@ public void testAddCounterInsertion() { @Test public void testAddCounterDeletion() { - CounterDeletion counterDeletion = new CounterDeletion(); + Deletion counterDeletion = new Deletion(); SlicePredicate slicePredicate = new SlicePredicate(); slicePredicate.addToColumn_names(StringSerializer.get().toByteBuffer("c_name")); counterDeletion.setPredicate(slicePredicate); - batchMutate.addCounterDeletion("key1", columnFamilies, counterDeletion); + batchMutate.addDeletion("key1", columnFamilies, counterDeletion); - assertEquals(1, batchMutate.getCounterMutationMap().get(StringSerializer.get().toByteBuffer("key1")).size()); + assertEquals(1, batchMutate.getMutationMap().get(StringSerializer.get().toByteBuffer("key1")).size()); - counterDeletion = new CounterDeletion(); + counterDeletion = new Deletion(); slicePredicate = new SlicePredicate(); slicePredicate.addToColumn_names(StringSerializer.get().toByteBuffer("c_name2")); counterDeletion.setPredicate(slicePredicate); - batchMutate.addCounterDeletion("key1", columnFamilies, counterDeletion); - assertEquals(2,batchMutate.getCounterMutationMap().get(StringSerializer.get().toByteBuffer("key1")).get("Standard1").size()); + batchMutate.addDeletion("key1", columnFamilies, counterDeletion); + assertEquals(2,batchMutate.getMutationMap().get(StringSerializer.get().toByteBuffer("key1")).get("Standard1").size()); } @Test @@ -164,13 +156,13 @@ public void testAddSuperCounterInsertion() { batchMutate.addSuperCounterInsertion("key1", columnFamilies, csc1); // assert there is one outter map row with 'key' as the key - assertEquals(1, batchMutate.getCounterMutationMap().get(StringSerializer.get().toByteBuffer("key1")).size()); + assertEquals(1, batchMutate.getMutationMap().get(StringSerializer.get().toByteBuffer("key1")).size()); // add again with a different column and verify there is one key and two mutations underneath // for "standard1" CounterSuperColumn csc2 = new CounterSuperColumn(StringSerializer.get().toByteBuffer("c_name2"), Arrays.asList(new CounterColumn(StringSerializer.get().toByteBuffer("c_name"), 456))); batchMutate.addSuperCounterInsertion("key1", columnFamilies, csc2); - assertEquals(2, batchMutate.getCounterMutationMap().get(StringSerializer.get().toByteBuffer("key1")).get("Standard1").size()); + assertEquals(2, batchMutate.getMutationMap().get(StringSerializer.get().toByteBuffer("key1")).get("Standard1").size()); } } diff --git a/core/src/test/java/me/prettyprint/cassandra/service/CassandraClusterTest.java b/core/src/test/java/me/prettyprint/cassandra/service/CassandraClusterTest.java index e3d95b80c..5b334f552 100644 --- a/core/src/test/java/me/prettyprint/cassandra/service/CassandraClusterTest.java +++ b/core/src/test/java/me/prettyprint/cassandra/service/CassandraClusterTest.java @@ -60,7 +60,7 @@ public void testDescribeClusterName() throws Exception { */ @Test public void testDescribeThriftVersion() throws Exception { - assertEquals("19.4.0",cassandraCluster.describeThriftVersion()); + assertEquals("19.10.0",cassandraCluster.describeThriftVersion()); } @Test diff --git a/core/src/test/java/me/prettyprint/cassandra/service/KeyspaceTest.java b/core/src/test/java/me/prettyprint/cassandra/service/KeyspaceTest.java index 9414a36bf..e74ef9f54 100644 --- a/core/src/test/java/me/prettyprint/cassandra/service/KeyspaceTest.java +++ b/core/src/test/java/me/prettyprint/cassandra/service/KeyspaceTest.java @@ -28,8 +28,6 @@ import org.apache.cassandra.thrift.ColumnOrSuperColumn; import org.apache.cassandra.thrift.ColumnParent; import org.apache.cassandra.thrift.ColumnPath; -//import org.apache.cassandra.thrift.ConsistencyLevel; -import org.apache.cassandra.thrift.Counter; import org.apache.cassandra.thrift.CounterColumn; import org.apache.cassandra.thrift.Deletion; import org.apache.cassandra.thrift.KeyRange; @@ -120,14 +118,14 @@ public void testInsertAndGetAndRemoveCounter() throws IllegalArgumentException, ColumnPath cph = new ColumnPath("Counter1"); cph.setColumn(ss.toByteBuffer("A")); - Counter counter = keyspace.getCounter("testInsertAndGetAndRemoveCounter_key1", cph); + CounterColumn counter = keyspace.getCounter("testInsertAndGetAndRemoveCounter_key1", cph); assertNotNull(counter); - assertEquals(4, counter.getColumn().value); + assertEquals(4, counter.value); cph.setColumn(ss.toByteBuffer("B")); counter = keyspace.getCounter("testInsertAndGetAndRemoveCounter_key1", cph); assertNotNull(counter); - assertEquals(10, counter.getColumn().value); + assertEquals(10, counter.value); // Reuse the ColumnPath associated to Conter B and remove only B. keyspace.removeCounter("testInsertAndGetAndRemoveCounter_key1", cph); @@ -287,7 +285,7 @@ public void testBatchMutate() throws HectorException { slicePredicate.addToColumn_names(StringSerializer.get().toByteBuffer("testBatchMutateColumn_" + j)); } Mutation mutation = new Mutation(); - Deletion deletion = new Deletion(connectionManager.createClock()); + Deletion deletion = new Deletion().setTimestamp(connectionManager.createClock()); deletion.setPredicate(slicePredicate); mutation.setDeletion(deletion); mutations.add(mutation); @@ -345,7 +343,7 @@ public void testBatchMutateBatchMutation() throws HectorException { for (int j = 0; j < 10; j++) { slicePredicate.addToColumn_names(StringSerializer.get().toByteBuffer("testBatchMutateColumn_" + j)); } - Deletion deletion = new Deletion(connectionManager.createClock()); + Deletion deletion = new Deletion().setTimestamp(connectionManager.createClock()); deletion.setPredicate(slicePredicate); batchMutation.addDeletion("testBatchMutateColumn_" + i, columnFamilies, deletion); } @@ -390,7 +388,7 @@ public void testBatchUpdateInsertAndDelOnSame() throws HectorException { SlicePredicate slicePredicate = new SlicePredicate(); slicePredicate.addToColumn_names(StringSerializer.get().toByteBuffer("deleteThroughInserBatch_col")); - Deletion deletion = new Deletion(connectionManager.createClock()); + Deletion deletion = new Deletion().setTimestamp(connectionManager.createClock()); deletion.setPredicate(slicePredicate); batchMutation.addDeletion("deleteThroughInserBatch_key", columnFamilies, deletion); From 14319170f6bf183b2533908beb1f1356fe0f26de Mon Sep 17 00:00:00 2001 From: patricioe Date: Sun, 17 Apr 2011 21:53:18 -0700 Subject: [PATCH 120/144] add get_slice for counters. --- .../cassandra/model/CounterSliceImpl.java | 51 ++++++++++++ .../model/thrift/ThriftSliceCounterQuery.java | 81 +++++++++++++++++++ .../cassandra/service/KeyspaceService.java | 18 ++++- .../service/KeyspaceServiceImpl.java | 43 +++++++++- .../service/VirtualKeyspaceServiceImpl.java | 8 ++ .../hector/api/beans/CounterSlice.java | 21 +++++ .../hector/api/factory/HFactory.java | 7 ++ .../hector/api/query/SliceCounterQuery.java | 22 +++++ .../cassandra/service/KeyspaceTest.java | 52 +++++++++++- .../hector/api/ApiV2SystemTest.java | 46 +++++++++++ 10 files changed, 345 insertions(+), 4 deletions(-) create mode 100644 core/src/main/java/me/prettyprint/cassandra/model/CounterSliceImpl.java create mode 100644 core/src/main/java/me/prettyprint/cassandra/model/thrift/ThriftSliceCounterQuery.java create mode 100644 core/src/main/java/me/prettyprint/hector/api/beans/CounterSlice.java create mode 100644 core/src/main/java/me/prettyprint/hector/api/query/SliceCounterQuery.java diff --git a/core/src/main/java/me/prettyprint/cassandra/model/CounterSliceImpl.java b/core/src/main/java/me/prettyprint/cassandra/model/CounterSliceImpl.java new file mode 100644 index 000000000..7cea60229 --- /dev/null +++ b/core/src/main/java/me/prettyprint/cassandra/model/CounterSliceImpl.java @@ -0,0 +1,51 @@ +package me.prettyprint.cassandra.model; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import me.prettyprint.cassandra.utils.Assert; +import me.prettyprint.hector.api.Serializer; +import me.prettyprint.hector.api.beans.CounterSlice; +import me.prettyprint.hector.api.beans.HCounterColumn; + +import org.apache.cassandra.thrift.CounterColumn; + +public final class CounterSliceImpl implements CounterSlice { + + private final Map> columnsMap; + private final List> columnsList; + + public CounterSliceImpl(List tColumns, Serializer nameSerializer) { + + Assert.noneNull(tColumns, nameSerializer); + columnsMap = new HashMap>(tColumns.size()); + List> list = new ArrayList>(tColumns.size()); + for (CounterColumn c: tColumns) { + HCounterColumn column = new HCounterColumnImpl(c, nameSerializer); + columnsMap.put(column.getName(), column); + list.add(column); + } + columnsList = list; + } + + /** + * + * @return an unmodifiable list of the columns + */ + @Override + public List> getColumns() { + return columnsList; + } + + @Override + public HCounterColumn getColumnByName(N columnName) { + return columnsMap.get(columnName); + } + + @Override + public String toString() { + return String.format("ColumnSlice(%s)", columnsList.toString()); + } +} diff --git a/core/src/main/java/me/prettyprint/cassandra/model/thrift/ThriftSliceCounterQuery.java b/core/src/main/java/me/prettyprint/cassandra/model/thrift/ThriftSliceCounterQuery.java new file mode 100644 index 000000000..5aaf448cc --- /dev/null +++ b/core/src/main/java/me/prettyprint/cassandra/model/thrift/ThriftSliceCounterQuery.java @@ -0,0 +1,81 @@ +package me.prettyprint.cassandra.model.thrift; + +import java.util.List; + +import me.prettyprint.cassandra.model.AbstractSliceQuery; +import me.prettyprint.cassandra.model.CounterSliceImpl; +import me.prettyprint.cassandra.model.KeyspaceOperationCallback; +import me.prettyprint.cassandra.model.QueryResultImpl; +import me.prettyprint.cassandra.serializers.LongSerializer; +import me.prettyprint.cassandra.service.KeyspaceService; +import me.prettyprint.hector.api.Keyspace; +import me.prettyprint.hector.api.Serializer; +import me.prettyprint.hector.api.beans.CounterSlice; +import me.prettyprint.hector.api.exceptions.HectorException; +import me.prettyprint.hector.api.query.QueryResult; +import me.prettyprint.hector.api.query.SliceCounterQuery; + +import org.apache.cassandra.thrift.ColumnParent; +import org.apache.cassandra.thrift.CounterColumn; + +/** + * A query for the thrift call get_slice + * + * @author patricioe (Patricio Echague) + * + * @param + */ +public final class ThriftSliceCounterQuery extends AbstractSliceQuery> + implements SliceCounterQuery { + + private K key; + + public ThriftSliceCounterQuery(Keyspace k, + Serializer keySerializer, + Serializer nameSerializer) { + // The reason of Longserializer is just to + super(k, keySerializer, nameSerializer, LongSerializer.get()); + } + + @Override + public SliceCounterQuery setKey(K key) { + this.key = key; + return this; + } + + @Override + public QueryResult> execute() { + return new QueryResultImpl>(keyspace.doExecute( + new KeyspaceOperationCallback>() { + @Override + public CounterSlice doInKeyspace(KeyspaceService ks) throws HectorException { + ColumnParent columnParent = new ColumnParent(columnFamilyName); + List thriftRet = ks.getCounterSlice(keySerializer.toByteBuffer(key), columnParent, getPredicate()); + return new CounterSliceImpl(thriftRet, columnNameSerializer); + } + }), this); + } + + @Override + public String toString() { + return "CounterSliceQuery(" + key + "," + toStringInternal() + ")"; + } + + @SuppressWarnings("unchecked") + @Override + public SliceCounterQuery setColumnNames(N... columnNames) { + return (SliceCounterQuery) super.setColumnNames(columnNames); + } + + @SuppressWarnings("unchecked") + @Override + public SliceCounterQuery setRange(N start, N finish, boolean reversed, int count) { + return (SliceCounterQuery) super.setRange(start, finish, reversed, count); + } + + @SuppressWarnings("unchecked") + @Override + public SliceCounterQuery setColumnFamily(String cf) { + return (SliceCounterQuery) super.setColumnFamily(cf); + } +} diff --git a/core/src/main/java/me/prettyprint/cassandra/service/KeyspaceService.java b/core/src/main/java/me/prettyprint/cassandra/service/KeyspaceService.java index d22ae55df..0cdabdd8b 100644 --- a/core/src/main/java/me/prettyprint/cassandra/service/KeyspaceService.java +++ b/core/src/main/java/me/prettyprint/cassandra/service/KeyspaceService.java @@ -102,9 +102,23 @@ SuperColumn getSuperColumn(ByteBuffer key, ColumnPath columnPath, boolean revers */ List getSlice(ByteBuffer key, ColumnParent columnParent, SlicePredicate predicate) throws HectorException; - + List getSlice(String key, ColumnParent columnParent, SlicePredicate predicate) - throws HectorException; + throws HectorException; + + /** + * Get the group of counter columns contained by columnParent. + * + * Returns Either a ColumnFamily name or a ColumnFamily/SuperColumn specified + * by the given predicate. If no matching values are found, an empty list is + * returned. + */ + List getCounterSlice(ByteBuffer key, ColumnParent columnParent, SlicePredicate predicate) + throws HectorException; + + public List getCounterSlice(String key, ColumnParent columnParent, SlicePredicate predicate) + throws HectorException; + /** * Get the group of superColumn contained by columnParent. diff --git a/core/src/main/java/me/prettyprint/cassandra/service/KeyspaceServiceImpl.java b/core/src/main/java/me/prettyprint/cassandra/service/KeyspaceServiceImpl.java index c15cab68b..bf55b5e34 100644 --- a/core/src/main/java/me/prettyprint/cassandra/service/KeyspaceServiceImpl.java +++ b/core/src/main/java/me/prettyprint/cassandra/service/KeyspaceServiceImpl.java @@ -228,12 +228,53 @@ public List execute(Cassandra.Client cassandra) throws HectorException { operateWithFailover(op); return op.getResult(); } - + @Override public List getSlice(String key, ColumnParent columnParent, SlicePredicate predicate) throws HectorException { return getSlice(StringSerializer.get().toByteBuffer(key), columnParent, predicate); } + + @Override + public List getCounterSlice(final ByteBuffer key, final ColumnParent columnParent, + final SlicePredicate predicate) throws HectorException { + Operation> op = + new Operation>(OperationType.READ, failoverPolicy, keyspaceName, credentials) { + + @Override + public List execute(Cassandra.Client cassandra) throws HectorException { + try { + List cosclist = cassandra.get_slice(key, columnParent, + predicate, getThriftCl(OperationType.READ)); + + if (cosclist == null) { + return null; + } + ArrayList result = new ArrayList(cosclist.size()); + for (ColumnOrSuperColumn cosc : cosclist) { + if (cosc.isSetCounter_column()) { + result.add(cosc.getCounter_column()); + } else { + // Inconsistency + throw new HectorException("Regular Column is part of the set of Counter Column"); + } + + } + return result; + } catch (Exception e) { + throw xtrans.translate(e); + } + } + }; + operateWithFailover(op); + return op.getResult(); + } + + @Override + public List getCounterSlice(String key, ColumnParent columnParent, SlicePredicate predicate) + throws HectorException { + return getCounterSlice(StringSerializer.get().toByteBuffer(key), columnParent, predicate); + } @Override public SuperColumn getSuperColumn(final ByteBuffer key, final ColumnPath columnPath) throws HectorException { diff --git a/core/src/main/java/me/prettyprint/cassandra/service/VirtualKeyspaceServiceImpl.java b/core/src/main/java/me/prettyprint/cassandra/service/VirtualKeyspaceServiceImpl.java index f446658cb..39c810d20 100644 --- a/core/src/main/java/me/prettyprint/cassandra/service/VirtualKeyspaceServiceImpl.java +++ b/core/src/main/java/me/prettyprint/cassandra/service/VirtualKeyspaceServiceImpl.java @@ -16,6 +16,7 @@ import org.apache.cassandra.thrift.Column; import org.apache.cassandra.thrift.ColumnParent; import org.apache.cassandra.thrift.ColumnPath; +import org.apache.cassandra.thrift.CounterColumn; import org.apache.cassandra.thrift.IndexClause; import org.apache.cassandra.thrift.KeyRange; import org.apache.cassandra.thrift.Mutation; @@ -111,6 +112,13 @@ public List getSlice(ByteBuffer key, ColumnParent columnParent, return super.getSlice(ps.toByteBuffer(key), columnParent, predicate); } + + @Override + public List getCounterSlice(ByteBuffer key, ColumnParent columnParent, + SlicePredicate predicate) throws HectorException { + + return super.getCounterSlice(ps.toByteBuffer(key), columnParent, predicate); + } @Override public SuperColumn getSuperColumn(ByteBuffer key, ColumnPath columnPath) diff --git a/core/src/main/java/me/prettyprint/hector/api/beans/CounterSlice.java b/core/src/main/java/me/prettyprint/hector/api/beans/CounterSlice.java new file mode 100644 index 000000000..93383dfbe --- /dev/null +++ b/core/src/main/java/me/prettyprint/hector/api/beans/CounterSlice.java @@ -0,0 +1,21 @@ +package me.prettyprint.hector.api.beans; + +import java.util.List; + + +/** + * A ColumnSlice represents a set of columns as returned by calls such as get_slice + * + * @author patricioe (Patricio Echague) + */ +public interface CounterSlice { + + /** + * + * @return an unmodifiable list of the columns + */ + List> getColumns(); + + HCounterColumn getColumnByName(N columnName); + +} \ No newline at end of file diff --git a/core/src/main/java/me/prettyprint/hector/api/factory/HFactory.java b/core/src/main/java/me/prettyprint/hector/api/factory/HFactory.java index f78025ca0..9e5d2f5e2 100644 --- a/core/src/main/java/me/prettyprint/hector/api/factory/HFactory.java +++ b/core/src/main/java/me/prettyprint/hector/api/factory/HFactory.java @@ -22,6 +22,7 @@ import me.prettyprint.cassandra.model.thrift.ThriftRangeSlicesQuery; import me.prettyprint.cassandra.model.thrift.ThriftRangeSubSlicesQuery; import me.prettyprint.cassandra.model.thrift.ThriftRangeSuperSlicesQuery; +import me.prettyprint.cassandra.model.thrift.ThriftSliceCounterQuery; import me.prettyprint.cassandra.model.thrift.ThriftSliceQuery; import me.prettyprint.cassandra.model.thrift.ThriftSubColumnQuery; import me.prettyprint.cassandra.model.thrift.ThriftSubCountQuery; @@ -62,6 +63,7 @@ import me.prettyprint.hector.api.query.RangeSlicesQuery; import me.prettyprint.hector.api.query.RangeSubSlicesQuery; import me.prettyprint.hector.api.query.RangeSuperSlicesQuery; +import me.prettyprint.hector.api.query.SliceCounterQuery; import me.prettyprint.hector.api.query.SliceQuery; import me.prettyprint.hector.api.query.SubColumnQuery; import me.prettyprint.hector.api.query.SubCountQuery; @@ -431,6 +433,11 @@ public static SliceQuery createSliceQuery( return new ThriftSliceQuery(keyspace, keySerializer, nameSerializer, valueSerializer); } + + public static SliceCounterQuery createCounterSliceQuery( + Keyspace keyspace, Serializer keySerializer, Serializer nameSerializer) { + return new ThriftSliceCounterQuery(keyspace, keySerializer, nameSerializer); + } public static SubSliceQuery createSubSliceQuery( Keyspace keyspace, Serializer keySerializer, diff --git a/core/src/main/java/me/prettyprint/hector/api/query/SliceCounterQuery.java b/core/src/main/java/me/prettyprint/hector/api/query/SliceCounterQuery.java new file mode 100644 index 000000000..72f7379c5 --- /dev/null +++ b/core/src/main/java/me/prettyprint/hector/api/query/SliceCounterQuery.java @@ -0,0 +1,22 @@ +package me.prettyprint.hector.api.query; + +import me.prettyprint.hector.api.beans.CounterSlice; + +/** + * A query for the thrift call get_counter_slice + * + * @author patricioe (Patricio Echague) + * + * @param Name Type + */ +public interface SliceCounterQuery extends Query> { + + SliceCounterQuery setKey(K key); + + SliceCounterQuery setColumnNames(N... columnNames); + + SliceCounterQuery setRange(N start, N finish, boolean reversed, int count); + + SliceCounterQuery setColumnFamily(String cf); + +} \ No newline at end of file diff --git a/core/src/test/java/me/prettyprint/cassandra/service/KeyspaceTest.java b/core/src/test/java/me/prettyprint/cassandra/service/KeyspaceTest.java index e74ef9f54..54d23169f 100644 --- a/core/src/test/java/me/prettyprint/cassandra/service/KeyspaceTest.java +++ b/core/src/test/java/me/prettyprint/cassandra/service/KeyspaceTest.java @@ -11,6 +11,7 @@ import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; +import java.util.Comparator; import java.util.HashMap; import java.util.List; import java.util.Map; @@ -477,8 +478,57 @@ public void testGetSlice() throws HectorException { keyspace.remove("testGetSlice_", cp); keyspace.remove("testGetSlice", cp); } - + @Test + public void testGetCounterSlice() throws HectorException { + // insert value + ArrayList columnnames = new ArrayList(50); + final StringSerializer ss = StringSerializer.get(); + for (int i = 0; i < 100; i++) { + ColumnParent cp = new ColumnParent("Counter1"); + + keyspace.addCounter("testGetCounterSlice", cp, createCounterColumn("testGetCounterSlice_" + i, i)); + + if (i < 50) { + // I want to query only 50. + columnnames.add(ss.toByteBuffer("testGetCounterSlice_" + i)); + } + } + + // Query 50 counters. From testGetCounterSlice_0 to testGetCounterSlice_49. + ColumnParent clp = new ColumnParent("Counter1"); + + // TODO (patricioe) Slice by range will be in the next snapshot. + //SliceRange sr = new SliceRange(ByteBuffer.wrap(new byte[0]), ByteBuffer.wrap(new byte[49]), false, 150); + SlicePredicate sp = new SlicePredicate(); + //sp.setSlice_range(sr); + sp.setColumn_names(columnnames); + List cols = keyspace.getCounterSlice("testGetCounterSlice", clp, sp); + + assertNotNull(cols); + assertEquals(50, cols.size()); + + Collections.sort(columnnames); + + ArrayList gotlist = new ArrayList(50); + for (int i = 0; i < 50; i++) { + CounterColumn cc = cols.get(i); + gotlist.add(cc.name.duplicate()); + assertEquals(getValueFromName(ss.fromByteBuffer(cc.name.duplicate())), cc.getValue()); + } + assertEquals(columnnames, gotlist); + + // Clean up the data this test wrote + ColumnPath cp = new ColumnPath("Counter1"); + keyspace.removeCounter("testGetCounterSlice", cp); + } + + // extract counter value from names like counter_23. In that case the value is 23. + private long getValueFromName(String counterName) { + return Long.valueOf(counterName.substring(counterName.indexOf("_") +1)); +} + +@Test public void testGetSuperSlice() throws HectorException { // insert value for (int i = 0; i < 100; i++) { diff --git a/core/src/test/java/me/prettyprint/hector/api/ApiV2SystemTest.java b/core/src/test/java/me/prettyprint/hector/api/ApiV2SystemTest.java index f35da870d..5ca72af06 100644 --- a/core/src/test/java/me/prettyprint/hector/api/ApiV2SystemTest.java +++ b/core/src/test/java/me/prettyprint/hector/api/ApiV2SystemTest.java @@ -14,6 +14,7 @@ import static me.prettyprint.hector.api.factory.HFactory.createRangeSubSlicesQuery; import static me.prettyprint.hector.api.factory.HFactory.createRangeSuperSlicesQuery; import static me.prettyprint.hector.api.factory.HFactory.createSliceQuery; +import static me.prettyprint.hector.api.factory.HFactory.createCounterSliceQuery; import static me.prettyprint.hector.api.factory.HFactory.createSubColumnQuery; import static me.prettyprint.hector.api.factory.HFactory.createSubCountQuery; import static me.prettyprint.hector.api.factory.HFactory.createSubSliceQuery; @@ -35,6 +36,7 @@ import me.prettyprint.cassandra.BaseEmbededServerSetupTest; import me.prettyprint.cassandra.serializers.StringSerializer; import me.prettyprint.hector.api.beans.ColumnSlice; +import me.prettyprint.hector.api.beans.CounterSlice; import me.prettyprint.hector.api.beans.HColumn; import me.prettyprint.hector.api.beans.HCounterColumn; import me.prettyprint.hector.api.beans.HSuperColumn; @@ -57,6 +59,7 @@ import me.prettyprint.hector.api.query.RangeSlicesQuery; import me.prettyprint.hector.api.query.RangeSubSlicesQuery; import me.prettyprint.hector.api.query.RangeSuperSlicesQuery; +import me.prettyprint.hector.api.query.SliceCounterQuery; import me.prettyprint.hector.api.query.SliceQuery; import me.prettyprint.hector.api.query.SubColumnQuery; import me.prettyprint.hector.api.query.SubCountQuery; @@ -439,6 +442,49 @@ public void testSliceQuery() { deleteColumns(cleanup); } + + @Test + public void testCounterSliceQuery() { + String cf = "Counter1"; + + //TestCleanupDescriptor cleanup = insertColumns(cf, 1, "testSliceQuery", 4, "testSliceQuery"); + Mutator mutator = createMutator(ko, se); + for (int i = 0; i < 10; i++) { + mutator.addCounter("testCounterSliceQuery_key", cf, createCounterColumn("" + i, i)); + } + mutator.execute(); + + // get value + SliceCounterQuery q = createCounterSliceQuery(ko, se, se); + q.setColumnFamily(cf); + q.setKey("testCounterSliceQuery_key"); + + // try with column name first + q.setColumnNames("4", "5", "6"); + QueryResult> r = q.execute(); + + assertNotNull(r); + + CounterSlice slice = r.get(); + + assertNotNull(slice); + + assertEquals(3, slice.getColumns().size()); + + // Test slice.getColumnByName + assertEquals(4, slice.getColumnByName("4").getValue().longValue()); + assertEquals(5, slice.getColumnByName("5").getValue().longValue()); + assertEquals(6, slice.getColumnByName("6").getValue().longValue()); + + // Test slice.getColumns + List> columns = slice.getColumns(); + assertNotNull(columns); + assertEquals(3, columns.size()); + + // Cleanup + mutator.deleteCounter("testCounterSliceQuery_key", cf, null, se); + mutator.execute(); + } @Test public void testSuperSliceQuery() { From a5da40900a5a28030766b047eecb851dc807579d Mon Sep 17 00:00:00 2001 From: Janne Jalkanen Date: Tue, 19 Apr 2011 22:24:03 +0300 Subject: [PATCH 121/144] First proposal to switch to Speed4j. --- core/pom.xml | 6 +-- .../connection/HConnectionManager.java | 49 +++++++++---------- .../cassandra/service/JmxMonitor.java | 31 ------------ .../cassandra/service/OperationType.java | 2 +- core/src/main/resources/hectorLog4j.xml | 48 ------------------ core/src/main/resources/speed4j.properties | 21 ++++++++ 6 files changed, 47 insertions(+), 110 deletions(-) create mode 100644 core/src/main/resources/speed4j.properties diff --git a/core/pom.xml b/core/pom.xml index b21b6314f..48586a14b 100644 --- a/core/pom.xml +++ b/core/pom.xml @@ -193,9 +193,9 @@ 3.2.0 - org.perf4j - perf4j - 0.9.12 + com.ecyrd.speed4j + speed4j + 0.5-SNAPSHOT diff --git a/core/src/main/java/me/prettyprint/cassandra/connection/HConnectionManager.java b/core/src/main/java/me/prettyprint/cassandra/connection/HConnectionManager.java index 66f0f23ff..27abb8f75 100644 --- a/core/src/main/java/me/prettyprint/cassandra/connection/HConnectionManager.java +++ b/core/src/main/java/me/prettyprint/cassandra/connection/HConnectionManager.java @@ -1,43 +1,26 @@ package me.prettyprint.cassandra.connection; -import java.util.ArrayList; -import java.util.Collection; -import java.util.Collections; -import java.util.HashSet; -import java.util.List; -import java.util.Set; +import java.util.*; -import me.prettyprint.cassandra.service.CassandraClientMonitor; +import me.prettyprint.cassandra.service.*; import me.prettyprint.cassandra.service.CassandraClientMonitor.Counter; -import me.prettyprint.cassandra.service.CassandraHost; -import me.prettyprint.cassandra.service.CassandraHostConfigurator; -import me.prettyprint.cassandra.service.ExceptionsTranslator; -import me.prettyprint.cassandra.service.ExceptionsTranslatorImpl; -import me.prettyprint.cassandra.service.FailoverPolicy; -import me.prettyprint.cassandra.service.JmxMonitor; -import me.prettyprint.cassandra.service.Operation; import me.prettyprint.hector.api.ClockResolution; -import me.prettyprint.hector.api.exceptions.HCassandraInternalException; -import me.prettyprint.hector.api.exceptions.HInvalidRequestException; -import me.prettyprint.hector.api.exceptions.HTimedOutException; -import me.prettyprint.hector.api.exceptions.HUnavailableException; -import me.prettyprint.hector.api.exceptions.HectorException; -import me.prettyprint.hector.api.exceptions.HectorTransportException; -import me.prettyprint.hector.api.exceptions.PoolExhaustedException; +import me.prettyprint.hector.api.exceptions.*; import org.apache.cassandra.thrift.AuthenticationRequest; import org.apache.cassandra.thrift.Cassandra; import org.cliffc.high_scale_lib.NonBlockingHashMap; -import org.perf4j.StopWatch; -import org.perf4j.slf4j.Slf4JStopWatch; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import com.ecyrd.speed4j.StopWatch; +import com.ecyrd.speed4j.StopWatchFactory; +import com.ecyrd.speed4j.log.PeriodicalLog; + public class HConnectionManager { private static final Logger log = LoggerFactory.getLogger(HConnectionManager.class); - private static final Logger perf4jLogger = - LoggerFactory.getLogger("me.prettyprint.cassandra.hector.TimingLogger"); + private StopWatchFactory stopWatchFactory; private final NonBlockingHashMap hostPools; private final NonBlockingHashMap suspendedHostPools; @@ -48,7 +31,6 @@ public class HConnectionManager { private LoadBalancingPolicy loadBalancingPolicy; private CassandraHostConfigurator cassandraHostConfigurator; private HostTimeoutTracker hostTimeoutTracker; - private final ClockResolution clock; final ExceptionsTranslator exceptionsTranslator; @@ -89,6 +71,19 @@ public HConnectionManager(String clusterName, CassandraHostConfigurator cassandr nodeAutoDiscoverService.doAddNodes(); } } + + // + // This sets up the Speed4J logging system. Alternatively, we could + // use the speed4j.properties -file. This was chosen just so that + // it wouldn't confuse anyone and would work pretty much the same + // way as what the old hector config does. + // + PeriodicalLog slog = new PeriodicalLog(); + slog.setName("hector-"+clusterName); + slog.setPeriod(60); // 60 seconds + slog.setSlf4jLogname( "me.prettyprint.cassandra.hector.TimingLogger" ); + + stopWatchFactory = StopWatchFactory.getInstance( slog ); } /** @@ -202,7 +197,7 @@ public List getStatusPerPool() { public void operateWithFailover(Operation op) throws HectorException { - final StopWatch stopWatch = new Slf4JStopWatch(perf4jLogger); + final StopWatch stopWatch = stopWatchFactory.getStopWatch(); int retries = Math.min(op.failoverPolicy.numRetries, hostPools.size()); HThriftClient client = null; HClientPool pool = null; diff --git a/core/src/main/java/me/prettyprint/cassandra/service/JmxMonitor.java b/core/src/main/java/me/prettyprint/cassandra/service/JmxMonitor.java index 223bed6d0..17f401a30 100644 --- a/core/src/main/java/me/prettyprint/cassandra/service/JmxMonitor.java +++ b/core/src/main/java/me/prettyprint/cassandra/service/JmxMonitor.java @@ -62,37 +62,6 @@ public void registerMonitor(String name, String monitorType, Object monitoringIn } mbs.registerMBean(monitoringInterface, oName); - - // Register perf4j monitors - if ("true".equalsIgnoreCase( - System.getProperty("com.prettyprint.cassandra.load_hector_log4j", "true"))) { - registerPerf4J(); - } - } - - private void registerPerf4J() { - URL url = getClass().getClassLoader().getResource("hectorLog4j.xml"); - if (url == null) { - log.warn("Unable to locate hectorLog4j.xml; performance counters will not be exported"); - } else { - try { - final Class domConfiguratorClass = getClass().getClassLoader().loadClass("org.apache.log4j.xml.DOMConfigurator"); - final Method method = domConfiguratorClass.getMethod( "configure", URL.class ); - method.invoke( null, url ); - } catch( ClassNotFoundException e ) { - log.warn("Unable to load log4j's DOMConfigurator. Performance counters will not be exported. To fix, include the log4j jar in your application's classpath."); - } catch( SecurityException e ) { - log.error( "Could not access method DOMConfigurator.configure(URL)", e ); - } catch( NoSuchMethodException e ) { - log.error( "Could not find method DOMConfigurator.configure(URL)", e ); - } catch( IllegalArgumentException e ) { - log.error( "Could not invoke method DOMConfigurator.configure(URL)", e ); - } catch( IllegalAccessException e ) { - log.error( "Could not invoke method DOMConfigurator.configure(URL)", e ); - } catch( InvocationTargetException e ) { - throw new RuntimeException(e.getCause()); - } - } } private String generateMonitorName(String className, String monitorType) { diff --git a/core/src/main/java/me/prettyprint/cassandra/service/OperationType.java b/core/src/main/java/me/prettyprint/cassandra/service/OperationType.java index ea2adcbf5..ef9b28f68 100644 --- a/core/src/main/java/me/prettyprint/cassandra/service/OperationType.java +++ b/core/src/main/java/me/prettyprint/cassandra/service/OperationType.java @@ -2,7 +2,7 @@ /** * Specifies the "type" of operation - read or write. - * It's used for perf4j, so should be in sync with hectorLog4j.xml + * It's used for Speed4j, so should be in sync with hectorLog4j.xml * @author Ran Tavory (ran@outbain.com) * */ diff --git a/core/src/main/resources/hectorLog4j.xml b/core/src/main/resources/hectorLog4j.xml index a33e9b0cc..b25a39739 100644 --- a/core/src/main/resources/hectorLog4j.xml +++ b/core/src/main/resources/hectorLog4j.xml @@ -1,53 +1,5 @@ - - - - - - - - - - - - - - - - - - - - - - - diff --git a/core/src/main/resources/speed4j.properties b/core/src/main/resources/speed4j.properties new file mode 100644 index 000000000..cef18ac45 --- /dev/null +++ b/core/src/main/resources/speed4j.properties @@ -0,0 +1,21 @@ +# +# This is the main configuration for Speed4j. You may override this in your +# own project. +# +# For hector, we define a simple Log which is used to both output things +# to the given SLF4J logger, as well as display it in a JMX bean. +# + +# Which Log instance shall we use? +speed4j.hector = com.ecyrd.speed4j.log.PeriodicalLog + +# How often shall this Log log (in seconds)? +speed4j.hector.period = 60 + +# Which of these attributes shall be exposed in JMX? +speed4j.hector.jmx=READ.success_,WRITE.success_,READ.fail_,WRITE.fail_,META_READ.success_,META_READ.fail_ + +# Which SLF4J log name shall we output to? Speed4J uses the INFO log level. +# Note that this is slightly different from the old Log4J name just to ensure that we +# don't mess with any existing log4j configurations. +speed4j.hector.slf4jLogname=me.prettyprint.hector.TimingLogger From 5cfd9c79bf00de3456479bf9ed5dd1c8436537b9 Mon Sep 17 00:00:00 2001 From: Janne Jalkanen Date: Tue, 19 Apr 2011 23:13:38 +0300 Subject: [PATCH 122/144] Now using 0.5 speed4j proper. --- core/pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/pom.xml b/core/pom.xml index 48586a14b..ab71bdc08 100644 --- a/core/pom.xml +++ b/core/pom.xml @@ -195,7 +195,7 @@ com.ecyrd.speed4j speed4j - 0.5-SNAPSHOT + 0.5 From 5627182e016c208463cc20976f83a18c7c1dbf74 Mon Sep 17 00:00:00 2001 From: zznate Date: Wed, 20 Apr 2011 15:57:06 -0500 Subject: [PATCH 123/144] potential work around for improperly wrapped socket timeout exception exception --- .../cassandra/service/ExceptionsTranslatorImpl.java | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/core/src/main/java/me/prettyprint/cassandra/service/ExceptionsTranslatorImpl.java b/core/src/main/java/me/prettyprint/cassandra/service/ExceptionsTranslatorImpl.java index 21f32cd20..89f0d81a2 100644 --- a/core/src/main/java/me/prettyprint/cassandra/service/ExceptionsTranslatorImpl.java +++ b/core/src/main/java/me/prettyprint/cassandra/service/ExceptionsTranslatorImpl.java @@ -1,5 +1,6 @@ package me.prettyprint.cassandra.service; +import java.net.SocketTimeoutException; import java.util.NoSuchElementException; import me.prettyprint.hector.api.exceptions.HCassandraInternalException; @@ -26,8 +27,17 @@ public HectorException translate(Throwable original) { return (HectorException) original; } else if (original instanceof TApplicationException) { return new HCassandraInternalException(((TApplicationException)original).getType(), original.getMessage()); - } else if (original instanceof TException || original instanceof TTransportException) { + } else if (original instanceof TException) { return new HectorTransportException(original); + } else if (original instanceof TTransportException) { + // if the underlying cause is a scoket timeout, reflect that directly + // TODO this may be an issue on the Cassandra side which warrants ivestigation. + // I seem to remember these coming back as TimedOutException previously + if ( ((TTransportException)original).getCause() instanceof SocketTimeoutException ) { + return new HTimedOutException(original); + } else { + return new HectorTransportException(original); + } } else if (original instanceof org.apache.cassandra.thrift.TimedOutException) { return new HTimedOutException(original); } else if (original instanceof org.apache.cassandra.thrift.InvalidRequestException) { From 8a6f22bd55f4c502b34159a92469386ba327b1c2 Mon Sep 17 00:00:00 2001 From: zznate Date: Wed, 20 Apr 2011 17:51:26 -0500 Subject: [PATCH 124/144] moved check for transport exception to where it will actually do something. Thanks to Jim Ancona for changes --- .../cassandra/service/ExceptionsTranslatorImpl.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/core/src/main/java/me/prettyprint/cassandra/service/ExceptionsTranslatorImpl.java b/core/src/main/java/me/prettyprint/cassandra/service/ExceptionsTranslatorImpl.java index 89f0d81a2..87ad0fa42 100644 --- a/core/src/main/java/me/prettyprint/cassandra/service/ExceptionsTranslatorImpl.java +++ b/core/src/main/java/me/prettyprint/cassandra/service/ExceptionsTranslatorImpl.java @@ -26,9 +26,7 @@ public HectorException translate(Throwable original) { if (original instanceof HectorException) { return (HectorException) original; } else if (original instanceof TApplicationException) { - return new HCassandraInternalException(((TApplicationException)original).getType(), original.getMessage()); - } else if (original instanceof TException) { - return new HectorTransportException(original); + return new HCassandraInternalException(((TApplicationException)original).getType(), original.getMessage()); } else if (original instanceof TTransportException) { // if the underlying cause is a scoket timeout, reflect that directly // TODO this may be an issue on the Cassandra side which warrants ivestigation. @@ -60,6 +58,8 @@ public HectorException translate(Throwable original) { return new HNotFoundException(original); } else if (original instanceof org.apache.cassandra.thrift.UnavailableException) { return new HUnavailableException(original); + } else if (original instanceof TException) { + return new HectorTransportException(original); } else if (original instanceof NoSuchElementException) { return new PoolExhaustedException(original); } else if (original instanceof IllegalStateException) { From 4717c1cb80aae733dac98cffeb4220762c57c9aa Mon Sep 17 00:00:00 2001 From: zznate Date: Tue, 26 Apr 2011 17:15:25 -0500 Subject: [PATCH 125/144] Added option for setting keepalive on the underlying socket. Defaults to false --- .../cassandra/connection/HThriftClient.java | 15 +++++++++++++-- .../cassandra/service/CassandraHost.java | 10 ++++++++++ .../service/CassandraHostConfigurator.java | 15 ++++++++++++++- 3 files changed, 37 insertions(+), 3 deletions(-) diff --git a/core/src/main/java/me/prettyprint/cassandra/connection/HThriftClient.java b/core/src/main/java/me/prettyprint/cassandra/connection/HThriftClient.java index 492519089..290ee6cca 100644 --- a/core/src/main/java/me/prettyprint/cassandra/connection/HThriftClient.java +++ b/core/src/main/java/me/prettyprint/cassandra/connection/HThriftClient.java @@ -1,5 +1,7 @@ package me.prettyprint.cassandra.connection; +import java.net.Socket; +import java.net.SocketException; import java.util.concurrent.atomic.AtomicLong; import me.prettyprint.cassandra.service.CassandraHost; @@ -103,11 +105,20 @@ HThriftClient open() { log.debug("Creating a new thrift connection to {}", cassandraHost); } + TSocket socket = new TSocket(cassandraHost.getHost(), cassandraHost.getPort(), timeout); + if ( cassandraHost.getUseSocketKeepalive() ) { + try { + socket.getSocket().setKeepAlive(true); + } catch (SocketException se) { + throw new HectorTransportException("Could not set SO_KEEPALIVE on socket: ", se); + } + } if (cassandraHost.getUseThriftFramedTransport()) { - transport = new TFramedTransport(new TSocket(cassandraHost.getHost(), cassandraHost.getPort(), timeout)); + transport = new TFramedTransport(socket); } else { - transport = new TSocket(cassandraHost.getHost(), cassandraHost.getPort(), timeout); + transport = socket; } + try { transport.open(); } catch (TTransportException e) { diff --git a/core/src/main/java/me/prettyprint/cassandra/service/CassandraHost.java b/core/src/main/java/me/prettyprint/cassandra/service/CassandraHost.java index 93bd0c22d..e03e7144f 100644 --- a/core/src/main/java/me/prettyprint/cassandra/service/CassandraHost.java +++ b/core/src/main/java/me/prettyprint/cassandra/service/CassandraHost.java @@ -59,6 +59,7 @@ public final class CassandraHost { private int cassandraThriftSocketTimeout; private ExhaustedPolicy exhaustedPolicy = ExhaustedPolicy.WHEN_EXHAUSTED_BLOCK; private boolean useThriftFramedTransport = DEFAULT_USE_FRAMED_THRIFT_TRANSPORT; + private boolean useSocketKeepalive; //TODO(ran): private FailoverPolicy failoverPolicy = DEFAULT_FAILOVER_POLICY; public CassandraHost(String url) { @@ -225,4 +226,13 @@ public void setTimeBetweenEvictionRunsMillis(long timeBetweenEvictionRunsMillis) this.timeBetweenEvictionRunsMillis = timeBetweenEvictionRunsMillis; } + public boolean getUseSocketKeepalive() { + return useSocketKeepalive; + } + + public void setUseSocketKeepalive(boolean useSocketKeepalive) { + this.useSocketKeepalive = useSocketKeepalive; + } + + } diff --git a/core/src/main/java/me/prettyprint/cassandra/service/CassandraHostConfigurator.java b/core/src/main/java/me/prettyprint/cassandra/service/CassandraHostConfigurator.java index 6f41223ad..b121638dd 100644 --- a/core/src/main/java/me/prettyprint/cassandra/service/CassandraHostConfigurator.java +++ b/core/src/main/java/me/prettyprint/cassandra/service/CassandraHostConfigurator.java @@ -40,7 +40,7 @@ public final class CassandraHostConfigurator implements Serializable { private int hostTimeoutUnsuspendCheckDelay = HostTimeoutTracker.DEF_NODE_UNSUSPEND_CHECK_DELAY_IN_SECONDS; private boolean useHostTimeoutTracker = false; private boolean runAutoDiscoveryAtStartup = false; - + private boolean useSocketKeepalive = false; public CassandraHostConfigurator() { this.hosts = null; @@ -73,6 +73,7 @@ public void applyConfig(CassandraHost cassandraHost) { cassandraHost.setTimeBetweenEvictionRunsMillis(timeBetweenEvictionRunsMillis); cassandraHost.setMaxWaitTimeWhenExhausted(maxWaitTimeWhenExhausted); cassandraHost.setUseThriftFramedTransport(useThriftFramedTransport); + cassandraHost.setUseSocketKeepalive(useSocketKeepalive); // this is special as it can be passed in as a system property if (cassandraThriftSocketTimeout > 0) { @@ -290,6 +291,18 @@ public void setRunAutoDiscoveryAtStartup(boolean runAutoDiscoveryAtStartup) { this.runAutoDiscoveryAtStartup = runAutoDiscoveryAtStartup; } + public boolean getUseSocketKeepalive() { + return useSocketKeepalive; + } + + /** + * Enable SO_KEEPALIVE on the underlying socket. OFF by default (per java.net.Socket) + * + */ + public void setUseSocketKeepalive(boolean useSocketKeepalive) { + this.useSocketKeepalive = useSocketKeepalive; + } + } From b7947dec8c7fa8a133fed1ed3f39b81d240cabe4 Mon Sep 17 00:00:00 2001 From: zznate Date: Thu, 28 Apr 2011 11:09:36 -0500 Subject: [PATCH 126/144] updated logger level to more appropriate warn --- .../prettyprint/cassandra/connection/HConnectionManager.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/core/src/main/java/me/prettyprint/cassandra/connection/HConnectionManager.java b/core/src/main/java/me/prettyprint/cassandra/connection/HConnectionManager.java index 66f0f23ff..72de6d668 100644 --- a/core/src/main/java/me/prettyprint/cassandra/connection/HConnectionManager.java +++ b/core/src/main/java/me/prettyprint/cassandra/connection/HConnectionManager.java @@ -269,8 +269,8 @@ public void operateWithFailover(Operation op) throws HectorException { retryable = false; } if ( retries <= 0 || retryable == false) throw he; - log.error("Could not fullfill request on this host {}", client); - log.error("Exception: ", he); + log.warn("Could not fullfill request on this host {}", client); + log.warn("Exception: ", he); monitor.incCounter(Counter.SKIP_HOST_SUCCESS); sleepBetweenHostSkips(op.failoverPolicy); } finally { From cd5410a1d60801c124a3c49cf91a55958850db1d Mon Sep 17 00:00:00 2001 From: Oleg Tsvinev Date: Thu, 28 Apr 2011 12:50:00 -0700 Subject: [PATCH 127/144] Committer: Oleg Tsvinev modified: core/src/main/java/me/prettyprint/cassandra/model/HColumnImpl.java modified: core/src/main/java/me/prettyprint/hector/api/factory/HFactory.java --- .../prettyprint/cassandra/model/HColumnImpl.java | 6 ++++++ .../prettyprint/hector/api/factory/HFactory.java | 15 +++++++++++++++ 2 files changed, 21 insertions(+) diff --git a/core/src/main/java/me/prettyprint/cassandra/model/HColumnImpl.java b/core/src/main/java/me/prettyprint/cassandra/model/HColumnImpl.java index 1cedf751c..5e765c9c0 100644 --- a/core/src/main/java/me/prettyprint/cassandra/model/HColumnImpl.java +++ b/core/src/main/java/me/prettyprint/cassandra/model/HColumnImpl.java @@ -37,6 +37,12 @@ public HColumnImpl(N name, V value, long clock, Serializer nameSerializer, valueSerializer.toByteBuffer(value), clock); } + public HColumnImpl(N name, V value, long clock, int ttl, + Serializer nameSerializer, Serializer valueSerializer) { + this(name, value, clock, nameSerializer, valueSerializer); + setTtl(ttl); + } + public HColumnImpl(Column thriftColumn, Serializer nameSerializer, Serializer valueSerializer) { this(nameSerializer, valueSerializer); diff --git a/core/src/main/java/me/prettyprint/hector/api/factory/HFactory.java b/core/src/main/java/me/prettyprint/hector/api/factory/HFactory.java index 9e5d2f5e2..238bf7a6d 100644 --- a/core/src/main/java/me/prettyprint/hector/api/factory/HFactory.java +++ b/core/src/main/java/me/prettyprint/hector/api/factory/HFactory.java @@ -492,6 +492,12 @@ public static HColumn createColumn(N name, V value, long clock, valueSerializer); } + public static HColumn createColumn(N name, V value, long clock, int ttl, + Serializer nameSerializer, Serializer valueSerializer) { + return new HColumnImpl(name, value, clock, ttl, nameSerializer, + valueSerializer); + } + /** * Creates a column with the clock of now. */ @@ -501,6 +507,15 @@ public static HColumn createColumn(N name, V value, valueSerializer); } + /** + * Creates a column with the clock of now + */ + public static HColumn createColumn(N name, V value, int ttl, + Serializer nameSerializer, Serializer valueSerializer) { + return new HColumnImpl(name, value, createClock(), ttl, nameSerializer, + valueSerializer); + } + /** * Convienience method for creating a column with a String name and String * value From c2aeed008d6c7ec8e656350c6fb06f7741408f6c Mon Sep 17 00:00:00 2001 From: mck Date: Fri, 29 Apr 2011 10:01:23 +0200 Subject: [PATCH 128/144] make hector build for cassandra beta1 changes (see r1094102 and r1094628) object-mapper tests still fail because schema can no longer be defined in cassandra.yaml (see cassandra/conf/schema-sample.txt) --- core/pom.xml | 108 +++++++++--------- .../cassandra/model/HColumnImpl.java | 31 ++--- .../service/KeyspaceServiceImpl.java | 34 +++--- .../cassandra/model/ColumnSliceTest.java | 21 +++- .../cassandra/model/SuperColumnSliceTest.java | 4 +- .../cassandra/service/BatchMutationTest.java | 59 +++++----- .../cassandra/service/KeyspaceTest.java | 60 +++++----- object-mapper/pom.xml | 15 ++- .../me/prettyprint/hom/CassandraTestBase.java | 21 ++-- .../src/test/resources/cassandra.yaml | 57 ++++----- pom.xml | 10 +- 11 files changed, 225 insertions(+), 195 deletions(-) diff --git a/core/pom.xml b/core/pom.xml index b21b6314f..d356d1b94 100644 --- a/core/pom.xml +++ b/core/pom.xml @@ -104,70 +104,70 @@ org.apache.cassandra - apache-cassandra - 0.8.0-20110415 + cassandra-all + 0.8.0-beta1 org.apache.cassandra - apache-cassandra-thrift - 0.8.0-20110415 + cassandra-thrift + 0.8.0-beta1 - org.apache.cassandra.deps + org.apache.thrift libthrift - 0.6.0 + 0.6.1 - - org.yaml - snakeyaml - 1.6 + + org.yaml + snakeyaml + 1.6 test - - - com.google.guava - guava - r08 - - - commons-collections - commons-collections - 3.2.1 + + + com.google.guava + guava + r08 + + + commons-collections + commons-collections + 3.2.1 test - - - org.apache.cassandra.deps - avro - 1.4.0-cassandra-1 + + + org.apache.cassandra.deps + avro + 1.4.0-cassandra-1 test - - - netty - org.jboss.netty - - - paranamer - com.thoughtworks.paranamer - - - paranamer-ant - com.thoughtworks.paranamer - - - velocity - org.apache.velocity - - - - - org.antlr - antlr + + + netty + org.jboss.netty + + + paranamer + com.thoughtworks.paranamer + + + paranamer-ant + com.thoughtworks.paranamer + + + velocity + org.apache.velocity + + + + + org.antlr + antlr 3.1.3 - test - - - com.googlecode.concurrentlinkedhashmap - concurrentlinkedhashmap-lru - 1.1 + test + + + com.googlecode.concurrentlinkedhashmap + concurrentlinkedhashmap-lru + 1.1 test @@ -232,7 +232,7 @@ org.mockito mockito-all test - + org.slf4j slf4j-log4j12 diff --git a/core/src/main/java/me/prettyprint/cassandra/model/HColumnImpl.java b/core/src/main/java/me/prettyprint/cassandra/model/HColumnImpl.java index 1cedf751c..d3a2535b7 100644 --- a/core/src/main/java/me/prettyprint/cassandra/model/HColumnImpl.java +++ b/core/src/main/java/me/prettyprint/cassandra/model/HColumnImpl.java @@ -33,8 +33,9 @@ public HColumnImpl(N name, V value, long clock, Serializer nameSerializer, notNull(name, "name is null"); notNull(value, "value is null"); - this.column = new Column(nameSerializer.toByteBuffer(name), - valueSerializer.toByteBuffer(value), clock); + this.column = new Column(nameSerializer.toByteBuffer(name)); + this.column.setValue(valueSerializer.toByteBuffer(value)); + this.column.setTimestamp(clock); } public HColumnImpl(Column thriftColumn, Serializer nameSerializer, @@ -78,7 +79,7 @@ public HColumn setClock(long clock) { } /** - * Set the time-to-live value for this column in seconds. + * Set the time-to-live value for this column in seconds. * The server will mark this column as deleted once the number of seconds has elapsed. */ @Override @@ -93,15 +94,15 @@ public int getTtl() { } @Override - public N getName() { + public N getName() { return column.isSetName() ? nameSerializer.fromByteBuffer(column.name.duplicate()) : null; } @Override - public V getValue() { + public V getValue() { return column.isSetValue() ? valueSerializer.fromByteBuffer(column.value.duplicate()) : null; - } - + } + @Override public long getClock() { @@ -126,15 +127,15 @@ public Serializer getNameSerializer() { @Override public Serializer getValueSerializer() { return valueSerializer; - } - + } + @Override - public ByteBuffer getNameBytes() { + public ByteBuffer getNameBytes() { return column.isSetName() ? column.name.duplicate() : null; } @Override - public ByteBuffer getValueBytes() { + public ByteBuffer getValueBytes() { return column.isSetValue() ? column.value.duplicate() : null; } @@ -142,7 +143,7 @@ public ByteBuffer getValueBytes() { * Clear value, timestamp and ttl (the latter two set to '0') leaving only the column name */ @Override - public HColumn clear() { + public HColumn clear() { column.value = null; column.timestamp = 0; column.ttl = 0; @@ -151,8 +152,8 @@ public HColumn clear() { column.setValueIsSet(false); return this; } - - + + @Override public HColumn apply(V value, long clock, int ttl) { @@ -166,7 +167,7 @@ public HColumn apply(Column c) { this.column = c; return this; } - + @Override public String toString() { return String.format("HColumn(%s=%s)",getName(), getValue()); diff --git a/core/src/main/java/me/prettyprint/cassandra/service/KeyspaceServiceImpl.java b/core/src/main/java/me/prettyprint/cassandra/service/KeyspaceServiceImpl.java index bf55b5e34..72cb9e158 100644 --- a/core/src/main/java/me/prettyprint/cassandra/service/KeyspaceServiceImpl.java +++ b/core/src/main/java/me/prettyprint/cassandra/service/KeyspaceServiceImpl.java @@ -57,7 +57,7 @@ public class KeyspaceServiceImpl implements KeyspaceService { private CassandraHost cassandraHost; private final FailoverPolicy failoverPolicy; - + private final Map credentials; public KeyspaceServiceImpl(String keyspaceName, @@ -228,17 +228,17 @@ public List execute(Cassandra.Client cassandra) throws HectorException { operateWithFailover(op); return op.getResult(); } - + @Override public List getSlice(String key, ColumnParent columnParent, SlicePredicate predicate) throws HectorException { return getSlice(StringSerializer.get().toByteBuffer(key), columnParent, predicate); } - + @Override public List getCounterSlice(final ByteBuffer key, final ColumnParent columnParent, final SlicePredicate predicate) throws HectorException { - Operation> op = + Operation> op = new Operation>(OperationType.READ, failoverPolicy, keyspaceName, credentials) { @Override @@ -258,7 +258,7 @@ public List execute(Cassandra.Client cassandra) throws HectorExce // Inconsistency throw new HectorException("Regular Column is part of the set of Counter Column"); } - + } return result; } catch (Exception e) { @@ -394,9 +394,9 @@ public Void execute(Cassandra.Client cassandra) throws HectorException { }; operateWithFailover(op); } - + @Override - public void addCounter(final ByteBuffer key, final ColumnParent columnParent, final CounterColumn counterColumn) + public void addCounter(final ByteBuffer key, final ColumnParent columnParent, final CounterColumn counterColumn) throws HectorException { Operation op = new Operation(OperationType.WRITE, failoverPolicy, keyspaceName, credentials) { @@ -412,7 +412,7 @@ public Void execute(Cassandra.Client cassandra) throws HectorException { }; operateWithFailover(op); } - + @Override public void addCounter(String key, ColumnParent columnParent, CounterColumn counterColumn) throws HectorException { addCounter(StringSerializer.get().toByteBuffer(key), columnParent, counterColumn); @@ -425,7 +425,9 @@ public void insert(String key, ColumnPath columnPath, ByteBuffer value) throws H if (columnPath.isSetSuper_column()) { columnParent.setSuper_column(columnPath.getSuper_column()); } - Column column = new Column(ByteBuffer.wrap(columnPath.getColumn()), value, connectionManager.createClock()); + Column column = new Column(ByteBuffer.wrap(columnPath.getColumn())); + column.setValue(value); + column.setTimestamp(connectionManager.createClock()); insert(StringSerializer.get().toByteBuffer(key), columnParent, column); } @@ -436,7 +438,9 @@ public void insert(String key, ColumnPath columnPath, ByteBuffer value, long tim if (columnPath.isSetSuper_column()) { columnParent.setSuper_column(columnPath.getSuper_column()); } - Column column = new Column(ByteBuffer.wrap(columnPath.getColumn()), value, timestamp); + Column column = new Column(ByteBuffer.wrap(columnPath.getColumn())); + column.setValue(value); + column.setTimestamp(timestamp); insert(StringSerializer.get().toByteBuffer(key), columnParent, column); } @@ -624,7 +628,7 @@ public Void execute(Cassandra.Client cassandra) throws HectorException { }; operateWithFailover(op); } - + @Override public void removeCounter(final ByteBuffer key, final ColumnPath columnPath) throws HectorException { Operation op = new Operation(OperationType.WRITE, failoverPolicy, keyspaceName, credentials) { @@ -641,7 +645,7 @@ public Void execute(Cassandra.Client cassandra) throws HectorException { }; operateWithFailover(op); } - + @Override public void removeCounter(String key, ColumnPath columnPath) throws HectorException { removeCounter(StringSerializer.get().toByteBuffer(key), columnPath); @@ -694,7 +698,7 @@ public Column execute(Cassandra.Client cassandra) throws HectorException { } return op.getResult(); } - + @Override public CounterColumn getCounter(final ByteBuffer key, final ColumnPath columnPath) throws HectorException { Operation op = new Operation(OperationType.READ, failoverPolicy, keyspaceName, credentials) { @@ -703,7 +707,7 @@ public CounterColumn getCounter(final ByteBuffer key, final ColumnPath columnPat public CounterColumn execute(Cassandra.Client cassandra) throws HectorException { ColumnOrSuperColumn cosc; try { - cosc = cassandra.get(key, columnPath, getThriftCl(OperationType.READ)); + cosc = cassandra.get(key, columnPath, getThriftCl(OperationType.READ)); } catch (NotFoundException e) { setException(xtrans.translate(e)); return null; @@ -720,7 +724,7 @@ public CounterColumn execute(Cassandra.Client cassandra) throws HectorException } return op.getResult(); } - + @Override public CounterColumn getCounter(String key, ColumnPath columnPath) throws HectorException { return getCounter(StringSerializer.get().toByteBuffer(key), columnPath); diff --git a/core/src/test/java/me/prettyprint/cassandra/model/ColumnSliceTest.java b/core/src/test/java/me/prettyprint/cassandra/model/ColumnSliceTest.java index 5576695e6..d81b1a525 100644 --- a/core/src/test/java/me/prettyprint/cassandra/model/ColumnSliceTest.java +++ b/core/src/test/java/me/prettyprint/cassandra/model/ColumnSliceTest.java @@ -22,7 +22,7 @@ public class ColumnSliceTest { StringSerializer se = StringSerializer.get(); LongSerializer le = LongSerializer.get(); - + @Test public void testConstruction() { @@ -30,22 +30,31 @@ public void testConstruction() { ColumnSlice slice = new ColumnSliceImpl(tColumns, se, le); Assert.assertTrue(slice.getColumns().isEmpty()); - tColumns.add(new Column(ByteBuffer.wrap(new byte[]{}), ByteBuffer.wrap(new byte[]{}), 0L)); + Column column = new Column(ByteBuffer.wrap(new byte[]{})); + column.setValue(ByteBuffer.wrap(new byte[]{})); + column.setTimestamp(0L); + tColumns.add(column); slice = new ColumnSliceImpl(tColumns, se, le); Assert.assertEquals(1, slice.getColumns().size()); tColumns = new ArrayList(); - tColumns.add(new Column(se.toByteBuffer("1"), le.toByteBuffer(1L), 0L)); + column = new Column(se.toByteBuffer("1")); + column.setValue(le.toByteBuffer(1L)); + column.setTimestamp(0L); + tColumns.add(column); slice = new ColumnSliceImpl(tColumns, se, le); Assert.assertEquals((Long) 1L, slice.getColumnByName("1").getValue()); } - + @Test public void testMultiCallOnByteBuffer() { List tColumns = new ArrayList(); - tColumns.add(new Column(se.toByteBuffer("1"), ByteBuffer.wrap("colvalue".getBytes()), 0L)); + Column column = new Column(se.toByteBuffer("1")); + column.setValue(ByteBuffer.wrap("colvalue".getBytes())); + column.setTimestamp(0L); + tColumns.add(column); ColumnSlice slice = new ColumnSliceImpl(tColumns, se, ByteBufferSerializer.get()); - + ByteBuffer value = slice.getColumnByName("1").getValue(); Assert.assertEquals("colvalue", se.fromByteBuffer(value)); value.rewind(); diff --git a/core/src/test/java/me/prettyprint/cassandra/model/SuperColumnSliceTest.java b/core/src/test/java/me/prettyprint/cassandra/model/SuperColumnSliceTest.java index 0455c3d77..0b9980f35 100644 --- a/core/src/test/java/me/prettyprint/cassandra/model/SuperColumnSliceTest.java +++ b/core/src/test/java/me/prettyprint/cassandra/model/SuperColumnSliceTest.java @@ -34,7 +34,9 @@ public void testConstruction() { Assert.assertTrue(slice.getSuperColumns().isEmpty()); // non-empty one - Column c = new Column(le.toByteBuffer(5L), be.toByteBuffer(ByteBuffer.wrap(new byte[] { 1 })), 2L); + Column c = new Column(le.toByteBuffer(5L)); + c.setValue(be.toByteBuffer(ByteBuffer.wrap(new byte[] { 1 }))); + c.setTimestamp(2L); tColumns.add(new SuperColumn(se.toByteBuffer("super"), Arrays.asList(c))); slice = new SuperSliceImpl(tColumns, se, le, be); Assert.assertEquals(1, slice.getSuperColumns().size()); diff --git a/core/src/test/java/me/prettyprint/cassandra/service/BatchMutationTest.java b/core/src/test/java/me/prettyprint/cassandra/service/BatchMutationTest.java index af527df5f..fa2667ae7 100644 --- a/core/src/test/java/me/prettyprint/cassandra/service/BatchMutationTest.java +++ b/core/src/test/java/me/prettyprint/cassandra/service/BatchMutationTest.java @@ -36,7 +36,9 @@ public void setup() { @Test public void testAddInsertion() { - Column column = new Column(StringSerializer.get().toByteBuffer("c_name"), StringSerializer.get().toByteBuffer("c_val"), System.currentTimeMillis()); + Column column = new Column(StringSerializer.get().toByteBuffer("c_name")); + column.setValue(StringSerializer.get().toByteBuffer("c_val")); + column.setTimestamp(System.currentTimeMillis()); batchMutate.addInsertion("key1", columnFamilies, column); // assert there is one outter map row with 'key' as the key Map>> mutationMap = batchMutate.getMutationMap(); @@ -45,23 +47,29 @@ public void testAddInsertion() { // add again with a different column and verify there is one key and two mutations underneath // for "standard1" - Column column2 = new Column(StringSerializer.get().toByteBuffer("c_name2"), StringSerializer.get().toByteBuffer("c_val2"), System.currentTimeMillis()); + Column column2 = new Column(StringSerializer.get().toByteBuffer("c_name2")); + column2.setValue(StringSerializer.get().toByteBuffer("c_val2")); + column2.setTimestamp(System.currentTimeMillis()); batchMutate.addInsertion("key1",columnFamilies, column2); assertEquals(2, mutationMap.get(StringSerializer.get().toByteBuffer("key1")).get("Standard1").size()); } @Test public void testAddSuperInsertion() { - SuperColumn sc = new SuperColumn(StringSerializer.get().toByteBuffer("c_name"), - Arrays.asList(new Column(StringSerializer.get().toByteBuffer("c_name"), StringSerializer.get().toByteBuffer("c_val"), System.currentTimeMillis()))); + Column column = new Column(StringSerializer.get().toByteBuffer("c_name")); + column.setValue(StringSerializer.get().toByteBuffer("c_val")); + column.setTimestamp(System.currentTimeMillis()); + SuperColumn sc = new SuperColumn(StringSerializer.get().toByteBuffer("c_name"), Arrays.asList(column)); batchMutate.addSuperInsertion("key1", columnFamilies, sc); // assert there is one outter map row with 'key' as the key assertEquals(1, batchMutate.getMutationMap().get(StringSerializer.get().toByteBuffer("key1")).size()); // add again with a different column and verify there is one key and two mutations underneath // for "standard1" - SuperColumn sc2 = new SuperColumn(StringSerializer.get().toByteBuffer("c_name2"), - Arrays.asList(new Column(StringSerializer.get().toByteBuffer("c_name"), StringSerializer.get().toByteBuffer("c_val"), System.currentTimeMillis()))); + column = new Column(StringSerializer.get().toByteBuffer("c_name")); + column.setValue(StringSerializer.get().toByteBuffer("c_val")); + column.setTimestamp(System.currentTimeMillis()); + SuperColumn sc2 = new SuperColumn(StringSerializer.get().toByteBuffer("c_name2"), Arrays.asList(column)); batchMutate.addSuperInsertion("key1", columnFamilies, sc2); assertEquals(2, batchMutate.getMutationMap().get(StringSerializer.get().toByteBuffer("key1")).get("Standard1").size()); } @@ -84,16 +92,15 @@ public void testAddDeletion() { batchMutate.addDeletion("key1", columnFamilies, deletion); assertEquals(2,batchMutate.getMutationMap().get(StringSerializer.get().toByteBuffer("key1")).get("Standard1").size()); } - + @Test public void testIsEmpty() { assertTrue(batchMutate.isEmpty()); - + // Insert a column - Column c1 = new Column( - StringSerializer.get().toByteBuffer("c_name"), - StringSerializer.get().toByteBuffer("c_val"), - System.currentTimeMillis()); + Column c1 = new Column(StringSerializer.get().toByteBuffer("c_name")); + c1.setValue(StringSerializer.get().toByteBuffer("c_val")); + c1.setTimestamp(System.currentTimeMillis()); batchMutate.addInsertion("key1", columnFamilies, c1); assertFalse(batchMutate.isEmpty()); @@ -101,21 +108,21 @@ public void testIsEmpty() { CounterColumn cc1 = new CounterColumn(StringSerializer.get().toByteBuffer("c_name"), 13); batchMutate.addCounterInsertion("key1", columnFamilies, cc1); assertFalse(batchMutate.isEmpty()); - - - + + + } - + // ********** Test Counters related operations ****************** - + @Test public void testAddCounterInsertion() { // Insert a Counter. CounterColumn cc1 = new CounterColumn(StringSerializer.get().toByteBuffer("c_name"), 222); - + batchMutate.addCounterInsertion("key1", columnFamilies, cc1); - + // assert there is one outter map row with 'key' as the key Map>> mutationMap = batchMutate.getMutationMap(); assertEquals(1, mutationMap.get(StringSerializer.get().toByteBuffer("key1")).size()); @@ -126,16 +133,16 @@ public void testAddCounterInsertion() { batchMutate.addCounterInsertion("key1", columnFamilies, cc2); assertEquals(2, mutationMap.get(StringSerializer.get().toByteBuffer("key1")).get("Standard1").size()); } - + @Test public void testAddCounterDeletion() { - + Deletion counterDeletion = new Deletion(); - + SlicePredicate slicePredicate = new SlicePredicate(); slicePredicate.addToColumn_names(StringSerializer.get().toByteBuffer("c_name")); counterDeletion.setPredicate(slicePredicate); - + batchMutate.addDeletion("key1", columnFamilies, counterDeletion); assertEquals(1, batchMutate.getMutationMap().get(StringSerializer.get().toByteBuffer("key1")).size()); @@ -147,11 +154,11 @@ public void testAddCounterDeletion() { batchMutate.addDeletion("key1", columnFamilies, counterDeletion); assertEquals(2,batchMutate.getMutationMap().get(StringSerializer.get().toByteBuffer("key1")).get("Standard1").size()); } - + @Test public void testAddSuperCounterInsertion() { // Create 1 super counter. - CounterSuperColumn csc1 = new CounterSuperColumn(StringSerializer.get().toByteBuffer("c_name"), + CounterSuperColumn csc1 = new CounterSuperColumn(StringSerializer.get().toByteBuffer("c_name"), Arrays.asList(new CounterColumn(StringSerializer.get().toByteBuffer("c_name"), 123))); batchMutate.addSuperCounterInsertion("key1", columnFamilies, csc1); @@ -160,7 +167,7 @@ public void testAddSuperCounterInsertion() { // add again with a different column and verify there is one key and two mutations underneath // for "standard1" - CounterSuperColumn csc2 = new CounterSuperColumn(StringSerializer.get().toByteBuffer("c_name2"), + CounterSuperColumn csc2 = new CounterSuperColumn(StringSerializer.get().toByteBuffer("c_name2"), Arrays.asList(new CounterColumn(StringSerializer.get().toByteBuffer("c_name"), 456))); batchMutate.addSuperCounterInsertion("key1", columnFamilies, csc2); assertEquals(2, batchMutate.getMutationMap().get(StringSerializer.get().toByteBuffer("key1")).get("Standard1").size()); diff --git a/core/src/test/java/me/prettyprint/cassandra/service/KeyspaceTest.java b/core/src/test/java/me/prettyprint/cassandra/service/KeyspaceTest.java index 54d23169f..9531e01d0 100644 --- a/core/src/test/java/me/prettyprint/cassandra/service/KeyspaceTest.java +++ b/core/src/test/java/me/prettyprint/cassandra/service/KeyspaceTest.java @@ -60,8 +60,8 @@ public class KeyspaceTest extends BaseEmbededServerSetupTest { @Before public void setupCase() throws IllegalStateException, PoolExhaustedException, Exception { super.setupClient(); - - keyspace = new KeyspaceServiceImpl("Keyspace1", new QuorumAllConsistencyLevelPolicy(), + + keyspace = new KeyspaceServiceImpl("Keyspace1", new QuorumAllConsistencyLevelPolicy(), connectionManager, FailoverPolicy.ON_FAIL_TRY_ALL_AVAILABLE); } @@ -100,7 +100,7 @@ public void testInsertAndGetAndRemove() throws IllegalArgumentException, NoSuchE } } } - + @Test public void testInsertAndGetAndRemoveCounter() throws IllegalArgumentException, NoSuchElementException, IllegalStateException, HNotFoundException, Exception { @@ -109,11 +109,11 @@ public void testInsertAndGetAndRemoveCounter() throws IllegalArgumentException, // insert value ColumnParent cp = new ColumnParent("Counter1"); //cp.setColumn(bytes("testInsertAndGetAndRemoveCounter")); - + // Insert 3 counters for the same key - keyspace.addCounter("testInsertAndGetAndRemoveCounter_key1", cp, createCounterColumn("A", 5L)); - keyspace.addCounter("testInsertAndGetAndRemoveCounter_key1", cp, createCounterColumn("A", -1L)); - keyspace.addCounter("testInsertAndGetAndRemoveCounter_key1", cp, createCounterColumn("B", 10L)); + keyspace.addCounter("testInsertAndGetAndRemoveCounter_key1", cp, createCounterColumn("A", 5L)); + keyspace.addCounter("testInsertAndGetAndRemoveCounter_key1", cp, createCounterColumn("A", -1L)); + keyspace.addCounter("testInsertAndGetAndRemoveCounter_key1", cp, createCounterColumn("B", 10L)); // Total for counter A is (5 - 1) = 4. // Total for counter B is 10. @@ -122,27 +122,27 @@ public void testInsertAndGetAndRemoveCounter() throws IllegalArgumentException, CounterColumn counter = keyspace.getCounter("testInsertAndGetAndRemoveCounter_key1", cph); assertNotNull(counter); assertEquals(4, counter.value); - + cph.setColumn(ss.toByteBuffer("B")); counter = keyspace.getCounter("testInsertAndGetAndRemoveCounter_key1", cph); assertNotNull(counter); assertEquals(10, counter.value); - + // Reuse the ColumnPath associated to Conter B and remove only B. keyspace.removeCounter("testInsertAndGetAndRemoveCounter_key1", cph); - + // Fetch it and it should not exist try { keyspace.getCounter("testInsertAndGetAndRemoveCounter_key1", cph); } catch (HNotFoundException e) { // good } - + // Fetch Counter A again to verify I did not delete it cph.setColumn(ss.toByteBuffer("A")); counter = keyspace.getCounter("testInsertAndGetAndRemoveCounter_key1", cph); assertNotNull(counter); - + // Delete the whole row cph.column = null; keyspace.removeCounter("testInsertAndGetAndRemoveCounter_key1", cph); @@ -155,7 +155,7 @@ public void testInsertAndGetAndRemoveCounter() throws IllegalArgumentException, // good } } - + private CounterColumn createCounterColumn(String name, long value) { CounterColumn cc = new CounterColumn(); cc.setName(StringSerializer.get().toByteBuffer(name)); @@ -173,10 +173,9 @@ public void testInsertSuper() throws IllegalArgumentException, NoSuchElementExce // insert value ColumnParent columnParent = new ColumnParent("Super1"); columnParent.setSuper_column(StringSerializer.get().toByteBuffer("testInsertSuper_super")); - Column column = new Column(StringSerializer.get().toByteBuffer("testInsertSuper_column"), - StringSerializer.get().toByteBuffer("testInsertSuper_value"), connectionManager.createClock()); - - + Column column = new Column(StringSerializer.get().toByteBuffer("testInsertSuper_column")); + column.setValue(StringSerializer.get().toByteBuffer("testInsertSuper_value")); + column.setTimestamp(connectionManager.createClock()); keyspace.insert(StringSerializer.get().toByteBuffer("testInsertSuper_key"), columnParent, column); column.setName(StringSerializer.get().toByteBuffer("testInsertSuper_column2")); @@ -247,8 +246,9 @@ public void testBatchMutate() throws HectorException { ArrayList mutations = new ArrayList(10); for (int j = 0; j < 10; j++) { - Column col = new Column(StringSerializer.get().toByteBuffer("testBatchMutateColumn_" + j), - StringSerializer.get().toByteBuffer("testBatchMutateColumn_value_" + j), connectionManager.createClock()); + Column col = new Column(StringSerializer.get().toByteBuffer("testBatchMutateColumn_" + j)); + col.setValue(StringSerializer.get().toByteBuffer("testBatchMutateColumn_value_" + j)); + col.setTimestamp(connectionManager.createClock()); //list.add(col); ColumnOrSuperColumn cosc = new ColumnOrSuperColumn(); cosc.setColumn(col); @@ -317,8 +317,9 @@ public void testBatchMutateBatchMutation() throws HectorException { for (int i = 0; i < 10; i++) { for (int j = 0; j < 10; j++) { - Column col = new Column(StringSerializer.get().toByteBuffer("testBatchMutateColumn_" + j), - StringSerializer.get().toByteBuffer("testBatchMutateColumn_value_" + j), connectionManager.createClock()); + Column col = new Column(StringSerializer.get().toByteBuffer("testBatchMutateColumn_" + j)); + col.setValue(StringSerializer.get().toByteBuffer("testBatchMutateColumn_value_" + j)); + col.setTimestamp(connectionManager.createClock()); batchMutation.addInsertion("testBatchMutateColumn_" + i, columnFamilies, col); } } @@ -381,8 +382,9 @@ public void testBatchUpdateInsertAndDelOnSame() throws HectorException { for (int i = 0; i < 10; i++) { for (int j = 0; j < 10; j++) { - Column col = new Column(StringSerializer.get().toByteBuffer("testBatchMutateColumn_" + j), - StringSerializer.get().toByteBuffer("testBatchMutateColumn_value_" + j), connectionManager.createClock()); + Column col = new Column(StringSerializer.get().toByteBuffer("testBatchMutateColumn_" + j)); + col.setValue(StringSerializer.get().toByteBuffer("testBatchMutateColumn_value_" + j)); + col.setTimestamp(connectionManager.createClock()); batchMutation.addInsertion("testBatchMutateColumn_" + i, columnFamilies, col); } } @@ -478,7 +480,7 @@ public void testGetSlice() throws HectorException { keyspace.remove("testGetSlice_", cp); keyspace.remove("testGetSlice", cp); } - + @Test public void testGetCounterSlice() throws HectorException { // insert value @@ -487,8 +489,8 @@ public void testGetCounterSlice() throws HectorException { for (int i = 0; i < 100; i++) { ColumnParent cp = new ColumnParent("Counter1"); - keyspace.addCounter("testGetCounterSlice", cp, createCounterColumn("testGetCounterSlice_" + i, i)); - + keyspace.addCounter("testGetCounterSlice", cp, createCounterColumn("testGetCounterSlice_" + i, i)); + if (i < 50) { // I want to query only 50. columnnames.add(ss.toByteBuffer("testGetCounterSlice_" + i)); @@ -497,7 +499,7 @@ public void testGetCounterSlice() throws HectorException { // Query 50 counters. From testGetCounterSlice_0 to testGetCounterSlice_49. ColumnParent clp = new ColumnParent("Counter1"); - + // TODO (patricioe) Slice by range will be in the next snapshot. //SliceRange sr = new SliceRange(ByteBuffer.wrap(new byte[0]), ByteBuffer.wrap(new byte[49]), false, 150); SlicePredicate sp = new SlicePredicate(); @@ -509,7 +511,7 @@ public void testGetCounterSlice() throws HectorException { assertEquals(50, cols.size()); Collections.sort(columnnames); - + ArrayList gotlist = new ArrayList(50); for (int i = 0; i < 50; i++) { CounterColumn cc = cols.get(i); @@ -931,7 +933,7 @@ public void testMultigetCount() { assertEquals(5,counts.size()); assertEquals(new Integer(25),counts.entrySet().iterator().next().getValue()); - slicePredicate.setSlice_range(new SliceRange(StringSerializer.get().toByteBuffer(""), + slicePredicate.setSlice_range(new SliceRange(StringSerializer.get().toByteBuffer(""), StringSerializer.get().toByteBuffer(""), false, 5)); counts = keyspace.multigetCount(keys, clp, slicePredicate); diff --git a/object-mapper/pom.xml b/object-mapper/pom.xml index 6b7f768a1..71557edfe 100644 --- a/object-mapper/pom.xml +++ b/object-mapper/pom.xml @@ -29,7 +29,7 @@ - + org.slf4j @@ -65,6 +65,12 @@ provided + + org.apache.cassandra + cassandra-thrift + 0.8.0-beta1 + test + log4j log4j @@ -82,7 +88,7 @@ mockito-all 1.8.2 test - + org.slf4j slf4j-log4j12 @@ -94,8 +100,7 @@ - - + org.apache.cassandra cassandra-javautils test @@ -139,7 +144,7 @@ 2.0.1 test-jar test - + diff --git a/object-mapper/src/test/java/me/prettyprint/hom/CassandraTestBase.java b/object-mapper/src/test/java/me/prettyprint/hom/CassandraTestBase.java index 4e1c4a3d3..9e99b2e00 100644 --- a/object-mapper/src/test/java/me/prettyprint/hom/CassandraTestBase.java +++ b/object-mapper/src/test/java/me/prettyprint/hom/CassandraTestBase.java @@ -47,28 +47,23 @@ public static void startCassandraInstance(String pathToDataDir) throws TTranspor cleaner.prepare(); EmbeddedCassandraService cassandra = new EmbeddedCassandraService(); try { - cassandra.init(); + cassandraStarted = true; + cassandra.start(); } - catch (TTransportException e) { + catch (IOException e) { System.out.println( "Could not initialize Cassandra"); e.printStackTrace(); throw e; } - cassandraStarted = true; - - Thread t = new Thread(cassandra); - t.setName(cassandra.getClass().getSimpleName()); - t.setDaemon(true); - t.start(); - System.out.println( "Successfully started Cassandra"); } public static void createKeyspace(Cluster cluster, String name, String strategy, int replicationFactor, List cfDefList) { try { - KsDef ksDef = new KsDef(name, strategy, replicationFactor, cfDefList); + KsDef ksDef = new KsDef(name, strategy, cfDefList); + ksDef.setReplication_factor(replicationFactor); cluster.addKeyspace(new ThriftKsDef(ksDef)); return; } @@ -94,16 +89,16 @@ public static void setupKeyspace() throws TTransportException, InterruptedException, NoSuchMethodException, IllegalAccessException, InvocationTargetException { startCassandraInstance("tmp/var/lib/cassandra"); - + ArrayList cfDefList = new ArrayList(2); cfDefList.add(new CfDef("TestKeyspace", "TestBeanColumnFamily").setComparator_type(BytesType.class.getSimpleName()) .setKey_cache_size(0).setRow_cache_size(0).setGc_grace_seconds(86400)); cfDefList.add(new CfDef("TestKeyspace", "CustomIdColumnFamily").setComparator_type(BytesType.class.getSimpleName()) .setKey_cache_size(0).setRow_cache_size(0).setGc_grace_seconds(86400)); cfDefList.add(new CfDef("TestKeyspace", "SimpleTestBeanColumnFamily").setComparator_type(BytesType.class.getSimpleName()) - .setKey_cache_size(0).setRow_cache_size(0).setGc_grace_seconds(86400)); + .setKey_cache_size(0).setRow_cache_size(0).setGc_grace_seconds(86400)); cfDefList.add(new CfDef("TestKeyspace", "SimpleRelationshipBeanColumnFamily").setComparator_type(BytesType.class.getSimpleName()) - .setKey_cache_size(0).setRow_cache_size(0).setGc_grace_seconds(86400)); + .setKey_cache_size(0).setRow_cache_size(0).setGc_grace_seconds(86400)); cfDefList.add(new CfDef("TestKeyspace", "NoAnonymousColumnFamily").setComparator_type(BytesType.class.getSimpleName()) .setKey_cache_size(0).setRow_cache_size(0).setGc_grace_seconds(86400)); cfDefList.add(new CfDef("TestKeyspace", "ComplexColumnFamily").setComparator_type(BytesType.class.getSimpleName()) diff --git a/object-mapper/src/test/resources/cassandra.yaml b/object-mapper/src/test/resources/cassandra.yaml index 1bf3adc8f..da813423c 100644 --- a/object-mapper/src/test/resources/cassandra.yaml +++ b/object-mapper/src/test/resources/cassandra.yaml @@ -1,9 +1,9 @@ -# Cassandra storage config YAML +# Cassandra storage config YAML -#NOTE !!!!!!!! NOTE +#NOTE !!!!!!!! NOTE # See http://wiki.apache.org/cassandra/StorageConfiguration for # full explanations of configuration directives -#NOTE !!!!!!!! NOTE +#NOTE !!!!!!!! NOTE # The name of the cluster. This is mainly used to prevent machines in # one logical cluster from joining another. @@ -58,10 +58,10 @@ commitlog_directory: target/cassandra-data/commitlog # saved caches saved_caches_directory: target/cassandra-data/saved_caches -# Size to allow commitlog to grow to before creating a new segment +# Size to allow commitlog to grow to before creating a new segment commitlog_rotation_threshold_in_mb: 128 -# commitlog_sync may be either "periodic" or "batch." +# commitlog_sync may be either "periodic" or "batch." # When in batch mode, Cassandra won't ack writes until the commit log # has been fsynced to disk. It will wait up to # CommitLogSyncBatchWindowInMS milliseconds for other writes, before @@ -73,12 +73,17 @@ commitlog_sync: periodic # milliseconds. commitlog_sync_period_in_ms: 10000 -# Addresses of hosts that are deemed contact points. -# Cassandra nodes use this list of hosts to find each other and learn -# the topology of the ring. You must change this if you are running -# multiple nodes! -seeds: - - 127.0.0.1 +# any class that implements the SeedProvider interface and has a constructor that takes a Map of +# parameters will do. +seed_provider: + # Addresses of hosts that are deemed contact points. + # Cassandra nodes use this list of hosts to find each other and learn + # the topology of the ring. You must change this if you are running + # multiple nodes! + - class_name: org.apache.cassandra.locator.SimpleSeedProvider + parameters: + # seeds is actually a comma-delimited list of addresses. + - seeds: "127.0.0.1" # Access mode. mmapped i/o is substantially faster, but only practical on # a 64bit machine (which notably does not include EC2 "small" instances) @@ -105,7 +110,7 @@ concurrent_writes: 8 # By default this will be set to the amount of data directories defined. #memtable_flush_writers: 1 -# Buffer size to use when performing contiguous column slices. +# Buffer size to use when performing contiguous column slices. # Increase this to the size of the column slices you typically perform sliced_buffer_size_in_kb: 64 @@ -115,7 +120,7 @@ storage_port: 7001 # Address to bind to and tell other Cassandra nodes to connect to. You # _must_ change this if you want multiple nodes to be able to # communicate! -# +# # Leaving it blank leaves it up to InetAddress.getLocalHost(). This # will always do the Right Thing *if* the node is properly configured # (hostname, name resolution, etc), and the Right Thing is to use the @@ -127,7 +132,7 @@ listen_address: localhost # The address to bind the Thrift RPC service to -- clients connect # here. Unlike ListenAddress above, you *can* specify 0.0.0.0 here if # you want Thrift to listen on all interfaces. -# +# # Leaving this blank has the same effect it does for ListenAddress, # (i.e. it will be based on the configured hostname of the node). rpc_address: localhost @@ -178,7 +183,7 @@ column_index_size_in_kb: 64 # will be logged specifying the row key. in_memory_compaction_limit_in_mb: 64 -# Time to wait for a reply from other nodes before failing the command +# Time to wait for a reply from other nodes before failing the command rpc_timeout_in_ms: 10000 # phi value that must be reached for a host to be marked down. @@ -208,7 +213,7 @@ endpoint_snitch: org.apache.cassandra.locator.SimpleSnitch dynamic_snitch: true # controls how often to perform the more expensive part of host score # calculation -dynamic_snitch_update_interval_in_ms: 100 +dynamic_snitch_update_interval_in_ms: 100 # controls how often to reset all host scores, allowing a bad host to # possibly recover dynamic_snitch_reset_interval_in_ms: 600000 @@ -238,7 +243,7 @@ request_scheduler: org.apache.cassandra.scheduler.NoScheduler # NoScheduler - Has no options # RoundRobin # - throttle_limit -- The throttle_limit is the number of in-flight -# requests per client. Requests beyond +# requests per client. Requests beyond # that limit are queued up until # running requests can complete. # The value of 80 here is twice the number of @@ -266,7 +271,7 @@ request_scheduler: org.apache.cassandra.scheduler.NoScheduler # the index is at the cost of space. index_interval: 128 -# A ColumnFamily is the Cassandra concept closest to a relational table. +# A ColumnFamily is the Cassandra concept closest to a relational table. # # Keyspaces are separate groups of ColumnFamilies. Except in very # unusual circumstances you will have one Keyspace per application. @@ -277,8 +282,8 @@ index_interval: 128 # - replica_placement_strategy: the class that determines how replicas # are distributed among nodes. Contains both the class as well as # configuration information. Must extend AbstractReplicationStrategy. -# Out of the box, Cassandra provides -# * org.apache.cassandra.locator.SimpleStrategy +# Out of the box, Cassandra provides +# * org.apache.cassandra.locator.SimpleStrategy # * org.apache.cassandra.locator.NetworkTopologyStrategy # * org.apache.cassandra.locator.OldNetworkTopologyStrategy # @@ -286,7 +291,7 @@ index_interval: 128 # replica at the node whose token is closest to the key (as determined # by the Partitioner), and additional replicas on subsequent nodes # along the ring in increasing Token order. -# +# # With NetworkTopologyStrategy, # for each datacenter, you can specify how many replicas you want # on a per-keyspace basis. Replicas are placed on different racks @@ -299,8 +304,8 @@ index_interval: 128 # DC1 : 3 # DC2 : 2 # DC3 : 1 -# -# OldNetworkToplogyStrategy [formerly RackAwareStrategy] +# +# OldNetworkToplogyStrategy [formerly RackAwareStrategy] # places one replica in each of two datacenters, and the third on a # different rack in in the first. Additional datacenters are not # guaranteed to get a replica. Additional replicas after three are placed @@ -318,7 +323,7 @@ index_interval: 128 # and IntegerType (a generic variable-length integer type). # You can also specify the fully-qualified class name to a class of # your choice extending org.apache.cassandra.db.marshal.AbstractType. -# +# # ColumnFamily optional parameters: # - keys_cached: specifies the number of keys per sstable whose # locations we keep in memory in "mostly LRU" order. (JUST the key @@ -330,7 +335,7 @@ index_interval: 128 # or ColumnFamilies with high write:read ratios. Specify a fraction # (value less than 1) or an absolute number of rows to cache. # Defaults to 0. (i.e. row caching is off by default) -# - comment: used to attach additional human-readable information about +# - comment: used to attach additional human-readable information about # the column family to its definition. # - read_repair_chance: specifies the probability with which read # repairs should be invoked on non-quorum reads. must be between 0 @@ -352,7 +357,7 @@ index_interval: 128 # row caches. The row caches can be saved periodically and if one # exists on startup it will be loaded. # - key_cache_save_period_in_seconds: number of seconds between saving -# key caches. The key caches can be saved periodically and if one +# key caches. The key caches can be saved periodically and if one # exists on startup it will be loaded. # - memtable_flush_after_mins: The maximum time to leave a dirty table # unflushed. This should be large enough that it won't cause a flush diff --git a/pom.xml b/pom.xml index cb8b50d72..05a618bda 100644 --- a/pom.xml +++ b/pom.xml @@ -88,7 +88,7 @@ org.apache.cassandra cassandra-all - 0.7.0 + 0.8.0-beta1 org.apache.cassandra @@ -96,9 +96,9 @@ 0.7.0 - org.apache.cassandra.deps + org.apache.thrift libthrift - 0.5.0 + 0.6.1 com.github.stephenc.high-scale-lib @@ -137,8 +137,8 @@ mockito-all 1.8.2 test - - + + From 6b91d934299203e1a3e48ba7ccc2e3d2d7874123 Mon Sep 17 00:00:00 2001 From: zznate Date: Fri, 29 Apr 2011 11:35:06 -0500 Subject: [PATCH 129/144] additional cleanup from bump of 0.8 to beta, fixes in HOM implied by such --- core/pom.xml | 5 ++ object-mapper/pom.xml | 4 +- .../mycompany/furniture/FurnitureTest.java | 15 ++---- .../me/prettyprint/hom/CassandraTestBase.java | 21 ++++---- .../src/test/resources/cassandra.yaml | 50 +------------------ pom.xml | 12 +++++ 6 files changed, 31 insertions(+), 76 deletions(-) diff --git a/core/pom.xml b/core/pom.xml index d356d1b94..44daa1eb0 100644 --- a/core/pom.xml +++ b/core/pom.xml @@ -286,5 +286,10 @@ riptano http://mvn.riptano.com/content/repositories/public/ + + apache-staging + apache-staging + https://repository.apache.org/content/repositories/orgapachecassandra-114 + diff --git a/object-mapper/pom.xml b/object-mapper/pom.xml index 71557edfe..b7681ad01 100644 --- a/object-mapper/pom.xml +++ b/object-mapper/pom.xml @@ -39,7 +39,7 @@ me.prettyprint hector-core - 0.7.0-28 + 0.8.0-1-SNAPSHOT org.slf4j @@ -68,8 +68,6 @@ org.apache.cassandra cassandra-thrift - 0.8.0-beta1 - test log4j diff --git a/object-mapper/src/test/java/com/mycompany/furniture/FurnitureTest.java b/object-mapper/src/test/java/com/mycompany/furniture/FurnitureTest.java index 485298526..18cee5942 100644 --- a/object-mapper/src/test/java/com/mycompany/furniture/FurnitureTest.java +++ b/object-mapper/src/test/java/com/mycompany/furniture/FurnitureTest.java @@ -15,12 +15,12 @@ import org.apache.cassandra.db.marshal.BytesType; import org.apache.cassandra.thrift.CfDef; import org.apache.thrift.transport.TTransportException; +import org.junit.Before; import org.junit.BeforeClass; import org.junit.Test; public class FurnitureTest extends CassandraTestBase { - static Keyspace keyspace; static EntityManagerImpl entityMgr; @Test @@ -71,18 +71,9 @@ public void testFurniture() { // -------------- - @BeforeClass - public static void setup() throws TTransportException, SecurityException, IllegalArgumentException, + @Before + public void setup() throws TTransportException, SecurityException, IllegalArgumentException, IOException, InterruptedException, NoSuchMethodException, IllegalAccessException, InvocationTargetException { - startCassandraInstance("target/cassandra-data"); - ArrayList cfDefList = new ArrayList(2); - cfDefList.add(new CfDef("TestKeyspace", "Furniture").setComparator_type(BytesType.class.getSimpleName()) - .setKey_cache_size(0).setRow_cache_size(0).setGc_grace_seconds(86400)); - - Cluster cluster = HFactory.getOrCreateCluster("TestPool", "localhost:9170"); - createKeyspace(cluster, "TestKeyspace", "org.apache.cassandra.locator.SimpleStrategy", 1, cfDefList); - keyspace = HFactory.createKeyspace("TestKeyspace", cluster); - entityMgr = new EntityManagerImpl(keyspace, "com.mycompany.furniture"); } diff --git a/object-mapper/src/test/java/me/prettyprint/hom/CassandraTestBase.java b/object-mapper/src/test/java/me/prettyprint/hom/CassandraTestBase.java index 9e99b2e00..cd310504f 100644 --- a/object-mapper/src/test/java/me/prettyprint/hom/CassandraTestBase.java +++ b/object-mapper/src/test/java/me/prettyprint/hom/CassandraTestBase.java @@ -8,10 +8,12 @@ import me.prettyprint.cassandra.service.ThriftCfDef; import me.prettyprint.cassandra.service.ThriftKsDef; +import me.prettyprint.cassandra.testutils.EmbeddedServerHelper; import me.prettyprint.hector.api.Cluster; import me.prettyprint.hector.api.Keyspace; import me.prettyprint.hector.api.factory.HFactory; +import org.apache.cassandra.config.ConfigurationException; import org.apache.cassandra.contrib.utils.service.CassandraServiceDataCleaner; import org.apache.cassandra.db.marshal.BytesType; import org.apache.cassandra.io.util.FileUtils; @@ -25,6 +27,7 @@ public class CassandraTestBase { protected static boolean cassandraStarted = false; protected static Keyspace keyspace; protected static Cluster cluster; + protected static EmbeddedServerHelper embedded; public static void startCassandraInstance(String pathToDataDir) throws TTransportException, IOException, InterruptedException, SecurityException, IllegalArgumentException, NoSuchMethodException, @@ -43,20 +46,12 @@ public static void startCassandraInstance(String pathToDataDir) throws TTranspor // eat } - CassandraServiceDataCleaner cleaner = new CassandraServiceDataCleaner(); - cleaner.prepare(); - EmbeddedCassandraService cassandra = new EmbeddedCassandraService(); + embedded = new EmbeddedServerHelper(); try { - cassandraStarted = true; - cassandra.start(); + embedded.setup(); + } catch (ConfigurationException ce) { + throw new RuntimeException(ce); } - catch (IOException e) { - System.out.println( "Could not initialize Cassandra"); - e.printStackTrace(); - throw e; - } - - System.out.println( "Successfully started Cassandra"); } public static void createKeyspace(Cluster cluster, String name, String strategy, int replicationFactor, @@ -103,6 +98,8 @@ public static void setupKeyspace() throws TTransportException, .setKey_cache_size(0).setRow_cache_size(0).setGc_grace_seconds(86400)); cfDefList.add(new CfDef("TestKeyspace", "ComplexColumnFamily").setComparator_type(BytesType.class.getSimpleName()) .setKey_cache_size(0).setRow_cache_size(0).setGc_grace_seconds(86400)); + cfDefList.add(new CfDef("TestKeyspace", "Furniture").setComparator_type(BytesType.class.getSimpleName()) + .setKey_cache_size(0).setRow_cache_size(0).setGc_grace_seconds(86400)); cluster = HFactory.getOrCreateCluster("TestPool", "localhost:9161"); createKeyspace(cluster, "TestKeyspace", "org.apache.cassandra.locator.SimpleStrategy", 1, cfDefList); keyspace = HFactory.createKeyspace("TestKeyspace", cluster); diff --git a/object-mapper/src/test/resources/cassandra.yaml b/object-mapper/src/test/resources/cassandra.yaml index da813423c..521d8edd9 100644 --- a/object-mapper/src/test/resources/cassandra.yaml +++ b/object-mapper/src/test/resources/cassandra.yaml @@ -371,52 +371,4 @@ index_interval: 128 # NOTE: this keyspace definition is for demonstration purposes only. # Cassandra will not load these definitions during startup. See # http://wiki.apache.org/cassandra/FAQ#no_keyspaces for an explanation. -keyspaces: - - name: Keyspace1 - replica_placement_strategy: org.apache.cassandra.locator.SimpleStrategy - replication_factor: 1 - column_families: - - name: Standard1 - compare_with: BytesType - keys_cached: 10000 - rows_cached: 1000 - row_cache_save_period_in_seconds: 0 - key_cache_save_period_in_seconds: 3600 - memtable_flush_after_mins: 59 - memtable_throughput_in_mb: 255 - memtable_operations_in_millions: 0.29 - - - name: Standard2 - compare_with: UTF8Type - read_repair_chance: 0.1 - keys_cached: 100 - gc_grace_seconds: 0 - min_compaction_threshold: 5 - max_compaction_threshold: 31 - - - name: StandardByUUID1 - compare_with: TimeUUIDType - - - name: Super1 - column_type: Super - compare_with: BytesType - compare_subcolumns_with: BytesType - - - name: Super2 - column_type: Super - compare_subcolumns_with: UTF8Type - rows_cached: 10000 - keys_cached: 50 - comment: 'A column family with supercolumns, whose column and subcolumn names are UTF8 strings' - - - name: Super3 - column_type: Super - compare_with: LongType - comment: 'A column family with supercolumns, whose column names are Longs (8 bytes)' - - - name: Indexed1 - default_validation_class: LongType - column_metadata: - - name: birthdate - validator_class: LongType - index_type: KEYS + diff --git a/pom.xml b/pom.xml index 05a618bda..9fc8e1375 100644 --- a/pom.xml +++ b/pom.xml @@ -90,6 +90,11 @@ cassandra-all 0.8.0-beta1 + + org.apache.cassandra + cassandra-thrift + 0.8.0-beta1 + org.apache.cassandra cassandra-javautils @@ -321,5 +326,12 @@ 3.0.0.RELEASE 1.6.1 + + + apache-staging + apache-staging + https://repository.apache.org/content/repositories/orgapachecassandra-114 + + From 1f18e6fcc580f1e8efc07f42981fa44f38eabe12 Mon Sep 17 00:00:00 2001 From: patricioe Date: Mon, 16 May 2011 10:43:32 -0700 Subject: [PATCH 130/144] Add ability to return a timestamp based on UUID --- .../cassandra/utils/TimeUUIDUtils.java | 10 ++++- .../cassandra/utils/TimeUUIDUtilsTest.java | 41 ++++++++++++++++++- 2 files changed, 48 insertions(+), 3 deletions(-) diff --git a/core/src/main/java/me/prettyprint/cassandra/utils/TimeUUIDUtils.java b/core/src/main/java/me/prettyprint/cassandra/utils/TimeUUIDUtils.java index e683f57ae..e4536fda2 100644 --- a/core/src/main/java/me/prettyprint/cassandra/utils/TimeUUIDUtils.java +++ b/core/src/main/java/me/prettyprint/cassandra/utils/TimeUUIDUtils.java @@ -15,6 +15,8 @@ * */ public final class TimeUUIDUtils { + + static final long NUM_100NS_INTERVALS_SINCE_UUID_EPOCH = 0x01b21dd213814000L; /** * Gets a new and unique time uuid in milliseconds. It is useful to use in a TimeUUIDType sorted column family. @@ -54,7 +56,7 @@ private static long createTime(long currentTime) { long time; // UTC time - long timeToUse = (currentTime * 10000) + 0x01B21DD213814000L; + long timeToUse = (currentTime * 10000) + NUM_100NS_INTERVALS_SINCE_UUID_EPOCH; // time low time = timeToUse << 32; @@ -86,7 +88,11 @@ public static java.util.UUID toUUID(byte[] uuid) { * @return a long representing the time */ public static long getTimeFromUUID(byte[] uuid) { - return TimeUUIDUtils.toUUID(uuid).timestamp(); + return getTimeFromUUID(TimeUUIDUtils.toUUID(uuid)); + } + + public static long getTimeFromUUID(UUID uuid) { + return (uuid.timestamp() - NUM_100NS_INTERVALS_SINCE_UUID_EPOCH) / 10000; } /** diff --git a/core/src/test/java/me/prettyprint/cassandra/utils/TimeUUIDUtilsTest.java b/core/src/test/java/me/prettyprint/cassandra/utils/TimeUUIDUtilsTest.java index 6529f565d..f3f8e0545 100644 --- a/core/src/test/java/me/prettyprint/cassandra/utils/TimeUUIDUtilsTest.java +++ b/core/src/test/java/me/prettyprint/cassandra/utils/TimeUUIDUtilsTest.java @@ -3,13 +3,17 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertTrue; +import java.util.Date; import java.util.UUID; +import me.prettyprint.cassandra.BaseEmbededServerSetupTest; import me.prettyprint.cassandra.service.clock.MicrosecondsClockResolution; import me.prettyprint.cassandra.service.clock.MicrosecondsSyncClockResolution; import me.prettyprint.hector.api.ClockResolution; import org.junit.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import com.eaio.uuid.UUIDGen; @@ -20,6 +24,8 @@ * */ public class TimeUUIDUtilsTest { + + private static Logger log = LoggerFactory.getLogger(TimeUUIDUtilsTest.class); /** * This test must be placed FIRST. Please don't change the order. @@ -53,7 +59,7 @@ public void testTimeUUIDAsByteArray() { // Used the previously generated UUID, convert to array and back to UUID. Then compare their times. long timeInUUID = TimeUUIDUtils.getTimeFromUUID(TimeUUIDUtils.asByteArray(uuid)); - assertEquals(uuid.timestamp(), timeInUUID); + assertEquals((uuid.timestamp() - 0x01b21dd213814000L) / 10000, timeInUUID); } @Test @@ -62,7 +68,40 @@ public void testTimeUUIDAsByteBuffer() { UUID actualUuid = TimeUUIDUtils.uuid(TimeUUIDUtils.asByteBuffer(expectedUuid)); assertEquals(expectedUuid, actualUuid); } + + @Test + public void testTimestampConsistency() { + final long originalTime = System.currentTimeMillis(); + + log.info("Original Time: " + originalTime); + log.info("----"); + + final UUID u1 = TimeUUIDUtils.getTimeUUID(originalTime); + + log.info("Java UUID: " + u1); + log.info("Java UUID timestamp: " + u1.timestamp()); + log.info("Date: "+ new Date(u1.timestamp())); + + log.info("----"); + final com.eaio.uuid.UUID u = new com.eaio.uuid.UUID(originalTime, 0); + log.info("eaio UUID: " + u); + log.info("eaio UUID timestamp: " + u.getTime()); + log.info("Date: "+ new Date(u.getTime())); + log.info("----"); + final long actual1 = TimeUUIDUtils.getTimeFromUUID(TimeUUIDUtils.asByteArray(u1)); + log.info("Java UUID to bytes to time: " + actual1); + log.info("Java UUID to bytes time to Date: " + new Date(actual1)); + + log.info("----"); + + final long actual2 = TimeUUIDUtils.getTimeFromUUID(u1); + log.info("Java UUID to time: " + actual2); + log.info("Java UUID to time to Date: " + new Date(actual2)); + + assertEquals(originalTime, actual1); + assertEquals(originalTime, actual2); + } } From 25c2810ab1f656783fa38a4bef994c7511be8ac8 Mon Sep 17 00:00:00 2001 From: Ed Anuff Date: Mon, 11 Apr 2011 11:44:07 -0700 Subject: [PATCH 131/144] warning message when adding zero-length bytestype components --- .../hector/api/beans/AbstractComposite.java | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/core/src/main/java/me/prettyprint/hector/api/beans/AbstractComposite.java b/core/src/main/java/me/prettyprint/hector/api/beans/AbstractComposite.java index e50da8c69..2f9abd258 100644 --- a/core/src/main/java/me/prettyprint/hector/api/beans/AbstractComposite.java +++ b/core/src/main/java/me/prettyprint/hector/api/beans/AbstractComposite.java @@ -19,7 +19,6 @@ import java.util.Iterator; import java.util.List; import java.util.Map; -import java.util.logging.Logger; import me.prettyprint.cassandra.serializers.AsciiSerializer; import me.prettyprint.cassandra.serializers.BigIntegerSerializer; @@ -32,6 +31,8 @@ import me.prettyprint.hector.api.Serializer; import org.apache.cassandra.utils.ByteBufferUtil; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import com.google.common.collect.BiMap; import com.google.common.collect.ImmutableBiMap; @@ -49,6 +50,8 @@ public abstract class AbstractComposite extends AbstractList implements Comparable { + private static Logger log = LoggerFactory.getLogger(AbstractComposite.class); + public enum ComponentEquality { LESS_THAN_EQUAL((byte) -1), EQUAL((byte) 0), GREATER_THAN_EQUAL((byte) 1); @@ -73,9 +76,6 @@ public static ComponentEquality fromByte(byte equality) { } } - static final Logger logger = Logger.getLogger(AbstractComposite.class - .getName()); - public static final BiMap, String> DEFAULT_SERIALIZER_TO_COMPARATOR_MAPPING = new ImmutableBiMap.Builder, String>() .put(AsciiSerializer.class, AsciiSerializer.get().getComparatorType().getTypeName()) @@ -668,6 +668,9 @@ public ByteBuffer serialize() { out.writeShort((short) comparator.length()); out.write(ByteBufferUtil.bytes(comparator)); } + if (comparator.equals(BYTESTYPE.getTypeName()) && (cb.remaining() == 0)) { + log.warn("Writing zero-length BytesType, probably an error"); + } } out.writeShort((short) cb.remaining()); out.write(cb.slice()); From 33b925f3d5bf7a41aec7143962a7b1beb0b2624f Mon Sep 17 00:00:00 2001 From: bburruss Date: Thu, 19 May 2011 13:39:32 -0700 Subject: [PATCH 132/144] changed UnsupportedException to RuntimeException since it is openjpa specific --- .../me/prettyprint/hom/cache/InheritanceParserValidator.java | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/object-mapper/src/main/java/me/prettyprint/hom/cache/InheritanceParserValidator.java b/object-mapper/src/main/java/me/prettyprint/hom/cache/InheritanceParserValidator.java index 1cea2c8f7..89552872b 100644 --- a/object-mapper/src/main/java/me/prettyprint/hom/cache/InheritanceParserValidator.java +++ b/object-mapper/src/main/java/me/prettyprint/hom/cache/InheritanceParserValidator.java @@ -11,7 +11,6 @@ import me.prettyprint.hom.CFMappingDef; import me.prettyprint.hom.ClassCacheMgr; -import org.apache.openjpa.util.UnsupportedException; /** * Parse, validate, and set defaults if needed for Inheritance functionality. @@ -38,7 +37,7 @@ private void parseInheritanceAnnotation(Inheritance anno, CFMappingDef cf if (InheritanceType.SINGLE_TABLE == anno.strategy()) { cfMapDef.setInheritanceType(InheritanceType.SINGLE_TABLE); } else { - throw new UnsupportedException("Hector object mapper only supports " + throw new RuntimeException("Hector object mapper only supports " + InheritanceType.SINGLE_TABLE + " inheritance at the moment"); } } From 6ffa130ea963d896206056b26e4bd3ba33e26f11 Mon Sep 17 00:00:00 2001 From: btoddb Date: Thu, 19 May 2011 13:43:25 -0700 Subject: [PATCH 133/144] was never setting cassandraStarted to true causing unit tests to try and start server again. this is a problem in eclipse (or possibly other IDEs) but not using maven cmd line "mvn clean package" --- .../src/test/java/me/prettyprint/hom/CassandraTestBase.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/object-mapper/src/test/java/me/prettyprint/hom/CassandraTestBase.java b/object-mapper/src/test/java/me/prettyprint/hom/CassandraTestBase.java index cd310504f..c0139a104 100644 --- a/object-mapper/src/test/java/me/prettyprint/hom/CassandraTestBase.java +++ b/object-mapper/src/test/java/me/prettyprint/hom/CassandraTestBase.java @@ -52,6 +52,8 @@ public static void startCassandraInstance(String pathToDataDir) throws TTranspor } catch (ConfigurationException ce) { throw new RuntimeException(ce); } + + cassandraStarted = true; } public static void createKeyspace(Cluster cluster, String name, String strategy, int replicationFactor, From 04b28483cbf7fb0f21ad4050a0cea44289568491 Mon Sep 17 00:00:00 2001 From: btoddb Date: Thu, 19 May 2011 13:57:43 -0700 Subject: [PATCH 134/144] added ignore for 'target' dir --- object-mapper/.gitignore | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 object-mapper/.gitignore diff --git a/object-mapper/.gitignore b/object-mapper/.gitignore new file mode 100644 index 000000000..034f39c0b --- /dev/null +++ b/object-mapper/.gitignore @@ -0,0 +1,15 @@ +.idea +.DS_Store +hector.iml +releases +target +tmp +bin +.classpath +.project +.settings +out +*.ipr +*.iws +*.iml +.springBeans From 9c12ef87d3b8a34793d8ba2d3198bdab5fd737de Mon Sep 17 00:00:00 2001 From: btoddb Date: Thu, 19 May 2011 13:59:22 -0700 Subject: [PATCH 135/144] removed openjpa and JPA 2.0 targeted code just to be clear this version isn't supporting it. still JPA influenced with the use of its annotations --- .../hom/CassandraPersistenceProvider.java | 53 ---- .../hom/EntityManagerFactoryImpl.java | 127 ---------- .../hom/openjpa/CassandraStore.java | 131 ---------- .../openjpa/CassandraStoreConfiguration.java | 24 -- .../hom/openjpa/CassandraStoreManager.java | 237 ------------------ .../prettyprint/hom/openjpa/EntityFacade.java | 101 -------- .../prettyprint/hom/openjpa/MappingUtils.java | 194 -------------- .../hom/service/EntitySchemaManager.java | 63 ----- .../hom/service/EntitySchemaStatus.java | 21 -- .../hom/EntityManagerFactoryTest.java | 47 ---- ...KeyConcatenationDelimiterStrategyTest.java | 1 - .../hom/SpringContainerInitalizationTest.java | 60 ----- .../hom/openjpa/EntityFacadeTest.java | 49 ---- .../hom/openjpa/FullContainerTest.java | 63 ----- .../hom/openjpa/ManagedEntityTestBase.java | 18 -- .../hom/openjpa/MappingUtilsTest.java | 76 ------ .../openjpa/OneToManyRelationshipTest.java | 45 ---- .../StandaloneEntityManagerFactoryTest.java | 40 --- .../hom/service/EntitySchemaManagerTest.java | 67 ----- .../test/resources/META-INF/persistence.xml | 43 ---- .../spring-jpa-container-context.xml | 34 --- 21 files changed, 1494 deletions(-) delete mode 100644 object-mapper/src/main/java/me/prettyprint/hom/CassandraPersistenceProvider.java delete mode 100644 object-mapper/src/main/java/me/prettyprint/hom/EntityManagerFactoryImpl.java delete mode 100644 object-mapper/src/main/java/me/prettyprint/hom/openjpa/CassandraStore.java delete mode 100644 object-mapper/src/main/java/me/prettyprint/hom/openjpa/CassandraStoreConfiguration.java delete mode 100644 object-mapper/src/main/java/me/prettyprint/hom/openjpa/CassandraStoreManager.java delete mode 100644 object-mapper/src/main/java/me/prettyprint/hom/openjpa/EntityFacade.java delete mode 100644 object-mapper/src/main/java/me/prettyprint/hom/openjpa/MappingUtils.java delete mode 100644 object-mapper/src/main/java/me/prettyprint/hom/service/EntitySchemaManager.java delete mode 100644 object-mapper/src/main/java/me/prettyprint/hom/service/EntitySchemaStatus.java delete mode 100644 object-mapper/src/test/java/me/prettyprint/hom/EntityManagerFactoryTest.java delete mode 100644 object-mapper/src/test/java/me/prettyprint/hom/SpringContainerInitalizationTest.java delete mode 100644 object-mapper/src/test/java/me/prettyprint/hom/openjpa/EntityFacadeTest.java delete mode 100644 object-mapper/src/test/java/me/prettyprint/hom/openjpa/FullContainerTest.java delete mode 100644 object-mapper/src/test/java/me/prettyprint/hom/openjpa/ManagedEntityTestBase.java delete mode 100644 object-mapper/src/test/java/me/prettyprint/hom/openjpa/MappingUtilsTest.java delete mode 100644 object-mapper/src/test/java/me/prettyprint/hom/openjpa/OneToManyRelationshipTest.java delete mode 100644 object-mapper/src/test/java/me/prettyprint/hom/openjpa/StandaloneEntityManagerFactoryTest.java delete mode 100644 object-mapper/src/test/java/me/prettyprint/hom/service/EntitySchemaManagerTest.java delete mode 100644 object-mapper/src/test/resources/META-INF/persistence.xml delete mode 100644 object-mapper/src/test/resources/spring-jpa-container-context.xml diff --git a/object-mapper/src/main/java/me/prettyprint/hom/CassandraPersistenceProvider.java b/object-mapper/src/main/java/me/prettyprint/hom/CassandraPersistenceProvider.java deleted file mode 100644 index 4e8306d9e..000000000 --- a/object-mapper/src/main/java/me/prettyprint/hom/CassandraPersistenceProvider.java +++ /dev/null @@ -1,53 +0,0 @@ -package me.prettyprint.hom; - -import java.util.Map; - -import javax.persistence.EntityManagerFactory; -import javax.persistence.spi.PersistenceProvider; -import javax.persistence.spi.PersistenceUnitInfo; -import javax.persistence.spi.ProviderUtil; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -public class CassandraPersistenceProvider implements PersistenceProvider { - private static Logger log = LoggerFactory.getLogger(CassandraPersistenceProvider.class); - - private Map defProperties; - - public CassandraPersistenceProvider() { - - } - - public CassandraPersistenceProvider(Map map) { - this.defProperties = map; - } - - @Override - public EntityManagerFactory createContainerEntityManagerFactory( - PersistenceUnitInfo info, Map map) { - if ( log.isDebugEnabled() ) { - log.debug("creating EntityManagerFactory {} with properties {} ", "null", map); - } - // TODO Auto-generated method stub - return null; - } - - @Override - public EntityManagerFactory createEntityManagerFactory(String emName, Map map) { - if ( log.isDebugEnabled() ) { - log.debug("creating EntityManagerFactory {} with properties {} ", emName, map); - } - if ( map == null || map.isEmpty()) { - return new EntityManagerFactoryImpl(defProperties); - } - return new EntityManagerFactoryImpl(map); - } - - @Override - public ProviderUtil getProviderUtil() { - // TODO Auto-generated method stub - return null; - } - -} diff --git a/object-mapper/src/main/java/me/prettyprint/hom/EntityManagerFactoryImpl.java b/object-mapper/src/main/java/me/prettyprint/hom/EntityManagerFactoryImpl.java deleted file mode 100644 index 10d1674a0..000000000 --- a/object-mapper/src/main/java/me/prettyprint/hom/EntityManagerFactoryImpl.java +++ /dev/null @@ -1,127 +0,0 @@ -package me.prettyprint.hom; - -import java.util.Map; - -import javax.persistence.Cache; -import javax.persistence.EntityManager; -import javax.persistence.EntityManagerFactory; -import javax.persistence.PersistenceUnitUtil; -import javax.persistence.criteria.CriteriaBuilder; -import javax.persistence.metamodel.Metamodel; - -import org.apache.commons.lang.StringUtils; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import me.prettyprint.cassandra.service.CassandraHostConfigurator; -import me.prettyprint.hector.api.Cluster; -import me.prettyprint.hector.api.exceptions.HectorException; -import me.prettyprint.hector.api.factory.HFactory; - -public class EntityManagerFactoryImpl implements EntityManagerFactory { - - private Logger log = LoggerFactory.getLogger(EntityManagerFactoryImpl.class); - - private EntityManagerConfigurator entityManagerConfigurator; - private Cluster cluster; - - public EntityManagerFactoryImpl() { - - } - - public EntityManagerFactoryImpl(Map properties) { - this(new EntityManagerConfigurator(properties)); - - // cassandraHostConfigurator properties - // Steps: - // 1. initialize Hector - // 2. aquire keyspace - // 3. build the cache mgr (internal to EntityManager) - // 4. build the objectMapper (internal to EntityManger) - } - - public EntityManagerFactoryImpl(EntityManagerConfigurator entityManagerConfigurator) { - this.entityManagerConfigurator = entityManagerConfigurator; - this.cluster = HFactory.getOrCreateCluster(entityManagerConfigurator.getClusterName(), - entityManagerConfigurator.getCassandraHostConfigurator()); - - } - - @Override - public void close() { - cluster.getConnectionManager().shutdown(); - } - - @Override - public EntityManager createEntityManager() { - log.debug("building EMF"); - return buildEntityManager(entityManagerConfigurator.getKeyspace()); - } - - private EntityManagerImpl buildEntityManager(String keyspace) { - EntityManagerImpl entityManager = new EntityManagerImpl(HFactory. - createKeyspace(keyspace, cluster), - entityManagerConfigurator.getClasspathPrefix()); - log.debug("Built entityManager {}", entityManager); - return entityManager; - } - - /** - * Looks for the {@link EntityManagerConfigurator#KEYSPACE_PROP} using the - * keyspace already present on the internal EntityManagerConfigurator if already found - * @param map - * @return - */ - @SuppressWarnings("unchecked") - @Override - public EntityManager createEntityManager(Map map) { - log.debug("building EMF with props"); - String keyspaceStr = EntityManagerConfigurator.getPropertyGently(map, EntityManagerConfigurator.KEYSPACE_PROP, false); - if ( StringUtils.isBlank(keyspaceStr) ) { - keyspaceStr = entityManagerConfigurator.getKeyspace(); - } - return buildEntityManager(keyspaceStr); - } - - @Override - public boolean isOpen() { - try { - cluster.describeClusterName(); - return true; - } catch (HectorException he) { - log.debug("isOpen failed to connecto to cluster: {}",he.getMessage()); - } - return false; - } - - @Override - public Cache getCache() { - // TODO Auto-generated method stub - return null; - } - - @Override - public CriteriaBuilder getCriteriaBuilder() { - // TODO Auto-generated method stub - return null; - } - - @Override - public Metamodel getMetamodel() { - // TODO Auto-generated method stub - return null; - } - - @Override - public PersistenceUnitUtil getPersistenceUnitUtil() { - // TODO Auto-generated method stub - return null; - } - - @Override - public Map getProperties() { - // TODO Auto-generated method stub - return null; - } - -} diff --git a/object-mapper/src/main/java/me/prettyprint/hom/openjpa/CassandraStore.java b/object-mapper/src/main/java/me/prettyprint/hom/openjpa/CassandraStore.java deleted file mode 100644 index 28c8fbe65..000000000 --- a/object-mapper/src/main/java/me/prettyprint/hom/openjpa/CassandraStore.java +++ /dev/null @@ -1,131 +0,0 @@ -package me.prettyprint.hom.openjpa; - -import java.util.ArrayList; -import java.util.Collection; -import java.util.Iterator; -import java.util.Map; - -import org.apache.openjpa.kernel.OpenJPAStateManager; -import org.apache.openjpa.meta.ClassMetaData; -import org.apache.openjpa.meta.FieldMetaData; -import org.apache.openjpa.meta.JavaTypes; -import org.apache.openjpa.util.OpenJPAId; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.springframework.jdbc.core.ColumnMapRowMapper; - -import me.prettyprint.cassandra.model.HColumnImpl; -import me.prettyprint.cassandra.model.MutatorImpl; -import me.prettyprint.cassandra.serializers.BytesArraySerializer; -import me.prettyprint.cassandra.serializers.LongSerializer; -import me.prettyprint.cassandra.serializers.StringSerializer; -import me.prettyprint.cassandra.utils.StringUtils; -import me.prettyprint.hector.api.Cluster; -import me.prettyprint.hector.api.Keyspace; -import me.prettyprint.hector.api.Serializer; -import me.prettyprint.hector.api.beans.ColumnSlice; -import me.prettyprint.hector.api.beans.HColumn; -import me.prettyprint.hector.api.factory.HFactory; -import me.prettyprint.hector.api.mutation.Mutator; -import me.prettyprint.hector.api.query.QueryResult; -import me.prettyprint.hector.api.query.SliceQuery; -import me.prettyprint.hom.EntityManagerConfigurator; -import me.prettyprint.hom.openjpa.EntityFacade.ColumnMeta; - - -/** - * Holds the {@link Cluster} and {@link Keyspace} references needed - * for accessing Cassandra - * - * @author zznate - */ -public class CassandraStore { - - private static final Logger log = LoggerFactory.getLogger(CassandraStore.class); - - private final Cluster cluster; - private final CassandraStoreConfiguration conf; - private Keyspace keyspace; - private MappingUtils mappingUtils; - - public CassandraStore(CassandraStoreConfiguration conf) { - this.conf = conf; - this.cluster = HFactory.getCluster(conf.getValue(EntityManagerConfigurator.CLUSTER_NAME_PROP) - .getOriginalValue()); - // TODO needs passthrough of other configuration - mappingUtils = new MappingUtils(); - } - - public CassandraStore open() { - this.keyspace = HFactory.createKeyspace(conf.getValue(EntityManagerConfigurator.KEYSPACE_PROP) - .getOriginalValue(), cluster); - return this; - } - - public boolean getObject(OpenJPAStateManager stateManager) { - - ClassMetaData metaData = stateManager.getMetaData(); - EntityFacade entityFacade = new EntityFacade(metaData); - Object idObj = stateManager.getId(); - - SliceQuery sliceQuery = mappingUtils.buildSliceQuery(idObj, entityFacade, keyspace); - - //stateManager.storeObject(0, idObj); - - QueryResult> result = sliceQuery.execute(); - - for (Map.Entry> entry : entityFacade.getColumnMeta().entrySet()) { - HColumn column = result.get().getColumnByName(entry.getKey()); - if ( column != null ) - stateManager.storeObject(entry.getValue().fieldId, entry.getValue().serializer.fromBytes(column.getValue())); - } - return true; - } - - public Mutator storeObject(Mutator mutator, OpenJPAStateManager stateManager, Object idObj) { - if ( mutator == null ) - mutator = new MutatorImpl(keyspace, BytesArraySerializer.get()); - if ( log.isDebugEnabled() ) { - log.debug("Adding mutation (insertion) for class {}", stateManager.getManagedInstance().getClass().getName()); - } - ClassMetaData metaData = stateManager.getMetaData(); - EntityFacade entityFacade = new EntityFacade(metaData); - Object field; - for (Map.Entry> entry : entityFacade.getColumnMeta().entrySet()) { - field = stateManager.fetch(entry.getValue().fieldId); - if ( field != null ) { - mutator.addInsertion(mappingUtils.getKeyBytes(idObj), entityFacade.getColumnFamilyName(), - new HColumnImpl(entry.getKey(), field, - keyspace.createClock(), StringSerializer.get(), - entry.getValue().serializer)); - } - - } - return mutator; - } - - public Mutator removeObject(Mutator mutator, OpenJPAStateManager stateManager, Object idObj) { - if ( mutator == null ) - mutator = new MutatorImpl(keyspace, BytesArraySerializer.get()); - if ( log.isDebugEnabled() ) { - log.debug("Adding mutation (deletion) for class {}", stateManager.getManagedInstance().getClass().getName()); - } - ClassMetaData metaData = stateManager.getMetaData(); - EntityFacade entityFacade = new EntityFacade(metaData); - mutator.addDeletion(mappingUtils.getKeyBytes(idObj), entityFacade.getColumnFamilyName(),null,StringSerializer.get()); - return mutator; - } - - - public Cluster getCluster() { - return cluster; - } - - public Keyspace getKeyspace() { - return keyspace; - } - - - - -} diff --git a/object-mapper/src/main/java/me/prettyprint/hom/openjpa/CassandraStoreConfiguration.java b/object-mapper/src/main/java/me/prettyprint/hom/openjpa/CassandraStoreConfiguration.java deleted file mode 100644 index b062fb2cf..000000000 --- a/object-mapper/src/main/java/me/prettyprint/hom/openjpa/CassandraStoreConfiguration.java +++ /dev/null @@ -1,24 +0,0 @@ -package me.prettyprint.hom.openjpa; - -import org.apache.openjpa.conf.OpenJPAConfigurationImpl; -import org.apache.openjpa.lib.conf.ProductDerivations; - -public class CassandraStoreConfiguration extends OpenJPAConfigurationImpl { - - public CassandraStoreConfiguration() { - super(false, false); - - // override the default and the current value of lock manager plugin - // from our superclass to use the single-jvm lock manager - lockManagerPlugin.setDefault("version"); - lockManagerPlugin.setString("version"); - addString("me.prettyprint.hom.classpathPrefix"); - addString("me.prettyprint.hom.keyspace"); - addString("me.prettyprint.hom.clusterName"); - addString("me.prettyprint.hom.hostList"); - - ProductDerivations.beforeConfigurationLoad(this); - loadGlobals(); - - } -} diff --git a/object-mapper/src/main/java/me/prettyprint/hom/openjpa/CassandraStoreManager.java b/object-mapper/src/main/java/me/prettyprint/hom/openjpa/CassandraStoreManager.java deleted file mode 100644 index 732b0d11a..000000000 --- a/object-mapper/src/main/java/me/prettyprint/hom/openjpa/CassandraStoreManager.java +++ /dev/null @@ -1,237 +0,0 @@ -package me.prettyprint.hom.openjpa; - -import java.util.ArrayList; -import java.util.BitSet; -import java.util.Collection; -import java.util.Iterator; -import java.util.List; - -import me.prettyprint.cassandra.serializers.LongSerializer; -import me.prettyprint.cassandra.serializers.StringSerializer; -import me.prettyprint.cassandra.service.CassandraHostConfigurator; -import me.prettyprint.hector.api.Cluster; -import me.prettyprint.hector.api.Keyspace; -import me.prettyprint.hector.api.factory.HFactory; -import me.prettyprint.hector.api.mutation.Mutator; - -import org.apache.openjpa.abstractstore.AbstractStoreManager; -import org.apache.openjpa.conf.OpenJPAConfiguration; -import org.apache.openjpa.kernel.FetchConfiguration; -import org.apache.openjpa.kernel.OpenJPAStateManager; -import org.apache.openjpa.kernel.PCState; -import org.apache.openjpa.lib.rop.ListResultObjectProvider; -import org.apache.openjpa.lib.rop.ResultObjectProvider; -import org.apache.openjpa.meta.ClassMetaData; -import org.apache.openjpa.meta.FieldMetaData; -import org.apache.openjpa.util.OpenJPAId; -import org.apache.openjpa.util.StoreException; -import org.apache.openjpa.xmlstore.ObjectData; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -public class CassandraStoreManager extends AbstractStoreManager { - - private static Logger log = LoggerFactory.getLogger(CassandraStoreManager.class); - - private CassandraStore cassandraStore; - - @Override - public ResultObjectProvider executeExtent(ClassMetaData cMetaData, boolean useSubClasses, - FetchConfiguration fetchConfiguration) { - // cMetaData is essentially the query construct - // the ResultObjectProvider runs the query - if ( log.isDebugEnabled() ) { - log.debug("in executeExtent with ClassMetaData {}: useSubClasses: {} and fetchConfiguration: {}", - new Object[]{cMetaData, useSubClasses, fetchConfiguration}); - } - - // ask the store for all ObjectDatas for the given type; this - // actually gives us all instances of the base class of the type - // CFMetaData - // ObjectData[] datas = _store.getData(meta); - // get the Class - // Class candidate = meta.getDescribedType(); - - // create a list of the corresponding persistent objects that - // match the type and subclasses criteria - /* - List pcs = new ArrayList(datas.length); - for (int i = 0; i < datas.length; i++) { - // does this instance belong in the extent? - Class c = datas[i].getMetaData().getDescribedType(); - if (c != candidate && (!subclasses - || !candidate.isAssignableFrom(c))) - continue; - - // look up the pc instance for the data, passing in the data - // as well so that we can take advantage of the fact that we've - // already looked it up. note that in the store manager's - // initialize(), load(), etc methods we check for this data - // being passed through and save ourselves a trip to the store - // if it is present; this is particularly important in systems - // where a trip to the store can be expensive. - pcs.add(ctx.find(datas[i].getId(), fetch, null, datas[i], 0)); - } - return new ListResultObjectProvider(pcs); - */ - return null; - } - - @SuppressWarnings("unchecked") - @Override - protected Collection flush(Collection pNew, Collection pNewUpdated, Collection pNewFlushedDeleted, - Collection pDirty, Collection pDeleted) { - /* defn. of above arguments - * --------------------------- - * pNew - Objects that should be added to the store, and that have not previously been flushed. - pNewUpdated - New objects that have been modified since they were initially flushed. These were in persistentNew in an earlier flush invocation. - pNewFlushedDeleted - New objects that have been deleted since they were initially flushed. These were in persistentNew in an earlier flush invocation. - pDirty - Objects that were loaded from the data store and have since been modified. - pDeleted - Objects that were loaded from the data store and have since been deleted. These may have been in a previous flush invocation's persistentDirty list. - */ - //_updates = new ArrayList(pNew.size() + pDirty.size()); - //_deletes = new ArrayList(pDeleted.size()); - cassandraStore.open(); - OpenJPAConfiguration conf = ctx.getConfiguration(); - Mutator mutator = null; - for (Iterator itr = pNew.iterator(); itr.hasNext();) { - // create new object data for instance - OpenJPAStateManager sm = (OpenJPAStateManager) itr.next(); - Object oid = sm.getObjectId(); - mutator = cassandraStore.storeObject(mutator, sm, oid); - } - // TODO combine pNewUpdated and pDirty and use sm.getDirty for bitmask of field - // - //for (int i = 0; i < fmds.length; i++) - //if (fields.get(i)) - // sm.store(i, toLoadable(sm, fmds[i], _data[i], fetch)); - // - // combine pNewFlushDelete and pDeleted into the mutator - if ( !pDeleted.isEmpty() || !pNewFlushedDeleted.isEmpty() ) { - List deletes = new ArrayList(pDeleted.size() + pNewFlushedDeleted.size()); - deletes.addAll(pDeleted); - deletes.addAll(pNewFlushedDeleted); - for (Iterator itr = deletes.iterator(); itr.hasNext();) { - // create new object data for instance - OpenJPAStateManager sm = (OpenJPAStateManager) itr.next(); - Object oid = sm.getObjectId(); - mutator = cassandraStore.removeObject(mutator, sm, oid); - } - } - mutator.execute(); - - - return null; - } - - @Override - public boolean initialize(OpenJPAStateManager stateManager, PCState pcState, - FetchConfiguration fetchConfiguration, Object obj) { - log.debug("In initialize operation..."); - cassandraStore.open(); - stateManager.initialize(stateManager.getMetaData().getDescribedType(), pcState); - cassandraStore.getObject(stateManager); - // TODO need to add the not-found case - return true; - } - - @Override - public boolean load(OpenJPAStateManager stateManager, BitSet arg1, - FetchConfiguration arg2, int arg3, Object arg4) { - log.debug("In load operation..."); - cassandraStore.getObject(stateManager); - // TODO implement the loading of properties marked as lazy (is this correct?) - // this is a misnomer. load is called to fill in additional information not retrieved from - // initialize call above - return false; - } - - @Override - public boolean exists(OpenJPAStateManager arg0, Object arg1) { - // TODO - // hit cassandra to see if the object exists - log.debug("In CSM.exists()"); - return false; - } - - - @Override - public boolean isCached(List arg0, BitSet arg1) { - log.debug("In CSM.isCached()"); - // TODO Auto-generated method stub - return false; - } - - - - @Override - public int compareVersion(OpenJPAStateManager state, Object v1, Object v2) { - log.debug("in CSM.compareVersion"); - return super.compareVersion(state, v1, v2); - } - - @Override - public boolean syncVersion(OpenJPAStateManager sm, Object edata) { - // TODO Auto-generated method stub - log.debug("in CSM.syncVersion"); - return super.syncVersion(sm, edata); - } - - @Override - protected void open() { - OpenJPAConfiguration conf = ctx.getConfiguration(); - // TODO - // encapsulate into CassandraStore or similar (should take CassandraStoreConfig as an argg - //cluster = HFactory.getCluster(conf.getValue("me.prettyprint.hom.clusterName").getOriginalValue()); - //keyspace = HFactory.createKeyspace(conf.getValue("me.prettyprint.hom.keyspace").getOriginalValue(), cluster); - - cassandraStore = new CassandraStore((CassandraStoreConfiguration)conf); - log.debug("in CSM.open()"); - } - - protected Collection getUnsupportedOptions() { - Collection c = super.getUnsupportedOptions(); - - // remove options we do support but the abstract store doesn't - //c.remove(OpenJPAConfiguration.OPTION_ID_DATASTORE); - //c.remove(OpenJPAConfiguration.OPTION_OPTIMISTIC); - - // and add some that we don't support but the abstract store does - // TODO take these out one by one - c.add(OpenJPAConfiguration.OPTION_EMBEDDED_RELATION); - c.add(OpenJPAConfiguration.OPTION_EMBEDDED_COLLECTION_RELATION); - c.add(OpenJPAConfiguration.OPTION_EMBEDDED_MAP_RELATION); - //c.add(OpenJPAConfiguration.OPTION_OPTIMISTIC); - return c; - } - - @Override - protected OpenJPAConfiguration newConfiguration() { - - CassandraStoreConfiguration conf = new CassandraStoreConfiguration(); - - if ( log.isDebugEnabled() ) { - log.debug("In newConfiguration with conf: {}", conf.toProperties(true)); - } - - return conf; - } - - - -} - -/* - * NOTES - - OpenJPAStateManager holds the 'state' of an Entity instance - - ClassMetaData holds the details of the persistable Class - - Consider using ClassCacheMgr as a MetaDataFactory - http://openjpa.apache.org/builds/2.0.1/apache-openjpa-2.0.1/docs/manual/ref_guide_meta.html - - Consider rolling a custom FetchPlan (or FetchConfiguration?) for CL - - Returns the OpenJPAId.getType() returns Entity class: - log.debug("OID class name: {}",((OpenJPAId)idObj).getType().getName()); - - */ diff --git a/object-mapper/src/main/java/me/prettyprint/hom/openjpa/EntityFacade.java b/object-mapper/src/main/java/me/prettyprint/hom/openjpa/EntityFacade.java deleted file mode 100644 index dea9cecfe..000000000 --- a/object-mapper/src/main/java/me/prettyprint/hom/openjpa/EntityFacade.java +++ /dev/null @@ -1,101 +0,0 @@ -package me.prettyprint.hom.openjpa; - -import java.io.Serializable; -import java.util.Arrays; -import java.util.HashMap; -import java.util.Map; - -import javax.persistence.Table; - -import org.apache.openjpa.meta.ClassMetaData; -import org.apache.openjpa.meta.FieldMetaData; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import me.prettyprint.hector.api.Serializer; - -public class EntityFacade implements Serializable { - private static final Logger log = LoggerFactory.getLogger(EntityFacade.class); - - private static final long serialVersionUID = 4777260639119126462L; - - private final String columnFamilyName; - private final Class clazz; - private final Serializer keySerializer; - private final Map> columnMetas; - - public EntityFacade(ClassMetaData classMetaData) { - clazz = classMetaData.getDescribedType(); - this.columnFamilyName = clazz.getAnnotation(Table.class) != null ? clazz.getAnnotation(Table.class).name() : clazz.getSimpleName(); - if ( log.isDebugEnabled() ) { - log.debug("PK field name: {} and typeCode: {}", - classMetaData.getPrimaryKeyFields()[0].getType(), - classMetaData.getPrimaryKeyFields()[0].getObjectIdFieldTypeCode()); - } - this.keySerializer = MappingUtils.getSerializer(classMetaData.getPrimaryKeyFields()[0]); - columnMetas = new HashMap>(); - FieldMetaData[] fmds = classMetaData.getFields(); - for (int i = 0; i < fmds.length; i++) { - if ( fmds[i].getManagement() == FieldMetaData.MANAGE_NONE || fmds[i].isPrimaryKey()) - continue; - if ( log.isDebugEnabled()) { - log.debug("field name {} typeCode {} associationType: {} declaredType: {} embeddedMetaData: {}", - new Object[]{fmds[i].getName(), - fmds[i].getTypeCode(), - fmds[i].getAssociationType(), - fmds[i].getDeclaredType().getName(), - fmds[i].getElement().getDeclaredTypeMetaData() - }); - } - // TODO if fmds[i].getAssociationType() > 0 .. we found an attached entity and need to find it's entityFacade - columnMetas.put(fmds[i].getName(), new ColumnMeta(fmds[i].getIndex(), MappingUtils.getSerializer(fmds[i].getTypeCode()))); - } - } - - public String[] getColumnNames() { - return columnMetas.keySet().toArray(new String[]{}); - } - - public String getColumnFamilyName() { - return columnFamilyName; - } - - public Class getClazz() { - return clazz; - } - - public Serializer getKeySerializer() { - return keySerializer; - } - - public int getFieldId(String columnName) { - return columnMetas.get(columnName).fieldId; - } - - public Serializer getSerializer(String columnName) { - return columnMetas.get(columnName).serializer; - } - - public Map> getColumnMeta() { - return columnMetas; - } - - class ColumnMeta { - int fieldId; - Serializer serializer; - - ColumnMeta(int fieldId, Serializer serializer) { - this.fieldId = fieldId; - this.serializer = serializer; - } - } - - @Override - public String toString() { - return String.format("EntityFacade[class: %s, columnFamily: %s, columnNames: %s]", - clazz.getName(), - columnFamilyName, Arrays.toString(getColumnNames())); - } - - -} diff --git a/object-mapper/src/main/java/me/prettyprint/hom/openjpa/MappingUtils.java b/object-mapper/src/main/java/me/prettyprint/hom/openjpa/MappingUtils.java deleted file mode 100644 index e5038e999..000000000 --- a/object-mapper/src/main/java/me/prettyprint/hom/openjpa/MappingUtils.java +++ /dev/null @@ -1,194 +0,0 @@ -package me.prettyprint.hom.openjpa; - -import java.nio.ByteBuffer; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import java.util.UUID; - -import me.prettyprint.cassandra.model.MutatorImpl; -import me.prettyprint.cassandra.model.thrift.ThriftSliceQuery; -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.ObjectSerializer; -import me.prettyprint.cassandra.serializers.SerializerTypeInferer; -import me.prettyprint.cassandra.serializers.StringSerializer; -import me.prettyprint.cassandra.serializers.UUIDSerializer; -import me.prettyprint.hector.api.Keyspace; -import me.prettyprint.hector.api.Serializer; -import me.prettyprint.hector.api.factory.HFactory; -import me.prettyprint.hector.api.mutation.Mutator; -import me.prettyprint.hector.api.query.SliceQuery; - -import org.apache.openjpa.kernel.OpenJPAStateManager; -import org.apache.openjpa.meta.ClassMetaData; -import org.apache.openjpa.meta.FieldMetaData; -import org.apache.openjpa.meta.JavaTypes; -import org.apache.openjpa.util.LongId; -import org.apache.openjpa.util.OpenJPAId; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -/** - * Utility class for bridging Hector/OpenJPA functionality - * - * @author zznate - * - */ -public class MappingUtils { - private static final Logger log = LoggerFactory.getLogger(MappingUtils.class); - - private static final Map> typeSerializerMap = new HashMap>(); - private static final Map, Serializer> classSerializerMap = new HashMap, Serializer>(); - - // TODO need to figure out UUID - static { - typeSerializerMap.put(JavaTypes.STRING, StringSerializer.get()); - typeSerializerMap.put(JavaTypes.INT, IntegerSerializer.get()); - typeSerializerMap.put(JavaTypes.INT_OBJ, IntegerSerializer.get()); - typeSerializerMap.put(JavaTypes.DATE, DateSerializer.get()); - typeSerializerMap.put(JavaTypes.LONG, LongSerializer.get()); - typeSerializerMap.put(JavaTypes.LONG_OBJ, LongSerializer.get()); - typeSerializerMap.put(JavaTypes.DOUBLE, DoubleSerializer.get()); - typeSerializerMap.put(JavaTypes.DOUBLE_OBJ, DoubleSerializer.get()); - - classSerializerMap.put(UUID.class, UUIDSerializer.get()); - classSerializerMap.put(byte[].class, BytesArraySerializer.get()); - classSerializerMap.put(ByteBuffer.class, ByteBufferSerializer.get()); - - } - - public static Serializer getSerializer(int javaType) { - return typeSerializerMap.get(javaType) != null ? typeSerializerMap.get(javaType) : ObjectSerializer.get(); - } - - public static Serializer getSerializer(FieldMetaData fieldMetaData) { - Serializer serializer = getSerializer(fieldMetaData.getTypeCode()); - if ( serializer instanceof ObjectSerializer ) { - Class clazz = fieldMetaData.getType(); - if ( classSerializerMap.get(clazz) != null ) { - serializer = classSerializerMap.get(clazz); - } - } - return serializer; - } - - public SliceQuery buildSliceQuery(Object idObj, EntityFacade entityFacade, Keyspace keyspace) { - SliceQuery query = new ThriftSliceQuery(keyspace, BytesArraySerializer.get(), StringSerializer.get(), BytesArraySerializer.get()); - query.setColumnNames(entityFacade.getColumnNames()); - query.setKey(getKeyBytes(idObj)); - query.setColumnFamily(entityFacade.getColumnFamilyName()); - return query; - } - - /** - * Get the byte[] representing this Id object - * @param idObj - * @return - */ - public byte[] getKeyBytes(Object idObj) { - Serializer serializer = getSerializer(idObj); - if ( idObj instanceof OpenJPAId ) - return serializer.toBytes(((OpenJPAId)idObj).getIdObject()); - return serializer.toBytes(idObj); - } - - /** - * Retrieve the {@link Serializer} implementation for the id Object. - * @see {@link SerializerTypeInferer} for specifics. - * @param idObj - */ - public Serializer getSerializer(Object idObj) { - Serializer serializer; - if ( idObj instanceof OpenJPAId ) { - serializer = SerializerTypeInferer.getSerializer(((OpenJPAId)idObj).getIdObject()); - } else { - serializer = SerializerTypeInferer.getSerializer(idObj); - } - return serializer; - } - - /** - * Return a list of field names for which there are a 1-1 mapping - * to Column names - * - * @param metaData - * @return - */ - List buildColumnList(ClassMetaData metaData) { - FieldMetaData[] fmds = metaData.getFields(); - List cols = new ArrayList(fmds.length); - for (int i = 0; i < fmds.length; i++) { - if (fmds[i].getManagement() != fmds[i].MANAGE_PERSISTENT || fmds[i].isPrimaryKey()) - continue; - - log.debug("fmd.name: {}",fmds[i].getName()); - cols.add(fmds[i].getName()); - } - return cols; - } - - /** - * Map the column names to their respective serializers - * TODO cache this - * @param metaData - * @return - */ - Map buildColumnSerializerMap(ClassMetaData metaData) { - Map serMap = new HashMap(); - - FieldMetaData[] fmds = metaData.getFields(); - for (int i = 0; i < fmds.length; i++) { - if (fmds[i].getManagement() != fmds[i].MANAGE_PERSISTENT || fmds[i].isPrimaryKey()) - continue; - switch (fmds[i].getTypeCode()) { - case JavaTypes.STRING: - serMap.put(fmds[i].getName(), StringSerializer.get()); - break; - case JavaTypes.INT: - serMap.put(fmds[i].getName(), IntegerSerializer.get()); - break; - case JavaTypes.INT_OBJ: - serMap.put(fmds[i].getName(), IntegerSerializer.get()); - break; - case JavaTypes.DATE: - serMap.put(fmds[i].getName(), DateSerializer.get()); - break; - case JavaTypes.LONG: - serMap.put(fmds[i].getName(), LongSerializer.get()); - break; - case JavaTypes.LONG_OBJ: - serMap.put(fmds[i].getName(), LongSerializer.get()); - break; - case JavaTypes.DOUBLE: - serMap.put(fmds[i].getName(), DoubleSerializer.get()); - break; - case JavaTypes.DOUBLE_OBJ: - serMap.put(fmds[i].getName(), DoubleSerializer.get()); - break; - default: - serMap.put(fmds[i].getName(), ObjectSerializer.get()); - break; - } - } - return serMap; - } - - - - public Mutator addMutation(Mutator mutator, Object idObj, OpenJPAStateManager stateManager, Keyspace keyspace) { - ClassMetaData metaData = stateManager.getMetaData(); - Map serMap = buildColumnSerializerMap(metaData); - for (Map.Entry entry : serMap.entrySet()) { - mutator.addInsertion(getKeyBytes(idObj), "TestBeanColumnFamily", - HFactory.createColumn(entry.getKey(), stateManager.fetch(metaData.getField(entry.getKey()).getIndex()), - StringSerializer.get(), entry.getValue())); - } - return mutator; - } -} diff --git a/object-mapper/src/main/java/me/prettyprint/hom/service/EntitySchemaManager.java b/object-mapper/src/main/java/me/prettyprint/hom/service/EntitySchemaManager.java deleted file mode 100644 index 36566befc..000000000 --- a/object-mapper/src/main/java/me/prettyprint/hom/service/EntitySchemaManager.java +++ /dev/null @@ -1,63 +0,0 @@ -package me.prettyprint.hom.service; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import me.prettyprint.cassandra.model.BasicColumnFamilyDefinition; -import me.prettyprint.cassandra.service.ThriftCfDef; -import me.prettyprint.hector.api.Cluster; -import me.prettyprint.hector.api.Keyspace; -import me.prettyprint.hector.api.ddl.ColumnFamilyDefinition; -import me.prettyprint.hector.api.exceptions.HInvalidRequestException; -import me.prettyprint.hom.openjpa.EntityFacade; -import me.prettyprint.hom.service.EntitySchemaStatus.SchemaResult; - -/** - * A service to manage the schema of JPA Entities - * - * @author zznate - */ -public class EntitySchemaManager { - - private static Logger log = LoggerFactory.getLogger(EntitySchemaManager.class); - - private Cluster cluster; - private Keyspace keyspace; - - public EntitySchemaManager(Cluster cluster, Keyspace keyspace) { - this.cluster = cluster; - this.keyspace = keyspace; - } - - /** - * Creates the schema if it does not already exist. If it does exist, - * we catch the HInvalidRequestException and return an {@link EntitySchemaStatus} - * with {@link SchemaResult#NOT_MODIFIED} - * @param entityFacade - * @return {@link EntitySchemaStatus} with {@link SchemaResult#CREATED} if successful - */ - public EntitySchemaStatus createSchema(EntityFacade entityFacade) { - if ( log.isDebugEnabled() ) { - log.debug("EntitySchemaManager creating schema for: {}", entityFacade); - } - BasicColumnFamilyDefinition columnFamilyDefinition = new BasicColumnFamilyDefinition(); - columnFamilyDefinition.setKeyspaceName(keyspace.getKeyspaceName()); - columnFamilyDefinition.setName(entityFacade.getColumnFamilyName()); - // TODO determine how to pull in other CF creation settings... custom annotations? - ColumnFamilyDefinition cfDef = new ThriftCfDef(columnFamilyDefinition); - EntitySchemaStatus entitySchemaStatus; - try { - cluster.addColumnFamily(cfDef); - entitySchemaStatus = new EntitySchemaStatus(SchemaResult.CREATED); - } catch (HInvalidRequestException ire) { - if ( log.isDebugEnabled() ) { - log.debug("Caught HInvalidRequestException on createSchema. Details {}", ire.getMessage()); - } - entitySchemaStatus = new EntitySchemaStatus(SchemaResult.NOT_MODIFIED); - } - return entitySchemaStatus; - - } - - -} diff --git a/object-mapper/src/main/java/me/prettyprint/hom/service/EntitySchemaStatus.java b/object-mapper/src/main/java/me/prettyprint/hom/service/EntitySchemaStatus.java deleted file mode 100644 index 6b4a958a0..000000000 --- a/object-mapper/src/main/java/me/prettyprint/hom/service/EntitySchemaStatus.java +++ /dev/null @@ -1,21 +0,0 @@ -package me.prettyprint.hom.service; - -public class EntitySchemaStatus { - - private final SchemaResult schemaResult; - - public EntitySchemaStatus(SchemaResult result) { - this.schemaResult = result; - } - - - public SchemaResult getSchemaResult() { - return schemaResult; - } - - public enum SchemaResult { - CREATED, - UPDATED, - NOT_MODIFIED - } -} diff --git a/object-mapper/src/test/java/me/prettyprint/hom/EntityManagerFactoryTest.java b/object-mapper/src/test/java/me/prettyprint/hom/EntityManagerFactoryTest.java deleted file mode 100644 index f20026ece..000000000 --- a/object-mapper/src/test/java/me/prettyprint/hom/EntityManagerFactoryTest.java +++ /dev/null @@ -1,47 +0,0 @@ -package me.prettyprint.hom; - -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertTrue; - -import java.util.HashMap; -import java.util.Map; - -import javax.persistence.EntityManager; -import javax.persistence.EntityManagerFactory; - -import org.junit.Before; -import org.junit.Ignore; -import org.junit.Test; - -@Ignore -public class EntityManagerFactoryTest extends CassandraTestBase { - - private EntityManagerFactory entityManagerFactory; - - - @Test - public void testCreateEntityManager() { - EntityManager entityManager = entityManagerFactory.createEntityManager(); - assertNotNull(entityManager); - assertTrue(entityManagerFactory.isOpen()); - } - - @Test - public void testCloseEntityManagerFactory() { - assertTrue(entityManagerFactory.isOpen()); - entityManagerFactory.close(); - assertFalse(entityManagerFactory.isOpen()); - - } - - @Before - public void initFactory() { - Map properties = new HashMap(); - properties.put(EntityManagerConfigurator.CLASSPATH_PREFIX_PROP, "me.prettyprint.hom.beans"); - properties.put(EntityManagerConfigurator.CLUSTER_NAME_PROP, "TestPool"); - properties.put(EntityManagerConfigurator.KEYSPACE_PROP, "TestKeyspace"); - EntityManagerConfigurator emc = new EntityManagerConfigurator(properties); - entityManagerFactory = new EntityManagerFactoryImpl(properties); - } -} diff --git a/object-mapper/src/test/java/me/prettyprint/hom/KeyConcatenationDelimiterStrategyTest.java b/object-mapper/src/test/java/me/prettyprint/hom/KeyConcatenationDelimiterStrategyTest.java index 8af7820ce..179ef782c 100644 --- a/object-mapper/src/test/java/me/prettyprint/hom/KeyConcatenationDelimiterStrategyTest.java +++ b/object-mapper/src/test/java/me/prettyprint/hom/KeyConcatenationDelimiterStrategyTest.java @@ -6,7 +6,6 @@ import java.util.LinkedList; import java.util.List; -import org.apache.openjpa.persistence.kernel.common.apps.ByteArray; import org.junit.Test; public class KeyConcatenationDelimiterStrategyTest { diff --git a/object-mapper/src/test/java/me/prettyprint/hom/SpringContainerInitalizationTest.java b/object-mapper/src/test/java/me/prettyprint/hom/SpringContainerInitalizationTest.java deleted file mode 100644 index 97faaee6d..000000000 --- a/object-mapper/src/test/java/me/prettyprint/hom/SpringContainerInitalizationTest.java +++ /dev/null @@ -1,60 +0,0 @@ -package me.prettyprint.hom; - -import static org.junit.Assert.*; - -import java.util.Properties; - -import javax.persistence.EntityManager; -import javax.persistence.EntityManagerFactory; -import javax.persistence.Persistence; -import javax.persistence.PersistenceUnit; - -import me.prettyprint.hom.beans.MyTestBean; -import me.prettyprint.hom.beans.SimpleTestBean; - -import org.junit.Ignore; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.springframework.test.context.ContextConfiguration; -import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; - -@RunWith(SpringJUnit4ClassRunner.class) -@ContextConfiguration(locations="/spring-jpa-container-context.xml") -public class SpringContainerInitalizationTest extends CassandraTestBase { - - @PersistenceUnit - private EntityManagerFactory entityManagerFactory; - - @Test - @Ignore - public void testLookupEntityManagerFactory() { - - // does not use open-jpa - entityManagerFactory = Persistence.createEntityManagerFactory("myprovider", buildProps()); - //entityManagerFactory = Persistence.createEntityManagerFactory("myprovider"); - - EntityManager entityManager = entityManagerFactory.createEntityManager(); - - assertTrue(entityManagerFactory.isOpen()); - } - - @Test - public void testStandaloneOpenJpaProviderConfig() { - //PersistenceProviderImpl ppi = new PersistenceProviderImpl(); - entityManagerFactory = Persistence.createEntityManagerFactory("openjpa"); - EntityManager entityManager = entityManagerFactory.createEntityManager(); - //assertTrue(entityManager.contains(new SimpleTestBean())); - - //EntityManager entityManager = entityManagerFactory.createEntityManager(); - - } - - private Properties buildProps() { - Properties props = new Properties(); - props.put(EntityManagerConfigurator.CLUSTER_NAME_PROP, "TestPool"); - props.put(EntityManagerConfigurator.CLASSPATH_PREFIX_PROP, "me.prettyprint.hom.beans"); - props.put(EntityManagerConfigurator.HOST_LIST_PROP, "localhost:9161"); - props.put(EntityManagerConfigurator.KEYSPACE_PROP, "TestKeyspace"); - return props; - } -} diff --git a/object-mapper/src/test/java/me/prettyprint/hom/openjpa/EntityFacadeTest.java b/object-mapper/src/test/java/me/prettyprint/hom/openjpa/EntityFacadeTest.java deleted file mode 100644 index 4f61030b7..000000000 --- a/object-mapper/src/test/java/me/prettyprint/hom/openjpa/EntityFacadeTest.java +++ /dev/null @@ -1,49 +0,0 @@ -package me.prettyprint.hom.openjpa; - -import static org.junit.Assert.*; - -import javax.persistence.EntityManagerFactory; -import javax.persistence.Persistence; - -import me.prettyprint.cassandra.serializers.LongSerializer; -import me.prettyprint.cassandra.serializers.StringSerializer; -import me.prettyprint.cassandra.serializers.UUIDSerializer; -import me.prettyprint.hector.api.query.SliceQuery; -import me.prettyprint.hom.beans.SimpleRelationshipBean; -import me.prettyprint.hom.beans.SimpleTestBean; - -import org.apache.openjpa.persistence.JPAFacadeHelper; -import org.junit.BeforeClass; -import org.junit.Ignore; -import org.junit.Test; - -public class EntityFacadeTest { - - private static EntityManagerFactory entityManagerFactory; - - @Test - public void testEntityFacadeValues() { - EntityFacade entityFacade = new EntityFacade(JPAFacadeHelper.getMetaData(entityManagerFactory, SimpleTestBean.class)); - assertEquals("SimpleTestBeanColumnFamily", entityFacade.getColumnFamilyName()); - assertEquals(LongSerializer.get(), entityFacade.getKeySerializer()); - assertEquals(StringSerializer.get(), entityFacade.getSerializer("name")); - } - - @Test - public void testOneToManyFacadeValues() { - EntityFacade entityFacade = new EntityFacade(JPAFacadeHelper.getMetaData(entityManagerFactory, SimpleRelationshipBean.class)); - assertEquals(UUIDSerializer.get(), entityFacade.getKeySerializer()); - } - - @Ignore - public void testBuildSliceQuery() { - EntityFacade entityFacade = new EntityFacade(JPAFacadeHelper.getMetaData(entityManagerFactory, SimpleTestBean.class)); - // ColumnFamily wrapper would work really well here - } - - @BeforeClass - public static void setup() { - entityManagerFactory = Persistence.createEntityManagerFactory("openjpa"); - } - -} diff --git a/object-mapper/src/test/java/me/prettyprint/hom/openjpa/FullContainerTest.java b/object-mapper/src/test/java/me/prettyprint/hom/openjpa/FullContainerTest.java deleted file mode 100644 index 6f54b731d..000000000 --- a/object-mapper/src/test/java/me/prettyprint/hom/openjpa/FullContainerTest.java +++ /dev/null @@ -1,63 +0,0 @@ -package me.prettyprint.hom.openjpa; - -import java.util.Map; - -import javax.persistence.EntityManager; -import javax.persistence.EntityTransaction; - -import me.prettyprint.hom.beans.MyTestBean; -import me.prettyprint.hom.beans.SimpleTestBean; - -import org.apache.openjpa.persistence.OpenJPAEntityManager; -import org.apache.openjpa.persistence.OpenJPAEntityManagerFactorySPI; -import org.apache.openjpa.persistence.test.AbstractPersistenceTestCase; -import org.junit.Ignore; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -@Ignore -public class FullContainerTest extends AbstractPersistenceTestCase { - private static Logger log = LoggerFactory.getLogger(FullContainerTest.class); - @Ignore - public void ptestCreateEntityManager() { - OpenJPAEntityManagerFactorySPI emf = createNamedEMF("openjpa"); - try { - EntityManager em = emf.createEntityManager(); - - EntityTransaction t = em.getTransaction(); - assertNotNull(t); - t.begin(); - t.setRollbackOnly(); - t.rollback(); - - // openjpa-facade test - assertTrue(em instanceof OpenJPAEntityManager); - OpenJPAEntityManager ojem = (OpenJPAEntityManager) em; - ojem.getFetchPlan().setMaxFetchDepth(1); - assertEquals(1, ojem.getFetchPlan().getMaxFetchDepth()); - em.close(); - } finally { - closeEMF(emf); - } - } - - public void testPersistEntity() { - OpenJPAEntityManagerFactorySPI emf = createNamedEMF("openjpa"); - - - try { - EntityManager em = emf.createEntityManager(); - SimpleTestBean stb = new SimpleTestBean(); - stb.setId(1); - stb.setName("my name"); - em.persist(stb); - - - } catch (Exception e) { - e.printStackTrace(); - - } finally { - closeEMF(emf); - } - } -} diff --git a/object-mapper/src/test/java/me/prettyprint/hom/openjpa/ManagedEntityTestBase.java b/object-mapper/src/test/java/me/prettyprint/hom/openjpa/ManagedEntityTestBase.java deleted file mode 100644 index 81acb587d..000000000 --- a/object-mapper/src/test/java/me/prettyprint/hom/openjpa/ManagedEntityTestBase.java +++ /dev/null @@ -1,18 +0,0 @@ -package me.prettyprint.hom.openjpa; - -import javax.persistence.EntityManagerFactory; -import javax.persistence.Persistence; - -import org.junit.BeforeClass; - -import me.prettyprint.hom.CassandraTestBase; - -public abstract class ManagedEntityTestBase extends CassandraTestBase { - - protected static EntityManagerFactory entityManagerFactory; - - @BeforeClass - public static void setup() { - entityManagerFactory = Persistence.createEntityManagerFactory("openjpa"); - } -} diff --git a/object-mapper/src/test/java/me/prettyprint/hom/openjpa/MappingUtilsTest.java b/object-mapper/src/test/java/me/prettyprint/hom/openjpa/MappingUtilsTest.java deleted file mode 100644 index 84144d570..000000000 --- a/object-mapper/src/test/java/me/prettyprint/hom/openjpa/MappingUtilsTest.java +++ /dev/null @@ -1,76 +0,0 @@ -package me.prettyprint.hom.openjpa; - -import static org.junit.Assert.*; - -import java.util.List; - -import javax.persistence.EntityManagerFactory; -import javax.persistence.Persistence; - -import me.prettyprint.cassandra.model.ExecutingKeyspace; -import me.prettyprint.cassandra.model.thrift.ThriftSliceQuery; -import me.prettyprint.cassandra.serializers.LongSerializer; -import me.prettyprint.hector.api.Keyspace; -import me.prettyprint.hector.api.Serializer; -import me.prettyprint.hector.api.mutation.Mutator; -import me.prettyprint.hector.api.query.SliceQuery; -import me.prettyprint.hom.beans.SimpleTestBean; -import me.prettyprint.hom.openjpa.MappingUtils; - -import org.apache.openjpa.kernel.Broker; -import org.apache.openjpa.meta.ClassMetaData; -import org.apache.openjpa.persistence.JPAFacadeHelper; -import org.apache.openjpa.util.LongId; -import org.junit.BeforeClass; -import org.junit.Ignore; -import org.junit.Test; -import org.mockito.Mock; -import org.mockito.Mockito; - -public class MappingUtilsTest { - - private MappingUtils mappingUtils; - private static EntityManagerFactory entityManagerFactory; - private Keyspace keyspace = Mockito.mock(ExecutingKeyspace.class); - - @Test - public void testConvertClassToSlice() { - mappingUtils = new MappingUtils(); - - List cols = mappingUtils.buildColumnList(JPAFacadeHelper.getMetaData(entityManagerFactory, SimpleTestBean.class)); - - assertEquals("name",cols.get(0)); - } - - @Test - public void testGetSerializer() { - mappingUtils = new MappingUtils(); - - Serializer serializer = mappingUtils.getSerializer(new LongId(SimpleTestBean.class, 1L)); - - assertTrue(serializer instanceof LongSerializer); - } - - @Test - public void testBuildSliceQuery() { - mappingUtils = new MappingUtils(); - LongId id = new LongId(SimpleTestBean.class, 1L); - EntityFacade entityFacade = new EntityFacade(JPAFacadeHelper.getMetaData(entityManagerFactory, SimpleTestBean.class)); - SliceQuery sliceQuery = mappingUtils.buildSliceQuery(id, entityFacade, keyspace); - assertTrue(((ThriftSliceQuery)sliceQuery).getColumnNames().contains("name")); - assertEquals(1,((ThriftSliceQuery)sliceQuery).getColumnNames().size()); - } - - @Test - public void testBuildMutation() { - mappingUtils = new MappingUtils(); - LongId id = new LongId(SimpleTestBean.class, 1L); - Broker broker = JPAFacadeHelper.toBroker(entityManagerFactory.createEntityManager()); - //Mutator mutator = mappingUtils.buildMutator(id, broker., keyspace); - } - - @BeforeClass - public static void setup() { - entityManagerFactory = Persistence.createEntityManagerFactory("openjpa"); - } -} diff --git a/object-mapper/src/test/java/me/prettyprint/hom/openjpa/OneToManyRelationshipTest.java b/object-mapper/src/test/java/me/prettyprint/hom/openjpa/OneToManyRelationshipTest.java deleted file mode 100644 index f0cb03985..000000000 --- a/object-mapper/src/test/java/me/prettyprint/hom/openjpa/OneToManyRelationshipTest.java +++ /dev/null @@ -1,45 +0,0 @@ -package me.prettyprint.hom.openjpa; - -import static org.junit.Assert.*; - -import java.util.UUID; - -import javax.persistence.EntityManager; - -import me.prettyprint.cassandra.utils.TimeUUIDUtils; -import me.prettyprint.hom.beans.SimpleRelationshipBean; -import me.prettyprint.hom.beans.SimpleTestBean; -import me.prettyprint.hom.service.EntitySchemaManager; -import me.prettyprint.hom.service.EntitySchemaStatus; - -import org.apache.openjpa.persistence.JPAFacadeHelper; -import org.junit.Before; -import org.junit.Test; - - -public class OneToManyRelationshipTest extends ManagedEntityTestBase { - - @Test - public void testManyToOneSaveAndLoad() { - EntityManager em = entityManagerFactory.createEntityManager(); - em.getTransaction().begin(); - - SimpleRelationshipBean srb = new SimpleRelationshipBean(); - UUID uuid = TimeUUIDUtils.getUniqueTimeUUIDinMillis(); - srb.setBaseId(uuid); - srb.setMyType("my_type"); - srb.addSimpleTestBean(new SimpleTestBean(5L, "my owned bean")); - - em.persist(srb); - - em.getTransaction().commit(); - em.close(); - - EntityManager em2 = entityManagerFactory.createEntityManager(); - SimpleRelationshipBean srb2 = em2.find(SimpleRelationshipBean.class, uuid); - assertEquals(1,srb.getSimpleTestBeans().size()); - assertEquals("my owned bean", srb.getSimpleTestBeans().iterator().next().getName()); - assertEquals(uuid,srb.getBaseId()); - - } -} diff --git a/object-mapper/src/test/java/me/prettyprint/hom/openjpa/StandaloneEntityManagerFactoryTest.java b/object-mapper/src/test/java/me/prettyprint/hom/openjpa/StandaloneEntityManagerFactoryTest.java deleted file mode 100644 index 0befcad26..000000000 --- a/object-mapper/src/test/java/me/prettyprint/hom/openjpa/StandaloneEntityManagerFactoryTest.java +++ /dev/null @@ -1,40 +0,0 @@ -package me.prettyprint.hom.openjpa; - -import static org.junit.Assert.*; - -import javax.persistence.EntityManager; -import javax.persistence.EntityManagerFactory; -import javax.persistence.Persistence; - -import me.prettyprint.hom.CassandraTestBase; -import me.prettyprint.hom.beans.SimpleTestBean; - -import org.junit.BeforeClass; -import org.junit.Test; - -public class StandaloneEntityManagerFactoryTest extends ManagedEntityTestBase { - - @Test - public void testBuildEntityManagerFactory() { - - EntityManager em = entityManagerFactory.createEntityManager(); - em.getTransaction().begin(); - em.persist(new SimpleTestBean(1, "foo")); - em.getTransaction().commit(); - em.close(); - - //em.getTransaction().begin(); - - EntityManager em2 = entityManagerFactory.createEntityManager(); - SimpleTestBean stb = em2.find(SimpleTestBean.class, 1); - //em.getTransaction().commit(); - assertEquals("foo",stb.getName()); - - - em2.getTransaction().begin(); - em2.remove(stb); - em2.getTransaction().commit(); - em2.close(); - } - -} diff --git a/object-mapper/src/test/java/me/prettyprint/hom/service/EntitySchemaManagerTest.java b/object-mapper/src/test/java/me/prettyprint/hom/service/EntitySchemaManagerTest.java deleted file mode 100644 index 6b08414d6..000000000 --- a/object-mapper/src/test/java/me/prettyprint/hom/service/EntitySchemaManagerTest.java +++ /dev/null @@ -1,67 +0,0 @@ -package me.prettyprint.hom.service; - -import static org.junit.Assert.assertEquals; - -import javax.persistence.Column; -import javax.persistence.Entity; -import javax.persistence.EntityManagerFactory; -import javax.persistence.Id; -import javax.persistence.Persistence; - -import me.prettyprint.hom.CassandraTestBase; -import me.prettyprint.hom.beans.SimpleRelationshipBean; -import me.prettyprint.hom.openjpa.EntityFacade; -import me.prettyprint.hom.service.EntitySchemaStatus.SchemaResult; - -import org.apache.openjpa.persistence.JPAFacadeHelper; -import org.junit.BeforeClass; -import org.junit.Test; - -public class EntitySchemaManagerTest extends CassandraTestBase { - - private static EntitySchemaManager entitySchemaManager; - private static EntityManagerFactory entityManagerFactory; - private static EntityFacade entityFacade; - - @BeforeClass - public static void setup() { - entitySchemaManager = new EntitySchemaManager(cluster, keyspace); - entityManagerFactory = Persistence.createEntityManagerFactory("openjpa"); - } - - @Test - public void testCreateSchemaFromEntity() { - entityFacade = new EntityFacade(JPAFacadeHelper.getMetaData(entityManagerFactory, ReallySimpleBean.class)); - EntitySchemaStatus ecs = entitySchemaManager.createSchema(entityFacade); - assertEquals(SchemaResult.CREATED, ecs.getSchemaResult()); - } - - @Test - public void testCreateSchemaFailExisting() { - entityFacade = new EntityFacade(JPAFacadeHelper.getMetaData(entityManagerFactory, ReallySimpleBean.class)); - EntitySchemaStatus ecs = entitySchemaManager.createSchema(entityFacade); - assertEquals(SchemaResult.NOT_MODIFIED, ecs.getSchemaResult()); - } - - @Entity - class ReallySimpleBean { - int id; - String val; - @Id - public int getId() { - return id; - } - public void setId(int id) { - this.id = id; - } - @Column - public String getVal() { - return val; - } - public void setVal(String val) { - this.val = val; - } - - - } -} diff --git a/object-mapper/src/test/resources/META-INF/persistence.xml b/object-mapper/src/test/resources/META-INF/persistence.xml deleted file mode 100644 index 444de2b0e..000000000 --- a/object-mapper/src/test/resources/META-INF/persistence.xml +++ /dev/null @@ -1,43 +0,0 @@ - - - me.prettyprint.hom.CassandraPersistenceProvider - - - - - - - - - - org.apache.openjpa.persistence.PersistenceProviderImpl - - me.prettyprint.hom.beans.SimpleTestBean - me.prettyprint.hom.beans.SimpleRelationshipBean - - - - - - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/object-mapper/src/test/resources/spring-jpa-container-context.xml b/object-mapper/src/test/resources/spring-jpa-container-context.xml deleted file mode 100644 index 46ba03deb..000000000 --- a/object-mapper/src/test/resources/spring-jpa-container-context.xml +++ /dev/null @@ -1,34 +0,0 @@ - - - - - - - - - - - - \ No newline at end of file From b2ee5f90d918c2712d13ccc26b7243ad05b58727 Mon Sep 17 00:00:00 2001 From: btoddb Date: Thu, 19 May 2011 14:00:14 -0700 Subject: [PATCH 136/144] changed HOM version to 1.1-01-SNAPSHOT ... the 1.1 is for the 0.8 cassandra version. 1.0 is still for 0.7 --- object-mapper/pom.xml | 58 +++++++++++++++++++------------------------ 1 file changed, 25 insertions(+), 33 deletions(-) diff --git a/object-mapper/pom.xml b/object-mapper/pom.xml index b7681ad01..e83530783 100644 --- a/object-mapper/pom.xml +++ b/object-mapper/pom.xml @@ -8,7 +8,7 @@ hector-object-mapper hector-object-mapper - 1.0-05-SNAPSHOT + 1.1-01-SNAPSHOT @@ -34,6 +34,7 @@ org.slf4j slf4j-api + 1.6.1 @@ -68,6 +69,12 @@ org.apache.cassandra cassandra-thrift + + + servlet-api + javax.servlet + + log4j @@ -80,12 +87,6 @@ junit 4.8.1 test - - - org.mockito - mockito-all - 1.8.2 - test org.slf4j @@ -108,11 +109,12 @@ org.springframework spring-context 3.0.5.RELEASE - - - org.springframework - spring-orm - 3.0.5.RELEASE + + + spring-aop + org.springframework + + org.springframework @@ -121,27 +123,17 @@ test - org.apache.openjpa - openjpa - 2.0.1 - - - org.apache.openjpa - openjpa-kernel - 2.0.1 - - - org.apache.openjpa - openjpa-persistence-jdbc - 2.0.1 - test - - - org.apache.openjpa - openjpa-persistence-jdbc - 2.0.1 - test-jar - test + org.apache.openjpa + openjpa-persistence + 2.1.0 + jar + compile + + + openjpa-kernel + org.apache.openjpa + + From 9b777c9c9dfe5ef4cd669b8e91fc0c950a130dc0 Mon Sep 17 00:00:00 2001 From: zznate Date: Fri, 20 May 2011 16:09:16 -0500 Subject: [PATCH 137/144] reset to the results of adding the pool to the connection manager. This could cause a pretty ugly issue where the host would just disappear if there was a failure adding it back after the successful test --- .../cassandra/connection/CassandraHostRetryService.java | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/core/src/main/java/me/prettyprint/cassandra/connection/CassandraHostRetryService.java b/core/src/main/java/me/prettyprint/cassandra/connection/CassandraHostRetryService.java index 1cdd94b8e..5112b4a8e 100644 --- a/core/src/main/java/me/prettyprint/cassandra/connection/CassandraHostRetryService.java +++ b/core/src/main/java/me/prettyprint/cassandra/connection/CassandraHostRetryService.java @@ -96,8 +96,7 @@ public void run() { boolean reconnected = verifyConnection(cassandraHost); log.info("Downed Host retry status {} with host: {}", reconnected, cassandraHost.getName()); if ( reconnected ) { - //cassandraClientPool.getCluster().addHost(cassandraHost, true); - connectionManager.addCassandraHost(cassandraHost); + reconnected = connectionManager.addCassandraHost(cassandraHost); } if ( !reconnected && cassandraHost != null ) { downedHostQueue.add(cassandraHost); @@ -128,7 +127,4 @@ private boolean verifyConnection(CassandraHost cassandraHost) { } - // TODO create callable to handle checking - - // perhaps wrap CassandraHost and add a lastRetryTime? } From 385303a7d0f80a916d10a2d6fb42e8e799964223 Mon Sep 17 00:00:00 2001 From: zznate Date: Fri, 20 May 2011 16:11:01 -0500 Subject: [PATCH 138/144] fixed retry logic in the case of fail after one more try configuration. Also modifies some long standing (and I believe incorrect) behaviour of trying until successful on a timeout exception --- .../connection/HConnectionManager.java | 16 ++++---- .../connection/HConnectionManagerTest.java | 37 +++++++++++++++++++ 2 files changed, 45 insertions(+), 8 deletions(-) diff --git a/core/src/main/java/me/prettyprint/cassandra/connection/HConnectionManager.java b/core/src/main/java/me/prettyprint/cassandra/connection/HConnectionManager.java index ec4f6cb8a..43a1735fb 100644 --- a/core/src/main/java/me/prettyprint/cassandra/connection/HConnectionManager.java +++ b/core/src/main/java/me/prettyprint/cassandra/connection/HConnectionManager.java @@ -227,7 +227,6 @@ public void operateWithFailover(Operation op) throws HectorException { // break out on HUnavailableException as well since we can no longer satisfy the CL throw he; } else if ( he instanceof HectorTransportException) { - --retries; // client can be null in this situation if ( client != null ) { client.close(); @@ -235,23 +234,22 @@ public void operateWithFailover(Operation op) throws HectorException { markHostAsDown(pool.getCassandraHost()); excludeHosts.add(pool.getCassandraHost()); retryable = true; - if ( retries > 0 ) { - monitor.incCounter(Counter.RECOVERABLE_TRANSPORT_EXCEPTIONS); - } + + monitor.incCounter(Counter.RECOVERABLE_TRANSPORT_EXCEPTIONS); + } else if (he instanceof HTimedOutException ) { // DO NOT drecrement retries, we will be keep retrying on timeouts until it comes back // if HLT.checkTimeout(cassandraHost): suspendHost(cassandraHost); doTimeoutCheck(pool.getCassandraHost()); - if ( hostPools.size() > 1) { - retryable = true; - } + + retryable = true; + monitor.incCounter(Counter.RECOVERABLE_TIMED_OUT_EXCEPTIONS); client.close(); // TODO timecheck on how long we've been waiting on timeouts here // suggestion per user moores on hector-users } else if ( he instanceof PoolExhaustedException ) { retryable = true; - --retries; if ( hostPools.size() == 1 ) { throw he; } @@ -264,11 +262,13 @@ public void operateWithFailover(Operation op) throws HectorException { retryable = false; } if ( retries <= 0 || retryable == false) throw he; + log.warn("Could not fullfill request on this host {}", client); log.warn("Exception: ", he); monitor.incCounter(Counter.SKIP_HOST_SUCCESS); sleepBetweenHostSkips(op.failoverPolicy); } finally { + --retries; if ( !success ) { monitor.incCounter(op.failCounter); stopWatch.stop(op.stopWatchTagName + ".fail_"); diff --git a/core/src/test/java/me/prettyprint/cassandra/connection/HConnectionManagerTest.java b/core/src/test/java/me/prettyprint/cassandra/connection/HConnectionManagerTest.java index fe59ad31a..590aef571 100644 --- a/core/src/test/java/me/prettyprint/cassandra/connection/HConnectionManagerTest.java +++ b/core/src/test/java/me/prettyprint/cassandra/connection/HConnectionManagerTest.java @@ -2,12 +2,18 @@ import static org.junit.Assert.*; +import org.apache.cassandra.thrift.Cassandra.Client; import org.junit.Before; import org.junit.Test; import me.prettyprint.cassandra.BaseEmbededServerSetupTest; import me.prettyprint.cassandra.service.CassandraHost; import me.prettyprint.cassandra.service.CassandraHostConfigurator; +import me.prettyprint.cassandra.service.FailoverPolicy; +import me.prettyprint.cassandra.service.Operation; +import me.prettyprint.cassandra.service.OperationType; +import me.prettyprint.hector.api.exceptions.HTimedOutException; +import me.prettyprint.hector.api.exceptions.HectorException; import me.prettyprint.hector.api.exceptions.HectorTransportException; public class HConnectionManagerTest extends BaseEmbededServerSetupTest { @@ -56,4 +62,35 @@ public void testSuspendCassandraHost() { assertEquals(1,connectionManager.getSuspendedCassandraHosts().size()); assertTrue(connectionManager.unsuspendCassandraHost(cassandraHost)); } + + @Test(expected=HTimedOutException.class) + public void testTimedOutOperateWithFailover() { + setupClient(); + FailoverPolicy fp = FailoverPolicy.ON_FAIL_TRY_ONE_NEXT_AVAILABLE; + connectionManager.operateWithFailover(new TimeoutOp(fp)); + } + + abstract class StubOp extends Operation { + + StubOp(FailoverPolicy fp) { + this(OperationType.META_READ); + failoverPolicy = fp; + } + + public StubOp(OperationType operationType) { + super(operationType); + } + } + + class TimeoutOp extends StubOp { + + TimeoutOp(FailoverPolicy fp) { + super(fp); + } + + @Override + public String execute(Client cassandra) throws HectorException { + throw new HTimedOutException("fake timeout"); + } + } } From c88e59d49a50daba1ef287418d5c835291b5a98d Mon Sep 17 00:00:00 2001 From: Ed Anuff Date: Sat, 21 May 2011 15:41:28 -0700 Subject: [PATCH 139/144] adding recommended default configuration string for dynamic comparator --- .../java/me/prettyprint/hector/api/beans/DynamicComposite.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/core/src/main/java/me/prettyprint/hector/api/beans/DynamicComposite.java b/core/src/main/java/me/prettyprint/hector/api/beans/DynamicComposite.java index 2d06b77e5..5d1e5d219 100644 --- a/core/src/main/java/me/prettyprint/hector/api/beans/DynamicComposite.java +++ b/core/src/main/java/me/prettyprint/hector/api/beans/DynamicComposite.java @@ -5,6 +5,8 @@ public class DynamicComposite extends AbstractComposite { + public final static String DEFAULT_DYNAMIC_COMPOSITE_ALIASES = "(a=>AsciiType,b=>BytesType,i=>IntegerType,x=>LexicalUUIDType,l=>LongType,t=>TimeUUIDType,s=>UTF8Type,u=>UUIDType,A=>AsciiType(reversed=true),B=>BytesType(reversed=true),I=>IntegerType(reversed=true),X=>LexicalUUIDType(reversed=true),L=>LongType(reversed=true),T=>TimeUUIDType(reversed=true),S=>UTF8Type(reversed=true),U=>UUIDType(reversed=true))"; + public DynamicComposite() { super(true); } From c9f3d3e3acfb2e5e10f0232887a88afe523e4edd Mon Sep 17 00:00:00 2001 From: zznate Date: Tue, 24 May 2011 14:05:31 -0500 Subject: [PATCH 140/144] first cut of CqlQuery, loosened OrderedRowsImpl to hornshoe in results --- .../prettyprint/cassandra/model/CqlQuery.java | 94 +++++++++++++++++++ .../prettyprint/cassandra/model/CqlRows.java | 60 ++++++++++++ .../cassandra/model/OrderedRowsImpl.java | 9 +- .../prettyprint/cassandra/model/RowsImpl.java | 4 + .../testutils/EmbeddedSchemaLoader.java | 3 +- .../cassandra/model/CqlQueryTest.java | 65 +++++++++++++ 6 files changed, 232 insertions(+), 3 deletions(-) create mode 100644 core/src/main/java/me/prettyprint/cassandra/model/CqlQuery.java create mode 100644 core/src/main/java/me/prettyprint/cassandra/model/CqlRows.java create mode 100644 core/src/test/java/me/prettyprint/cassandra/model/CqlQueryTest.java diff --git a/core/src/main/java/me/prettyprint/cassandra/model/CqlQuery.java b/core/src/main/java/me/prettyprint/cassandra/model/CqlQuery.java new file mode 100644 index 000000000..b1c75d430 --- /dev/null +++ b/core/src/main/java/me/prettyprint/cassandra/model/CqlQuery.java @@ -0,0 +1,94 @@ +package me.prettyprint.cassandra.model; + +import java.nio.ByteBuffer; +import java.util.Iterator; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; + +import me.prettyprint.cassandra.serializers.StringSerializer; +import me.prettyprint.cassandra.service.Operation; +import me.prettyprint.cassandra.service.OperationType; +import me.prettyprint.hector.api.Keyspace; +import me.prettyprint.hector.api.Serializer; +import me.prettyprint.hector.api.exceptions.HectorException; +import me.prettyprint.hector.api.query.QueryResult; + +import org.apache.cassandra.thrift.Column; +import org.apache.cassandra.thrift.Compression; +import org.apache.cassandra.thrift.CqlResult; +import org.apache.cassandra.thrift.CqlRow; +import org.apache.cassandra.thrift.Cassandra.Client; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class CqlQuery extends AbstractBasicQuery> { + private static Logger log = LoggerFactory.getLogger(CqlQuery.class); + + private Serializer valueSerializer; + private String query; + private boolean useCompression; + + public CqlQuery(Keyspace k, Serializer keySerializer, + Serializer nameSerializer, Serializer valueSerializer) { + super(k, keySerializer, nameSerializer); + this.valueSerializer = valueSerializer; + } + + public CqlQuery setQuery(String query) { + this.query = query; + return this; + } + + public CqlQuery useCompression() { + useCompression = true; + return this; + } + + + @Override + public QueryResult> execute() { + + return new QueryResultImpl>( + keyspace.doExecuteOperation(new Operation>(OperationType.READ) { + + @Override + public CqlRows execute(Client cassandra) throws HectorException { + CqlRows rows = null; + try { + CqlResult result = cassandra.execute_cql_query(StringSerializer.get().toByteBuffer(query), + useCompression ? Compression.GZIP : Compression.NONE); + if ( log.isDebugEnabled() ) { + log.debug("Found CqlResult: {}", result); + } + switch (result.getType()) { + case INT: + rows = new CqlRows(result.getNum()); + break; + case VOID: + rows = new CqlRows(); + break; + + default: + if ( result.getRowsSize() > 0 ) { + LinkedHashMap> ret = new LinkedHashMap>(result.getRowsSize()); + + for (Iterator rowsIter = result.getRowsIterator(); rowsIter.hasNext(); ) { + CqlRow row = rowsIter.next(); + ret.put(ByteBuffer.wrap(row.getKey()), row.getColumns()); + } + Map> thriftRet = keySerializer.fromBytesMap(ret); + rows = new CqlRows((LinkedHashMap>)thriftRet, columnNameSerializer, valueSerializer); + } + break; + } + } catch (Exception ex) { + throw keyspace.getExceptionsTranslator().translate(ex); + } + return rows; + } + + }), this); + } + +} diff --git a/core/src/main/java/me/prettyprint/cassandra/model/CqlRows.java b/core/src/main/java/me/prettyprint/cassandra/model/CqlRows.java new file mode 100644 index 000000000..862d7130a --- /dev/null +++ b/core/src/main/java/me/prettyprint/cassandra/model/CqlRows.java @@ -0,0 +1,60 @@ +package me.prettyprint.cassandra.model; + +import java.util.LinkedHashMap; +import java.util.List; + +import me.prettyprint.hector.api.Serializer; + +import org.apache.cassandra.thrift.Column; +import org.apache.cassandra.thrift.CqlResultType; + +/** + * Row wrapper specific to the multi-type results capable from a CqlQuery. + * This is a bit more convoluted than I would like, put most of this API + * is still moving around, so we will stick with the overloading for now. + * + * @author zznate + */ +public final class CqlRows extends OrderedRowsImpl { + + private final CqlResultType resultType; + private int count; + + /** + * Constructed for {@link CqlResultType#ROWS} + * @param thriftRet + * @param nameSerializer + * @param valueSerializer + * @param resultType + */ + public CqlRows(LinkedHashMap> thriftRet, + Serializer nameSerializer, Serializer valueSerializer) { + super(thriftRet, nameSerializer, valueSerializer); + this.resultType = CqlResultType.ROWS; + } + + /** + * Constructed with only a count for {@link CqlResultType#INT} + * @param count + */ + public CqlRows(int count) { + super(); + this.resultType = CqlResultType.INT; + this.count = count; + } + + /** + * Constructed as empty for {@link CqlResultType#VOID} + */ + public CqlRows() { + super(); + this.resultType = CqlResultType.VOID; + } + + public int getAsCount() { + if ( !resultType.equals(CqlResultType.INT)) + throw new IllegalArgumentException("Attempted to extract count from the wrong type of CQL query: " + resultType.toString()); + return count; + } + +} diff --git a/core/src/main/java/me/prettyprint/cassandra/model/OrderedRowsImpl.java b/core/src/main/java/me/prettyprint/cassandra/model/OrderedRowsImpl.java index e785fc8a1..7abcac28f 100644 --- a/core/src/main/java/me/prettyprint/cassandra/model/OrderedRowsImpl.java +++ b/core/src/main/java/me/prettyprint/cassandra/model/OrderedRowsImpl.java @@ -16,15 +16,20 @@ * @param * @param */ -public final class OrderedRowsImpl extends RowsImpl implements OrderedRows { +public class OrderedRowsImpl extends RowsImpl implements OrderedRows { - private final List> rowsList; + protected final List> rowsList; public OrderedRowsImpl(LinkedHashMap> thriftRet, Serializer nameSerializer, Serializer valueSerializer) { super(thriftRet, nameSerializer, valueSerializer); rowsList = new ArrayList>(rows.values()); } + + protected OrderedRowsImpl() { + super(); + rowsList = new ArrayList>(); + } /** * Preserves rows order diff --git a/core/src/main/java/me/prettyprint/cassandra/model/RowsImpl.java b/core/src/main/java/me/prettyprint/cassandra/model/RowsImpl.java index adc7cf78e..8cb5c496d 100644 --- a/core/src/main/java/me/prettyprint/cassandra/model/RowsImpl.java +++ b/core/src/main/java/me/prettyprint/cassandra/model/RowsImpl.java @@ -34,6 +34,10 @@ public RowsImpl(Map> thriftRet, Serializer nameSerializer, valueSerializer)); } } + + protected RowsImpl() { + this.rows = new HashMap>(); + } @Override public Row getByKey(K key) { diff --git a/core/src/main/java/me/prettyprint/cassandra/testutils/EmbeddedSchemaLoader.java b/core/src/main/java/me/prettyprint/cassandra/testutils/EmbeddedSchemaLoader.java index c0b3da05d..4e3fc8ca1 100644 --- a/core/src/main/java/me/prettyprint/cassandra/testutils/EmbeddedSchemaLoader.java +++ b/core/src/main/java/me/prettyprint/cassandra/testutils/EmbeddedSchemaLoader.java @@ -66,7 +66,8 @@ public static Collection schemaDefinition() { // Column Families standardCFMD(ks1, "Standard1"), standardCFMD(ks1, "Standard2"), standardCFMD(ks1, "Standard3"), standardCFMD(ks1, "Standard4"), - standardCFMD(ks1, "StandardLong1"), standardCFMD(ks1, "StandardLong2"), + standardCFMD(ks1, "StandardLong1").keyValidator(UTF8Type.instance), + standardCFMD(ks1, "StandardLong2"), superCFMD(ks1, "Super1", BytesType.instance), superCFMD(ks1, "Super2", LongType.instance), superCFMD(ks1, "Super3", LongType.instance), superCFMD(ks1, "Super4", UTF8Type.instance), superCFMD(ks1, "Super5", diff --git a/core/src/test/java/me/prettyprint/cassandra/model/CqlQueryTest.java b/core/src/test/java/me/prettyprint/cassandra/model/CqlQueryTest.java new file mode 100644 index 000000000..86fcd3487 --- /dev/null +++ b/core/src/test/java/me/prettyprint/cassandra/model/CqlQueryTest.java @@ -0,0 +1,65 @@ +package me.prettyprint.cassandra.model; + +import static me.prettyprint.hector.api.factory.HFactory.createColumn; +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.getOrCreateCluster; +import static org.junit.Assert.*; + +import org.junit.Before; +import org.junit.Test; + +import me.prettyprint.cassandra.BaseEmbededServerSetupTest; +import me.prettyprint.cassandra.serializers.LongSerializer; +import me.prettyprint.cassandra.serializers.StringSerializer; +import me.prettyprint.hector.api.Cluster; +import me.prettyprint.hector.api.Keyspace; +import me.prettyprint.hector.api.query.QueryResult; + +public class CqlQueryTest extends BaseEmbededServerSetupTest { + + private final static String KEYSPACE = "Keyspace1"; + private static final StringSerializer se = new StringSerializer(); + private static final LongSerializer le = new LongSerializer(); + private Cluster cluster; + private Keyspace keyspace; + private String cf = "StandardLong1"; + + @Before + public void setupCase() { + cluster = getOrCreateCluster("MyCluster", "127.0.0.1:9170"); + keyspace = createKeyspace(KEYSPACE, cluster); + createMutator(keyspace, se) + .addInsertion("cqlQueryTest_key1", cf, createColumn("birthyear", 1974L, se, le)) + .addInsertion("cqlQueryTest_key1", cf, createColumn("birthmonth", 4L, se, le)) + .addInsertion("cqlQueryTest_key2", cf, createColumn("birthyear", 1975L, se, le)) + .addInsertion("cqlQueryTest_key2", cf, createColumn("birthmonth", 4L, se, le)) + .addInsertion("cqlQueryTest_key3", cf, createColumn("birthyear", 1975L, se, le)) + .addInsertion("cqlQueryTest_key3", cf, createColumn("birthmonth", 5L, se, le)) + .addInsertion("cqlQueryTest_key4", cf, createColumn("birthyear", 1975L, se, le)) + .addInsertion("cqlQueryTest_key4", cf, createColumn("birthmonth", 6L, se, le)) + .addInsertion("cqlQueryTest_key5", cf, createColumn("birthyear", 1975L, se, le)) + .addInsertion("cqlQueryTest_key5", cf, createColumn("birthmonth", 7L, se, le)) + .addInsertion("cqlQueryTest_key6", cf, createColumn("birthyear", 1976L, se, le)) + .addInsertion("cqlQueryTest_key6", cf, createColumn("birthmonth", 6L, se, le)) + .execute(); + } + + @Test + public void testSimpleSelect() { + CqlQuery cqlQuery = new CqlQuery(keyspace, se, se, le); + cqlQuery.setQuery("select * from StandardLong1"); + QueryResult> result = cqlQuery.execute(); + assertEquals(6,result.get().getCount()); + + } + + @Test + public void testCountQuery() { + CqlQuery cqlQuery = new CqlQuery(keyspace, se, se, le); + cqlQuery.setQuery("SELECT COUNT(*) FROM StandardLong1 WHERE KEY = 'cqlQueryTest_key1'"); + QueryResult> result = cqlQuery.execute(); + assertEquals(2, result.get().getAsCount()); + } + +} From 9642077d7c40aa1cd2fdf52f386dcde3bce97b06 Mon Sep 17 00:00:00 2001 From: zznate Date: Tue, 24 May 2011 15:06:22 -0500 Subject: [PATCH 141/144] added additional javadoc, test coverage for validation format error --- .../prettyprint/cassandra/model/CqlQuery.java | 45 ++++++++++++++++++- .../prettyprint/cassandra/model/CqlRows.java | 5 +++ .../cassandra/model/CqlQueryTest.java | 18 +++++--- 3 files changed, 61 insertions(+), 7 deletions(-) diff --git a/core/src/main/java/me/prettyprint/cassandra/model/CqlQuery.java b/core/src/main/java/me/prettyprint/cassandra/model/CqlQuery.java index b1c75d430..68f618992 100644 --- a/core/src/main/java/me/prettyprint/cassandra/model/CqlQuery.java +++ b/core/src/main/java/me/prettyprint/cassandra/model/CqlQuery.java @@ -22,11 +22,41 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; +/** + * First cut at a CQL implementation. Not too much time has been spent here + * as this API is currently a moving target. We have a lot of experience with + * these hijinks by the Apache Cassandra team, so it was deemed prudent to do + * something simple initially until the dust settles. + * + * You are expected to know what you are getting into if you plan on using + * CQL queries in your application. Spend some time looking through the + * unit tests here in Hector and the Cassandra source tree. For a number of + * detailed examples, see test_cql.py in the test/system folder of the + * Apache Cassandra source distribution. + * + * Note: if you immediately get an exception such as: + * "InvalidRequestException(why:cannot parse 'foo' as hex bytes)" + * It means one of two things: + *
    + *
  1. you have not formatted your query correct
  2. + *
  3. You have not configured the correct validators on your column family
  4. + *
+ * + * In both cases, even though the query is most likely a string, it is up to you to format + * this query according to the comparator (used for the column name), key validator + * and value validator. This can be a little confusing as only the comparator is required. + * The other two default to BytesType. + * + * See the docs on {@link CqlRows} for additional details. + * + * @author zznate + * + */ public class CqlQuery extends AbstractBasicQuery> { private static Logger log = LoggerFactory.getLogger(CqlQuery.class); private Serializer valueSerializer; - private String query; + private ByteBuffer query; private boolean useCompression; public CqlQuery(Keyspace k, Serializer keySerializer, @@ -35,7 +65,18 @@ public CqlQuery(Keyspace k, Serializer keySerializer, this.valueSerializer = valueSerializer; } + /** + * Set the query as a String. Here for convienience. See above for some + * caveats. Calls {@link StringSerializer#toByteBuffer(String)} directly. + * @param query + * @return + */ public CqlQuery setQuery(String query) { + this.query = StringSerializer.get().toByteBuffer(query); + return this; + } + + public CqlQuery setQuery(ByteBuffer qeury) { this.query = query; return this; } @@ -56,7 +97,7 @@ public QueryResult> execute() { public CqlRows execute(Client cassandra) throws HectorException { CqlRows rows = null; try { - CqlResult result = cassandra.execute_cql_query(StringSerializer.get().toByteBuffer(query), + CqlResult result = cassandra.execute_cql_query(query, useCompression ? Compression.GZIP : Compression.NONE); if ( log.isDebugEnabled() ) { log.debug("Found CqlResult: {}", result); diff --git a/core/src/main/java/me/prettyprint/cassandra/model/CqlRows.java b/core/src/main/java/me/prettyprint/cassandra/model/CqlRows.java index 862d7130a..03c7fec5c 100644 --- a/core/src/main/java/me/prettyprint/cassandra/model/CqlRows.java +++ b/core/src/main/java/me/prettyprint/cassandra/model/CqlRows.java @@ -51,6 +51,11 @@ public CqlRows() { this.resultType = CqlResultType.VOID; } + /** + * Will throw an IllegalArgumentException if called on a query that was not + * {@link CqlResultType#INT} + * @return + */ public int getAsCount() { if ( !resultType.equals(CqlResultType.INT)) throw new IllegalArgumentException("Attempted to extract count from the wrong type of CQL query: " + resultType.toString()); diff --git a/core/src/test/java/me/prettyprint/cassandra/model/CqlQueryTest.java b/core/src/test/java/me/prettyprint/cassandra/model/CqlQueryTest.java index 86fcd3487..f6c7790c0 100644 --- a/core/src/test/java/me/prettyprint/cassandra/model/CqlQueryTest.java +++ b/core/src/test/java/me/prettyprint/cassandra/model/CqlQueryTest.java @@ -4,18 +4,18 @@ 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.getOrCreateCluster; -import static org.junit.Assert.*; - -import org.junit.Before; -import org.junit.Test; - +import static org.junit.Assert.assertEquals; import me.prettyprint.cassandra.BaseEmbededServerSetupTest; import me.prettyprint.cassandra.serializers.LongSerializer; import me.prettyprint.cassandra.serializers.StringSerializer; import me.prettyprint.hector.api.Cluster; import me.prettyprint.hector.api.Keyspace; +import me.prettyprint.hector.api.exceptions.HInvalidRequestException; import me.prettyprint.hector.api.query.QueryResult; +import org.junit.Before; +import org.junit.Test; + public class CqlQueryTest extends BaseEmbededServerSetupTest { private final static String KEYSPACE = "Keyspace1"; @@ -62,4 +62,12 @@ public void testCountQuery() { assertEquals(2, result.get().getAsCount()); } + @Test(expected=HInvalidRequestException.class) + public void testSyntaxFailQuery() { + CqlQuery cqlQuery = new CqlQuery(keyspace, se, se, le); + cqlQuery.setQuery("SELECT COUNT(*) FROM Standard1 WHERE KEY = 'cqlQueryTest_key1'"); + QueryResult> result = cqlQuery.execute(); + + } + } From 093f25a206cad9fd5767e3d885758057dd1827fa Mon Sep 17 00:00:00 2001 From: patricioe Date: Fri, 27 May 2011 11:50:55 -0700 Subject: [PATCH 142/144] Upgrade to cassandra beta2 --- core/pom.xml | 4 ++-- pom.xml | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/core/pom.xml b/core/pom.xml index b7174f938..112eff812 100644 --- a/core/pom.xml +++ b/core/pom.xml @@ -105,12 +105,12 @@ org.apache.cassandra cassandra-all - 0.8.0-beta1 + 0.8.0-beta2 org.apache.cassandra cassandra-thrift - 0.8.0-beta1 + 0.8.0-beta2 org.apache.thrift diff --git a/pom.xml b/pom.xml index 9fc8e1375..209d3f4d5 100644 --- a/pom.xml +++ b/pom.xml @@ -88,12 +88,12 @@ org.apache.cassandra cassandra-all - 0.8.0-beta1 + 0.8.0-beta2 org.apache.cassandra cassandra-thrift - 0.8.0-beta1 + 0.8.0-beta2 org.apache.cassandra From f3d23e331273fe98f6d326caa9c6926704269633 Mon Sep 17 00:00:00 2001 From: mck Date: Tue, 31 May 2011 13:39:30 +0000 Subject: [PATCH 143/144] - add CfDef.key_validation_class support see https://issues.apache.org/jira/browse/CASSANDRA-2311 requires an update to cassandra-0.8.0-rc1 --- core/pom.xml | 3 -- .../model/BasicColumnFamilyDefinition.java | 20 ++++++--- .../cassandra/service/ThriftCfDef.java | 43 ++++++++++++------- .../api/ddl/ColumnFamilyDefinition.java | 1 + pom.xml | 4 +- 5 files changed, 45 insertions(+), 26 deletions(-) diff --git a/core/pom.xml b/core/pom.xml index 112eff812..05fbaa878 100644 --- a/core/pom.xml +++ b/core/pom.xml @@ -105,17 +105,14 @@ org.apache.cassandra cassandra-all - 0.8.0-beta2 org.apache.cassandra cassandra-thrift - 0.8.0-beta2 org.apache.thrift libthrift - 0.6.1 org.yaml diff --git a/core/src/main/java/me/prettyprint/cassandra/model/BasicColumnFamilyDefinition.java b/core/src/main/java/me/prettyprint/cassandra/model/BasicColumnFamilyDefinition.java index bc7fc5f50..f4bbc6f44 100644 --- a/core/src/main/java/me/prettyprint/cassandra/model/BasicColumnFamilyDefinition.java +++ b/core/src/main/java/me/prettyprint/cassandra/model/BasicColumnFamilyDefinition.java @@ -25,6 +25,7 @@ public class BasicColumnFamilyDefinition implements ColumnFamilyDefinition { private double readRepairChance; private int gcGraceSeconds; private String defaultValidationClass; + private String keyValidationClass; private int id; private int maxCompactionThreshold; private int minCompactionThreshold; @@ -40,7 +41,7 @@ public class BasicColumnFamilyDefinition implements ColumnFamilyDefinition { public BasicColumnFamilyDefinition() { this.columnDefinitions = new ArrayList(); } - + /** * Builds a {@link BasicColumnFamilyDefinition} based off the interface */ @@ -56,8 +57,8 @@ public BasicColumnFamilyDefinition(ColumnFamilyDefinition columnFamilyDefinition keyCacheSize = columnFamilyDefinition.getKeyCacheSize(); keyCacheSavePeriodInSeconds = columnFamilyDefinition.getKeyCacheSavePeriodInSeconds(); readRepairChance = columnFamilyDefinition.getReadRepairChance(); - columnDefinitions = columnFamilyDefinition.getColumnMetadata() != null - ? new ArrayList(columnFamilyDefinition.getColumnMetadata()) + columnDefinitions = columnFamilyDefinition.getColumnMetadata() != null + ? new ArrayList(columnFamilyDefinition.getColumnMetadata()) : new ArrayList(); gcGraceSeconds = columnFamilyDefinition.getGcGraceSeconds(); defaultValidationClass = columnFamilyDefinition.getDefaultValidationClass(); @@ -123,7 +124,7 @@ public void setMaxCompactionThreshold(int maxCompactionThreshold) { public void setMinCompactionThreshold(int minCompactionThreshold) { this.minCompactionThreshold = minCompactionThreshold; - } + } public void setRowCacheSavePeriodInSeconds(int rowCacheSavePeriodInSeconds) { this.rowCacheSavePeriodInSeconds = rowCacheSavePeriodInSeconds; @@ -143,12 +144,15 @@ public void setMemtableFlushAfterMins(int memtableFlushAfterMins) { public void addColumnDefinition( ColumnDefinition columnDefinition){ this.columnDefinitions.add( columnDefinition ); - } + } public void setKeyCacheSavePeriodInSeconds(int keyCacheSavePeriodInSeconds) { this.keyCacheSavePeriodInSeconds = keyCacheSavePeriodInSeconds; } - + + public void setKeyValidationClass(String keyValidationClass){ + this.keyValidationClass = keyValidationClass; + } /** * SHOULD THIS BE HERE? A COLUMN DEFINITION IS PART OF A KEYSPACE BY VIRTUE @@ -253,5 +257,9 @@ public int getKeyCacheSavePeriodInSeconds() { return keyCacheSavePeriodInSeconds; } + @Override + public String getKeyValidationClass() { + return keyValidationClass; + } } diff --git a/core/src/main/java/me/prettyprint/cassandra/service/ThriftCfDef.java b/core/src/main/java/me/prettyprint/cassandra/service/ThriftCfDef.java index 25963c649..eb6542e90 100644 --- a/core/src/main/java/me/prettyprint/cassandra/service/ThriftCfDef.java +++ b/core/src/main/java/me/prettyprint/cassandra/service/ThriftCfDef.java @@ -29,6 +29,7 @@ public class ThriftCfDef implements ColumnFamilyDefinition { private double readRepairChance; private List columnMetadata; private int gcGraceSeconds; + private String keyValidationClass; private String defaultValidationClass; private int id; private int maxCompactionThreshold; @@ -50,24 +51,25 @@ public ThriftCfDef(CfDef d) { rowCacheSavePeriodInSeconds = d.row_cache_save_period_in_seconds; keyCacheSize = d.key_cache_size; keyCacheSavePeriodInSeconds = d.key_cache_save_period_in_seconds; + keyValidationClass = d.key_validation_class; readRepairChance = d.read_repair_chance; columnMetadata = ThriftColumnDef.fromThriftList(d.column_metadata); gcGraceSeconds = d.gc_grace_seconds; defaultValidationClass = d.default_validation_class; id = d.id; - minCompactionThreshold = d.min_compaction_threshold == 0 ? + minCompactionThreshold = d.min_compaction_threshold == 0 ? CFMetaData.DEFAULT_MIN_COMPACTION_THRESHOLD : d.min_compaction_threshold; maxCompactionThreshold = d.max_compaction_threshold == 0 ? CFMetaData.DEFAULT_MAX_COMPACTION_THRESHOLD : d.max_compaction_threshold; - memtableOperationsInMillions = d.memtable_operations_in_millions == 0 ? + memtableOperationsInMillions = d.memtable_operations_in_millions == 0 ? CFMetaData.DEFAULT_MEMTABLE_OPERATIONS_IN_MILLIONS : d.memtable_operations_in_millions; - memtableFlushAfterMins = d.memtable_flush_after_mins == 0 ? + memtableFlushAfterMins = d.memtable_flush_after_mins == 0 ? CFMetaData.DEFAULT_MEMTABLE_LIFETIME_IN_MINS : d.memtable_flush_after_mins; - memtableThroughputInMb = d.memtable_throughput_in_mb == 0 ? + memtableThroughputInMb = d.memtable_throughput_in_mb == 0 ? CFMetaData.DEFAULT_MEMTABLE_THROUGHPUT_IN_MB : d.memtable_throughput_in_mb; } - + public ThriftCfDef(ColumnFamilyDefinition columnFamilyDefinition) { keyspace = columnFamilyDefinition.getKeyspaceName(); name = columnFamilyDefinition.getName(); @@ -79,6 +81,7 @@ public ThriftCfDef(ColumnFamilyDefinition columnFamilyDefinition) { rowCacheSavePeriodInSeconds = columnFamilyDefinition.getRowCacheSavePeriodInSeconds(); keyCacheSize = columnFamilyDefinition.getKeyCacheSize(); keyCacheSavePeriodInSeconds = columnFamilyDefinition.getKeyCacheSavePeriodInSeconds(); + keyValidationClass = columnFamilyDefinition.getKeyValidationClass(); readRepairChance = columnFamilyDefinition.getReadRepairChance(); columnMetadata = columnFamilyDefinition.getColumnMetadata(); gcGraceSeconds = columnFamilyDefinition.getGcGraceSeconds(); @@ -86,13 +89,13 @@ public ThriftCfDef(ColumnFamilyDefinition columnFamilyDefinition) { id = columnFamilyDefinition.getId(); minCompactionThreshold = columnFamilyDefinition.getMinCompactionThreshold() == 0 ? CFMetaData.DEFAULT_MIN_COMPACTION_THRESHOLD : columnFamilyDefinition.getMinCompactionThreshold(); - maxCompactionThreshold = columnFamilyDefinition.getMaxCompactionThreshold() == 0 ? + maxCompactionThreshold = columnFamilyDefinition.getMaxCompactionThreshold() == 0 ? CFMetaData.DEFAULT_MAX_COMPACTION_THRESHOLD : columnFamilyDefinition.getMaxCompactionThreshold(); - memtableFlushAfterMins = columnFamilyDefinition.getMemtableFlushAfterMins() == 0 ? - CFMetaData.DEFAULT_MEMTABLE_LIFETIME_IN_MINS : columnFamilyDefinition.getMemtableFlushAfterMins(); - memtableThroughputInMb = columnFamilyDefinition.getMemtableThroughputInMb() == 0 ? - CFMetaData.DEFAULT_MEMTABLE_THROUGHPUT_IN_MB : columnFamilyDefinition.getMemtableThroughputInMb(); - memtableOperationsInMillions = columnFamilyDefinition.getMemtableOperationsInMillions() == 0 ? + memtableFlushAfterMins = columnFamilyDefinition.getMemtableFlushAfterMins() == 0 ? + CFMetaData.DEFAULT_MEMTABLE_LIFETIME_IN_MINS : columnFamilyDefinition.getMemtableFlushAfterMins(); + memtableThroughputInMb = columnFamilyDefinition.getMemtableThroughputInMb() == 0 ? + CFMetaData.DEFAULT_MEMTABLE_THROUGHPUT_IN_MB : columnFamilyDefinition.getMemtableThroughputInMb(); + memtableOperationsInMillions = columnFamilyDefinition.getMemtableOperationsInMillions() == 0 ? CFMetaData.DEFAULT_MEMTABLE_OPERATIONS_IN_MILLIONS : columnFamilyDefinition.getMemtableOperationsInMillions(); } @@ -109,8 +112,8 @@ public ThriftCfDef(String keyspace, String columnFamilyName) { gcGraceSeconds = CFMetaData.DEFAULT_GC_GRACE_SECONDS; minCompactionThreshold = CFMetaData.DEFAULT_MIN_COMPACTION_THRESHOLD; maxCompactionThreshold = CFMetaData.DEFAULT_MAX_COMPACTION_THRESHOLD; - memtableFlushAfterMins = CFMetaData.DEFAULT_MEMTABLE_LIFETIME_IN_MINS; - memtableThroughputInMb = CFMetaData.DEFAULT_MEMTABLE_THROUGHPUT_IN_MB; + memtableFlushAfterMins = CFMetaData.DEFAULT_MEMTABLE_LIFETIME_IN_MINS; + memtableThroughputInMb = CFMetaData.DEFAULT_MEMTABLE_THROUGHPUT_IN_MB; memtableOperationsInMillions = CFMetaData.DEFAULT_MEMTABLE_OPERATIONS_IN_MILLIONS; } @@ -222,14 +225,15 @@ public CfDef toThrift() { d.setId(id); d.setKey_cache_size(keyCacheSize); d.setKey_cache_save_period_in_seconds(keyCacheSavePeriodInSeconds); + d.setKey_validation_class(keyValidationClass); d.setMax_compaction_threshold(maxCompactionThreshold); d.setMin_compaction_threshold(minCompactionThreshold); d.setRead_repair_chance(readRepairChance); d.setRow_cache_size(rowCacheSize); d.setMemtable_operations_in_millions(memtableOperationsInMillions); d.setMemtable_throughput_in_mb(memtableThroughputInMb); - d.setMemtable_flush_after_mins(memtableFlushAfterMins); - + d.setMemtable_flush_after_mins(memtableFlushAfterMins); + if (subComparatorType != null) { d.setSubcomparator_type(subComparatorType.getClassName()); } @@ -241,6 +245,11 @@ public String getDefaultValidationClass() { return defaultValidationClass; } + @Override + public String getKeyValidationClass(){ + return keyValidationClass; + } + @Override public int getId() { return id; @@ -300,6 +309,10 @@ public void setDefaultValidationClass(String defaultValidationClass) { this.defaultValidationClass = defaultValidationClass; } + public void setKeyValidationClass(String keyValidationClass){ + this.keyValidationClass = keyValidationClass; + } + public void setId(int id) { this.id = id; } diff --git a/core/src/main/java/me/prettyprint/hector/api/ddl/ColumnFamilyDefinition.java b/core/src/main/java/me/prettyprint/hector/api/ddl/ColumnFamilyDefinition.java index 8e5bae5ab..3ec5d2ed3 100644 --- a/core/src/main/java/me/prettyprint/hector/api/ddl/ColumnFamilyDefinition.java +++ b/core/src/main/java/me/prettyprint/hector/api/ddl/ColumnFamilyDefinition.java @@ -20,6 +20,7 @@ public interface ColumnFamilyDefinition { int getRowCacheSavePeriodInSeconds(); int getKeyCacheSavePeriodInSeconds(); double getKeyCacheSize(); + String getKeyValidationClass(); double getReadRepairChance(); List getColumnMetadata(); int getGcGraceSeconds(); diff --git a/pom.xml b/pom.xml index 209d3f4d5..5ef0052ad 100644 --- a/pom.xml +++ b/pom.xml @@ -88,12 +88,12 @@ org.apache.cassandra cassandra-all - 0.8.0-beta2 + 0.8.0-rc1 org.apache.cassandra cassandra-thrift - 0.8.0-beta2 + 0.8.0-rc1 org.apache.cassandra From 39ec0955c98cde8388d1b359a9a777e8bb94a132 Mon Sep 17 00:00:00 2001 From: "Gun.io Whitespace Robot" Date: Thu, 27 Oct 2011 20:55:54 -0400 Subject: [PATCH 144/144] Remove whitespace [Gun.io WhitespaceBot] --- CHANGELOG | 20 ++-- README | 4 +- .../BackgroundCassandraHostService.java | 2 +- .../connection/CassandraHostRetryService.java | 20 ++-- .../connection/ConcurrentHClientPool.java | 4 +- .../DynamicLoadBalancingPolicy.java | 10 +- .../connection/HConnectionManager.java | 52 ++++----- .../cassandra/connection/HThriftClient.java | 10 +- .../connection/HostTimeoutTracker.java | 36 +++---- .../connection/LatencyAwareHClientPool.java | 2 +- .../LeastActiveBalancingPolicy.java | 16 +-- .../connection/NodeAutoDiscoverService.java | 2 +- .../cassandra/connection/PoolMetric.java | 2 +- .../connection/RoundRobinBalancingPolicy.java | 18 ++-- .../cassandra/io/ChunkInputStream.java | 8 +- .../cassandra/io/ChunkOutputStream.java | 8 +- .../jndi/CassandraClientJndiResourcePool.java | 8 +- .../cassandra/model/AbstractBasicQuery.java | 2 +- .../cassandra/model/AbstractSliceQuery.java | 2 +- .../cassandra/model/CounterSliceImpl.java | 2 +- .../prettyprint/cassandra/model/CqlQuery.java | 40 +++---- .../prettyprint/cassandra/model/CqlRows.java | 16 +-- .../cassandra/model/ExecutingKeyspace.java | 6 +- .../cassandra/model/ExecutionResult.java | 6 +- .../cassandra/model/HCounterColumnImpl.java | 14 +-- .../model/HCounterSuperColumnImpl.java | 2 +- .../cassandra/model/HSlicePredicate.java | 42 ++++---- .../cassandra/model/HSuperColumnImpl.java | 2 +- .../cassandra/model/IndexedSlicesQuery.java | 4 +- .../cassandra/model/MutatorImpl.java | 66 ++++++------ .../cassandra/model/OrderedRowsImpl.java | 2 +- .../prettyprint/cassandra/model/RowsImpl.java | 2 +- .../thrift/AbstractThriftClientWrapper.java | 2 +- .../model/thrift/ThriftConverter.java | 4 +- .../thrift/ThriftCounterColumnQuery.java | 8 +- .../cassandra/model/thrift/ThriftFactory.java | 4 +- .../model/thrift/ThriftRangeSlicesQuery.java | 6 +- .../model/thrift/ThriftSliceCounterQuery.java | 2 +- .../serializers/AbstractSerializer.java | 8 +- .../serializers/AsciiSerializer.java | 2 +- .../serializers/BigIntegerSerializer.java | 2 +- .../serializers/BooleanSerializer.java | 4 +- .../serializers/ByteBufferSerializer.java | 8 +- .../serializers/BytesArraySerializer.java | 4 +- .../cassandra/serializers/CharSerializer.java | 2 +- .../serializers/CompositeSerializer.java | 4 +- .../serializers/DoubleSerializer.java | 6 +- .../DynamicCompositeSerializer.java | 4 +- .../serializers/FastInfosetSerializer.java | 6 +- .../serializers/FloatSerializer.java | 4 +- .../serializers/IntegerSerializer.java | 6 +- .../cassandra/serializers/JaxbSerializer.java | 8 +- .../cassandra/serializers/LongSerializer.java | 4 +- .../serializers/ObjectSerializer.java | 4 +- .../serializers/ShortSerializer.java | 2 +- .../serializers/StringSerializer.java | 4 +- .../cassandra/serializers/UUIDSerializer.java | 4 +- .../cassandra/service/AbstractCluster.java | 8 +- .../cassandra/service/BatchMutation.java | 14 +-- .../service/CassandraClientMonitor.java | 16 +-- .../service/CassandraClientMonitorMBean.java | 18 ++-- .../cassandra/service/CassandraHost.java | 2 +- .../service/CassandraHostConfigurator.java | 6 +- .../service/ExceptionsTranslator.java | 6 +- .../service/ExceptionsTranslatorImpl.java | 2 +- .../cassandra/service/ExhaustedPolicy.java | 4 +- .../cassandra/service/HColumnFamilyImpl.java | 76 ++++++------- .../cassandra/service/JmxMonitor.java | 2 +- .../cassandra/service/KeyIterator.java | 4 +- .../cassandra/service/KeyspaceService.java | 18 ++-- .../cassandra/service/Operation.java | 18 ++-- .../cassandra/service/OperationType.java | 2 +- .../cassandra/service/StringKeyIterator.java | 2 +- .../cassandra/service/ThriftCluster.java | 6 +- .../cassandra/service/ThriftColumnDef.java | 6 +- .../cassandra/service/ThriftKsDef.java | 4 +- .../service/VirtualKeyspaceServiceImpl.java | 2 +- .../service/spring/HectorTemplate.java | 2 +- .../service/spring/HectorTemplateImpl.java | 2 +- .../AbstractColumnFamilyTemplate.java | 22 ++-- .../template/AbstractResultWrapper.java | 18 ++-- .../template/AbstractTemplateUpdater.java | 14 +-- .../template/CassandraClusterFactory.java | 10 +- .../service/template/ColumnFamilyResult.java | 12 +-- .../template/ColumnFamilyResultWrapper.java | 46 ++++---- .../template/ColumnFamilyRowMapper.java | 2 +- .../template/ColumnFamilyTemplate.java | 40 +++---- .../service/template/ColumnFamilyUpdater.java | 22 ++-- .../template/MappedColumnFamilyResult.java | 2 +- .../MappedColumnFamilyResultWrapper.java | 6 +- .../service/template/MappedSuperCfResult.java | 2 +- .../template/MappedSuperCfResultWrapper.java | 8 +- .../service/template/SuperCfResult.java | 16 +-- .../template/SuperCfResultWrapper.java | 42 ++++---- .../service/template/SuperCfRowMapper.java | 2 +- .../service/template/SuperCfTemplate.java | 62 +++++------ .../service/template/SuperCfUpdater.java | 58 +++++----- .../template/ThriftColumnFamilyTemplate.java | 44 ++++---- .../template/ThriftSuperCfTemplate.java | 38 +++---- .../testutils/EmbeddedSchemaLoader.java | 6 +- .../testutils/EmbeddedServerHelper.java | 40 +++---- .../utils/ByteBufferOutputStream.java | 2 +- .../me/prettyprint/hector/api/Cluster.java | 6 +- .../prettyprint/hector/api/ColumnFactory.java | 4 +- .../prettyprint/hector/api/HColumnFamily.java | 44 ++++---- .../me/prettyprint/hector/api/Keyspace.java | 4 +- .../prettyprint/hector/api/ResultStatus.java | 4 +- .../me/prettyprint/hector/api/Serializer.java | 10 +- .../hector/api/beans/AbstractComposite.java | 2 +- .../prettyprint/hector/api/beans/HColumn.java | 8 +- .../hector/api/beans/HCounterColumn.java | 8 +- .../hector/api/beans/HCounterSuperColumn.java | 2 +- .../hector/api/beans/HSuperColumn.java | 2 +- .../prettyprint/hector/api/ddl/package.html | 10 +- .../HCassandraInternalException.java | 20 ++-- .../api/exceptions/HUnavailableException.java | 2 +- .../api/exceptions/HectorException.java | 4 +- .../hector/api/factory/HFactory.java | 56 +++++----- .../hector/api/mutation/MutationResult.java | 2 +- .../hector/api/mutation/Mutator.java | 50 ++++----- .../hector/api/query/MultigetSliceQuery.java | 2 +- .../hector/api/query/QueryResult.java | 2 +- .../hector/api/query/RangeSlicesQuery.java | 2 +- .../hector/api/query/SliceCounterQuery.java | 2 +- .../connection/BaseBalancingPolicyTest.java | 16 +-- .../connection/ConcurrentHClientPoolTest.java | 10 +- .../connection/HConnectionManagerTest.java | 32 +++--- .../connection/HThriftClientTest.java | 14 +-- .../connection/HostTimeoutTrackerTest.java | 8 +- .../LeastActiveBalancingPolicyTest.java | 20 ++-- .../RoundRobinBalancingPolicyTest.java | 20 ++-- .../cassandra/dao/SimpleCassandraDaoTest.java | 4 +- ...assandraClientJndiResourceFactoryTest.java | 2 +- .../cassandra/model/CqlQueryTest.java | 12 +-- .../cassandra/model/HColumnFamilyTest.java | 14 +-- .../model/IndexedSlicesQueryTest.java | 18 ++-- .../cassandra/model/MutatorTest.java | 30 +++--- .../cassandra/model/RangeSlicesQueryTest.java | 6 +- .../serializers/BigIntegerSerializerTest.java | 4 +- .../FastInfosetSerializerTest.java | 2 +- .../serializers/IntegerSerializerTest.java | 14 +-- .../serializers/JaxbSerializerTest.java | 4 +- .../serializers/LongSerializerTest.java | 6 +- .../serializers/SerializerBaseTest.java | 4 +- .../serializers/ShortSerializerTest.java | 4 +- .../cassandra/service/CassandraAuthTest.java | 8 +- .../service/CassandraClusterTest.java | 56 +++++----- .../BaseColumnFamilyTemplateTest.java | 2 +- .../template/ColumnFamilyTemplateTest.java | 32 +++--- .../service/template/SuperCfTemplateTest.java | 102 +++++++++--------- .../hector/api/ApiV2SystemTest.java | 40 +++---- .../java/me/prettyprint/hom/CFMappingDef.java | 10 +- .../me/prettyprint/hom/ClassCacheMgr.java | 16 +-- .../hom/EntityManagerConfigurator.java | 28 ++--- .../prettyprint/hom/HectorObjectMapper.java | 18 ++-- ...KeyConcatenationDelimiterStrategyImpl.java | 10 +- .../me/prettyprint/hom/KeyDefinition.java | 2 +- .../hom/annotations/AnnotationScanner.java | 2 +- .../AnonymousPropertyAddHandler.java | 2 +- .../AnonymousPropertyCollectionGetter.java | 2 +- .../prettyprint/hom/annotations/Column.java | 6 +- .../me/prettyprint/hom/annotations/Id.java | 4 +- .../prettyprint/hom/cache/ColumnParser.java | 2 +- .../hom/cache/ColumnParserValidator.java | 2 +- .../hom/cache/IdClassParserValidator.java | 8 +- .../hom/cache/InheritanceParserValidator.java | 2 +- .../hom/cache/ParserValidator.java | 2 +- .../hom/cache/TableParserValidator.java | 2 +- .../prettyprint/hom/converters/Converter.java | 2 +- .../hom/converters/DefaultConverter.java | 2 +- .../src/test/java/com/mycompany/MySerial.java | 4 +- .../me/prettyprint/hom/CassandraTestBase.java | 2 +- .../me/prettyprint/hom/ClassCacheMgrTest.java | 4 +- .../me/prettyprint/hom/EntityManagerTest.java | 8 +- .../hom/HectorObjectMapperTest.java | 6 +- .../MyComplexEntityMissingIdField.java | 4 +- .../hom/badbeans/MyCompositePK.java | 4 +- .../prettyprint/hom/beans/ColorEmbedded.java | 4 +- .../hom/beans/MyComplexEntity.java | 6 +- .../prettyprint/hom/beans/MyCompositePK.java | 4 +- .../hom/beans/SimpleRelationshipBean.java | 6 +- .../prettyprint/hom/beans/SimpleTestBean.java | 20 ++-- .../hom/cache/IdClassParserValidatorTest.java | 6 +- .../prettyprint/hom/dupebean/MyDupeCF1.java | 2 +- .../prettyprint/hom/dupebean/MyDupeCF2.java | 2 +- 185 files changed, 1105 insertions(+), 1105 deletions(-) diff --git a/CHANGELOG b/CHANGELOG index 5443c55c3..49c4d5d18 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -7,20 +7,20 @@ Versions above 0.7.0-17 work well with cassandra's 0.7.* (after 0.7.0-beta2) 0.7.0-27 ======== -Execution time is now actually in micro seconds. Thanks to several folks for pointing this out. +Execution time is now actually in micro seconds. Thanks to several folks for pointing this out. Added IO stream mutators. Thanks to Norman Maurer for the pull request. Added method to HFactory for safe shutdown of cluster Added suspend/unsuspend operations to HConnectionManager, operations for suh in JMX Cleanup of numActive counter on ConcurrentHClientPool. Could cause negative results under some high-load situations. Thanks to Yang Yang for the tip. fix NPE with poolExhaustedException. Thanks to Yang Yang for the tip -Fixed issue with ttl not being set on result columns. Thanks to ML user Kallin for the heads up. +Fixed issue with ttl not being set on result columns. Thanks to ML user Kallin for the heads up. 0.7.0-26 ======== Fix issue with NPE in ThriftColumnDef when a column name was declared with no index type (thanks to Steve Willcox for the heads up) Mutator has easier row level deletion methods Fix potential race condition in ConcurrentHClientPool with large number of threads. https://github.com/rantav/hector/issues#issue/136 (Thanks to @masseyke for the test case) -LeastActive policy updated to shuffle before selection. This spreads load out better among pools in low to moderate traffic environments (ie. when ConcurrentHClientPool#getNumActive are all equal). +LeastActive policy updated to shuffle before selection. This spreads load out better among pools in low to moderate traffic environments (ie. when ConcurrentHClientPool#getNumActive are all equal). Fix NPE in checking that cassandraHostRetryService is turned when host is being marked down (thanks to ML poster Shimi for bug report) A CassandraClientMonitor instance is now created per cluster to make JMX integration with multi-cluster setups much easier. (https://github.com/rantav/hector/issues/issue/137) @@ -33,7 +33,7 @@ Fix issue with RR Load balancing policy that cause ArrayOutOfBounds.. in some si 0.7.0-24 ======== Fixed issue with null keyspaces and credentials in HConnectionManager (via Michael Moores) -Added class-level default for failoverPolicy +Added class-level default for failoverPolicy Rudimentary (very much so) JPA 2.0 support for save and load via openjpa Ligthweight ORM for JPA 1.0 annotations (additional improvements to BTodd's hector-object-mapper merged in) Some cleanup of bytebuffer handling in serializers @@ -47,9 +47,9 @@ CassandraHostRetryService default queue size now defaults unbounded Initial import of BToddB's hector-object-mapper as object-mapper module Object mapper ported over to JPA annotations and EntityManager (very redimentary support) Fix issue with createKeyspace requiring additional column info -Massive changes to pom structure courtesy of Stephen Connolly to facilitate inclusion into maven central repository. +Massive changes to pom structure courtesy of Stephen Connolly to facilitate inclusion into maven central repository. Small tweaks to HConnectionManager courtesy of Benoit Perroud -Collection conversions in AbstractSerializer use the size of the provided collection for initialization. Patch courtesy of Benoit Perroud. +Collection conversions in AbstractSerializer use the size of the provided collection for initialization. Patch courtesy of Benoit Perroud. Treat HUnavailableException the same as HTimeoutException 0.7.0-22 @@ -57,7 +57,7 @@ Treat HUnavailableException the same as HTimeoutException Fix issue with LeastActiveBalancingPolicy that favoured same host repeatedly Define ClockResolution as an Interface to allow client to define their own implementations Added TimeUUID support (me.prettyprint.cassandra.utils.TimeUUIDUtils) -Added updateColumnFamily to Cluster +Added updateColumnFamily to Cluster Shorterm fix for catching InvalidRequestException and handling it correctly for a bootstraping node @@ -89,7 +89,7 @@ Refactoring of the connection pooling innards to fix epic race condition on fail - Removal of classes effectively matching the pattern CassandraClient* from the service package - Command is gone. It was no longer needed and was more confusing than anything else - Pooling logic has been stress tested by a neutral third party (no, really!) -Upgrade to thrift 0.5 to match Cassandra (it caught us by surprise as well). +Upgrade to thrift 0.5 to match Cassandra (it caught us by surprise as well). The system_rename_* methods were removed matching removal of such in Thrift API Microsecond level granularity is now the default NodeAutoDiscoverService will periodically look for new hosts on the ring and add them (off by default) @@ -103,13 +103,13 @@ Move all the API stuff to me.prettyprint.hector.api.*. Extract interfaces and pr - Rename KeyspaceOperator and extract a Keyspace interface from it Rename a few exceptions to begin with HSomething so they are hard to unintentionally mix with their thrift doubles. Bug fixes: - CassandraClientPoolByHostImpl can throw NoSuchElementException + CassandraClientPoolByHostImpl can throw NoSuchElementException KeyspaceImpl.toString returns super.toString() API V2 has no means of getting all columns from a row. KeyspaceOperator throws NPE that masks real exceptions Timestamp (and Clock in 0.7.0) are not set on HColumn in the constructor Friendlier API and spring integration - Error in failover - incorrect operation when borrowClient throws an + Error in failover - incorrect operation when borrowClient throws an batchMutate doesn't work with null predicate in deletion. Cannot batch-delete rows. getSuperColumn() does not return null KeyspaceImpl.getSuperColumn() should use cassandra.get() and not cassandra.get_slice() diff --git a/README b/README index cadaeb644..4b70f50de 100644 --- a/README +++ b/README @@ -1,6 +1,6 @@ The current 0.7.0 branch will work with Apache Cassandra 0.7.x. The master has been switched to tracking Apache Cassandra 0.8.0. Current releases on the downloads section should be cosidered production ready. You should always choose the most recent release for your version of Apache Cassandra. -As of version 0.7.0-23, Hector artificats are deployed to Maven Central. If you use maven for your build system, you need only include the hector-core dependency and all related dependencies will be managed automatically. +As of version 0.7.0-23, Hector artificats are deployed to Maven Central. If you use maven for your build system, you need only include the hector-core dependency and all related dependencies will be managed automatically. ---------------------------------------------------------------------------------------------------- Hector is a high level Java client for Apache Cassandra. @@ -25,7 +25,7 @@ Some features provided by this client: o simple ORM layer that works o a type-safe approach to dealing with Apache Cassandra's data model -Detailed documentation of Hector features and usage can be found on the wiki: +Detailed documentation of Hector features and usage can be found on the wiki: https://github.com/rantav/hector/wiki/User-Guide Some additional pages from the wiki that may be of interest: diff --git a/core/src/main/java/me/prettyprint/cassandra/connection/BackgroundCassandraHostService.java b/core/src/main/java/me/prettyprint/cassandra/connection/BackgroundCassandraHostService.java index 5679c8bd1..817b7e576 100644 --- a/core/src/main/java/me/prettyprint/cassandra/connection/BackgroundCassandraHostService.java +++ b/core/src/main/java/me/prettyprint/cassandra/connection/BackgroundCassandraHostService.java @@ -20,7 +20,7 @@ public BackgroundCassandraHostService(HConnectionManager connectionManager, CassandraHostConfigurator cassandraHostConfigurator) { this.connectionManager = connectionManager; this.cassandraHostConfigurator = cassandraHostConfigurator; - + } abstract void shutdown(); diff --git a/core/src/main/java/me/prettyprint/cassandra/connection/CassandraHostRetryService.java b/core/src/main/java/me/prettyprint/cassandra/connection/CassandraHostRetryService.java index 5112b4a8e..3313d8868 100644 --- a/core/src/main/java/me/prettyprint/cassandra/connection/CassandraHostRetryService.java +++ b/core/src/main/java/me/prettyprint/cassandra/connection/CassandraHostRetryService.java @@ -31,9 +31,9 @@ public CassandraHostRetryService(HConnectionManager connectionManager, super(connectionManager, cassandraHostConfigurator); this.exceptionsTranslator = connectionManager.exceptionsTranslator; this.retryDelayInSeconds = cassandraHostConfigurator.getRetryDownedHostsDelayInSeconds(); - downedHostQueue = new LinkedBlockingQueue(cassandraHostConfigurator.getRetryDownedHostsQueueSize() < 1 + downedHostQueue = new LinkedBlockingQueue(cassandraHostConfigurator.getRetryDownedHostsQueueSize() < 1 ? Integer.MAX_VALUE : cassandraHostConfigurator.getRetryDownedHostsQueueSize()); - + sf = executor.scheduleWithFixedDelay(new RetryRunner(), this.retryDelayInSeconds,this.retryDelayInSeconds, TimeUnit.SECONDS); log.info("Downed Host Retry service started with queue size {} and retry delay {}s", @@ -87,12 +87,12 @@ class RetryRunner implements Runnable { public void run() { CassandraHost cassandraHost = downedHostQueue.poll(); if ( cassandraHost == null ) { - if ( log.isDebugEnabled() ) { + if ( log.isDebugEnabled() ) { log.debug("Retry service fired... nothing to do."); } return; } - + boolean reconnected = verifyConnection(cassandraHost); log.info("Downed Host retry status {} with host: {}", reconnected, cassandraHost.getName()); if ( reconnected ) { @@ -111,17 +111,17 @@ private boolean verifyConnection(CassandraHost cassandraHost) { boolean found = false; HThriftClient client = new HThriftClient(cassandraHost); try { - + client.open(); found = client.getCassandra().describe_cluster_name() != null; - client.close(); - } catch (HectorTransportException he) { + client.close(); + } catch (HectorTransportException he) { log.warn("Downed {} host still appears to be down: {}", cassandraHost, he.getMessage()); } catch (Exception ex) { - + log.error("Downed Host retry failed attempt to verify CassandraHost", ex); - - } + + } return found; } diff --git a/core/src/main/java/me/prettyprint/cassandra/connection/ConcurrentHClientPool.java b/core/src/main/java/me/prettyprint/cassandra/connection/ConcurrentHClientPool.java index b17a76f92..49f4ccf8e 100644 --- a/core/src/main/java/me/prettyprint/cassandra/connection/ConcurrentHClientPool.java +++ b/core/src/main/java/me/prettyprint/cassandra/connection/ConcurrentHClientPool.java @@ -66,7 +66,7 @@ public HThriftClient borrowClient() throws HectorException { if ( tillExhausted > 0 ) { // if we start with #of threads == getMaxActive, we could trigger this condition // replace addClientToPoolGently(new HThriftClient(cassandraHost).open()) with immediate acquisition - return greedyCreate(); + return greedyCreate(); } // blocked take on the queue if we are configured to wait forever if ( log.isDebugEnabled() ) { @@ -109,7 +109,7 @@ public HThriftClient borrowClient() throws HectorException { return cassandraClient; } - + /** * Used when we still have room to grow. Return an HThriftClient without * having to wait on polling logic. (But still increment all the counters) diff --git a/core/src/main/java/me/prettyprint/cassandra/connection/DynamicLoadBalancingPolicy.java b/core/src/main/java/me/prettyprint/cassandra/connection/DynamicLoadBalancingPolicy.java index a0c8f5f9b..21f49408d 100644 --- a/core/src/main/java/me/prettyprint/cassandra/connection/DynamicLoadBalancingPolicy.java +++ b/core/src/main/java/me/prettyprint/cassandra/connection/DynamicLoadBalancingPolicy.java @@ -22,7 +22,7 @@ /** * This LB Algorithm has the Phi algo which Dynamic snitch uses, LB is based on the probablity of failure of the node. * TODO: Make cassandra code abstracted enough so we can inherit from the same. - * + * * @author Vijay Parthasarathy */ public class DynamicLoadBalancingPolicy implements LoadBalancingPolicy { @@ -134,7 +134,7 @@ public int getUpdateInterval() { /** * Set the configured interval for the stats to be recalculated (until this time it is been cached. - * + * * @param updateInterval * In ms. */ @@ -149,7 +149,7 @@ public int getResetInterval() { /** * Set the configured interval for the stats to be reset so that the new stats are allowed and we can get rid of bad * nodes value. This is under the assumption that the bad nodes will eventually get better.... - * + * * @param resetInterval * in ms */ @@ -163,9 +163,9 @@ public double getBadnessThreshold() { /** * This is the percentage of badness which is acceptable... - * + * * Example: A should be 0.20 (20%) bad than B before B is choosen rathar than A. - * + * * @param badness * in % */ diff --git a/core/src/main/java/me/prettyprint/cassandra/connection/HConnectionManager.java b/core/src/main/java/me/prettyprint/cassandra/connection/HConnectionManager.java index 43a1735fb..28003a6fc 100644 --- a/core/src/main/java/me/prettyprint/cassandra/connection/HConnectionManager.java +++ b/core/src/main/java/me/prettyprint/cassandra/connection/HConnectionManager.java @@ -23,7 +23,7 @@ public class HConnectionManager { private StopWatchFactory stopWatchFactory; private final NonBlockingHashMap hostPools; - private final NonBlockingHashMap suspendedHostPools; + private final NonBlockingHashMap suspendedHostPools; private final Collection hostPoolValues; private final String clusterName; private CassandraHostRetryService cassandraHostRetryService; @@ -45,7 +45,7 @@ public HConnectionManager(String clusterName, CassandraHostConfigurator cassandr this.clusterName = clusterName; if ( cassandraHostConfigurator.getRetryDownedHosts() ) { cassandraHostRetryService = new CassandraHostRetryService(this, cassandraHostConfigurator); - } + } for ( CassandraHost host : cassandraHostConfigurator.buildCassandraHosts()) { try { HClientPool hcp = loadBalancingPolicy.createConnection(host); @@ -57,7 +57,7 @@ public HConnectionManager(String clusterName, CassandraHostConfigurator cassandr } } } - + if ( cassandraHostConfigurator.getUseHostTimeoutTracker() ) { hostTimeoutTracker = new HostTimeoutTracker(this, cassandraHostConfigurator); } @@ -71,7 +71,7 @@ public HConnectionManager(String clusterName, CassandraHostConfigurator cassandr nodeAutoDiscoverService.doAddNodes(); } } - + // // This sets up the Speed4J logging system. Alternatively, we could // use the speed4j.properties -file. This was chosen just so that @@ -82,12 +82,12 @@ public HConnectionManager(String clusterName, CassandraHostConfigurator cassandr slog.setName("hector-"+clusterName); slog.setPeriod(60); // 60 seconds slog.setSlf4jLogname( "me.prettyprint.cassandra.hector.TimingLogger" ); - + stopWatchFactory = StopWatchFactory.getInstance( slog ); } /** - * Returns true if the host was successfully added. In any sort of failure exceptions are + * Returns true if the host was successfully added. In any sort of failure exceptions are * caught and logged, returning false. * @param cassandraHost * @return @@ -138,9 +138,9 @@ public boolean removeCassandraHost(CassandraHost cassandraHost) { log.info("Remove status for CassandraHost pool {} was {}", cassandraHost, removed); return removed; } - + /** - * Remove the {@link HClientPool} referenced by the {@link CassandraHost} from + * Remove the {@link HClientPool} referenced by the {@link CassandraHost} from * the active host pools. This does not shut down the pool, only removes it as a candidate from * future operations. * @param cassandraHost @@ -149,23 +149,23 @@ public boolean removeCassandraHost(CassandraHost cassandraHost) { public boolean suspendCassandraHost(CassandraHost cassandraHost) { HClientPool pool = hostPools.remove(cassandraHost); boolean removed = pool != null; - if ( removed ) { + if ( removed ) { suspendedHostPools.put(cassandraHost, pool); } log.info("Suspend operation status was {} for CassandraHost {}", removed, cassandraHost); return removed; } - /** + /** * The opposite of suspendCassandraHost, places the pool back into selection * @param cassandraHost - * @return true if this operation was successful. A no-op returning false + * @return true if this operation was successful. A no-op returning false * if there was no such host in the underlying suspendedHostPool map. */ public boolean unsuspendCassandraHost(CassandraHost cassandraHost) { HClientPool pool = suspendedHostPools.remove(cassandraHost); boolean readded = pool != null; - if ( readded ) { + if ( readded ) { boolean alreadyThere = hostPools.putIfAbsent(cassandraHost, pool) != null; if ( alreadyThere ) { log.error("Unsuspend called on a pool that was already active for CassandraHost {}", cassandraHost); @@ -174,7 +174,7 @@ public boolean unsuspendCassandraHost(CassandraHost cassandraHost) { log.info("UN-Suspend operation status was {} for CassandraHost {}", readded, cassandraHost); return readded; } - + /** * Returns a Set of {@link CassandraHost} which are in the suspended status * @return @@ -182,7 +182,7 @@ public boolean unsuspendCassandraHost(CassandraHost cassandraHost) { public Set getSuspendedCassandraHosts() { return suspendedHostPools.keySet(); } - + public Set getHosts() { return Collections.unmodifiableSet(hostPools.keySet()); } @@ -228,18 +228,18 @@ public void operateWithFailover(Operation op) throws HectorException { throw he; } else if ( he instanceof HectorTransportException) { // client can be null in this situation - if ( client != null ) { + if ( client != null ) { client.close(); } markHostAsDown(pool.getCassandraHost()); excludeHosts.add(pool.getCassandraHost()); retryable = true; - + monitor.incCounter(Counter.RECOVERABLE_TRANSPORT_EXCEPTIONS); - + } else if (he instanceof HTimedOutException ) { // DO NOT drecrement retries, we will be keep retrying on timeouts until it comes back - // if HLT.checkTimeout(cassandraHost): suspendHost(cassandraHost); + // if HLT.checkTimeout(cassandraHost): suspendHost(cassandraHost); doTimeoutCheck(pool.getCassandraHost()); retryable = true; @@ -258,11 +258,11 @@ public void operateWithFailover(Operation op) throws HectorException { } else { // something strange happened. Added here as suggested by sbridges. // I think this gives a sane way to future-proof against any API additions - // that we don't add in time. + // that we don't add in time. retryable = false; } if ( retries <= 0 || retryable == false) throw he; - + log.warn("Could not fullfill request on this host {}", client); log.warn("Exception: ", he); monitor.incCounter(Counter.SKIP_HOST_SUCCESS); @@ -277,7 +277,7 @@ public void operateWithFailover(Operation op) throws HectorException { } } } - + /** * Use the HostTimeoutCheck and initiate a suspend if and only if * we are configured for such AND there is more than one operating host pool @@ -312,8 +312,8 @@ private void sleepBetweenHostSkips(FailoverPolicy failoverPolicy) { private HClientPool getClientFromLBPolicy(Set excludeHosts) { if ( hostPools.isEmpty() ) { throw new HectorException("All host pools marked down. Retry burden pushed out to client."); - } - return loadBalancingPolicy.getPool(hostPoolValues, excludeHosts); + } + return loadBalancingPolicy.getPool(hostPoolValues, excludeHosts); } void releaseClient(HThriftClient client) { @@ -341,7 +341,7 @@ void markHostAsDown(CassandraHost cassandraHost) { if ( pool != null ) { log.error("Pool state on shutdown: {}", pool.getStatusAsString()); pool.shutdown(); - if ( cassandraHostRetryService != null ) + if ( cassandraHostRetryService != null ) cassandraHostRetryService.add(cassandraHost); } } @@ -357,7 +357,7 @@ public Collection getActivePools() { public long createClock() { return this.clock.createClock(); } - + public String getClusterName() { return clusterName; } @@ -368,7 +368,7 @@ public void shutdown() { cassandraHostRetryService.shutdown(); if ( nodeAutoDiscoverService != null ) nodeAutoDiscoverService.shutdown(); - if ( hostTimeoutTracker != null ) + if ( hostTimeoutTracker != null ) hostTimeoutTracker.shutdown(); for (HClientPool pool : hostPools.values()) { diff --git a/core/src/main/java/me/prettyprint/cassandra/connection/HThriftClient.java b/core/src/main/java/me/prettyprint/cassandra/connection/HThriftClient.java index 290ee6cca..f7c4f4104 100644 --- a/core/src/main/java/me/prettyprint/cassandra/connection/HThriftClient.java +++ b/core/src/main/java/me/prettyprint/cassandra/connection/HThriftClient.java @@ -59,18 +59,18 @@ public Cassandra.Client getCassandra() { } public Cassandra.Client getCassandra(String keyspaceNameArg) { - getCassandra(); + getCassandra(); if ( keyspaceNameArg != null && !StringUtils.equals(keyspaceName, keyspaceNameArg)) { if ( log.isDebugEnabled() ) log.debug("keyspace reseting from {} to {}", keyspaceName, keyspaceNameArg); keyspaceName = keyspaceNameArg; try { - cassandraClient.set_keyspace(keyspaceName); + cassandraClient.set_keyspace(keyspaceName); } catch (InvalidRequestException ire) { throw new HInvalidRequestException(ire); } catch (TException e) { throw new HectorTransportException(e); - } + } } return cassandraClient; @@ -118,7 +118,7 @@ HThriftClient open() { } else { transport = socket; } - + try { transport.open(); } catch (TTransportException e) { @@ -172,7 +172,7 @@ private int getTimeout(CassandraHost cassandraHost) { public void startToUse() { useageStartTime = System.currentTimeMillis(); } - + /** * @return Time in MS since it was used. */ diff --git a/core/src/main/java/me/prettyprint/cassandra/connection/HostTimeoutTracker.java b/core/src/main/java/me/prettyprint/cassandra/connection/HostTimeoutTracker.java index 7bdf0fa0e..7ec78b380 100644 --- a/core/src/main/java/me/prettyprint/cassandra/connection/HostTimeoutTracker.java +++ b/core/src/main/java/me/prettyprint/cassandra/connection/HostTimeoutTracker.java @@ -13,14 +13,14 @@ import me.prettyprint.cassandra.service.CassandraHostConfigurator; /** - * Keep track of how often a node replies with a HTimeoutException. If we go + * Keep track of how often a node replies with a HTimeoutException. If we go * past the threshold of [timeoutCounter] timeouts within [timeWindow] milliseconds, * then we mark the node as suspended. (10 timeouts within 500ms by default) - * - * Periodically check the suspended nodes list every retryDelayInSeconds. If + * + * Periodically check the suspended nodes list every retryDelayInSeconds. If * the node has been suspended longer than nodeSuspensionDurationInSeconds, - * then we unsuspend, placing it back in the available pool. (10 second - * suspension retried every 10 seconds by default). + * then we unsuspend, placing it back in the available pool. (10 second + * suspension retried every 10 seconds by default). * * @author zznate */ @@ -32,13 +32,13 @@ public class HostTimeoutTracker extends BackgroundCassandraHostService { private int timeoutCounter; private int timeoutWindow; private int nodeSuspensionDurationInSeconds; - + public static final int DEF_TIMEOUT_COUNTER = 10; public static final int DEF_TIMEOUT_WINDOW = 500; public static final int DEF_NODE_SUSPENSION_DURATION_IN_SECONDS = 10; public static final int DEF_NODE_UNSUSPEND_CHECK_DELAY_IN_SECONDS = 10; - - + + public HostTimeoutTracker(HConnectionManager connectionManager, CassandraHostConfigurator cassandraHostConfigurator) { super(connectionManager, cassandraHostConfigurator); @@ -58,16 +58,16 @@ public boolean checkTimeout(CassandraHost cassandraHost) { boolean timeout = false; // if there are 3 timeouts within 500ms, return false if ( timeouts.get(cassandraHost).size() > timeoutCounter) { - Long last = timeouts.get(cassandraHost).remove(); - if (last.longValue() < (currentTimeMillis - timeoutWindow)) { + Long last = timeouts.get(cassandraHost).remove(); + if (last.longValue() < (currentTimeMillis - timeoutWindow)) { timeout = true; connectionManager.suspendCassandraHost(cassandraHost); - suspended.putIfAbsent(cassandraHost, currentTimeMillis); - } + suspended.putIfAbsent(cassandraHost, currentTimeMillis); + } } return timeout; } - + class Unsuspender implements Runnable { @Override @@ -76,13 +76,13 @@ public void run() { Entry vals = iterator.next(); if ( vals.getValue() < (System.currentTimeMillis() - (nodeSuspensionDurationInSeconds * 1000)) ) { connectionManager.unsuspendCassandraHost(vals.getKey()); - iterator.remove(); + iterator.remove(); } - } + } } - + } - + @Override void applyRetryDelay() { @@ -94,7 +94,7 @@ void shutdown() { log.info("Shutting down HostTimeoutTracker"); if ( sf != null ) sf.cancel(true); - if ( executor != null ) + if ( executor != null ) executor.shutdownNow(); log.info("HostTimeTracker shutdown complete."); } diff --git a/core/src/main/java/me/prettyprint/cassandra/connection/LatencyAwareHClientPool.java b/core/src/main/java/me/prettyprint/cassandra/connection/LatencyAwareHClientPool.java index 2561e8c4d..5912c54cc 100644 --- a/core/src/main/java/me/prettyprint/cassandra/connection/LatencyAwareHClientPool.java +++ b/core/src/main/java/me/prettyprint/cassandra/connection/LatencyAwareHClientPool.java @@ -9,7 +9,7 @@ /** * This class provides a queue function of latencies over CHCP, collecting all the latency information and calculates * the score (expensive operation). - * + * * @author Vijay Parthasarathy */ public class LatencyAwareHClientPool extends ConcurrentHClientPool { diff --git a/core/src/main/java/me/prettyprint/cassandra/connection/LeastActiveBalancingPolicy.java b/core/src/main/java/me/prettyprint/cassandra/connection/LeastActiveBalancingPolicy.java index 9f1cfac96..ffc8e7d44 100644 --- a/core/src/main/java/me/prettyprint/cassandra/connection/LeastActiveBalancingPolicy.java +++ b/core/src/main/java/me/prettyprint/cassandra/connection/LeastActiveBalancingPolicy.java @@ -15,15 +15,15 @@ * The list of hosts is shuffled on each pass to account for the case * where a number of hosts are at the minimum number of connections * (ie. they are not busy). - * - * + * + * * @author zznate */ public class LeastActiveBalancingPolicy implements LoadBalancingPolicy { - + private static final long serialVersionUID = 329849818218657061L; private static final Logger log = LoggerFactory.getLogger(LeastActiveBalancingPolicy.class); - + @Override public HClientPool getPool(Collection pools, Set excludeHosts) { List vals = Lists.newArrayList(pools); @@ -33,7 +33,7 @@ public HClientPool getPool(Collection pools, Set exc Iterator iterator = vals.iterator(); HClientPool concurrentHClientPool = iterator.next(); if ( excludeHosts != null && excludeHosts.size() > 0 ) { - while (iterator.hasNext()) { + while (iterator.hasNext()) { if ( !excludeHosts.contains(concurrentHClientPool.getCassandraHost()) ) { break; } @@ -44,16 +44,16 @@ public HClientPool getPool(Collection pools, Set exc } private final class ShufflingCompare implements Comparator { - + public int compare(HClientPool o1, HClientPool o2) { if ( log.isDebugEnabled() ) { log.debug("comparing 1: {} and count {} with 2: {} and count {}", new Object[]{o1.getCassandraHost(), o1.getNumActive(), o2.getCassandraHost(), o2.getNumActive()}); } - return o1.getNumActive() - o2.getNumActive(); + return o1.getNumActive() - o2.getNumActive(); } } - + @Override public HClientPool createConnection(CassandraHost host) { return new ConcurrentHClientPool(host); diff --git a/core/src/main/java/me/prettyprint/cassandra/connection/NodeAutoDiscoverService.java b/core/src/main/java/me/prettyprint/cassandra/connection/NodeAutoDiscoverService.java index bc2e3a3a4..399348e57 100644 --- a/core/src/main/java/me/prettyprint/cassandra/connection/NodeAutoDiscoverService.java +++ b/core/src/main/java/me/prettyprint/cassandra/connection/NodeAutoDiscoverService.java @@ -54,7 +54,7 @@ public void run() { } } - + public void doAddNodes() { if ( log.isDebugEnabled() ) { log.debug("Auto discovery service running..."); diff --git a/core/src/main/java/me/prettyprint/cassandra/connection/PoolMetric.java b/core/src/main/java/me/prettyprint/cassandra/connection/PoolMetric.java index 043772ec4..8b1ff3c2e 100644 --- a/core/src/main/java/me/prettyprint/cassandra/connection/PoolMetric.java +++ b/core/src/main/java/me/prettyprint/cassandra/connection/PoolMetric.java @@ -7,5 +7,5 @@ public interface PoolMetric { int getNumBlockedThreads(); String getName(); boolean getIsActive(); - + } diff --git a/core/src/main/java/me/prettyprint/cassandra/connection/RoundRobinBalancingPolicy.java b/core/src/main/java/me/prettyprint/cassandra/connection/RoundRobinBalancingPolicy.java index eebe84967..9a9c945b9 100644 --- a/core/src/main/java/me/prettyprint/cassandra/connection/RoundRobinBalancingPolicy.java +++ b/core/src/main/java/me/prettyprint/cassandra/connection/RoundRobinBalancingPolicy.java @@ -11,7 +11,7 @@ /** * Implements a RoundRobin balancing policy based off the contents - * of the active {@link HClientPool}. If a pool is shutdown by another + * of the active {@link HClientPool}. If a pool is shutdown by another * thread in the midst of the selection process, we return the pool * at position 0 * @@ -21,36 +21,36 @@ public class RoundRobinBalancingPolicy implements LoadBalancingPolicy { private static final long serialVersionUID = 1107204068032227079L; private AtomicInteger counter; - + public RoundRobinBalancingPolicy() { counter = new AtomicInteger(); } - + @Override public HClientPool getPool(Collection pools, Set excludeHosts) { - HClientPool pool = getPoolSafely(pools); + HClientPool pool = getPoolSafely(pools); if ( excludeHosts != null && excludeHosts.size() > 0 ) { while ( excludeHosts.contains(pool.getCassandraHost()) ) { pool = getPoolSafely(pools); if ( excludeHosts.size() >= pools.size() ) break; } - } + } return pool; } - + private HClientPool getPoolSafely(Collection pools) { try { return Iterables.get(pools, getAndIncrement(pools.size())); } catch (IndexOutOfBoundsException e) { return pools.iterator().next(); - } + } } - + private int getAndIncrement(int size) { counter.compareAndSet(16384, 0); - return counter.getAndIncrement() % size; + return counter.getAndIncrement() % size; } @Override diff --git a/core/src/main/java/me/prettyprint/cassandra/io/ChunkInputStream.java b/core/src/main/java/me/prettyprint/cassandra/io/ChunkInputStream.java index c97204358..f265d1dea 100644 --- a/core/src/main/java/me/prettyprint/cassandra/io/ChunkInputStream.java +++ b/core/src/main/java/me/prettyprint/cassandra/io/ChunkInputStream.java @@ -16,9 +16,9 @@ /** * Return an InputStream which retrieve columns from a row which stores chunk of * data. See also {@link ChunkOutputStream} - * + * * This implementation is not thread-safe! - * + * * @param */ public class ChunkInputStream extends InputStream { @@ -40,7 +40,7 @@ public ChunkInputStream(Keyspace keyspace, String cf, T key, Serializer rowKe /* * (non-Javadoc) - * + * * @see java.io.InputStream#read() */ public int read() throws IOException { @@ -54,7 +54,7 @@ public int read() throws IOException { /** * Fetch the next chunk. - * + * * @return exists if there was a chunk to fetch. * @throws IOException */ diff --git a/core/src/main/java/me/prettyprint/cassandra/io/ChunkOutputStream.java b/core/src/main/java/me/prettyprint/cassandra/io/ChunkOutputStream.java index 3d4323c5b..bf0496b1d 100644 --- a/core/src/main/java/me/prettyprint/cassandra/io/ChunkOutputStream.java +++ b/core/src/main/java/me/prettyprint/cassandra/io/ChunkOutputStream.java @@ -16,9 +16,9 @@ * will be split up by chunks of the given chunkSize. Each chunk we get written * to own column which will have the chunk number (starting at 0) as column key * (Long). - * + * * This implementation is not thread-safe! - * + * */ public class ChunkOutputStream extends OutputStream { private byte[] chunk; @@ -37,7 +37,7 @@ public ChunkOutputStream(Keyspace keyspace, String cf, T key, Serializer keyS /* * (non-Javadoc) - * + * * @see java.io.OutputStream#write(int) */ public void write(int b) throws IOException { @@ -64,7 +64,7 @@ public void flush() throws IOException { /** * Write the data to column if the configured chunk size is reached or if the * stream should be closed - * + * * @param close * @throws IOException */ diff --git a/core/src/main/java/me/prettyprint/cassandra/jndi/CassandraClientJndiResourcePool.java b/core/src/main/java/me/prettyprint/cassandra/jndi/CassandraClientJndiResourcePool.java index 36c6f2f11..e044c437d 100644 --- a/core/src/main/java/me/prettyprint/cassandra/jndi/CassandraClientJndiResourcePool.java +++ b/core/src/main/java/me/prettyprint/cassandra/jndi/CassandraClientJndiResourcePool.java @@ -9,20 +9,20 @@ * as Apache Tomcat. * * @see GenericObjectPool - * + * * @author Perry Hoekstra (dutchman_mn@charter.net) * */ public class CassandraClientJndiResourcePool extends GenericObjectPool { /** * CassandraClientJndiResourcePool constructor. - * + * * @param url url of the host that contains Cassandra. * @param port port number that Cassandra is listening on. */ - + public CassandraClientJndiResourcePool(String url, int port) { // TODO fix this - //super(new JndiCassandraClientFactory(url, port)); + //super(new JndiCassandraClientFactory(url, port)); } } diff --git a/core/src/main/java/me/prettyprint/cassandra/model/AbstractBasicQuery.java b/core/src/main/java/me/prettyprint/cassandra/model/AbstractBasicQuery.java index 8dd01c667..eff40fd53 100644 --- a/core/src/main/java/me/prettyprint/cassandra/model/AbstractBasicQuery.java +++ b/core/src/main/java/me/prettyprint/cassandra/model/AbstractBasicQuery.java @@ -6,7 +6,7 @@ import me.prettyprint.hector.api.query.Query; /** - * + * * @author patricioe (Patricio Echague - patricio@datastax.com) * * @param Key type diff --git a/core/src/main/java/me/prettyprint/cassandra/model/AbstractSliceQuery.java b/core/src/main/java/me/prettyprint/cassandra/model/AbstractSliceQuery.java index 9370982f1..c97f70d51 100644 --- a/core/src/main/java/me/prettyprint/cassandra/model/AbstractSliceQuery.java +++ b/core/src/main/java/me/prettyprint/cassandra/model/AbstractSliceQuery.java @@ -50,7 +50,7 @@ public Query setColumnNames(Collection columnNames) { } return this; } - + /** * Wraps the underlying call to {@link HSlicePredicate#setKeysOnlyPredicate()} * Use this for a substantial performance increase when you only need the keys returned diff --git a/core/src/main/java/me/prettyprint/cassandra/model/CounterSliceImpl.java b/core/src/main/java/me/prettyprint/cassandra/model/CounterSliceImpl.java index 7cea60229..6f648699c 100644 --- a/core/src/main/java/me/prettyprint/cassandra/model/CounterSliceImpl.java +++ b/core/src/main/java/me/prettyprint/cassandra/model/CounterSliceImpl.java @@ -18,7 +18,7 @@ public final class CounterSliceImpl implements CounterSlice { private final List> columnsList; public CounterSliceImpl(List tColumns, Serializer nameSerializer) { - + Assert.noneNull(tColumns, nameSerializer); columnsMap = new HashMap>(tColumns.size()); List> list = new ArrayList>(tColumns.size()); diff --git a/core/src/main/java/me/prettyprint/cassandra/model/CqlQuery.java b/core/src/main/java/me/prettyprint/cassandra/model/CqlQuery.java index 68f618992..821286ea0 100644 --- a/core/src/main/java/me/prettyprint/cassandra/model/CqlQuery.java +++ b/core/src/main/java/me/prettyprint/cassandra/model/CqlQuery.java @@ -26,14 +26,14 @@ * First cut at a CQL implementation. Not too much time has been spent here * as this API is currently a moving target. We have a lot of experience with * these hijinks by the Apache Cassandra team, so it was deemed prudent to do - * something simple initially until the dust settles. - * + * something simple initially until the dust settles. + * * You are expected to know what you are getting into if you plan on using - * CQL queries in your application. Spend some time looking through the - * unit tests here in Hector and the Cassandra source tree. For a number of + * CQL queries in your application. Spend some time looking through the + * unit tests here in Hector and the Cassandra source tree. For a number of * detailed examples, see test_cql.py in the test/system folder of the * Apache Cassandra source distribution. - * + * * Note: if you immediately get an exception such as: * "InvalidRequestException(why:cannot parse 'foo' as hex bytes)" * It means one of two things: @@ -41,30 +41,30 @@ *
  • you have not formatted your query correct
  • *
  • You have not configured the correct validators on your column family
  • * - * + * * In both cases, even though the query is most likely a string, it is up to you to format - * this query according to the comparator (used for the column name), key validator + * this query according to the comparator (used for the column name), key validator * and value validator. This can be a little confusing as only the comparator is required. * The other two default to BytesType. - * + * * See the docs on {@link CqlRows} for additional details. - * + * * @author zznate * */ public class CqlQuery extends AbstractBasicQuery> { private static Logger log = LoggerFactory.getLogger(CqlQuery.class); - + private Serializer valueSerializer; private ByteBuffer query; private boolean useCompression; - + public CqlQuery(Keyspace k, Serializer keySerializer, Serializer nameSerializer, Serializer valueSerializer) { super(k, keySerializer, nameSerializer); this.valueSerializer = valueSerializer; } - + /** * Set the query as a String. Here for convienience. See above for some * caveats. Calls {@link StringSerializer#toByteBuffer(String)} directly. @@ -75,21 +75,21 @@ public CqlQuery setQuery(String query) { this.query = StringSerializer.get().toByteBuffer(query); return this; } - + public CqlQuery setQuery(ByteBuffer qeury) { this.query = query; return this; } - + public CqlQuery useCompression() { useCompression = true; return this; } - - + + @Override public QueryResult> execute() { - + return new QueryResultImpl>( keyspace.doExecuteOperation(new Operation>(OperationType.READ) { @@ -97,7 +97,7 @@ public QueryResult> execute() { public CqlRows execute(Client cassandra) throws HectorException { CqlRows rows = null; try { - CqlResult result = cassandra.execute_cql_query(query, + CqlResult result = cassandra.execute_cql_query(query, useCompression ? Compression.GZIP : Compression.NONE); if ( log.isDebugEnabled() ) { log.debug("Found CqlResult: {}", result); @@ -113,7 +113,7 @@ public CqlRows execute(Client cassandra) throws HectorException { default: if ( result.getRowsSize() > 0 ) { LinkedHashMap> ret = new LinkedHashMap>(result.getRowsSize()); - + for (Iterator rowsIter = result.getRowsIterator(); rowsIter.hasNext(); ) { CqlRow row = rowsIter.next(); ret.put(ByteBuffer.wrap(row.getKey()), row.getColumns()); @@ -128,7 +128,7 @@ public CqlRows execute(Client cassandra) throws HectorException { } return rows; } - + }), this); } diff --git a/core/src/main/java/me/prettyprint/cassandra/model/CqlRows.java b/core/src/main/java/me/prettyprint/cassandra/model/CqlRows.java index 03c7fec5c..a3fc58a12 100644 --- a/core/src/main/java/me/prettyprint/cassandra/model/CqlRows.java +++ b/core/src/main/java/me/prettyprint/cassandra/model/CqlRows.java @@ -10,16 +10,16 @@ /** * Row wrapper specific to the multi-type results capable from a CqlQuery. - * This is a bit more convoluted than I would like, put most of this API + * This is a bit more convoluted than I would like, put most of this API * is still moving around, so we will stick with the overloading for now. - * + * * @author zznate */ public final class CqlRows extends OrderedRowsImpl { private final CqlResultType resultType; private int count; - + /** * Constructed for {@link CqlResultType#ROWS} * @param thriftRet @@ -32,7 +32,7 @@ public CqlRows(LinkedHashMap> thriftRet, super(thriftRet, nameSerializer, valueSerializer); this.resultType = CqlResultType.ROWS; } - + /** * Constructed with only a count for {@link CqlResultType#INT} * @param count @@ -42,13 +42,13 @@ public CqlRows(int count) { this.resultType = CqlResultType.INT; this.count = count; } - + /** * Constructed as empty for {@link CqlResultType#VOID} */ public CqlRows() { super(); - this.resultType = CqlResultType.VOID; + this.resultType = CqlResultType.VOID; } /** @@ -57,9 +57,9 @@ public CqlRows() { * @return */ public int getAsCount() { - if ( !resultType.equals(CqlResultType.INT)) + if ( !resultType.equals(CqlResultType.INT)) throw new IllegalArgumentException("Attempted to extract count from the wrong type of CQL query: " + resultType.toString()); return count; } - + } diff --git a/core/src/main/java/me/prettyprint/cassandra/model/ExecutingKeyspace.java b/core/src/main/java/me/prettyprint/cassandra/model/ExecutingKeyspace.java index 4707eb1ae..1d2c32938 100644 --- a/core/src/main/java/me/prettyprint/cassandra/model/ExecutingKeyspace.java +++ b/core/src/main/java/me/prettyprint/cassandra/model/ExecutingKeyspace.java @@ -17,7 +17,7 @@ /** * Thread Safe - * + * * @author Ran Tavory * @author zznate */ @@ -59,8 +59,8 @@ public ExecutingKeyspace(String keyspace, public void setConsistencyLevelPolicy(ConsistencyLevelPolicy cp) { // TODO remove this method consistencyLevelPolicy = cp; - } - + } + @Override public String getKeyspaceName() { return keyspace; diff --git a/core/src/main/java/me/prettyprint/cassandra/model/ExecutionResult.java b/core/src/main/java/me/prettyprint/cassandra/model/ExecutionResult.java index 4cba4e828..62a43519c 100644 --- a/core/src/main/java/me/prettyprint/cassandra/model/ExecutionResult.java +++ b/core/src/main/java/me/prettyprint/cassandra/model/ExecutionResult.java @@ -17,7 +17,7 @@ public class ExecutionResult implements ResultStatus { private final T value; private final long execTime; private final CassandraHost cassandraHost; - + protected static final String BASE_MSG_FORMAT = "%s took (%dus) for query (%s) on host: %s"; private static final int MICRO_DENOM = 1000; @@ -43,7 +43,7 @@ public long getExecutionTimeNano() { } /** - * Execution time is actually recorded in nanos, so we divide this by 1000 + * Execution time is actually recorded in nanos, so we divide this by 1000 * make the number more sensible * @return */ @@ -56,7 +56,7 @@ public long getExecutionTimeMicro() { public String toString() { return formatMessage("ExecutionResult", "n/a"); } - + protected String formatMessage(String resultName, String query) { return String.format(BASE_MSG_FORMAT, resultName, getExecutionTimeMicro(), query, (cassandraHost != null ? cassandraHost.getName() : "[none]")); } diff --git a/core/src/main/java/me/prettyprint/cassandra/model/HCounterColumnImpl.java b/core/src/main/java/me/prettyprint/cassandra/model/HCounterColumnImpl.java index c3e49e498..db833b5f3 100644 --- a/core/src/main/java/me/prettyprint/cassandra/model/HCounterColumnImpl.java +++ b/core/src/main/java/me/prettyprint/cassandra/model/HCounterColumnImpl.java @@ -65,7 +65,7 @@ public HCounterColumn setValue(Long value) { } /** - * Set the time-to-live value for this column in seconds. + * Set the time-to-live value for this column in seconds. * The server will mark this column as deleted once the number of seconds has elapsed. */ @Override @@ -83,14 +83,14 @@ public int getTtl() { } @Override - public N getName() { + public N getName() { return counterColumn.isSetName() ? nameSerializer.fromByteBuffer(counterColumn.name.duplicate()) : null; } @Override - public Long getValue() { + public Long getValue() { return counterColumn.value; - } + } public CounterColumn toThrift() { return counterColumn; @@ -108,7 +108,7 @@ public Serializer getNameSerializer() { } @Override - public ByteBuffer getNameBytes() { + public ByteBuffer getNameBytes() { return counterColumn.isSetName() ? counterColumn.name.duplicate() : null; } @@ -116,7 +116,7 @@ public ByteBuffer getNameBytes() { * Clear value, timestamp and ttl (the latter two set to '0') leaving only the column name */ @Override - public HCounterColumn clear() { + public HCounterColumn clear() { counterColumn.value = 0; // TODO (patricioe) Is this ok? //counterColumn.ttl = 0; TODO (patricioe) pending on trunk //counterColumn.setTtlIsSet(false); @@ -135,7 +135,7 @@ public HCounterColumn apply(CounterColumn c) { this.counterColumn = c; return this; } - + @Override public String toString() { return String.format("HCounterColumn(%s=%s)",getName(), getValue()); diff --git a/core/src/main/java/me/prettyprint/cassandra/model/HCounterSuperColumnImpl.java b/core/src/main/java/me/prettyprint/cassandra/model/HCounterSuperColumnImpl.java index f091998c8..9e8a411e6 100644 --- a/core/src/main/java/me/prettyprint/cassandra/model/HCounterSuperColumnImpl.java +++ b/core/src/main/java/me/prettyprint/cassandra/model/HCounterSuperColumnImpl.java @@ -107,7 +107,7 @@ public Serializer getNameSerializer() { public byte[] getNameBytes() { return superNameSerializer.toByteBuffer(getName()).array(); } - + public ByteBuffer getNameByteBuffer() { return superNameSerializer.toByteBuffer(getName()); } diff --git a/core/src/main/java/me/prettyprint/cassandra/model/HSlicePredicate.java b/core/src/main/java/me/prettyprint/cassandra/model/HSlicePredicate.java index 6be03341c..616e386f6 100644 --- a/core/src/main/java/me/prettyprint/cassandra/model/HSlicePredicate.java +++ b/core/src/main/java/me/prettyprint/cassandra/model/HSlicePredicate.java @@ -21,7 +21,7 @@ * @author zznate */ public final class HSlicePredicate { - + protected Collection columnNames; protected N start; protected N finish; @@ -34,7 +34,7 @@ protected enum PredicateType {Unknown, ColumnNames, Range}; protected PredicateType predicateType = PredicateType.Unknown; private static final ByteBuffer EMPTY_BYTE_BUFFER = ByteBuffer.wrap(new byte[0]); - + public HSlicePredicate(Serializer columnNameSerializer) { Assert.notNull(columnNameSerializer, "columnNameSerializer can't be null"); this.columnNameSerializer = columnNameSerializer; @@ -49,7 +49,7 @@ public HSlicePredicate(Serializer columnNameSerializer) { public HSlicePredicate setColumnNames(N... columnNames) { return setColumnNames(Arrays.asList(columnNames)); } - + public HSlicePredicate addColumnName(N columnName) { if ( columnNames == null ) columnNames = new ArrayList(); @@ -57,7 +57,7 @@ public HSlicePredicate addColumnName(N columnName) { predicateType = PredicateType.ColumnNames; return this; } - + /** * Same as varargs signature, except we take a collection * @@ -82,28 +82,28 @@ public HSlicePredicate setKeysOnlyPredicate() { } /** - * Set the columnName on which we will start. - * Switches to {@link PredicateType#Range} + * Set the columnName on which we will start. + * Switches to {@link PredicateType#Range} */ public HSlicePredicate setStartOn(N start) { this.start = start; predicateType = PredicateType.Range; return this; } - + /** - * Set the columnName on which we will end. - * Switches to {@link PredicateType#Range} + * Set the columnName on which we will end. + * Switches to {@link PredicateType#Range} */ public HSlicePredicate setEndOn(N finish) { this.finish = finish; predicateType = PredicateType.Range; return this; } - + /** * Set the number of columns to return for this slice - * Switches to {@link PredicateType#Range} + * Switches to {@link PredicateType#Range} */ public HSlicePredicate setCount(int count) { this.count = count; @@ -113,19 +113,19 @@ public HSlicePredicate setCount(int count) { } /** - * Sets the return order of the columns to be reversed. - * NOTE: this is slightly less efficient than reading in comparator order. - * Switches to {@link PredicateType#Range} + * Sets the return order of the columns to be reversed. + * NOTE: this is slightly less efficient than reading in comparator order. + * Switches to {@link PredicateType#Range} */ public HSlicePredicate setReversed(boolean reversed) { this.reversed = reversed; predicateType = PredicateType.Range; return this; } - - - - + + + + /** * Set a predicate of start/finish to retrieve a list of columns in this range. * Either start and or finish can be null which will toggle the underlying predicate to @@ -199,11 +199,11 @@ private List toThriftColumnNames(Collection clms) { @Override public String toString() { - return String.format("HSlicePredicate(%s)", predicateType == PredicateType.ColumnNames ? columnNames : formatPredicate()); + return String.format("HSlicePredicate(%s)", predicateType == PredicateType.ColumnNames ? columnNames : formatPredicate()); } - + private String formatPredicate() { - return String.format("start:[%s],end:[%s],count:%d,reversed:%b", + return String.format("start:[%s],end:[%s],count:%d,reversed:%b", start != null ? start.toString() : "''", finish != null ? finish.toString() : "''", count, reversed); diff --git a/core/src/main/java/me/prettyprint/cassandra/model/HSuperColumnImpl.java b/core/src/main/java/me/prettyprint/cassandra/model/HSuperColumnImpl.java index ca9a25414..48713c0a6 100644 --- a/core/src/main/java/me/prettyprint/cassandra/model/HSuperColumnImpl.java +++ b/core/src/main/java/me/prettyprint/cassandra/model/HSuperColumnImpl.java @@ -126,7 +126,7 @@ public Serializer getNameSerializer() { public byte[] getNameBytes() { return superNameSerializer.toByteBuffer(getName()).array(); } - + public ByteBuffer getNameByteBuffer() { return superNameSerializer.toByteBuffer(getName()); } diff --git a/core/src/main/java/me/prettyprint/cassandra/model/IndexedSlicesQuery.java b/core/src/main/java/me/prettyprint/cassandra/model/IndexedSlicesQuery.java index fb1cad03e..66ed268e0 100644 --- a/core/src/main/java/me/prettyprint/cassandra/model/IndexedSlicesQuery.java +++ b/core/src/main/java/me/prettyprint/cassandra/model/IndexedSlicesQuery.java @@ -23,7 +23,7 @@ * Uses new secondary indexes. Your CF must be configured for such to use this. * The following creates an Indexed CF with the "birthday" column indexed (where * birthdate represents a timestamp as it is validated by the LongType): - * + * *
      *         - name: Indexed1
      *           column_metadata:
    @@ -31,7 +31,7 @@
      *               validator_class: LongType
      *               index_type: KEYS
      * 
    - * + * * @author zznate (nate@riptano.com) */ public class IndexedSlicesQuery extends diff --git a/core/src/main/java/me/prettyprint/cassandra/model/MutatorImpl.java b/core/src/main/java/me/prettyprint/cassandra/model/MutatorImpl.java index 9b88c9978..46fcb7374 100644 --- a/core/src/main/java/me/prettyprint/cassandra/model/MutatorImpl.java +++ b/core/src/main/java/me/prettyprint/cassandra/model/MutatorImpl.java @@ -73,10 +73,10 @@ public MutationResult delete(final K key, final String cf, final N columnNam addDeletion(key, cf, columnName, nameSerializer); return execute(); } - + @Override public MutationResult delete(K key, String cf, N columnName, - Serializer nameSerializer, long clock) { + Serializer nameSerializer, long clock) { addDeletion(key, cf, columnName, nameSerializer, clock); return execute(); } @@ -98,46 +98,46 @@ public Void doInKeyspace(KeyspaceService ks) throws HectorException { return null; } })); - } - + } + @Override - public MutationResult superDelete(final K key, final String cf, final SN supercolumnName, + public MutationResult superDelete(final K key, final String cf, final SN supercolumnName, final Serializer sNameSerializer) { return new MutationResultImpl(keyspace.doExecute(new KeyspaceOperationCallback() { @Override public Void doInKeyspace(KeyspaceService ks) throws HectorException { // Remove a Super Column. ks.remove( - keySerializer.toByteBuffer(key), + keySerializer.toByteBuffer(key), ThriftFactory.createSuperColumnPath(cf, supercolumnName, sNameSerializer)); return null; } })); } - + /** * Deletes the columns defined in the HSuperColumn. If there are no HColumns attached, - * we delete the whole thing. - * + * we delete the whole thing. + * */ public Mutator addSubDelete(K key, String cf, HSuperColumn sc) { return addSubDelete(key, cf, sc, keyspace.createClock()); } - + public Mutator addSubDelete(K key, String cf, HSuperColumn sc, long clock) { SlicePredicate pred = new SlicePredicate(); Deletion d = new Deletion().setTimestamp(clock); - if ( sc.getColumns() != null ) { + if ( sc.getColumns() != null ) { for (HColumn col : sc.getColumns()) { pred.addToColumn_names(col.getNameSerializer().toByteBuffer(col.getName())); } d.setPredicate(pred); - } + } d.setSuper_column(sc.getNameByteBuffer()); - getPendingMutations().addDeletion(key, Arrays.asList(cf), d); + getPendingMutations().addDeletion(key, Arrays.asList(cf), d); return this; } - + // schedule an insertion to be executed in batch by the execute method // CAVEAT: a large number of calls with a typo in one of them will leave things in an // indeterminant state if we dont validate against LIVE (but cached of course) @@ -168,7 +168,7 @@ public Mutator addDeletion(K key, String cf, N columnName, Serializer addDeletion(key, cf, columnName, nameSerializer, keyspace.createClock()); return this; } - + /** * {@inheritDoc} */ @@ -177,7 +177,7 @@ public Mutator addDeletion(K key, String cf) { addDeletion(key, cf, null, null, keyspace.createClock()); return this; } - + /** * {@inheritDoc} */ @@ -197,7 +197,7 @@ public Mutator addDeletion(K key, String cf, N columnName, Serializer if ( columnName != null ) { sp.addToColumn_names(nameSerializer.toByteBuffer(columnName)); d = new Deletion().setTimestamp(clock).setPredicate(sp); - } else { + } else { d = new Deletion().setTimestamp(clock); } getPendingMutations().addDeletion(key, Arrays.asList(cf), d); @@ -245,7 +245,7 @@ private BatchMutation getPendingMutations() { } return pendingMutations; } - + // Counters support. @Override @@ -258,12 +258,12 @@ public Void doInKeyspace(KeyspaceService ks) throws HectorException { } })); } - + @Override public MutationResult incrementCounter(final K key, final String cf, final N columnName, final long increment) { return insertCounter(key, cf, new HCounterColumnImpl(columnName, increment, TypeInferringSerializer. get())); } - + @Override public MutationResult decrementCounter(final K key, final String cf, final N columnName, final long increment) { return incrementCounter(key, cf, columnName, increment * -1L); @@ -278,12 +278,12 @@ public MutationResult insertCounter(K key, String cf, HCounterSuperColum @Override - public MutationResult deleteCounter(final K key, final String cf, final N counterColumnName, + public MutationResult deleteCounter(final K key, final String cf, final N counterColumnName, final Serializer nameSerializer) { return new MutationResultImpl(keyspace.doExecute(new KeyspaceOperationCallback() { @Override public Void doInKeyspace(KeyspaceService ks) throws HectorException { - ks.removeCounter(keySerializer.toByteBuffer(key), ThriftFactory.createColumnPath(cf, counterColumnName, + ks.removeCounter(keySerializer.toByteBuffer(key), ThriftFactory.createColumnPath(cf, counterColumnName, nameSerializer)); return null; } @@ -291,7 +291,7 @@ public Void doInKeyspace(KeyspaceService ks) throws HectorException { } @Override - public MutationResult subDeleteCounter(final K key, final String cf, final SN supercolumnName, + public MutationResult subDeleteCounter(final K key, final String cf, final SN supercolumnName, final N columnName, final Serializer sNameSerializer, final Serializer nameSerializer) { return new MutationResultImpl(keyspace.doExecute(new KeyspaceOperationCallback() { @Override @@ -311,7 +311,7 @@ public Mutator addCounter(K key, String cf, HCounterColumn c) { @Override public Mutator addCounter(K key, String cf, HCounterSuperColumn sc) { - getPendingMutations().addSuperCounterInsertion(key, Arrays.asList(cf), + getPendingMutations().addSuperCounterInsertion(key, Arrays.asList(cf), ((HCounterSuperColumnImpl) sc).toThrift()); return this; } @@ -324,7 +324,7 @@ public Mutator addCounterDeletion(K key, String cf, N counterColumnName, if ( counterColumnName != null ) { sp.addToColumn_names(nameSerializer.toByteBuffer(counterColumnName)); d = new Deletion().setPredicate(sp); - } else { + } else { d = new Deletion(); } getPendingMutations().addDeletion(key, Arrays.asList(cf), d); @@ -341,12 +341,12 @@ public Mutator addCounterDeletion(K key, String cf) { public Mutator addCounterSubDeletion(K key, String cf, HCounterSuperColumn sc) { SlicePredicate pred = new SlicePredicate(); Deletion d = new Deletion(); - if ( sc.getColumns() != null ) { + if ( sc.getColumns() != null ) { for (HCounterColumn col : sc.getColumns()) { pred.addToColumn_names(col.getNameSerializer().toByteBuffer(col.getName())); } d.setPredicate(pred); - } + } d.setSuper_column(sc.getNameByteBuffer()); getPendingMutations().addDeletion(key, Arrays.asList(cf), d); return this; @@ -357,11 +357,11 @@ public Mutator addSubDelete(K key, String cf, SN sColumnName, N columnName, Serializer sNameSerializer, Serializer nameSerializer) { return addSubDelete(key, cf, sColumnName, columnName, sNameSerializer, nameSerializer, keyspace.createClock()); } - + @Override public Mutator addSubDelete(K key, String cf, SN sColumnName, N columnName, Serializer sNameSerializer, Serializer nameSerializer, long clock) { - Deletion d = new Deletion().setTimestamp(clock); + Deletion d = new Deletion().setTimestamp(clock); SlicePredicate predicate = new SlicePredicate(); predicate.addToColumn_names(nameSerializer.toByteBuffer(columnName)); d.setPredicate(predicate); @@ -371,13 +371,13 @@ public Mutator addSubDelete(K key, String cf, SN sColumnName, } - + @Override public Mutator addSuperDelete(K key, String cf, SN sColumnName, - Serializer sNameSerializer) { - Deletion d = new Deletion().setTimestamp(keyspace.createClock()); + Serializer sNameSerializer) { + Deletion d = new Deletion().setTimestamp(keyspace.createClock()); d.setSuper_column(sNameSerializer.toByteBuffer(sColumnName)); - getPendingMutations().addDeletion(key, Arrays.asList(cf), d); + getPendingMutations().addDeletion(key, Arrays.asList(cf), d); return this; diff --git a/core/src/main/java/me/prettyprint/cassandra/model/OrderedRowsImpl.java b/core/src/main/java/me/prettyprint/cassandra/model/OrderedRowsImpl.java index 7abcac28f..a19d0c8d5 100644 --- a/core/src/main/java/me/prettyprint/cassandra/model/OrderedRowsImpl.java +++ b/core/src/main/java/me/prettyprint/cassandra/model/OrderedRowsImpl.java @@ -25,7 +25,7 @@ public OrderedRowsImpl(LinkedHashMap> thriftRet, Serializer n super(thriftRet, nameSerializer, valueSerializer); rowsList = new ArrayList>(rows.values()); } - + protected OrderedRowsImpl() { super(); rowsList = new ArrayList>(); diff --git a/core/src/main/java/me/prettyprint/cassandra/model/RowsImpl.java b/core/src/main/java/me/prettyprint/cassandra/model/RowsImpl.java index 8cb5c496d..ae20e0216 100644 --- a/core/src/main/java/me/prettyprint/cassandra/model/RowsImpl.java +++ b/core/src/main/java/me/prettyprint/cassandra/model/RowsImpl.java @@ -34,7 +34,7 @@ public RowsImpl(Map> thriftRet, Serializer nameSerializer, valueSerializer)); } } - + protected RowsImpl() { this.rows = new HashMap>(); } diff --git a/core/src/main/java/me/prettyprint/cassandra/model/thrift/AbstractThriftClientWrapper.java b/core/src/main/java/me/prettyprint/cassandra/model/thrift/AbstractThriftClientWrapper.java index dae7b10a4..7b564cd23 100644 --- a/core/src/main/java/me/prettyprint/cassandra/model/thrift/AbstractThriftClientWrapper.java +++ b/core/src/main/java/me/prettyprint/cassandra/model/thrift/AbstractThriftClientWrapper.java @@ -32,7 +32,7 @@ /** * For creating wrappers around the Cassandra client in order to perform pre and * post processing - * + * * @author Ed Anuff */ diff --git a/core/src/main/java/me/prettyprint/cassandra/model/thrift/ThriftConverter.java b/core/src/main/java/me/prettyprint/cassandra/model/thrift/ThriftConverter.java index 42f428d22..1c3e512ff 100644 --- a/core/src/main/java/me/prettyprint/cassandra/model/thrift/ThriftConverter.java +++ b/core/src/main/java/me/prettyprint/cassandra/model/thrift/ThriftConverter.java @@ -37,7 +37,7 @@ public static ConsistencyLevel consistencyLevel(HConsistencyLevel c) { throw new RuntimeException("Unregornized consistency level " + c); } } - + /** * Converts a list of ColumnOrSuperColumn to Column * @param columns @@ -50,5 +50,5 @@ public static List getColumnList(List columns) { } return list; } - + } diff --git a/core/src/main/java/me/prettyprint/cassandra/model/thrift/ThriftCounterColumnQuery.java b/core/src/main/java/me/prettyprint/cassandra/model/thrift/ThriftCounterColumnQuery.java index 6fef3e8d7..2189ea647 100644 --- a/core/src/main/java/me/prettyprint/cassandra/model/thrift/ThriftCounterColumnQuery.java +++ b/core/src/main/java/me/prettyprint/cassandra/model/thrift/ThriftCounterColumnQuery.java @@ -26,9 +26,9 @@ * @param * value type */ -public class ThriftCounterColumnQuery extends AbstractBasicQuery> +public class ThriftCounterColumnQuery extends AbstractBasicQuery> implements CounterQuery { - + protected K key; protected N name; @@ -40,7 +40,7 @@ public ThriftCounterColumnQuery(Keyspace keyspace, Serializer keySerializer, public ThriftCounterColumnQuery(Keyspace keyspace) { super(keyspace, TypeInferringSerializer. get(), TypeInferringSerializer. get()); } - + public CounterQuery setKey(K key) { this.key = key; return this; @@ -50,7 +50,7 @@ public CounterQuery setName(N name) { this.name = name; return this; } - + @SuppressWarnings("unchecked") @Override public CounterQuery setColumnFamily(String cf) { diff --git a/core/src/main/java/me/prettyprint/cassandra/model/thrift/ThriftFactory.java b/core/src/main/java/me/prettyprint/cassandra/model/thrift/ThriftFactory.java index c121a3f57..3dd081ccf 100644 --- a/core/src/main/java/me/prettyprint/cassandra/model/thrift/ThriftFactory.java +++ b/core/src/main/java/me/prettyprint/cassandra/model/thrift/ThriftFactory.java @@ -55,14 +55,14 @@ public static ColumnPath createSuperColumnPath(String columnFamilyName, } return columnPath; } - + public static CounterColumn createCounterColumn(String name, long value) { CounterColumn cc = new CounterColumn(); cc.setName(StringSerializer.get().toByteBuffer(name)); cc.setValue(value); return cc; } - + public static CounterColumn createCounterColumn(N name, long value, Serializer ns) { CounterColumn cc = new CounterColumn(); cc.setName(ns.toByteBuffer(name)); diff --git a/core/src/main/java/me/prettyprint/cassandra/model/thrift/ThriftRangeSlicesQuery.java b/core/src/main/java/me/prettyprint/cassandra/model/thrift/ThriftRangeSlicesQuery.java index 2392da939..eac855e3b 100644 --- a/core/src/main/java/me/prettyprint/cassandra/model/thrift/ThriftRangeSlicesQuery.java +++ b/core/src/main/java/me/prettyprint/cassandra/model/thrift/ThriftRangeSlicesQuery.java @@ -100,8 +100,8 @@ public ThriftRangeSlicesQuery setReturnKeysOnly() { return this; } - - - + + + } diff --git a/core/src/main/java/me/prettyprint/cassandra/model/thrift/ThriftSliceCounterQuery.java b/core/src/main/java/me/prettyprint/cassandra/model/thrift/ThriftSliceCounterQuery.java index 5aaf448cc..568778bce 100644 --- a/core/src/main/java/me/prettyprint/cassandra/model/thrift/ThriftSliceCounterQuery.java +++ b/core/src/main/java/me/prettyprint/cassandra/model/thrift/ThriftSliceCounterQuery.java @@ -33,7 +33,7 @@ public final class ThriftSliceCounterQuery extends AbstractSliceQuery keySerializer, Serializer nameSerializer) { - // The reason of Longserializer is just to + // The reason of Longserializer is just to super(k, keySerializer, nameSerializer, LongSerializer.get()); } diff --git a/core/src/main/java/me/prettyprint/cassandra/serializers/AbstractSerializer.java b/core/src/main/java/me/prettyprint/cassandra/serializers/AbstractSerializer.java index 412c2705b..a36fa70e4 100644 --- a/core/src/main/java/me/prettyprint/cassandra/serializers/AbstractSerializer.java +++ b/core/src/main/java/me/prettyprint/cassandra/serializers/AbstractSerializer.java @@ -18,9 +18,9 @@ * A base class for serializer implementations. Takes care of the default * implementations of to/fromBytesList and to/fromBytesMap. Extenders of this * class only need to implement the toBytes and fromBytes. - * + * * @author Ed Anuff - * + * * @param */ public abstract class AbstractSerializer implements Serializer { @@ -44,7 +44,7 @@ public T fromBytes(byte[] bytes) { /* * public ByteBuffer toByteBuffer(T obj) { return * ByteBuffer.wrap(toBytes(obj)); } - * + * * public ByteBuffer toByteBuffer(T obj, ByteBuffer byteBuffer, int offset, * int length) { byteBuffer.put(toBytes(obj), offset, length); return * byteBuffer; } @@ -56,7 +56,7 @@ public T fromBytes(byte[] bytes) { /* * public T fromByteBuffer(ByteBuffer byteBuffer) { return * fromBytes(byteBuffer.array()); } - * + * * public T fromByteBuffer(ByteBuffer byteBuffer, int offset, int length) { * return fromBytes(Arrays.copyOfRange(byteBuffer.array(), offset, length)); } */ diff --git a/core/src/main/java/me/prettyprint/cassandra/serializers/AsciiSerializer.java b/core/src/main/java/me/prettyprint/cassandra/serializers/AsciiSerializer.java index 21e8eb4aa..b167bc867 100644 --- a/core/src/main/java/me/prettyprint/cassandra/serializers/AsciiSerializer.java +++ b/core/src/main/java/me/prettyprint/cassandra/serializers/AsciiSerializer.java @@ -10,7 +10,7 @@ /** * Almost identical to StringSerializer except we use the US-ASCII character set * code - * + * * @author zznate */ public final class AsciiSerializer extends AbstractSerializer { diff --git a/core/src/main/java/me/prettyprint/cassandra/serializers/BigIntegerSerializer.java b/core/src/main/java/me/prettyprint/cassandra/serializers/BigIntegerSerializer.java index f2602d234..b16a0008e 100644 --- a/core/src/main/java/me/prettyprint/cassandra/serializers/BigIntegerSerializer.java +++ b/core/src/main/java/me/prettyprint/cassandra/serializers/BigIntegerSerializer.java @@ -9,7 +9,7 @@ /** * Serializer implementation for BigInteger - * + * * @author zznate */ public final class BigIntegerSerializer extends AbstractSerializer { diff --git a/core/src/main/java/me/prettyprint/cassandra/serializers/BooleanSerializer.java b/core/src/main/java/me/prettyprint/cassandra/serializers/BooleanSerializer.java index 56d1f16d1..9d85bcb5e 100644 --- a/core/src/main/java/me/prettyprint/cassandra/serializers/BooleanSerializer.java +++ b/core/src/main/java/me/prettyprint/cassandra/serializers/BooleanSerializer.java @@ -4,9 +4,9 @@ /** * Converts bytes to Boolean and vice versa - * + * * @author Bozhidar Bozhanov - * + * */ public final class BooleanSerializer extends AbstractSerializer { diff --git a/core/src/main/java/me/prettyprint/cassandra/serializers/ByteBufferSerializer.java b/core/src/main/java/me/prettyprint/cassandra/serializers/ByteBufferSerializer.java index 1d75618b6..f76fd3fcf 100644 --- a/core/src/main/java/me/prettyprint/cassandra/serializers/ByteBufferSerializer.java +++ b/core/src/main/java/me/prettyprint/cassandra/serializers/ByteBufferSerializer.java @@ -9,10 +9,10 @@ /** * The BytesExtractor is a simple identity function. It supports the Extractor * interface and implements the fromBytes and toBytes as simple identity - * functions. However, the from and to methods both return the results of - * {@link ByteBuffer#duplicate()} - * - * + * functions. However, the from and to methods both return the results of + * {@link ByteBuffer#duplicate()} + * + * * @author Ran Tavory * @author zznate */ diff --git a/core/src/main/java/me/prettyprint/cassandra/serializers/BytesArraySerializer.java b/core/src/main/java/me/prettyprint/cassandra/serializers/BytesArraySerializer.java index a8bafb69b..72e8e8d4b 100644 --- a/core/src/main/java/me/prettyprint/cassandra/serializers/BytesArraySerializer.java +++ b/core/src/main/java/me/prettyprint/cassandra/serializers/BytesArraySerializer.java @@ -6,9 +6,9 @@ /** * A BytesArraySerializer translates the byte[] to and from ByteBuffer. - * + * * @author Patricio Echague - * + * */ public final class BytesArraySerializer extends AbstractSerializer implements Serializer { diff --git a/core/src/main/java/me/prettyprint/cassandra/serializers/CharSerializer.java b/core/src/main/java/me/prettyprint/cassandra/serializers/CharSerializer.java index a18bfa7e6..a466b353c 100644 --- a/core/src/main/java/me/prettyprint/cassandra/serializers/CharSerializer.java +++ b/core/src/main/java/me/prettyprint/cassandra/serializers/CharSerializer.java @@ -4,7 +4,7 @@ /** * Uses Char Serializer - * + * * @author Todd Nine */ public class CharSerializer extends AbstractSerializer { diff --git a/core/src/main/java/me/prettyprint/cassandra/serializers/CompositeSerializer.java b/core/src/main/java/me/prettyprint/cassandra/serializers/CompositeSerializer.java index 9065c8408..ae1a19cba 100644 --- a/core/src/main/java/me/prettyprint/cassandra/serializers/CompositeSerializer.java +++ b/core/src/main/java/me/prettyprint/cassandra/serializers/CompositeSerializer.java @@ -1,5 +1,5 @@ /** - * + * */ package me.prettyprint.cassandra.serializers; @@ -12,7 +12,7 @@ /** * @author Todd Nine - * + * */ public class CompositeSerializer extends AbstractSerializer { diff --git a/core/src/main/java/me/prettyprint/cassandra/serializers/DoubleSerializer.java b/core/src/main/java/me/prettyprint/cassandra/serializers/DoubleSerializer.java index 0394805bd..5ed457fb3 100644 --- a/core/src/main/java/me/prettyprint/cassandra/serializers/DoubleSerializer.java +++ b/core/src/main/java/me/prettyprint/cassandra/serializers/DoubleSerializer.java @@ -7,8 +7,8 @@ /** * Uses LongSerializer via translating Doubles to and from raw long bytes form. - * - * @author Yuri Finkelstein + * + * @author Yuri Finkelstein */ public class DoubleSerializer extends AbstractSerializer { @@ -17,7 +17,7 @@ public class DoubleSerializer extends AbstractSerializer { public static DoubleSerializer get() { return instance; } - + @Override public ByteBuffer toByteBuffer(Double obj) { return LongSerializer.get().toByteBuffer(Double.doubleToRawLongBits(obj)); diff --git a/core/src/main/java/me/prettyprint/cassandra/serializers/DynamicCompositeSerializer.java b/core/src/main/java/me/prettyprint/cassandra/serializers/DynamicCompositeSerializer.java index 93c4be43d..9447dabc7 100644 --- a/core/src/main/java/me/prettyprint/cassandra/serializers/DynamicCompositeSerializer.java +++ b/core/src/main/java/me/prettyprint/cassandra/serializers/DynamicCompositeSerializer.java @@ -1,5 +1,5 @@ /** - * + * */ package me.prettyprint.cassandra.serializers; @@ -12,7 +12,7 @@ /** * @author Todd Nine - * + * */ public class DynamicCompositeSerializer extends AbstractSerializer { diff --git a/core/src/main/java/me/prettyprint/cassandra/serializers/FastInfosetSerializer.java b/core/src/main/java/me/prettyprint/cassandra/serializers/FastInfosetSerializer.java index 0b11e9a89..452e357a4 100644 --- a/core/src/main/java/me/prettyprint/cassandra/serializers/FastInfosetSerializer.java +++ b/core/src/main/java/me/prettyprint/cassandra/serializers/FastInfosetSerializer.java @@ -16,14 +16,14 @@ * size as well as parse and serialization time. An instance of this class may * only serialize JAXB compatible objects of classes known to its configured * context. - * + * * @author shuzhang0@gmail.com - * + * */ public class FastInfosetSerializer extends JaxbSerializer { /** * Constructor. - * + * * @param serializableClasses * List of classes which can be serialized by this instance. Note * that concrete classes directly referenced by any class in the list diff --git a/core/src/main/java/me/prettyprint/cassandra/serializers/FloatSerializer.java b/core/src/main/java/me/prettyprint/cassandra/serializers/FloatSerializer.java index 95c768958..3435f881f 100644 --- a/core/src/main/java/me/prettyprint/cassandra/serializers/FloatSerializer.java +++ b/core/src/main/java/me/prettyprint/cassandra/serializers/FloatSerializer.java @@ -4,7 +4,7 @@ /** * Uses IntSerializer via translating Float objects to and from raw long bytes form. - * + * * @author Todd Nine */ public class FloatSerializer extends AbstractSerializer { @@ -14,7 +14,7 @@ public class FloatSerializer extends AbstractSerializer { public static FloatSerializer get() { return instance; } - + @Override public ByteBuffer toByteBuffer(Float obj) { return IntegerSerializer.get().toByteBuffer(Float.floatToRawIntBits(obj)); diff --git a/core/src/main/java/me/prettyprint/cassandra/serializers/IntegerSerializer.java b/core/src/main/java/me/prettyprint/cassandra/serializers/IntegerSerializer.java index a2000b5c5..9dce79402 100644 --- a/core/src/main/java/me/prettyprint/cassandra/serializers/IntegerSerializer.java +++ b/core/src/main/java/me/prettyprint/cassandra/serializers/IntegerSerializer.java @@ -4,9 +4,9 @@ /** * Converts bytes to Integer and vice versa - * + * * @author Bozhidar Bozhanov - * + * */ public final class IntegerSerializer extends AbstractSerializer { @@ -35,7 +35,7 @@ public Integer fromByteBuffer(ByteBuffer byteBuffer) { int in = byteBuffer.getInt(); return in; } - + @Override public Integer fromBytes(byte[] bytes) { ByteBuffer bb = ByteBuffer.allocate(4).put(bytes, 0, 4); diff --git a/core/src/main/java/me/prettyprint/cassandra/serializers/JaxbSerializer.java b/core/src/main/java/me/prettyprint/cassandra/serializers/JaxbSerializer.java index a376a759b..8e4053ae0 100644 --- a/core/src/main/java/me/prettyprint/cassandra/serializers/JaxbSerializer.java +++ b/core/src/main/java/me/prettyprint/cassandra/serializers/JaxbSerializer.java @@ -22,7 +22,7 @@ /** * Serializes Objects using Jaxb. An instance of this class may only serialize * JAXB compatible objects of classes known to its configured context. - * + * * @author shuzhang0@gmail.com */ public class JaxbSerializer extends AbstractSerializer { @@ -45,7 +45,7 @@ public class JaxbSerializer extends AbstractSerializer { /** * Constructor. - * + * * @param serializableClasses * List of classes which can be serialized by this instance. Note * that concrete classes directly referenced by any class in the list @@ -126,7 +126,7 @@ public Object fromByteBuffer(ByteBuffer bytes) { /** * Get a new XML stream writer. - * + * * @param output * An underlying OutputStream to write to. * @return a new {@link XMLStreamWriter} which writes to the specified @@ -147,7 +147,7 @@ protected XMLStreamWriter createStreamWriter(OutputStream output) /** * Get a new XML stream reader. - * + * * @param input * the underlying InputStream to read from. * @return a new {@link XmlStreamReader} which reads from the specified diff --git a/core/src/main/java/me/prettyprint/cassandra/serializers/LongSerializer.java b/core/src/main/java/me/prettyprint/cassandra/serializers/LongSerializer.java index 2bb23b969..ff2974bf5 100644 --- a/core/src/main/java/me/prettyprint/cassandra/serializers/LongSerializer.java +++ b/core/src/main/java/me/prettyprint/cassandra/serializers/LongSerializer.java @@ -8,9 +8,9 @@ /** * Converts bytes to Long and vise a versa - * + * * @author Ran Tavory - * + * */ public final class LongSerializer extends AbstractSerializer { diff --git a/core/src/main/java/me/prettyprint/cassandra/serializers/ObjectSerializer.java b/core/src/main/java/me/prettyprint/cassandra/serializers/ObjectSerializer.java index a3622b99b..dd651fd14 100644 --- a/core/src/main/java/me/prettyprint/cassandra/serializers/ObjectSerializer.java +++ b/core/src/main/java/me/prettyprint/cassandra/serializers/ObjectSerializer.java @@ -13,9 +13,9 @@ /** * The ObjectSerializer is used to turn objects into their binary * representations. - * + * * @author Bozhidar Bozhanov - * + * */ public class ObjectSerializer extends AbstractSerializer implements Serializer { diff --git a/core/src/main/java/me/prettyprint/cassandra/serializers/ShortSerializer.java b/core/src/main/java/me/prettyprint/cassandra/serializers/ShortSerializer.java index d77e9ac70..4a0ffdaf7 100644 --- a/core/src/main/java/me/prettyprint/cassandra/serializers/ShortSerializer.java +++ b/core/src/main/java/me/prettyprint/cassandra/serializers/ShortSerializer.java @@ -4,7 +4,7 @@ /** * {@link Serializer} for {@link Short}s (no pun intended). - * + * */ public final class ShortSerializer extends AbstractSerializer { diff --git a/core/src/main/java/me/prettyprint/cassandra/serializers/StringSerializer.java b/core/src/main/java/me/prettyprint/cassandra/serializers/StringSerializer.java index 9e84c0eb4..772a3cfbb 100644 --- a/core/src/main/java/me/prettyprint/cassandra/serializers/StringSerializer.java +++ b/core/src/main/java/me/prettyprint/cassandra/serializers/StringSerializer.java @@ -10,9 +10,9 @@ /** * A StringSerializer translates the byte[] to and from string using utf-8 * encoding. - * + * * @author Ran Tavory - * + * */ public final class StringSerializer extends AbstractSerializer { diff --git a/core/src/main/java/me/prettyprint/cassandra/serializers/UUIDSerializer.java b/core/src/main/java/me/prettyprint/cassandra/serializers/UUIDSerializer.java index ab25a9408..c7569a715 100644 --- a/core/src/main/java/me/prettyprint/cassandra/serializers/UUIDSerializer.java +++ b/core/src/main/java/me/prettyprint/cassandra/serializers/UUIDSerializer.java @@ -9,9 +9,9 @@ /** * A UUIDSerializer translates the byte[] to and from UUID types. - * + * * @author Ed Anuff - * + * */ public final class UUIDSerializer extends AbstractSerializer { diff --git a/core/src/main/java/me/prettyprint/cassandra/service/AbstractCluster.java b/core/src/main/java/me/prettyprint/cassandra/service/AbstractCluster.java index 6b90fdc0c..b2d9c0548 100644 --- a/core/src/main/java/me/prettyprint/cassandra/service/AbstractCluster.java +++ b/core/src/main/java/me/prettyprint/cassandra/service/AbstractCluster.java @@ -272,13 +272,13 @@ public Void execute(Cassandra.Client cassandra) throws HectorException { throw xtrans.translate(e); } return null; - } + } }; connectionManager.operateWithFailover(op); } - - - + + + } diff --git a/core/src/main/java/me/prettyprint/cassandra/service/BatchMutation.java b/core/src/main/java/me/prettyprint/cassandra/service/BatchMutation.java index e15ffd412..fca0fd370 100644 --- a/core/src/main/java/me/prettyprint/cassandra/service/BatchMutation.java +++ b/core/src/main/java/me/prettyprint/cassandra/service/BatchMutation.java @@ -54,7 +54,7 @@ public BatchMutation addInsertion(K key, List columnFamilies, addMutation(key, columnFamilies, mutation); return this; } - + /** * Add a SuperColumn insertion (or update) to the batch mutation request. */ @@ -65,7 +65,7 @@ public BatchMutation addSuperInsertion(K key, List columnFamilies, addMutation(key, columnFamilies, mutation); return this; } - + /** * Add a ColumnCounter insertion (or update) */ @@ -75,11 +75,11 @@ public BatchMutation addCounterInsertion(K key, List columnFamilies, addMutation(key, columnFamilies, mutation); return this; } - + /** * Add a SuperColumnCounter insertion (or update) */ - public BatchMutation addSuperCounterInsertion(K key, List columnFamilies, + public BatchMutation addSuperCounterInsertion(K key, List columnFamilies, CounterSuperColumn counterSuperColumn) { Mutation mutation = new Mutation(); mutation.setColumn_or_supercolumn(new ColumnOrSuperColumn().setCounter_super_column(counterSuperColumn)); @@ -96,7 +96,7 @@ public BatchMutation addDeletion(K key, List columnFamilies, Deletion addMutation(key, columnFamilies, mutation); return this; } - + private void addMutation(K key, List columnFamilies, Mutation mutation) { Map> innerMutationMap = getInnerMutationMap(key); @@ -111,7 +111,7 @@ private void addMutation(K key, List columnFamilies, Mutation mutation) } mutationMap.put(keySerializer.toByteBuffer(key), innerMutationMap); } - + private Map> getInnerMutationMap(K key) { @@ -121,7 +121,7 @@ private Map> getInnerMutationMap(K key) { } return innerMutationMap; } - + Map>> getMutationMap() { return mutationMap; } diff --git a/core/src/main/java/me/prettyprint/cassandra/service/CassandraClientMonitor.java b/core/src/main/java/me/prettyprint/cassandra/service/CassandraClientMonitor.java index 5c8b9068a..2e78bb214 100644 --- a/core/src/main/java/me/prettyprint/cassandra/service/CassandraClientMonitor.java +++ b/core/src/main/java/me/prettyprint/cassandra/service/CassandraClientMonitor.java @@ -203,12 +203,12 @@ public long getNumConnectionErrors() { } @Override - public boolean addCassandraHost(String hostStr) { + public boolean addCassandraHost(String hostStr) { return connectionManager.addCassandraHost(new CassandraHost(hostStr)); } @Override - public boolean removeCassandraHost(String hostStr) { + public boolean removeCassandraHost(String hostStr) { return connectionManager.removeCassandraHost(new CassandraHost(hostStr)); } @@ -217,13 +217,13 @@ public Set getSuspendedCassandraHosts() { Set hosts = connectionManager.getSuspendedCassandraHosts(); Set hostsStr = new HashSet(); for (CassandraHost host : hosts) { - hostsStr.add(host.getName()); + hostsStr.add(host.getName()); } return hostsStr; } @Override - public boolean suspendCassandraHost(String hostStr) { + public boolean suspendCassandraHost(String hostStr) { return connectionManager.suspendCassandraHost(new CassandraHost(hostStr)); } @@ -231,9 +231,9 @@ public boolean suspendCassandraHost(String hostStr) { public boolean unsuspendCassandraHost(String hostStr) { return connectionManager.unsuspendCassandraHost(new CassandraHost(hostStr)); } - - - - + + + + } diff --git a/core/src/main/java/me/prettyprint/cassandra/service/CassandraClientMonitorMBean.java b/core/src/main/java/me/prettyprint/cassandra/service/CassandraClientMonitorMBean.java index d537b6296..298192256 100644 --- a/core/src/main/java/me/prettyprint/cassandra/service/CassandraClientMonitorMBean.java +++ b/core/src/main/java/me/prettyprint/cassandra/service/CassandraClientMonitorMBean.java @@ -118,38 +118,38 @@ public interface CassandraClientMonitorMBean { * */ public List getStatisticsPerPool(); - + /** * Add a host in the format of "[hostname]:[port]" - * + * * @param hostStr * @return */ boolean addCassandraHost(String hostStr); - + /** * Remove a host in the format of "[hostname]:[port]" * @see {@link CassandraHost#equals(Object)} for how hosts are compared - * + * * @param hostStr * @return */ boolean removeCassandraHost(String hostStr); - + /** - * @see {@link #removeCassandraHost(String)} above for semantics of the host string. + * @see {@link #removeCassandraHost(String)} above for semantics of the host string. * @see {@link HConnectionManager#removeCassandraHost(CassandraHost)} for details of this operation. * @param hostStr * @return */ boolean suspendCassandraHost(String hostStr); - + /** * @see {@link #suspendCassandraHost(String)} above. This is the opposite. * @param hostStr * @return */ boolean unsuspendCassandraHost(String hostStr); - - Set getSuspendedCassandraHosts(); + + Set getSuspendedCassandraHosts(); } diff --git a/core/src/main/java/me/prettyprint/cassandra/service/CassandraHost.java b/core/src/main/java/me/prettyprint/cassandra/service/CassandraHost.java index e03e7144f..845d84892 100644 --- a/core/src/main/java/me/prettyprint/cassandra/service/CassandraHost.java +++ b/core/src/main/java/me/prettyprint/cassandra/service/CassandraHost.java @@ -234,5 +234,5 @@ public void setUseSocketKeepalive(boolean useSocketKeepalive) { this.useSocketKeepalive = useSocketKeepalive; } - + } diff --git a/core/src/main/java/me/prettyprint/cassandra/service/CassandraHostConfigurator.java b/core/src/main/java/me/prettyprint/cassandra/service/CassandraHostConfigurator.java index b121638dd..b2deda483 100644 --- a/core/src/main/java/me/prettyprint/cassandra/service/CassandraHostConfigurator.java +++ b/core/src/main/java/me/prettyprint/cassandra/service/CassandraHostConfigurator.java @@ -296,13 +296,13 @@ public boolean getUseSocketKeepalive() { } /** - * Enable SO_KEEPALIVE on the underlying socket. OFF by default (per java.net.Socket) - * + * Enable SO_KEEPALIVE on the underlying socket. OFF by default (per java.net.Socket) + * */ public void setUseSocketKeepalive(boolean useSocketKeepalive) { this.useSocketKeepalive = useSocketKeepalive; } - + } diff --git a/core/src/main/java/me/prettyprint/cassandra/service/ExceptionsTranslator.java b/core/src/main/java/me/prettyprint/cassandra/service/ExceptionsTranslator.java index 377a95d39..99ae27794 100644 --- a/core/src/main/java/me/prettyprint/cassandra/service/ExceptionsTranslator.java +++ b/core/src/main/java/me/prettyprint/cassandra/service/ExceptionsTranslator.java @@ -4,12 +4,12 @@ /** * Translates exceptions throw by thrift or pool to HectorException instances. - * + * * @author Ran Tavory (ran@outbrain.com) * */ public interface ExceptionsTranslator { - + HectorException translate(Throwable originalException); - + } diff --git a/core/src/main/java/me/prettyprint/cassandra/service/ExceptionsTranslatorImpl.java b/core/src/main/java/me/prettyprint/cassandra/service/ExceptionsTranslatorImpl.java index 87ad0fa42..19fdd875a 100644 --- a/core/src/main/java/me/prettyprint/cassandra/service/ExceptionsTranslatorImpl.java +++ b/core/src/main/java/me/prettyprint/cassandra/service/ExceptionsTranslatorImpl.java @@ -26,7 +26,7 @@ public HectorException translate(Throwable original) { if (original instanceof HectorException) { return (HectorException) original; } else if (original instanceof TApplicationException) { - return new HCassandraInternalException(((TApplicationException)original).getType(), original.getMessage()); + return new HCassandraInternalException(((TApplicationException)original).getType(), original.getMessage()); } else if (original instanceof TTransportException) { // if the underlying cause is a scoket timeout, reflect that directly // TODO this may be an issue on the Cassandra side which warrants ivestigation. diff --git a/core/src/main/java/me/prettyprint/cassandra/service/ExhaustedPolicy.java b/core/src/main/java/me/prettyprint/cassandra/service/ExhaustedPolicy.java index a8463850a..72a3a5239 100644 --- a/core/src/main/java/me/prettyprint/cassandra/service/ExhaustedPolicy.java +++ b/core/src/main/java/me/prettyprint/cassandra/service/ExhaustedPolicy.java @@ -11,11 +11,11 @@ public enum ExhaustedPolicy { /** * If the pool is full, fail with the exception {@link PoolExhaustedException} */ - WHEN_EXHAUSTED_FAIL, + WHEN_EXHAUSTED_FAIL, /** * When pool exhausted, grow. */ - WHEN_EXHAUSTED_GROW, + WHEN_EXHAUSTED_GROW, /** * Block the requesting thread when the pool is exhausted until new connections are available. */ diff --git a/core/src/main/java/me/prettyprint/cassandra/service/HColumnFamilyImpl.java b/core/src/main/java/me/prettyprint/cassandra/service/HColumnFamilyImpl.java index 1f8b94c01..8b332bf66 100644 --- a/core/src/main/java/me/prettyprint/cassandra/service/HColumnFamilyImpl.java +++ b/core/src/main/java/me/prettyprint/cassandra/service/HColumnFamilyImpl.java @@ -43,7 +43,7 @@ public class HColumnFamilyImpl implements HColumnFamily { private final Logger queryLogger = LoggerFactory.getLogger("HColumnFamilyLogger"); private final Logger log = LoggerFactory.getLogger(HColumnFamily.class); - + private final ExecutingKeyspace keyspace; private final String columnFamilyName; private List _keys; @@ -62,7 +62,7 @@ public class HColumnFamilyImpl implements HColumnFamily { private Map> rows; private CassandraHost lastHostUsed; private long lastExecutionTime; - + public HColumnFamilyImpl(Keyspace keyspace, String columnFamilyName, Serializer keySerializer, Serializer columnNameSerializer) { this.keyspace = (ExecutingKeyspace)keyspace; @@ -75,7 +75,7 @@ public HColumnFamilyImpl(Keyspace keyspace, String columnFamilyName, Serializer< exceptionsTranslator = new ExceptionsTranslatorImpl(); this.consistencyLevelPolicy = new ConfigurableConsistencyLevel(); } - + @Override public HColumnFamily addKey(K key) { _keys.add(key); @@ -86,9 +86,9 @@ public HColumnFamily addKey(K key) { public HColumnFamily addKeys(Collection keys) { _keys.addAll(keys); return this; - } - - + } + + @Override public HColumnFamily removeKeys() { @@ -118,7 +118,7 @@ public HColumnFamily setReversed(boolean reversed) { public HColumnFamily setStart(N name) { activeSlicePredicate.setStartOn(name); return this; - } + } @Override public HColumnFamily addColumnName(N columnName) { @@ -141,16 +141,16 @@ public HColumnFamily setColumnNames(Collection columnNames) { @Override public Collection> getColumns() { if ( columns == null ) - columns = new HashMap>(); - + columns = new HashMap>(); + if ( !hasValues ) doExecuteSlice(); - + return columns.values(); } - - + + @Override public HColumnFamily clear() { for (HColumn col : columns.values() ) { @@ -172,7 +172,7 @@ public double getDouble(N name) { } @Override - public int getInt(N name) { + public int getInt(N name) { return extractColumnValue(name, IntegerSerializer.get()); } @@ -182,7 +182,7 @@ public long getLong(N name) { } @Override - public String getString(N name) { + public String getString(N name) { return extractColumnValue(name, StringSerializer.get()); } @@ -201,7 +201,7 @@ public HColumnFamily next() { applyToRow(key, rows.get(keySerializer.toByteBuffer(key))); return this; } - + @Override public boolean hasNext() { return rowIndex < rows.size() - 1 ; @@ -217,7 +217,7 @@ public void remove() { * Extract a value for the specified name and serializer */ @Override - public V getValue(N name, Serializer valueSerializer) { + public V getValue(N name, Serializer valueSerializer) { return extractColumnValue(name, valueSerializer); } @@ -233,10 +233,10 @@ public HColumnFamily setWriteConsistencyLevel(HConsistencyLevel writeLevel return this; } - - + + @Override - public long getExecutionTimeMicro() { + public long getExecutionTimeMicro() { return lastExecutionTime / 1000; } @@ -246,10 +246,10 @@ public CassandraHost getHostUsed() { } private V extractColumnValue(N columnName, Serializer valueSerializer) { - maybeExecuteSlice(columnName); - return columns.get(columnName) != null && columns.get(columnName).getValue() != null ? valueSerializer.fromByteBuffer(columns.get(columnName).getValue()) : null; + maybeExecuteSlice(columnName); + return columns.get(columnName) != null && columns.get(columnName).getValue() != null ? valueSerializer.fromByteBuffer(columns.get(columnName).getValue()) : null; } - + private void maybeExecuteSlice(N columnName) { if ( columnNames == null ) { columnNames = new HashSet(); @@ -264,48 +264,48 @@ private void maybeExecuteSlice(N columnName) { doExecuteSlice(); } else { doExecuteMultigetSlice(); - } - } + } + } } - - + + private void applyToRow(K key, List cosclist) { HColumn column; N colName; for (Iterator iterator = cosclist.iterator(); iterator.hasNext();) { - ColumnOrSuperColumn cosc = iterator.next(); + ColumnOrSuperColumn cosc = iterator.next(); colName = columnNameSerializer.fromByteBuffer(cosc.getColumn().name.duplicate()); column = columns.get(colName); - + if ( column == null ) { column = new HColumnImpl(cosc.getColumn(), columnNameSerializer, ByteBufferSerializer.get()); } else { ((HColumnImpl)column).apply(cosc.getColumn()); } - columns.put(colName, column); + columns.put(colName, column); iterator.remove(); } } - + private void applyResultStatus(long execTime, CassandraHost cassandraHost) { lastExecutionTime = execTime; lastHostUsed = cassandraHost; } - + private void doExecuteSlice() { keyspace.doExecuteOperation(new Operation(OperationType.READ) { @Override public Column execute(Cassandra.Client cassandra) throws HectorException { - - try { + + try { if ( queryLogger.isDebugEnabled() ) { queryLogger.debug("---------\nColumnFamily: {} slicePredicate: {}", columnFamilyName, activeSlicePredicate.toString()); } K key = _keys.iterator().next(); List cosclist = cassandra.get_slice(keySerializer.toByteBuffer(key), columnParent, - activeSlicePredicate.toThrift(), + activeSlicePredicate.toThrift(), ThriftConverter.consistencyLevel(consistencyLevelPolicy.get(operationType))); applyResultStatus(execTime, getCassandraHost()); applyToRow(key, cosclist); @@ -321,22 +321,22 @@ public Column execute(Cassandra.Client cassandra) throws HectorException { } }); } - + private void doExecuteMultigetSlice() { keyspace.doExecuteOperation(new Operation(OperationType.READ) { @Override public Column execute(Cassandra.Client cassandra) throws HectorException { - try { + try { if ( queryLogger.isDebugEnabled() ) { queryLogger.debug("---------\nColumnFamily multiget: {} slicePredicate: {}", columnFamilyName, activeSlicePredicate.toString()); } - rows = cassandra.multiget_slice(keySerializer.toBytesList(_keys), columnParent, activeSlicePredicate.toThrift(), + rows = cassandra.multiget_slice(keySerializer.toBytesList(_keys), columnParent, activeSlicePredicate.toThrift(), ThriftConverter.consistencyLevel(consistencyLevelPolicy.get(operationType))); applyResultStatus(execTime, getCassandraHost()); - + if ( queryLogger.isDebugEnabled() ) { queryLogger.debug("Execution took {} microseconds on host {}\n----------", lastExecutionTime, lastHostUsed); } diff --git a/core/src/main/java/me/prettyprint/cassandra/service/JmxMonitor.java b/core/src/main/java/me/prettyprint/cassandra/service/JmxMonitor.java index 17f401a30..08d9b5414 100644 --- a/core/src/main/java/me/prettyprint/cassandra/service/JmxMonitor.java +++ b/core/src/main/java/me/prettyprint/cassandra/service/JmxMonitor.java @@ -35,7 +35,7 @@ public class JmxMonitor { private JmxMonitor() { mbs = ManagementFactory.getPlatformMBeanServer(); - monitors = new HashMap(); + monitors = new HashMap(); } public static JmxMonitor getInstance() { diff --git a/core/src/main/java/me/prettyprint/cassandra/service/KeyIterator.java b/core/src/main/java/me/prettyprint/cassandra/service/KeyIterator.java index b69fc5b75..09a055418 100644 --- a/core/src/main/java/me/prettyprint/cassandra/service/KeyIterator.java +++ b/core/src/main/java/me/prettyprint/cassandra/service/KeyIterator.java @@ -13,7 +13,7 @@ /** - * This class returns each key in the specified Column Family as an Iterator. You + * This class returns each key in the specified Column Family as an Iterator. You * can use this class in a for loop without the overhead of first storing each * key in a large array. See StringKeyIterator for a convenience class if the key * is a String. @@ -89,7 +89,7 @@ private void runQuery(K start) { rowsIterator = (rows != null) ? rows.iterator() : null; // we'll skip this first one, since it is the same as the last one from previous time we executed - if (start != null && rowsIterator != null) rowsIterator.next(); + if (start != null && rowsIterator != null) rowsIterator.next(); if (!rowsIterator.hasNext()) { nextValue = null; // all done. our iterator's hasNext() will now return false; diff --git a/core/src/main/java/me/prettyprint/cassandra/service/KeyspaceService.java b/core/src/main/java/me/prettyprint/cassandra/service/KeyspaceService.java index 0cdabdd8b..c3e00a10d 100644 --- a/core/src/main/java/me/prettyprint/cassandra/service/KeyspaceService.java +++ b/core/src/main/java/me/prettyprint/cassandra/service/KeyspaceService.java @@ -47,7 +47,7 @@ public interface KeyspaceService { Column getColumn(ByteBuffer key, ColumnPath columnPath) throws HectorException; Column getColumn(String key, ColumnPath columnPath) throws HectorException; - + /** * Get the Counter at the given columnPath. * @@ -57,7 +57,7 @@ public interface KeyspaceService { * if no value exists for the counter */ CounterColumn getCounter(ByteBuffer key, ColumnPath columnPath) throws HectorException; - + CounterColumn getCounter(String key, ColumnPath columnPath) throws HectorException; /** @@ -102,10 +102,10 @@ SuperColumn getSuperColumn(ByteBuffer key, ColumnPath columnPath, boolean revers */ List getSlice(ByteBuffer key, ColumnParent columnParent, SlicePredicate predicate) throws HectorException; - + List getSlice(String key, ColumnParent columnParent, SlicePredicate predicate) throws HectorException; - + /** * Get the group of counter columns contained by columnParent. * @@ -115,7 +115,7 @@ List getSlice(String key, ColumnParent columnParent, SlicePredicate pred */ List getCounterSlice(ByteBuffer key, ColumnParent columnParent, SlicePredicate predicate) throws HectorException; - + public List getCounterSlice(String key, ColumnParent columnParent, SlicePredicate predicate) throws HectorException; @@ -173,12 +173,12 @@ Map> multigetSuperSlice(List keys, void insert(String key, ColumnPath columnPath, ByteBuffer value) throws HectorException; void insert(String key, ColumnPath columnPath, ByteBuffer value, long timestamp) throws HectorException; - + /** * Add a counter with CL.ONE */ void addCounter(ByteBuffer key, ColumnParent columnParent, CounterColumn counterColumn) throws HectorException; - + /** * Add a counter with CL.ONE */ @@ -206,9 +206,9 @@ Map> multigetSuperSlice(List keys, void remove(String key, ColumnPath columnPath) throws HectorException; void remove(String key, ColumnPath columnPath, long timestamp) throws HectorException; - + void removeCounter(ByteBuffer key, ColumnPath columnPath) throws HectorException; - + void removeCounter(String key, ColumnPath columnPath) throws HectorException; diff --git a/core/src/main/java/me/prettyprint/cassandra/service/Operation.java b/core/src/main/java/me/prettyprint/cassandra/service/Operation.java index c101ec153..66fbe2cfa 100644 --- a/core/src/main/java/me/prettyprint/cassandra/service/Operation.java +++ b/core/src/main/java/me/prettyprint/cassandra/service/Operation.java @@ -30,17 +30,17 @@ public abstract class Operation { public FailoverPolicy failoverPolicy = FailoverPolicy.ON_FAIL_TRY_ALL_AVAILABLE; public ConsistencyLevelPolicy consistencyLevelPolicy; - + public String keyspaceName; public Map credentials; - + protected T result; private HectorException exception; private CassandraHost cassandraHost; protected long execTime; public final OperationType operationType; - + public Operation(OperationType operationType) { this.failCounter = operationType.equals(OperationType.READ) ? Counter.READ_FAIL : Counter.WRITE_FAIL; @@ -51,7 +51,7 @@ public Operation(OperationType operationType) { public Operation(OperationType operationType, Map credentials) { this(operationType, FailoverPolicy.ON_FAIL_TRY_ALL_AVAILABLE, null, credentials); } - + public Operation(OperationType operationType, FailoverPolicy failoverPolicy, String keyspaceName, Map credentials) { this.failCounter = operationType.equals(OperationType.READ) ? Counter.READ_FAIL : Counter.WRITE_FAIL; @@ -61,8 +61,8 @@ public Operation(OperationType operationType, FailoverPolicy failoverPolicy, Str this.keyspaceName = keyspaceName; this.credentials = Collections.unmodifiableMap(credentials); } - - + + public void applyConnectionParams(String keyspace, ConsistencyLevelPolicy consistencyLevelPolicy, FailoverPolicy failoverPolicy, Map credentials) { // TODO this is a first step. must be cleaned up. @@ -85,7 +85,7 @@ public T getResult() { // TODO remove in favor of getExecutionResult return result; } - + public ExecutionResult getExecutionResult() { return new ExecutionResult(result, execTime, cassandraHost); } @@ -113,10 +113,10 @@ public boolean hasException() { public HectorException getException() { return exception; } - + public CassandraHost getCassandraHost() { return this.cassandraHost; } - + } diff --git a/core/src/main/java/me/prettyprint/cassandra/service/OperationType.java b/core/src/main/java/me/prettyprint/cassandra/service/OperationType.java index ef9b28f68..a97e994f3 100644 --- a/core/src/main/java/me/prettyprint/cassandra/service/OperationType.java +++ b/core/src/main/java/me/prettyprint/cassandra/service/OperationType.java @@ -4,7 +4,7 @@ * Specifies the "type" of operation - read or write. * It's used for Speed4j, so should be in sync with hectorLog4j.xml * @author Ran Tavory (ran@outbain.com) - * + * */ public enum OperationType { /** Read operations*/ diff --git a/core/src/main/java/me/prettyprint/cassandra/service/StringKeyIterator.java b/core/src/main/java/me/prettyprint/cassandra/service/StringKeyIterator.java index 1cddbd872..a8ac4fc6c 100644 --- a/core/src/main/java/me/prettyprint/cassandra/service/StringKeyIterator.java +++ b/core/src/main/java/me/prettyprint/cassandra/service/StringKeyIterator.java @@ -4,7 +4,7 @@ import me.prettyprint.hector.api.Keyspace; /** - * This class returns each key in the specified Column Family as an Iterator. You + * This class returns each key in the specified Column Family as an Iterator. You * can use this class in a for loop without the overhead of first storing each * key in a large array. This is a convenience class for KeyIterator when the key * is a String. diff --git a/core/src/main/java/me/prettyprint/cassandra/service/ThriftCluster.java b/core/src/main/java/me/prettyprint/cassandra/service/ThriftCluster.java index b47e3100e..f03e6dae9 100644 --- a/core/src/main/java/me/prettyprint/cassandra/service/ThriftCluster.java +++ b/core/src/main/java/me/prettyprint/cassandra/service/ThriftCluster.java @@ -72,7 +72,7 @@ public String execute(Cassandra.Client cassandra) throws HectorException { public String addColumnFamily(final ColumnFamilyDefinition cfdef) throws HectorException { Operation op = new Operation(OperationType.META_WRITE, FailoverPolicy.ON_FAIL_TRY_ALL_AVAILABLE, - cfdef.getKeyspaceName(), + cfdef.getKeyspaceName(), getCredentials()) { @Override public String execute(Cassandra.Client cassandra) throws HectorException { @@ -91,7 +91,7 @@ public String execute(Cassandra.Client cassandra) throws HectorException { public String updateColumnFamily(final ColumnFamilyDefinition cfdef) throws HectorException { Operation op = new Operation(OperationType.META_WRITE, FailoverPolicy.ON_FAIL_TRY_ALL_AVAILABLE, - cfdef.getKeyspaceName(), + cfdef.getKeyspaceName(), getCredentials()) { @Override public String execute(Cassandra.Client cassandra) throws HectorException { @@ -105,7 +105,7 @@ public String execute(Cassandra.Client cassandra) throws HectorException { connectionManager.operateWithFailover(op); return op.getResult(); } - + @Override public String addKeyspace(final KeyspaceDefinition ksdef) throws HectorException { Operation op = new Operation(OperationType.META_WRITE, getCredentials()) { diff --git a/core/src/main/java/me/prettyprint/cassandra/service/ThriftColumnDef.java b/core/src/main/java/me/prettyprint/cassandra/service/ThriftColumnDef.java index 6f4328bc8..4467d0718 100644 --- a/core/src/main/java/me/prettyprint/cassandra/service/ThriftColumnDef.java +++ b/core/src/main/java/me/prettyprint/cassandra/service/ThriftColumnDef.java @@ -26,9 +26,9 @@ public ThriftColumnDef(ColumnDef cd) { name = cd.name; validationClass = cd.validation_class; indexType = indexTypeFromThrift(cd.index_type); - indexName = cd.index_name; + indexName = cd.index_name; } - + public ThriftColumnDef(ColumnDefinition columnDefinition) { name = columnDefinition.getName(); validationClass = columnDefinition.getValidationClass(); @@ -107,7 +107,7 @@ private IndexType indexTypeToThrift(ColumnIndexType indexType2) { throw new RuntimeException("Unknown ColumnIndexType value: " + indexType2); } } - + @Override public String toString() { return ToStringBuilder.reflectionToString(this, ToStringStyle.SHORT_PREFIX_STYLE); diff --git a/core/src/main/java/me/prettyprint/cassandra/service/ThriftKsDef.java b/core/src/main/java/me/prettyprint/cassandra/service/ThriftKsDef.java index 8904f7e86..ef2e6560b 100644 --- a/core/src/main/java/me/prettyprint/cassandra/service/ThriftKsDef.java +++ b/core/src/main/java/me/prettyprint/cassandra/service/ThriftKsDef.java @@ -47,7 +47,7 @@ public ThriftKsDef(String keyspaceName) { setReplicationFactor(1); this.strategyClass = DEF_STRATEGY_CLASS; } - + public ThriftKsDef(KeyspaceDefinition keyspaceDefinition) { name = keyspaceDefinition.getName(); strategyClass = keyspaceDefinition.getStrategyClass(); @@ -93,7 +93,7 @@ public List getCfDefs() { } public KsDef toThrift() { - KsDef def = new KsDef(name, strategyClass, ThriftCfDef.toThriftList(cfDefs)); + KsDef def = new KsDef(name, strategyClass, ThriftCfDef.toThriftList(cfDefs)); def.setStrategy_options(strategyOptions); return def; } diff --git a/core/src/main/java/me/prettyprint/cassandra/service/VirtualKeyspaceServiceImpl.java b/core/src/main/java/me/prettyprint/cassandra/service/VirtualKeyspaceServiceImpl.java index 39c810d20..179b1e356 100644 --- a/core/src/main/java/me/prettyprint/cassandra/service/VirtualKeyspaceServiceImpl.java +++ b/core/src/main/java/me/prettyprint/cassandra/service/VirtualKeyspaceServiceImpl.java @@ -112,7 +112,7 @@ public List getSlice(ByteBuffer key, ColumnParent columnParent, return super.getSlice(ps.toByteBuffer(key), columnParent, predicate); } - + @Override public List getCounterSlice(ByteBuffer key, ColumnParent columnParent, SlicePredicate predicate) throws HectorException { diff --git a/core/src/main/java/me/prettyprint/cassandra/service/spring/HectorTemplate.java b/core/src/main/java/me/prettyprint/cassandra/service/spring/HectorTemplate.java index 1b6d92b19..824aa06a7 100644 --- a/core/src/main/java/me/prettyprint/cassandra/service/spring/HectorTemplate.java +++ b/core/src/main/java/me/prettyprint/cassandra/service/spring/HectorTemplate.java @@ -117,7 +117,7 @@ HColumn createColumn(N name, V value, long clock, Serializer nam * Creates a column with the clock of now. */ HColumn createColumn(N name, V value); - + /** * Creates a column with the specified name/value and clock. */ diff --git a/core/src/main/java/me/prettyprint/cassandra/service/spring/HectorTemplateImpl.java b/core/src/main/java/me/prettyprint/cassandra/service/spring/HectorTemplateImpl.java index ab2196096..4b11e1c79 100644 --- a/core/src/main/java/me/prettyprint/cassandra/service/spring/HectorTemplateImpl.java +++ b/core/src/main/java/me/prettyprint/cassandra/service/spring/HectorTemplateImpl.java @@ -217,7 +217,7 @@ public HColumn createColumn(N name, V value, long clock, public HColumn createColumn(N name, V value) { return new HColumnImpl(name, value, createClock()); } - + @Override public HColumn createColumn(N name, V value, long clock) { return new HColumnImpl(name, value, clock); diff --git a/core/src/main/java/me/prettyprint/cassandra/service/template/AbstractColumnFamilyTemplate.java b/core/src/main/java/me/prettyprint/cassandra/service/template/AbstractColumnFamilyTemplate.java index b851bbd60..3c0c50de5 100644 --- a/core/src/main/java/me/prettyprint/cassandra/service/template/AbstractColumnFamilyTemplate.java +++ b/core/src/main/java/me/prettyprint/cassandra/service/template/AbstractColumnFamilyTemplate.java @@ -29,7 +29,7 @@ public class AbstractColumnFamilyTemplate { protected ColumnParent columnParent; protected HSlicePredicate activeSlicePredicate; protected ColumnFactory columnFactory; - + /** The serializer for a standard column name or a super-column name */ protected Serializer topSerializer; @@ -53,7 +53,7 @@ public class AbstractColumnFamilyTemplate { * generated by Hector is used */ protected Long clock; - + protected ExceptionsTranslator exceptionsTranslator; public AbstractColumnFamilyTemplate(Keyspace keyspace, String columnFamily, @@ -61,7 +61,7 @@ public AbstractColumnFamilyTemplate(Keyspace keyspace, String columnFamily, this(keyspace, columnFamily, keySerializer, topSerializer, HFactory .createMutator(keyspace, keySerializer)); } - + public AbstractColumnFamilyTemplate(Keyspace keyspace, String columnFamily, Serializer keySerializer, Serializer topSerializer, Mutator mutator) { @@ -81,7 +81,7 @@ public AbstractColumnFamilyTemplate(Keyspace keyspace, String columnFamily, /** - * Add a column to the static set of columns which will be used in constructing + * Add a column to the static set of columns which will be used in constructing * the single-argument form of slicing operations * @param columnName * @param valueSerializer @@ -91,7 +91,7 @@ public AbstractColumnFamilyTemplate addColumn(N columnName, Serializer v activeSlicePredicate.addColumnName(columnName); return this; } - + /** * Get the value serializer for a given column. Returns null if none found * @param columnName @@ -100,8 +100,8 @@ public AbstractColumnFamilyTemplate addColumn(N columnName, Serializer v public Serializer getValueSerializer(N columnName) { return columnValueSerializers.get(columnName); } - - + + public boolean isBatched() { return batched; } @@ -123,7 +123,7 @@ public Serializer getTopSerializer() { return topSerializer; } - public MutationResult executeBatch() { + public MutationResult executeBatch() { MutationResult result = mutator.execute(); mutator.discardPendingMutations(); return result; @@ -148,17 +148,17 @@ public void setClock(Long clock) { public long getEffectiveClock() { return clock != null ? clock.longValue() : keyspace.createClock(); - } + } public void setExceptionsTranslator(ExceptionsTranslator exceptionsTranslator) { this.exceptionsTranslator = exceptionsTranslator; - } + } public void setColumnFactory(ColumnFactory columnFactory) { this.columnFactory = columnFactory; } - protected MutationResult executeIfNotBatched() { + protected MutationResult executeIfNotBatched() { return !isBatched() ? executeBatch() : null; } diff --git a/core/src/main/java/me/prettyprint/cassandra/service/template/AbstractResultWrapper.java b/core/src/main/java/me/prettyprint/cassandra/service/template/AbstractResultWrapper.java index 674d46d39..e0c4a848b 100644 --- a/core/src/main/java/me/prettyprint/cassandra/service/template/AbstractResultWrapper.java +++ b/core/src/main/java/me/prettyprint/cassandra/service/template/AbstractResultWrapper.java @@ -20,17 +20,17 @@ * overlap in needs for both standard and super queries. This class consolidates * what they have in common. All data is read into ByteBuffers and translated to * a primitive type when requested. - * + * * This class is a non-static inner class which inherits the Java generic * parameters of it's containing ColumnFamilyTemplate instance. This allows it to * inherit the parameter from ColumnFamilyTemplate. - * + * * The parameters allows this to be used by standard and super column * queries - * + * * @author david * @author zznate - * @param + * @param * the type of the key * @param * the standard column name type or the super column's child column @@ -41,7 +41,7 @@ public abstract class AbstractResultWrapper implements ColumnFamilyResult< protected Serializer keySerializer; protected Serializer columnNameSerializer; protected ResultStatus resultStatus; - + public AbstractResultWrapper(Serializer keySerializer, Serializer columnNameSerializer, ResultStatus resultStatus) { this.keySerializer = keySerializer; this.columnNameSerializer = columnNameSerializer; @@ -78,12 +78,12 @@ public byte[] getByteArray(N columnName) { public Date getDate(N columnName) { return DateSerializer.get().fromByteBuffer(getColumnValue(columnName)); } - + @Override public long getExecutionTimeMicro() { return resultStatus.getExecutionTimeMicro(); - } - + } + @Override public long getExecutionTimeNano() { return resultStatus.getExecutionTimeNano(); @@ -93,5 +93,5 @@ public long getExecutionTimeNano() { public CassandraHost getHostUsed() { return resultStatus.getHostUsed(); } - + } diff --git a/core/src/main/java/me/prettyprint/cassandra/service/template/AbstractTemplateUpdater.java b/core/src/main/java/me/prettyprint/cassandra/service/template/AbstractTemplateUpdater.java index f23ef0938..5ef0c67ae 100644 --- a/core/src/main/java/me/prettyprint/cassandra/service/template/AbstractTemplateUpdater.java +++ b/core/src/main/java/me/prettyprint/cassandra/service/template/AbstractTemplateUpdater.java @@ -11,34 +11,34 @@ public abstract class AbstractTemplateUpdater { protected int keyPos = 0; protected ColumnFactory columnFactory; protected AbstractColumnFamilyTemplate template; - + public AbstractTemplateUpdater(AbstractColumnFamilyTemplate template, ColumnFactory columnFactory) { this.template = template; this.columnFactory = columnFactory; } - + public AbstractTemplateUpdater addKey(K key) { if ( keys == null ) { - keys = new ArrayList(); + keys = new ArrayList(); } else { keyPos++; } keys.add(key); - + return this; } - + /** * @return Give the updater access to the current key if it needs it */ public K getCurrentKey() { return keys.get(keyPos); } - + /** * To be overridden by folks choosing to add their own functionality. Default is a no-op. */ public void update() { - + } } diff --git a/core/src/main/java/me/prettyprint/cassandra/service/template/CassandraClusterFactory.java b/core/src/main/java/me/prettyprint/cassandra/service/template/CassandraClusterFactory.java index 917342989..3d8e68bfe 100644 --- a/core/src/main/java/me/prettyprint/cassandra/service/template/CassandraClusterFactory.java +++ b/core/src/main/java/me/prettyprint/cassandra/service/template/CassandraClusterFactory.java @@ -7,11 +7,11 @@ import org.slf4j.LoggerFactory; /** - * Factory to remove Cassandra configuration concerns from DAO objects. + * Factory to remove Cassandra configuration concerns from DAO objects. * This is invoked via a spring factory method that allows injection of the - * Hector Cluster object into the DAO. + * Hector Cluster object into the DAO. *

    - * + * *

    * @author david * @since Jan 14, 2011 @@ -20,11 +20,11 @@ public class CassandraClusterFactory { static final Logger LOGGER = LoggerFactory.getLogger( CassandraClusterFactory.class ); - + public static Cluster getInstance( String name, String host, int port ) { LOGGER.debug( "getInstance: creating cluster name=" + name + ", host=" + host + ", port=" + port ); return HFactory.getOrCreateCluster( name, host + ":" + port ); - + } } diff --git a/core/src/main/java/me/prettyprint/cassandra/service/template/ColumnFamilyResult.java b/core/src/main/java/me/prettyprint/cassandra/service/template/ColumnFamilyResult.java index e81dac7a9..fbad6d1c5 100644 --- a/core/src/main/java/me/prettyprint/cassandra/service/template/ColumnFamilyResult.java +++ b/core/src/main/java/me/prettyprint/cassandra/service/template/ColumnFamilyResult.java @@ -12,10 +12,10 @@ /** * A common interface for access to the resuls of a query of either a standard or super column family. - * There are different implementations of this which hide the differences requires of standar/super + * There are different implementations of this which hide the differences requires of standar/super * column families. As this interface inherits from {@link ResultStatus}, results will also provide * execution details. - * + * * @author david * @author zznate * @param @@ -36,12 +36,12 @@ public interface ColumnFamilyResult extends Iterator getColumnNames(); - + HColumn getColumn(N columnName); - + boolean hasResults(); - + } diff --git a/core/src/main/java/me/prettyprint/cassandra/service/template/ColumnFamilyResultWrapper.java b/core/src/main/java/me/prettyprint/cassandra/service/template/ColumnFamilyResultWrapper.java index b596eb967..45712b1f4 100644 --- a/core/src/main/java/me/prettyprint/cassandra/service/template/ColumnFamilyResultWrapper.java +++ b/core/src/main/java/me/prettyprint/cassandra/service/template/ColumnFamilyResultWrapper.java @@ -22,26 +22,26 @@ /** * Wraps the results with as an Iterator. The underlying Iterator has already been advanced * to the first row upon construction. - * + * * @author zznate */ public class ColumnFamilyResultWrapper extends AbstractResultWrapper { - + private Map> columns = new LinkedHashMap>(); private Iterator>> rows; private Map.Entry> entry; private ExecutionResult>> executionResult; private boolean hasEntries; - + public ColumnFamilyResultWrapper(Serializer keySerializer, Serializer columnNameSerializer, ExecutionResult>> executionResult) { - super(keySerializer, columnNameSerializer, executionResult); - this.rows = executionResult.get().entrySet().iterator(); + super(keySerializer, columnNameSerializer, executionResult); + this.rows = executionResult.get().entrySet().iterator(); next(); hasEntries = getColumnNames() != null && getColumnNames().size() > 0; } - + /** * All the column names we know about in the current iterator position * @return @@ -49,7 +49,7 @@ public ColumnFamilyResultWrapper(Serializer keySerializer, public Collection getColumnNames() { return columns.keySet(); } - + public ByteBuffer getColumnValue( N columnName) { HColumn col = getColumn( columnName ); return col != null ? col.getValue() : null; @@ -58,44 +58,44 @@ public ByteBuffer getColumnValue( N columnName) { public HColumn getColumn( N columnName ) { return columns.get( columnName ); } - + private void applyToRow(List cosclist) { - + for (Iterator iterator = cosclist.iterator(); iterator.hasNext();) { - ColumnOrSuperColumn cosc = iterator.next(); + ColumnOrSuperColumn cosc = iterator.next(); if ( cosc.isSetSuper_column() ) { applySuper(cosc); } else { - applyStandard(cosc.getColumn()); + applyStandard(cosc.getColumn()); } - + iterator.remove(); } } - + private void applySuper(ColumnOrSuperColumn cosc) { Iterator tcolumns = cosc.getSuper_column().getColumnsIterator(); while ( tcolumns.hasNext() ) { applyStandard(tcolumns.next()); - } + } } - + private void applyStandard(Column cosc) { N colName = columnNameSerializer.fromByteBuffer(cosc.name.duplicate()); HColumn column = columns.get(colName); - + if ( column == null ) { column = new HColumnImpl(cosc, columnNameSerializer, ByteBufferSerializer.get()); } else { ((HColumnImpl)column).apply(cosc); } - columns.put(colName, column); + columns.put(colName, column); } @Override - public K getKey() { + public K getKey() { return keySerializer.fromByteBuffer(entry.getKey()); } @@ -104,11 +104,11 @@ public ColumnFamilyResult next() { if ( !hasNext() ) { throw new NoSuchElementException("No more rows left on this HColumnFamily"); } - entry = rows.next(); + entry = rows.next(); applyToRow(entry.getValue()); return this; } - + @Override public boolean hasNext() { return rows.hasNext(); @@ -120,10 +120,10 @@ public void remove() { } @Override - public boolean hasResults() { + public boolean hasResults() { return hasEntries; } - - + + } diff --git a/core/src/main/java/me/prettyprint/cassandra/service/template/ColumnFamilyRowMapper.java b/core/src/main/java/me/prettyprint/cassandra/service/template/ColumnFamilyRowMapper.java index 2d76144e8..d4daf7467 100644 --- a/core/src/main/java/me/prettyprint/cassandra/service/template/ColumnFamilyRowMapper.java +++ b/core/src/main/java/me/prettyprint/cassandra/service/template/ColumnFamilyRowMapper.java @@ -2,7 +2,7 @@ /** * Converts the contents of a standard column family row into an object. - * + * * @author david * @since Mar 10, 2011 * @param diff --git a/core/src/main/java/me/prettyprint/cassandra/service/template/ColumnFamilyTemplate.java b/core/src/main/java/me/prettyprint/cassandra/service/template/ColumnFamilyTemplate.java index 3038a7034..cd5bb3467 100644 --- a/core/src/main/java/me/prettyprint/cassandra/service/template/ColumnFamilyTemplate.java +++ b/core/src/main/java/me/prettyprint/cassandra/service/template/ColumnFamilyTemplate.java @@ -35,20 +35,20 @@ * constantly passed for every operation on the column family. These include the * keyspace, column family name, key serializer, and the column name serializer * (for standard column name or the super column name). - * + * * The Java generic types of the ColumnFamilyTemplate class itself are limited to * the key and column name type. It defers the generic types for super column * child types to the individual update/query operation. - * + * * @author david * @author zznate * @param * The column family key type * @param - * The column family name type + * The column family name type */ public abstract class ColumnFamilyTemplate extends AbstractColumnFamilyTemplate { - + public ColumnFamilyTemplate(Keyspace keyspace, String columnFamily, Serializer keySerializer, Serializer topSerializer) { super(keyspace, columnFamily, keySerializer, topSerializer); @@ -79,17 +79,17 @@ public ColumnFamilyUpdater createUpdater(K key) { updater.addKey(key); return updater; } - + public void update(ColumnFamilyUpdater updater) { updater.update(); executeIfNotBatched(); } - - + + /** * Checks if there are any columns at a row specified by key in a standard * column family - * + * * @param key * @return true if columns exist */ @@ -110,7 +110,7 @@ public int countColumns(K key) { /** * Counts columns in the specified range of a standard column family - * + * * @param key * @param start * @param end @@ -133,7 +133,7 @@ public ColumnFamilyResult queryColumns(K key) { public ColumnFamilyResult queryColumns(Iterable keys) { return doExecuteMultigetSlice(keys, activeSlicePredicate); } - + @SuppressWarnings("unchecked") public T queryColumns(K key, ColumnFamilyRowMapper mapper) { return queryColumns(key, activeSlicePredicate, mapper); @@ -142,7 +142,7 @@ public T queryColumns(K key, ColumnFamilyRowMapper mapper) { /** * Queries a range of columns at the given key and maps them to an object of * type OBJ using the given mapping object - * + * * @param * @param key * @param start @@ -158,7 +158,7 @@ public T queryColumns(K key, HSlicePredicate predicate, /** * Queries all columns at a given key and maps them to an object of type OBJ * using the given mapping object - * + * * @param * @param key * @param columns @@ -169,18 +169,18 @@ public T queryColumns(K key, HSlicePredicate predicate, public T queryColumns(K key, List columns, ColumnFamilyRowMapper mapper) { HSlicePredicate predicate = new HSlicePredicate(topSerializer); - predicate.setColumnNames(columns); + predicate.setColumnNames(columns); return doExecuteSlice(key, predicate, mapper); } public MappedColumnFamilyResult queryColumns(Iterable keys, - ColumnFamilyRowMapper mapper) { + ColumnFamilyRowMapper mapper) { return doExecuteMultigetSlice(keys, activeSlicePredicate, mapper); } public MappedColumnFamilyResult queryColumns(Iterable keys, - HSlicePredicate predicate, ColumnFamilyRowMapper mapper) { + HSlicePredicate predicate, ColumnFamilyRowMapper mapper) { return doExecuteMultigetSlice(keys, predicate, mapper); } @@ -211,16 +211,16 @@ public HColumn querySingleColumn(K key, N columnName, } //-------------------------- delegation methods ---------------------------- - + protected abstract T doExecuteSlice(K key, HSlicePredicate predicate, ColumnFamilyRowMapper mapper); - + protected abstract ColumnFamilyResult doExecuteSlice(final K key, final HSlicePredicate workingSlicePredicate); - + protected abstract ColumnFamilyResult doExecuteMultigetSlice(final Iterable keys, final HSlicePredicate workingSlicePredicate); - protected abstract MappedColumnFamilyResult doExecuteMultigetSlice(final Iterable keys, + protected abstract MappedColumnFamilyResult doExecuteMultigetSlice(final Iterable keys, final HSlicePredicate workingSlicePredicate, final ColumnFamilyRowMapper mapper); - + } diff --git a/core/src/main/java/me/prettyprint/cassandra/service/template/ColumnFamilyUpdater.java b/core/src/main/java/me/prettyprint/cassandra/service/template/ColumnFamilyUpdater.java index e75c37376..a53d53348 100644 --- a/core/src/main/java/me/prettyprint/cassandra/service/template/ColumnFamilyUpdater.java +++ b/core/src/main/java/me/prettyprint/cassandra/service/template/ColumnFamilyUpdater.java @@ -22,30 +22,30 @@ * contents of an object. This would likely by implemented as an anonymous inner * class with access to a final object in scope. It would update the given row * with the object's data. - * + * * For more complex behaviour, subclasses should implementat update() to simply make * consecutive calls to various set****() methods which already have the * contextual information they need to update the correct row. - * + * * The downside of this approach is that the updater is essentially stateful and * cannot be used concurrently. The alternative is to pass an object in to * update() as a parameter with the setter methods, leaving the updater to be * stateless. - * + * * @author david * @author zznate - * + * * @param * the key's data type * @param * the standard or super column's data type */ public class ColumnFamilyUpdater extends AbstractTemplateUpdater { - + public ColumnFamilyUpdater(ColumnFamilyTemplate template, ColumnFactory columnFactory) { super(template, columnFactory); } - + public void deleteColumn(N columnName) { template.getMutator().addDeletion(getCurrentKey(), template.getColumnFamily(), columnName, template.getTopSerializer()); @@ -74,13 +74,13 @@ public void setInteger(N columnName, Integer value) { template.getTopSerializer(), IntegerSerializer.get()); template.getMutator().addInsertion(getCurrentKey(), template.getColumnFamily(), column); } - + public void setDouble(N columnName, Double value) { HColumn column = columnFactory.createColumn(columnName, value, template.getTopSerializer(), DoubleSerializer.get()); template.getMutator().addInsertion(getCurrentKey(), template.getColumnFamily(), column); - } - + } + public void setBoolean(N columnName, Boolean value) { HColumn column = columnFactory.createColumn(columnName, value, template.getTopSerializer(), BooleanSerializer.get()); @@ -98,13 +98,13 @@ public void setByteBuffer(N columnName, ByteBuffer value) { template.getTopSerializer(), ByteBufferSerializer.get()); template.getMutator().addInsertion(getCurrentKey(), template.getColumnFamily(), column); } - + public void setDate(N columnName, Date value) { HColumn column = columnFactory.createColumn(columnName, value, template.getTopSerializer(), DateSerializer.get()); template.getMutator().addInsertion(getCurrentKey(), template.getColumnFamily(), column); } - + public void setValue(N columnName, V value, Serializer serializer) { HColumn column = columnFactory.createColumn(columnName, value, template.getTopSerializer(), serializer); diff --git a/core/src/main/java/me/prettyprint/cassandra/service/template/MappedColumnFamilyResult.java b/core/src/main/java/me/prettyprint/cassandra/service/template/MappedColumnFamilyResult.java index f1fc54cb3..ad18f8192 100644 --- a/core/src/main/java/me/prettyprint/cassandra/service/template/MappedColumnFamilyResult.java +++ b/core/src/main/java/me/prettyprint/cassandra/service/template/MappedColumnFamilyResult.java @@ -3,5 +3,5 @@ public interface MappedColumnFamilyResult extends ColumnFamilyResult { V getRow(); - + } diff --git a/core/src/main/java/me/prettyprint/cassandra/service/template/MappedColumnFamilyResultWrapper.java b/core/src/main/java/me/prettyprint/cassandra/service/template/MappedColumnFamilyResultWrapper.java index d191a0fca..955ce9d74 100644 --- a/core/src/main/java/me/prettyprint/cassandra/service/template/MappedColumnFamilyResultWrapper.java +++ b/core/src/main/java/me/prettyprint/cassandra/service/template/MappedColumnFamilyResultWrapper.java @@ -12,15 +12,15 @@ public class MappedColumnFamilyResultWrapper extends ColumnFamilyResultWrapper implements MappedColumnFamilyResult{ private ColumnFamilyRowMapper rowMapper; - + public MappedColumnFamilyResultWrapper(Serializer keySerializer, Serializer columnNameSerializer, ExecutionResult>> executionResult, ColumnFamilyRowMapper mapper) { - super(keySerializer, columnNameSerializer, executionResult); + super(keySerializer, columnNameSerializer, executionResult); } @Override - public V getRow() { + public V getRow() { return rowMapper.mapRow(this); } diff --git a/core/src/main/java/me/prettyprint/cassandra/service/template/MappedSuperCfResult.java b/core/src/main/java/me/prettyprint/cassandra/service/template/MappedSuperCfResult.java index a94758a35..e771efd90 100644 --- a/core/src/main/java/me/prettyprint/cassandra/service/template/MappedSuperCfResult.java +++ b/core/src/main/java/me/prettyprint/cassandra/service/template/MappedSuperCfResult.java @@ -1,6 +1,6 @@ package me.prettyprint.cassandra.service.template; public interface MappedSuperCfResult extends SuperCfResult { - + V getRow(); } diff --git a/core/src/main/java/me/prettyprint/cassandra/service/template/MappedSuperCfResultWrapper.java b/core/src/main/java/me/prettyprint/cassandra/service/template/MappedSuperCfResultWrapper.java index d07612ecf..e0773d3c7 100644 --- a/core/src/main/java/me/prettyprint/cassandra/service/template/MappedSuperCfResultWrapper.java +++ b/core/src/main/java/me/prettyprint/cassandra/service/template/MappedSuperCfResultWrapper.java @@ -11,7 +11,7 @@ public class MappedSuperCfResultWrapper extends SuperCfResultWrapper implements MappedSuperCfResult { - + private SuperCfRowMapper rowMapper; public MappedSuperCfResultWrapper( @@ -25,10 +25,10 @@ public MappedSuperCfResultWrapper( } @Override - public V getRow() { + public V getRow() { return rowMapper.mapRow(this); } - - + + } diff --git a/core/src/main/java/me/prettyprint/cassandra/service/template/SuperCfResult.java b/core/src/main/java/me/prettyprint/cassandra/service/template/SuperCfResult.java index c647a5739..9c50621b3 100644 --- a/core/src/main/java/me/prettyprint/cassandra/service/template/SuperCfResult.java +++ b/core/src/main/java/me/prettyprint/cassandra/service/template/SuperCfResult.java @@ -13,7 +13,7 @@ * Holds the result for the contents of a super column. This interface add * access to the current super column name since this may be a property of the * object being mapped into. - * + * * @author david * @since Mar 10, 2011 * @param @@ -24,9 +24,9 @@ */ public interface SuperCfResult extends ColumnFamilyResult { // TODO remove. this no loger makes sense with many-to-one on a row - + Collection getSuperColumns(); - + K getKey(); UUID getUUID(SN sColumnName, N columnName); @@ -40,15 +40,15 @@ public interface SuperCfResult extends ColumnFamilyResult { Boolean getBoolean(SN sColumnName, N columnName); byte[] getByteArray(SN sColumnName, N columnName); - + ByteBuffer getByteBuffer(SN sColumnName, N columnName); - Date getDate(SN sColumnName, N columnName); - + Date getDate(SN sColumnName, N columnName); + void applySuperColumn(SN sColumnName); - + SN getActiveSuperColumn(); - + @Override SuperCfResult next(); } diff --git a/core/src/main/java/me/prettyprint/cassandra/service/template/SuperCfResultWrapper.java b/core/src/main/java/me/prettyprint/cassandra/service/template/SuperCfResultWrapper.java index c0bce21d7..1d4469a86 100644 --- a/core/src/main/java/me/prettyprint/cassandra/service/template/SuperCfResultWrapper.java +++ b/core/src/main/java/me/prettyprint/cassandra/service/template/SuperCfResultWrapper.java @@ -30,14 +30,14 @@ import org.slf4j.LoggerFactory; /** - * Provides access to the current row of data during super column queries. - * + * Provides access to the current row of data during super column queries. + * * @author zznate * @param the super column's sub column name type */ public class SuperCfResultWrapper extends AbstractResultWrapper implements SuperCfResult { private static final Logger log = LoggerFactory.getLogger(SuperCfResultWrapper.class); - + private Map>> columns = new LinkedHashMap>>(); private Iterator>> rows; private Map.Entry> entry; @@ -48,36 +48,36 @@ public class SuperCfResultWrapper extends AbstractResultWrapper imp private boolean hasEntries; private Serializer sNameSerializer; - + public SuperCfResultWrapper(Serializer keySerializer, Serializer sNameSerializer, Serializer subSerializer, ExecutionResult>> executionResult) { super(keySerializer, subSerializer, executionResult); this.sNameSerializer = sNameSerializer; - this.rows = executionResult.get().entrySet().iterator(); + this.rows = executionResult.get().entrySet().iterator(); next(); hasEntries = getSuperColumns() != null && getSuperColumns().size() > 0; - } + } @Override public SuperCfResult next() { if ( !hasNext() ) { throw new NoSuchElementException("No more rows left on this HColumnFamily"); } - entry = rows.next(); + entry = rows.next(); log.debug("found entry {} with value {}", getKey(), entry.getValue()); applyToRow(entry.getValue()); return this; } - + private void applyToRow(List cosclist) { superColumns = new ArrayList(cosclist.size()); for (Iterator iterator = cosclist.iterator(); iterator.hasNext();) { ColumnOrSuperColumn cosc = iterator.next(); SN sColName = sNameSerializer.fromByteBuffer(cosc.super_column.name); log.debug("cosc {}", cosc.super_column); - + superColumns.add(sColName); Iterator tcolumns = cosc.getSuper_column().getColumnsIterator(); Map> subColMap = new LinkedHashMap>(); @@ -88,23 +88,23 @@ private void applyToRow(List cosclist) { columns.put(sColName, subColMap); } } - - + + public List getSuperColumns() { return superColumns; } - - + + @Override public ByteBuffer getColumnValue(N columnName) { HColumn col = getColumn( columnName ); - return col != null ? col.getValue() : null; + return col != null ? col.getValue() : null; } @Override public K getKey() { - return keySerializer.fromByteBuffer(entry.getKey()); + return keySerializer.fromByteBuffer(entry.getKey()); } @@ -127,7 +127,7 @@ public boolean hasNext() { @Override public void remove() { - rows.remove(); + rows.remove(); } private V extractType(SN sColumnName, N columnName, Serializer valueSerializer) { @@ -136,9 +136,9 @@ private V extractType(SN sColumnName, N columnName, Serializer valueSeria return valueSerializer.fromByteBuffer(map.get(columnName).getValue()); return null; } - + @Override - public Boolean getBoolean(SN sColumnName, N columnName) { + public Boolean getBoolean(SN sColumnName, N columnName) { return extractType(sColumnName, columnName, BooleanSerializer.get()); } @@ -184,7 +184,7 @@ public ByteBuffer getByteBuffer(SN sColumnName, N columnName) { return extractType(sColumnName, columnName, ByteBufferSerializer.get()); } - + @Override public SN getActiveSuperColumn() { return currentSuperColumn; @@ -201,7 +201,7 @@ public void applySuperColumn(SN sColumnName) { public boolean hasResults() { return hasEntries; } - - + + } diff --git a/core/src/main/java/me/prettyprint/cassandra/service/template/SuperCfRowMapper.java b/core/src/main/java/me/prettyprint/cassandra/service/template/SuperCfRowMapper.java index 97210c1cf..21c0e52fd 100644 --- a/core/src/main/java/me/prettyprint/cassandra/service/template/SuperCfRowMapper.java +++ b/core/src/main/java/me/prettyprint/cassandra/service/template/SuperCfRowMapper.java @@ -3,7 +3,7 @@ /** * Converts the contents of a given super column into an object. It is passed a * results object with the data of the current super column. - * + * * @author david * @since Mar 10, 2011 * @param diff --git a/core/src/main/java/me/prettyprint/cassandra/service/template/SuperCfTemplate.java b/core/src/main/java/me/prettyprint/cassandra/service/template/SuperCfTemplate.java index 8e9e25fc7..9aa75e8d1 100644 --- a/core/src/main/java/me/prettyprint/cassandra/service/template/SuperCfTemplate.java +++ b/core/src/main/java/me/prettyprint/cassandra/service/template/SuperCfTemplate.java @@ -34,11 +34,11 @@ public SuperCfTemplate(Keyspace keyspace, String columnFamily, public Serializer getSubSerializer() { return subSerializer; } - + /** * Checks if there are any columns at a row specified by key in a super column * family - * + * * @param key * @return true if columns exist */ @@ -49,7 +49,7 @@ public boolean isColumnsExist(K key) { /** * Checks if there are any columns at a row specified by key in a specific * super column - * + * * @param key * @param superColumnName * @param subSerializer @@ -72,7 +72,7 @@ public int countColumns(K key) { } /** - * + * * @param key * @param superColumnName * @return the number of child columns in a specified super column @@ -85,7 +85,7 @@ public int countSubColumns(K key, SN superColumnName) { /** * Counts columns in the specified range of a super column family - * + * * @param key * @param start * @param end @@ -104,7 +104,7 @@ 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 * @param key * @param superColumnName @@ -126,63 +126,63 @@ public int countSubColumns(K key, SN superColumnName, N start, } - + public HColumn querySingleSubColumn(K key, SN columnName, N subColumnName, Serializer valueSerializer) { - + SuperCfResult result = doExecuteSlice(key, columnName, activeSlicePredicate); - - if (result == null) { + + if (result == null) { return null; } - + HColumn origCol = result.getColumn(subColumnName); - + // TODO make this far less hacky if ( columnName == null || origCol == null ) { return null; } - - return new HColumnImpl(subColumnName, - valueSerializer.fromByteBuffer(origCol.getValue()), origCol.getClock(), + + return new HColumnImpl(subColumnName, + valueSerializer.fromByteBuffer(origCol.getValue()), origCol.getClock(), subSerializer, valueSerializer); } - + public SuperCfResult querySuperColumns(K key, List sColumnNames) { HSlicePredicate workingSlicePredicate = new HSlicePredicate(topSerializer); workingSlicePredicate.setColumnNames(sColumnNames); - return doExecuteSlice(key, null, workingSlicePredicate); + return doExecuteSlice(key, null, workingSlicePredicate); } - + public SuperCfResult querySuperColumns(List keys, List sColumnNames) { HSlicePredicate workingSlicePredicate = new HSlicePredicate(topSerializer); workingSlicePredicate.setColumnNames(sColumnNames); - return doExecuteMultigetSlice(keys, workingSlicePredicate); + return doExecuteMultigetSlice(keys, workingSlicePredicate); } - - public SuperCfResult querySuperColumns(List keys) { - return doExecuteMultigetSlice(keys, activeSlicePredicate); + + public SuperCfResult querySuperColumns(List keys) { + return doExecuteMultigetSlice(keys, activeSlicePredicate); } public T querySuperColumns(K key, List sColumnNames, SuperCfRowMapper mapper) { - + HSlicePredicate workingSlicePredicate = new HSlicePredicate(topSerializer); workingSlicePredicate.setColumnNames(sColumnNames); return mapper.mapRow(doExecuteSlice(key, null, workingSlicePredicate)); } - public SuperCfResult querySuperColumns(K key) { + public SuperCfResult querySuperColumns(K key) { return doExecuteSlice(key, null, activeSlicePredicate); } - + public SuperCfResult querySuperColumn(K key, SN sColumnName) { HSlicePredicate workingSlicePredicate = new HSlicePredicate(topSerializer); workingSlicePredicate.addColumnName(sColumnName); return doExecuteSlice(key, sColumnName, workingSlicePredicate); } - - public SuperCfUpdater createUpdater(K key, SN sColumnName) { + + public SuperCfUpdater createUpdater(K key, SN sColumnName) { return createUpdater(key).addSuperColumn(sColumnName); } @@ -191,13 +191,13 @@ public SuperCfUpdater createUpdater(K key) { updater.addKey(key); return updater; } - + public void update(SuperCfUpdater updater) { updater.updateInternal(); updater.update(); executeIfNotBatched(); } - + /** * Immediately delete the key and superColumn combination */ @@ -215,8 +215,8 @@ public void deleteRow(K key) { } protected abstract SuperCfResult doExecuteSlice(K key, SN sColumnName, HSlicePredicate predicate); - + protected abstract SuperCfResult doExecuteMultigetSlice(List keys, HSlicePredicate predicate); - + } diff --git a/core/src/main/java/me/prettyprint/cassandra/service/template/SuperCfUpdater.java b/core/src/main/java/me/prettyprint/cassandra/service/template/SuperCfUpdater.java index 61d70c035..4cf52ef5c 100644 --- a/core/src/main/java/me/prettyprint/cassandra/service/template/SuperCfUpdater.java +++ b/core/src/main/java/me/prettyprint/cassandra/service/template/SuperCfUpdater.java @@ -31,19 +31,19 @@ * contents of an object. This would likely by implemented as an anonymous inner * class with access to a final object in scope. It would update the given row * with the object's data. - * + * * This is currently implemented as an abstract base class instead of an * interface. This could change in the future. Being an abstract base class * allows CassandraTemplate to initialize this instance through package scope * field access. This means that implementation of update() simply makes * consecutive calls to various set****() methods which already have the * contextual information they need to update the correct row. - * + * * The downside of this approach is that the updater is essentially stateful and * cannot be used concurrently. The alternative is to pass an object in to * update() as a parameter with the setter methods, leaving the updater to be * stateless. - * + * * @author david * @since Mar 10, 2011 * @param @@ -62,17 +62,17 @@ public class SuperCfUpdater extends AbstractTemplateUpdater { private int sColPos; private HSuperColumnImpl activeColumn; private List subColumns; - + public SuperCfUpdater(SuperCfTemplate sTemplate, ColumnFactory columnFactory) { super((AbstractColumnFamilyTemplate) sTemplate, columnFactory); this.template = sTemplate; } - - - + + + @Override - public SuperCfUpdater addKey(K key) { - + public SuperCfUpdater addKey(K key) { + if ( keys != null && keys.size() > 0 ) { updateInternal(); } @@ -87,30 +87,30 @@ public SuperCfUpdater addKey(K key) { public SuperCfUpdater addSuperColumn(SN sColumnName) { if ( sColumnNames.size() > 0 ) { updateInternal(); - sColPos++; + sColPos++; } - + subColumns = new ArrayList(); - + sColumnNames.add(sColumnName); - - + + return this; } - + public SN getCurrentSuperColumn() { return sColumnNames.get(sColPos); } - + /** - * collapse the state of the active HSuperColumn + * collapse the state of the active HSuperColumn */ - void updateInternal() { + void updateInternal() { // HSuperColumnImpl needs a refactor, this construction is lame. // the value serializer is not used in HSuperColumnImpl, so this is safe for name if ( !subColumns.isEmpty() ) { log.debug("Adding column {} for key {} and cols {}", new Object[]{getCurrentSuperColumn(), getCurrentKey(), subColumns}); - HSuperColumnImpl column = new HSuperColumnImpl(getCurrentSuperColumn(), subColumns, + HSuperColumnImpl column = new HSuperColumnImpl(getCurrentSuperColumn(), subColumns, 0, template.getTopSerializer(), template.getSubSerializer(), TypeInferringSerializer.get()); template.getMutator().addInsertion(getCurrentKey(), template.getColumnFamily(), column); } @@ -121,14 +121,14 @@ void updateInternal() { * Deletes the super column and all of its sub columns */ public void deleteSuperColumn() { - //template.getMutator().addDeletion(getCurrentKey(), template.getColumnFamily(), - // getCurrentSuperColumn(), template.getTopSerializer()); - template.getMutator().addSuperDelete(getCurrentKey(), template.getColumnFamily(), - getCurrentSuperColumn(), template.getTopSerializer()); + //template.getMutator().addDeletion(getCurrentKey(), template.getColumnFamily(), + // getCurrentSuperColumn(), template.getTopSerializer()); + template.getMutator().addSuperDelete(getCurrentKey(), template.getColumnFamily(), + getCurrentSuperColumn(), template.getTopSerializer()); } - + public void deleteSubColumn(N columnName) { - template.getMutator().addSubDelete(getCurrentKey(), template.getColumnFamily(), + template.getMutator().addSubDelete(getCurrentKey(), template.getColumnFamily(), getCurrentSuperColumn(), columnName, template.getTopSerializer(), template.getSubSerializer()); } @@ -154,7 +154,7 @@ public void setInteger(N subColumnName, Integer value) { public void setBoolean(N subColumnName, Boolean value) { subColumns.add(columnFactory.createColumn(subColumnName, value, template.getEffectiveClock(), - template.getSubSerializer(), BooleanSerializer.get())); + template.getSubSerializer(), BooleanSerializer.get())); } public void setByteArray(N subColumnName, byte[] value) { @@ -166,17 +166,17 @@ public void setByteBuffer(N subColumnName, ByteBuffer value) { subColumns.add(columnFactory.createColumn(subColumnName, value, template.getEffectiveClock(), template.getSubSerializer(), ByteBufferSerializer.get())); } - + public void setDate(N subColumnName, Date value) { subColumns.add(columnFactory.createColumn(subColumnName, value, template.getEffectiveClock(), template.getSubSerializer(), DateSerializer.get())); } - + public void setDouble(N subColumnName, Double value) { subColumns.add(columnFactory.createColumn(subColumnName, value, template.getEffectiveClock(), template.getSubSerializer(), DoubleSerializer.get())); } - + public void setValue(N subColumnName, V value, Serializer serializer) { subColumns.add(columnFactory.createColumn(subColumnName, value, template.getEffectiveClock(), template.getSubSerializer(), serializer)); diff --git a/core/src/main/java/me/prettyprint/cassandra/service/template/ThriftColumnFamilyTemplate.java b/core/src/main/java/me/prettyprint/cassandra/service/template/ThriftColumnFamilyTemplate.java index 10595971a..a08bf22cc 100644 --- a/core/src/main/java/me/prettyprint/cassandra/service/template/ThriftColumnFamilyTemplate.java +++ b/core/src/main/java/me/prettyprint/cassandra/service/template/ThriftColumnFamilyTemplate.java @@ -23,15 +23,15 @@ import com.google.common.collect.Iterators; /** - * Thrift specific implementation of {@link ColumnFamilyTemplate} + * Thrift specific implementation of {@link ColumnFamilyTemplate} * @author nate * * @param * @param */ public class ThriftColumnFamilyTemplate extends ColumnFamilyTemplate { - - + + public ThriftColumnFamilyTemplate(Keyspace keyspace, String columnFamily, Serializer keySerializer, Serializer topSerializer) { super(keyspace, columnFamily, keySerializer, topSerializer); @@ -42,66 +42,66 @@ public ThriftColumnFamilyTemplate(Keyspace keyspace, String columnFamily, Mutator mutator) { super(keyspace, columnFamily, keySerializer, topSerializer, mutator); } - + public T doExecuteSlice(K key, HSlicePredicate predicate, ColumnFamilyRowMapper mapper) { return mapper.mapRow(doExecuteSlice(key,predicate)); } - - public ColumnFamilyResult doExecuteSlice(final K key, final HSlicePredicate workingSlicePredicate) { - return new ColumnFamilyResultWrapper(keySerializer, topSerializer, + + public ColumnFamilyResult doExecuteSlice(final K key, final HSlicePredicate workingSlicePredicate) { + return new ColumnFamilyResultWrapper(keySerializer, topSerializer, sliceInternal(key, workingSlicePredicate)); } - - public ColumnFamilyResult doExecuteMultigetSlice(final Iterable keys, final HSlicePredicate workingSlicePredicate) { - return new ColumnFamilyResultWrapper(keySerializer, topSerializer, + + public ColumnFamilyResult doExecuteMultigetSlice(final Iterable keys, final HSlicePredicate workingSlicePredicate) { + return new ColumnFamilyResultWrapper(keySerializer, topSerializer, multigetSliceInternal(keys, workingSlicePredicate)); } - public MappedColumnFamilyResult doExecuteMultigetSlice(final Iterable keys, + public MappedColumnFamilyResult doExecuteMultigetSlice(final Iterable keys, final HSlicePredicate workingSlicePredicate, - final ColumnFamilyRowMapper mapper) { - return new MappedColumnFamilyResultWrapper(keySerializer, topSerializer, - multigetSliceInternal(keys, workingSlicePredicate), mapper); + final ColumnFamilyRowMapper mapper) { + return new MappedColumnFamilyResultWrapper(keySerializer, topSerializer, + multigetSliceInternal(keys, workingSlicePredicate), mapper); } - + private ExecutionResult>> sliceInternal(final K key, final HSlicePredicate workingSlicePredicate) { return ((ExecutingKeyspace)keyspace).doExecuteOperation(new Operation>>(OperationType.READ) { @Override public Map> execute(Cassandra.Client cassandra) throws HectorException { Map> cosc = new LinkedHashMap>(); - try { + try { ByteBuffer sKey = keySerializer.toByteBuffer(key); cosc.put(sKey, cassandra.get_slice(sKey, columnParent, - workingSlicePredicate.toThrift(), + workingSlicePredicate.toThrift(), ThriftConverter.consistencyLevel(consistencyLevelPolicy.get(operationType)))); } catch (Exception e) { throw exceptionsTranslator.translate(e); - } + } return cosc; } }); } - + private ExecutionResult>> multigetSliceInternal(final Iterable keys, final HSlicePredicate workingSlicePredicate) { return ((ExecutingKeyspace)keyspace).doExecuteOperation(new Operation>>(OperationType.READ) { @Override public Map> execute(Cassandra.Client cassandra) throws HectorException { Map> cosc = new LinkedHashMap>(); - try { + try { List keyList = new ArrayList(); Iterators.addAll(keyList, keys.iterator()); cosc = cassandra.multiget_slice(keySerializer.toBytesList(keyList), columnParent, - (workingSlicePredicate == null ? activeSlicePredicate.setColumnNames(columnValueSerializers.keySet()).toThrift() : workingSlicePredicate.toThrift()), + (workingSlicePredicate == null ? activeSlicePredicate.setColumnNames(columnValueSerializers.keySet()).toThrift() : workingSlicePredicate.toThrift()), ThriftConverter.consistencyLevel(consistencyLevelPolicy.get(operationType))); } catch (Exception e) { throw exceptionsTranslator.translate(e); - } + } return cosc; } diff --git a/core/src/main/java/me/prettyprint/cassandra/service/template/ThriftSuperCfTemplate.java b/core/src/main/java/me/prettyprint/cassandra/service/template/ThriftSuperCfTemplate.java index 63db9480c..e3a725405 100644 --- a/core/src/main/java/me/prettyprint/cassandra/service/template/ThriftSuperCfTemplate.java +++ b/core/src/main/java/me/prettyprint/cassandra/service/template/ThriftSuperCfTemplate.java @@ -26,46 +26,46 @@ public ThriftSuperCfTemplate(Keyspace keyspace, String columnFamily, Serializer subSerializer) { super(keyspace, columnFamily, keySerializer, topSerializer, subSerializer); } - - protected SuperCfResult doExecuteSlice(K key, SN sColumnName, HSlicePredicate predicate) { - SuperCfResultWrapper wrapper = - new SuperCfResultWrapper(keySerializer, topSerializer, subSerializer, + + protected SuperCfResult doExecuteSlice(K key, SN sColumnName, HSlicePredicate predicate) { + SuperCfResultWrapper wrapper = + new SuperCfResultWrapper(keySerializer, topSerializer, subSerializer, sliceInternal(key, predicate)); if ( sColumnName != null ) { wrapper.applySuperColumn(sColumnName); } return wrapper; - } - - protected SuperCfResult doExecuteMultigetSlice(List keys, HSlicePredicate predicate) { - SuperCfResultWrapper wrapper = - new SuperCfResultWrapper(keySerializer, topSerializer, subSerializer, - multigetSliceInternal(keys, columnParent, predicate)); + } + + protected SuperCfResult doExecuteMultigetSlice(List keys, HSlicePredicate predicate) { + SuperCfResultWrapper wrapper = + new SuperCfResultWrapper(keySerializer, topSerializer, subSerializer, + multigetSliceInternal(keys, columnParent, predicate)); return wrapper; - } - + } + private ExecutionResult>> sliceInternal(final K key, final HSlicePredicate workingSlicePredicate) { return ((ExecutingKeyspace)keyspace).doExecuteOperation(new Operation>>(OperationType.READ) { @Override public Map> execute(Cassandra.Client cassandra) throws HectorException { Map> cosc = new LinkedHashMap>(); - try { + try { ByteBuffer sKey = keySerializer.toByteBuffer(key); cosc.put(sKey, cassandra.get_slice(sKey, columnParent, - workingSlicePredicate.toThrift(), + workingSlicePredicate.toThrift(), ThriftConverter.consistencyLevel(consistencyLevelPolicy.get(operationType)))); } catch (Exception e) { throw exceptionsTranslator.translate(e); - } + } return cosc; } }); } - + private ExecutionResult>> multigetSliceInternal(final List keys, final ColumnParent workingColumnParent, final HSlicePredicate workingSlicePredicate) { @@ -73,16 +73,16 @@ private ExecutionResult>> multigetSlic @Override public Map> execute(Cassandra.Client cassandra) throws HectorException { Map> cosc; - try { + try { List sKeys = keySerializer.toBytesList(keys); cosc = cassandra.multiget_slice(sKeys, workingColumnParent, - workingSlicePredicate.toThrift(), + workingSlicePredicate.toThrift(), ThriftConverter.consistencyLevel(consistencyLevelPolicy.get(operationType))); } catch (Exception e) { throw exceptionsTranslator.translate(e); - } + } return cosc; } diff --git a/core/src/main/java/me/prettyprint/cassandra/testutils/EmbeddedSchemaLoader.java b/core/src/main/java/me/prettyprint/cassandra/testutils/EmbeddedSchemaLoader.java index 4e3fc8ca1..d2f47bf8f 100644 --- a/core/src/main/java/me/prettyprint/cassandra/testutils/EmbeddedSchemaLoader.java +++ b/core/src/main/java/me/prettyprint/cassandra/testutils/EmbeddedSchemaLoader.java @@ -52,7 +52,7 @@ public static Collection schemaDefinition() { Class simple = SimpleStrategy.class; Map opts = new HashMap(); opts.put("replication_factor", Integer.toString(1)); - + ColumnFamilyType st = ColumnFamilyType.Standard; ColumnFamilyType su = ColumnFamilyType.Super; AbstractType bytes = BytesType.instance; @@ -66,7 +66,7 @@ public static Collection schemaDefinition() { // Column Families standardCFMD(ks1, "Standard1"), standardCFMD(ks1, "Standard2"), standardCFMD(ks1, "Standard3"), standardCFMD(ks1, "Standard4"), - standardCFMD(ks1, "StandardLong1").keyValidator(UTF8Type.instance), + standardCFMD(ks1, "StandardLong1").keyValidator(UTF8Type.instance), standardCFMD(ks1, "StandardLong2"), superCFMD(ks1, "Super1", BytesType.instance), superCFMD(ks1, "Super2", LongType.instance), superCFMD(ks1, "Super3", LongType.instance), @@ -84,7 +84,7 @@ public static Collection schemaDefinition() { AsciiType.instance))); // Keyspace 2 - + return schema; } diff --git a/core/src/main/java/me/prettyprint/cassandra/testutils/EmbeddedServerHelper.java b/core/src/main/java/me/prettyprint/cassandra/testutils/EmbeddedServerHelper.java index c38acfc1e..64af7cc77 100644 --- a/core/src/main/java/me/prettyprint/cassandra/testutils/EmbeddedServerHelper.java +++ b/core/src/main/java/me/prettyprint/cassandra/testutils/EmbeddedServerHelper.java @@ -23,9 +23,9 @@ import org.slf4j.LoggerFactory; /** - * + * * @author Ran Tavory (rantav@gmail.com) - * + * */ public class EmbeddedServerHelper { private static Logger log = LoggerFactory.getLogger(EmbeddedServerHelper.class); @@ -35,7 +35,7 @@ public class EmbeddedServerHelper { private EmbeddedCassandraService cassandra; private final String yamlFile; static CassandraDaemon cassandraDaemon; - + public EmbeddedServerHelper() { this("/cassandra.yaml"); } @@ -43,12 +43,12 @@ public EmbeddedServerHelper() { public EmbeddedServerHelper(String yamlFile) { this.yamlFile = yamlFile; } - - static ExecutorService executor = Executors.newSingleThreadExecutor(); + + static ExecutorService executor = Executors.newSingleThreadExecutor(); /** * Set embedded cassandra up and spawn it in a new thread. - * + * * @throws TTransportException * @throws IOException * @throws InterruptedException @@ -68,7 +68,7 @@ public void setup() throws TTransportException, IOException, loadSchemaFromYaml(); //loadYamlTables(); log.info("Starting executor"); - + executor.execute(new CassandraRunner()); log.info("Started executor"); try @@ -101,7 +101,7 @@ private static void rmdir(String dir) throws IOException { /** * Copies a resource from within the jar to a directory. - * + * * @param resource * @param directory * @throws IOException @@ -125,14 +125,14 @@ private static void copy(String resource, String directory) /** * Creates a directory - * + * * @param dir * @throws IOException */ private static void mkdir(String dir) throws IOException { FileUtils.createDirectory(dir); } - + public static void cleanupAndLeaveDirs() throws IOException { @@ -174,25 +174,25 @@ public static void mkdirs() { throw new RuntimeException(e); } - } - - public static void loadSchemaFromYaml() + } + + public static void loadSchemaFromYaml() { EmbeddedSchemaLoader.loadSchema(); - - } - - class CassandraRunner implements Runnable { - + } + + + class CassandraRunner implements Runnable { + @Override public void run() { cassandraDaemon = new CassandraDaemon(); - + cassandraDaemon.activate(); } - + } } diff --git a/core/src/main/java/me/prettyprint/cassandra/utils/ByteBufferOutputStream.java b/core/src/main/java/me/prettyprint/cassandra/utils/ByteBufferOutputStream.java index 423cc40be..4cf28fd82 100644 --- a/core/src/main/java/me/prettyprint/cassandra/utils/ByteBufferOutputStream.java +++ b/core/src/main/java/me/prettyprint/cassandra/utils/ByteBufferOutputStream.java @@ -26,7 +26,7 @@ /** * Utility to collect data written to an {@link OutputStream} in * {@link ByteBuffer}s. - * + * * Originally from org.apache.avro.util.ByteBufferOutputStream, moved into * Hector and added getByteBuffer to return single ByteBuffer from contents. */ diff --git a/core/src/main/java/me/prettyprint/hector/api/Cluster.java b/core/src/main/java/me/prettyprint/hector/api/Cluster.java index fa0a9fd63..fdeeeabff 100644 --- a/core/src/main/java/me/prettyprint/hector/api/Cluster.java +++ b/core/src/main/java/me/prettyprint/hector/api/Cluster.java @@ -41,7 +41,7 @@ public interface Cluster { String describeClusterName() throws HectorException; String describeThriftVersion() throws HectorException; - + Map> describeSchemaVersions() throws HectorException; KeyspaceDefinition describeKeyspace(final String keyspace) throws HectorException; @@ -65,11 +65,11 @@ public interface Cluster { String dropColumnFamily(final String keyspaceName, final String columnFamily) throws HectorException; - + String updateColumnFamily(final ColumnFamilyDefinition cfdef) throws HectorException; String addKeyspace(final KeyspaceDefinition ksdef) throws HectorException; - + void truncate(final String keyspaceName, final String columnFamily) throws HectorException; Map getCredentials(); diff --git a/core/src/main/java/me/prettyprint/hector/api/ColumnFactory.java b/core/src/main/java/me/prettyprint/hector/api/ColumnFactory.java index 1863d8bf6..ccaf6a820 100644 --- a/core/src/main/java/me/prettyprint/hector/api/ColumnFactory.java +++ b/core/src/main/java/me/prettyprint/hector/api/ColumnFactory.java @@ -4,8 +4,8 @@ public interface ColumnFactory { HColumn createColumn(N name, V value, Serializer nameSerializer, Serializer valueSerializer); - + HColumn createColumn(N name, V value, long clock, Serializer nameSerializer, Serializer valueSerializer); - + HColumn createStringColumn(String name, String value); } diff --git a/core/src/main/java/me/prettyprint/hector/api/HColumnFamily.java b/core/src/main/java/me/prettyprint/hector/api/HColumnFamily.java index f1bf0c018..a6be7961b 100644 --- a/core/src/main/java/me/prettyprint/hector/api/HColumnFamily.java +++ b/core/src/main/java/me/prettyprint/hector/api/HColumnFamily.java @@ -12,45 +12,45 @@ public interface HColumnFamily extends Iterator>,ResultStatus { HColumnFamily setReadConsistencyLevel(HConsistencyLevel readLevel); - + HColumnFamily setWriteConsistencyLevel(HConsistencyLevel writeLevel); - + HColumnFamily addKey(K key); - + HColumnFamily addKeys(Collection keys); - - HColumnFamily removeKeys(); - + + HColumnFamily removeKeys(); + HColumnFamily clear(); - + HColumnFamily setStart(N name); - + HColumnFamily setFinish(N name); - + HColumnFamily setCount(int count); - + HColumnFamily setReversed(boolean reversed); - + HColumnFamily setColumnNames(Collection columnNames); - + HColumnFamily addColumnName(N columnName); - + Collection> getColumns(); - + HColumn getColumn(N name); - + String getString(N name); - + int getInt(N name); - + long getLong(N name); - + UUID getUUID(N name); - + Date getDate(N name); - + double getDouble(N name); - + V getValue(N name, Serializer valueSerializer); - + } diff --git a/core/src/main/java/me/prettyprint/hector/api/Keyspace.java b/core/src/main/java/me/prettyprint/hector/api/Keyspace.java index 5477a84a4..e9a73a083 100644 --- a/core/src/main/java/me/prettyprint/hector/api/Keyspace.java +++ b/core/src/main/java/me/prettyprint/hector/api/Keyspace.java @@ -12,11 +12,11 @@ public interface Keyspace { public static final String KEYSPACE_SYSTEM = "system"; - + void setConsistencyLevelPolicy(ConsistencyLevelPolicy cp); String getKeyspaceName(); - + long createClock(); } \ No newline at end of file diff --git a/core/src/main/java/me/prettyprint/hector/api/ResultStatus.java b/core/src/main/java/me/prettyprint/hector/api/ResultStatus.java index 1b562abf5..196f88013 100644 --- a/core/src/main/java/me/prettyprint/hector/api/ResultStatus.java +++ b/core/src/main/java/me/prettyprint/hector/api/ResultStatus.java @@ -13,12 +13,12 @@ public interface ResultStatus { /** * How long the operation took to execute in MICRO-seconds. Internally - * this is usually the difference between two calls of {@link System#nanoTime()} + * this is usually the difference between two calls of {@link System#nanoTime()} * divided by 1000 * @return */ long getExecutionTimeMicro(); - + long getExecutionTimeNano(); /** diff --git a/core/src/main/java/me/prettyprint/hector/api/Serializer.java b/core/src/main/java/me/prettyprint/hector/api/Serializer.java index dc3bc9cec..996d12264 100644 --- a/core/src/main/java/me/prettyprint/hector/api/Serializer.java +++ b/core/src/main/java/me/prettyprint/hector/api/Serializer.java @@ -9,16 +9,16 @@ /** * Serializes a type T from the given bytes, or vice a versa. - * + * * In cassandra column names and column values (and starting with 0.7.0 row * keys) are all byte[]. To allow type safe conversion in java and keep all * conversion code in one place we define the Extractor interface. Implementors * of the interface define type conversion according to their domains. A * predefined set of common extractors can be found in the extractors package, * for example {@link StringSerializer}. - * + * * @author Ran Tavory - * + * * @param * The type to which data extraction should work. */ @@ -26,7 +26,7 @@ public interface Serializer { /** * Extract bytes from the obj of type T - * + * * @param obj * @return */ @@ -38,7 +38,7 @@ public interface Serializer { /** * Extract an object of type T from the bytes. - * + * * @param bytes * @return */ diff --git a/core/src/main/java/me/prettyprint/hector/api/beans/AbstractComposite.java b/core/src/main/java/me/prettyprint/hector/api/beans/AbstractComposite.java index 2f9abd258..2d855932e 100644 --- a/core/src/main/java/me/prettyprint/hector/api/beans/AbstractComposite.java +++ b/core/src/main/java/me/prettyprint/hector/api/beans/AbstractComposite.java @@ -43,7 +43,7 @@ * that get serialized into a composite column name. Unless * setAutoDeserialize(true) is called, it's going to try to match serializers to * Cassandra comparator types. - * + * * @author edanuff */ @SuppressWarnings("rawtypes") diff --git a/core/src/main/java/me/prettyprint/hector/api/beans/HColumn.java b/core/src/main/java/me/prettyprint/hector/api/beans/HColumn.java index 95a20a058..6c6a83e5a 100644 --- a/core/src/main/java/me/prettyprint/hector/api/beans/HColumn.java +++ b/core/src/main/java/me/prettyprint/hector/api/beans/HColumn.java @@ -27,12 +27,12 @@ public interface HColumn { * (Advanced) Returns the underlying ByteBuffer for the value via ByteBuffer.duplicate(). */ ByteBuffer getValueBytes(); - + /** * (Advanced) Returns the underlying ByteBuffer for the name via ByteBuffer.duplicate(). */ ByteBuffer getNameBytes(); - + long getClock(); HColumn setClock(long clock); @@ -40,9 +40,9 @@ public interface HColumn { int getTtl(); HColumn setTtl(int ttl); - + HColumn clear(); - + HColumn apply(V value, long clock, int ttl); Serializer getNameSerializer(); diff --git a/core/src/main/java/me/prettyprint/hector/api/beans/HCounterColumn.java b/core/src/main/java/me/prettyprint/hector/api/beans/HCounterColumn.java index 194b14a6f..29958c7bf 100644 --- a/core/src/main/java/me/prettyprint/hector/api/beans/HCounterColumn.java +++ b/core/src/main/java/me/prettyprint/hector/api/beans/HCounterColumn.java @@ -21,18 +21,18 @@ public interface HCounterColumn { Long getValue(); - + /** * (Advanced) Returns the underlying ByteBuffer for the name via ByteBuffer.duplicate(). */ ByteBuffer getNameBytes(); - + HCounterColumn clear(); - + int getTtl(); HCounterColumn setTtl(int ttl); - + HCounterColumn apply(Long value, int ttl); Serializer getNameSerializer(); diff --git a/core/src/main/java/me/prettyprint/hector/api/beans/HCounterSuperColumn.java b/core/src/main/java/me/prettyprint/hector/api/beans/HCounterSuperColumn.java index e65543f40..65c79c1ec 100644 --- a/core/src/main/java/me/prettyprint/hector/api/beans/HCounterSuperColumn.java +++ b/core/src/main/java/me/prettyprint/hector/api/beans/HCounterSuperColumn.java @@ -36,7 +36,7 @@ public interface HCounterSuperColumn { Serializer getNameSerializer(); byte[] getNameBytes(); - + ByteBuffer getNameByteBuffer(); Serializer getSuperNameSerializer(); diff --git a/core/src/main/java/me/prettyprint/hector/api/beans/HSuperColumn.java b/core/src/main/java/me/prettyprint/hector/api/beans/HSuperColumn.java index 2ffc7ce99..9a1fc2aef 100644 --- a/core/src/main/java/me/prettyprint/hector/api/beans/HSuperColumn.java +++ b/core/src/main/java/me/prettyprint/hector/api/beans/HSuperColumn.java @@ -42,7 +42,7 @@ public interface HSuperColumn { Serializer getNameSerializer(); byte[] getNameBytes(); - + ByteBuffer getNameByteBuffer(); Serializer getSuperNameSerializer(); diff --git a/core/src/main/java/me/prettyprint/hector/api/ddl/package.html b/core/src/main/java/me/prettyprint/hector/api/ddl/package.html index 787bc98ae..bcb94c6e6 100644 --- a/core/src/main/java/me/prettyprint/hector/api/ddl/package.html +++ b/core/src/main/java/me/prettyprint/hector/api/ddl/package.html @@ -9,16 +9,16 @@
  • Adding new keyspaces
  • Adding new column families
  • Defining indices
  • -
  • Deleting exisiting constructs
  • +
  • Deleting exisiting constructs
  • -A note should be taken that by server design, it is required that all DDL operations are to be -performed in a serialized manner.
    +A note should be taken that by server design, it is required that all DDL operations are to be +performed in a serialized manner.
    Cassandra cannot support two DDL operations issued at the same time. The first operation needs to end before the next operation can take place and Hecotr does not try to protect its users from it, -it's the responsibility of hector's users to prevent from multiple schema changes to happen at the +it's the responsibility of hector's users to prevent from multiple schema changes to happen at the same time.
    -See Live Schema Updates for more +See Live Schema Updates for more details.
    \ No newline at end of file diff --git a/core/src/main/java/me/prettyprint/hector/api/exceptions/HCassandraInternalException.java b/core/src/main/java/me/prettyprint/hector/api/exceptions/HCassandraInternalException.java index 4398f8e59..9a1687a58 100644 --- a/core/src/main/java/me/prettyprint/hector/api/exceptions/HCassandraInternalException.java +++ b/core/src/main/java/me/prettyprint/hector/api/exceptions/HCassandraInternalException.java @@ -3,21 +3,21 @@ /** * Designed to loosely wrap TApplicationException which can be generated * by Apache Cassandra under a variety of ambiguous conditions - some of - * them transient, some of them not. + * them transient, some of them not. * * @author zznate */ public class HCassandraInternalException extends HectorException { private static final long serialVersionUID = -266109391311421129L; - + private int type; - - private static final String ERR_MSG = + + private static final String ERR_MSG = "Cassandra encountered an internal error processing this request: "; - + public HCassandraInternalException(String msg) { - super(ERR_MSG + msg); + super(ERR_MSG + msg); } public HCassandraInternalException(int type, String msg) { @@ -26,11 +26,11 @@ public HCassandraInternalException(int type, String msg) { } public HCassandraInternalException(String s, Throwable t) { - super(ERR_MSG + s, t); + super(ERR_MSG + s, t); } public HCassandraInternalException(Throwable t) { - super(t); + super(t); } /** @@ -40,7 +40,7 @@ public HCassandraInternalException(Throwable t) { public int getType() { return type; } - - + + } diff --git a/core/src/main/java/me/prettyprint/hector/api/exceptions/HUnavailableException.java b/core/src/main/java/me/prettyprint/hector/api/exceptions/HUnavailableException.java index b815a4bb1..17120c395 100644 --- a/core/src/main/java/me/prettyprint/hector/api/exceptions/HUnavailableException.java +++ b/core/src/main/java/me/prettyprint/hector/api/exceptions/HUnavailableException.java @@ -6,7 +6,7 @@ */ public final class HUnavailableException extends HectorException { - private static final String ERR_MSG = + private static final String ERR_MSG = ": May not be enough replicas present to handle consistency level."; private static final long serialVersionUID = 1971498442136497970L; diff --git a/core/src/main/java/me/prettyprint/hector/api/exceptions/HectorException.java b/core/src/main/java/me/prettyprint/hector/api/exceptions/HectorException.java index 59efad204..42cd84da2 100644 --- a/core/src/main/java/me/prettyprint/hector/api/exceptions/HectorException.java +++ b/core/src/main/java/me/prettyprint/hector/api/exceptions/HectorException.java @@ -2,7 +2,7 @@ /** * Base exception class for all Hector related exceptions. - * + * * @author Ran Tavory (rantav@gmail.com) * */ @@ -13,7 +13,7 @@ public class HectorException extends RuntimeException { public HectorException(String msg) { super(msg); } - + public HectorException(Throwable t) { super(t); } diff --git a/core/src/main/java/me/prettyprint/hector/api/factory/HFactory.java b/core/src/main/java/me/prettyprint/hector/api/factory/HFactory.java index 238bf7a6d..912b5f285 100644 --- a/core/src/main/java/me/prettyprint/hector/api/factory/HFactory.java +++ b/core/src/main/java/me/prettyprint/hector/api/factory/HFactory.java @@ -75,7 +75,7 @@ /** * A convenience class with bunch of factory static methods to help create a * mutator, queries etc. - * + * * @author Ran * @author zznate */ @@ -96,15 +96,15 @@ public static Cluster getCluster(String clusterName) { * cluster. If another class already called getOrCreateCluster, the factory * returns the cached instance. If the instance doesn't exist in memory, a new * ThriftCluster is created and cached. - * + * * Example usage for a default installation of Cassandra. - * + * * String clusterName = "Test Cluster"; String host = "localhost:9160"; * Cluster cluster = HFactory.getOrCreateCluster(clusterName, host); - * + * * Note the host should be the hostname and port number. It is preferable to * use the hostname instead of the IP address. - * + * * @param clusterName * The cluster name. This is an identifying string for the cluster, * e.g. "production" or "test" etc. Clusters will be created on @@ -123,13 +123,13 @@ public static Cluster getOrCreateCluster(String clusterName, String hostIp) { * cluster. If another class already called getOrCreateCluster, the factory * returns the cached instance. If the instance doesn't exist in memory, a new * ThriftCluster is created and cached. - * + * * Example usage for a default installation of Cassandra. - * + * * String clusterName = "Test Cluster"; String host = "localhost:9160"; * Cluster cluster = HFactory.getOrCreateCluster(clusterName, new * CassandraHostConfigurator(host)); - * + * * @param clusterName * The cluster name. This is an identifying string for the cluster, * e.g. "production" or "test" etc. Clusters will be created on @@ -151,13 +151,13 @@ public static Cluster getOrCreateCluster(String clusterName, /** * Method looks in the cache for the cluster by name. If none exists, a new * ThriftCluster instance is created. - * + * * @param clusterName * The cluster name. This is an identifying string for the cluster, * e.g. "production" or "test" etc. Clusters will be created on * demand per each unique clusterName key. * @param cassandraHostConfigurator - * + * */ public static Cluster createCluster(String clusterName, CassandraHostConfigurator cassandraHostConfigurator) { @@ -170,7 +170,7 @@ public static Cluster createCluster(String clusterName, /** * Method looks in the cache for the cluster by name. If none exists, a new * ThriftCluster instance is created. - * + * * @param clusterName * The cluster name. This is an identifying string for the cluster, * e.g. "production" or "test" etc. Clusters will be created on @@ -186,7 +186,7 @@ public static Cluster createCluster(String clusterName, cassandraHostConfigurator, credentials) : clusters.get(clusterName); } } - + /** * Shutdown this cluster, removing it from the Map. This operation is * extremely expensive and should not be done lightly. @@ -204,7 +204,7 @@ public static void shutdownCluster(Cluster cluster) { /** * Creates a Keyspace with the default consistency level policy. - * + * * Example usage. * * String clusterName = "Test Cluster"; @@ -212,7 +212,7 @@ public static void shutdownCluster(Cluster cluster) { * Cluster cluster = HFactory.getOrCreateCluster(clusterName, host); * String keyspaceName = "testKeyspace"; * Keyspace myKeyspace = HFactory.createKeyspace(keyspaceName, cluster); - * + * * @param keyspace * @param cluster * @return @@ -327,7 +327,7 @@ public static ColumnQuery createColumnQuery( return new ThriftColumnQuery(keyspace, keySerializer, nameSerializer, valueSerializer); } - + public static CounterQuery createCounterColumnQuery( Keyspace keyspace, Serializer keySerializer, Serializer nameSerializer) { return new ThriftCounterColumnQuery(keyspace, keySerializer, nameSerializer); @@ -433,7 +433,7 @@ public static SliceQuery createSliceQuery( return new ThriftSliceQuery(keyspace, keySerializer, nameSerializer, valueSerializer); } - + public static SliceCounterQuery createCounterSliceQuery( Keyspace keyspace, Serializer keySerializer, Serializer nameSerializer) { return new ThriftSliceCounterQuery(keyspace, keySerializer, nameSerializer); @@ -457,7 +457,7 @@ public static SuperSliceQuery createSuperSliceQuery( /** * createSuperColumn accepts a variable number of column arguments - * + * * @param name * supercolumn name * @param columns @@ -480,7 +480,7 @@ public static HSuperColumn createSuperColumn(SN name, return new HSuperColumnImpl(name, columns, clock, superNameSerializer, nameSerializer, valueSerializer); } - + public static HCounterSuperColumn createCounterSuperColumn(SN name, List> columns, Serializer superNameSerializer, Serializer nameSerializer) { return new HCounterSuperColumnImpl(name, columns, superNameSerializer, nameSerializer); @@ -515,7 +515,7 @@ public static HColumn createColumn(N name, V value, int ttl, return new HColumnImpl(name, value, createClock(), ttl, nameSerializer, valueSerializer); } - + /** * Convienience method for creating a column with a String name and String * value @@ -525,14 +525,14 @@ public static HColumn createStringColumn(String name, StringSerializer se = StringSerializer.get(); return createColumn(name, value, se, se); } - + /** * Create a counter column with a name and long value */ public static HCounterColumn createCounterColumn(N name, long value, Serializer nameSerializer) { return new HCounterColumnImpl(name, value, nameSerializer); } - + /** * Convenient method for creating a counter column with a String name and long value */ @@ -555,11 +555,11 @@ public static long createClock() { /** * Use createKeyspaceDefinition to add a new Keyspace to cluster. Example: - * + * * String testKeyspace = "testKeyspace"; KeyspaceDefinition newKeyspace = * HFactory.createKeyspaceDefinition(testKeyspace); * cluster.addKeyspace(newKeyspace); - * + * * @param keyspace */ public static KeyspaceDefinition createKeyspaceDefinition(String keyspace) { @@ -568,11 +568,11 @@ public static KeyspaceDefinition createKeyspaceDefinition(String keyspace) { /** * Use createKeyspaceDefinition to add a new Keyspace to cluster. Example: - * + * * String testKeyspace = "testKeyspace"; KeyspaceDefinition newKeyspace = * HFactory.createKeyspaceDefinition(testKeyspace); * cluster.addKeyspace(newKeyspace); - * + * * @param keyspace * @param strategyClass * - example: @@ -598,7 +598,7 @@ public static KeyspaceDefinition createKeyspaceDefinition( * HFactory.createKeyspaceDefinition(keyspace, * org.apache.cassandra.locator.SimpleStrategy.class.getName(), 1, columns); * cluster.addKeyspace(testKeyspace); - * + * * @param keyspace * @param columnFamilyName */ @@ -618,7 +618,7 @@ public static ColumnFamilyDefinition createColumnFamilyDefinition( * HFactory.createKeyspaceDefinition(keyspace, * org.apache.cassandra.locator.SimpleStrategy.class.getName(), 1, columns); * cluster.addKeyspace(testKeyspace); - * + * * @param keyspace * @param columnFamilyName * @param comparatorType @@ -637,7 +637,7 @@ public static ColumnFamilyDefinition createColumnFamilyDefinition( /** * Create a clock resolution based on clockResolutionName which * has to match any of the constants defined at {@link ClockResolution} - * + * * @param clockResolutionName * type of clock resolution to create * @return a ClockResolution diff --git a/core/src/main/java/me/prettyprint/hector/api/mutation/MutationResult.java b/core/src/main/java/me/prettyprint/hector/api/mutation/MutationResult.java index 9d6e2d5d9..e895e9bac 100644 --- a/core/src/main/java/me/prettyprint/hector/api/mutation/MutationResult.java +++ b/core/src/main/java/me/prettyprint/hector/api/mutation/MutationResult.java @@ -12,5 +12,5 @@ */ public interface MutationResult extends ResultStatus { - + } \ No newline at end of file diff --git a/core/src/main/java/me/prettyprint/hector/api/mutation/Mutator.java b/core/src/main/java/me/prettyprint/hector/api/mutation/Mutator.java index fd3a7e01a..9f59c8516 100644 --- a/core/src/main/java/me/prettyprint/hector/api/mutation/Mutator.java +++ b/core/src/main/java/me/prettyprint/hector/api/mutation/Mutator.java @@ -28,7 +28,7 @@ MutationResult insert(final K key, final String cf, MutationResult delete(final K key, final String cf, final N columnName, final Serializer nameSerializer); - + MutationResult delete(final K key, final String cf, final N columnName, final Serializer nameSerializer, long clock); @@ -39,20 +39,20 @@ MutationResult delete(final K key, final String cf, final N columnName, */ MutationResult subDelete(final K key, final String cf, final SN supercolumnName, final N columnName, final Serializer sNameSerializer, final Serializer nameSerializer); - + /** * Deletes a supercolumn immediately * @param super column type */ MutationResult superDelete(K key, String cf, SN supercolumnName, Serializer sNameSerializer); - + /** * batches a super column for deletion - * + * */ Mutator addSuperDelete(K key, String cf, SN sColumnName, Serializer sNameSerializer); - + // schedule an insertion to be executed in batch by the execute method // CAVEAT: a large number of calls with a typo in one of them will leave things in an @@ -79,19 +79,19 @@ MutationResult superDelete(K key, String cf, SN supercolumnName, * @return a mutator */ Mutator addDeletion(K key, String cf, N columnName, Serializer nameSerializer); - + /** * Alternate form for easy deletion of the whole row. - * + * * @param * @param key * @return */ Mutator addDeletion(K key, String cf); - + /** * Same as above accept we add the clock - * + * * @param * @param key * @return @@ -113,17 +113,17 @@ MutationResult superDelete(K key, String cf, SN supercolumnName, */ Mutator addDeletion(K key, String cf, N columnName, Serializer nameSerializer, long clock); - + Mutator addSubDelete(K key, String cf, HSuperColumn sc); Mutator addSubDelete(K key, String cf, HSuperColumn sc, long clock); - + Mutator addSubDelete(K key, String cf, SN sColumnName, N columnName, Serializer sNameSerializer, Serializer nameSerialer); - + Mutator addSubDelete(K key, String cf, SN sColumnName, N columnName, Serializer sNameSerializer, Serializer nameSerialer, long clock); - - - + + + /** * Batch executes all mutations scheduled to this Mutator instance by addInsertion, addDeletion etc. * May throw a HectorException which is a RuntimeException. @@ -135,23 +135,23 @@ MutationResult superDelete(K key, String cf, SN supercolumnName, * Discards all pending mutations. */ Mutator discardPendingMutations(); - + // Support for counters - + /** Simple and immediate insertion (increment/decrement) of a counter */ MutationResult insertCounter(final K key, final String cf, final HCounterColumn c); - + /** Simple and immediate insertion (increment/decrement) of a counter part of a super column */ MutationResult insertCounter(final K key, final String cf, final HCounterSuperColumn superColumn); - + /** Convenient method to increment a simple counter */ MutationResult incrementCounter(final K key, final String cf, final N columnName, final long increment); - + /** Convenient method to decrement a simple counter */ MutationResult decrementCounter(final K key, final String cf, final N columnName, final long increment); MutationResult deleteCounter(final K key, final String cf, final N columnName, final Serializer nameSerializer); - + /** * Deletes a subcolumn of a supercolumn for a counter * @param super column type @@ -159,7 +159,7 @@ MutationResult superDelete(K key, String cf, SN supercolumnName, */ MutationResult subDeleteCounter(final K key, final String cf, final SN supercolumnName, final N columnName, final Serializer sNameSerializer, final Serializer nameSerializer); - + /** * Schedule an increment of a CounterColumn to be inserted in batch mode by {@link #execute()} */ @@ -169,7 +169,7 @@ MutationResult subDeleteCounter(final K key, final String cf, final SN s * Schedule an increment of a SuperColumn to be inserted in batch mode by {@link #execute()} */ Mutator addCounter(K key, String cf, HCounterSuperColumn sc); - + /** * Adds a Deletion to the underlying batch_mutate call. The columnName argument can be null * in which case the whole row being deleted. @@ -182,10 +182,10 @@ MutationResult subDeleteCounter(final K key, final String cf, final SN s * @return a mutator */ Mutator addCounterDeletion(K key, String cf, N counterColumnName, Serializer nameSerializer); - + /** * Alternate form for easy deletion of the whole row. - * + * * @param * @param key * @return diff --git a/core/src/main/java/me/prettyprint/hector/api/query/MultigetSliceQuery.java b/core/src/main/java/me/prettyprint/hector/api/query/MultigetSliceQuery.java index 2dd451ccf..836b8f6b4 100644 --- a/core/src/main/java/me/prettyprint/hector/api/query/MultigetSliceQuery.java +++ b/core/src/main/java/me/prettyprint/hector/api/query/MultigetSliceQuery.java @@ -12,7 +12,7 @@ public interface MultigetSliceQuery extends Query> { MultigetSliceQuery setKeys(K... keys); - + MultigetSliceQuery setKeys(Iterable keys); /** diff --git a/core/src/main/java/me/prettyprint/hector/api/query/QueryResult.java b/core/src/main/java/me/prettyprint/hector/api/query/QueryResult.java index 9e9bc6277..dde4e7d0a 100644 --- a/core/src/main/java/me/prettyprint/hector/api/query/QueryResult.java +++ b/core/src/main/java/me/prettyprint/hector/api/query/QueryResult.java @@ -6,7 +6,7 @@ * Return type from queries execution. * * @author Ran Tavory - * + * * @param The type of the result. May be for example Column of SuperColumn */ diff --git a/core/src/main/java/me/prettyprint/hector/api/query/RangeSlicesQuery.java b/core/src/main/java/me/prettyprint/hector/api/query/RangeSlicesQuery.java index 75efa9770..33eeb92a9 100644 --- a/core/src/main/java/me/prettyprint/hector/api/query/RangeSlicesQuery.java +++ b/core/src/main/java/me/prettyprint/hector/api/query/RangeSlicesQuery.java @@ -25,7 +25,7 @@ public interface RangeSlicesQuery extends Query>{ RangeSlicesQuery setColumnFamily(String cf); RangeSlicesQuery setRange(N start, N finish, boolean reversed, int count); - + RangeSlicesQuery setReturnKeysOnly(); } \ No newline at end of file diff --git a/core/src/main/java/me/prettyprint/hector/api/query/SliceCounterQuery.java b/core/src/main/java/me/prettyprint/hector/api/query/SliceCounterQuery.java index 72f7379c5..efc344e99 100644 --- a/core/src/main/java/me/prettyprint/hector/api/query/SliceCounterQuery.java +++ b/core/src/main/java/me/prettyprint/hector/api/query/SliceCounterQuery.java @@ -4,7 +4,7 @@ /** * A query for the thrift call get_counter_slice - * + * * @author patricioe (Patricio Echague) * * @param Name Type diff --git a/core/src/test/java/me/prettyprint/cassandra/connection/BaseBalancingPolicyTest.java b/core/src/test/java/me/prettyprint/cassandra/connection/BaseBalancingPolicyTest.java index c1bd26f3e..aa88bda64 100644 --- a/core/src/test/java/me/prettyprint/cassandra/connection/BaseBalancingPolicyTest.java +++ b/core/src/test/java/me/prettyprint/cassandra/connection/BaseBalancingPolicyTest.java @@ -11,28 +11,28 @@ public abstract class BaseBalancingPolicyTest { protected List pools = new ArrayList(); - + protected ConcurrentHClientPool poolWith5Active; protected ConcurrentHClientPool poolWith7Active; protected ConcurrentHClientPool poolWith10Active; - - + + @Before public void setup() { - poolWith5Active = Mockito.mock(ConcurrentHClientPool.class); + poolWith5Active = Mockito.mock(ConcurrentHClientPool.class); Mockito.when(poolWith5Active.getNumActive()).thenReturn(5); poolWith7Active = Mockito.mock(ConcurrentHClientPool.class); Mockito.when(poolWith7Active.getNumActive()).thenReturn(7); poolWith10Active = Mockito.mock(ConcurrentHClientPool.class); Mockito.when(poolWith10Active.getNumActive()).thenReturn(10); - + Mockito.when(poolWith5Active.getCassandraHost()).thenReturn(new CassandraHost("127.0.0.1:9160")); Mockito.when(poolWith7Active.getCassandraHost()).thenReturn(new CassandraHost("127.0.0.2:9161")); Mockito.when(poolWith10Active.getCassandraHost()).thenReturn(new CassandraHost("127.0.0.3:9162")); - - pools.add(poolWith5Active); + + pools.add(poolWith5Active); pools.add(poolWith7Active); pools.add(poolWith10Active); } - + } diff --git a/core/src/test/java/me/prettyprint/cassandra/connection/ConcurrentHClientPoolTest.java b/core/src/test/java/me/prettyprint/cassandra/connection/ConcurrentHClientPoolTest.java index baef86eef..a61284a1e 100644 --- a/core/src/test/java/me/prettyprint/cassandra/connection/ConcurrentHClientPoolTest.java +++ b/core/src/test/java/me/prettyprint/cassandra/connection/ConcurrentHClientPoolTest.java @@ -8,17 +8,17 @@ import org.junit.Test; public class ConcurrentHClientPoolTest extends BaseEmbededServerSetupTest { - + private CassandraHost cassandraHost; private ConcurrentHClientPool clientPool; - + @Before public void setupTest() { setupClient(); cassandraHost = cassandraHostConfigurator.buildCassandraHosts()[0]; clientPool = new ConcurrentHClientPool(cassandraHost); } - + @Test public void testSpinUp() { assertEquals(16, clientPool.getNumIdle()); @@ -26,7 +26,7 @@ public void testSpinUp() { assertEquals(0, clientPool.getNumBlockedThreads()); assertEquals(0, clientPool.getNumActive()); } - + @Test public void testShutdown() { clientPool.shutdown(); @@ -34,7 +34,7 @@ public void testShutdown() { assertEquals(0, clientPool.getNumBlockedThreads()); assertEquals(0, clientPool.getNumActive()); } - + @Test public void testBorrowRelease() { HThriftClient client = clientPool.borrowClient(); diff --git a/core/src/test/java/me/prettyprint/cassandra/connection/HConnectionManagerTest.java b/core/src/test/java/me/prettyprint/cassandra/connection/HConnectionManagerTest.java index 590aef571..4cd62efd6 100644 --- a/core/src/test/java/me/prettyprint/cassandra/connection/HConnectionManagerTest.java +++ b/core/src/test/java/me/prettyprint/cassandra/connection/HConnectionManagerTest.java @@ -18,9 +18,9 @@ public class HConnectionManagerTest extends BaseEmbededServerSetupTest { - - - + + + @Test public void testRemoveHost() { setupClient(); @@ -30,30 +30,30 @@ public void testRemoveHost() { assertTrue(connectionManager.addCassandraHost(cassandraHost)); assertEquals(1,connectionManager.getActivePools().size()); } - - @Test + + @Test public void testAddCassandraHostFail() { setupClient(); CassandraHost cassandraHost = new CassandraHost("127.0.0.1", 9180); assertFalse(connectionManager.addCassandraHost(cassandraHost)); } - + @Test(expected=IllegalArgumentException.class) public void testNullHostList() { HConnectionManager hcm = new HConnectionManager(clusterName, new CassandraHostConfigurator()); } - + @Test public void testMarkHostDownWithNoRetry() { cassandraHostConfigurator = new CassandraHostConfigurator("127.0.0.1:9170"); cassandraHostConfigurator.setRetryDownedHosts(false); connectionManager = new HConnectionManager(clusterName, cassandraHostConfigurator); - CassandraHost cassandraHost = new CassandraHost("127.0.0.1", 9170); + CassandraHost cassandraHost = new CassandraHost("127.0.0.1", 9170); HThriftClient client = connectionManager.borrowClient(); connectionManager.markHostAsDown(client.cassandraHost); assertEquals(0,connectionManager.getActivePools().size()); } - + @Test public void testSuspendCassandraHost() { setupClient(); @@ -62,28 +62,28 @@ public void testSuspendCassandraHost() { assertEquals(1,connectionManager.getSuspendedCassandraHosts().size()); assertTrue(connectionManager.unsuspendCassandraHost(cassandraHost)); } - + @Test(expected=HTimedOutException.class) public void testTimedOutOperateWithFailover() { setupClient(); FailoverPolicy fp = FailoverPolicy.ON_FAIL_TRY_ONE_NEXT_AVAILABLE; connectionManager.operateWithFailover(new TimeoutOp(fp)); } - + abstract class StubOp extends Operation { - StubOp(FailoverPolicy fp) { + StubOp(FailoverPolicy fp) { this(OperationType.META_READ); failoverPolicy = fp; } - + public StubOp(OperationType operationType) { super(operationType); - } + } } - + class TimeoutOp extends StubOp { - + TimeoutOp(FailoverPolicy fp) { super(fp); } diff --git a/core/src/test/java/me/prettyprint/cassandra/connection/HThriftClientTest.java b/core/src/test/java/me/prettyprint/cassandra/connection/HThriftClientTest.java index 4f9ca9a6a..2853818b6 100644 --- a/core/src/test/java/me/prettyprint/cassandra/connection/HThriftClientTest.java +++ b/core/src/test/java/me/prettyprint/cassandra/connection/HThriftClientTest.java @@ -13,18 +13,18 @@ public class HThriftClientTest extends BaseEmbededServerSetupTest { private HThriftClient hThriftClient; // cassandraHostConfigurator = new CassandraHostConfigurator("127.0.0.1:9170"); private CassandraHost cassandraHost; - + @Before public void doSetup() { cassandraHost = new CassandraHost("127.0.0.1:9170"); hThriftClient = new HThriftClient(cassandraHost); } - + @After public void doTeardown() { hThriftClient.close(); } - + @Test public void testOpenAndClose() { assertTrue(hThriftClient.open().isOpen()); @@ -35,19 +35,19 @@ public void testOpenAndClose() { public void testFailOnDoubleOpen() { hThriftClient.open().open(); } - + @Test(expected=IllegalStateException.class) public void testGetCassandraNotOpen() { hThriftClient.getCassandra(); } - + @Test public void testGetCassandraWithKeyspace() { hThriftClient.open(); hThriftClient.getCassandra("Keyspace1"); assertTrue(hThriftClient.isOpen()); } - + @Test public void testGetCassandraWithNullKeyspace() { hThriftClient.open(); @@ -55,5 +55,5 @@ public void testGetCassandraWithNullKeyspace() { hThriftClient.getCassandra(null); assertTrue(hThriftClient.isOpen()); } - + } diff --git a/core/src/test/java/me/prettyprint/cassandra/connection/HostTimeoutTrackerTest.java b/core/src/test/java/me/prettyprint/cassandra/connection/HostTimeoutTrackerTest.java index 833a34ed5..12bf10b06 100644 --- a/core/src/test/java/me/prettyprint/cassandra/connection/HostTimeoutTrackerTest.java +++ b/core/src/test/java/me/prettyprint/cassandra/connection/HostTimeoutTrackerTest.java @@ -24,7 +24,7 @@ public void setup() { HConnectionManager connectionManager = new HConnectionManager("TestCluster", cassandraHostConfigurator); hostTimeoutTracker = new HostTimeoutTracker(connectionManager, cassandraHostConfigurator); } - + @Test public void testTrackHostLatency() { CassandraHost cassandraHost = new CassandraHost("localhost:9170"); @@ -37,11 +37,11 @@ public void testTrackHostLatency() { } assertTrue(hostTimeoutTracker.checkTimeout(cassandraHost)); - // ... - // in HConnectionManager: + // ... + // in HConnectionManager: // - if ( hostLatencyTracker.checkTimeout(cassandraHost) ) // markHostAsDown(cassandraHost); // excludeHosts.add(cassandraHost); - + } } diff --git a/core/src/test/java/me/prettyprint/cassandra/connection/LeastActiveBalancingPolicyTest.java b/core/src/test/java/me/prettyprint/cassandra/connection/LeastActiveBalancingPolicyTest.java index ea8803fe0..81be48380 100644 --- a/core/src/test/java/me/prettyprint/cassandra/connection/LeastActiveBalancingPolicyTest.java +++ b/core/src/test/java/me/prettyprint/cassandra/connection/LeastActiveBalancingPolicyTest.java @@ -16,13 +16,13 @@ public class LeastActiveBalancingPolicyTest extends BaseBalancingPolicyTest { private LeastActiveBalancingPolicy leastActiveBalancingPolicy; - + @Test public void testGetPoolOk() { leastActiveBalancingPolicy = new LeastActiveBalancingPolicy(); assertEquals(poolWith5Active, leastActiveBalancingPolicy.getPool(pools, null)); assertEquals(poolWith5Active, leastActiveBalancingPolicy.getPool(pools, null)); - assertEquals(poolWith5Active, leastActiveBalancingPolicy.getPool(pools, null)); + assertEquals(poolWith5Active, leastActiveBalancingPolicy.getPool(pools, null)); Mockito.when(poolWith5Active.getNumActive()).thenReturn(8); assertEquals(poolWith7Active, leastActiveBalancingPolicy.getPool(pools, null)); assertEquals(poolWith7Active, leastActiveBalancingPolicy.getPool(pools, null)); @@ -32,33 +32,33 @@ public void testGetPoolOk() { assertEquals(poolWith5Active, leastActiveBalancingPolicy.getPool(pools, null)); assertEquals(poolWith5Active, leastActiveBalancingPolicy.getPool(pools, null)); } - + @Test - public void testSkipExhausted() { + public void testSkipExhausted() { leastActiveBalancingPolicy = new LeastActiveBalancingPolicy(); assertEquals(poolWith7Active, leastActiveBalancingPolicy.getPool(pools, new HashSet(Arrays.asList(new CassandraHost("127.0.0.1:9160"))))); assertEquals(poolWith5Active, leastActiveBalancingPolicy.getPool(pools, new HashSet(Arrays.asList(new CassandraHost("127.0.0.2:9161"))))); } - + @Test public void testShuffleOnAllEqual() { - ConcurrentHClientPool poolWith5Active2 = Mockito.mock(ConcurrentHClientPool.class); + ConcurrentHClientPool poolWith5Active2 = Mockito.mock(ConcurrentHClientPool.class); Mockito.when(poolWith5Active2.getNumActive()).thenReturn(5); Mockito.when(poolWith5Active2.getCassandraHost()).thenReturn(new CassandraHost("127.0.0.4:9163")); - ConcurrentHClientPool poolWith5Active3 = Mockito.mock(ConcurrentHClientPool.class); + ConcurrentHClientPool poolWith5Active3 = Mockito.mock(ConcurrentHClientPool.class); Mockito.when(poolWith5Active3.getNumActive()).thenReturn(5); Mockito.when(poolWith5Active3.getCassandraHost()).thenReturn(new CassandraHost("127.0.0.5:9164")); - + pools.add(poolWith5Active2); pools.add(poolWith5Active3); - + leastActiveBalancingPolicy = new LeastActiveBalancingPolicy(); // should hit all three equal hosts over the course of 50 runs Set foundHosts = new HashSet(3); for (int i = 0; i < 50; i++) { HClientPool foundPool = leastActiveBalancingPolicy.getPool(pools, null); foundHosts.add(foundPool.getCassandraHost()); - assert 5 == foundPool.getNumActive(); + assert 5 == foundPool.getNumActive(); } assertEquals(3, foundHosts.size()); } diff --git a/core/src/test/java/me/prettyprint/cassandra/connection/RoundRobinBalancingPolicyTest.java b/core/src/test/java/me/prettyprint/cassandra/connection/RoundRobinBalancingPolicyTest.java index f59e2d301..a5e56c056 100644 --- a/core/src/test/java/me/prettyprint/cassandra/connection/RoundRobinBalancingPolicyTest.java +++ b/core/src/test/java/me/prettyprint/cassandra/connection/RoundRobinBalancingPolicyTest.java @@ -15,7 +15,7 @@ public class RoundRobinBalancingPolicyTest extends BaseBalancingPolicyTest { private RoundRobinBalancingPolicy roundRobinBalancingPolicy; - + @Test public void testGetPoolOk() { roundRobinBalancingPolicy = new RoundRobinBalancingPolicy(); @@ -30,37 +30,37 @@ public void testGetPoolOk() { assertEquals(poolWith10Active.getNumActive(), roundRobinBalancingPolicy.getPool(pools, null).getNumActive()); // go to 65k to roll the counter a couple of times for (int x=0; x<(256*256); x++) { - assert roundRobinBalancingPolicy.getPool(pools, null).getNumActive() >= 5; + assert roundRobinBalancingPolicy.getPool(pools, null).getNumActive() >= 5; } } - + @Test public void testIgnoreExhausted() { Mockito.when(poolWith5Active.getCassandraHost()).thenReturn(new CassandraHost("127.0.0.1:9160")); Mockito.when(poolWith7Active.getCassandraHost()).thenReturn(new CassandraHost("127.0.0.2:9161")); Mockito.when(poolWith10Active.getCassandraHost()).thenReturn(new CassandraHost("127.0.0.3:9162")); - + roundRobinBalancingPolicy = new RoundRobinBalancingPolicy(); assertEquals(poolWith7Active, roundRobinBalancingPolicy.getPool(pools, new HashSet(Arrays.asList(new CassandraHost("127.0.0.1:9160"))))); assertEquals(poolWith10Active, roundRobinBalancingPolicy.getPool(pools, new HashSet(Arrays.asList(new CassandraHost("127.0.0.1:9160"))))); assertEquals(poolWith7Active, roundRobinBalancingPolicy.getPool(pools, new HashSet(Arrays.asList(new CassandraHost("127.0.0.1:9160"))))); assertEquals(poolWith10Active, roundRobinBalancingPolicy.getPool(pools, new HashSet(Arrays.asList(new CassandraHost("127.0.0.1:9160"))))); } - + @Test public void testIgnoreExhaustedAll() { Mockito.when(poolWith5Active.getCassandraHost()).thenReturn(new CassandraHost("127.0.0.1:9160")); Mockito.when(poolWith7Active.getCassandraHost()).thenReturn(new CassandraHost("127.0.0.2:9161")); Mockito.when(poolWith10Active.getCassandraHost()).thenReturn(new CassandraHost("127.0.0.3:9162")); - + roundRobinBalancingPolicy = new RoundRobinBalancingPolicy(); /* - assertEquals(poolWith10Active, roundRobinBalancingPolicy.getPool(pools, + assertEquals(poolWith10Active, roundRobinBalancingPolicy.getPool(pools, new HashSet(Arrays.asList(new CassandraHost("127.0.0.1:9160"),new CassandraHost("127.0.0.2:9161"))))); */ - assertNotNull(roundRobinBalancingPolicy.getPool(pools, + assertNotNull(roundRobinBalancingPolicy.getPool(pools, new HashSet(Arrays.asList(new CassandraHost("127.0.0.1:9160"),new CassandraHost("127.0.0.2:9161"),new CassandraHost("127.0.0.3:9162"))))); - - + + } } diff --git a/core/src/test/java/me/prettyprint/cassandra/dao/SimpleCassandraDaoTest.java b/core/src/test/java/me/prettyprint/cassandra/dao/SimpleCassandraDaoTest.java index 69e3612bc..ea9bf8a3b 100644 --- a/core/src/test/java/me/prettyprint/cassandra/dao/SimpleCassandraDaoTest.java +++ b/core/src/test/java/me/prettyprint/cassandra/dao/SimpleCassandraDaoTest.java @@ -17,7 +17,7 @@ public class SimpleCassandraDaoTest extends BaseEmbededServerSetupTest { @Resource private SimpleCassandraDao simpleCassandraDao; - + @Test public void testInsertGetDelete() { simpleCassandraDao.insert("fk1", "colName1", "value1"); @@ -25,5 +25,5 @@ public void testInsertGetDelete() { simpleCassandraDao.delete("colName1", "fk1"); assertNull(simpleCassandraDao.get("fk1", "colName1")); } - + } diff --git a/core/src/test/java/me/prettyprint/cassandra/jndi/CassandraClientJndiResourceFactoryTest.java b/core/src/test/java/me/prettyprint/cassandra/jndi/CassandraClientJndiResourceFactoryTest.java index 9abb2fed5..485d57634 100644 --- a/core/src/test/java/me/prettyprint/cassandra/jndi/CassandraClientJndiResourceFactoryTest.java +++ b/core/src/test/java/me/prettyprint/cassandra/jndi/CassandraClientJndiResourceFactoryTest.java @@ -50,7 +50,7 @@ public void getObjectInstance() throws Exception { resource.add(new StringRefAddr("clusterName", clusterName)); resource.add(new StringRefAddr("keyspace", "Keyspace1")); resource.add(new StringRefAddr("autoDiscoverHosts", "true")); - + Name jndiName = mock(Name.class); Context context = new InitialContext(); diff --git a/core/src/test/java/me/prettyprint/cassandra/model/CqlQueryTest.java b/core/src/test/java/me/prettyprint/cassandra/model/CqlQueryTest.java index f6c7790c0..b91cb304b 100644 --- a/core/src/test/java/me/prettyprint/cassandra/model/CqlQueryTest.java +++ b/core/src/test/java/me/prettyprint/cassandra/model/CqlQueryTest.java @@ -17,14 +17,14 @@ import org.junit.Test; public class CqlQueryTest extends BaseEmbededServerSetupTest { - + private final static String KEYSPACE = "Keyspace1"; private static final StringSerializer se = new StringSerializer(); private static final LongSerializer le = new LongSerializer(); private Cluster cluster; private Keyspace keyspace; private String cf = "StandardLong1"; - + @Before public void setupCase() { cluster = getOrCreateCluster("MyCluster", "127.0.0.1:9170"); @@ -44,16 +44,16 @@ public void setupCase() { .addInsertion("cqlQueryTest_key6", cf, createColumn("birthmonth", 6L, se, le)) .execute(); } - + @Test public void testSimpleSelect() { CqlQuery cqlQuery = new CqlQuery(keyspace, se, se, le); cqlQuery.setQuery("select * from StandardLong1"); QueryResult> result = cqlQuery.execute(); assertEquals(6,result.get().getCount()); - + } - + @Test public void testCountQuery() { CqlQuery cqlQuery = new CqlQuery(keyspace, se, se, le); @@ -69,5 +69,5 @@ public void testSyntaxFailQuery() { QueryResult> result = cqlQuery.execute(); } - + } diff --git a/core/src/test/java/me/prettyprint/cassandra/model/HColumnFamilyTest.java b/core/src/test/java/me/prettyprint/cassandra/model/HColumnFamilyTest.java index 2cc6f40f8..2cf664322 100644 --- a/core/src/test/java/me/prettyprint/cassandra/model/HColumnFamilyTest.java +++ b/core/src/test/java/me/prettyprint/cassandra/model/HColumnFamilyTest.java @@ -27,7 +27,7 @@ public class HColumnFamilyTest extends BaseEmbededServerSetupTest { - + private Keyspace keyspace; private UUID timeUUID; @Before @@ -35,7 +35,7 @@ public void setupLocal() { //setupClient(); Cluster cluster = getOrCreateCluster("MyCluster", "127.0.0.1:9170"); keyspace = createKeyspace("Keyspace1", cluster); - + Mutator mutator = HFactory.createMutator(keyspace, StringSerializer.get()); mutator.addInsertion("zznate", "Standard1", HFactory.createStringColumn("email", "nate@datastax.com")); mutator.addInsertion("zznate", "Standard1", HFactory.createColumn("int", 1, StringSerializer.get(), IntegerSerializer.get())); @@ -44,7 +44,7 @@ public void setupLocal() { mutator.addInsertion("zznate", "Standard1", HFactory.createColumn("uuid", timeUUID, StringSerializer.get(), UUIDSerializer.get())); mutator.execute(); } - + @Test public void testColumnFamilySetup() { HColumnFamily columnFamily = new HColumnFamilyImpl(keyspace, "Standard1",StringSerializer.get(), StringSerializer.get()); @@ -56,7 +56,7 @@ public void testColumnFamilySetup() { assertEquals(timeUUID, columnFamily.getUUID("uuid")); } - + @Test public void testColumnFamilyReadahead() { HColumnFamily columnFamily = new HColumnFamilyImpl(keyspace, "Standard1",StringSerializer.get(), StringSerializer.get()); @@ -68,7 +68,7 @@ public void testColumnFamilyReadahead() { assertEquals(timeUUID, columnFamily.getUUID("uuid")); } - + @Test public void testClearAndRecall() { HColumnFamily columnFamily = new HColumnFamilyImpl(keyspace, "Standard1",StringSerializer.get(), StringSerializer.get()); @@ -83,7 +83,7 @@ public void testClearAndRecall() { assertEquals(4,columnFamily.getColumns().size()); assertEquals(timeUUID, columnFamily.getUUID("uuid")); } - + @Test public void testToggleMultiget() { Mutator mutator = HFactory.createMutator(keyspace, StringSerializer.get()); @@ -93,7 +93,7 @@ public void testToggleMultiget() { timeUUID = TimeUUIDUtils.getTimeUUID(System.currentTimeMillis()); mutator.addInsertion("patricioe", "Standard1", HFactory.createColumn("uuid", timeUUID, StringSerializer.get(), UUIDSerializer.get())); mutator.execute(); - + HColumnFamilyImpl columnFamily = new HColumnFamilyImpl(keyspace, "Standard1",StringSerializer.get(), StringSerializer.get()); columnFamily.addKey("zznate").addKey("patricioe").setCount(10); assertEquals("nate@datastax.com",columnFamily.getString("email")); diff --git a/core/src/test/java/me/prettyprint/cassandra/model/IndexedSlicesQueryTest.java b/core/src/test/java/me/prettyprint/cassandra/model/IndexedSlicesQueryTest.java index 12a411030..3ed19f4de 100644 --- a/core/src/test/java/me/prettyprint/cassandra/model/IndexedSlicesQueryTest.java +++ b/core/src/test/java/me/prettyprint/cassandra/model/IndexedSlicesQueryTest.java @@ -53,7 +53,7 @@ public void teardownCase() { } @Test - public void testInsertGetRemove() { + public void testInsertGetRemove() { IndexedSlicesQuery indexedSlicesQuery = new IndexedSlicesQuery(keyspace, se, se, le); indexedSlicesQuery.addEqualsExpression("birthyear", 1975L); @@ -66,11 +66,11 @@ public void testInsertGetRemove() { } - + @Test - public void testMultiClause() { + public void testMultiClause() { - QueryResult> result = + QueryResult> result = new IndexedSlicesQuery(keyspace, se, se, le) .addEqualsExpression("birthyear", 1975L) .addGteExpression("birthmonth", 4L) @@ -85,12 +85,12 @@ public void testMultiClause() { } @Test - public void testEqClauseMiss() { - QueryResult> result = + public void testEqClauseMiss() { + QueryResult> result = new IndexedSlicesQuery(keyspace, se, se, le) .addEqualsExpression("birthyear", 5L) .addGteExpression("birthmonth", 4L) - .addLteExpression("birthmonth", 6L) + .addLteExpression("birthmonth", 6L) .setColumnNames("birthyear") .setColumnFamily(cf) .setStartKey("") @@ -100,7 +100,7 @@ public void testEqClauseMiss() { @Test public void testRowCountLimit() { - QueryResult> result = + QueryResult> result = new IndexedSlicesQuery(keyspace, se, se, le) .addEqualsExpression("birthyear", 1975L) .addGteExpression("birthmonth", 4L) @@ -112,5 +112,5 @@ public void testRowCountLimit() { .execute(); assertEquals(2, result.get().getList().size()); } - + } diff --git a/core/src/test/java/me/prettyprint/cassandra/model/MutatorTest.java b/core/src/test/java/me/prettyprint/cassandra/model/MutatorTest.java index b5f8c9f57..13705132e 100644 --- a/core/src/test/java/me/prettyprint/cassandra/model/MutatorTest.java +++ b/core/src/test/java/me/prettyprint/cassandra/model/MutatorTest.java @@ -65,23 +65,23 @@ public void testInsertAndDeleteSuper() { List> columnList = new ArrayList>(); columnList.add(createColumn("name","value",se,se)); HSuperColumn superColumn = createSuperColumn("super_name", columnList, se, se, se); - + // Insert Super Column MutationResult r = m.insert("sk", "Super1", superColumn); - + assertTrue("Execute time should be > 0", r.getExecutionTimeMicro() > 0); assertTrue("Should have operated on a host", r.getHostUsed() != null); - + // Fetch and verify it exists. SuperColumnQuery scq = HFactory.createSuperColumnQuery(keyspace, se, se, se, se); scq.setColumnFamily("Super1"); scq.setKey("sk"); scq.setSuperName("super_name"); assertEquals("super_name", scq.execute().get().getName()); - + // Remove the Super Column m.superDelete("sk", "Super1", "super_name", se); - + // Fetch and verify it exists. scq = HFactory.createSuperColumnQuery(keyspace, se, se, se, se); scq.setColumnFamily("Super1"); @@ -100,21 +100,21 @@ public void testSubDelete() { HSuperColumn superColumn = createSuperColumn("super_name", columnList, se, se, se); MutationResult r = m.insert("sk1", "Super1", superColumn); - + SuperColumnQuery scq = HFactory.createSuperColumnQuery(keyspace, se, se, se, se); scq.setColumnFamily("Super1"); scq.setKey("sk1"); scq.setSuperName("super_name"); assertEquals(3,scq.execute().get().getColumns().size()); - + m.discardPendingMutations(); - + m.addSubDelete("sk1", "Super1", "super_name", "col_1", se, se); m.execute(); - + assertEquals(2,scq.execute().get().getColumns().size()); } - + @Test public void testSubDeleteHSuperColumn() { Mutator m = createMutator(keyspace, se); @@ -125,23 +125,23 @@ public void testSubDeleteHSuperColumn() { HSuperColumn superColumn = createSuperColumn("super_name", columnList, se, se, se); MutationResult r = m.insert("sk1", "Super1", superColumn); - + SuperColumnQuery scq = HFactory.createSuperColumnQuery(keyspace, se, se, se, se); scq.setColumnFamily("Super1"); scq.setKey("sk1"); scq.setSuperName("super_name"); assertEquals(3,scq.execute().get().getColumns().size()); - + m.discardPendingMutations(); columnList.remove(1); columnList.remove(0); superColumn.setSubcolumns(columnList); m.addSubDelete("sk1", "Super1", superColumn); m.execute(); - + assertEquals(2,scq.execute().get().getColumns().size()); } - + @Test public void testBatchMutationManagement() { String cf = "Standard1"; @@ -209,7 +209,7 @@ public void testRowDeletion() { setColumnFamily(cf).setKey("key0").setName("name").execute(); assertNull(columnResult.get()); } - + @Test public void testInsertCounter() { Mutator m = createMutator(keyspace, se); diff --git a/core/src/test/java/me/prettyprint/cassandra/model/RangeSlicesQueryTest.java b/core/src/test/java/me/prettyprint/cassandra/model/RangeSlicesQueryTest.java index b0925b611..63dc5e69a 100644 --- a/core/src/test/java/me/prettyprint/cassandra/model/RangeSlicesQueryTest.java +++ b/core/src/test/java/me/prettyprint/cassandra/model/RangeSlicesQueryTest.java @@ -47,17 +47,17 @@ public void setupCase() { .addInsertion("rangeSlicesTest_key6", cf, createColumn("birthmonth", 6L, se, le)) .execute(); } - + @Test public void testKeysOnlyPredicate() { RangeSlicesQuery rangeSlicesQuery = HFactory.createRangeSlicesQuery(keyspace, se, se, le); - QueryResult> result = + QueryResult> result = rangeSlicesQuery.setColumnFamily(cf).setKeys("", "").setReturnKeysOnly().execute(); OrderedRows orderedRows = result.get(); Row row = orderedRows.iterator().next(); assertNotNull(row.getKey()); assertEquals(0,row.getColumnSlice().getColumns().size()); - + result = rangeSlicesQuery.setColumnNames("birthyear","birthmonth").setRowCount(5).execute(); orderedRows = result.get(); row = orderedRows.iterator().next(); diff --git a/core/src/test/java/me/prettyprint/cassandra/serializers/BigIntegerSerializerTest.java b/core/src/test/java/me/prettyprint/cassandra/serializers/BigIntegerSerializerTest.java index 51bc09d8c..73c69d2b2 100644 --- a/core/src/test/java/me/prettyprint/cassandra/serializers/BigIntegerSerializerTest.java +++ b/core/src/test/java/me/prettyprint/cassandra/serializers/BigIntegerSerializerTest.java @@ -8,14 +8,14 @@ import org.junit.Test; public class BigIntegerSerializerTest { - + @Test public void testToAndFrom() { BigInteger bi = new BigInteger(new byte[]{0,0,0,1}); ByteBuffer bb = BigIntegerSerializer.get().toByteBuffer(bi); assertTrue(bb != null); assertEquals(1,bb.array().length); - assertEquals(bi, BigIntegerSerializer.get().fromByteBuffer(bb)); + assertEquals(bi, BigIntegerSerializer.get().fromByteBuffer(bb)); } } diff --git a/core/src/test/java/me/prettyprint/cassandra/serializers/FastInfosetSerializerTest.java b/core/src/test/java/me/prettyprint/cassandra/serializers/FastInfosetSerializerTest.java index 4c1e11203..d47a7f0bf 100644 --- a/core/src/test/java/me/prettyprint/cassandra/serializers/FastInfosetSerializerTest.java +++ b/core/src/test/java/me/prettyprint/cassandra/serializers/FastInfosetSerializerTest.java @@ -4,7 +4,7 @@ /** * @author shuzhang0@gmail.com - * + * */ public class FastInfosetSerializerTest extends JaxbSerializerTest { diff --git a/core/src/test/java/me/prettyprint/cassandra/serializers/IntegerSerializerTest.java b/core/src/test/java/me/prettyprint/cassandra/serializers/IntegerSerializerTest.java index 99e193d7e..fb1e935e3 100644 --- a/core/src/test/java/me/prettyprint/cassandra/serializers/IntegerSerializerTest.java +++ b/core/src/test/java/me/prettyprint/cassandra/serializers/IntegerSerializerTest.java @@ -9,14 +9,14 @@ import org.junit.Test; /** - * + * * @author Bozhidar Bozhanov - * + * */ public class IntegerSerializerTest { static IntegerSerializer ext = IntegerSerializer.get(); - + @Test public void testConversions() { test(0); @@ -35,7 +35,7 @@ public void testFromCassandra() { assertEquals(new Integer(Integer.MAX_VALUE), ext.fromByteBuffer(ByteBufferUtil.bytes(Integer.MAX_VALUE))); assertEquals(new Integer(Integer.MIN_VALUE), ext.fromByteBuffer(ByteBufferUtil.bytes(Integer.MIN_VALUE))); } - + @Test public void testFromCassandraAsBytes() { assertEquals(new Integer(1), ext.fromBytes(ByteBufferUtil.bytes(1).array())); @@ -44,10 +44,10 @@ public void testFromCassandraAsBytes() { assertEquals(new Integer(Integer.MAX_VALUE), ext.fromBytes(ByteBufferUtil.bytes(Integer.MAX_VALUE).array())); assertEquals(new Integer(Integer.MIN_VALUE), ext.fromBytes(ByteBufferUtil.bytes(Integer.MIN_VALUE).array())); } - - + + private void test(Integer number) { - + assertEquals(number, ext.fromByteBuffer(ext.toByteBuffer(number))); // test compatibility with ByteBuffer default byte order diff --git a/core/src/test/java/me/prettyprint/cassandra/serializers/JaxbSerializerTest.java b/core/src/test/java/me/prettyprint/cassandra/serializers/JaxbSerializerTest.java index 6411a5152..08281ec9a 100644 --- a/core/src/test/java/me/prettyprint/cassandra/serializers/JaxbSerializerTest.java +++ b/core/src/test/java/me/prettyprint/cassandra/serializers/JaxbSerializerTest.java @@ -11,9 +11,9 @@ /** * Unit test for {@link JaxbSerializer}. - * + * * @author shuzhang0@gmail.com - * + * */ public class JaxbSerializerTest extends SerializerBaseTest { diff --git a/core/src/test/java/me/prettyprint/cassandra/serializers/LongSerializerTest.java b/core/src/test/java/me/prettyprint/cassandra/serializers/LongSerializerTest.java index 60555eaa9..31146eda1 100644 --- a/core/src/test/java/me/prettyprint/cassandra/serializers/LongSerializerTest.java +++ b/core/src/test/java/me/prettyprint/cassandra/serializers/LongSerializerTest.java @@ -11,7 +11,7 @@ public class LongSerializerTest { static LongSerializer ext = LongSerializer.get(); - + @Test public void testConversions() { test(0l); @@ -30,9 +30,9 @@ public void testFromCassandra() { assertEquals(new Long(Long.MIN_VALUE), ext.fromByteBuffer(ByteBufferUtil.bytes(Long.MIN_VALUE))); assertEquals(new Long(Long.MAX_VALUE), ext.fromByteBuffer(ByteBufferUtil.bytes(Long.MAX_VALUE))); } - + private void test(Long number) { - + assertEquals(number, ext.fromByteBuffer(ext.toByteBuffer(number))); // test compatibility with ByteBuffer default byte order diff --git a/core/src/test/java/me/prettyprint/cassandra/serializers/SerializerBaseTest.java b/core/src/test/java/me/prettyprint/cassandra/serializers/SerializerBaseTest.java index 395544aac..2645f1089 100644 --- a/core/src/test/java/me/prettyprint/cassandra/serializers/SerializerBaseTest.java +++ b/core/src/test/java/me/prettyprint/cassandra/serializers/SerializerBaseTest.java @@ -14,9 +14,9 @@ * Tests for:
    * 1. Valid serialization roundtrip for a collection of objects.
    * 2. Proper handling of NULL. - * + * * @author shuzhang0@gmail.com - * + * * @param * the type which the Serializer under test can serialize. */ diff --git a/core/src/test/java/me/prettyprint/cassandra/serializers/ShortSerializerTest.java b/core/src/test/java/me/prettyprint/cassandra/serializers/ShortSerializerTest.java index a54810691..70094d851 100644 --- a/core/src/test/java/me/prettyprint/cassandra/serializers/ShortSerializerTest.java +++ b/core/src/test/java/me/prettyprint/cassandra/serializers/ShortSerializerTest.java @@ -7,9 +7,9 @@ /** * Unit tests for {@link ShortSerializer}. - * + * * @author shuzhang0@gmail.com - * + * */ public class ShortSerializerTest extends SerializerBaseTest { diff --git a/core/src/test/java/me/prettyprint/cassandra/service/CassandraAuthTest.java b/core/src/test/java/me/prettyprint/cassandra/service/CassandraAuthTest.java index 79f583503..5289a17e3 100644 --- a/core/src/test/java/me/prettyprint/cassandra/service/CassandraAuthTest.java +++ b/core/src/test/java/me/prettyprint/cassandra/service/CassandraAuthTest.java @@ -54,7 +54,7 @@ public class CassandraAuthTest { private CassandraHostConfigurator cassandraHostConfigurator; private HConnectionManager connectionManager; private String clusterName = "TestCluster"; - + /** * Set embedded cassandra up and spawn it in a new thread. */ @@ -135,11 +135,11 @@ public void testAddDropKeyspace() throws Exception { String ksid2 = cassandraCluster.dropKeyspace("DynKeyspace"); assertNotNull(ksid2); } - + @Test public void testInsertAndGetAndRemove() throws IllegalArgumentException, NoSuchElementException, IllegalStateException, HNotFoundException, Exception { - KeyspaceService keyspace = new KeyspaceServiceImpl("Keyspace1", new QuorumAllConsistencyLevelPolicy(), + KeyspaceService keyspace = new KeyspaceServiceImpl("Keyspace1", new QuorumAllConsistencyLevelPolicy(), connectionManager, FailoverPolicy.ON_FAIL_TRY_ALL_AVAILABLE, user1Credentials); // insert value @@ -177,7 +177,7 @@ public void testInsertAndGetAndRemove() throws IllegalArgumentException, NoSuchE @Test public void testInsertAndGetAndRemoveBadAuth() throws IllegalArgumentException, NoSuchElementException, IllegalStateException, HNotFoundException, Exception { - KeyspaceService keyspace = new KeyspaceServiceImpl("Keyspace1", new QuorumAllConsistencyLevelPolicy(), + KeyspaceService keyspace = new KeyspaceServiceImpl("Keyspace1", new QuorumAllConsistencyLevelPolicy(), connectionManager, FailoverPolicy.ON_FAIL_TRY_ALL_AVAILABLE, user1CredentialsBad); try { // insert value diff --git a/core/src/test/java/me/prettyprint/cassandra/service/CassandraClusterTest.java b/core/src/test/java/me/prettyprint/cassandra/service/CassandraClusterTest.java index 5b334f552..727ef400e 100644 --- a/core/src/test/java/me/prettyprint/cassandra/service/CassandraClusterTest.java +++ b/core/src/test/java/me/prettyprint/cassandra/service/CassandraClusterTest.java @@ -91,7 +91,7 @@ public void testAddDropColumnFamily() throws Exception { String cfid2 = cassandraCluster.dropColumnFamily("Keyspace1", "DynCf"); assertNotNull(cfid2); } - + @Test public void testTruncateColumnFamily() throws Exception { ColumnFamilyDefinition cfDef = HFactory.createColumnFamilyDefinition("Keyspace1", "TruncateableCf"); @@ -115,65 +115,65 @@ public void testAddDropKeyspace() throws Exception { String ksid2 = cassandraCluster.dropKeyspace("DynKeyspace"); assertNotNull(ksid2); } - + @Test public void testEditKeyspace() throws Exception { - + BasicColumnFamilyDefinition columnFamilyDefinition = new BasicColumnFamilyDefinition(); columnFamilyDefinition.setKeyspaceName("DynKeyspace2"); - columnFamilyDefinition.setName("DynamicCF"); - + columnFamilyDefinition.setName("DynamicCF"); + ColumnFamilyDefinition cfDef = new ThriftCfDef(columnFamilyDefinition); - - KeyspaceDefinition keyspaceDefinition = + + KeyspaceDefinition keyspaceDefinition = HFactory.createKeyspaceDefinition("DynKeyspace2", "org.apache.cassandra.locator.SimpleStrategy", 1, Arrays.asList(cfDef)); - + cassandraCluster.addKeyspace(keyspaceDefinition); - - keyspaceDefinition = + + keyspaceDefinition = HFactory.createKeyspaceDefinition("DynKeyspace2", "org.apache.cassandra.locator.SimpleStrategy", 2, null); - + cassandraCluster.updateKeyspace(keyspaceDefinition); - + KeyspaceDefinition fromCluster = cassandraCluster.describeKeyspace("DynKeyspace2"); assertEquals(2,fromCluster.getReplicationFactor()); cassandraCluster.dropKeyspace("DynKeyspace2"); } - + @Test public void testEditColumnFamily() throws Exception { - + BasicColumnFamilyDefinition columnFamilyDefinition = new BasicColumnFamilyDefinition(); columnFamilyDefinition.setKeyspaceName("DynKeyspace3"); - columnFamilyDefinition.setName("DynamicCF"); - + columnFamilyDefinition.setName("DynamicCF"); + ColumnFamilyDefinition cfDef = new ThriftCfDef(columnFamilyDefinition); - - KeyspaceDefinition keyspaceDefinition = + + KeyspaceDefinition keyspaceDefinition = HFactory.createKeyspaceDefinition("DynKeyspace3", "org.apache.cassandra.locator.SimpleStrategy", 1, Arrays.asList(cfDef)); - + cassandraCluster.addKeyspace(keyspaceDefinition); - - + + KeyspaceDefinition fromCluster = cassandraCluster.describeKeyspace("DynKeyspace3"); cfDef = fromCluster.getCfDefs().get(0); - + columnFamilyDefinition = new BasicColumnFamilyDefinition(cfDef); BasicColumnDefinition columnDefinition = new BasicColumnDefinition(); columnDefinition.setName(StringSerializer.get().toByteBuffer("birthdate")); columnDefinition.setIndexType(ColumnIndexType.KEYS); columnDefinition.setValidationClass(ComparatorType.LONGTYPE.getClassName()); columnFamilyDefinition.addColumnDefinition(columnDefinition); - + columnDefinition = new BasicColumnDefinition(); - columnDefinition.setName(StringSerializer.get().toByteBuffer("nonindexed_field")); + columnDefinition.setName(StringSerializer.get().toByteBuffer("nonindexed_field")); columnDefinition.setValidationClass(ComparatorType.LONGTYPE.getClassName()); - columnFamilyDefinition.addColumnDefinition(columnDefinition); - + columnFamilyDefinition.addColumnDefinition(columnDefinition); + cassandraCluster.updateColumnFamily(new ThriftCfDef(columnFamilyDefinition)); - + fromCluster = cassandraCluster.describeKeyspace("DynKeyspace3"); - + assertEquals("birthdate",StringSerializer.get().fromByteBuffer(fromCluster.getCfDefs().get(0).getColumnMetadata().get(0).getName())); assertEquals("nonindexed_field",StringSerializer.get().fromByteBuffer(fromCluster.getCfDefs().get(0).getColumnMetadata().get(1).getName())); } diff --git a/core/src/test/java/me/prettyprint/cassandra/service/template/BaseColumnFamilyTemplateTest.java b/core/src/test/java/me/prettyprint/cassandra/service/template/BaseColumnFamilyTemplateTest.java index b82e729c5..d8b426e10 100644 --- a/core/src/test/java/me/prettyprint/cassandra/service/template/BaseColumnFamilyTemplateTest.java +++ b/core/src/test/java/me/prettyprint/cassandra/service/template/BaseColumnFamilyTemplateTest.java @@ -14,7 +14,7 @@ public abstract class BaseColumnFamilyTemplateTest extends BaseEmbededServerSetu protected Keyspace keyspace; static final StringSerializer se = StringSerializer.get(); - + @Before public void setupLocal() { Cluster cluster = getOrCreateCluster("MyCluster", "127.0.0.1:9170"); diff --git a/core/src/test/java/me/prettyprint/cassandra/service/template/ColumnFamilyTemplateTest.java b/core/src/test/java/me/prettyprint/cassandra/service/template/ColumnFamilyTemplateTest.java index 665b7807d..f5edb646e 100644 --- a/core/src/test/java/me/prettyprint/cassandra/service/template/ColumnFamilyTemplateTest.java +++ b/core/src/test/java/me/prettyprint/cassandra/service/template/ColumnFamilyTemplateTest.java @@ -10,25 +10,25 @@ import org.junit.Test; public class ColumnFamilyTemplateTest extends BaseColumnFamilyTemplateTest { - + @Test public void testCreateSelect() { ColumnFamilyTemplate template = new ThriftColumnFamilyTemplate(keyspace, "Standard1", se, se, HFactory.createMutator(keyspace, se)); - - ColumnFamilyUpdater updater = template.createUpdater("key1"); + + ColumnFamilyUpdater updater = template.createUpdater("key1"); updater.setString("column1","value1"); template.update(updater); - + template.addColumn("column1", se); - ColumnFamilyResult wrapper = template.queryColumns("key1"); + ColumnFamilyResult wrapper = template.queryColumns("key1"); assertEquals("value1",wrapper.getString("column1")); - + } @Test public void testCreateSelectTemplate() { ColumnFamilyTemplate template = new ThriftColumnFamilyTemplate(keyspace, "Standard1", se, se, HFactory.createMutator(keyspace, se)); - ColumnFamilyUpdater updater = template.createUpdater("key1"); + ColumnFamilyUpdater updater = template.createUpdater("key1"); updater.setString("column1","value1"); updater.update(); template.setCount(10); @@ -39,34 +39,34 @@ public String mapRow(ColumnFamilyResult results) { return results.getString("column1"); } }); - assertEquals("value1",value); + assertEquals("value1",value); } - + @Test public void testQueryMultiget() { - ColumnFamilyTemplate template = new ThriftColumnFamilyTemplate(keyspace, "Standard1", se, se, HFactory.createMutator(keyspace, se)); - ColumnFamilyUpdater updater = template.createUpdater("mg_key1"); + ColumnFamilyTemplate template = new ThriftColumnFamilyTemplate(keyspace, "Standard1", se, se, HFactory.createMutator(keyspace, se)); + ColumnFamilyUpdater updater = template.createUpdater("mg_key1"); updater.setString("column1","value1"); updater.addKey("mg_key2"); updater.setString("column1","value2"); updater.addKey("mg_key3"); updater.setString("column1","value3"); template.update(updater); - + template.addColumn("column1", se); ColumnFamilyResult wrapper = template.queryColumns(Arrays.asList("mg_key1", "mg_key2", "mg_key3")); - assertEquals("value1",wrapper.getString("column1")); + assertEquals("value1",wrapper.getString("column1")); wrapper.next(); assertEquals("value2",wrapper.getString("column1")); wrapper.next(); assertEquals("value3",wrapper.getString("column1")); } - + @Test public void testHasNoResults() { - ColumnFamilyTemplate template = new ThriftColumnFamilyTemplate(keyspace, "Standard1", se, se, HFactory.createMutator(keyspace, se)); + ColumnFamilyTemplate template = new ThriftColumnFamilyTemplate(keyspace, "Standard1", se, se, HFactory.createMutator(keyspace, se)); assertFalse(template.queryColumns("noresults").hasResults()); - + } } diff --git a/core/src/test/java/me/prettyprint/cassandra/service/template/SuperCfTemplateTest.java b/core/src/test/java/me/prettyprint/cassandra/service/template/SuperCfTemplateTest.java index 31d04e24a..94809d7c2 100644 --- a/core/src/test/java/me/prettyprint/cassandra/service/template/SuperCfTemplateTest.java +++ b/core/src/test/java/me/prettyprint/cassandra/service/template/SuperCfTemplateTest.java @@ -13,27 +13,27 @@ public class SuperCfTemplateTest extends BaseColumnFamilyTemplateTest { @Test public void testSuperCfInsertReadTemplate() { - SuperCfTemplate sTemplate = + SuperCfTemplate sTemplate = new ThriftSuperCfTemplate(keyspace, "Super1", se, se, se); SuperCfUpdater sUpdater = sTemplate.createUpdater("skey1","super1"); sUpdater.setString("sub_col_1", "sub_val_1"); sTemplate.update(sUpdater); SuperCfResult result = sTemplate.querySuperColumn("skey1", "super1"); - + assertEquals("sub_val_1",result.getString("super1","sub_col_1")); - + sUpdater.deleteSuperColumn(); sTemplate.update(sUpdater); assertEquals("super1",sUpdater.getCurrentSuperColumn()); result = sTemplate.querySuperColumn("skey1", "super1"); assertFalse(result.hasResults()); } - - + + @Test public void testSuperCfMultiSc() { - SuperCfTemplate sTemplate = + SuperCfTemplate sTemplate = new ThriftSuperCfTemplate(keyspace, "Super1", se, se, se); SuperCfUpdater sUpdater = sTemplate.createUpdater("skey2","super1"); sUpdater.setString("sub1_col_1", "sub1_val_1"); @@ -46,40 +46,40 @@ public void testSuperCfMultiSc() { /*for (String sName : result.getSuperColumns() ) { result.getString(sName,"sub1_col_1"); }*/ - + //assertEquals("sub1_val_1",result.getString("sub1_col_1")); //assertEquals("sub2_val_1",result.next().getString("sub2_col_1")); - + } - + @Test public void testQuerySingleSubColumn() { - SuperCfTemplate sTemplate = + SuperCfTemplate sTemplate = new ThriftSuperCfTemplate(keyspace, "Super1", se, se, se); SuperCfUpdater sUpdater = sTemplate.createUpdater("skey3","super1"); sUpdater.setString("sub1_col_1", "sub1_val_1"); sTemplate.update(sUpdater); - + HColumn myCol = sTemplate.querySingleSubColumn("skey3", "super1", "sub1_col_1", se); assertEquals("sub1_val_1", myCol.getValue()); } - - + + @Test public void testQuerySingleSubColumnEmpty() { - SuperCfTemplate sTemplate = + SuperCfTemplate sTemplate = new ThriftSuperCfTemplate(keyspace, "Super1", se, se, se); SuperCfUpdater sUpdater = sTemplate.createUpdater("skey3","super1"); sUpdater.setString("sub1_col_1", "sub1_val_1"); sTemplate.update(sUpdater); - + HColumn myCol = sTemplate.querySingleSubColumn("skey3", "super2", "sub1_col_1", se); assertNull(myCol); } - + @Test public void testSuperCfInsertReadMultiKey() { - SuperCfTemplate sTemplate = + SuperCfTemplate sTemplate = new ThriftSuperCfTemplate(keyspace, "Super1", se, se, se); SuperCfUpdater sUpdater = sTemplate.createUpdater("s_multi_key1","super1"); sUpdater.setString("sub_col_1", "sub_val_1"); @@ -92,12 +92,12 @@ public void testSuperCfInsertReadMultiKey() { assertTrue(result.hasResults()); assertEquals("sub_val_2",result.getString("super1","sub_col_1")); assertEquals("sub_val_1",result.next().getString("super1","sub_col_1")); - + } - + @Test public void testSuperCfInsertReadMultiKeyNoSc() { - SuperCfTemplate sTemplate = + SuperCfTemplate sTemplate = new ThriftSuperCfTemplate(keyspace, "Super1", se, se, se); SuperCfUpdater sUpdater = sTemplate.createUpdater("s_multi_key1","super1"); sUpdater.setString("sub_col_1", "sub_val_1"); @@ -110,107 +110,107 @@ public void testSuperCfInsertReadMultiKeyNoSc() { assertTrue(result.hasResults()); assertEquals("sub_val_2",result.getString("super1","sub_col_1")); assertEquals("sub_val_1",result.next().getString("super1","sub_col_1")); - + } @Test public void testSuperCfKeyOnly() { - SuperCfTemplate sTemplate = + SuperCfTemplate sTemplate = new ThriftSuperCfTemplate(keyspace, "Super1", se, se, se); SuperCfUpdater sUpdater = sTemplate.createUpdater("skey1","super1"); sUpdater.setString("sub_col_1", "sub_val_1"); sUpdater.addSuperColumn("super2"); sUpdater.setString("sub_col_1", "sub_val_2"); sTemplate.update(sUpdater); - - SuperCfResult result = sTemplate.querySuperColumns("skey1"); + + SuperCfResult result = sTemplate.querySuperColumns("skey1"); assertEquals(2, result.getSuperColumns().size()); assertTrue(result.hasResults()); result = sTemplate.querySuperColumns("skey1-non-existing-key"); assertNull(result.getActiveSuperColumn()); } - + @Test public void testSuperCfNoResults() { - SuperCfTemplate sTemplate = + SuperCfTemplate sTemplate = new ThriftSuperCfTemplate(keyspace, "Super1", se, se, se); - + assertFalse(sTemplate.querySuperColumns("no_results").hasResults()); } - + @Test public void testDeleteSubColumns() { - SuperCfTemplate sTemplate = + SuperCfTemplate sTemplate = new ThriftSuperCfTemplate(keyspace, "Super1", se, se, se); SuperCfUpdater sUpdater = sTemplate.createUpdater("skey3","super1"); sUpdater.setString("sub1_col_1", "sub1_val_1"); sUpdater.setString("sub1_col_2", "sub1_val_2"); sUpdater.setString("sub1_col_3", "sub1_val_3"); sTemplate.update(sUpdater); - + SuperCfResult result = sTemplate.querySuperColumn("skey3","super1"); assertEquals(3, result.getColumnNames().size()); - + sUpdater.deleteSubColumn("sub1_col_1"); sTemplate.update(sUpdater); - + result = sTemplate.querySuperColumn("skey3","super1"); assertEquals(2, result.getColumnNames().size()); } - + @Test public void testTemplateLevelDeleteSuper() { - SuperCfTemplate sTemplate = + SuperCfTemplate sTemplate = new ThriftSuperCfTemplate(keyspace, "Super1", se, se, se); SuperCfUpdater sUpdater = sTemplate.createUpdater("skey_del_super","super1"); sUpdater.setString("sub1_col_1", "sub1_val_1"); sTemplate.update(sUpdater); - + SuperCfResult result = sTemplate.querySuperColumn("skey_del_super","super1"); assertEquals(1, result.getColumnNames().size()); - - sTemplate.deleteColumn("skey_del_super", "super1"); - + + sTemplate.deleteColumn("skey_del_super", "super1"); + result = sTemplate.querySuperColumn("skey_del_super","super1"); assertFalse(result.hasResults()); assertEquals(0, result.getColumnNames().size()); } - + @Test public void testTemplateLevelDeleteRow() { - SuperCfTemplate sTemplate = + SuperCfTemplate sTemplate = new ThriftSuperCfTemplate(keyspace, "Super1", se, se, se); SuperCfUpdater sUpdater = sTemplate.createUpdater("skey_row_del","super1"); sUpdater.setString("sub1_col_1", "sub1_val_1"); sTemplate.update(sUpdater); - + SuperCfResult result = sTemplate.querySuperColumn("skey_row_del","super1"); assertEquals(1, result.getColumnNames().size()); - - sTemplate.deleteRow("skey_row_del"); - + + sTemplate.deleteRow("skey_row_del"); + result = sTemplate.querySuperColumns("skey_row_del"); assertFalse(result.hasResults()); assertEquals(0, result.getSuperColumns().size()); } - + @Test public void testTemplateLevelDeleteMiss() { - SuperCfTemplate sTemplate = + SuperCfTemplate sTemplate = new ThriftSuperCfTemplate(keyspace, "Super1", se, se, se); SuperCfUpdater sUpdater = sTemplate.createUpdater("skey_row_del_miss","super1"); sUpdater.setString("sub1_col_1", "sub1_val_1"); sTemplate.update(sUpdater); - + SuperCfResult result = sTemplate.querySuperColumn("skey_row_del_miss","super1"); assertEquals(1, result.getColumnNames().size()); - - sTemplate.deleteRow("skey_row_miss_foo"); + + sTemplate.deleteRow("skey_row_miss_foo"); sTemplate.deleteColumn("skey_row_del", "foo"); - + result = sTemplate.querySuperColumns("skey_row_del_miss"); assertTrue(result.hasResults()); assertEquals(1, result.getSuperColumns().size()); } - + } diff --git a/core/src/test/java/me/prettyprint/hector/api/ApiV2SystemTest.java b/core/src/test/java/me/prettyprint/hector/api/ApiV2SystemTest.java index 5ca72af06..a6fc7b744 100644 --- a/core/src/test/java/me/prettyprint/hector/api/ApiV2SystemTest.java +++ b/core/src/test/java/me/prettyprint/hector/api/ApiV2SystemTest.java @@ -93,16 +93,16 @@ public void teardownCase() { ko = null; cluster = null; } - + @Test public void testInsertGetRemoveCounter() { String cf = "Counter1"; Mutator m = createMutator(ko, se); - MutationResult mr = m.insertCounter("testInsertGetRemoveCounter", cf, + MutationResult mr = m.insertCounter("testInsertGetRemoveCounter", cf, createCounterColumn("testInsertGetRemoveCounter_name", 25)); log.debug("insert execution time: {}", mr.getExecutionTimeMicro()); - + // get value CounterQuery q = createCounterColumnQuery(ko, se, se); q.setColumnFamily(cf).setName("testInsertGetRemoveCounter_name"); @@ -116,8 +116,8 @@ public void testInsertGetRemoveCounter() { assertEquals(25, value.longValue()); String name = c.getName(); assertEquals("testInsertGetRemoveCounter_name", name); - assertEquals(q, r.getQuery()); - + assertEquals(q, r.getQuery()); + // remove value m = createMutator(ko, se); MutationResult mr2 = m.deleteCounter("testInsertGetRemoveCounter", cf, "testInsertGetRemoveCounter_name", se); @@ -130,7 +130,7 @@ public void testInsertGetRemoveCounter() { assertNotNull(r2); assertNull("Value should have been deleted", r2.get()); } - + @Test public void testIncrementDecrementCounter() { String cf = "Counter1"; @@ -138,7 +138,7 @@ public void testIncrementDecrementCounter() { createMutator(ko, se).decrementCounter("testIncrementDecrementCounter", cf, "testIncrementDecrementCounter_name", 2); // The total in the counter is 5. (7 - 2) - + // get value CounterQuery q = createCounterColumnQuery(ko, se, se); q.setColumnFamily(cf).setName("testIncrementDecrementCounter_name"); @@ -188,7 +188,7 @@ public void testInsertGetRemove() { m = createMutator(ko, se); MutationResult mr2 = m.delete("testInsertGetRemove", cf, "testInsertGetRemove", se); - + // get already removed value ColumnQuery q2 = createColumnQuery(ko, se, se, se); @@ -212,7 +212,7 @@ public void testBatchInsertGetRemove() { se, se)); } m.execute(); - + // get value ColumnQuery q = createColumnQuery(ko, se, se, se); q.setName("testInsertGetRemove").setColumnFamily(cf); @@ -442,7 +442,7 @@ public void testSliceQuery() { deleteColumns(cleanup); } - + @Test public void testCounterSliceQuery() { String cf = "Counter1"; @@ -458,29 +458,29 @@ public void testCounterSliceQuery() { SliceCounterQuery q = createCounterSliceQuery(ko, se, se); q.setColumnFamily(cf); q.setKey("testCounterSliceQuery_key"); - + // try with column name first q.setColumnNames("4", "5", "6"); QueryResult> r = q.execute(); - + assertNotNull(r); - + CounterSlice slice = r.get(); - + assertNotNull(slice); - + assertEquals(3, slice.getColumns().size()); - + // Test slice.getColumnByName assertEquals(4, slice.getColumnByName("4").getValue().longValue()); assertEquals(5, slice.getColumnByName("5").getValue().longValue()); assertEquals(6, slice.getColumnByName("6").getValue().longValue()); - + // Test slice.getColumns List> columns = slice.getColumns(); assertNotNull(columns); assertEquals(3, columns.size()); - + // Cleanup mutator.deleteCounter("testCounterSliceQuery_key", cf, null, se); mutator.execute(); @@ -1036,9 +1036,9 @@ private TestCleanupDescriptor insertColumns(String cf, int rowCount, /** * A class describing what kind of cleanup is required at the end of the test. * Just some bookeeping, that's all. - * + * * @author Ran Tavory - * + * */ private static class TestCleanupDescriptor { public final String cf; diff --git a/object-mapper/src/main/java/me/prettyprint/hom/CFMappingDef.java b/object-mapper/src/main/java/me/prettyprint/hom/CFMappingDef.java index 40a74e677..8c45e474f 100644 --- a/object-mapper/src/main/java/me/prettyprint/hom/CFMappingDef.java +++ b/object-mapper/src/main/java/me/prettyprint/hom/CFMappingDef.java @@ -19,10 +19,10 @@ /** * Holder for the mapping between a Class annotated with {@link Entity} and the * Cassandra column family name. - * + * * @author Todd Burruss - * - * @param + * + * @param */ public class CFMappingDef { private Class realClass; @@ -54,7 +54,7 @@ public CFMappingDef(Class clazz) { /** * Setup mapping with defaults for the given class. Does not parse all * annotations. - * + * * @param realClass */ public void setDefaults(Class realClass) { @@ -107,7 +107,7 @@ public void addPropertyDefinition(PropertyMappingDefinition propDef) { public String getColFamName() { return colFamName; } - + public String getEffectiveColFamName() { if ( null != colFamName ) { return colFamName; diff --git a/object-mapper/src/main/java/me/prettyprint/hom/ClassCacheMgr.java b/object-mapper/src/main/java/me/prettyprint/hom/ClassCacheMgr.java index f1402293d..8b6d5d7f9 100644 --- a/object-mapper/src/main/java/me/prettyprint/hom/ClassCacheMgr.java +++ b/object-mapper/src/main/java/me/prettyprint/hom/ClassCacheMgr.java @@ -30,7 +30,7 @@ /** * Manage parsing and caching of class meta-data. - * + * * @author */ public class ClassCacheMgr { @@ -46,9 +46,9 @@ public class ClassCacheMgr { * Examine class hierarchy using {@link CFMappingDef} objects to discover the * given class' "base inheritance" class. A base inheritance class is * determined by {@link CFMappingDef#isBaseInheritanceClass()} - * + * * @param - * + * * @param cfMapDef * @return returns the base in the ColumnFamily mapping hierarchy */ @@ -66,7 +66,7 @@ public CFMappingDef findBaseClassViaMappings(CFMappingDef cfMa /** * Retrieve class mapping meta-data by Class object. - * + * * @param * @param clazz * @param throwException @@ -88,7 +88,7 @@ public CFMappingDef getCfMapDef(Class clazz, boolean throwException) { /** * Retrieve class mapping meta-data by ColumnFamily name. - * + * * @param * @param colFamName * @param throwException @@ -111,9 +111,9 @@ public CFMappingDef getCfMapDef(String colFamName, boolean throwException /** * For each class that should be managed, this method must be called to parse * its annotations and derive its meta-data. - * + * * @param - * + * * @param clazz * @return CFMapping describing the initialized class. */ @@ -349,7 +349,7 @@ private void generateColumnSliceIfNeeded(CFMappingDef cfMapDef) { /** * Find method annotated with the given annotation. - * + * * @param clazz * @param anno * @return returns Method if found, null otherwise diff --git a/object-mapper/src/main/java/me/prettyprint/hom/EntityManagerConfigurator.java b/object-mapper/src/main/java/me/prettyprint/hom/EntityManagerConfigurator.java index c63797207..772f1cacb 100644 --- a/object-mapper/src/main/java/me/prettyprint/hom/EntityManagerConfigurator.java +++ b/object-mapper/src/main/java/me/prettyprint/hom/EntityManagerConfigurator.java @@ -9,7 +9,7 @@ /** * Config wrapper around the properties map required in the JPA * specification - * + * * @author zznate * */ @@ -20,30 +20,30 @@ public class EntityManagerConfigurator { public static final String CLUSTER_NAME_PROP = PROP_PREFIX + "clusterName"; public static final String KEYSPACE_PROP = PROP_PREFIX + "keyspace"; public static final String HOST_LIST_PROP = PROP_PREFIX + "hostList"; - + private final String classpathPrefix; private final String clusterName; private final String keyspace; private CassandraHostConfigurator cassandraHostConfigurator; - - + + /** * Construct an EntityManagerConfigurator to extract the propeties related * to entity management * @param properties */ public EntityManagerConfigurator(Map properties) { - this(properties, null); + this(properties, null); } - + /** - * Same as single argument version, but allows for (nullable) + * Same as single argument version, but allows for (nullable) * {@link CassandraHostConfigurator} to be provided explicitly - * + * * @param properties * @param cassandraHostConfigurator */ - public EntityManagerConfigurator(Map properties, + public EntityManagerConfigurator(Map properties, CassandraHostConfigurator cassandraHostConfigurator) { classpathPrefix = getPropertyGently(properties, CLASSPATH_PREFIX_PROP,true); clusterName = getPropertyGently(properties, CLUSTER_NAME_PROP,true); @@ -58,8 +58,8 @@ public EntityManagerConfigurator(Map properties, } this.cassandraHostConfigurator = cassandraHostConfigurator; } - - + + public static String getPropertyGently(Map props, String key, boolean throwError) { if ( props.get(key) != null ) { return props.get(key).toString(); @@ -94,7 +94,7 @@ public String toString() { .append(KEYSPACE_PROP).append(":") .append(keyspace).toString(); } - - - + + + } diff --git a/object-mapper/src/main/java/me/prettyprint/hom/HectorObjectMapper.java b/object-mapper/src/main/java/me/prettyprint/hom/HectorObjectMapper.java index c413607d9..f06b35bae 100644 --- a/object-mapper/src/main/java/me/prettyprint/hom/HectorObjectMapper.java +++ b/object-mapper/src/main/java/me/prettyprint/hom/HectorObjectMapper.java @@ -48,10 +48,10 @@ *

    * As mentioned above all column names must be Strings - doesn't * really make sense to have other types when mapping to object properties. - * + * * @param * Type of object mapping to cassandra row - * + * * @author Todd Burruss */ public class HectorObjectMapper { @@ -70,9 +70,9 @@ public HectorObjectMapper(ClassCacheMgr cacheMgr) { /** * Retrieve columns from cassandra keyspace and column family, instantiate a * new object of required type, and then map them to the object's properties. - * - * @param - * + * + * @param + * * @param keyspace * @param colFamName * @param pkObj @@ -183,7 +183,7 @@ private byte[] callMethodAndConvertToCassandraType(Object obj, Method meth, Conv * {@link HectorExtraProperties}. If so call * {@link HectorExtraProperties#addExtraProperty(String, String)}, on the * object. - * + * * @param id * ID (row key) of the object we are retrieving from Cassandra * @param clazz @@ -191,7 +191,7 @@ private byte[] callMethodAndConvertToCassandraType(Object obj, Method meth, Conv * @param slice * column slice from Hector of type * ColumnSlice - * + * * @return instantiated object if success, null if slice is empty, * RuntimeException otherwise */ @@ -239,7 +239,7 @@ else if (null != cfMapDef.getDiscColumn() && colName.equals(cfMapDef.getDiscColu /** * Create Set of HColumns for the given Object. The Object must be annotated * with {@link Column} on the desired fields. - * + * * @param obj * @return */ @@ -254,7 +254,7 @@ private Collection> createColumnSet(Object obj) { /** * Creates a Map of property names as key and HColumns as value. See # - * + * * @param obj * @return */ diff --git a/object-mapper/src/main/java/me/prettyprint/hom/KeyConcatenationDelimiterStrategyImpl.java b/object-mapper/src/main/java/me/prettyprint/hom/KeyConcatenationDelimiterStrategyImpl.java index 50c163403..e8c228185 100644 --- a/object-mapper/src/main/java/me/prettyprint/hom/KeyConcatenationDelimiterStrategyImpl.java +++ b/object-mapper/src/main/java/me/prettyprint/hom/KeyConcatenationDelimiterStrategyImpl.java @@ -7,12 +7,12 @@ /** * Keys in Cassandra cannot inherently be multi-field, so a strategy must be * employed to concatenate fields together. - * + * *

    * This strategy uses a delimiter to segment the fields. By default the * delimiter is 2 pipes, ||, but can be overriden by calling * {@link #setDelimiter(byte[])}. - * + * * @author B. Todd Burruss */ public class KeyConcatenationDelimiterStrategyImpl implements KeyConcatenationStrategy { @@ -54,7 +54,7 @@ public List split(byte[] colFamKey) { break; } } - + if (delimiterFound) { int segSize = bb.position() - segStart-delimiter.length; segmentList.add(copyFromMark(bb, segStart, segSize)); @@ -63,8 +63,8 @@ public List split(byte[] colFamKey) { bb.mark(); } } - - + + segmentList.add(copyFromMark(bb, segStart, bb.capacity()-segStart)); return segmentList; diff --git a/object-mapper/src/main/java/me/prettyprint/hom/KeyDefinition.java b/object-mapper/src/main/java/me/prettyprint/hom/KeyDefinition.java index b988bd903..3052e9c92 100644 --- a/object-mapper/src/main/java/me/prettyprint/hom/KeyDefinition.java +++ b/object-mapper/src/main/java/me/prettyprint/hom/KeyDefinition.java @@ -11,7 +11,7 @@ /** * Defines properties used for representing and constructing a cassandra key and * mapping to/from POJO property(ies). - * + * * @author B. Todd Burruss */ public class KeyDefinition { diff --git a/object-mapper/src/main/java/me/prettyprint/hom/annotations/AnnotationScanner.java b/object-mapper/src/main/java/me/prettyprint/hom/annotations/AnnotationScanner.java index 9e86d8135..c910a20a0 100644 --- a/object-mapper/src/main/java/me/prettyprint/hom/annotations/AnnotationScanner.java +++ b/object-mapper/src/main/java/me/prettyprint/hom/annotations/AnnotationScanner.java @@ -13,7 +13,7 @@ /** * Scan for classes annotated with an annotation. The scan starts in the given * package root so it doesn't need to scan through the entire package structure. - * + * * @author Todd Burruss */ public class AnnotationScanner { diff --git a/object-mapper/src/main/java/me/prettyprint/hom/annotations/AnonymousPropertyAddHandler.java b/object-mapper/src/main/java/me/prettyprint/hom/annotations/AnonymousPropertyAddHandler.java index 734c73742..992f36fa1 100644 --- a/object-mapper/src/main/java/me/prettyprint/hom/annotations/AnonymousPropertyAddHandler.java +++ b/object-mapper/src/main/java/me/prettyprint/hom/annotations/AnonymousPropertyAddHandler.java @@ -12,7 +12,7 @@ * Annotation for marking the method used to add "anonymous" properties to the * POJO. Anonymous properties are Columns in Cassandra that do not map directly * to the POJO. See {@link HectorObjectMapper} for details. - * + * * @author Todd Burruss */ @Retention(RetentionPolicy.RUNTIME) diff --git a/object-mapper/src/main/java/me/prettyprint/hom/annotations/AnonymousPropertyCollectionGetter.java b/object-mapper/src/main/java/me/prettyprint/hom/annotations/AnonymousPropertyCollectionGetter.java index 3d3131b52..59c5a72f0 100644 --- a/object-mapper/src/main/java/me/prettyprint/hom/annotations/AnonymousPropertyCollectionGetter.java +++ b/object-mapper/src/main/java/me/prettyprint/hom/annotations/AnonymousPropertyCollectionGetter.java @@ -13,7 +13,7 @@ * Annotation for marking the method used to get "anonymous" properties from the * POJO. Anonymous properties are Columns in Cassandra that do not map directly * to the POJO. See {@link HectorObjectMapper} for details. - * + * * @author Todd Burruss */ @Retention(RetentionPolicy.RUNTIME) diff --git a/object-mapper/src/main/java/me/prettyprint/hom/annotations/Column.java b/object-mapper/src/main/java/me/prettyprint/hom/annotations/Column.java index 8e2c9e134..18ae33eeb 100644 --- a/object-mapper/src/main/java/me/prettyprint/hom/annotations/Column.java +++ b/object-mapper/src/main/java/me/prettyprint/hom/annotations/Column.java @@ -11,7 +11,7 @@ /** * Annotation for specifying which POJO properties should be mapped to Cassandra * columns. Must specify "name" as the column name in Cassandra. - * + * * @author */ @Retention(RetentionPolicy.RUNTIME) @@ -20,7 +20,7 @@ /** * The Cassandra column name. - * + * * @return name of column */ String name(); @@ -28,7 +28,7 @@ /** * The optional converter to use when converting POJO property value to/from * byte[]. If not specified, {@link me.prettyprint.hom.converters.DefaultConverter} is used. - * + * * @return Class of converter */ Class converter() default me.prettyprint.hom.converters.DefaultConverter.class; diff --git a/object-mapper/src/main/java/me/prettyprint/hom/annotations/Id.java b/object-mapper/src/main/java/me/prettyprint/hom/annotations/Id.java index db1181bb2..a3078a3fe 100644 --- a/object-mapper/src/main/java/me/prettyprint/hom/annotations/Id.java +++ b/object-mapper/src/main/java/me/prettyprint/hom/annotations/Id.java @@ -10,7 +10,7 @@ * Annotation marking the ID property in the POJO. Marking the ID property is * required so the object mapper can get/set the ID (Cassandra row key) in the * POJO. - * + * * @author Todd Burruss */ @Retention(RetentionPolicy.RUNTIME) @@ -19,7 +19,7 @@ /** * The optional converter to use when converting POJO property value to/from * byte[]. If not specified, {@link me.prettyprint.hom.converters.DefaultConverter} is used. - * + * * @return Class of converter */ Class converter() default me.prettyprint.hom.converters.DefaultConverter.class; diff --git a/object-mapper/src/main/java/me/prettyprint/hom/cache/ColumnParser.java b/object-mapper/src/main/java/me/prettyprint/hom/cache/ColumnParser.java index e42486a1e..ae9487e8f 100644 --- a/object-mapper/src/main/java/me/prettyprint/hom/cache/ColumnParser.java +++ b/object-mapper/src/main/java/me/prettyprint/hom/cache/ColumnParser.java @@ -12,7 +12,7 @@ /** * Parse, validate, and set defaults if needed for Inheritance functionality. - * + * * @author bburruss */ public class ColumnParser implements ColumnParserValidator { diff --git a/object-mapper/src/main/java/me/prettyprint/hom/cache/ColumnParserValidator.java b/object-mapper/src/main/java/me/prettyprint/hom/cache/ColumnParserValidator.java index 14521bf69..9a19e095f 100644 --- a/object-mapper/src/main/java/me/prettyprint/hom/cache/ColumnParserValidator.java +++ b/object-mapper/src/main/java/me/prettyprint/hom/cache/ColumnParserValidator.java @@ -12,5 +12,5 @@ public interface ColumnParserValidator { void parse(Field field, Annotation anno, PropertyDescriptor pd, CFMappingDef cfMapDef); // void validateAndSetDefaults(ClassCacheMgr cacheMgr, CFMappingDef cfMapDef); - + } diff --git a/object-mapper/src/main/java/me/prettyprint/hom/cache/IdClassParserValidator.java b/object-mapper/src/main/java/me/prettyprint/hom/cache/IdClassParserValidator.java index 65d1bc7a5..0246913fa 100644 --- a/object-mapper/src/main/java/me/prettyprint/hom/cache/IdClassParserValidator.java +++ b/object-mapper/src/main/java/me/prettyprint/hom/cache/IdClassParserValidator.java @@ -28,11 +28,11 @@ public void parse(ClassCacheMgr cacheMgr, Annotation anno, CFMappingDef c @Override public void validateAndSetDefaults(ClassCacheMgr cacheMgr, CFMappingDef cfMapDef) { KeyDefinition keyDef = cfMapDef.getKeyDef(); - + if ( null == keyDef.getPkClazz() ) { return; } - + Map pdMap; try { pdMap = cacheMgr.getFieldPropertyDescriptorMap(keyDef.getPkClazz()); @@ -40,12 +40,12 @@ public void validateAndSetDefaults(ClassCacheMgr cacheMgr, CFMappingDef c throw new HectorObjectMapperException("exception while introspecting class, " + keyDef.getPkClazz().getName(), e); } - + if ( keyDef.getIdPropertyMap().size() != pdMap.size() ) { throw new HectorObjectMapperException("Each field in the primary key class, " + keyDef.getPkClazz().getName() + ", must have a corresponding property in the entity, " + cfMapDef.getRealClass().getName() + ", annotated with @" + Id.class.getSimpleName() ); } - + for ( String idFieldName : pdMap.keySet() ) { if ( !keyDef.getIdPropertyMap().keySet().contains(idFieldName)) { throw new HectorObjectMapperException("Each field in the primary key class, " + keyDef.getPkClazz().getName() diff --git a/object-mapper/src/main/java/me/prettyprint/hom/cache/InheritanceParserValidator.java b/object-mapper/src/main/java/me/prettyprint/hom/cache/InheritanceParserValidator.java index 89552872b..fd48b2e62 100644 --- a/object-mapper/src/main/java/me/prettyprint/hom/cache/InheritanceParserValidator.java +++ b/object-mapper/src/main/java/me/prettyprint/hom/cache/InheritanceParserValidator.java @@ -14,7 +14,7 @@ /** * Parse, validate, and set defaults if needed for Inheritance functionality. - * + * * @author bburruss */ public class InheritanceParserValidator implements ParserValidator { diff --git a/object-mapper/src/main/java/me/prettyprint/hom/cache/ParserValidator.java b/object-mapper/src/main/java/me/prettyprint/hom/cache/ParserValidator.java index fb0aacebb..5897cb557 100644 --- a/object-mapper/src/main/java/me/prettyprint/hom/cache/ParserValidator.java +++ b/object-mapper/src/main/java/me/prettyprint/hom/cache/ParserValidator.java @@ -10,5 +10,5 @@ public interface ParserValidator { void parse(ClassCacheMgr cacheMgr, Annotation anno, CFMappingDef cfMapDef); void validateAndSetDefaults(ClassCacheMgr cacheMgr, CFMappingDef cfMapDef); - + } diff --git a/object-mapper/src/main/java/me/prettyprint/hom/cache/TableParserValidator.java b/object-mapper/src/main/java/me/prettyprint/hom/cache/TableParserValidator.java index 212ac67d4..ad0a5bf1a 100644 --- a/object-mapper/src/main/java/me/prettyprint/hom/cache/TableParserValidator.java +++ b/object-mapper/src/main/java/me/prettyprint/hom/cache/TableParserValidator.java @@ -10,7 +10,7 @@ /** * Parse, validate, and set defaults if needed for Inheritance functionality. - * + * * @author bburruss */ public class TableParserValidator implements ParserValidator { diff --git a/object-mapper/src/main/java/me/prettyprint/hom/converters/Converter.java b/object-mapper/src/main/java/me/prettyprint/hom/converters/Converter.java index 5efd4d46e..f837978b7 100644 --- a/object-mapper/src/main/java/me/prettyprint/hom/converters/Converter.java +++ b/object-mapper/src/main/java/me/prettyprint/hom/converters/Converter.java @@ -4,7 +4,7 @@ /** * Interface defining a custom object mapper conversion. For instance, from an * enum to a string. - * + * * @author Todd Burruss */ public interface Converter { diff --git a/object-mapper/src/main/java/me/prettyprint/hom/converters/DefaultConverter.java b/object-mapper/src/main/java/me/prettyprint/hom/converters/DefaultConverter.java index 050ef82bd..ba09b835f 100644 --- a/object-mapper/src/main/java/me/prettyprint/hom/converters/DefaultConverter.java +++ b/object-mapper/src/main/java/me/prettyprint/hom/converters/DefaultConverter.java @@ -8,7 +8,7 @@ /** * Default converter used when none specified in {@link Column} annotation. Uses * Java reflection to determine the Java type to use. - * + * * @author Todd Burruss */ public class DefaultConverter implements Converter { diff --git a/object-mapper/src/test/java/com/mycompany/MySerial.java b/object-mapper/src/test/java/com/mycompany/MySerial.java index f45ecdb18..42fa59114 100644 --- a/object-mapper/src/test/java/com/mycompany/MySerial.java +++ b/object-mapper/src/test/java/com/mycompany/MySerial.java @@ -6,13 +6,13 @@ public class MySerial implements Serializable { private int prop1; private long prop2; - + public MySerial(int prop1, long prop2) { super(); this.prop1 = prop1; this.prop2 = prop2; } - + public int getProp1() { return prop1; } diff --git a/object-mapper/src/test/java/me/prettyprint/hom/CassandraTestBase.java b/object-mapper/src/test/java/me/prettyprint/hom/CassandraTestBase.java index c0139a104..d89c7fb33 100644 --- a/object-mapper/src/test/java/me/prettyprint/hom/CassandraTestBase.java +++ b/object-mapper/src/test/java/me/prettyprint/hom/CassandraTestBase.java @@ -52,7 +52,7 @@ public static void startCassandraInstance(String pathToDataDir) throws TTranspor } catch (ConfigurationException ce) { throw new RuntimeException(ce); } - + cassandraStarted = true; } diff --git a/object-mapper/src/test/java/me/prettyprint/hom/ClassCacheMgrTest.java b/object-mapper/src/test/java/me/prettyprint/hom/ClassCacheMgrTest.java index dc7a2f68c..e8801d592 100644 --- a/object-mapper/src/test/java/me/prettyprint/hom/ClassCacheMgrTest.java +++ b/object-mapper/src/test/java/me/prettyprint/hom/ClassCacheMgrTest.java @@ -211,12 +211,12 @@ public void testParsingInheritedEntityWithoutAnonymousAddHandler() { public void testParsingComplexEntity() { ClassCacheMgr cacheMgr = new ClassCacheMgr(); CFMappingDef cfMapDef = cacheMgr.initializeCacheForClass(MyComplexEntity.class); - + KeyDefinition keyDef = cfMapDef.getKeyDef(); assertEquals( MyCompositePK.class, keyDef.getPkClazz() ); assertEquals( 2, keyDef.getIdPropertyMap().size() ); assertEquals( keyDef.getIdPropertyMap().size(), keyDef.getPropertyDescriptorMap().size() ); - + } @Ignore( "Cannot enable until method annotations are supported by ClassCacheMgr") diff --git a/object-mapper/src/test/java/me/prettyprint/hom/EntityManagerTest.java b/object-mapper/src/test/java/me/prettyprint/hom/EntityManagerTest.java index 4bc6063b2..64b762103 100644 --- a/object-mapper/src/test/java/me/prettyprint/hom/EntityManagerTest.java +++ b/object-mapper/src/test/java/me/prettyprint/hom/EntityManagerTest.java @@ -86,9 +86,9 @@ public void testPersistAndFindComplexType() { entity1.setStrProp2("str-prop-two"); em.persist(entity1); - + MyComplexEntity entity2 = em.find(MyComplexEntity.class, pkKey); - + assertEquals( entity1.getIntProp1(), entity2.getIntProp1() ); assertEquals( entity1.getStrProp1(), entity2.getStrProp1() ); assertEquals( entity1.getStrProp2(), entity2.getStrProp2() ); @@ -106,9 +106,9 @@ public void testMissingColumnsForPojoProps() { entity1.setStrProp2("str-prop-two"); em.persist(entity1); - + MyComplexEntity entity2 = em.find(MyComplexEntity.class, pkKey); - + assertEquals( entity1.getIntProp1(), entity2.getIntProp1() ); assertEquals( entity1.getStrProp1(), entity2.getStrProp1() ); assertEquals( entity1.getStrProp2(), entity2.getStrProp2() ); diff --git a/object-mapper/src/test/java/me/prettyprint/hom/HectorObjectMapperTest.java b/object-mapper/src/test/java/me/prettyprint/hom/HectorObjectMapperTest.java index 609db3ecf..7ec58f098 100644 --- a/object-mapper/src/test/java/me/prettyprint/hom/HectorObjectMapperTest.java +++ b/object-mapper/src/test/java/me/prettyprint/hom/HectorObjectMapperTest.java @@ -144,17 +144,17 @@ public void testCreateInstanceCustomIdType() { assertEquals(id, obj.getId()); assertEquals(longProp1, obj.getLongProp1()); } - + @Test public void testIsSerializable() { assertTrue(HectorObjectMapper.isSerializable(UUID.class)); } - + @Test public void testIsNotSerializable() { assertFalse(HectorObjectMapper.isSerializable(HectorObjectMapper.class)); } - + // -------------------- @Before diff --git a/object-mapper/src/test/java/me/prettyprint/hom/badbeans/MyComplexEntityMissingIdField.java b/object-mapper/src/test/java/me/prettyprint/hom/badbeans/MyComplexEntityMissingIdField.java index 9de281c73..65a066152 100644 --- a/object-mapper/src/test/java/me/prettyprint/hom/badbeans/MyComplexEntityMissingIdField.java +++ b/object-mapper/src/test/java/me/prettyprint/hom/badbeans/MyComplexEntityMissingIdField.java @@ -10,10 +10,10 @@ @IdClass( MyCompositePK.class ) @Table(name="ComplexColumnFamily") public class MyComplexEntityMissingIdField { - + @Id private String strProp1; - + @Column( name ="strProp2") private String strProp2; diff --git a/object-mapper/src/test/java/me/prettyprint/hom/badbeans/MyCompositePK.java b/object-mapper/src/test/java/me/prettyprint/hom/badbeans/MyCompositePK.java index d1b580c17..a178a342d 100644 --- a/object-mapper/src/test/java/me/prettyprint/hom/badbeans/MyCompositePK.java +++ b/object-mapper/src/test/java/me/prettyprint/hom/badbeans/MyCompositePK.java @@ -5,7 +5,7 @@ /** * Used with @IdClass. Properties of this class must match @Id properties * defined by the entity. - * + * * @author B. Todd Burruss */ public class MyCompositePK implements Serializable { @@ -15,7 +15,7 @@ public class MyCompositePK implements Serializable { public MyCompositePK() { } - + public MyCompositePK(int intProp1, String strProp1) { this.intProp1 = intProp1; this.strProp1 = strProp1; diff --git a/object-mapper/src/test/java/me/prettyprint/hom/beans/ColorEmbedded.java b/object-mapper/src/test/java/me/prettyprint/hom/beans/ColorEmbedded.java index 4d182fef1..4ba895a98 100644 --- a/object-mapper/src/test/java/me/prettyprint/hom/beans/ColorEmbedded.java +++ b/object-mapper/src/test/java/me/prettyprint/hom/beans/ColorEmbedded.java @@ -7,7 +7,7 @@ public class ColorEmbedded implements Serializable { private Colors color; - + public Colors getColor() { return color; } @@ -15,5 +15,5 @@ public Colors getColor() { public void setColor(Colors color) { this.color = color; } - + } diff --git a/object-mapper/src/test/java/me/prettyprint/hom/beans/MyComplexEntity.java b/object-mapper/src/test/java/me/prettyprint/hom/beans/MyComplexEntity.java index 27f08c684..372b22f9e 100644 --- a/object-mapper/src/test/java/me/prettyprint/hom/beans/MyComplexEntity.java +++ b/object-mapper/src/test/java/me/prettyprint/hom/beans/MyComplexEntity.java @@ -13,13 +13,13 @@ public class MyComplexEntity { @Id private int intProp1; - + @Id private String strProp1; - + @Column( name ="strProp2") private String strProp2; - + @Column( name ="strProp3") private String strProp3; diff --git a/object-mapper/src/test/java/me/prettyprint/hom/beans/MyCompositePK.java b/object-mapper/src/test/java/me/prettyprint/hom/beans/MyCompositePK.java index 4b4259e4c..3af01bd23 100644 --- a/object-mapper/src/test/java/me/prettyprint/hom/beans/MyCompositePK.java +++ b/object-mapper/src/test/java/me/prettyprint/hom/beans/MyCompositePK.java @@ -5,7 +5,7 @@ /** * Used with @IdClass. Properties of this class must match @Id properties * defined by the entity. - * + * * @author B. Todd Burruss */ public class MyCompositePK implements Serializable { @@ -15,7 +15,7 @@ public class MyCompositePK implements Serializable { public MyCompositePK() { } - + public MyCompositePK(int intProp1, String strProp1) { this.intProp1 = intProp1; this.strProp1 = strProp1; diff --git a/object-mapper/src/test/java/me/prettyprint/hom/beans/SimpleRelationshipBean.java b/object-mapper/src/test/java/me/prettyprint/hom/beans/SimpleRelationshipBean.java index 3b9ab8ca0..a43f0bb5b 100644 --- a/object-mapper/src/test/java/me/prettyprint/hom/beans/SimpleRelationshipBean.java +++ b/object-mapper/src/test/java/me/prettyprint/hom/beans/SimpleRelationshipBean.java @@ -13,11 +13,11 @@ @Entity @Table(name="SimpleRelationshipBeanColumnFamily") public class SimpleRelationshipBean { - + private UUID baseId; - + private String myType; - + private Set simpleTestBeans; @Id diff --git a/object-mapper/src/test/java/me/prettyprint/hom/beans/SimpleTestBean.java b/object-mapper/src/test/java/me/prettyprint/hom/beans/SimpleTestBean.java index 35e4682ea..a14c35a25 100644 --- a/object-mapper/src/test/java/me/prettyprint/hom/beans/SimpleTestBean.java +++ b/object-mapper/src/test/java/me/prettyprint/hom/beans/SimpleTestBean.java @@ -10,32 +10,32 @@ @Entity @Table(name="SimpleTestBeanColumnFamily") public class SimpleTestBean implements Serializable { - + private long id; private String name; - - public SimpleTestBean() { + + public SimpleTestBean() { } - + public SimpleTestBean(long id, String name) { this.id = id; this.name = name; } - + @Id public long getId() { return id; } - + public void setId(long id) { this.id = id; } - + @Column(name="name") public String getName() { return name; } - + public void setName(String name) { this.name = name; } @@ -49,7 +49,7 @@ public String toString() { .append(getId()) .toString(); } - - + + } diff --git a/object-mapper/src/test/java/me/prettyprint/hom/cache/IdClassParserValidatorTest.java b/object-mapper/src/test/java/me/prettyprint/hom/cache/IdClassParserValidatorTest.java index 7e34c276f..c4075f0de 100644 --- a/object-mapper/src/test/java/me/prettyprint/hom/cache/IdClassParserValidatorTest.java +++ b/object-mapper/src/test/java/me/prettyprint/hom/cache/IdClassParserValidatorTest.java @@ -19,19 +19,19 @@ public void testNotSerializable() { IdClassParserValidator parVal = new IdClassParserValidator(); CFMappingDef cfMapDef = new CFMappingDef(MyTestBean.class); - + IdClass anno = new IdClass() { @Override public Class annotationType() { return IdClass.class; } - + @Override public Class value() { return MyCompositePK.class; } }; - + parVal.parse(cacheMgr, anno, cfMapDef); } } diff --git a/object-mapper/src/test/java/me/prettyprint/hom/dupebean/MyDupeCF1.java b/object-mapper/src/test/java/me/prettyprint/hom/dupebean/MyDupeCF1.java index 55901d21a..be73fad78 100644 --- a/object-mapper/src/test/java/me/prettyprint/hom/dupebean/MyDupeCF1.java +++ b/object-mapper/src/test/java/me/prettyprint/hom/dupebean/MyDupeCF1.java @@ -7,7 +7,7 @@ /** * Entity for testing duplication class <-> column family mapping. - * + * * @author Todd Burruss */ @Entity diff --git a/object-mapper/src/test/java/me/prettyprint/hom/dupebean/MyDupeCF2.java b/object-mapper/src/test/java/me/prettyprint/hom/dupebean/MyDupeCF2.java index 57292804f..6b3e2c068 100644 --- a/object-mapper/src/test/java/me/prettyprint/hom/dupebean/MyDupeCF2.java +++ b/object-mapper/src/test/java/me/prettyprint/hom/dupebean/MyDupeCF2.java @@ -7,7 +7,7 @@ /** * Entity for testing duplication class <-> column family mapping. - * + * * @author Todd Burruss */ @Entity