kikocis/java-driver public
Changesets · Branches · Tags · Compare
| Changeset | f19a17f6430ddcc08d8c9107be14e42ccec0de52e818c903af857fee35b04d02 |
|---|---|
| Author | Alexandre Dutra <alexandre.dutra@datastax.com> · 2015-11-19 10:51 +01:00 |
| Committer | Alexandre Dutra <alexandre.dutra@datastax.com> · 2015-11-25 17:45 +01:00 |
| Parents | 2a523e2ece4d |
5 files changed
modified changelog/README.md +5 −0
| @@ -1,5 +1,10 @@ | ||
| 1 | 1 | ## Changelog |
| 2 | 2 | |
| 3 | +### 3.0.0-beta1 (in progress) | |
| 4 | + | |
| 5 | +- [bug] JAVA-993: Support for "custom" types after CASSANDRA-10365. | |
| 6 | + | |
| 7 | + | |
| 3 | 8 | ### 3.0.0-alpha5 |
| 4 | 9 | |
| 5 | 10 | - [improvement] JAVA-958: Make TableOrView.Order visible. |
modified driver-core/src/main/java/com/datastax/driver/core/DataTypeCqlNameParser.java +3 −0
| @@ -79,6 +79,9 @@ | ||
| 79 | 79 | */ |
| 80 | 80 | static DataType parse(String toParse, Cluster cluster, String currentKeyspaceName, Map<String, UserType> currentUserTypes, Map<String, UserType> oldUserTypes, boolean frozen, boolean shallowUserTypes) { |
| 81 | 81 | |
| 82 | + if(toParse.startsWith("'")) | |
| 83 | + return custom(toParse.substring(1, toParse.length() - 1)); | |
| 84 | + | |
| 82 | 85 | Parser parser = new Parser(toParse, 0); |
| 83 | 86 | String type = parser.parseTypeName(); |
| 84 | 87 |
modified driver-core/src/main/java/com/datastax/driver/core/Metadata.java +1 −1
| @@ -116,7 +116,7 @@ | ||
| 116 | 116 | return id.toLowerCase(); |
| 117 | 117 | |
| 118 | 118 | // Check if it's enclosed in quotes. If it is, remove them and unescape internal double quotes |
| 119 | - if (id.charAt(0) == '"' && id.charAt(id.length() - 1) == '"') | |
| 119 | + if (!id.isEmpty() && id.charAt(0) == '"' && id.charAt(id.length() - 1) == '"') | |
| 120 | 120 | return id.substring(1, id.length() - 1).replaceAll("\"\"", "\""); |
| 121 | 121 | |
| 122 | 122 | // Otherwise, just return the id. |
modified driver-core/src/test/java/com/datastax/driver/core/ColumnMetadataAssert.java +4 −4
| @@ -36,22 +36,22 @@ | ||
| 36 | 36 | } |
| 37 | 37 | |
| 38 | 38 | public ColumnMetadataAssert isPrimaryKey() { |
| 39 | - assertThat(actual.getParent().getPrimaryKey().contains(actual)); | |
| 39 | + assertThat(actual.getParent().getPrimaryKey().contains(actual)).as("Expecting %s to be part of the primary key, but it was not", actual).isTrue(); | |
| 40 | 40 | return this; |
| 41 | 41 | } |
| 42 | 42 | |
| 43 | 43 | public ColumnMetadataAssert isPartitionKey() { |
| 44 | - assertThat(actual.getParent().getPartitionKey().contains(actual)); | |
| 44 | + assertThat(actual.getParent().getPartitionKey().contains(actual)).as("Expecting %s to be part of the partition key, but it was not", actual).isTrue(); | |
| 45 | 45 | return this; |
| 46 | 46 | } |
| 47 | 47 | |
| 48 | 48 | public ColumnMetadataAssert isClusteringColumn() { |
| 49 | - assertThat(actual.getParent().getClusteringColumns().contains(actual)); | |
| 49 | + assertThat(actual.getParent().getClusteringColumns().contains(actual)).as("Expecting %s to be a clustering column, but it was not", actual).isTrue(); | |
| 50 | 50 | return this; |
| 51 | 51 | } |
| 52 | 52 | |
| 53 | 53 | public ColumnMetadataAssert isRegularColumn() { |
| 54 | - assertThat(!actual.getParent().getPrimaryKey().contains(actual)); | |
| 54 | + assertThat(actual.getParent().getPrimaryKey().contains(actual)).as("Expecting %s to be a regular column, but it was not", actual).isFalse(); | |
| 55 | 55 | return this; |
| 56 | 56 | } |
| 57 | 57 |
modified driver-core/src/test/java/com/datastax/driver/core/CustomTypeTest.java +155 −30
| @@ -22,26 +22,142 @@ | ||
| 22 | 22 | import java.util.List; |
| 23 | 23 | |
| 24 | 24 | import org.testng.annotations.Test; |
| 25 | -import static org.testng.Assert.assertEquals; | |
| 25 | + | |
| 26 | +import com.datastax.driver.core.utils.CassandraVersion; | |
| 27 | + | |
| 28 | +import static com.datastax.driver.core.Assertions.assertThat; | |
| 29 | +import static com.datastax.driver.core.DataType.cint; | |
| 30 | +import static com.datastax.driver.core.DataType.custom; | |
| 26 | 31 | |
| 27 | 32 | /** |
| 28 | 33 | * Test we "support" custom types. |
| 29 | 34 | */ |
| 30 | 35 | public class CustomTypeTest extends CCMBridge.PerClassSingleNodeCluster { |
| 31 | 36 | |
| 37 | + public static final DataType CUSTOM_DYNAMIC_COMPOSITE = custom( | |
| 38 | + "org.apache.cassandra.db.marshal.DynamicCompositeType(" | |
| 39 | + + "s=>org.apache.cassandra.db.marshal.UTF8Type," | |
| 40 | + + "i=>org.apache.cassandra.db.marshal.Int32Type)"); | |
| 41 | + | |
| 42 | + public static final DataType CUSTOM_COMPOSITE = custom( | |
| 43 | + "org.apache.cassandra.db.marshal.CompositeType(" | |
| 44 | + + "org.apache.cassandra.db.marshal.UTF8Type," | |
| 45 | + + "org.apache.cassandra.db.marshal.Int32Type)"); | |
| 46 | + | |
| 32 | 47 | @Override |
| 33 | 48 | protected Collection<String> getTableDefinitions() { |
| 34 | 49 | return Collections.singleton( |
| 35 | - "CREATE TABLE test (" | |
| 36 | - + " k int," | |
| 37 | - + " c 'DynamicCompositeType(s => UTF8Type, i => Int32Type)'," | |
| 38 | - + " v int," | |
| 39 | - + " PRIMARY KEY (k, c)" | |
| 40 | - + ") WITH COMPACT STORAGE" | |
| 50 | + "CREATE TABLE test (" | |
| 51 | + + " k int," | |
| 52 | + + " c1 'DynamicCompositeType(s => UTF8Type, i => Int32Type)'," | |
| 53 | + + " c2 'ReversedType(CompositeType(UTF8Type, Int32Type))'," // reversed translates to CLUSTERING ORDER BY DESC | |
| 54 | + + " c3 'Int32Type'," // translates to int | |
| 55 | + + " PRIMARY KEY (k, c1, c2)" | |
| 56 | + + ") WITH COMPACT STORAGE" | |
| 41 | 57 | ); |
| 42 | 58 | } |
| 43 | 59 | |
| 44 | - private ByteBuffer serializeForDynamicType(Object... params) { | |
| 60 | + /** | |
| 61 | + * Validates that columns using custom types are properly handled by the driver in the following ways: | |
| 62 | + * <p/> | |
| 63 | + * <ol> | |
| 64 | + * <li>The column metadata appropriately represents the types as {@link DataType#custom(String)}</li> | |
| 65 | + * <li>ReversedType is appropriately detected and the clustering order of that column is marked as descending.</li> | |
| 66 | + * <li>ColumnDefinitions for a column in a {@link Row} matches the custom type and that inserted data is read back properly.</li> | |
| 67 | + * </ol> | |
| 68 | + * | |
| 69 | + * @jira_ticket JAVA-993 | |
| 70 | + * @test_category metadata | |
| 71 | + */ | |
| 72 | + @Test(groups = "short") | |
| 73 | + public void should_serialize_and_deserialize_custom_types() { | |
| 74 | + | |
| 75 | + TableMetadata table = cluster.getMetadata().getKeyspace(keyspace).getTable("test"); | |
| 76 | + | |
| 77 | + assertThat(table.getColumn("c1")).isClusteringColumn().hasType(CUSTOM_DYNAMIC_COMPOSITE); | |
| 78 | + assertThat(table.getColumn("c2")).isClusteringColumn().hasType(CUSTOM_COMPOSITE).hasClusteringOrder(ClusteringOrder.DESC); | |
| 79 | + assertThat(table.getColumn("c3")).hasType(cint()); | |
| 80 | + | |
| 81 | + session.execute("INSERT INTO test(k, c1, c2, c3) VALUES (0, 's@foo:i@32', 'foo:32', 1)"); | |
| 82 | + session.execute("INSERT INTO test(k, c1, c2, c3) VALUES (0, 'i@42', ':42', 2)"); | |
| 83 | + session.execute("INSERT INTO test(k, c1, c2, c3) VALUES (0, 'i@12:i@3', 'foo', 3)"); | |
| 84 | + | |
| 85 | + ResultSet rs = session.execute("SELECT * FROM test"); | |
| 86 | + | |
| 87 | + Row r = rs.one(); | |
| 88 | + | |
| 89 | + assertThat(r.getColumnDefinitions().getType("c1")).isEqualTo(CUSTOM_DYNAMIC_COMPOSITE); | |
| 90 | + assertThat(r.getColumnDefinitions().getType("c2")).isEqualTo(CUSTOM_COMPOSITE); | |
| 91 | + assertThat(r.getColumnDefinitions().getType("c3")).isEqualTo(cint()); | |
| 92 | + | |
| 93 | + assertThat(r.getInt("k")).isEqualTo(0); | |
| 94 | + assertThat(r.getBytesUnsafe("c1")).isEqualTo(serializeForDynamicCompositeType(12, 3)); | |
| 95 | + assertThat(r.getBytesUnsafe("c2")).isEqualTo(serializeForCompositeType("foo")); | |
| 96 | + assertThat(r.getInt("c3")).isEqualTo(3); | |
| 97 | + | |
| 98 | + r = rs.one(); | |
| 99 | + assertThat(r.getInt("k")).isEqualTo(0); | |
| 100 | + assertThat(r.getBytesUnsafe("c1")).isEqualTo(serializeForDynamicCompositeType(42)); | |
| 101 | + assertThat(r.getBytesUnsafe("c2")).isEqualTo(serializeForCompositeType("", 42)); | |
| 102 | + assertThat(r.getInt("c3")).isEqualTo(2); | |
| 103 | + | |
| 104 | + r = rs.one(); | |
| 105 | + assertThat(r.getInt("k")).isEqualTo(0); | |
| 106 | + assertThat(r.getBytesUnsafe("c1")).isEqualTo(serializeForDynamicCompositeType("foo", 32)); | |
| 107 | + assertThat(r.getBytesUnsafe("c2")).isEqualTo(serializeForCompositeType("foo", 32)); | |
| 108 | + assertThat(r.getInt("c3")).isEqualTo(1); | |
| 109 | + } | |
| 110 | + | |
| 111 | + /** | |
| 112 | + * Validates that UDTs with fields using custom types are properly handled by the driver in the following ways: | |
| 113 | + * <p/> | |
| 114 | + * <ol> | |
| 115 | + * <li>The {@link UserType} metadata appropriately represents the types of fields with custom types as {@link DataType#custom(String)}</li> | |
| 116 | + * <li>{@link TableMetadata} with a column having a {@link UserType} is properly referenced.</li> | |
| 117 | + * <li>ColumnDefinitions for a column in a {@link Row} matches the {@link UserType} and that inserted data is read back properly.</li> | |
| 118 | + * </ol> | |
| 119 | + * | |
| 120 | + * @jira_ticket JAVA-993 | |
| 121 | + * @test_category metadata | |
| 122 | + */ | |
| 123 | + @Test(groups = "short") | |
| 124 | + @CassandraVersion(major=2.1) | |
| 125 | + public void should_handle_udt_with_custom_type() { | |
| 126 | + // Given: a UDT with custom types, and a table using it. | |
| 127 | + session.execute("CREATE TYPE custom_udt (regular int, c1 'DynamicCompositeType(s => UTF8Type, i => Int32Type)', c2 'LongType')"); | |
| 128 | + session.execute("CREATE TABLE custom_udt_tbl (k int primary key, v frozen<custom_udt>)"); | |
| 129 | + | |
| 130 | + // When: Retrieving User Type via schema metadata. | |
| 131 | + UserType custom_udt = cluster.getMetadata().getKeyspace(keyspace).getUserType("custom_udt"); | |
| 132 | + assertThat(custom_udt.getFieldType("regular")).isEqualTo(cint()); | |
| 133 | + // Then: The fields with custom types should be appropriately represented with their defined types. | |
| 134 | + assertThat(custom_udt.getFieldType("c1")).isEqualTo(CUSTOM_DYNAMIC_COMPOSITE); | |
| 135 | + assertThat(custom_udt.getFieldType("c2")).isEqualTo(DataType.bigint()); | |
| 136 | + | |
| 137 | + // When: Retrieving Table via schema metadata. | |
| 138 | + TableMetadata table = cluster.getMetadata().getKeyspace(keyspace).getTable("custom_udt_tbl"); | |
| 139 | + // Then: The column with the user type should be represented and match the type previously retrieved. | |
| 140 | + assertThat(table.getColumn("v")).hasType(custom_udt); | |
| 141 | + | |
| 142 | + // Given: Existing data in table with a UDT with custom types. | |
| 143 | + session.execute("INSERT INTO custom_udt_tbl (k, v) VALUES (0, {regular: 5, c1: 's@hello:i@93', c2: 400})"); | |
| 144 | + | |
| 145 | + // When: Data is retrieved. | |
| 146 | + Row row = session.execute("select * from custom_udt_tbl").one(); | |
| 147 | + | |
| 148 | + // Then: The resulting row's column definitions should match the table definition. | |
| 149 | + assertThat(row.getColumnDefinitions().getType("k")).isEqualTo(cint()); | |
| 150 | + assertThat(row.getColumnDefinitions().getType("v")).isEqualTo(custom_udt); | |
| 151 | + | |
| 152 | + // And: The column values should represent what was inserted. | |
| 153 | + UDTValue value = row.getUDTValue("v"); | |
| 154 | + assertThat(value.getInt("regular")).isEqualTo(5); | |
| 155 | + assertThat(value.getBytes("c1")).isEqualTo(serializeForDynamicCompositeType("hello", 93)); | |
| 156 | + assertThat(value.getLong("c2")).isEqualTo(400); | |
| 157 | + } | |
| 158 | + | |
| 159 | + | |
| 160 | + private ByteBuffer serializeForDynamicCompositeType(Object... params) { | |
| 45 | 161 | |
| 46 | 162 | List<ByteBuffer> l = new ArrayList<ByteBuffer>(); |
| 47 | 163 | int size = 0; |
| @@ -76,28 +192,37 @@ | ||
| 76 | 192 | return res; |
| 77 | 193 | } |
| 78 | 194 | |
| 79 | - @Test(groups = "short") | |
| 80 | - public void DynamicCompositeTypeTest() { | |
| 81 | - | |
| 82 | - session.execute("INSERT INTO test(k, c, v) VALUES (0, 's@foo:i@32', 1)"); | |
| 83 | - session.execute("INSERT INTO test(k, c, v) VALUES (0, 'i@42', 2)"); | |
| 84 | - session.execute("INSERT INTO test(k, c, v) VALUES (0, 'i@12:i@3', 3)"); | |
| 85 | - | |
| 86 | - ResultSet rs = session.execute("SELECT * FROM test"); | |
| 87 | - | |
| 88 | - Row r = rs.one(); | |
| 89 | - assertEquals(r.getInt("k"), 0); | |
| 90 | - assertEquals(r.getBytesUnsafe("c"), serializeForDynamicType(12, 3)); | |
| 91 | - assertEquals(r.getInt("v"), 3); | |
| 92 | - | |
| 93 | - r = rs.one(); | |
| 94 | - assertEquals(r.getInt("k"), 0); | |
| 95 | - assertEquals(r.getBytesUnsafe("c"), serializeForDynamicType(42)); | |
| 96 | - assertEquals(r.getInt("v"), 2); | |
| 195 | + private ByteBuffer serializeForCompositeType(Object... params) { | |
| 97 | 196 | |
| 98 | - r = rs.one(); | |
| 99 | - assertEquals(r.getInt("k"), 0); | |
| 100 | - assertEquals(r.getBytesUnsafe("c"), serializeForDynamicType("foo", 32)); | |
| 101 | - assertEquals(r.getInt("v"), 1); | |
| 197 | + List<ByteBuffer> l = new ArrayList<ByteBuffer>(); | |
| 198 | + int size = 0; | |
| 199 | + for (Object p : params) { | |
| 200 | + if (p instanceof Integer) { | |
| 201 | + ByteBuffer elt = ByteBuffer.allocate(2 + 4 + 1); | |
| 202 | + elt.putShort((short) 4); | |
| 203 | + elt.putInt((Integer)p); | |
| 204 | + elt.put((byte)0); | |
| 205 | + elt.flip(); | |
| 206 | + size += elt.remaining(); | |
| 207 | + l.add(elt); | |
| 208 | + } else if (p instanceof String) { | |
| 209 | + ByteBuffer bytes = ByteBuffer.wrap(((String)p).getBytes()); | |
| 210 | + ByteBuffer elt = ByteBuffer.allocate(2 + bytes.remaining() + 1); | |
| 211 | + elt.putShort((short) bytes.remaining()); | |
| 212 | + elt.put(bytes); | |
| 213 | + elt.put((byte)0); | |
| 214 | + elt.flip(); | |
| 215 | + size += elt.remaining(); | |
| 216 | + l.add(elt); | |
| 217 | + } else { | |
| 218 | + throw new RuntimeException(); | |
| 219 | + } | |
| 220 | + } | |
| 221 | + ByteBuffer res = ByteBuffer.allocate(size); | |
| 222 | + for (ByteBuffer bb : l) | |
| 223 | + res.put(bb); | |
| 224 | + res.flip(); | |
| 225 | + return res; | |
| 102 | 226 | } |
| 227 | + | |
| 103 | 228 | } |