Elffuss BraidHub

kikocis/java-driver public

Merge pull request #535 from datastax/java996

JAVA-996: Make CodecRegistry available to ProtocolDecoder.
Changesetb4554bb54c38f6518409b532e1ed1991bb2f954d3f81a429447eb33cd6c13316
AuthorOlivier Michallat <olim7t@users.noreply.github.com> · 2015-12-03 18:22 +01:00
CommitterOlivier Michallat <olim7t@users.noreply.github.com> · 2015-12-03 18:22 +01:00
Parents9e8f5a07be4b, 51386aeba860

Browse the files at this changeset

10 files changed

modified changelog/README.md +1 −0
@@ -9,6 +9,7 @@
99 control connection.
1010 - [improvement] JAVA-1003: Change default consistency level to LOCAL_ONE (amends JAVA-926).
1111 - [improvement] JAVA-863: Idempotence propagation in prepared statements.
12+- [improvement] JAVA-996: Make CodecRegistry available to ProtocolDecoder.
1213
1314 Merged from 2.1 branch:
1415
modified driver-core/src/main/java/com/datastax/driver/core/ArrayBackedResultSet.java +0 −6
@@ -77,12 +77,6 @@
7777 // this explicitly because MultiPage implementation don't support info == null.
7878 assert r.metadata.pagingState == null || info != null;
7979
80- // if this is an internal call, it doesn't matter which codecRegistry will be used
81- CodecRegistry codecRegistry = session == null ?
82- CodecRegistry.DEFAULT_INSTANCE :
83- session.getCluster().getConfiguration().getCodecRegistry();
84- columnDefs.setCodecRegistry(codecRegistry);
85-
8680 return r.metadata.pagingState == null
8781 ? new SinglePage(columnDefs, tokenFactory, protocolVersion, r.data, info)
8882 : new MultiPage(columnDefs, tokenFactory, protocolVersion, r.data, info, r.metadata.pagingState, session, statement);
modified driver-core/src/main/java/com/datastax/driver/core/ArrayBackedRow.java +3 −4
@@ -16,11 +16,9 @@
1616 package com.datastax.driver.core;
1717
1818 import java.nio.ByteBuffer;
19-import java.util.*;
19+import java.util.List;
2020 import java.util.regex.Pattern;
2121
22-import com.google.common.base.Joiner;
23-
2422 import com.datastax.driver.core.exceptions.DriverInternalError;
2523
2624 /**
@@ -75,7 +73,7 @@
7573
7674 @Override
7775 protected CodecRegistry getCodecRegistry() {
78- return metadata.getCodecRegistry();
76+ return metadata.codecRegistry;
7977 }
8078
8179 @Override
@@ -102,6 +100,7 @@
102100 return getToken(metadata.getFirstIdx(name));
103101 }
104102
103+ @Override
105104 public Token getPartitionKeyToken() {
106105 int i = 0;
107106 for (ColumnDefinitions.Definition column : metadata) {
modified driver-core/src/main/java/com/datastax/driver/core/ColumnDefinitions.java +4 −52
@@ -55,15 +55,16 @@
5555 */
5656 public class ColumnDefinitions implements Iterable<ColumnDefinitions.Definition> {
5757
58- static final ColumnDefinitions EMPTY = new ColumnDefinitions(new Definition[0]);
58+ static final ColumnDefinitions EMPTY = new ColumnDefinitions(new Definition[0], CodecRegistry.DEFAULT_INSTANCE);
5959
6060 private final Definition[] byIdx;
6161 private final Map<String, int[]> byName;
62- private volatile CodecRegistry codecRegistry;
62+ final CodecRegistry codecRegistry;
6363
64- ColumnDefinitions(Definition[] defs) {
64+ ColumnDefinitions(Definition[] defs, CodecRegistry codecRegistry) {
6565
6666 this.byIdx = defs;
67+ this.codecRegistry = codecRegistry;
6768 this.byName = new HashMap<String, int[]>(defs.length);
6869
6970 for (int i = 0; i < defs.length; i++) {
@@ -278,55 +279,6 @@
278279 return getAllIdx(name)[0];
279280 }
280281
281- CodecRegistry getCodecRegistry() {
282- // this method should only be called after this field is set
283- assert codecRegistry != null;
284- return codecRegistry;
285- }
286-
287- void setCodecRegistry(CodecRegistry codecRegistry) {
288- for (Definition definition : byIdx) {
289- propagateCodecRegistry(definition.type, codecRegistry);
290- }
291- this.codecRegistry = codecRegistry;
292- }
293-
294- /**
295- * Propagate a CodecRegistry instance to a given type and its child types, if any.
296- * <p>
297- * By the time CQL types are decoded from response messages, the codec registry is not available;
298- * This method manually sets a CodecRegistry on all CQL types that require it,
299- * and thus should be called immediately after a response is decoded
300- * into either a prepared statement or a result set.
301- *
302- * @param cqlType The decodec {@link DataType cqlType}.
303- * @param codecRegistry The {@link CodecRegistry} instance to use.
304- */
305- static void propagateCodecRegistry(DataType cqlType, CodecRegistry codecRegistry) {
306- // user types
307- if(cqlType instanceof UserType) {
308- UserType userType = (UserType)cqlType;
309- userType.setCodecRegistry(codecRegistry);
310- for (UserType.Field field : userType.byIdx) {
311- propagateCodecRegistry(field.getType(), codecRegistry);
312- }
313- }
314- // tuples
315- else if(cqlType instanceof TupleType) {
316- TupleType tupleType = (TupleType)cqlType;
317- tupleType.setCodecRegistry(codecRegistry);
318- for (DataType componentType : tupleType.getComponentTypes()) {
319- propagateCodecRegistry(componentType, codecRegistry);
320- }
321- }
322- // collections
323- else if(cqlType.isCollection()) {
324- for (DataType inner : cqlType.getTypeArguments()) {
325- propagateCodecRegistry(inner, codecRegistry);
326- }
327- }
328- }
329-
330282 /**
331283 * A column definition.
332284 */
modified driver-core/src/main/java/com/datastax/driver/core/Connection.java +9 −2
@@ -135,7 +135,8 @@
135135 bootstrap.handler(
136136 new Initializer(this, protocolVersion, protocolOptions.getCompression().compressor(), protocolOptions.getSSLOptions(),
137137 factory.configuration.getPoolingOptions().getHeartbeatIntervalSeconds(),
138- factory.configuration.getNettyOptions()));
138+ factory.configuration.getNettyOptions(),
139+ factory.configuration.getCodecRegistry()));
139140
140141 ChannelFuture future = bootstrap.connect(address);
141142
@@ -1282,18 +1283,24 @@
12821283 private final SSLOptions sslOptions;
12831284 private final NettyOptions nettyOptions;
12841285 private final ChannelHandler idleStateHandler;
1286+ private final CodecRegistry codecRegistry;
12851287
1286- public Initializer(Connection connection, ProtocolVersion protocolVersion, FrameCompressor compressor, SSLOptions sslOptions, int heartBeatIntervalSeconds, NettyOptions nettyOptions) {
1288+ public Initializer(Connection connection, ProtocolVersion protocolVersion, FrameCompressor compressor, SSLOptions sslOptions, int heartBeatIntervalSeconds, NettyOptions nettyOptions, CodecRegistry codecRegistry) {
12871289 this.connection = connection;
12881290 this.protocolVersion = protocolVersion;
12891291 this.compressor = compressor;
12901292 this.sslOptions = sslOptions;
12911293 this.nettyOptions = nettyOptions;
1294+ this.codecRegistry = codecRegistry;
12921295 this.idleStateHandler = new IdleStateHandler(0, 0, heartBeatIntervalSeconds);
12931296 }
12941297
12951298 @Override
12961299 protected void initChannel(SocketChannel channel) throws Exception {
1300+
1301+ // set the codec registry so that it can be accessed by ProtocolDecoder
1302+ channel.attr(Message.CODEC_REGISTRY_ATTRIBUTE_KEY).set(codecRegistry);
1303+
12971304 ChannelPipeline pipeline = channel.pipeline();
12981305
12991306 if (sslOptions != null) {
modified driver-core/src/main/java/com/datastax/driver/core/DefaultPreparedStatement.java +0 −2
@@ -56,8 +56,6 @@
5656
5757 ColumnDefinitions defs = msg.metadata.columns;
5858
59- defs.setCodecRegistry(cluster.getConfiguration().getCodecRegistry());
60-
6159 ProtocolVersion protocolVersion = cluster.getConfiguration().getProtocolOptions().getProtocolVersion();
6260
6361 if (defs.size() == 0) {
modified driver-core/src/main/java/com/datastax/driver/core/Message.java +7 −2
@@ -23,6 +23,7 @@
2323 import io.netty.channel.ChannelHandlerContext;
2424 import io.netty.handler.codec.MessageToMessageDecoder;
2525 import io.netty.handler.codec.MessageToMessageEncoder;
26+import io.netty.util.AttributeKey;
2627 import org.slf4j.Logger;
2728 import org.slf4j.LoggerFactory;
2829
@@ -36,13 +37,15 @@
3637
3738 protected static final Logger logger = LoggerFactory.getLogger(Message.class);
3839
40+ static AttributeKey<CodecRegistry> CODEC_REGISTRY_ATTRIBUTE_KEY = AttributeKey.newInstance("com.datastax.driver.core.CodecRegistry");
41+
3942 public interface Coder<R extends Request> {
4043 public void encode(R request, ByteBuf dest, ProtocolVersion version);
4144 public int encodedSize(R request, ProtocolVersion version);
4245 }
4346
4447 public interface Decoder<R extends Response> {
45- public R decode(ByteBuf body, ProtocolVersion version);
48+ public R decode(ByteBuf body, ProtocolVersion version, CodecRegistry codecRegistry);
4649 }
4750
4851 private volatile int streamId;
@@ -240,7 +243,9 @@
240243 List<String> warnings = hasWarnings ? CBUtil.readStringList(frame.body) : Collections.<String>emptyList();
241244
242245 try {
243- Response response = Response.Type.fromOpcode(frame.header.opcode).decoder.decode(frame.body, frame.header.version);
246+ CodecRegistry codecRegistry = ctx.channel().attr(CODEC_REGISTRY_ATTRIBUTE_KEY).get();
247+ assert codecRegistry != null;
248+ Response response = Response.Type.fromOpcode(frame.header.opcode).decoder.decode(frame.body, frame.header.version, codecRegistry);
244249 response
245250 .setTracingId(tracingId)
246251 .setWarnings(warnings)
modified driver-core/src/main/java/com/datastax/driver/core/Responses.java +34 −29
@@ -39,7 +39,7 @@
3939
4040 public static final Message.Decoder<Error> decoder = new Message.Decoder<Error>() {
4141 @Override
42- public Error decode(ByteBuf body, ProtocolVersion version) {
42+ public Error decode(ByteBuf body, ProtocolVersion version, CodecRegistry codecRegistry) {
4343 ExceptionCode code = ExceptionCode.fromValue(body.readInt());
4444 String msg = CBUtil.readString(body);
4545 Object infos = null;
@@ -138,7 +138,7 @@
138138 public static class Ready extends Message.Response {
139139
140140 public static final Message.Decoder<Ready> decoder = new Message.Decoder<Ready>() {
141- public Ready decode(ByteBuf body, ProtocolVersion version) {
141+ public Ready decode(ByteBuf body, ProtocolVersion version, CodecRegistry codecRegistry) {
142142 // TODO: Would it be cool to return a singleton? Check we don't need to
143143 // set the streamId or something
144144 return new Ready();
@@ -158,7 +158,7 @@
158158 public static class Authenticate extends Message.Response {
159159
160160 public static final Message.Decoder<Authenticate> decoder = new Message.Decoder<Authenticate>() {
161- public Authenticate decode(ByteBuf body, ProtocolVersion version) {
161+ public Authenticate decode(ByteBuf body, ProtocolVersion version, CodecRegistry codecRegistry) {
162162 String authenticator = CBUtil.readString(body);
163163 return new Authenticate(authenticator);
164164 }
@@ -180,7 +180,7 @@
180180 public static class Supported extends Message.Response {
181181
182182 public static final Message.Decoder<Supported> decoder = new Message.Decoder<Supported>() {
183- public Supported decode(ByteBuf body, ProtocolVersion version) {
183+ public Supported decode(ByteBuf body, ProtocolVersion version, CodecRegistry codecRegistry) {
184184 return new Supported(CBUtil.readStringToStringListMap(body));
185185 }
186186 };
@@ -216,9 +216,9 @@
216216 public static abstract class Result extends Message.Response {
217217
218218 public static final Message.Decoder<Result> decoder = new Message.Decoder<Result>() {
219- public Result decode(ByteBuf body, ProtocolVersion version) {
219+ public Result decode(ByteBuf body, ProtocolVersion version, CodecRegistry codecRegistry) {
220220 Kind kind = Kind.fromId(body.readInt());
221- return kind.subDecoder.decode(body, version);
221+ return kind.subDecoder.decode(body, version, codecRegistry);
222222 }
223223 };
224224
@@ -273,7 +273,7 @@
273273 }
274274
275275 public static final Message.Decoder<Result> subcodec = new Message.Decoder<Result>() {
276- public Result decode(ByteBuf body, ProtocolVersion version) {
276+ public Result decode(ByteBuf body, ProtocolVersion version, CodecRegistry codecRegistry) {
277277 return new Void();
278278 }
279279 };
@@ -293,7 +293,7 @@
293293 }
294294
295295 public static final Message.Decoder<Result> subcodec = new Message.Decoder<Result>() {
296- public Result decode(ByteBuf body, ProtocolVersion version) {
296+ public Result decode(ByteBuf body, ProtocolVersion version, CodecRegistry codecRegistry) {
297297 return new SetKeyspace(CBUtil.readString(body));
298298 }
299299 };
@@ -347,11 +347,11 @@
347347 this.pkIndices = pkIndices;
348348 }
349349
350- public static Metadata decode(ByteBuf body, ProtocolVersion protocolVersion) {
351- return decode(body, false, protocolVersion);
350+ public static Metadata decode(ByteBuf body, ProtocolVersion protocolVersion, CodecRegistry codecRegistry) {
351+ return decode(body, false, protocolVersion, codecRegistry);
352352 }
353353
354- public static Metadata decode(ByteBuf body, boolean withPkIndices, ProtocolVersion protocolVersion) {
354+ public static Metadata decode(ByteBuf body, boolean withPkIndices, ProtocolVersion protocolVersion, CodecRegistry codecRegistry) {
355355
356356 // flags & column count
357357 EnumSet<Flag> flags = Flag.deserialize(body.readInt());
@@ -387,13 +387,11 @@
387387 String ksName = globalTablesSpec ? globalKsName : CBUtil.readString(body);
388388 String cfName = globalTablesSpec ? globalCfName : CBUtil.readString(body);
389389 String name = CBUtil.readString(body);
390- // will set the appropriate CodecRegistry instance later
391- // when the message will be decoded into either a PreparedStatement or a ResultSet
392- DataType type = DataType.decode(body, protocolVersion, CodecRegistry.DEFAULT_INSTANCE);
390+ DataType type = DataType.decode(body, protocolVersion, codecRegistry);
393391 defs[i] = new ColumnDefinitions.Definition(ksName, cfName, name, type);
394392 }
395393
396- return new Metadata(columnCount, new ColumnDefinitions(defs), state, pkIndices);
394+ return new Metadata(columnCount, new ColumnDefinitions(defs, codecRegistry), state, pkIndices);
397395 }
398396
399397 @Override
@@ -415,9 +413,9 @@
415413 }
416414
417415 public static final Message.Decoder<Result> subcodec = new Message.Decoder<Result>() {
418- public Result decode(ByteBuf body, ProtocolVersion version) {
416+ public Result decode(ByteBuf body, ProtocolVersion version, CodecRegistry codecRegistry) {
419417
420- Metadata metadata = Metadata.decode(body, version);
418+ Metadata metadata = Metadata.decode(body, version, codecRegistry);
421419
422420 int rowCount = body.readInt();
423421 int columnCount = metadata.columnCount;
@@ -457,11 +455,18 @@
457455 } else {
458456 sb.append(" | ");
459457 if (metadata.columns != null) {
460- sb.append(metadata.columns.getType(i));
458+ DataType dataType = metadata.columns.getType(i);
459+ sb.append(dataType);
461460 sb.append(" ");
461+ TypeCodec<Object> codec = metadata.columns.codecRegistry.codecFor(dataType);
462+ Object o = codec.deserialize(v, version);
463+ String s = codec.format(o);
464+ if (s.length() > 100)
465+ s = s.substring(0, 100) + "...";
466+ sb.append(s);
467+ } else {
468+ sb.append(Bytes.toHexString(v));
462469 }
463- // cannot deserialize without the right CodecRegistry instance
464- sb.append(Bytes.toHexString(v));
465470 }
466471 }
467472 sb.append('\n');
@@ -474,22 +479,22 @@
474479 public static class Prepared extends Result {
475480
476481 public static final Message.Decoder<Result> subcodec = new Message.Decoder<Result>() {
477- public Result decode(ByteBuf body, ProtocolVersion version) {
482+ public Result decode(ByteBuf body, ProtocolVersion version, CodecRegistry codecRegistry) {
478483 MD5Digest id = MD5Digest.wrap(CBUtil.readBytes(body));
479484 boolean withPkIndices = version.compareTo(V4) >= 0;
480- Rows.Metadata metadata = Rows.Metadata.decode(body, withPkIndices, version);
481- Rows.Metadata resultMetadata = decodeResultMetadata(body, version);
485+ Rows.Metadata metadata = Rows.Metadata.decode(body, withPkIndices, version, codecRegistry);
486+ Rows.Metadata resultMetadata = decodeResultMetadata(body, version, codecRegistry);
482487 return new Prepared(id, metadata, resultMetadata);
483488 }
484489
485- private Metadata decodeResultMetadata(ByteBuf body, ProtocolVersion version) {
490+ private Metadata decodeResultMetadata(ByteBuf body, ProtocolVersion version, CodecRegistry codecRegistry) {
486491 switch (version) {
487492 case V1:
488493 return Rows.Metadata.EMPTY;
489494 case V2:
490495 case V3:
491496 case V4:
492- return Rows.Metadata.decode(body, version);
497+ return Rows.Metadata.decode(body, version, codecRegistry);
493498 default:
494499 throw version.unsupported();
495500 }
@@ -524,7 +529,7 @@
524529 public final List<String> targetSignature;
525530
526531 public static final Message.Decoder<Result> subcodec = new Message.Decoder<Result>() {
527- public Result decode(ByteBuf body, ProtocolVersion version)
532+ public Result decode(ByteBuf body, ProtocolVersion version, CodecRegistry codecRegistry)
528533 {
529534 // Note: the CREATE KEYSPACE/TABLE/TYPE SCHEMA_CHANGE response is different from the SCHEMA_CHANGE EVENT type
530535 Change change;
@@ -575,7 +580,7 @@
575580 public static class Event extends Message.Response {
576581
577582 public static final Message.Decoder<Event> decoder = new Message.Decoder<Event>() {
578- public Event decode(ByteBuf body, ProtocolVersion version) {
583+ public Event decode(ByteBuf body, ProtocolVersion version, CodecRegistry codecRegistry) {
579584 return new Event(ProtocolEvent.deserialize(body, version));
580585 }
581586 };
@@ -596,7 +601,7 @@
596601 public static class AuthChallenge extends Message.Response {
597602
598603 public static final Message.Decoder<AuthChallenge> decoder = new Message.Decoder<AuthChallenge>() {
599- public AuthChallenge decode(ByteBuf body, ProtocolVersion version) {
604+ public AuthChallenge decode(ByteBuf body, ProtocolVersion version, CodecRegistry codecRegistry) {
600605 ByteBuffer b = CBUtil.readValue(body);
601606 if (b == null)
602607 return new AuthChallenge(null);
@@ -618,7 +623,7 @@
618623 public static class AuthSuccess extends Message.Response {
619624
620625 public static final Message.Decoder<AuthSuccess> decoder = new Message.Decoder<AuthSuccess>() {
621- public AuthSuccess decode(ByteBuf body, ProtocolVersion version) {
626+ public AuthSuccess decode(ByteBuf body, ProtocolVersion version, CodecRegistry codecRegistry) {
622627 ByteBuffer b = CBUtil.readValue(body);
623628 if (b == null)
624629 return new AuthSuccess(null);
modified driver-core/src/test/java/com/datastax/driver/core/ColumnDefinitionsTest.java +3 −3
@@ -29,7 +29,7 @@
2929 new ColumnDefinitions.Definition("ks", "cf", "aColumn", DataType.text()),
3030 new ColumnDefinitions.Definition("ks", "cf", "fOO", DataType.text()),
3131 new ColumnDefinitions.Definition("ks", "cf", "anotherColumn", DataType.text())
32- });
32+ }, CodecRegistry.DEFAULT_INSTANCE);
3333
3434 assertTrue(defs.contains("foo"));
3535 assertTrue(defs.contains("fOO"));
@@ -43,7 +43,7 @@
4343 new ColumnDefinitions.Definition("ks", "cf", "with \" quote", DataType.text()),
4444 new ColumnDefinitions.Definition("ks", "cf", "\"in quote\"", DataType.text()),
4545 new ColumnDefinitions.Definition("ks", "cf", "in quote", DataType.cint()),
46- });
46+ }, CodecRegistry.DEFAULT_INSTANCE);
4747
4848 assertTrue(defs.getType("foo").equals(DataType.text()));
4949 assertTrue(defs.getType("Foo").equals(DataType.text()));
@@ -64,7 +64,7 @@
6464 new ColumnDefinitions.Definition("ks", "cf1", "column", DataType.text()),
6565 new ColumnDefinitions.Definition("ks", "cf2", "column", DataType.cint()),
6666 new ColumnDefinitions.Definition("ks", "cf3", "column", DataType.cfloat())
67- });
67+ }, CodecRegistry.DEFAULT_INSTANCE);
6868
6969 assertTrue(defs.getType("column").equals(DataType.text()));
7070 }
modified driver-core/src/test/java/com/datastax/driver/core/IndexMetadataTest.java +2 −5
@@ -51,7 +51,7 @@
5151 definition(INDEX_TYPE, text()),
5252 definition(VALIDATOR, text()),
5353 definition(INDEX_OPTIONS, text())
54- });
54+ }, CodecRegistry.DEFAULT_INSTANCE);
5555
5656 /**
5757 * Column definitions for indexes table (post-3.0 layout).
@@ -60,7 +60,7 @@
6060 definition(IndexMetadata.NAME, text()),
6161 definition(IndexMetadata.KIND, text()),
6262 definition(IndexMetadata.OPTIONS, map(text(), text()))
63- });
63+ }, CodecRegistry.DEFAULT_INSTANCE);
6464
6565 private static final TypeCodec<Map<String, String>> MAP_CODEC = TypeCodec.map(TypeCodec.varchar(), TypeCodec.varchar());
6666
@@ -70,9 +70,6 @@
7070 @BeforeClass(groups = { "short", "long" })
7171 public void beforeClass() {
7272 super.beforeClass();
73- CodecRegistry codecRegistry = cluster.getConfiguration().getCodecRegistry();
74- legacyColumnDefs.setCodecRegistry(codecRegistry);
75- indexColumnDefs.setCodecRegistry(codecRegistry);
7673 protocolVersion = cluster.getConfiguration().getProtocolOptions().getProtocolVersion();
7774 }
7875