Elffuss BraidHub

kikocis/java-driver public

JAVA-1034: fix metadata parser for collections of custom types.
Changeset833376cd67158e0eedd36d8d15813fb29c1531cf2fbfa68c51d2ea10899a1fea
Authorolim7t <omichallat+github@gmail.com> · 2016-01-13 17:31 +01:00
Committerolim7t <omichallat+github@gmail.com> · 2016-01-15 16:00 +01:00
Parentsd854e0040cd7

Browse the files at this changeset

3 files changed

modified changelog/README.md +2 −0
@@ -2,6 +2,8 @@
22
33 ### 3.0.0 (in progress)
44
5+- [bug] JAVA-1034: fix metadata parser for collections of custom types.
6+
57 Merged from 2.1 branch:
68
79 - [improvement] JAVA-1030: Log token to replica map computation times.
modified driver-core/src/main/java/com/datastax/driver/core/DataTypeCqlNameParser.java +5 −2
@@ -200,8 +200,7 @@
200200 // left idx positioned on the character stopping the read
201201 private String readNextIdentifier() {
202202 int startIdx = idx;
203- // if first character is a quote, this is a quoted identifier.
204- if (str.charAt(startIdx) == '"') {
203+ if (str.charAt(startIdx) == '"') { // case-sensitive name included in double quotes
205204 ++idx;
206205 // read until closing quote.
207206 while (!isEOS()) {
@@ -216,6 +215,10 @@
216215 break;
217216 }
218217 }
218+ } else if (str.charAt(startIdx) == '\'') { // custom type name included in single quotes
219+ ++idx;
220+ // read until closing quote.
221+ while (!isEOS() && str.charAt(idx++) != '\'') { /* loop */ }
219222 } else {
220223 while (!isEOS() && (isIdentifierChar(str.charAt(idx)) || str.charAt(idx) == '"'))
221224 ++idx;
modified driver-core/src/test/java/com/datastax/driver/core/CustomTypeTest.java +36 −3
@@ -16,13 +16,14 @@
1616 package com.datastax.driver.core;
1717
1818 import com.datastax.driver.core.utils.CassandraVersion;
19+import com.google.common.collect.Lists;
1920 import org.testng.annotations.Test;
2021
2122 import java.nio.ByteBuffer;
2223 import java.util.ArrayList;
2324 import java.util.Collection;
24-import java.util.Collections;
2525 import java.util.List;
26+import java.util.Map;
2627
2728 import static com.datastax.driver.core.Assertions.assertThat;
2829 import static com.datastax.driver.core.DataType.cint;
@@ -45,14 +46,19 @@
4546
4647 @Override
4748 public Collection<String> createTestFixtures() {
48- return Collections.singleton(
49+ return Lists.newArrayList(
4950 "CREATE TABLE test ("
5051 + " k int,"
5152 + " c1 'DynamicCompositeType(s => UTF8Type, i => Int32Type)',"
5253 + " c2 'ReversedType(CompositeType(UTF8Type, Int32Type))'," // reversed translates to CLUSTERING ORDER BY DESC
5354 + " c3 'Int32Type'," // translates to int
5455 + " PRIMARY KEY (k, c1, c2)"
55- + ") WITH COMPACT STORAGE"
56+ + ") WITH COMPACT STORAGE",
57+ "CREATE TABLE test_collection("
58+ + " k int PRIMARY KEY,"
59+ + " c1 list<'DynamicCompositeType(s => UTF8Type, i => Int32Type)'>,"
60+ + " c2 map<'DynamicCompositeType(s => UTF8Type, i => Int32Type)', 'DynamicCompositeType(s => UTF8Type, i => Int32Type)'>"
61+ + ")"
5662 );
5763 }
5864
@@ -107,6 +113,33 @@
107113 assertThat(r.getInt("c3")).isEqualTo(1);
108114 }
109115
116+ /**
117+ * Validates that columns using collections of custom types are properly handled by the driver.
118+ *
119+ * @jira_ticket JAVA-1034
120+ * @test_category metadata
121+ */
122+ @Test(groups = "short")
123+ public void should_serialize_and_deserialize_collections_of_custom_types() {
124+ TableMetadata table = cluster.getMetadata().getKeyspace(keyspace).getTable("test_collection");
125+ assertThat(table.getColumn("c1")).hasType(DataType.list(CUSTOM_DYNAMIC_COMPOSITE));
126+ assertThat(table.getColumn("c2")).hasType(DataType.map(CUSTOM_DYNAMIC_COMPOSITE, CUSTOM_DYNAMIC_COMPOSITE));
127+
128+ session.execute("INSERT INTO test_collection(k, c1, c2) VALUES (0, [ 's@foo:i@32' ], { 's@foo:i@32': 's@bar:i@42' })");
129+
130+ Row r = session.execute("SELECT * FROM test_collection").one();
131+
132+ assertThat(r.getColumnDefinitions().getType("c1")).isEqualTo(DataType.list(CUSTOM_DYNAMIC_COMPOSITE));
133+ List<ByteBuffer> c1 = r.getList("c1", ByteBuffer.class);
134+ assertThat(c1.get(0)).isEqualTo(serializeForDynamicCompositeType("foo", 32));
135+
136+ assertThat(r.getColumnDefinitions().getType("c2")).isEqualTo(DataType.map(CUSTOM_DYNAMIC_COMPOSITE, CUSTOM_DYNAMIC_COMPOSITE));
137+ Map<ByteBuffer, ByteBuffer> c2 = r.getMap("c2", ByteBuffer.class, ByteBuffer.class);
138+ Map.Entry<ByteBuffer, ByteBuffer> entry = c2.entrySet().iterator().next();
139+ assertThat(entry.getKey()).isEqualTo(serializeForDynamicCompositeType("foo", 32));
140+ assertThat(entry.getValue()).isEqualTo(serializeForDynamicCompositeType("bar", 42));
141+ }
142+
110143 /**
111144 * Validates that UDTs with fields using custom types are properly handled by the driver in the following ways:
112145 * <p/>