Skip to content
Browse files

filter the column named KEY per issue #320

  • Loading branch information...
1 parent 0ddb8ca commit 62a8a3b0ae8f2f1aae508a80dac550834ce24fc9 zznate committed
View
23 core/src/main/java/me/prettyprint/cassandra/model/CqlQuery.java
@@ -58,6 +58,7 @@
private Serializer<V> valueSerializer;
private ByteBuffer query;
private boolean useCompression;
+ private boolean suppressKeyInColumns;
public CqlQuery(Keyspace k, Serializer<K> keySerializer,
Serializer<N> nameSerializer, Serializer<V> valueSerializer) {
@@ -81,6 +82,11 @@ public CqlQuery(Keyspace k, Serializer<K> keySerializer,
return this;
}
+ public CqlQuery<K, N, V> setSuppressKeyInColumns(boolean suppressKeyInColumns) {
+ this.suppressKeyInColumns = suppressKeyInColumns;
+ return this;
+ }
+
public CqlQuery<K, N, V> useCompression() {
useCompression = true;
return this;
@@ -113,7 +119,7 @@ public CqlQuery(Keyspace k, Serializer<K> keySerializer,
for (Iterator<CqlRow> rowsIter = result.getRowsIterator(); rowsIter.hasNext(); ) {
CqlRow row = rowsIter.next();
- ret.put(ByteBuffer.wrap(row.getKey()), row.getColumns());
+ ret.put(ByteBuffer.wrap(row.getKey()), filterKeyColumn(row));
}
Map<K, List<Column>> thriftRet = keySerializer.fromBytesMap(ret);
rows = new CqlRows<K, N, V>((LinkedHashMap<K, List<Column>>)thriftRet, columnNameSerializer, valueSerializer);
@@ -127,6 +133,21 @@ public CqlQuery(Keyspace k, Serializer<K> keySerializer,
}
}), this);
+ }
+
+ /*
+ * Trims the first column from the row if it's name is equal to "KEY"
+ */
+ private List<Column> filterKeyColumn(CqlRow row) {
+ if ( suppressKeyInColumns && row.isSetColumns() && row.columns.size() > 0) {
+ Iterator<Column> columnsIterator = row.getColumnsIterator();
+ Column column = columnsIterator.next();
+ if ( column.name.duplicate().equals(KEY_BB) ) {
+ columnsIterator.remove();
+ }
+ }
+ return row.getColumns();
}
+ private static ByteBuffer KEY_BB = StringSerializer.get().toByteBuffer("KEY");
}
View
1 core/src/main/java/me/prettyprint/cassandra/model/CqlRows.java
@@ -45,7 +45,6 @@ public CqlRows(LinkedHashMap<K, List<Column>> thriftRet,
}
}
-
/**
* Constructed as empty for {@link CqlResultType#VOID}
View
23 core/src/test/java/me/prettyprint/cassandra/model/CqlQueryTest.java
@@ -4,6 +4,7 @@
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 static org.junit.Assert.assertEquals;
import me.prettyprint.cassandra.BaseEmbededServerSetupTest;
import me.prettyprint.cassandra.serializers.LongSerializer;
@@ -18,9 +19,13 @@
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
public class CqlQueryTest extends BaseEmbededServerSetupTest {
+ private Logger log = LoggerFactory.getLogger(CqlQueryTest.class);
+
private final static String KEYSPACE = "Keyspace1";
private static final StringSerializer se = new StringSerializer();
private static final LongSerializer le = new LongSerializer();
@@ -53,8 +58,24 @@ public void testSimpleSelect() {
CqlQuery<String,String,Long> cqlQuery = new CqlQuery<String,String,Long>(keyspace, se, se, le);
cqlQuery.setQuery("select * from StandardLong1");
QueryResult<CqlRows<String,String,Long>> result = cqlQuery.execute();
- assertEquals(6,result.get().getCount());
+ CqlRows<String, String, Long> rows = result.get();
+ // check that we contain a 'key' column
+ assertNotNull(rows.getList().get(0).getColumnSlice().getColumnByName("KEY"));
+ assertEquals(6,rows.getCount());
+ }
+ @Test
+ public void testSelectAllSuppressesKeyColumn() {
+ CqlQuery<String,String,Long> cqlQuery = new CqlQuery<String,String,Long>(keyspace, se, se, le);
+ cqlQuery.setQuery("select * from StandardLong1");
+ cqlQuery.setSuppressKeyInColumns(true);
+ QueryResult<CqlRows<String,String,Long>> result = cqlQuery.execute();
+ CqlRows<String, String, Long> rows = result.get();
+ // check that we contain a 'key' column
+ assertNull(rows.getList().get(0).getColumnSlice().getColumnByName("KEY"));
+ // arbitrary row check
+ assertNull(rows.getList().get(3).getColumnSlice().getColumnByName("KEY"));
+ assertEquals(6,rows.getCount());
}
@Test

1 comment on commit 62a8a3b

@patricioe
Collaborator

LGTM

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