kikocis/java-driver public
Changesets · Branches · Tags · Compare
| Changeset | 420b015e3f44f305ec0e64fdcc8deae7128c35d9a8b8dffc4a5c3eb1d9ca08b3 |
|---|---|
| Author | Olivier Michallat <olim7t@users.noreply.github.com> · 2016-04-26 16:29 +02:00 |
| Committer | Olivier Michallat <olim7t@users.noreply.github.com> · 2016-04-26 16:29 +02:00 |
| Parents | 2e3795079caa, d7b1c9e40aa5 |
5 files changed
modified changelog/README.md +1 −0
| @@ -17,6 +17,7 @@ | ||
| 17 | 17 | - [bug] JAVA-1095: Check protocol version for custom payload before sending the query. |
| 18 | 18 | - [improvement] JAVA-1133: Add OSGi headers to cassandra-driver-extras. |
| 19 | 19 | - [bug] JAVA-1137: Incorrect string returned by DataType.asFunctionParameterString() for collections and tuples. |
| 20 | +- [bug] JAVA-1046: (Dynamic)CompositeTypes need to be parsed as string literal, not blob. | |
| 20 | 21 | |
| 21 | 22 | Merged from 2.1 branch: |
| 22 | 23 |
modified driver-core/src/main/java/com/datastax/driver/core/AggregateMetadata.java +45 −3
| @@ -15,9 +15,12 @@ | ||
| 15 | 15 | */ |
| 16 | 16 | package com.datastax.driver.core; |
| 17 | 17 | |
| 18 | +import com.datastax.driver.core.utils.Bytes; | |
| 18 | 19 | import com.google.common.base.Objects; |
| 19 | 20 | import com.google.common.collect.ImmutableList; |
| 20 | 21 | import com.google.common.collect.Lists; |
| 22 | +import org.slf4j.Logger; | |
| 23 | +import org.slf4j.LoggerFactory; | |
| 21 | 24 | |
| 22 | 25 | import java.nio.ByteBuffer; |
| 23 | 26 | import java.util.Collections; |
| @@ -28,6 +31,8 @@ | ||
| 28 | 31 | */ |
| 29 | 32 | public class AggregateMetadata { |
| 30 | 33 | |
| 34 | + private static final Logger LOGGER = LoggerFactory.getLogger(AggregateMetadata.class); | |
| 35 | + | |
| 31 | 36 | private final KeyspaceMetadata keyspace; |
| 32 | 37 | private final String simpleName; |
| 33 | 38 | private final List<DataType> argumentTypes; |
| @@ -101,11 +106,29 @@ | ||
| 101 | 106 | if (version.getMajor() >= 3) { |
| 102 | 107 | stateType = DataTypeCqlNameParser.parse(stateTypeName, cluster, ksm.getName(), ksm.userTypes, null, false, false); |
| 103 | 108 | String rawInitCond = row.getString("initcond"); |
| 104 | - initCond = rawInitCond == null ? null : codecRegistry.codecFor(stateType).parse(rawInitCond); | |
| 109 | + if (rawInitCond == null) { | |
| 110 | + initCond = null; | |
| 111 | + } else { | |
| 112 | + try { | |
| 113 | + initCond = codecRegistry.codecFor(stateType).parse(rawInitCond); | |
| 114 | + } catch (RuntimeException e) { | |
| 115 | + LOGGER.warn("Failed to parse INITCOND literal: {}; getInitCond() will return the text literal instead.", rawInitCond); | |
| 116 | + initCond = rawInitCond; | |
| 117 | + } | |
| 118 | + } | |
| 105 | 119 | } else { |
| 106 | 120 | stateType = DataTypeClassNameParser.parseOne(stateTypeName, protocolVersion, codecRegistry); |
| 107 | 121 | ByteBuffer rawInitCond = row.getBytes("initcond"); |
| 108 | - initCond = rawInitCond == null ? null : codecRegistry.codecFor(stateType).deserialize(rawInitCond, protocolVersion); | |
| 122 | + if (rawInitCond == null) { | |
| 123 | + initCond = null; | |
| 124 | + } else { | |
| 125 | + try { | |
| 126 | + initCond = codecRegistry.codecFor(stateType).deserialize(rawInitCond, protocolVersion); | |
| 127 | + } catch (RuntimeException e) { | |
| 128 | + LOGGER.warn("Failed to deserialize INITCOND value: {}; getInitCond() will return the raw bytes instead.", Bytes.toHexString(rawInitCond)); | |
| 129 | + initCond = rawInitCond; | |
| 130 | + } | |
| 131 | + } | |
| 109 | 132 | } |
| 110 | 133 | |
| 111 | 134 | String finalFuncFullName = finalFuncSimpleName == null ? null : Metadata.fullFunctionName(finalFuncSimpleName, Collections.singletonList(stateType)); |
| @@ -191,13 +214,24 @@ | ||
| 191 | 214 | if (initCond != null) |
| 192 | 215 | TableMetadata.spaceOrNewLine(sb, formatted) |
| 193 | 216 | .append("INITCOND ") |
| 194 | - .append(stateTypeCodec.format(initCond)); | |
| 217 | + .append(formatInitCond()); | |
| 195 | 218 | |
| 196 | 219 | sb.append(';'); |
| 197 | 220 | |
| 198 | 221 | return sb.toString(); |
| 199 | 222 | } |
| 200 | 223 | |
| 224 | + private String formatInitCond() { | |
| 225 | + if (stateTypeCodec.accepts(initCond)) { | |
| 226 | + try { | |
| 227 | + return stateTypeCodec.format(initCond); | |
| 228 | + } catch (RuntimeException e) { | |
| 229 | + LOGGER.info("Failed to format INITCOND literal: {}", initCond); | |
| 230 | + } | |
| 231 | + } | |
| 232 | + return initCond.toString(); | |
| 233 | + } | |
| 234 | + | |
| 201 | 235 | private void appendSignature(StringBuilder sb) { |
| 202 | 236 | sb |
| 203 | 237 | .append(Metadata.escapeId(simpleName)) |
| @@ -281,6 +315,14 @@ | ||
| 281 | 315 | * This is the value specified with {@code INITCOND} in the {@code CREATE AGGREGATE...} |
| 282 | 316 | * statement. It's passed to the initial invocation of the state function (if that function |
| 283 | 317 | * does not accept null arguments). |
| 318 | + * <p/> | |
| 319 | + * The actual type of the returned object depends on the aggregate's | |
| 320 | + * {@link #getStateType() state type} and on the {@link TypeCodec codec} used | |
| 321 | + * to {@link TypeCodec#parse(String) parse} the {@code INITCOND} literal. | |
| 322 | + * <p/> | |
| 323 | + * If, for some reason, the {@code INITCOND} literal cannot be parsed, | |
| 324 | + * a warning will be logged and the returned object will be the original | |
| 325 | + * {@code INITCOND} literal in its textual, non-parsed form. | |
| 284 | 326 | * |
| 285 | 327 | * @return the initial state, or {@code null} if there is none. |
| 286 | 328 | */ |
modified driver-core/src/test/java/com/datastax/driver/core/AggregateMetadataTest.java +89 −0
| @@ -16,11 +16,13 @@ | ||
| 16 | 16 | package com.datastax.driver.core; |
| 17 | 17 | |
| 18 | 18 | import com.datastax.driver.core.utils.CassandraVersion; |
| 19 | +import org.testng.SkipException; | |
| 19 | 20 | import org.testng.annotations.Test; |
| 20 | 21 | |
| 21 | 22 | import static com.datastax.driver.core.Assertions.assertThat; |
| 22 | 23 | import static com.datastax.driver.core.DataType.cint; |
| 23 | 24 | import static com.datastax.driver.core.DataType.text; |
| 25 | +import static com.datastax.driver.core.TestUtils.serializeForDynamicCompositeType; | |
| 24 | 26 | |
| 25 | 27 | @CassandraVersion(major = 2.2) |
| 26 | 28 | @CCMConfig(config = "enable_user_defined_functions:true") |
| @@ -171,6 +173,93 @@ | ||
| 171 | 173 | assertThat(aggregate.toString()).isEqualTo(cqlAggregate); |
| 172 | 174 | } |
| 173 | 175 | |
| 176 | + /** | |
| 177 | + * Validates aggregates with DynamicCompositeType state types and an initcond value that is a literal, i.e.: | |
| 178 | + * 's@foo:i@32' can be appropriately parsed and generate a CQL string with the init cond as a hex string, i.e.: | |
| 179 | + * 0x80730003666f6f00806900040000002000. | |
| 180 | + * | |
| 181 | + * @jira_ticket JAVA-1046 | |
| 182 | + * @test_category metadata | |
| 183 | + * @since 3.0.1 | |
| 184 | + */ | |
| 185 | + @Test(groups = "short") | |
| 186 | + @CassandraVersion(major = 2.2) | |
| 187 | + public void should_parse_and_format_aggregate_with_composite_type_literal_initcond() { | |
| 188 | + VersionNumber ver = VersionNumber.parse(CCMBridge.getCassandraVersion()); | |
| 189 | + if (ver.getMajor() == 3) { | |
| 190 | + if ((ver.getMinor() >= 1 && ver.getMinor() < 4) || (ver.getMinor() == 0 && ver.getPatch() < 4)) { | |
| 191 | + throw new SkipException("Requires C* 2.2.X, 3.0.4+ or 3.4.X+"); | |
| 192 | + } | |
| 193 | + } | |
| 194 | + parse_and_format_aggregate_with_composite_type("ag0", "'s@foo:i@32'"); | |
| 195 | + } | |
| 196 | + | |
| 197 | + /** | |
| 198 | + * Validates aggregates with DynamicCompositeType state types and an initcond value that is a hex string | |
| 199 | + * representing the bytes for the type, i.e.: 0x80730003666f6f00806900040000002000' can be appropriately parsed | |
| 200 | + * and generates an equivalent CQL string. | |
| 201 | + * | |
| 202 | + * @jira_ticket JAVA-1046 | |
| 203 | + * @test_category metadata | |
| 204 | + * @since 3.0.1 | |
| 205 | + */ | |
| 206 | + @Test(groups = "short") | |
| 207 | + @CassandraVersion(major = 3.0, minor = 4) | |
| 208 | + public void should_parse_and_format_aggregate_with_composite_type_hex_initcond() { | |
| 209 | + VersionNumber ver = VersionNumber.parse(CCMBridge.getCassandraVersion()); | |
| 210 | + if ((ver.getMinor() >= 1 && ver.getMinor() < 4)) { | |
| 211 | + throw new SkipException("Requires 3.0.4+ or 3.4.X+"); | |
| 212 | + } | |
| 213 | + parse_and_format_aggregate_with_composite_type("ag1", "0x80730003666f6f00806900040000002000"); | |
| 214 | + } | |
| 215 | + | |
| 216 | + public void parse_and_format_aggregate_with_composite_type(String aggregateName, String initCond) { | |
| 217 | + // given | |
| 218 | + DataType custom = DataType.custom( | |
| 219 | + "org.apache.cassandra.db.marshal.DynamicCompositeType(" + | |
| 220 | + "s=>org.apache.cassandra.db.marshal.UTF8Type," + | |
| 221 | + "i=>org.apache.cassandra.db.marshal.Int32Type)"); | |
| 222 | + String cqlFunction = String.format( | |
| 223 | + "CREATE FUNCTION %s.%s_id(i %s) " + | |
| 224 | + "RETURNS NULL ON NULL INPUT " + | |
| 225 | + "RETURNS %s " + | |
| 226 | + "LANGUAGE java " + | |
| 227 | + "AS 'return i;';", keyspace, aggregateName, custom, custom); | |
| 228 | + session().execute(cqlFunction); | |
| 229 | + | |
| 230 | + String cqlAggregate = String.format( | |
| 231 | + "CREATE AGGREGATE %s.%s() " + | |
| 232 | + "SFUNC %s_id " + | |
| 233 | + "STYPE %s " + | |
| 234 | + "INITCOND %s;", keyspace, aggregateName, aggregateName, custom, initCond); | |
| 235 | + | |
| 236 | + String expectedAggregate = String.format( | |
| 237 | + "CREATE AGGREGATE %s.%s() " + | |
| 238 | + "SFUNC %s_id " + | |
| 239 | + "STYPE %s " + | |
| 240 | + "INITCOND 0x80730003666f6f00806900040000002000;", keyspace, aggregateName, aggregateName, custom); | |
| 241 | + | |
| 242 | + int agCount = 0; | |
| 243 | + | |
| 244 | + // when | |
| 245 | + session().execute(cqlAggregate); | |
| 246 | + // then | |
| 247 | + KeyspaceMetadata keyspace = cluster().getMetadata().getKeyspace(this.keyspace); | |
| 248 | + FunctionMetadata stateFunc = keyspace.getFunction(aggregateName + "_id", custom); | |
| 249 | + AggregateMetadata aggregate = keyspace.getAggregate(aggregateName); | |
| 250 | + assertThat(aggregate).isNotNull(); | |
| 251 | + assertThat(aggregate.getSignature()).isEqualTo(aggregateName + "()"); | |
| 252 | + assertThat(aggregate.getSimpleName()).isEqualTo(aggregateName); | |
| 253 | + assertThat(aggregate.getArgumentTypes()).isEmpty(); | |
| 254 | + assertThat(aggregate.getFinalFunc()).isNull(); | |
| 255 | + assertThat(aggregate.getInitCond()).isEqualTo(serializeForDynamicCompositeType("foo", 32)); | |
| 256 | + assertThat(aggregate.getReturnType()).isEqualTo(custom); | |
| 257 | + assertThat(aggregate.getStateFunc()).isEqualTo(stateFunc); | |
| 258 | + assertThat(aggregate.getStateType()).isEqualTo(custom); | |
| 259 | + | |
| 260 | + assertThat(aggregate.toString()).isEqualTo(expectedAggregate); | |
| 261 | + } | |
| 262 | + | |
| 174 | 263 | @Override |
| 175 | 264 | public void onTestContextInitialized() { |
| 176 | 265 | execute( |
modified driver-core/src/test/java/com/datastax/driver/core/CustomTypeTest.java +2 −69
| @@ -26,6 +26,8 @@ | ||
| 26 | 26 | import static com.datastax.driver.core.Assertions.assertThat; |
| 27 | 27 | import static com.datastax.driver.core.DataType.cint; |
| 28 | 28 | import static com.datastax.driver.core.DataType.custom; |
| 29 | +import static com.datastax.driver.core.TestUtils.serializeForCompositeType; | |
| 30 | +import static com.datastax.driver.core.TestUtils.serializeForDynamicCompositeType; | |
| 29 | 31 | |
| 30 | 32 | /** |
| 31 | 33 | * Test we "support" custom types. |
| @@ -185,73 +187,4 @@ | ||
| 185 | 187 | assertThat(value.getBytes("c1")).isEqualTo(serializeForDynamicCompositeType("hello", 93)); |
| 186 | 188 | assertThat(value.getLong("c2")).isEqualTo(400); |
| 187 | 189 | } |
| 188 | - | |
| 189 | - | |
| 190 | - private ByteBuffer serializeForDynamicCompositeType(Object... params) { | |
| 191 | - List<ByteBuffer> l = new ArrayList<ByteBuffer>(); | |
| 192 | - int size = 0; | |
| 193 | - for (Object p : params) { | |
| 194 | - if (p instanceof Integer) { | |
| 195 | - ByteBuffer elt = ByteBuffer.allocate(2 + 2 + 4 + 1); | |
| 196 | - elt.putShort((short) (0x8000 | 'i')); | |
| 197 | - elt.putShort((short) 4); | |
| 198 | - elt.putInt((Integer) p); | |
| 199 | - elt.put((byte) 0); | |
| 200 | - elt.flip(); | |
| 201 | - size += elt.remaining(); | |
| 202 | - l.add(elt); | |
| 203 | - } else if (p instanceof String) { | |
| 204 | - ByteBuffer bytes = ByteBuffer.wrap(((String) p).getBytes()); | |
| 205 | - ByteBuffer elt = ByteBuffer.allocate(2 + 2 + bytes.remaining() + 1); | |
| 206 | - elt.putShort((short) (0x8000 | 's')); | |
| 207 | - elt.putShort((short) bytes.remaining()); | |
| 208 | - elt.put(bytes); | |
| 209 | - elt.put((byte) 0); | |
| 210 | - elt.flip(); | |
| 211 | - size += elt.remaining(); | |
| 212 | - l.add(elt); | |
| 213 | - } else { | |
| 214 | - throw new RuntimeException(); | |
| 215 | - } | |
| 216 | - } | |
| 217 | - ByteBuffer res = ByteBuffer.allocate(size); | |
| 218 | - for (ByteBuffer bb : l) | |
| 219 | - res.put(bb); | |
| 220 | - res.flip(); | |
| 221 | - return res; | |
| 222 | - } | |
| 223 | - | |
| 224 | - private ByteBuffer serializeForCompositeType(Object... params) { | |
| 225 | - | |
| 226 | - List<ByteBuffer> l = new ArrayList<ByteBuffer>(); | |
| 227 | - int size = 0; | |
| 228 | - for (Object p : params) { | |
| 229 | - if (p instanceof Integer) { | |
| 230 | - ByteBuffer elt = ByteBuffer.allocate(2 + 4 + 1); | |
| 231 | - elt.putShort((short) 4); | |
| 232 | - elt.putInt((Integer) p); | |
| 233 | - elt.put((byte) 0); | |
| 234 | - elt.flip(); | |
| 235 | - size += elt.remaining(); | |
| 236 | - l.add(elt); | |
| 237 | - } else if (p instanceof String) { | |
| 238 | - ByteBuffer bytes = ByteBuffer.wrap(((String) p).getBytes()); | |
| 239 | - ByteBuffer elt = ByteBuffer.allocate(2 + bytes.remaining() + 1); | |
| 240 | - elt.putShort((short) bytes.remaining()); | |
| 241 | - elt.put(bytes); | |
| 242 | - elt.put((byte) 0); | |
| 243 | - elt.flip(); | |
| 244 | - size += elt.remaining(); | |
| 245 | - l.add(elt); | |
| 246 | - } else { | |
| 247 | - throw new RuntimeException(); | |
| 248 | - } | |
| 249 | - } | |
| 250 | - ByteBuffer res = ByteBuffer.allocate(size); | |
| 251 | - for (ByteBuffer bb : l) | |
| 252 | - res.put(bb); | |
| 253 | - res.flip(); | |
| 254 | - return res; | |
| 255 | - } | |
| 256 | - | |
| 257 | 190 | } |
modified driver-core/src/test/java/com/datastax/driver/core/TestUtils.java +84 −0
| @@ -836,4 +836,88 @@ | ||
| 836 | 836 | return (bean.getFreePhysicalMemorySize() + bean.getFreeSwapSpaceSize()) / 1024 / 1024; |
| 837 | 837 | } |
| 838 | 838 | |
| 839 | + /** | |
| 840 | + * Helper for generating a DynamicCompositeType {@link ByteBuffer} from the given parameters. | |
| 841 | + * | |
| 842 | + * Any of params given as an Integer will be considered with a field name of 'i', any as String will | |
| 843 | + * be considered with a field name of 's'. | |
| 844 | + * | |
| 845 | + * @param params params to serialize. | |
| 846 | + * @return bytes representing a DynamicCompositeType. | |
| 847 | + */ | |
| 848 | + public static ByteBuffer serializeForDynamicCompositeType(Object... params) { | |
| 849 | + List<ByteBuffer> l = new ArrayList<ByteBuffer>(); | |
| 850 | + int size = 0; | |
| 851 | + for (Object p : params) { | |
| 852 | + if (p instanceof Integer) { | |
| 853 | + ByteBuffer elt = ByteBuffer.allocate(2 + 2 + 4 + 1); | |
| 854 | + elt.putShort((short) (0x8000 | 'i')); | |
| 855 | + elt.putShort((short) 4); | |
| 856 | + elt.putInt((Integer) p); | |
| 857 | + elt.put((byte) 0); | |
| 858 | + elt.flip(); | |
| 859 | + size += elt.remaining(); | |
| 860 | + l.add(elt); | |
| 861 | + } else if (p instanceof String) { | |
| 862 | + ByteBuffer bytes = ByteBuffer.wrap(((String) p).getBytes()); | |
| 863 | + ByteBuffer elt = ByteBuffer.allocate(2 + 2 + bytes.remaining() + 1); | |
| 864 | + elt.putShort((short) (0x8000 | 's')); | |
| 865 | + elt.putShort((short) bytes.remaining()); | |
| 866 | + elt.put(bytes); | |
| 867 | + elt.put((byte) 0); | |
| 868 | + elt.flip(); | |
| 869 | + size += elt.remaining(); | |
| 870 | + l.add(elt); | |
| 871 | + } else { | |
| 872 | + throw new RuntimeException(); | |
| 873 | + } | |
| 874 | + } | |
| 875 | + ByteBuffer res = ByteBuffer.allocate(size); | |
| 876 | + for (ByteBuffer bb : l) | |
| 877 | + res.put(bb); | |
| 878 | + res.flip(); | |
| 879 | + return res; | |
| 880 | + } | |
| 881 | + | |
| 882 | + /** | |
| 883 | + * Helper for generating a Composite {@link ByteBuffer} from the given parameters. | |
| 884 | + * | |
| 885 | + * Expects Integer and String types for parameters. | |
| 886 | + * | |
| 887 | + * @param params params to serialize. | |
| 888 | + * @return bytes representing a CompositeType | |
| 889 | + */ | |
| 890 | + public static ByteBuffer serializeForCompositeType(Object... params) { | |
| 891 | + | |
| 892 | + List<ByteBuffer> l = new ArrayList<ByteBuffer>(); | |
| 893 | + int size = 0; | |
| 894 | + for (Object p : params) { | |
| 895 | + if (p instanceof Integer) { | |
| 896 | + ByteBuffer elt = ByteBuffer.allocate(2 + 4 + 1); | |
| 897 | + elt.putShort((short) 4); | |
| 898 | + elt.putInt((Integer) p); | |
| 899 | + elt.put((byte) 0); | |
| 900 | + elt.flip(); | |
| 901 | + size += elt.remaining(); | |
| 902 | + l.add(elt); | |
| 903 | + } else if (p instanceof String) { | |
| 904 | + ByteBuffer bytes = ByteBuffer.wrap(((String) p).getBytes()); | |
| 905 | + ByteBuffer elt = ByteBuffer.allocate(2 + bytes.remaining() + 1); | |
| 906 | + elt.putShort((short) bytes.remaining()); | |
| 907 | + elt.put(bytes); | |
| 908 | + elt.put((byte) 0); | |
| 909 | + elt.flip(); | |
| 910 | + size += elt.remaining(); | |
| 911 | + l.add(elt); | |
| 912 | + } else { | |
| 913 | + throw new RuntimeException(); | |
| 914 | + } | |
| 915 | + } | |
| 916 | + ByteBuffer res = ByteBuffer.allocate(size); | |
| 917 | + for (ByteBuffer bb : l) | |
| 918 | + res.put(bb); | |
| 919 | + res.flip(); | |
| 920 | + return res; | |
| 921 | + } | |
| 922 | + | |
| 839 | 923 | } |