kikocis/java-driver public
Changesets · Branches · Tags · Compare
| Changeset | a00445aecc8c4a3643ebed4258a6ba27c5874abbc214554d474aa52d77b26a41 |
|---|---|
| Author | Olivier Michallat <olim7t@users.noreply.github.com> · 2015-08-17 10:33 +02:00 |
| Committer | Olivier Michallat <olim7t@users.noreply.github.com> · 2015-08-17 10:33 +02:00 |
| Parents | 7220e608dcd5, 6192348f6e89 |
4 files changed
modified changelog/README.md +5 −0
| @@ -1,5 +1,10 @@ | ||
| 1 | 1 | ## Changelog |
| 2 | 2 | |
| 3 | +### 2.2.0-rc3 (in progress) | |
| 4 | + | |
| 5 | +- [bug] Propagate CodecRegistry to nested UDTs (JAVA-847) | |
| 6 | + | |
| 7 | + | |
| 3 | 8 | ### 2.2.0-rc2 |
| 4 | 9 | |
| 5 | 10 | - [improvement] Rename DateWithoutTime to LocalDate (JAVA-810) |
modified driver-core/src/main/java/com/datastax/driver/core/CodecUtils.java +39 −3
| @@ -64,6 +64,42 @@ | ||
| 64 | 64 | .where(new TypeParameter<V>(){}, valueType); |
| 65 | 65 | } |
| 66 | 66 | |
| 67 | + /** | |
| 68 | + * Propagate a CodecRegistry instance to a given type and its child types, if any. | |
| 69 | + * <p> | |
| 70 | + * By the time CQL types are decoded from response messages, the codec registry is not available; | |
| 71 | + * This method manually sets a CodecRegistry on all CQL types that require it, | |
| 72 | + * and thus should be called immediately after a response is decoded | |
| 73 | + * into either a prepared statement or a result set. | |
| 74 | + * | |
| 75 | + * @param cqlType The decodec {@link DataType cqlType}. | |
| 76 | + * @param codecRegistry The {@link CodecRegistry} instance to use. | |
| 77 | + */ | |
| 78 | + static void setCodecRegistry(DataType cqlType, CodecRegistry codecRegistry) { | |
| 79 | + // user types | |
| 80 | + if(cqlType instanceof UserType) { | |
| 81 | + UserType userType = (UserType)cqlType; | |
| 82 | + userType.setCodecRegistry(codecRegistry); | |
| 83 | + for (UserType.Field field : userType.byIdx) { | |
| 84 | + setCodecRegistry(field.getType(), codecRegistry); | |
| 85 | + } | |
| 86 | + } | |
| 87 | + // tuples | |
| 88 | + else if(cqlType instanceof TupleType) { | |
| 89 | + TupleType tupleType = (TupleType)cqlType; | |
| 90 | + tupleType.setCodecRegistry(codecRegistry); | |
| 91 | + for (DataType componentType : tupleType.getComponentTypes()) { | |
| 92 | + setCodecRegistry(componentType, codecRegistry); | |
| 93 | + } | |
| 94 | + } | |
| 95 | + // collections | |
| 96 | + else if(cqlType.isCollection()) { | |
| 97 | + for (DataType inner : cqlType.getTypeArguments()) { | |
| 98 | + setCodecRegistry(inner, codecRegistry); | |
| 99 | + } | |
| 100 | + } | |
| 101 | + } | |
| 102 | + | |
| 67 | 103 | /** |
| 68 | 104 | * Utility method to serialize user-provided values. |
| 69 | 105 | * <p> |
| @@ -166,9 +202,9 @@ | ||
| 166 | 202 | /** |
| 167 | 203 | * Utility method that reads the collection size. |
| 168 | 204 | * Mainly intended for collection codecs when deserializing CQL collections. |
| 169 | - * @param input | |
| 170 | - * @param version | |
| 171 | - * @return | |
| 205 | + * @param input A ByteBuffer containing a serialized CQL collection | |
| 206 | + * @param version The protocol version to use. | |
| 207 | + * @return The collection size | |
| 172 | 208 | */ |
| 173 | 209 | public static int readCollectionSize(ByteBuffer input, ProtocolVersion version) { |
| 174 | 210 | switch (version) { |
modified driver-core/src/main/java/com/datastax/driver/core/ColumnDefinitions.java +1 −18
| @@ -286,28 +286,11 @@ | ||
| 286 | 286 | |
| 287 | 287 | void setCodecRegistry(CodecRegistry codecRegistry) { |
| 288 | 288 | for (Definition definition : byIdx) { |
| 289 | - setCodecRegistry(definition.type, codecRegistry); | |
| 289 | + CodecUtils.setCodecRegistry(definition.type, codecRegistry); | |
| 290 | 290 | } |
| 291 | 291 | this.codecRegistry = codecRegistry; |
| 292 | 292 | } |
| 293 | 293 | |
| 294 | - void setCodecRegistry(DataType cqlType, CodecRegistry codecRegistry) { | |
| 295 | - // by the time UDT and tuple values are decoded from response messages, the | |
| 296 | - // codec registry is not available, so set it now; | |
| 297 | - // this method should be called immediately after a response | |
| 298 | - // is decoded into either a prepared statement or a result set. | |
| 299 | - if(cqlType instanceof UserType) { | |
| 300 | - ((UserType)cqlType).setCodecRegistry(codecRegistry); | |
| 301 | - } | |
| 302 | - if(cqlType instanceof TupleType) { | |
| 303 | - ((TupleType)cqlType).setCodecRegistry(codecRegistry); | |
| 304 | - } | |
| 305 | - // propagate codec registry to inner types | |
| 306 | - for (DataType inner : cqlType.getTypeArguments()) { | |
| 307 | - setCodecRegistry(inner, codecRegistry); | |
| 308 | - } | |
| 309 | - } | |
| 310 | - | |
| 311 | 294 | /** |
| 312 | 295 | * A column definition. |
| 313 | 296 | */ |
added driver-core/src/test/java/com/datastax/driver/core/TypeCodecNestedUDTAndTupleIntegrationTest.java +111 −0
| @@ -0,0 +1,111 @@ | ||
| 1 | +/* | |
| 2 | + * Copyright (C) 2012-2015 DataStax Inc. | |
| 3 | + * | |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); | |
| 5 | + * you may not use this file except in compliance with the License. | |
| 6 | + * You may obtain a copy of the License at | |
| 7 | + * | |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 | |
| 9 | + * | |
| 10 | + * Unless required by applicable law or agreed to in writing, software | |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, | |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
| 13 | + * See the License for the specific language governing permissions and | |
| 14 | + * limitations under the License. | |
| 15 | + */ | |
| 16 | +package com.datastax.driver.core; | |
| 17 | + | |
| 18 | +import java.util.*; | |
| 19 | + | |
| 20 | +import com.google.common.collect.Lists; | |
| 21 | +import com.google.common.collect.Sets; | |
| 22 | +import org.testng.annotations.Test; | |
| 23 | + | |
| 24 | +import static org.assertj.core.api.Assertions.assertThat; | |
| 25 | + | |
| 26 | +import com.datastax.driver.core.utils.CassandraVersion; | |
| 27 | + | |
| 28 | +/** | |
| 29 | + * This tests that the CodecRegistry is able to create codecs on the fly | |
| 30 | + * for nested UDTs and tuples received from a ResultSet, i.e. the first time | |
| 31 | + * the registry encounters the UDT or tuple, it's coming from a ResultSet, | |
| 32 | + * not from the Cluster's metadata. | |
| 33 | + * | |
| 34 | + * @jira_ticket JAVA-847 | |
| 35 | + */ | |
| 36 | +@CassandraVersion(major = 2.1) | |
| 37 | +public class TypeCodecNestedUDTAndTupleIntegrationTest extends CCMBridge.PerClassSingleNodeCluster { | |
| 38 | + | |
| 39 | + @Override | |
| 40 | + protected Collection<String> getTableDefinitions() { | |
| 41 | + return Lists.newArrayList( | |
| 42 | + "CREATE TYPE IF NOT EXISTS \"udt3\" (f3 text)", | |
| 43 | + "CREATE TYPE IF NOT EXISTS \"udt2\" (f2 frozen<udt3>)", | |
| 44 | + "CREATE TYPE IF NOT EXISTS \"udt1\" (f1 frozen<udt2>)", | |
| 45 | + "CREATE TABLE IF NOT EXISTS \"t1\" (pk int PRIMARY KEY, " | |
| 46 | + + "c1 frozen<udt1>, " | |
| 47 | + + "c2 frozen<tuple<tuple<tuple<text>>>>, " | |
| 48 | + + "c3 frozen<tuple<tuple<tuple<udt1>>>>" | |
| 49 | + + ")", | |
| 50 | + // it's important to insert values using CQL literals | |
| 51 | + // so that the CodecRegistry will not be required until | |
| 52 | + // we receive a ResultSet | |
| 53 | + "INSERT INTO t1 (pk, c1) VALUES (1, {f1:{f2:{f3:'foo'}}})", | |
| 54 | + "INSERT INTO t1 (pk, c2) VALUES (2, ((('foo'))))", | |
| 55 | + "INSERT INTO t1 (pk, c3) VALUES (3, ((({f1:{f2:{f3:'foo'}}}))))" | |
| 56 | + ); | |
| 57 | + } | |
| 58 | + | |
| 59 | + @Test(groups = "short") | |
| 60 | + public void should_set_registry_on_nested_udts() { | |
| 61 | + ResultSet rows = session.execute("SELECT c1 FROM t1 WHERE pk = 1"); | |
| 62 | + Row row = rows.one(); | |
| 63 | + // here the CodecRegistry will create a codec on-the-fly using the UserType received from the resultset metadata | |
| 64 | + UDTValue udt1 = row.getUDTValue("c1"); | |
| 65 | + assertThat(udt1.getCodecRegistry()).isSameAs(cluster.getConfiguration().getCodecRegistry()); | |
| 66 | + UDTValue udt2 = udt1.getUDTValue("f1"); | |
| 67 | + assertThat(udt2.getCodecRegistry()).isSameAs(cluster.getConfiguration().getCodecRegistry()); | |
| 68 | + UDTValue udt3 = udt2.getUDTValue("f2"); | |
| 69 | + assertThat(udt3.getCodecRegistry()).isSameAs(cluster.getConfiguration().getCodecRegistry()); | |
| 70 | + String f3 = udt3.getString("f3"); | |
| 71 | + assertThat(f3).isEqualTo("foo"); | |
| 72 | + } | |
| 73 | + | |
| 74 | + @Test(groups = "short") | |
| 75 | + public void should_set_registry_on_nested_tuples() { | |
| 76 | + ResultSet rows = session.execute("SELECT c2 FROM t1 WHERE pk = 2"); | |
| 77 | + Row row = rows.one(); | |
| 78 | + // here the CodecRegistry will create a codec on-the-fly using the TupleType received from the resultset metadata | |
| 79 | + TupleValue tuple1 = row.getTupleValue("c2"); | |
| 80 | + assertThat(tuple1.getCodecRegistry()).isSameAs(cluster.getConfiguration().getCodecRegistry()); | |
| 81 | + TupleValue tuple2 = tuple1.getTupleValue(0); | |
| 82 | + assertThat(tuple2.getCodecRegistry()).isSameAs(cluster.getConfiguration().getCodecRegistry()); | |
| 83 | + TupleValue tuple3 = tuple2.getTupleValue(0); | |
| 84 | + assertThat(tuple3.getCodecRegistry()).isSameAs(cluster.getConfiguration().getCodecRegistry()); | |
| 85 | + String s = tuple3.getString(0); | |
| 86 | + assertThat(s).isEqualTo("foo"); | |
| 87 | + } | |
| 88 | + | |
| 89 | + @Test(groups = "short") | |
| 90 | + public void should_set_registry_on_nested_tuples_and_udts() { | |
| 91 | + ResultSet rows = session.execute("SELECT c3 FROM t1 WHERE pk = 3"); | |
| 92 | + Row row = rows.one(); | |
| 93 | + // here the CodecRegistry will create a codec on-the-fly using the TupleType received from the resultset metadata | |
| 94 | + TupleValue tuple1 = row.getTupleValue("c3"); | |
| 95 | + assertThat(tuple1.getCodecRegistry()).isSameAs(cluster.getConfiguration().getCodecRegistry()); | |
| 96 | + TupleValue tuple2 = tuple1.getTupleValue(0); | |
| 97 | + assertThat(tuple2.getCodecRegistry()).isSameAs(cluster.getConfiguration().getCodecRegistry()); | |
| 98 | + TupleValue tuple3 = tuple2.getTupleValue(0); | |
| 99 | + assertThat(tuple3.getCodecRegistry()).isSameAs(cluster.getConfiguration().getCodecRegistry()); | |
| 100 | + UDTValue udt1 = tuple3.getUDTValue(0); | |
| 101 | + assertThat(udt1.getCodecRegistry()).isSameAs(cluster.getConfiguration().getCodecRegistry()); | |
| 102 | + UDTValue udt2 = udt1.getUDTValue("f1"); | |
| 103 | + assertThat(udt2.getCodecRegistry()).isSameAs(cluster.getConfiguration().getCodecRegistry()); | |
| 104 | + UDTValue udt3 = udt2.getUDTValue("f2"); | |
| 105 | + assertThat(udt3.getCodecRegistry()).isSameAs(cluster.getConfiguration().getCodecRegistry()); | |
| 106 | + String f3 = udt3.getString("f3"); | |
| 107 | + assertThat(f3).isEqualTo("foo"); | |
| 108 | + } | |
| 109 | + | |
| 110 | + | |
| 111 | +} |