├── .gitignore ├── .travis.yml ├── CONTRIBUTING.md ├── LICENSE ├── README.md ├── ci └── install-jdk.sh ├── pom.xml ├── pre-commit.sh └── src ├── main ├── java │ └── com │ │ └── datastax │ │ ├── dse │ │ └── protocol │ │ │ └── internal │ │ │ ├── DseProtocolConstants.java │ │ │ ├── DseProtocolV1ClientCodecs.java │ │ │ ├── DseProtocolV1ServerCodecs.java │ │ │ ├── DseProtocolV2ClientCodecs.java │ │ │ ├── DseProtocolV2ServerCodecs.java │ │ │ ├── ProtocolV4ClientCodecsForDse.java │ │ │ ├── request │ │ │ ├── DseBatchCodec.java │ │ │ ├── DseExecuteCodec.java │ │ │ ├── DsePrepareCodec.java │ │ │ ├── DseQueryCodec.java │ │ │ ├── DseQueryCodecV4.java │ │ │ ├── RawBytesQuery.java │ │ │ ├── Revise.java │ │ │ └── query │ │ │ │ ├── ContinuousPagingOptions.java │ │ │ │ ├── DseQueryOptions.java │ │ │ │ └── DseQueryOptionsCodec.java │ │ │ └── response │ │ │ └── result │ │ │ ├── DsePreparedSubCodec.java │ │ │ ├── DseRowsMetadata.java │ │ │ └── DseRowsSubCodec.java │ │ └── oss │ │ └── protocol │ │ └── internal │ │ ├── Compressor.java │ │ ├── CrcMismatchException.java │ │ ├── Frame.java │ │ ├── FrameCodec.java │ │ ├── Message.java │ │ ├── NoopCompressor.java │ │ ├── PrimitiveCodec.java │ │ ├── PrimitiveSizes.java │ │ ├── ProtocolConstants.java │ │ ├── ProtocolErrors.java │ │ ├── ProtocolV3ClientCodecs.java │ │ ├── ProtocolV3ServerCodecs.java │ │ ├── ProtocolV4ClientCodecs.java │ │ ├── ProtocolV4ServerCodecs.java │ │ ├── ProtocolV5ClientCodecs.java │ │ ├── ProtocolV5ServerCodecs.java │ │ ├── ProtocolV6ClientCodecs.java │ │ ├── ProtocolV6ServerCodecs.java │ │ ├── Segment.java │ │ ├── SegmentBuilder.java │ │ ├── SegmentCodec.java │ │ ├── request │ │ ├── AuthResponse.java │ │ ├── Batch.java │ │ ├── Execute.java │ │ ├── Options.java │ │ ├── Prepare.java │ │ ├── Query.java │ │ ├── Register.java │ │ ├── Startup.java │ │ └── query │ │ │ ├── QueryOptions.java │ │ │ └── Values.java │ │ ├── response │ │ ├── AuthChallenge.java │ │ ├── AuthSuccess.java │ │ ├── Authenticate.java │ │ ├── Error.java │ │ ├── Event.java │ │ ├── Ready.java │ │ ├── Result.java │ │ ├── Supported.java │ │ ├── error │ │ │ ├── AlreadyExists.java │ │ │ ├── CASWriteUnknown.java │ │ │ ├── FunctionFailure.java │ │ │ ├── ReadFailure.java │ │ │ ├── ReadTimeout.java │ │ │ ├── Unavailable.java │ │ │ ├── Unprepared.java │ │ │ ├── WriteFailure.java │ │ │ └── WriteTimeout.java │ │ ├── event │ │ │ ├── SchemaChangeEvent.java │ │ │ ├── StatusChangeEvent.java │ │ │ └── TopologyChangeEvent.java │ │ └── result │ │ │ ├── ColumnSpec.java │ │ │ ├── DefaultRows.java │ │ │ ├── Prepared.java │ │ │ ├── RawType.java │ │ │ ├── Rows.java │ │ │ ├── RowsMetadata.java │ │ │ ├── SchemaChange.java │ │ │ ├── SetKeyspace.java │ │ │ └── Void.java │ │ └── util │ │ ├── Bytes.java │ │ ├── Crc.java │ │ ├── Flags.java │ │ ├── IntIntMap.java │ │ ├── IntMap.java │ │ └── collection │ │ ├── NullAllowingImmutableList.java │ │ ├── NullAllowingImmutableMap.java │ │ └── NullAllowingImmutableSet.java └── resources │ ├── dse_protocol_v1.spec │ ├── dse_protocol_v2.spec │ ├── native_protocol_v3.spec │ ├── native_protocol_v4.spec │ ├── native_protocol_v5.spec │ └── native_protocol_v6.spec └── test └── java └── com └── datastax ├── dse └── protocol │ └── internal │ ├── DseTestDataProviders.java │ ├── request │ ├── DseBatchTest.java │ ├── DseExecuteTest.java │ ├── DsePrepareTest.java │ ├── DseQueryTest.java │ ├── RawBytesQueryTest.java │ ├── ReviseTest.java │ └── query │ │ └── DseQueryOptionsBuilder.java │ └── response │ ├── DseErrorTest.java │ └── result │ ├── DsePreparedTest.java │ └── DseRowsTest.java └── oss └── protocol └── internal ├── Assertions.java ├── FrameCodecTestBase.java ├── MessageTestBase.java ├── PrimitiveCodecTest.java ├── PrimitiveSizesTest.java ├── RequestFrameCodecTest.java ├── ResponseFrameCodecTest.java ├── RowsAssert.java ├── RowsMetadataAssert.java ├── SegmentBuilderTest.java ├── SegmentCodecHeaderTest.java ├── SegmentCodecTest.java ├── TestDataProviders.java ├── binary ├── MockBinaryString.java ├── MockCompressor.java └── MockPrimitiveCodec.java ├── request ├── AuthResponseTest.java ├── BatchTest.java ├── ExecuteTest.java ├── OptionsTest.java ├── PrepareTest.java ├── QueryTest.java ├── RegisterTest.java ├── StartupTest.java └── query │ ├── QueryOptionsBuilder.java │ └── QueryOptionsBuilderBase.java ├── response ├── AuthChallengeTest.java ├── AuthSuccessTest.java ├── AuthenticateTest.java ├── ErrorTest.java ├── ReadyTest.java ├── SupportedTest.java ├── event │ ├── SchemaChangeEventTest.java │ ├── StatusChangeEventTest.java │ └── TopologyChangeEventTest.java └── result │ ├── PreparedTest.java │ ├── RowsMetadataTest.java │ ├── RowsTest.java │ ├── SchemaChangeTest.java │ ├── SetKeyspaceTest.java │ └── VoidTest.java └── util ├── IntIntMapTest.java ├── IntMapTest.java ├── SerializationHelper.java └── collection ├── NullAllowingImmutableListTest.java ├── NullAllowingImmutableMapTest.java └── NullAllowingImmutableSetTest.java /.gitignore: -------------------------------------------------------------------------------- 1 | target/ 2 | .settings 3 | .classpath 4 | .project 5 | .DS_Store 6 | 7 | /.idea 8 | *.iml 9 | 10 | .java-version 11 | 12 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: java 2 | sudo: false 3 | # see https://sormuras.github.io/blog/2018-03-20-jdk-matrix.html 4 | matrix: 5 | include: 6 | # 8 7 | - env: JDK='OpenJDK 8' 8 | jdk: openjdk8 9 | # 11 10 | - env: JDK='OpenJDK 11' 11 | install: . $TRAVIS_BUILD_DIR/ci/install-jdk.sh -F 11 -L GPL 12 | script: mvn test -B -Dmaven.main.skip=true -Dmaven.test.skip=true 13 | cache: 14 | directories: 15 | - $HOME/.m2 16 | -------------------------------------------------------------------------------- /CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | # Contributing guidelines 2 | 3 | ## Design goals 4 | 5 | * no runtime dependencies 6 | * extensibility: it is possible to add new message codecs, new subcodecs for result kinds, errors 7 | and even a whole new protocol version 8 | 9 | ## Code formatting 10 | 11 | We follow the [Google Java Style Guide](https://google.github.io/styleguide/javaguide.html). See 12 | https://github.com/google/google-java-format for IDE plugins. The rules are not configurable. 13 | 14 | The build will fail if the code is not formatted. To format all files from the command line, run: 15 | 16 | ``` 17 | mvn fmt:format 18 | ``` 19 | 20 | Some aspects are not covered by the formatter: 21 | 22 | * braces must be used with `if`, `else`, `for`, `do` and `while` statements, even when the body is 23 | empty or contains only a single statement. 24 | * XML files: indent with two spaces and wrap to respect the column limit of 100 characters. 25 | 26 | Also, if your IDE sorts import statements automatically, make sure it follows the same order as the 27 | formatter: all static imports in ASCII sort order, followed by a blank line, followed by all regular 28 | imports in ASCII sort order. 29 | 30 | ## Coding style 31 | 32 | Avoid static imports, with those exceptions: 33 | * `ProtocolConstants.Version` constants (`V3`, `V4`, etc). 34 | * AssertJ's `assertThat` / `fail` in unit tests. 35 | 36 | Tests: 37 | * test methods names use lower snake case, generally start with "should" and clearly indicate the 38 | purpose of the test, for example: `should_fail_if_key_already_exists`. If you have trouble coming 39 | up with a simple name, it might be a sign that your method does too much and should be split. 40 | * we use AssertJ (`assertThat`). Don't use TestNG's assertions (`assertEquals`, `assertNull`, etc). 41 | * don't try to generify at all cost: a bit of duplication is acceptable, if that helps keep the 42 | tests simple to understand (a newcomer should be able to understand how to fix a failing test 43 | without having to read too much code). 44 | * given the simplicity of this project, it's unlikely that we'll ever need integration tests: don't 45 | start any external process from the test, the whole suite should run in a couple of seconds. 46 | 47 | ## License headers 48 | 49 | The build will fail if some license headers are missing. To update all files from the command line, 50 | run: 51 | 52 | ``` 53 | mvn license:format 54 | ``` 55 | 56 | ## Pre-commit hook (highly recommended) 57 | 58 | Ensure `pre-commit.sh` is executable, then run: 59 | 60 | ``` 61 | ln -s ../../pre-commit.sh .git/hooks/pre-commit 62 | ``` 63 | 64 | This will only allow commits if the tests pass. It is also a good reminder to keep the test suite 65 | short. 66 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Native protocol layer compatible with Apache Cassandra® 2 | 3 | A set of Java types representing the frames and messages of the Apache Cassandra® native protocol, 4 | with the associated serialization and deserialization logic (this is a third-party implementation, 5 | not related to the Apache Cassandra project). It is used by [Datastax 6 | Java driver 4](https://github.com/datastax/java-driver/tree/4.x) and 7 | [Simulacron](https://github.com/datastax/simulacron). 8 | 9 | Native protocol versions 3 and above are supported. 10 | 11 | ## Usage 12 | 13 | The code is agnostic about the underlying binary representation: start by implementing a 14 | `PrimitiveCodec` for your target type `B` (which could be `ByteBuffer`, Netty's `ByteBuf`, 15 | `byte[]`, etc.) 16 | 17 | You may also implement a `Compressor` (it can be `Compressor.none()` if you're not going to 18 | compress frames). 19 | 20 | Finally, build a `FrameCodec` that will allow you to encode and decode frames. 21 | `Frame.defaultClient` and `Frame.defaultServer` give you the default sets of codecs for the 22 | protocol versions that are currently supported; alternatively, you can use the constructor 23 | to register an arbitrary set of codecs. 24 | 25 | `Frame`, `Message`, and `Message` subclasses are immutable, but for efficiency they don't make 26 | defensive copies of their fields. If these fields are mutable (for example collections), they 27 | shouldn't be modified after creating a message instance. 28 | 29 | The code makes very few assumptions about how the messages will be used. Data is often represented 30 | in the most simple way. For example, `ProtocolConstants` uses simple constants to represent the 31 | protocol codes; client code can (and probably should) wrap them in more type-safe structures (such 32 | as enums) before exposing them to higher-level layers. 33 | 34 | Well-formed inputs are expected; if you pass an inconsistent `Frame` (ex: protocol v3 with a custom 35 | payload), an `IllegalArgumentException` will be thrown. 36 | 37 | ## Versioning 38 | 39 | We follow [semantic versioning](http://semver.org/). In addition, the minor (second) number matches 40 | the highest *stable* protocol version supported. The next beta version might also be partially 41 | supported. Examples: 42 | * 1.4.0: supports up to protocol v4, and possibly some features of v5 beta. 43 | * 1.4.1: bugfixes and/or new v5 beta features. 44 | * 1.5.0: v5 stable. 45 | 46 | All the types in this project are considered low-level and intended for framework developers, not 47 | end users. We'll try to preserve binary compatibility (in particular for `Frame` and `Message` 48 | implementations), but we reserve the right to introduce breaking changes at any time. 49 | 50 | ## License 51 | 52 | Copyright 2017, DataStax 53 | 54 | Licensed under the Apache License, Version 2.0 (the "License"); 55 | you may not use this file except in compliance with the License. 56 | You may obtain a copy of the License at 57 | 58 | http://www.apache.org/licenses/LICENSE-2.0 59 | 60 | Unless required by applicable law or agreed to in writing, software 61 | distributed under the License is distributed on an "AS IS" BASIS, 62 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 63 | See the License for the specific language governing permissions and 64 | limitations under the License. 65 | 66 | ---- 67 | 68 | DataStax is a registered trademark of DataStax, Inc. and its subsidiaries in the United States 69 | and/or other countries. 70 | 71 | Apache Cassandra, Apache, Tomcat, Lucene, Solr, Hadoop, Spark, TinkerPop, and Cassandra are 72 | trademarks of the [Apache Software Foundation](http://www.apache.org/) or its subsidiaries in 73 | Canada, the United States and/or other countries. 74 | -------------------------------------------------------------------------------- /pre-commit.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | STASH_NAME="pre-commit-$(date +%s)" 4 | git stash save --keep-index $STASH_NAME 5 | 6 | mvn clean test 7 | RESULT=$? 8 | 9 | STASHES=$(git stash list) 10 | if [[ $STASHES == *$STASH_NAME* ]]; then 11 | git stash pop 12 | fi 13 | 14 | [ $RESULT -ne 0 ] && exit 1 15 | exit 0 16 | -------------------------------------------------------------------------------- /src/main/java/com/datastax/dse/protocol/internal/DseProtocolConstants.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 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.dse.protocol.internal; 17 | 18 | import com.datastax.oss.protocol.internal.ProtocolConstants; 19 | 20 | /** See {@link ProtocolConstants}, this only lists the additional DSE ones. */ 21 | public class DseProtocolConstants { 22 | 23 | public static class Version { 24 | public static final int DSE_V1 = 65; 25 | public static final int DSE_V2 = 66; 26 | public static final int MIN = DSE_V1; 27 | public static final int MAX = DSE_V2; 28 | /** If no beta version is currently supported, this will be negative. */ 29 | public static final int BETA = -1; 30 | } 31 | 32 | public static class Opcode { 33 | public static final int REVISE_REQUEST = 0xff; 34 | } 35 | 36 | public static class QueryFlag { 37 | public static final int PAGE_SIZE_BYTES = 0x40000000; 38 | public static final int CONTINUOUS_PAGING = 0x80000000; 39 | } 40 | 41 | public static class RowsFlag { 42 | public static final int CONTINUOUS_PAGING = 0x40000000; 43 | public static final int LAST_CONTINUOUS_PAGE = 0x80000000; 44 | } 45 | 46 | public static class RevisionType { 47 | public static final int CANCEL_CONTINUOUS_PAGING = 0x00000001; 48 | public static final int MORE_CONTINUOUS_PAGES = 0x00000002; 49 | } 50 | 51 | public static class ErrorCode { 52 | public static final int CLIENT_WRITE_FAILURE = 0x8000; 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /src/main/java/com/datastax/dse/protocol/internal/ProtocolV4ClientCodecsForDse.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 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.dse.protocol.internal; 17 | 18 | import com.datastax.dse.protocol.internal.request.DseQueryCodecV4; 19 | import com.datastax.dse.protocol.internal.request.RawBytesQuery; 20 | import com.datastax.oss.protocol.internal.FrameCodec; 21 | import com.datastax.oss.protocol.internal.ProtocolConstants; 22 | import com.datastax.oss.protocol.internal.request.AuthResponse; 23 | import com.datastax.oss.protocol.internal.request.Batch; 24 | import com.datastax.oss.protocol.internal.request.Execute; 25 | import com.datastax.oss.protocol.internal.request.Options; 26 | import com.datastax.oss.protocol.internal.request.Prepare; 27 | import com.datastax.oss.protocol.internal.request.Register; 28 | import com.datastax.oss.protocol.internal.request.Startup; 29 | import com.datastax.oss.protocol.internal.response.AuthChallenge; 30 | import com.datastax.oss.protocol.internal.response.AuthSuccess; 31 | import com.datastax.oss.protocol.internal.response.Authenticate; 32 | import com.datastax.oss.protocol.internal.response.Error; 33 | import com.datastax.oss.protocol.internal.response.Event; 34 | import com.datastax.oss.protocol.internal.response.Ready; 35 | import com.datastax.oss.protocol.internal.response.Result; 36 | import com.datastax.oss.protocol.internal.response.Supported; 37 | 38 | /** 39 | * DSE 5.0 still uses an OSS protocol version (V4), but we need to support {@link RawBytesQuery} for 40 | * fluent graph statements. 41 | * 42 | * @see DseQueryCodecV4 43 | */ 44 | public class ProtocolV4ClientCodecsForDse implements FrameCodec.CodecGroup { 45 | @Override 46 | public void registerCodecs(Registry registry) { 47 | registry 48 | .addEncoder(new AuthResponse.Codec(ProtocolConstants.Version.V4)) 49 | .addEncoder(new Batch.Codec(ProtocolConstants.Version.V4)) 50 | .addEncoder(new Execute.Codec(ProtocolConstants.Version.V4)) 51 | .addEncoder(new Options.Codec(ProtocolConstants.Version.V4)) 52 | .addEncoder(new Prepare.Codec(ProtocolConstants.Version.V4)) 53 | .addEncoder(new DseQueryCodecV4(ProtocolConstants.Version.V4)) 54 | .addEncoder(new Register.Codec(ProtocolConstants.Version.V4)) 55 | .addEncoder(new Startup.Codec(ProtocolConstants.Version.V4)); 56 | 57 | registry 58 | .addDecoder(new AuthChallenge.Codec(ProtocolConstants.Version.V4)) 59 | .addDecoder(new Authenticate.Codec(ProtocolConstants.Version.V4)) 60 | .addDecoder(new AuthSuccess.Codec(ProtocolConstants.Version.V4)) 61 | .addDecoder(new Error.Codec(ProtocolConstants.Version.V4)) 62 | .addDecoder(new Event.Codec(ProtocolConstants.Version.V4)) 63 | .addDecoder(new Ready.Codec(ProtocolConstants.Version.V4)) 64 | .addDecoder(new Result.Codec(ProtocolConstants.Version.V4)) 65 | .addDecoder(new Supported.Codec(ProtocolConstants.Version.V4)); 66 | } 67 | } 68 | -------------------------------------------------------------------------------- /src/main/java/com/datastax/dse/protocol/internal/request/DseExecuteCodec.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 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.dse.protocol.internal.request; 17 | 18 | import static com.datastax.dse.protocol.internal.DseProtocolConstants.Version.DSE_V2; 19 | 20 | import com.datastax.dse.protocol.internal.DseProtocolConstants; 21 | import com.datastax.dse.protocol.internal.request.query.DseQueryOptionsCodec; 22 | import com.datastax.oss.protocol.internal.Message; 23 | import com.datastax.oss.protocol.internal.PrimitiveCodec; 24 | import com.datastax.oss.protocol.internal.PrimitiveSizes; 25 | import com.datastax.oss.protocol.internal.ProtocolConstants; 26 | import com.datastax.oss.protocol.internal.request.Execute; 27 | import com.datastax.oss.protocol.internal.request.query.QueryOptions; 28 | 29 | public class DseExecuteCodec extends Message.Codec { 30 | 31 | private final QueryOptions.Codec optionsCodec; 32 | 33 | public DseExecuteCodec(int protocolVersion) { 34 | super(ProtocolConstants.Opcode.EXECUTE, protocolVersion); 35 | assert protocolVersion >= DseProtocolConstants.Version.DSE_V1; 36 | this.optionsCodec = new DseQueryOptionsCodec(protocolVersion); 37 | } 38 | 39 | @Override 40 | public void encode(B dest, Message message, PrimitiveCodec encoder) { 41 | Execute execute = (Execute) message; 42 | encoder.writeShortBytes(execute.queryId, dest); 43 | if (protocolVersion >= DSE_V2) { 44 | encoder.writeShortBytes(execute.resultMetadataId, dest); 45 | } 46 | optionsCodec.encode(dest, execute.options, encoder); 47 | } 48 | 49 | @Override 50 | public int encodedSize(Message message) { 51 | Execute execute = (Execute) message; 52 | int size = PrimitiveSizes.sizeOfShortBytes(execute.queryId); 53 | if (protocolVersion >= DSE_V2) { 54 | assert execute.resultMetadataId != null; 55 | size += PrimitiveSizes.sizeOfShortBytes(execute.resultMetadataId); 56 | } 57 | size += optionsCodec.encodedSize(execute.options); 58 | return size; 59 | } 60 | 61 | @Override 62 | public Message decode(B source, PrimitiveCodec decoder) { 63 | byte[] queryId = decoder.readShortBytes(source); 64 | byte[] resultMetadataId = (protocolVersion >= DSE_V2) ? decoder.readShortBytes(source) : null; 65 | QueryOptions options = optionsCodec.decode(source, decoder); 66 | return new Execute(queryId, resultMetadataId, options); 67 | } 68 | } 69 | -------------------------------------------------------------------------------- /src/main/java/com/datastax/dse/protocol/internal/request/DsePrepareCodec.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 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.dse.protocol.internal.request; 17 | 18 | import static com.datastax.dse.protocol.internal.DseProtocolConstants.Version.DSE_V1; 19 | import static com.datastax.dse.protocol.internal.DseProtocolConstants.Version.DSE_V2; 20 | 21 | import com.datastax.oss.protocol.internal.Message; 22 | import com.datastax.oss.protocol.internal.PrimitiveCodec; 23 | import com.datastax.oss.protocol.internal.PrimitiveSizes; 24 | import com.datastax.oss.protocol.internal.ProtocolConstants; 25 | import com.datastax.oss.protocol.internal.request.Prepare; 26 | 27 | public class DsePrepareCodec extends Message.Codec { 28 | 29 | public DsePrepareCodec(int protocolVersion) { 30 | super(ProtocolConstants.Opcode.PREPARE, protocolVersion); 31 | assert protocolVersion >= DSE_V1; 32 | } 33 | 34 | @Override 35 | public void encode(B dest, Message message, PrimitiveCodec encoder) { 36 | Prepare prepare = (Prepare) message; 37 | encoder.writeLongString(prepare.cqlQuery, dest); 38 | if (protocolVersion >= DSE_V2) { 39 | // There is only one PREPARE flag for now, so hard-code for simplicity: 40 | encoder.writeInt((prepare.keyspace == null) ? 0x00 : 0x01, dest); 41 | if (prepare.keyspace != null) { 42 | encoder.writeString(prepare.keyspace, dest); 43 | } 44 | } 45 | } 46 | 47 | @Override 48 | public int encodedSize(Message message) { 49 | Prepare prepare = (Prepare) message; 50 | int size = PrimitiveSizes.sizeOfLongString(prepare.cqlQuery); 51 | if (protocolVersion >= DSE_V2) { 52 | size += PrimitiveSizes.INT; // flags 53 | if (prepare.keyspace != null) { 54 | size += PrimitiveSizes.sizeOfString(prepare.keyspace); 55 | } 56 | } 57 | return size; 58 | } 59 | 60 | @Override 61 | public Message decode(B source, PrimitiveCodec decoder) { 62 | String cqlQuery = decoder.readLongString(source); 63 | String keyspace = null; 64 | if (protocolVersion >= DSE_V2) { 65 | int flags = decoder.readInt(source); 66 | if ((flags & 0x01) == 0x01) { 67 | keyspace = decoder.readString(source); 68 | } 69 | } 70 | return new Prepare(cqlQuery, keyspace); 71 | } 72 | } 73 | -------------------------------------------------------------------------------- /src/main/java/com/datastax/dse/protocol/internal/request/DseQueryCodec.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 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.dse.protocol.internal.request; 17 | 18 | import static com.datastax.dse.protocol.internal.DseProtocolConstants.Version.DSE_V1; 19 | 20 | import com.datastax.dse.protocol.internal.request.query.DseQueryOptions; 21 | import com.datastax.dse.protocol.internal.request.query.DseQueryOptionsCodec; 22 | import com.datastax.oss.protocol.internal.Message; 23 | import com.datastax.oss.protocol.internal.PrimitiveCodec; 24 | import com.datastax.oss.protocol.internal.PrimitiveSizes; 25 | import com.datastax.oss.protocol.internal.request.Query; 26 | 27 | /** 28 | * {@code QUERY} codec for DSE-specific native protocols (DSE 5.1 and above). 29 | * 30 | *

Similar to the OSS codec, but in addition to regular messages, it can encode {@link 31 | * RawBytesQuery} (for fluent graph statements), and the options can be a {@link DseQueryOptions}. 32 | * 33 | * @see DseQueryCodecV4 34 | */ 35 | public class DseQueryCodec extends Query.Codec { 36 | 37 | public DseQueryCodec(int protocolVersion) { 38 | super(protocolVersion, new DseQueryOptionsCodec(protocolVersion)); 39 | assert protocolVersion >= DSE_V1; 40 | } 41 | 42 | @Override 43 | public void encode(B dest, Message message, PrimitiveCodec encoder) { 44 | if (message instanceof RawBytesQuery) { 45 | RawBytesQuery query = (RawBytesQuery) message; 46 | encoder.writeBytes(query.query, dest); 47 | optionsCodec.encode(dest, query.options, encoder); 48 | } else { 49 | super.encode(dest, message, encoder); 50 | } 51 | } 52 | 53 | @Override 54 | public int encodedSize(Message message) { 55 | if (message instanceof RawBytesQuery) { 56 | RawBytesQuery query = (RawBytesQuery) message; 57 | return PrimitiveSizes.sizeOfBytes(query.query) + optionsCodec.encodedSize(query.options); 58 | } else { 59 | return super.encodedSize(message); 60 | } 61 | } 62 | } 63 | -------------------------------------------------------------------------------- /src/main/java/com/datastax/dse/protocol/internal/request/DseQueryCodecV4.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 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.dse.protocol.internal.request; 17 | 18 | import com.datastax.oss.protocol.internal.Message; 19 | import com.datastax.oss.protocol.internal.PrimitiveCodec; 20 | import com.datastax.oss.protocol.internal.PrimitiveSizes; 21 | import com.datastax.oss.protocol.internal.ProtocolConstants; 22 | import com.datastax.oss.protocol.internal.request.Query; 23 | import com.datastax.oss.protocol.internal.request.query.QueryOptions; 24 | 25 | /** 26 | * {@code QUERY} codec for native protocol V4 (DSE 5.0). 27 | * 28 | *

Similar to the OSS codec, but in addition to regular messages, it can encode {@link 29 | * RawBytesQuery} (for fluent graph statements). However, queries still uses a standard OSS {@link 30 | * QueryOptions}. 31 | * 32 | * @see DseQueryCodec 33 | */ 34 | public class DseQueryCodecV4 extends Query.Codec { 35 | 36 | public DseQueryCodecV4(int protocolVersion) { 37 | super(protocolVersion, new QueryOptions.Codec(protocolVersion)); 38 | assert protocolVersion == ProtocolConstants.Version.V4; 39 | } 40 | 41 | @Override 42 | public void encode(B dest, Message message, PrimitiveCodec encoder) { 43 | if (message instanceof RawBytesQuery) { 44 | RawBytesQuery query = (RawBytesQuery) message; 45 | encoder.writeBytes(query.query, dest); 46 | optionsCodec.encode(dest, query.options, encoder); 47 | } else { 48 | super.encode(dest, message, encoder); 49 | } 50 | } 51 | 52 | @Override 53 | public int encodedSize(Message message) { 54 | if (message instanceof RawBytesQuery) { 55 | RawBytesQuery query = (RawBytesQuery) message; 56 | return PrimitiveSizes.sizeOfBytes(query.query) + optionsCodec.encodedSize(query.options); 57 | } else { 58 | return super.encodedSize(message); 59 | } 60 | } 61 | } 62 | -------------------------------------------------------------------------------- /src/main/java/com/datastax/dse/protocol/internal/request/RawBytesQuery.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 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.dse.protocol.internal.request; 17 | 18 | import com.datastax.dse.protocol.internal.request.query.DseQueryOptions; 19 | import com.datastax.oss.protocol.internal.Message; 20 | import com.datastax.oss.protocol.internal.ProtocolConstants; 21 | import com.datastax.oss.protocol.internal.util.Bytes; 22 | 23 | /** 24 | * A specialized {@code QUERY} message where the query string is represented directly as a byte 25 | * array. 26 | * 27 | *

It is used to avoid materializing a string if the incoming query is already encoded (namely, 28 | * in DSE graph). 29 | */ 30 | public class RawBytesQuery extends Message { 31 | 32 | public final byte[] query; 33 | public final DseQueryOptions options; 34 | 35 | public RawBytesQuery(byte[] query, DseQueryOptions options) { 36 | super(false, ProtocolConstants.Opcode.QUERY); 37 | this.query = query; 38 | this.options = options; 39 | } 40 | 41 | @Override 42 | public String toString() { 43 | return "QUERY (" + Bytes.toHexString(query) + ')'; 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /src/main/java/com/datastax/dse/protocol/internal/request/Revise.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 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.dse.protocol.internal.request; 17 | 18 | import com.datastax.dse.protocol.internal.DseProtocolConstants; 19 | import com.datastax.oss.protocol.internal.Message; 20 | import com.datastax.oss.protocol.internal.PrimitiveCodec; 21 | import com.datastax.oss.protocol.internal.PrimitiveSizes; 22 | 23 | public class Revise extends Message { 24 | 25 | public static Revise cancelContinuousPaging(int streamId) { 26 | return new Revise(DseProtocolConstants.RevisionType.CANCEL_CONTINUOUS_PAGING, streamId, -1); 27 | } 28 | 29 | public static Revise requestMoreContinuousPages(int streamId, int amount) { 30 | return new Revise(DseProtocolConstants.RevisionType.MORE_CONTINUOUS_PAGES, streamId, amount); 31 | } 32 | 33 | /** @see DseProtocolConstants.RevisionType */ 34 | public final int revisionType; 35 | 36 | public final int streamId; 37 | public final int nextPages; 38 | 39 | public Revise(int revisionType, int streamId, int nextPages) { 40 | super(false, DseProtocolConstants.Opcode.REVISE_REQUEST); 41 | this.revisionType = revisionType; 42 | this.streamId = streamId; 43 | this.nextPages = nextPages; 44 | } 45 | 46 | public static class Codec extends Message.Codec { 47 | 48 | public Codec(int protocolVersion) { 49 | super(DseProtocolConstants.Opcode.REVISE_REQUEST, protocolVersion); 50 | } 51 | 52 | @Override 53 | public void encode(B dest, Message message, PrimitiveCodec encoder) { 54 | Revise revise = (Revise) message; 55 | encoder.writeInt(revise.revisionType, dest); 56 | encoder.writeInt(revise.streamId, dest); 57 | if (revise.revisionType == DseProtocolConstants.RevisionType.MORE_CONTINUOUS_PAGES) { 58 | encoder.writeInt(revise.nextPages, dest); 59 | } 60 | } 61 | 62 | @Override 63 | public int encodedSize(Message message) { 64 | Revise revise = (Revise) message; 65 | return PrimitiveSizes.INT 66 | * (revise.revisionType == DseProtocolConstants.RevisionType.MORE_CONTINUOUS_PAGES 67 | ? 3 68 | : 2); 69 | } 70 | 71 | @Override 72 | public Message decode(B source, PrimitiveCodec decoder) { 73 | int revisionType = decoder.readInt(source); 74 | int streamId = decoder.readInt(source); 75 | int nextPages = 76 | (revisionType == DseProtocolConstants.RevisionType.MORE_CONTINUOUS_PAGES) 77 | ? decoder.readInt(source) 78 | : -1; 79 | return new Revise(revisionType, streamId, nextPages); 80 | } 81 | } 82 | } 83 | -------------------------------------------------------------------------------- /src/main/java/com/datastax/dse/protocol/internal/request/query/ContinuousPagingOptions.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 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.dse.protocol.internal.request.query; 17 | 18 | public class ContinuousPagingOptions { 19 | 20 | public final int maxPages; 21 | public final int pagesPerSecond; 22 | public final int nextPages; 23 | 24 | public ContinuousPagingOptions(int maxPages, int pagesPerSecond, int nextPages) { 25 | this.maxPages = maxPages; 26 | this.pagesPerSecond = pagesPerSecond; 27 | this.nextPages = nextPages; 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /src/main/java/com/datastax/dse/protocol/internal/response/result/DsePreparedSubCodec.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 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.dse.protocol.internal.response.result; 17 | 18 | import static com.datastax.dse.protocol.internal.DseProtocolConstants.Version.DSE_V1; 19 | import static com.datastax.dse.protocol.internal.DseProtocolConstants.Version.DSE_V2; 20 | 21 | import com.datastax.oss.protocol.internal.Message; 22 | import com.datastax.oss.protocol.internal.PrimitiveCodec; 23 | import com.datastax.oss.protocol.internal.PrimitiveSizes; 24 | import com.datastax.oss.protocol.internal.ProtocolConstants; 25 | import com.datastax.oss.protocol.internal.response.Result; 26 | import com.datastax.oss.protocol.internal.response.result.Prepared; 27 | import com.datastax.oss.protocol.internal.response.result.RowsMetadata; 28 | 29 | public class DsePreparedSubCodec extends Result.SubCodec { 30 | 31 | public DsePreparedSubCodec(int protocolVersion) { 32 | super(ProtocolConstants.ResultKind.PREPARED, protocolVersion); 33 | assert protocolVersion >= DSE_V1; 34 | } 35 | 36 | @Override 37 | public void encode(B dest, Message message, PrimitiveCodec encoder) { 38 | Prepared prepared = (Prepared) message; 39 | encoder.writeShortBytes(prepared.preparedQueryId, dest); 40 | if (protocolVersion >= DSE_V2) { 41 | encoder.writeShortBytes(prepared.resultMetadataId, dest); 42 | } 43 | prepared.variablesMetadata.encode(dest, encoder, true, protocolVersion); 44 | prepared.resultMetadata.encode(dest, encoder, false, protocolVersion); 45 | } 46 | 47 | @Override 48 | public int encodedSize(Message message) { 49 | Prepared prepared = (Prepared) message; 50 | int size = PrimitiveSizes.sizeOfShortBytes(prepared.preparedQueryId); 51 | if (protocolVersion >= DSE_V2) { 52 | assert prepared.resultMetadataId != null; 53 | size += PrimitiveSizes.sizeOfShortBytes(prepared.resultMetadataId); 54 | } 55 | size += prepared.variablesMetadata.encodedSize(true, protocolVersion); 56 | size += prepared.resultMetadata.encodedSize(false, protocolVersion); 57 | return size; 58 | } 59 | 60 | @Override 61 | public Message decode(B source, PrimitiveCodec decoder) { 62 | byte[] preparedQueryId = decoder.readShortBytes(source); 63 | byte[] resultMetadataId = (protocolVersion >= DSE_V2) ? decoder.readShortBytes(source) : null; 64 | RowsMetadata variablesMetadata = RowsMetadata.decode(source, decoder, true, protocolVersion); 65 | RowsMetadata resultMetadata = RowsMetadata.decode(source, decoder, false, protocolVersion); 66 | return new Prepared(preparedQueryId, resultMetadataId, variablesMetadata, resultMetadata); 67 | } 68 | } 69 | -------------------------------------------------------------------------------- /src/main/java/com/datastax/dse/protocol/internal/response/result/DseRowsSubCodec.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 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.dse.protocol.internal.response.result; 17 | 18 | import com.datastax.oss.protocol.internal.Message; 19 | import com.datastax.oss.protocol.internal.PrimitiveCodec; 20 | import com.datastax.oss.protocol.internal.response.result.DefaultRows; 21 | import java.nio.ByteBuffer; 22 | import java.util.ArrayDeque; 23 | import java.util.ArrayList; 24 | import java.util.List; 25 | import java.util.Queue; 26 | 27 | public class DseRowsSubCodec extends DefaultRows.SubCodec { 28 | 29 | public DseRowsSubCodec(int protocolVersion) { 30 | super(protocolVersion); 31 | } 32 | 33 | // No need to override `encode` and `encodedSize`, if the metadata is a DseRowsMetadata it knows 34 | // how to encode itself. 35 | 36 | @Override 37 | public Message decode(B source, PrimitiveCodec decoder) { 38 | DseRowsMetadata metadata = DseRowsMetadata.decode(source, decoder, false, protocolVersion); 39 | int rowCount = decoder.readInt(source); 40 | 41 | Queue> data = new ArrayDeque<>(rowCount); 42 | for (int i = 0; i < rowCount; i++) { 43 | List row = new ArrayList<>(metadata.columnCount); 44 | for (int j = 0; j < metadata.columnCount; j++) { 45 | row.add(decoder.readBytes(source)); 46 | } 47 | data.add(row); 48 | } 49 | 50 | return new DefaultRows(metadata, data); 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /src/main/java/com/datastax/oss/protocol/internal/Compressor.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 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.oss.protocol.internal; 17 | 18 | /** @param the binary representation to compress. */ 19 | public interface Compressor { 20 | static Compressor none() { 21 | return new NoopCompressor<>(); 22 | } 23 | 24 | /** 25 | * The name of the algorithm used. 26 | * 27 | *

It's the string that will be used in the {@code STARTUP} message. Null or empty means no 28 | * compression. 29 | */ 30 | String algorithm(); 31 | 32 | /** 33 | * Compresses a payload using the "legacy" format of protocol v4- frame bodies. 34 | * 35 | *

The resulting payload encodes the uncompressed length, and is therefore self-sufficient for 36 | * decompression. 37 | */ 38 | B compress(B uncompressed); 39 | 40 | /** Decompresses a payload that was compressed with {@link #compress(Object)}. */ 41 | B decompress(B compressed); 42 | 43 | /** 44 | * Compresses a payload using the "modern" format of protocol v5+ segments. 45 | * 46 | *

The resulting payload does not encode the uncompressed length. It must be stored separately, 47 | * and provided to the decompression method. 48 | */ 49 | B compressWithoutLength(B uncompressed); 50 | 51 | /** Decompresses a payload that was compressed with {@link #compressWithoutLength(Object)}. */ 52 | B decompressWithoutLength(B compressed, int uncompressedLength); 53 | } 54 | -------------------------------------------------------------------------------- /src/main/java/com/datastax/oss/protocol/internal/CrcMismatchException.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 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.oss.protocol.internal; 17 | 18 | /** Thrown when the checksums in an incoming {@link Segment} don't match (protocol v5 or above). */ 19 | public class CrcMismatchException extends Exception { 20 | 21 | private static final long serialVersionUID = 0; 22 | 23 | public CrcMismatchException(String message) { 24 | super(message); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /src/main/java/com/datastax/oss/protocol/internal/Message.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 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.oss.protocol.internal; 17 | 18 | public abstract class Message { 19 | public final boolean isResponse; 20 | 21 | /** @see ProtocolConstants.Opcode */ 22 | public final int opcode; 23 | 24 | protected Message(boolean isResponse, int opcode) { 25 | this.isResponse = isResponse; 26 | this.opcode = opcode; 27 | } 28 | 29 | public abstract static class Codec { 30 | /** @see ProtocolConstants.Opcode */ 31 | public final int opcode; 32 | /** @see ProtocolConstants.Version */ 33 | public final int protocolVersion; 34 | 35 | protected Codec(int opcode, int protocolVersion) { 36 | this.opcode = opcode; 37 | this.protocolVersion = protocolVersion; 38 | } 39 | 40 | public abstract void encode(B dest, Message message, PrimitiveCodec encoder); 41 | 42 | public abstract int encodedSize(Message message); 43 | 44 | public abstract Message decode(B source, PrimitiveCodec decoder); 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /src/main/java/com/datastax/oss/protocol/internal/NoopCompressor.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 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.oss.protocol.internal; 17 | 18 | public class NoopCompressor implements Compressor { 19 | 20 | @Override 21 | public String algorithm() { 22 | return null; 23 | } 24 | 25 | @Override 26 | public B compress(B uncompressed) { 27 | return uncompressed; 28 | } 29 | 30 | @Override 31 | public B decompress(B compressed) { 32 | return compressed; 33 | } 34 | 35 | @Override 36 | public B compressWithoutLength(B uncompressed) { 37 | return uncompressed; 38 | } 39 | 40 | @Override 41 | public B decompressWithoutLength(B compressed, int uncompressedLength) { 42 | return compressed; 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /src/main/java/com/datastax/oss/protocol/internal/ProtocolErrors.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 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.oss.protocol.internal; 17 | 18 | /** 19 | * Errors thrown when a message to (de)serialize doesn't respect the protocol spec. 20 | * 21 | *

Clients are supposed to passed well-formed messages, so these are IllegalArgumentExceptions 22 | * indicating a programming error. 23 | */ 24 | public class ProtocolErrors { 25 | public static void check(boolean condition, String errorWhenFalse, Object... arguments) { 26 | if (!condition) { 27 | throw new IllegalArgumentException(String.format(errorWhenFalse, arguments)); 28 | } 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /src/main/java/com/datastax/oss/protocol/internal/ProtocolV3ClientCodecs.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 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.oss.protocol.internal; 17 | 18 | import com.datastax.oss.protocol.internal.request.AuthResponse; 19 | import com.datastax.oss.protocol.internal.request.Batch; 20 | import com.datastax.oss.protocol.internal.request.Execute; 21 | import com.datastax.oss.protocol.internal.request.Options; 22 | import com.datastax.oss.protocol.internal.request.Prepare; 23 | import com.datastax.oss.protocol.internal.request.Query; 24 | import com.datastax.oss.protocol.internal.request.Register; 25 | import com.datastax.oss.protocol.internal.request.Startup; 26 | import com.datastax.oss.protocol.internal.response.AuthChallenge; 27 | import com.datastax.oss.protocol.internal.response.AuthSuccess; 28 | import com.datastax.oss.protocol.internal.response.Authenticate; 29 | import com.datastax.oss.protocol.internal.response.Error; 30 | import com.datastax.oss.protocol.internal.response.Event; 31 | import com.datastax.oss.protocol.internal.response.Ready; 32 | import com.datastax.oss.protocol.internal.response.Result; 33 | import com.datastax.oss.protocol.internal.response.Supported; 34 | 35 | public class ProtocolV3ClientCodecs implements FrameCodec.CodecGroup { 36 | @Override 37 | public void registerCodecs(Registry registry) { 38 | registry 39 | .addEncoder(new AuthResponse.Codec(ProtocolConstants.Version.V3)) 40 | .addEncoder(new Batch.Codec(ProtocolConstants.Version.V3)) 41 | .addEncoder(new Execute.Codec(ProtocolConstants.Version.V3)) 42 | .addEncoder(new Options.Codec(ProtocolConstants.Version.V3)) 43 | .addEncoder(new Prepare.Codec(ProtocolConstants.Version.V3)) 44 | .addEncoder(new Query.Codec(ProtocolConstants.Version.V3)) 45 | .addEncoder(new Register.Codec(ProtocolConstants.Version.V3)) 46 | .addEncoder(new Startup.Codec(ProtocolConstants.Version.V3)); 47 | 48 | registry 49 | .addDecoder(new AuthChallenge.Codec(ProtocolConstants.Version.V3)) 50 | .addDecoder(new Authenticate.Codec(ProtocolConstants.Version.V3)) 51 | .addDecoder(new AuthSuccess.Codec(ProtocolConstants.Version.V3)) 52 | .addDecoder(new Error.Codec(ProtocolConstants.Version.V3)) 53 | .addDecoder(new Event.Codec(ProtocolConstants.Version.V3)) 54 | .addDecoder(new Ready.Codec(ProtocolConstants.Version.V3)) 55 | .addDecoder(new Result.Codec(ProtocolConstants.Version.V3)) 56 | .addDecoder(new Supported.Codec(ProtocolConstants.Version.V3)); 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /src/main/java/com/datastax/oss/protocol/internal/ProtocolV3ServerCodecs.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 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.oss.protocol.internal; 17 | 18 | import com.datastax.oss.protocol.internal.request.AuthResponse; 19 | import com.datastax.oss.protocol.internal.request.Batch; 20 | import com.datastax.oss.protocol.internal.request.Execute; 21 | import com.datastax.oss.protocol.internal.request.Options; 22 | import com.datastax.oss.protocol.internal.request.Prepare; 23 | import com.datastax.oss.protocol.internal.request.Query; 24 | import com.datastax.oss.protocol.internal.request.Register; 25 | import com.datastax.oss.protocol.internal.request.Startup; 26 | import com.datastax.oss.protocol.internal.response.AuthChallenge; 27 | import com.datastax.oss.protocol.internal.response.AuthSuccess; 28 | import com.datastax.oss.protocol.internal.response.Authenticate; 29 | import com.datastax.oss.protocol.internal.response.Error; 30 | import com.datastax.oss.protocol.internal.response.Event; 31 | import com.datastax.oss.protocol.internal.response.Ready; 32 | import com.datastax.oss.protocol.internal.response.Result; 33 | import com.datastax.oss.protocol.internal.response.Supported; 34 | 35 | public class ProtocolV3ServerCodecs implements FrameCodec.CodecGroup { 36 | @Override 37 | public void registerCodecs(Registry registry) { 38 | registry 39 | .addDecoder(new AuthResponse.Codec(ProtocolConstants.Version.V3)) 40 | .addDecoder(new Batch.Codec(ProtocolConstants.Version.V3)) 41 | .addDecoder(new Execute.Codec(ProtocolConstants.Version.V3)) 42 | .addDecoder(new Options.Codec(ProtocolConstants.Version.V3)) 43 | .addDecoder(new Prepare.Codec(ProtocolConstants.Version.V3)) 44 | .addDecoder(new Query.Codec(ProtocolConstants.Version.V3)) 45 | .addDecoder(new Register.Codec(ProtocolConstants.Version.V3)) 46 | .addDecoder(new Startup.Codec(ProtocolConstants.Version.V3)); 47 | 48 | registry 49 | .addEncoder(new AuthChallenge.Codec(ProtocolConstants.Version.V3)) 50 | .addEncoder(new Authenticate.Codec(ProtocolConstants.Version.V3)) 51 | .addEncoder(new AuthSuccess.Codec(ProtocolConstants.Version.V3)) 52 | .addEncoder(new Error.Codec(ProtocolConstants.Version.V3)) 53 | .addEncoder(new Event.Codec(ProtocolConstants.Version.V3)) 54 | .addEncoder(new Ready.Codec(ProtocolConstants.Version.V3)) 55 | .addEncoder(new Result.Codec(ProtocolConstants.Version.V3)) 56 | .addEncoder(new Supported.Codec(ProtocolConstants.Version.V3)); 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /src/main/java/com/datastax/oss/protocol/internal/ProtocolV4ClientCodecs.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 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.oss.protocol.internal; 17 | 18 | import com.datastax.oss.protocol.internal.request.AuthResponse; 19 | import com.datastax.oss.protocol.internal.request.Batch; 20 | import com.datastax.oss.protocol.internal.request.Execute; 21 | import com.datastax.oss.protocol.internal.request.Options; 22 | import com.datastax.oss.protocol.internal.request.Prepare; 23 | import com.datastax.oss.protocol.internal.request.Query; 24 | import com.datastax.oss.protocol.internal.request.Register; 25 | import com.datastax.oss.protocol.internal.request.Startup; 26 | import com.datastax.oss.protocol.internal.response.AuthChallenge; 27 | import com.datastax.oss.protocol.internal.response.AuthSuccess; 28 | import com.datastax.oss.protocol.internal.response.Authenticate; 29 | import com.datastax.oss.protocol.internal.response.Error; 30 | import com.datastax.oss.protocol.internal.response.Event; 31 | import com.datastax.oss.protocol.internal.response.Ready; 32 | import com.datastax.oss.protocol.internal.response.Result; 33 | import com.datastax.oss.protocol.internal.response.Supported; 34 | 35 | public class ProtocolV4ClientCodecs implements FrameCodec.CodecGroup { 36 | @Override 37 | public void registerCodecs(Registry registry) { 38 | registry 39 | .addEncoder(new AuthResponse.Codec(ProtocolConstants.Version.V4)) 40 | .addEncoder(new Batch.Codec(ProtocolConstants.Version.V4)) 41 | .addEncoder(new Execute.Codec(ProtocolConstants.Version.V4)) 42 | .addEncoder(new Options.Codec(ProtocolConstants.Version.V4)) 43 | .addEncoder(new Prepare.Codec(ProtocolConstants.Version.V4)) 44 | .addEncoder(new Query.Codec(ProtocolConstants.Version.V4)) 45 | .addEncoder(new Register.Codec(ProtocolConstants.Version.V4)) 46 | .addEncoder(new Startup.Codec(ProtocolConstants.Version.V4)); 47 | 48 | registry 49 | .addDecoder(new AuthChallenge.Codec(ProtocolConstants.Version.V4)) 50 | .addDecoder(new Authenticate.Codec(ProtocolConstants.Version.V4)) 51 | .addDecoder(new AuthSuccess.Codec(ProtocolConstants.Version.V4)) 52 | .addDecoder(new Error.Codec(ProtocolConstants.Version.V4)) 53 | .addDecoder(new Event.Codec(ProtocolConstants.Version.V4)) 54 | .addDecoder(new Ready.Codec(ProtocolConstants.Version.V4)) 55 | .addDecoder(new Result.Codec(ProtocolConstants.Version.V4)) 56 | .addDecoder(new Supported.Codec(ProtocolConstants.Version.V4)); 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /src/main/java/com/datastax/oss/protocol/internal/ProtocolV4ServerCodecs.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 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.oss.protocol.internal; 17 | 18 | import static com.datastax.oss.protocol.internal.ProtocolConstants.Version.V4; 19 | 20 | import com.datastax.oss.protocol.internal.request.AuthResponse; 21 | import com.datastax.oss.protocol.internal.request.Batch; 22 | import com.datastax.oss.protocol.internal.request.Execute; 23 | import com.datastax.oss.protocol.internal.request.Options; 24 | import com.datastax.oss.protocol.internal.request.Prepare; 25 | import com.datastax.oss.protocol.internal.request.Query; 26 | import com.datastax.oss.protocol.internal.request.Register; 27 | import com.datastax.oss.protocol.internal.request.Startup; 28 | import com.datastax.oss.protocol.internal.response.AuthChallenge; 29 | import com.datastax.oss.protocol.internal.response.AuthSuccess; 30 | import com.datastax.oss.protocol.internal.response.Authenticate; 31 | import com.datastax.oss.protocol.internal.response.Error; 32 | import com.datastax.oss.protocol.internal.response.Event; 33 | import com.datastax.oss.protocol.internal.response.Ready; 34 | import com.datastax.oss.protocol.internal.response.Result; 35 | import com.datastax.oss.protocol.internal.response.Supported; 36 | 37 | public class ProtocolV4ServerCodecs implements FrameCodec.CodecGroup { 38 | @Override 39 | public void registerCodecs(Registry registry) { 40 | registry 41 | .addDecoder(new AuthResponse.Codec(V4)) 42 | .addDecoder(new Batch.Codec(V4)) 43 | .addDecoder(new Execute.Codec(V4)) 44 | .addDecoder(new Options.Codec(V4)) 45 | .addDecoder(new Prepare.Codec(V4)) 46 | .addDecoder(new Query.Codec(V4)) 47 | .addDecoder(new Register.Codec(V4)) 48 | .addDecoder(new Startup.Codec(V4)); 49 | 50 | registry 51 | .addEncoder(new AuthChallenge.Codec(V4)) 52 | .addEncoder(new Authenticate.Codec(V4)) 53 | .addEncoder(new AuthSuccess.Codec(V4)) 54 | .addEncoder(new Error.Codec(V4)) 55 | .addEncoder(new Event.Codec(V4)) 56 | .addEncoder(new Ready.Codec(V4)) 57 | .addEncoder(new Result.Codec(V4)) 58 | .addEncoder(new Supported.Codec(V4)); 59 | } 60 | } 61 | -------------------------------------------------------------------------------- /src/main/java/com/datastax/oss/protocol/internal/ProtocolV5ClientCodecs.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 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.oss.protocol.internal; 17 | 18 | import static com.datastax.oss.protocol.internal.ProtocolConstants.Version.V5; 19 | 20 | import com.datastax.oss.protocol.internal.request.AuthResponse; 21 | import com.datastax.oss.protocol.internal.request.Batch; 22 | import com.datastax.oss.protocol.internal.request.Execute; 23 | import com.datastax.oss.protocol.internal.request.Options; 24 | import com.datastax.oss.protocol.internal.request.Prepare; 25 | import com.datastax.oss.protocol.internal.request.Query; 26 | import com.datastax.oss.protocol.internal.request.Register; 27 | import com.datastax.oss.protocol.internal.request.Startup; 28 | import com.datastax.oss.protocol.internal.response.AuthChallenge; 29 | import com.datastax.oss.protocol.internal.response.AuthSuccess; 30 | import com.datastax.oss.protocol.internal.response.Authenticate; 31 | import com.datastax.oss.protocol.internal.response.Error; 32 | import com.datastax.oss.protocol.internal.response.Event; 33 | import com.datastax.oss.protocol.internal.response.Ready; 34 | import com.datastax.oss.protocol.internal.response.Result; 35 | import com.datastax.oss.protocol.internal.response.Supported; 36 | 37 | public class ProtocolV5ClientCodecs implements FrameCodec.CodecGroup { 38 | @Override 39 | public void registerCodecs(Registry registry) { 40 | registry 41 | .addEncoder(new AuthResponse.Codec(V5)) 42 | .addEncoder(new Batch.Codec(V5)) 43 | .addEncoder(new Execute.Codec(V5)) 44 | .addEncoder(new Options.Codec(V5)) 45 | .addEncoder(new Prepare.Codec(V5)) 46 | .addEncoder(new Query.Codec(V5)) 47 | .addEncoder(new Register.Codec(V5)) 48 | .addEncoder(new Startup.Codec(V5)); 49 | 50 | registry 51 | .addDecoder(new AuthChallenge.Codec(V5)) 52 | .addDecoder(new Authenticate.Codec(V5)) 53 | .addDecoder(new AuthSuccess.Codec(V5)) 54 | .addDecoder(new Error.Codec(V5)) 55 | .addDecoder(new Event.Codec(V5)) 56 | .addDecoder(new Ready.Codec(V5)) 57 | .addDecoder(new Result.Codec(V5)) 58 | .addDecoder(new Supported.Codec(V5)); 59 | } 60 | } 61 | -------------------------------------------------------------------------------- /src/main/java/com/datastax/oss/protocol/internal/ProtocolV5ServerCodecs.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 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.oss.protocol.internal; 17 | 18 | import com.datastax.oss.protocol.internal.request.AuthResponse; 19 | import com.datastax.oss.protocol.internal.request.Batch; 20 | import com.datastax.oss.protocol.internal.request.Execute; 21 | import com.datastax.oss.protocol.internal.request.Options; 22 | import com.datastax.oss.protocol.internal.request.Prepare; 23 | import com.datastax.oss.protocol.internal.request.Query; 24 | import com.datastax.oss.protocol.internal.request.Register; 25 | import com.datastax.oss.protocol.internal.request.Startup; 26 | import com.datastax.oss.protocol.internal.response.AuthChallenge; 27 | import com.datastax.oss.protocol.internal.response.AuthSuccess; 28 | import com.datastax.oss.protocol.internal.response.Authenticate; 29 | import com.datastax.oss.protocol.internal.response.Error; 30 | import com.datastax.oss.protocol.internal.response.Event; 31 | import com.datastax.oss.protocol.internal.response.Ready; 32 | import com.datastax.oss.protocol.internal.response.Result; 33 | import com.datastax.oss.protocol.internal.response.Supported; 34 | 35 | public class ProtocolV5ServerCodecs implements FrameCodec.CodecGroup { 36 | @Override 37 | public void registerCodecs(Registry registry) { 38 | registry 39 | .addDecoder(new AuthResponse.Codec(ProtocolConstants.Version.V5)) 40 | .addDecoder(new Batch.Codec(ProtocolConstants.Version.V5)) 41 | .addDecoder(new Execute.Codec(ProtocolConstants.Version.V5)) 42 | .addDecoder(new Options.Codec(ProtocolConstants.Version.V5)) 43 | .addDecoder(new Prepare.Codec(ProtocolConstants.Version.V5)) 44 | .addDecoder(new Query.Codec(ProtocolConstants.Version.V5)) 45 | .addDecoder(new Register.Codec(ProtocolConstants.Version.V5)) 46 | .addDecoder(new Startup.Codec(ProtocolConstants.Version.V5)); 47 | 48 | registry 49 | .addEncoder(new AuthChallenge.Codec(ProtocolConstants.Version.V5)) 50 | .addEncoder(new Authenticate.Codec(ProtocolConstants.Version.V5)) 51 | .addEncoder(new AuthSuccess.Codec(ProtocolConstants.Version.V5)) 52 | .addEncoder(new Error.Codec(ProtocolConstants.Version.V5)) 53 | .addEncoder(new Event.Codec(ProtocolConstants.Version.V5)) 54 | .addEncoder(new Ready.Codec(ProtocolConstants.Version.V5)) 55 | .addEncoder(new Result.Codec(ProtocolConstants.Version.V5)) 56 | .addEncoder(new Supported.Codec(ProtocolConstants.Version.V5)); 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /src/main/java/com/datastax/oss/protocol/internal/ProtocolV6ClientCodecs.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 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.oss.protocol.internal; 17 | 18 | import static com.datastax.oss.protocol.internal.ProtocolConstants.Version.V6; 19 | 20 | import com.datastax.oss.protocol.internal.request.AuthResponse; 21 | import com.datastax.oss.protocol.internal.request.Batch; 22 | import com.datastax.oss.protocol.internal.request.Execute; 23 | import com.datastax.oss.protocol.internal.request.Options; 24 | import com.datastax.oss.protocol.internal.request.Prepare; 25 | import com.datastax.oss.protocol.internal.request.Query; 26 | import com.datastax.oss.protocol.internal.request.Register; 27 | import com.datastax.oss.protocol.internal.request.Startup; 28 | import com.datastax.oss.protocol.internal.response.AuthChallenge; 29 | import com.datastax.oss.protocol.internal.response.AuthSuccess; 30 | import com.datastax.oss.protocol.internal.response.Authenticate; 31 | import com.datastax.oss.protocol.internal.response.Error; 32 | import com.datastax.oss.protocol.internal.response.Event; 33 | import com.datastax.oss.protocol.internal.response.Ready; 34 | import com.datastax.oss.protocol.internal.response.Result; 35 | import com.datastax.oss.protocol.internal.response.Supported; 36 | 37 | public class ProtocolV6ClientCodecs implements FrameCodec.CodecGroup { 38 | @Override 39 | public void registerCodecs(Registry registry) { 40 | registry 41 | .addEncoder(new AuthResponse.Codec(V6)) 42 | .addEncoder(new Batch.Codec(V6)) 43 | .addEncoder(new Execute.Codec(V6)) 44 | .addEncoder(new Options.Codec(V6)) 45 | .addEncoder(new Prepare.Codec(V6)) 46 | .addEncoder(new Query.Codec(V6)) 47 | .addEncoder(new Register.Codec(V6)) 48 | .addEncoder(new Startup.Codec(V6)); 49 | 50 | registry 51 | .addDecoder(new AuthChallenge.Codec(V6)) 52 | .addDecoder(new Authenticate.Codec(V6)) 53 | .addDecoder(new AuthSuccess.Codec(V6)) 54 | .addDecoder(new Error.Codec(V6)) 55 | .addDecoder(new Event.Codec(V6)) 56 | .addDecoder(new Ready.Codec(V6)) 57 | .addDecoder(new Result.Codec(V6)) 58 | .addDecoder(new Supported.Codec(V6)); 59 | } 60 | } 61 | -------------------------------------------------------------------------------- /src/main/java/com/datastax/oss/protocol/internal/ProtocolV6ServerCodecs.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 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.oss.protocol.internal; 17 | 18 | import com.datastax.oss.protocol.internal.request.AuthResponse; 19 | import com.datastax.oss.protocol.internal.request.Batch; 20 | import com.datastax.oss.protocol.internal.request.Execute; 21 | import com.datastax.oss.protocol.internal.request.Options; 22 | import com.datastax.oss.protocol.internal.request.Prepare; 23 | import com.datastax.oss.protocol.internal.request.Query; 24 | import com.datastax.oss.protocol.internal.request.Register; 25 | import com.datastax.oss.protocol.internal.request.Startup; 26 | import com.datastax.oss.protocol.internal.response.AuthChallenge; 27 | import com.datastax.oss.protocol.internal.response.AuthSuccess; 28 | import com.datastax.oss.protocol.internal.response.Authenticate; 29 | import com.datastax.oss.protocol.internal.response.Error; 30 | import com.datastax.oss.protocol.internal.response.Event; 31 | import com.datastax.oss.protocol.internal.response.Ready; 32 | import com.datastax.oss.protocol.internal.response.Result; 33 | import com.datastax.oss.protocol.internal.response.Supported; 34 | 35 | public class ProtocolV6ServerCodecs implements FrameCodec.CodecGroup { 36 | @Override 37 | public void registerCodecs(Registry registry) { 38 | registry 39 | .addDecoder(new AuthResponse.Codec(ProtocolConstants.Version.V6)) 40 | .addDecoder(new Batch.Codec(ProtocolConstants.Version.V6)) 41 | .addDecoder(new Execute.Codec(ProtocolConstants.Version.V6)) 42 | .addDecoder(new Options.Codec(ProtocolConstants.Version.V6)) 43 | .addDecoder(new Prepare.Codec(ProtocolConstants.Version.V6)) 44 | .addDecoder(new Query.Codec(ProtocolConstants.Version.V6)) 45 | .addDecoder(new Register.Codec(ProtocolConstants.Version.V6)) 46 | .addDecoder(new Startup.Codec(ProtocolConstants.Version.V6)); 47 | 48 | registry 49 | .addEncoder(new AuthChallenge.Codec(ProtocolConstants.Version.V6)) 50 | .addEncoder(new Authenticate.Codec(ProtocolConstants.Version.V6)) 51 | .addEncoder(new AuthSuccess.Codec(ProtocolConstants.Version.V6)) 52 | .addEncoder(new Error.Codec(ProtocolConstants.Version.V6)) 53 | .addEncoder(new Event.Codec(ProtocolConstants.Version.V6)) 54 | .addEncoder(new Ready.Codec(ProtocolConstants.Version.V6)) 55 | .addEncoder(new Result.Codec(ProtocolConstants.Version.V6)) 56 | .addEncoder(new Supported.Codec(ProtocolConstants.Version.V6)); 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /src/main/java/com/datastax/oss/protocol/internal/Segment.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 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.oss.protocol.internal; 17 | 18 | /** 19 | * A container of {@link Frame}s in protocol v5 and above. This is a new protocol construct that 20 | * allows checksumming and compressing multiple messages together. 21 | * 22 | *

{@link #payload} contains either: 23 | * 24 | *

    25 | *
  • a sequence of encoded {@link Frame}s, all concatenated together. In this case, {@link 26 | * #isSelfContained} is true. 27 | *
  • or a slice of an encoded large {@link Frame} (if that frame is longer than {@link 28 | * #MAX_PAYLOAD_LENGTH}). In this case, {@link #isSelfContained} is false. 29 | *
30 | * 31 | * The payload is not compressed; compression is handled at a lower level when encoding or decoding 32 | * this object. 33 | * 34 | *

Naming is provisional: it's possible that this type will be renamed to "frame", and {@link 35 | * Frame} to something else, at some point in the future (this is an ongoing discussion on the 36 | * server ticket). 37 | */ 38 | public class Segment { 39 | 40 | public static int MAX_PAYLOAD_LENGTH = 128 * 1024 - 1; 41 | 42 | public final B payload; 43 | public final boolean isSelfContained; 44 | 45 | public Segment(B payload, boolean isSelfContained) { 46 | this.payload = payload; 47 | this.isSelfContained = isSelfContained; 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /src/main/java/com/datastax/oss/protocol/internal/request/AuthResponse.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 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.oss.protocol.internal.request; 17 | 18 | import com.datastax.oss.protocol.internal.Message; 19 | import com.datastax.oss.protocol.internal.PrimitiveCodec; 20 | import com.datastax.oss.protocol.internal.PrimitiveSizes; 21 | import com.datastax.oss.protocol.internal.ProtocolConstants; 22 | import com.datastax.oss.protocol.internal.util.Bytes; 23 | import java.nio.ByteBuffer; 24 | 25 | /** 26 | * Note that, if the token is writable, the built-in codec will clear its contents 27 | * immediately after writing it (to avoid keeping sensitive information in memory). If you want to 28 | * reuse the same buffer across multiple message instances, make it {@linkplain 29 | * ByteBuffer#asReadOnlyBuffer() read-only}. 30 | */ 31 | public class AuthResponse extends Message { 32 | public final ByteBuffer token; 33 | 34 | public AuthResponse(ByteBuffer token) { 35 | super(false, ProtocolConstants.Opcode.AUTH_RESPONSE); 36 | this.token = token; 37 | } 38 | 39 | @Override 40 | public String toString() { 41 | return "AUTH_RESPONSE"; 42 | } 43 | 44 | public static class Codec extends Message.Codec { 45 | 46 | public Codec(int protocolVersion) { 47 | super(ProtocolConstants.Opcode.AUTH_RESPONSE, protocolVersion); 48 | } 49 | 50 | @Override 51 | public void encode(B dest, Message message, PrimitiveCodec encoder) { 52 | AuthResponse authResponse = (AuthResponse) message; 53 | ByteBuffer token = authResponse.token; 54 | if (token == null) { 55 | encoder.writeBytes(token, dest); 56 | } else { 57 | token.mark(); 58 | encoder.writeBytes(token, dest); 59 | token.reset(); 60 | Bytes.erase(token); 61 | } 62 | } 63 | 64 | @Override 65 | public int encodedSize(Message message) { 66 | AuthResponse authResponse = (AuthResponse) message; 67 | return PrimitiveSizes.sizeOfBytes(authResponse.token); 68 | } 69 | 70 | @Override 71 | public Message decode(B source, PrimitiveCodec decoder) { 72 | ByteBuffer token = decoder.readBytes(source); 73 | return new AuthResponse(token); 74 | } 75 | } 76 | } 77 | -------------------------------------------------------------------------------- /src/main/java/com/datastax/oss/protocol/internal/request/Execute.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 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.oss.protocol.internal.request; 17 | 18 | import static com.datastax.oss.protocol.internal.ProtocolConstants.Version.V5; 19 | 20 | import com.datastax.oss.protocol.internal.Message; 21 | import com.datastax.oss.protocol.internal.PrimitiveCodec; 22 | import com.datastax.oss.protocol.internal.PrimitiveSizes; 23 | import com.datastax.oss.protocol.internal.ProtocolConstants; 24 | import com.datastax.oss.protocol.internal.request.query.QueryOptions; 25 | import com.datastax.oss.protocol.internal.util.Bytes; 26 | 27 | public class Execute extends Message { 28 | 29 | public final byte[] queryId; 30 | public final byte[] resultMetadataId; 31 | public final QueryOptions options; 32 | 33 | public Execute(byte[] queryId, byte[] resultMetadataId, QueryOptions options) { 34 | super(false, ProtocolConstants.Opcode.EXECUTE); 35 | this.queryId = queryId; 36 | this.resultMetadataId = resultMetadataId; 37 | this.options = options; 38 | } 39 | 40 | public Execute(byte[] queryId, QueryOptions options) { 41 | this(queryId, null, options); 42 | } 43 | 44 | @Override 45 | public String toString() { 46 | return "EXECUTE(" + Bytes.toHexString(queryId) + ')'; 47 | } 48 | 49 | public static class Codec extends Message.Codec { 50 | 51 | private final QueryOptions.Codec optionsCodec; 52 | 53 | public Codec(int protocolVersion, QueryOptions.Codec optionsCodec) { 54 | super(ProtocolConstants.Opcode.EXECUTE, protocolVersion); 55 | this.optionsCodec = optionsCodec; 56 | } 57 | 58 | public Codec(int protocolVersion) { 59 | this(protocolVersion, new QueryOptions.Codec(protocolVersion)); 60 | } 61 | 62 | @Override 63 | public void encode(B dest, Message message, PrimitiveCodec encoder) { 64 | Execute execute = (Execute) message; 65 | encoder.writeShortBytes(execute.queryId, dest); 66 | if (protocolVersion >= V5) { 67 | encoder.writeShortBytes(execute.resultMetadataId, dest); 68 | } 69 | optionsCodec.encode(dest, execute.options, encoder); 70 | } 71 | 72 | @Override 73 | public int encodedSize(Message message) { 74 | Execute execute = (Execute) message; 75 | int size = PrimitiveSizes.sizeOfShortBytes(execute.queryId); 76 | if (protocolVersion >= V5) { 77 | assert execute.resultMetadataId != null; 78 | size += PrimitiveSizes.sizeOfShortBytes(execute.resultMetadataId); 79 | } 80 | size += optionsCodec.encodedSize(execute.options); 81 | return size; 82 | } 83 | 84 | @Override 85 | public Message decode(B source, PrimitiveCodec decoder) { 86 | byte[] queryId = decoder.readShortBytes(source); 87 | byte[] resultMetadataId = (protocolVersion >= V5) ? decoder.readShortBytes(source) : null; 88 | QueryOptions options = optionsCodec.decode(source, decoder); 89 | return new Execute(queryId, resultMetadataId, options); 90 | } 91 | } 92 | } 93 | -------------------------------------------------------------------------------- /src/main/java/com/datastax/oss/protocol/internal/request/Options.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 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.oss.protocol.internal.request; 17 | 18 | import com.datastax.oss.protocol.internal.Message; 19 | import com.datastax.oss.protocol.internal.PrimitiveCodec; 20 | import com.datastax.oss.protocol.internal.ProtocolConstants; 21 | 22 | public class Options extends Message { 23 | public static final Options INSTANCE = new Options(); 24 | 25 | private Options() { 26 | super(false, ProtocolConstants.Opcode.OPTIONS); 27 | } 28 | 29 | @Override 30 | public String toString() { 31 | return "OPTIONS"; 32 | } 33 | 34 | public static class Codec extends Message.Codec { 35 | public Codec(int protocolVersion) { 36 | super(ProtocolConstants.Opcode.OPTIONS, protocolVersion); 37 | } 38 | 39 | @Override 40 | public void encode(B dest, Message message, PrimitiveCodec encoder) { 41 | // nothing to do 42 | } 43 | 44 | @Override 45 | public int encodedSize(Message message) { 46 | return 0; 47 | } 48 | 49 | @Override 50 | public Message decode(B source, PrimitiveCodec decoder) { 51 | return Options.INSTANCE; 52 | } 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /src/main/java/com/datastax/oss/protocol/internal/request/Prepare.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 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.oss.protocol.internal.request; 17 | 18 | import static com.datastax.oss.protocol.internal.ProtocolConstants.Version.V5; 19 | 20 | import com.datastax.oss.protocol.internal.Message; 21 | import com.datastax.oss.protocol.internal.PrimitiveCodec; 22 | import com.datastax.oss.protocol.internal.PrimitiveSizes; 23 | import com.datastax.oss.protocol.internal.ProtocolConstants; 24 | 25 | public class Prepare extends Message { 26 | public final String cqlQuery; 27 | public final String keyspace; 28 | 29 | public Prepare(String cqlQuery, String keyspace) { 30 | super(false, ProtocolConstants.Opcode.PREPARE); 31 | this.cqlQuery = cqlQuery; 32 | this.keyspace = keyspace; 33 | } 34 | 35 | public Prepare(String cqlQuery) { 36 | this(cqlQuery, null); 37 | } 38 | 39 | @Override 40 | public String toString() { 41 | return "PREPARE(" + cqlQuery + ", " + keyspace + ')'; 42 | } 43 | 44 | public static class Codec extends Message.Codec { 45 | public Codec(int protocolVersion) { 46 | super(ProtocolConstants.Opcode.PREPARE, protocolVersion); 47 | } 48 | 49 | @Override 50 | public void encode(B dest, Message message, PrimitiveCodec encoder) { 51 | Prepare prepare = (Prepare) message; 52 | encoder.writeLongString(prepare.cqlQuery, dest); 53 | if (protocolVersion >= V5) { 54 | // There is only one PREPARE flag for now, so hard-code for simplicity: 55 | encoder.writeInt((prepare.keyspace == null) ? 0x00 : 0x01, dest); 56 | if (prepare.keyspace != null) { 57 | encoder.writeString(prepare.keyspace, dest); 58 | } 59 | } 60 | } 61 | 62 | @Override 63 | public int encodedSize(Message message) { 64 | Prepare prepare = (Prepare) message; 65 | int size = PrimitiveSizes.sizeOfLongString(prepare.cqlQuery); 66 | if (protocolVersion >= V5) { 67 | size += PrimitiveSizes.INT; // flags 68 | if (prepare.keyspace != null) { 69 | size += PrimitiveSizes.sizeOfString(prepare.keyspace); 70 | } 71 | } 72 | return size; 73 | } 74 | 75 | @Override 76 | public Message decode(B source, PrimitiveCodec decoder) { 77 | String cqlQuery = decoder.readLongString(source); 78 | String keyspace = null; 79 | if (protocolVersion >= V5) { 80 | int flags = decoder.readInt(source); 81 | if ((flags & 0x01) == 0x01) { 82 | keyspace = decoder.readString(source); 83 | } 84 | } 85 | return new Prepare(cqlQuery, keyspace); 86 | } 87 | } 88 | } 89 | -------------------------------------------------------------------------------- /src/main/java/com/datastax/oss/protocol/internal/request/Query.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 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.oss.protocol.internal.request; 17 | 18 | import com.datastax.oss.protocol.internal.Message; 19 | import com.datastax.oss.protocol.internal.PrimitiveCodec; 20 | import com.datastax.oss.protocol.internal.PrimitiveSizes; 21 | import com.datastax.oss.protocol.internal.ProtocolConstants; 22 | import com.datastax.oss.protocol.internal.request.query.QueryOptions; 23 | 24 | public class Query extends Message { 25 | public final String query; 26 | public final QueryOptions options; 27 | 28 | public Query(String query, QueryOptions options) { 29 | super(false, ProtocolConstants.Opcode.QUERY); 30 | this.query = query; 31 | this.options = options; 32 | } 33 | 34 | public Query(String query) { 35 | this(query, QueryOptions.DEFAULT); 36 | } 37 | 38 | @Override 39 | public String toString() { 40 | return "QUERY (" + query + ')'; 41 | } 42 | 43 | public static class Codec extends Message.Codec { 44 | 45 | protected final QueryOptions.Codec optionsCodec; 46 | 47 | public Codec(int protocolVersion, QueryOptions.Codec optionsCodec) { 48 | super(ProtocolConstants.Opcode.QUERY, protocolVersion); 49 | this.optionsCodec = optionsCodec; 50 | } 51 | 52 | public Codec(int protocolVersion) { 53 | this(protocolVersion, new QueryOptions.Codec(protocolVersion)); 54 | } 55 | 56 | @Override 57 | public void encode(B dest, Message message, PrimitiveCodec encoder) { 58 | Query query = (Query) message; 59 | encoder.writeLongString(query.query, dest); 60 | optionsCodec.encode(dest, query.options, encoder); 61 | } 62 | 63 | @Override 64 | public int encodedSize(Message message) { 65 | Query query = (Query) message; 66 | return PrimitiveSizes.sizeOfLongString(query.query) + optionsCodec.encodedSize(query.options); 67 | } 68 | 69 | @Override 70 | public Message decode(B source, PrimitiveCodec decoder) { 71 | String query = decoder.readLongString(source); 72 | QueryOptions options = optionsCodec.decode(source, decoder); 73 | return new Query(query, options); 74 | } 75 | } 76 | } 77 | -------------------------------------------------------------------------------- /src/main/java/com/datastax/oss/protocol/internal/request/Register.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 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.oss.protocol.internal.request; 17 | 18 | import com.datastax.oss.protocol.internal.Message; 19 | import com.datastax.oss.protocol.internal.PrimitiveCodec; 20 | import com.datastax.oss.protocol.internal.PrimitiveSizes; 21 | import com.datastax.oss.protocol.internal.ProtocolConstants; 22 | import java.util.List; 23 | 24 | public class Register extends Message { 25 | 26 | /** @see ProtocolConstants.EventType */ 27 | public final List eventTypes; 28 | 29 | public Register(List eventTypes) { 30 | super(false, ProtocolConstants.Opcode.REGISTER); 31 | this.eventTypes = eventTypes; 32 | } 33 | 34 | @Override 35 | public String toString() { 36 | return "REGISTER " + eventTypes; 37 | } 38 | 39 | public static class Codec extends Message.Codec { 40 | 41 | public Codec(int protocolVersion) { 42 | super(ProtocolConstants.Opcode.REGISTER, protocolVersion); 43 | } 44 | 45 | @Override 46 | public void encode(B dest, Message message, PrimitiveCodec encoder) { 47 | Register register = (Register) message; 48 | encoder.writeStringList(register.eventTypes, dest); 49 | } 50 | 51 | @Override 52 | public int encodedSize(Message message) { 53 | Register register = (Register) message; 54 | return PrimitiveSizes.sizeOfStringList(register.eventTypes); 55 | } 56 | 57 | @Override 58 | public Message decode(B source, PrimitiveCodec decoder) { 59 | return new Register(decoder.readStringList(source)); 60 | } 61 | } 62 | } 63 | -------------------------------------------------------------------------------- /src/main/java/com/datastax/oss/protocol/internal/request/Startup.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 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.oss.protocol.internal.request; 17 | 18 | import com.datastax.oss.protocol.internal.Message; 19 | import com.datastax.oss.protocol.internal.PrimitiveCodec; 20 | import com.datastax.oss.protocol.internal.PrimitiveSizes; 21 | import com.datastax.oss.protocol.internal.ProtocolConstants; 22 | import com.datastax.oss.protocol.internal.util.collection.NullAllowingImmutableMap; 23 | import java.util.Map; 24 | 25 | public class Startup extends Message { 26 | public static final String CQL_VERSION_KEY = "CQL_VERSION"; 27 | public static final String COMPRESSION_KEY = "COMPRESSION"; 28 | 29 | private static final String CQL_VERSION = "3.0.0"; 30 | 31 | public final Map options; 32 | 33 | public Startup(String compressionAlgorithm) { 34 | this( 35 | (compressionAlgorithm == null || compressionAlgorithm.isEmpty()) 36 | ? NullAllowingImmutableMap.of(CQL_VERSION_KEY, CQL_VERSION) 37 | : NullAllowingImmutableMap.of( 38 | CQL_VERSION_KEY, CQL_VERSION, COMPRESSION_KEY, compressionAlgorithm)); 39 | } 40 | 41 | public Startup() { 42 | this((Map) null); 43 | } 44 | 45 | public Startup(Map options) { 46 | super(false, ProtocolConstants.Opcode.STARTUP); 47 | if (options != null) { 48 | if (options.containsKey(CQL_VERSION_KEY)) { 49 | this.options = NullAllowingImmutableMap.copyOf(options); 50 | } else { 51 | NullAllowingImmutableMap.Builder builder = 52 | NullAllowingImmutableMap.builder(options.size() + 1); 53 | this.options = builder.put(CQL_VERSION_KEY, CQL_VERSION).putAll(options).build(); 54 | } 55 | } else { 56 | this.options = NullAllowingImmutableMap.of(CQL_VERSION_KEY, CQL_VERSION); 57 | } 58 | } 59 | 60 | @Override 61 | public String toString() { 62 | return "STARTUP " + options; 63 | } 64 | 65 | public static class Codec extends Message.Codec { 66 | 67 | public Codec(int protocolVersion) { 68 | super(ProtocolConstants.Opcode.STARTUP, protocolVersion); 69 | } 70 | 71 | @Override 72 | public void encode(B dest, Message message, PrimitiveCodec encoder) { 73 | Startup startup = (Startup) message; 74 | encoder.writeStringMap(startup.options, dest); 75 | } 76 | 77 | @Override 78 | public int encodedSize(Message message) { 79 | Startup startup = (Startup) message; 80 | return PrimitiveSizes.sizeOfStringMap(startup.options); 81 | } 82 | 83 | @Override 84 | public Message decode(B source, PrimitiveCodec decoder) { 85 | Map map = decoder.readStringMap(source); 86 | return new Startup(map); 87 | } 88 | } 89 | } 90 | -------------------------------------------------------------------------------- /src/main/java/com/datastax/oss/protocol/internal/response/AuthChallenge.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 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.oss.protocol.internal.response; 17 | 18 | import com.datastax.oss.protocol.internal.Message; 19 | import com.datastax.oss.protocol.internal.PrimitiveCodec; 20 | import com.datastax.oss.protocol.internal.PrimitiveSizes; 21 | import com.datastax.oss.protocol.internal.ProtocolConstants; 22 | import java.nio.ByteBuffer; 23 | 24 | public class AuthChallenge extends Message { 25 | 26 | public final ByteBuffer token; 27 | 28 | public AuthChallenge(ByteBuffer token) { 29 | super(true, ProtocolConstants.Opcode.AUTH_CHALLENGE); 30 | this.token = token; 31 | } 32 | 33 | @Override 34 | public String toString() { 35 | return "AUTH_CHALLENGE"; 36 | } 37 | 38 | public static class Codec extends Message.Codec { 39 | public Codec(int protocolVersion) { 40 | super(ProtocolConstants.Opcode.AUTH_CHALLENGE, protocolVersion); 41 | } 42 | 43 | @Override 44 | public void encode(B dest, Message message, PrimitiveCodec encoder) { 45 | AuthChallenge authChallenge = (AuthChallenge) message; 46 | encoder.writeBytes(authChallenge.token, dest); 47 | } 48 | 49 | @Override 50 | public int encodedSize(Message message) { 51 | AuthChallenge authChallenge = (AuthChallenge) message; 52 | return PrimitiveSizes.sizeOfBytes(authChallenge.token); 53 | } 54 | 55 | @Override 56 | public Message decode(B source, PrimitiveCodec decoder) { 57 | return new AuthChallenge(decoder.readBytes(source)); 58 | } 59 | } 60 | } 61 | -------------------------------------------------------------------------------- /src/main/java/com/datastax/oss/protocol/internal/response/AuthSuccess.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 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.oss.protocol.internal.response; 17 | 18 | import com.datastax.oss.protocol.internal.Message; 19 | import com.datastax.oss.protocol.internal.PrimitiveCodec; 20 | import com.datastax.oss.protocol.internal.PrimitiveSizes; 21 | import com.datastax.oss.protocol.internal.ProtocolConstants; 22 | import java.nio.ByteBuffer; 23 | 24 | public class AuthSuccess extends Message { 25 | public final ByteBuffer token; 26 | 27 | public AuthSuccess(ByteBuffer token) { 28 | super(true, ProtocolConstants.Opcode.AUTH_SUCCESS); 29 | this.token = token; 30 | } 31 | 32 | @Override 33 | public String toString() { 34 | return "AUTH_SUCCESS"; 35 | } 36 | 37 | public static class Codec extends Message.Codec { 38 | public Codec(int protocolVersion) { 39 | super(ProtocolConstants.Opcode.AUTH_SUCCESS, protocolVersion); 40 | } 41 | 42 | @Override 43 | public void encode(B dest, Message message, PrimitiveCodec encoder) { 44 | AuthSuccess authSuccess = (AuthSuccess) message; 45 | encoder.writeBytes(authSuccess.token, dest); 46 | } 47 | 48 | @Override 49 | public int encodedSize(Message message) { 50 | AuthSuccess authSuccess = (AuthSuccess) message; 51 | return PrimitiveSizes.sizeOfBytes(authSuccess.token); 52 | } 53 | 54 | @Override 55 | public Message decode(B source, PrimitiveCodec decoder) { 56 | return new AuthSuccess(decoder.readBytes(source)); 57 | } 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /src/main/java/com/datastax/oss/protocol/internal/response/Authenticate.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 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.oss.protocol.internal.response; 17 | 18 | import com.datastax.oss.protocol.internal.Message; 19 | import com.datastax.oss.protocol.internal.PrimitiveCodec; 20 | import com.datastax.oss.protocol.internal.PrimitiveSizes; 21 | import com.datastax.oss.protocol.internal.ProtocolConstants; 22 | 23 | public class Authenticate extends Message { 24 | public final String authenticator; 25 | 26 | public Authenticate(String authenticator) { 27 | super(true, ProtocolConstants.Opcode.AUTHENTICATE); 28 | this.authenticator = authenticator; 29 | } 30 | 31 | @Override 32 | public String toString() { 33 | return "AUTHENTICATE"; 34 | } 35 | 36 | public static class Codec extends Message.Codec { 37 | public Codec(int protocolVersion) { 38 | super(ProtocolConstants.Opcode.AUTHENTICATE, protocolVersion); 39 | } 40 | 41 | @Override 42 | public void encode(B dest, Message message, PrimitiveCodec encoder) { 43 | Authenticate authenticate = (Authenticate) message; 44 | encoder.writeString(authenticate.authenticator, dest); 45 | } 46 | 47 | @Override 48 | public int encodedSize(Message message) { 49 | Authenticate authenticate = (Authenticate) message; 50 | return PrimitiveSizes.sizeOfString(authenticate.authenticator); 51 | } 52 | 53 | @Override 54 | public Message decode(B source, PrimitiveCodec decoder) { 55 | return new Authenticate(decoder.readString(source)); 56 | } 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /src/main/java/com/datastax/oss/protocol/internal/response/Event.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 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.oss.protocol.internal.response; 17 | 18 | import com.datastax.oss.protocol.internal.Message; 19 | import com.datastax.oss.protocol.internal.PrimitiveCodec; 20 | import com.datastax.oss.protocol.internal.PrimitiveSizes; 21 | import com.datastax.oss.protocol.internal.ProtocolConstants; 22 | import com.datastax.oss.protocol.internal.ProtocolErrors; 23 | import com.datastax.oss.protocol.internal.response.event.SchemaChangeEvent; 24 | import com.datastax.oss.protocol.internal.response.event.StatusChangeEvent; 25 | import com.datastax.oss.protocol.internal.response.event.TopologyChangeEvent; 26 | import java.util.Collections; 27 | import java.util.HashMap; 28 | import java.util.Map; 29 | 30 | public abstract class Event extends Message { 31 | /** @see ProtocolConstants.EventType */ 32 | public final String type; 33 | 34 | protected Event(String type) { 35 | super(true, ProtocolConstants.Opcode.EVENT); 36 | this.type = type; 37 | } 38 | 39 | public static class Codec extends Message.Codec { 40 | private final Map subDecoders; 41 | 42 | public Codec(int protocolVersion, SubCodec... subCodecs) { 43 | super(ProtocolConstants.Opcode.EVENT, protocolVersion); 44 | Map tmp = new HashMap<>(); 45 | for (SubCodec subCodec : subCodecs) { 46 | tmp.put(subCodec.type, subCodec); 47 | } 48 | this.subDecoders = Collections.unmodifiableMap(tmp); 49 | } 50 | 51 | /** Creates an instance with subdecoders for the default types. */ 52 | public Codec(int protocolVersion) { 53 | this( 54 | protocolVersion, 55 | new TopologyChangeEvent.SubCodec(protocolVersion), 56 | new StatusChangeEvent.SubCodec(protocolVersion), 57 | new SchemaChangeEvent.SubCodec(protocolVersion)); 58 | } 59 | 60 | @Override 61 | public void encode(B dest, Message message, PrimitiveCodec encoder) { 62 | Event event = (Event) message; 63 | encoder.writeString(event.type, dest); 64 | getSubCodec(event.type).encode(dest, message, encoder); 65 | } 66 | 67 | @Override 68 | public int encodedSize(Message message) { 69 | Event event = (Event) message; 70 | return PrimitiveSizes.sizeOfString(event.type) + getSubCodec(event.type).encodedSize(message); 71 | } 72 | 73 | @Override 74 | public Message decode(B source, PrimitiveCodec decoder) { 75 | String type = decoder.readString(source); 76 | return getSubCodec(type).decode(source, decoder); 77 | } 78 | 79 | private SubCodec getSubCodec(String type) { 80 | SubCodec subCodec = subDecoders.get(type); 81 | ProtocolErrors.check(subCodec != null, "Unsupported event type: %s", type); 82 | return subCodec; 83 | } 84 | } 85 | 86 | public abstract static class SubCodec { 87 | public final String type; 88 | public final int protocolVersion; 89 | 90 | protected SubCodec(String type, int protocolVersion) { 91 | this.type = type; 92 | this.protocolVersion = protocolVersion; 93 | } 94 | 95 | public abstract void encode(B dest, Message message, PrimitiveCodec encoder); 96 | 97 | public abstract int encodedSize(Message message); 98 | 99 | public abstract Message decode(B source, PrimitiveCodec decoder); 100 | } 101 | } 102 | -------------------------------------------------------------------------------- /src/main/java/com/datastax/oss/protocol/internal/response/Ready.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 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.oss.protocol.internal.response; 17 | 18 | import com.datastax.oss.protocol.internal.Message; 19 | import com.datastax.oss.protocol.internal.PrimitiveCodec; 20 | import com.datastax.oss.protocol.internal.ProtocolConstants; 21 | 22 | public class Ready extends Message { 23 | 24 | public Ready() { 25 | super(true, ProtocolConstants.Opcode.READY); 26 | } 27 | 28 | @Override 29 | public String toString() { 30 | return "READY"; 31 | } 32 | 33 | public static class Codec extends Message.Codec { 34 | public Codec(int protocolVersion) { 35 | super(ProtocolConstants.Opcode.READY, protocolVersion); 36 | } 37 | 38 | @Override 39 | public void encode(B dest, Message message, PrimitiveCodec encoder) {} 40 | 41 | @Override 42 | public int encodedSize(Message message) { 43 | return 0; 44 | } 45 | 46 | @Override 47 | public Message decode(B source, PrimitiveCodec decoder) { 48 | return new Ready(); 49 | } 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /src/main/java/com/datastax/oss/protocol/internal/response/Supported.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 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.oss.protocol.internal.response; 17 | 18 | import com.datastax.oss.protocol.internal.Message; 19 | import com.datastax.oss.protocol.internal.PrimitiveCodec; 20 | import com.datastax.oss.protocol.internal.PrimitiveSizes; 21 | import com.datastax.oss.protocol.internal.ProtocolConstants; 22 | import java.util.List; 23 | import java.util.Map; 24 | 25 | public class Supported extends Message { 26 | 27 | public final Map> options; 28 | 29 | public Supported(Map> options) { 30 | super(true, ProtocolConstants.Opcode.SUPPORTED); 31 | this.options = options; 32 | } 33 | 34 | @Override 35 | public String toString() { 36 | return "SUPPORTED " + options; 37 | } 38 | 39 | public static class Codec extends Message.Codec { 40 | public Codec(int protocolVersion) { 41 | super(ProtocolConstants.Opcode.SUPPORTED, protocolVersion); 42 | } 43 | 44 | @Override 45 | public void encode(B dest, Message message, PrimitiveCodec encoder) { 46 | encoder.writeStringMultimap(((Supported) message).options, dest); 47 | } 48 | 49 | @Override 50 | public int encodedSize(Message message) { 51 | Supported supported = (Supported) message; 52 | return PrimitiveSizes.sizeOfStringMultimap(supported.options); 53 | } 54 | 55 | @Override 56 | public Message decode(B source, PrimitiveCodec decoder) { 57 | Map> options = decoder.readStringMultimap(source); 58 | return new Supported(options); 59 | } 60 | } 61 | } 62 | -------------------------------------------------------------------------------- /src/main/java/com/datastax/oss/protocol/internal/response/error/AlreadyExists.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 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.oss.protocol.internal.response.error; 17 | 18 | import com.datastax.oss.protocol.internal.Message; 19 | import com.datastax.oss.protocol.internal.PrimitiveCodec; 20 | import com.datastax.oss.protocol.internal.PrimitiveSizes; 21 | import com.datastax.oss.protocol.internal.ProtocolConstants; 22 | import com.datastax.oss.protocol.internal.response.Error; 23 | 24 | public class AlreadyExists extends Error { 25 | public final String keyspace; 26 | public final String table; 27 | 28 | public AlreadyExists(String message, String keyspace, String table) { 29 | super(ProtocolConstants.ErrorCode.ALREADY_EXISTS, message); 30 | this.keyspace = keyspace; 31 | this.table = table; 32 | } 33 | 34 | public static class SubCodec extends Error.SubCodec { 35 | public SubCodec(int protocolVersion) { 36 | super(ProtocolConstants.ErrorCode.ALREADY_EXISTS, protocolVersion); 37 | } 38 | 39 | @Override 40 | public void encode(B dest, Message message, PrimitiveCodec encoder) { 41 | AlreadyExists alreadyExists = (AlreadyExists) message; 42 | encoder.writeString(alreadyExists.message, dest); 43 | encoder.writeString(alreadyExists.keyspace, dest); 44 | encoder.writeString(alreadyExists.table, dest); 45 | } 46 | 47 | @Override 48 | public int encodedSize(Message message) { 49 | AlreadyExists alreadyExists = (AlreadyExists) message; 50 | return PrimitiveSizes.sizeOfString(alreadyExists.message) 51 | + PrimitiveSizes.sizeOfString(alreadyExists.keyspace) 52 | + PrimitiveSizes.sizeOfString(alreadyExists.table); 53 | } 54 | 55 | @Override 56 | public Message decode(B source, PrimitiveCodec decoder) { 57 | String message = decoder.readString(source); 58 | String keyspace = decoder.readString(source); 59 | String table = decoder.readString(source); 60 | return new AlreadyExists(message, keyspace, table); 61 | } 62 | } 63 | } 64 | -------------------------------------------------------------------------------- /src/main/java/com/datastax/oss/protocol/internal/response/error/CASWriteUnknown.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 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.oss.protocol.internal.response.error; 17 | 18 | import com.datastax.oss.protocol.internal.Message; 19 | import com.datastax.oss.protocol.internal.PrimitiveCodec; 20 | import com.datastax.oss.protocol.internal.PrimitiveSizes; 21 | import com.datastax.oss.protocol.internal.ProtocolConstants; 22 | import com.datastax.oss.protocol.internal.response.Error; 23 | 24 | public class CASWriteUnknown extends Error { 25 | 26 | /** The consistency level of the query that triggered the exception. */ 27 | public final int consistencyLevel; 28 | /** The number of nodes having answered the request. */ 29 | public final int received; 30 | /** The number of replicas whose response is required to achieve {@code consistencyLevel}. */ 31 | public final int blockFor; 32 | 33 | public CASWriteUnknown(String message, int consistencyLevel, int received, int blockFor) { 34 | super(ProtocolConstants.ErrorCode.CAS_WRITE_UNKNOWN, message); 35 | this.consistencyLevel = consistencyLevel; 36 | this.received = received; 37 | this.blockFor = blockFor; 38 | } 39 | 40 | public static class SubCodec extends Error.SubCodec { 41 | public SubCodec(int protocolVersion) { 42 | super(ProtocolConstants.ErrorCode.CAS_WRITE_UNKNOWN, protocolVersion); 43 | } 44 | 45 | @Override 46 | public void encode(B dest, Message message, PrimitiveCodec encoder) { 47 | CASWriteUnknown readTimeout = (CASWriteUnknown) message; 48 | encoder.writeString(readTimeout.message, dest); 49 | encoder.writeUnsignedShort(readTimeout.consistencyLevel, dest); 50 | encoder.writeInt(readTimeout.received, dest); 51 | encoder.writeInt(readTimeout.blockFor, dest); 52 | } 53 | 54 | @Override 55 | public int encodedSize(Message message) { 56 | CASWriteUnknown readTimeout = (CASWriteUnknown) message; 57 | return PrimitiveSizes.sizeOfString(readTimeout.message) 58 | + PrimitiveSizes.SHORT 59 | + PrimitiveSizes.INT 60 | + PrimitiveSizes.INT; 61 | } 62 | 63 | @Override 64 | public Message decode(B source, PrimitiveCodec decoder) { 65 | String message = decoder.readString(source); 66 | int consistencyLevel = decoder.readUnsignedShort(source); 67 | int received = decoder.readInt(source); 68 | int blockFor = decoder.readInt(source); 69 | return new CASWriteUnknown(message, consistencyLevel, received, blockFor); 70 | } 71 | } 72 | } 73 | -------------------------------------------------------------------------------- /src/main/java/com/datastax/oss/protocol/internal/response/error/FunctionFailure.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 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.oss.protocol.internal.response.error; 17 | 18 | import com.datastax.oss.protocol.internal.Message; 19 | import com.datastax.oss.protocol.internal.PrimitiveCodec; 20 | import com.datastax.oss.protocol.internal.PrimitiveSizes; 21 | import com.datastax.oss.protocol.internal.ProtocolConstants; 22 | import com.datastax.oss.protocol.internal.response.Error; 23 | import java.util.List; 24 | 25 | public class FunctionFailure extends Error { 26 | public final String keyspace; 27 | public final String function; 28 | public final List argTypes; 29 | 30 | public FunctionFailure(String message, String keyspace, String function, List argTypes) { 31 | super(ProtocolConstants.ErrorCode.FUNCTION_FAILURE, message); 32 | this.keyspace = keyspace; 33 | this.function = function; 34 | this.argTypes = argTypes; 35 | } 36 | 37 | public static class SubCodec extends Error.SubCodec { 38 | public SubCodec(int protocolVersion) { 39 | super(ProtocolConstants.ErrorCode.FUNCTION_FAILURE, protocolVersion); 40 | } 41 | 42 | @Override 43 | public void encode(B dest, Message message, PrimitiveCodec encoder) { 44 | FunctionFailure functionFailure = (FunctionFailure) message; 45 | encoder.writeString(functionFailure.message, dest); 46 | encoder.writeString(functionFailure.keyspace, dest); 47 | encoder.writeString(functionFailure.function, dest); 48 | encoder.writeStringList(functionFailure.argTypes, dest); 49 | } 50 | 51 | @Override 52 | public int encodedSize(Message message) { 53 | FunctionFailure functionFailure = (FunctionFailure) message; 54 | return PrimitiveSizes.sizeOfString(functionFailure.message) 55 | + PrimitiveSizes.sizeOfString(functionFailure.keyspace) 56 | + PrimitiveSizes.sizeOfString(functionFailure.function) 57 | + PrimitiveSizes.sizeOfStringList(functionFailure.argTypes); 58 | } 59 | 60 | @Override 61 | public Message decode(B source, PrimitiveCodec decoder) { 62 | String message = decoder.readString(source); 63 | String keyspace = decoder.readString(source); 64 | String function = decoder.readString(source); 65 | List argTypes = decoder.readStringList(source); 66 | return new FunctionFailure(message, keyspace, function, argTypes); 67 | } 68 | } 69 | } 70 | -------------------------------------------------------------------------------- /src/main/java/com/datastax/oss/protocol/internal/response/error/ReadTimeout.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 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.oss.protocol.internal.response.error; 17 | 18 | import com.datastax.oss.protocol.internal.Message; 19 | import com.datastax.oss.protocol.internal.PrimitiveCodec; 20 | import com.datastax.oss.protocol.internal.PrimitiveSizes; 21 | import com.datastax.oss.protocol.internal.ProtocolConstants; 22 | import com.datastax.oss.protocol.internal.response.Error; 23 | 24 | public class ReadTimeout extends Error { 25 | /** The consistency level of the query that triggered the exception. */ 26 | public final int consistencyLevel; 27 | /** The number of nodes having answered the request. */ 28 | public final int received; 29 | /** 30 | * The number of replicas whose response is required to achieve {@code consistencyLevel}. 31 | * 32 | *

It is possible to have {@code received >= blockFor} if {@code data_present} is false. Also 33 | * in the (unlikely) case where {@code consistencyLevel} is achieved but the coordinator node 34 | * times out while waiting for read-repair acknowledgement. 35 | */ 36 | public final int blockFor; 37 | /** Whether the replica that was asked for data responded. */ 38 | public final boolean dataPresent; 39 | 40 | public ReadTimeout( 41 | String message, int consistencyLevel, int received, int blockFor, boolean dataPresent) { 42 | super(ProtocolConstants.ErrorCode.READ_TIMEOUT, message); 43 | this.consistencyLevel = consistencyLevel; 44 | this.received = received; 45 | this.blockFor = blockFor; 46 | this.dataPresent = dataPresent; 47 | } 48 | 49 | public static class SubCodec extends Error.SubCodec { 50 | public SubCodec(int protocolVersion) { 51 | super(ProtocolConstants.ErrorCode.READ_TIMEOUT, protocolVersion); 52 | } 53 | 54 | @Override 55 | public void encode(B dest, Message message, PrimitiveCodec encoder) { 56 | ReadTimeout readTimeout = (ReadTimeout) message; 57 | encoder.writeString(readTimeout.message, dest); 58 | encoder.writeUnsignedShort(readTimeout.consistencyLevel, dest); 59 | encoder.writeInt(readTimeout.received, dest); 60 | encoder.writeInt(readTimeout.blockFor, dest); 61 | encoder.writeByte((byte) (readTimeout.dataPresent ? 1 : 0), dest); 62 | } 63 | 64 | @Override 65 | public int encodedSize(Message message) { 66 | ReadTimeout readTimeout = (ReadTimeout) message; 67 | return PrimitiveSizes.sizeOfString(readTimeout.message) 68 | + PrimitiveSizes.SHORT 69 | + PrimitiveSizes.INT 70 | + PrimitiveSizes.INT 71 | + PrimitiveSizes.BYTE; 72 | } 73 | 74 | @Override 75 | public Message decode(B source, PrimitiveCodec decoder) { 76 | String message = decoder.readString(source); 77 | int consistencyLevel = decoder.readUnsignedShort(source); 78 | int received = decoder.readInt(source); 79 | int blockFor = decoder.readInt(source); 80 | boolean dataPresent = decoder.readByte(source) != 0; 81 | return new ReadTimeout(message, consistencyLevel, received, blockFor, dataPresent); 82 | } 83 | } 84 | } 85 | -------------------------------------------------------------------------------- /src/main/java/com/datastax/oss/protocol/internal/response/error/Unavailable.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 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.oss.protocol.internal.response.error; 17 | 18 | import com.datastax.oss.protocol.internal.Message; 19 | import com.datastax.oss.protocol.internal.PrimitiveCodec; 20 | import com.datastax.oss.protocol.internal.PrimitiveSizes; 21 | import com.datastax.oss.protocol.internal.ProtocolConstants; 22 | import com.datastax.oss.protocol.internal.response.Error; 23 | 24 | public class Unavailable extends Error { 25 | /** The consistency level of the query that triggered the exception. */ 26 | public final int consistencyLevel; 27 | /** The number of nodes that should be alive to respect {@code consistencyLevel}. */ 28 | public final int required; 29 | /** 30 | * The number of replicas that were known to be alive when the request was processed (since an 31 | * unavailable exception has been triggered, {@code alive < required}. 32 | */ 33 | public final int alive; 34 | 35 | public Unavailable(String message, int consistencyLevel, int required, int alive) { 36 | super(ProtocolConstants.ErrorCode.UNAVAILABLE, message); 37 | this.consistencyLevel = consistencyLevel; 38 | this.required = required; 39 | this.alive = alive; 40 | } 41 | 42 | public static class SubCodec extends Error.SubCodec { 43 | public SubCodec(int protocolVersion) { 44 | super(ProtocolConstants.ErrorCode.UNAVAILABLE, protocolVersion); 45 | } 46 | 47 | @Override 48 | public void encode(B dest, Message message, PrimitiveCodec encoder) { 49 | Unavailable unavailable = (Unavailable) message; 50 | encoder.writeString(unavailable.message, dest); 51 | encoder.writeUnsignedShort(unavailable.consistencyLevel, dest); 52 | encoder.writeInt(unavailable.required, dest); 53 | encoder.writeInt(unavailable.alive, dest); 54 | } 55 | 56 | @Override 57 | public int encodedSize(Message message) { 58 | Unavailable unavailable = (Unavailable) message; 59 | return PrimitiveSizes.sizeOfString(unavailable.message) 60 | + PrimitiveSizes.SHORT 61 | + PrimitiveSizes.INT 62 | + PrimitiveSizes.INT; 63 | } 64 | 65 | @Override 66 | public Message decode(B source, PrimitiveCodec decoder) { 67 | String message = decoder.readString(source); 68 | int consistencyLevel = decoder.readUnsignedShort(source); 69 | int required = decoder.readInt(source); 70 | int alive = decoder.readInt(source); 71 | return new Unavailable(message, consistencyLevel, required, alive); 72 | } 73 | } 74 | } 75 | -------------------------------------------------------------------------------- /src/main/java/com/datastax/oss/protocol/internal/response/error/Unprepared.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 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.oss.protocol.internal.response.error; 17 | 18 | import com.datastax.oss.protocol.internal.Message; 19 | import com.datastax.oss.protocol.internal.PrimitiveCodec; 20 | import com.datastax.oss.protocol.internal.PrimitiveSizes; 21 | import com.datastax.oss.protocol.internal.ProtocolConstants; 22 | import com.datastax.oss.protocol.internal.response.Error; 23 | 24 | public class Unprepared extends Error { 25 | public final byte[] id; 26 | 27 | public Unprepared(String message, byte[] id) { 28 | super(ProtocolConstants.ErrorCode.UNPREPARED, message); 29 | this.id = id; 30 | } 31 | 32 | public static class SubCodec extends Error.SubCodec { 33 | public SubCodec(int protocolVersion) { 34 | super(ProtocolConstants.ErrorCode.UNPREPARED, protocolVersion); 35 | } 36 | 37 | @Override 38 | public void encode(B dest, Message message, PrimitiveCodec encoder) { 39 | Unprepared unprepared = (Unprepared) message; 40 | encoder.writeString(unprepared.message, dest); 41 | encoder.writeShortBytes(unprepared.id, dest); 42 | } 43 | 44 | @Override 45 | public int encodedSize(Message message) { 46 | Unprepared unprepared = (Unprepared) message; 47 | return PrimitiveSizes.sizeOfString(unprepared.message) 48 | + PrimitiveSizes.sizeOfShortBytes(unprepared.id); 49 | } 50 | 51 | @Override 52 | public Message decode(B source, PrimitiveCodec decoder) { 53 | String message = decoder.readString(source); 54 | byte[] id = decoder.readShortBytes(source); 55 | return new Unprepared(message, id); 56 | } 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /src/main/java/com/datastax/oss/protocol/internal/response/error/WriteTimeout.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 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.oss.protocol.internal.response.error; 17 | 18 | import com.datastax.oss.protocol.internal.Message; 19 | import com.datastax.oss.protocol.internal.PrimitiveCodec; 20 | import com.datastax.oss.protocol.internal.PrimitiveSizes; 21 | import com.datastax.oss.protocol.internal.ProtocolConstants; 22 | import com.datastax.oss.protocol.internal.response.Error; 23 | 24 | public class WriteTimeout extends Error { 25 | /** The consistency level of the query that triggered the exception. */ 26 | public final int consistencyLevel; 27 | /** The number of nodes having acknowledged the request. */ 28 | public final int received; 29 | /** 30 | * The number of replicas whose acknowledgment is required to achieve {@code consistencyLevel}. 31 | */ 32 | public final int blockFor; 33 | /** The type of the write that timed out. */ 34 | public final String writeType; 35 | 36 | public WriteTimeout( 37 | String message, int consistencyLevel, int received, int blockFor, String writeType) { 38 | super(ProtocolConstants.ErrorCode.WRITE_TIMEOUT, message); 39 | this.consistencyLevel = consistencyLevel; 40 | this.received = received; 41 | this.blockFor = blockFor; 42 | this.writeType = writeType; 43 | } 44 | 45 | public static class SubCodec extends Error.SubCodec { 46 | public SubCodec(int protocolVersion) { 47 | super(ProtocolConstants.ErrorCode.WRITE_TIMEOUT, protocolVersion); 48 | } 49 | 50 | @Override 51 | public void encode(B dest, Message message, PrimitiveCodec encoder) { 52 | WriteTimeout writeTimeout = (WriteTimeout) message; 53 | encoder.writeString(writeTimeout.message, dest); 54 | encoder.writeUnsignedShort(writeTimeout.consistencyLevel, dest); 55 | encoder.writeInt(writeTimeout.received, dest); 56 | encoder.writeInt(writeTimeout.blockFor, dest); 57 | encoder.writeString(writeTimeout.writeType, dest); 58 | } 59 | 60 | @Override 61 | public int encodedSize(Message message) { 62 | WriteTimeout writeTimeout = (WriteTimeout) message; 63 | return PrimitiveSizes.sizeOfString(writeTimeout.message) 64 | + PrimitiveSizes.SHORT 65 | + PrimitiveSizes.INT 66 | + PrimitiveSizes.INT 67 | + PrimitiveSizes.sizeOfString(writeTimeout.writeType); 68 | } 69 | 70 | @Override 71 | public Message decode(B source, PrimitiveCodec decoder) { 72 | String message = decoder.readString(source); 73 | int consistencyLevel = decoder.readUnsignedShort(source); 74 | int received = decoder.readInt(source); 75 | int blockFor = decoder.readInt(source); 76 | String writeType = decoder.readString(source); 77 | return new WriteTimeout(message, consistencyLevel, received, blockFor, writeType); 78 | } 79 | } 80 | } 81 | -------------------------------------------------------------------------------- /src/main/java/com/datastax/oss/protocol/internal/response/event/StatusChangeEvent.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 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.oss.protocol.internal.response.event; 17 | 18 | import com.datastax.oss.protocol.internal.Message; 19 | import com.datastax.oss.protocol.internal.PrimitiveCodec; 20 | import com.datastax.oss.protocol.internal.PrimitiveSizes; 21 | import com.datastax.oss.protocol.internal.ProtocolConstants; 22 | import com.datastax.oss.protocol.internal.response.Event; 23 | import java.net.InetSocketAddress; 24 | 25 | public class StatusChangeEvent extends Event { 26 | 27 | /** @see ProtocolConstants.StatusChangeType */ 28 | public final String changeType; 29 | 30 | public final InetSocketAddress address; 31 | 32 | public StatusChangeEvent(String changeType, InetSocketAddress address) { 33 | super(ProtocolConstants.EventType.STATUS_CHANGE); 34 | this.changeType = changeType; 35 | this.address = address; 36 | } 37 | 38 | @Override 39 | public String toString() { 40 | return String.format("EVENT STATUS_CHANGE(%s %s)", changeType, address); 41 | } 42 | 43 | public static class SubCodec extends Event.SubCodec { 44 | public SubCodec(int protocolVersion) { 45 | super(ProtocolConstants.EventType.STATUS_CHANGE, protocolVersion); 46 | } 47 | 48 | @Override 49 | public void encode(B dest, Message message, PrimitiveCodec encoder) { 50 | StatusChangeEvent event = (StatusChangeEvent) message; 51 | encoder.writeString(event.changeType, dest); 52 | encoder.writeInet(event.address, dest); 53 | } 54 | 55 | @Override 56 | public int encodedSize(Message message) { 57 | StatusChangeEvent event = (StatusChangeEvent) message; 58 | return PrimitiveSizes.sizeOfString(event.changeType) 59 | + PrimitiveSizes.sizeOfInet(event.address); 60 | } 61 | 62 | @Override 63 | public Message decode(B source, PrimitiveCodec decoder) { 64 | String changeType = decoder.readString(source); 65 | InetSocketAddress address = decoder.readInet(source); 66 | return new StatusChangeEvent(changeType, address); 67 | } 68 | } 69 | } 70 | -------------------------------------------------------------------------------- /src/main/java/com/datastax/oss/protocol/internal/response/event/TopologyChangeEvent.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 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.oss.protocol.internal.response.event; 17 | 18 | import com.datastax.oss.protocol.internal.Message; 19 | import com.datastax.oss.protocol.internal.PrimitiveCodec; 20 | import com.datastax.oss.protocol.internal.PrimitiveSizes; 21 | import com.datastax.oss.protocol.internal.ProtocolConstants; 22 | import com.datastax.oss.protocol.internal.response.Event; 23 | import java.net.InetSocketAddress; 24 | 25 | public class TopologyChangeEvent extends Event { 26 | 27 | /** @see ProtocolConstants.TopologyChangeType */ 28 | public final String changeType; 29 | 30 | public final InetSocketAddress address; 31 | 32 | public TopologyChangeEvent(String changeType, InetSocketAddress address) { 33 | super(ProtocolConstants.EventType.TOPOLOGY_CHANGE); 34 | this.changeType = changeType; 35 | this.address = address; 36 | } 37 | 38 | @Override 39 | public String toString() { 40 | return String.format("EVENT TOPOLOGY_CHANGE(%s %s)", changeType, address); 41 | } 42 | 43 | public static class SubCodec extends Event.SubCodec { 44 | public SubCodec(int protocolVersion) { 45 | super(ProtocolConstants.EventType.TOPOLOGY_CHANGE, protocolVersion); 46 | } 47 | 48 | @Override 49 | public void encode(B dest, Message message, PrimitiveCodec encoder) { 50 | TopologyChangeEvent event = (TopologyChangeEvent) message; 51 | encoder.writeString(event.changeType, dest); 52 | encoder.writeInet(event.address, dest); 53 | } 54 | 55 | @Override 56 | public int encodedSize(Message message) { 57 | TopologyChangeEvent event = (TopologyChangeEvent) message; 58 | return PrimitiveSizes.sizeOfString(event.changeType) 59 | + PrimitiveSizes.sizeOfInet(event.address); 60 | } 61 | 62 | @Override 63 | public Message decode(B source, PrimitiveCodec decoder) { 64 | String changeType = decoder.readString(source); 65 | InetSocketAddress address = decoder.readInet(source); 66 | return new TopologyChangeEvent(changeType, address); 67 | } 68 | } 69 | } 70 | -------------------------------------------------------------------------------- /src/main/java/com/datastax/oss/protocol/internal/response/result/ColumnSpec.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 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.oss.protocol.internal.response.result; 17 | 18 | import java.util.Objects; 19 | 20 | public class ColumnSpec { 21 | public final String ksName; 22 | public final String tableName; 23 | public final String name; 24 | public final int index; 25 | public final RawType type; 26 | 27 | /** 28 | * @param index the position of the column. This is provided for convenience if a decoding client 29 | * needs to reorder the specs (for example index them by name). For encoding, it is ignored. 30 | */ 31 | public ColumnSpec(String ksName, String tableName, String name, int index, RawType type) { 32 | this.ksName = ksName; 33 | this.tableName = tableName; 34 | this.name = name; 35 | this.index = index; 36 | this.type = type; 37 | } 38 | 39 | @Override 40 | public boolean equals(Object other) { 41 | if (other == this) { 42 | return true; 43 | } else if (other instanceof ColumnSpec) { 44 | ColumnSpec that = (ColumnSpec) other; 45 | return Objects.equals(this.ksName, that.ksName) 46 | && Objects.equals(this.tableName, that.tableName) 47 | && Objects.equals(this.name, that.name) 48 | && Objects.equals(this.type, that.type); 49 | } else { 50 | return false; 51 | } 52 | } 53 | 54 | @Override 55 | public int hashCode() { 56 | return Objects.hash(ksName, tableName, name, type); 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /src/main/java/com/datastax/oss/protocol/internal/response/result/DefaultRows.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 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.oss.protocol.internal.response.result; 17 | 18 | import com.datastax.oss.protocol.internal.Message; 19 | import com.datastax.oss.protocol.internal.PrimitiveCodec; 20 | import com.datastax.oss.protocol.internal.PrimitiveSizes; 21 | import com.datastax.oss.protocol.internal.ProtocolConstants; 22 | import com.datastax.oss.protocol.internal.response.Result; 23 | import com.datastax.oss.protocol.internal.util.collection.NullAllowingImmutableList; 24 | import java.nio.ByteBuffer; 25 | import java.util.ArrayDeque; 26 | import java.util.List; 27 | import java.util.Queue; 28 | 29 | public class DefaultRows extends Rows { 30 | private final RowsMetadata metadata; 31 | private final Queue> data; 32 | 33 | public DefaultRows(RowsMetadata metadata, Queue> data) { 34 | this.metadata = metadata; 35 | this.data = data; 36 | } 37 | 38 | @Override 39 | public RowsMetadata getMetadata() { 40 | return metadata; 41 | } 42 | 43 | @Override 44 | public Queue> getData() { 45 | return data; 46 | } 47 | 48 | @Override 49 | public String toString() { 50 | return "ROWS(" + data.size() + " x " + metadata.columnCount + " columns)"; 51 | } 52 | 53 | public static class SubCodec extends Result.SubCodec { 54 | public SubCodec(int protocolVersion) { 55 | super(ProtocolConstants.ResultKind.ROWS, protocolVersion); 56 | } 57 | 58 | @Override 59 | public void encode(B dest, Message message, PrimitiveCodec encoder) { 60 | DefaultRows rows = (DefaultRows) message; 61 | rows.metadata.encode(dest, encoder, false, protocolVersion); 62 | encoder.writeInt(rows.data.size(), dest); 63 | for (List row : rows.data) { 64 | for (ByteBuffer column : row) { 65 | encoder.writeBytes(column, dest); 66 | } 67 | } 68 | } 69 | 70 | @Override 71 | public int encodedSize(Message message) { 72 | DefaultRows rows = (DefaultRows) message; 73 | int size = rows.metadata.encodedSize(false, protocolVersion) + PrimitiveSizes.INT; 74 | for (List row : rows.data) { 75 | for (ByteBuffer column : row) { 76 | size += PrimitiveSizes.sizeOfBytes(column); 77 | } 78 | } 79 | return size; 80 | } 81 | 82 | @Override 83 | public Message decode(B source, PrimitiveCodec decoder) { 84 | RowsMetadata metadata = RowsMetadata.decode(source, decoder, false, protocolVersion); 85 | int rowCount = decoder.readInt(source); 86 | 87 | Queue> data = new ArrayDeque<>(rowCount); 88 | for (int i = 0; i < rowCount; i++) { 89 | NullAllowingImmutableList.Builder row = 90 | NullAllowingImmutableList.builder(metadata.columnCount); 91 | for (int j = 0; j < metadata.columnCount; j++) { 92 | row.add(decoder.readBytes(source)); 93 | } 94 | data.add(row.build()); 95 | } 96 | 97 | return new DefaultRows(metadata, data); 98 | } 99 | } 100 | } 101 | -------------------------------------------------------------------------------- /src/main/java/com/datastax/oss/protocol/internal/response/result/Rows.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 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.oss.protocol.internal.response.result; 17 | 18 | import com.datastax.oss.protocol.internal.ProtocolConstants; 19 | import com.datastax.oss.protocol.internal.response.Result; 20 | import java.nio.ByteBuffer; 21 | import java.util.List; 22 | import java.util.Queue; 23 | 24 | public abstract class Rows extends Result { 25 | 26 | public abstract RowsMetadata getMetadata(); 27 | 28 | public abstract Queue> getData(); 29 | 30 | protected Rows() { 31 | super(ProtocolConstants.ResultKind.ROWS); 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /src/main/java/com/datastax/oss/protocol/internal/response/result/SetKeyspace.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 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.oss.protocol.internal.response.result; 17 | 18 | import com.datastax.oss.protocol.internal.Message; 19 | import com.datastax.oss.protocol.internal.PrimitiveCodec; 20 | import com.datastax.oss.protocol.internal.PrimitiveSizes; 21 | import com.datastax.oss.protocol.internal.ProtocolConstants; 22 | import com.datastax.oss.protocol.internal.response.Result; 23 | 24 | public class SetKeyspace extends Result { 25 | public final String keyspace; 26 | 27 | public SetKeyspace(String keyspace) { 28 | super(ProtocolConstants.ResultKind.SET_KEYSPACE); 29 | this.keyspace = keyspace; 30 | } 31 | 32 | @Override 33 | public String toString() { 34 | return "SET_KEYSPACE(" + keyspace + ')'; 35 | } 36 | 37 | public static class SubCodec extends Result.SubCodec { 38 | public SubCodec(int protocolVersion) { 39 | super(ProtocolConstants.ResultKind.SET_KEYSPACE, protocolVersion); 40 | } 41 | 42 | @Override 43 | public void encode(B dest, Message message, PrimitiveCodec encoder) { 44 | SetKeyspace setKeyspace = (SetKeyspace) message; 45 | encoder.writeString(setKeyspace.keyspace, dest); 46 | } 47 | 48 | @Override 49 | public int encodedSize(Message message) { 50 | SetKeyspace setKeyspace = (SetKeyspace) message; 51 | return PrimitiveSizes.sizeOfString(setKeyspace.keyspace); 52 | } 53 | 54 | @Override 55 | public Message decode(B source, PrimitiveCodec decoder) { 56 | String keyspace = decoder.readString(source); 57 | return new SetKeyspace(keyspace); 58 | } 59 | } 60 | } 61 | -------------------------------------------------------------------------------- /src/main/java/com/datastax/oss/protocol/internal/response/result/Void.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 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.oss.protocol.internal.response.result; 17 | 18 | import com.datastax.oss.protocol.internal.Message; 19 | import com.datastax.oss.protocol.internal.PrimitiveCodec; 20 | import com.datastax.oss.protocol.internal.ProtocolConstants; 21 | import com.datastax.oss.protocol.internal.response.Result; 22 | 23 | @SuppressWarnings("JavaLangClash") 24 | public class Void extends Result { 25 | public static final Void INSTANCE = new Void(); 26 | 27 | private Void() { 28 | super(ProtocolConstants.ResultKind.VOID); 29 | } 30 | 31 | @Override 32 | public String toString() { 33 | return "VOID"; 34 | } 35 | 36 | public static class SubCodec extends Result.SubCodec { 37 | public SubCodec(int protocolVersion) { 38 | super(ProtocolConstants.ResultKind.VOID, protocolVersion); 39 | } 40 | 41 | @Override 42 | public void encode(B dest, Message message, PrimitiveCodec encoder) {} 43 | 44 | @Override 45 | public int encodedSize(Message message) { 46 | return 0; 47 | } 48 | 49 | @Override 50 | public Message decode(B source, PrimitiveCodec decoder) { 51 | return INSTANCE; 52 | } 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /src/main/java/com/datastax/oss/protocol/internal/util/Flags.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 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.oss.protocol.internal.util; 17 | 18 | public class Flags { 19 | 20 | public static boolean contains(int flags, int mask) { 21 | return (flags & mask) == mask; 22 | } 23 | 24 | public static int add(int flags, int mask) { 25 | return flags | mask; 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /src/main/java/com/datastax/oss/protocol/internal/util/IntIntMap.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 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.oss.protocol.internal.util; 17 | 18 | import java.util.HashMap; 19 | import java.util.Map; 20 | 21 | /** An {@link IntMap} containing other {@code IntMap}s. */ 22 | public class IntIntMap { 23 | 24 | public static Builder builder() { 25 | return new Builder<>(); 26 | } 27 | 28 | private final IntMap> outer; 29 | 30 | private IntIntMap(IntMap> outer) { 31 | this.outer = outer; 32 | } 33 | 34 | public V get(int key1, int key2) { 35 | IntMap inner = outer.get(key1); 36 | return (inner == null) ? null : inner.get(key2); 37 | } 38 | 39 | public static class Builder { 40 | private Map> innerBuilders = new HashMap<>(); 41 | 42 | public Builder put(int key1, int key2, V value) { 43 | innerBuilders.computeIfAbsent(key1, k -> IntMap.builder()).put(key2, value); 44 | return this; 45 | } 46 | 47 | public IntIntMap build() { 48 | IntMap.Builder> outerBuilder = IntMap.builder(); 49 | for (Map.Entry> entry : innerBuilders.entrySet()) { 50 | outerBuilder.put(entry.getKey(), entry.getValue().build()); 51 | } 52 | return new IntIntMap<>(outerBuilder.build()); 53 | } 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /src/main/java/com/datastax/oss/protocol/internal/util/IntMap.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 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.oss.protocol.internal.util; 17 | 18 | import java.util.HashMap; 19 | import java.util.Map; 20 | import java.util.Set; 21 | 22 | /** 23 | * Barebones map-like structure with positive integer keys. It is immutable and optimized for low 24 | * cardinalities (entries are stored in a sparse array). 25 | */ 26 | public class IntMap { 27 | 28 | public static Builder builder() { 29 | return new Builder<>(); 30 | } 31 | 32 | private final Object[] values; 33 | 34 | private IntMap(Set> entries) { 35 | int maxKey = -1; 36 | for (Map.Entry entry : entries) { 37 | maxKey = Math.max(maxKey, entry.getKey()); 38 | } 39 | this.values = new Object[maxKey + 1]; 40 | for (Map.Entry entry : entries) { 41 | this.values[entry.getKey()] = entry.getValue(); 42 | } 43 | } 44 | 45 | public V get(int key) { 46 | if (key < 0) { 47 | throw new IllegalArgumentException("key must be positive"); 48 | } 49 | if (key >= values.length) { 50 | return null; 51 | } else { 52 | @SuppressWarnings("unchecked") 53 | V value = (V) values[key]; 54 | return value; 55 | } 56 | } 57 | 58 | public static class Builder { 59 | private Map map = new HashMap<>(); 60 | 61 | public Builder put(int key, V value) { 62 | if (key < 0) { 63 | throw new IllegalArgumentException("key must be positive: " + key); 64 | } 65 | if (map.containsKey(key)) { 66 | throw new IllegalArgumentException("key already exists: " + key); 67 | } 68 | map.put(key, value); 69 | return this; 70 | } 71 | 72 | public IntMap build() { 73 | return new IntMap<>(map.entrySet()); 74 | } 75 | } 76 | } 77 | -------------------------------------------------------------------------------- /src/test/java/com/datastax/dse/protocol/internal/DseTestDataProviders.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 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.dse.protocol.internal; 17 | 18 | import com.datastax.oss.protocol.internal.TestDataProviders; 19 | import com.tngtech.java.junit.dataprovider.DataProvider; 20 | import java.util.ArrayList; 21 | import java.util.List; 22 | 23 | public class DseTestDataProviders { 24 | @DataProvider 25 | public static Object[][] protocolDseV1OrAbove() { 26 | return protocolVersions(null, null); 27 | } 28 | 29 | @DataProvider 30 | public static Object[][] protocolDseV2OrAbove() { 31 | return protocolVersions(DseProtocolConstants.Version.DSE_V2, null); 32 | } 33 | 34 | /** 35 | * @param min inclusive 36 | * @param max inclusive 37 | */ 38 | private static Object[][] protocolVersions(Integer min, Integer max) { 39 | if (min == null) { 40 | min = DseProtocolConstants.Version.MIN; 41 | } 42 | if (max == null) { 43 | max = Math.max(DseProtocolConstants.Version.MAX, DseProtocolConstants.Version.BETA); 44 | } 45 | List l = new ArrayList<>(); 46 | for (int i = min; i <= max; i++) { 47 | l.add(i); 48 | } 49 | return TestDataProviders.fromList(l); 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /src/test/java/com/datastax/dse/protocol/internal/request/RawBytesQueryTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 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.dse.protocol.internal.request; 17 | 18 | import static org.assertj.core.api.Assertions.assertThat; 19 | 20 | import com.datastax.dse.protocol.internal.DseTestDataProviders; 21 | import com.datastax.dse.protocol.internal.request.query.DseQueryOptions; 22 | import com.datastax.dse.protocol.internal.request.query.DseQueryOptionsBuilder; 23 | import com.datastax.oss.protocol.internal.Message; 24 | import com.datastax.oss.protocol.internal.MessageTestBase; 25 | import com.datastax.oss.protocol.internal.PrimitiveSizes; 26 | import com.datastax.oss.protocol.internal.ProtocolConstants; 27 | import com.datastax.oss.protocol.internal.binary.MockBinaryString; 28 | import com.datastax.oss.protocol.internal.util.Bytes; 29 | import com.tngtech.java.junit.dataprovider.DataProviderRunner; 30 | import com.tngtech.java.junit.dataprovider.UseDataProvider; 31 | import java.nio.charset.Charset; 32 | import org.junit.Test; 33 | import org.junit.runner.RunWith; 34 | 35 | @RunWith(DataProviderRunner.class) 36 | public class RawBytesQueryTest extends MessageTestBase { 37 | 38 | private String queryString = "select * from system.local"; 39 | 40 | public RawBytesQueryTest() { 41 | super(RawBytesQuery.class); 42 | } 43 | 44 | @Override 45 | protected Message.Codec newCodec(int protocolVersion) { 46 | return new DseQueryCodec(protocolVersion); 47 | } 48 | 49 | @Test 50 | @UseDataProvider(location = DseTestDataProviders.class, value = "protocolDseV1OrAbove") 51 | public void should_encode(int protocolVersion) { 52 | DseQueryOptions queryOptions = new DseQueryOptionsBuilder().build(); 53 | byte[] bytes = queryString.getBytes(Charset.forName("UTF-8")); 54 | RawBytesQuery initial = new RawBytesQuery(bytes, queryOptions); 55 | 56 | MockBinaryString encoded = encode(initial, protocolVersion); 57 | 58 | assertThat(encoded) 59 | .isEqualTo( 60 | new MockBinaryString() 61 | .bytes(Bytes.toHexString(bytes)) 62 | .unsignedShort(ProtocolConstants.ConsistencyLevel.ONE) 63 | .int_(0x00) // no flags 64 | ); 65 | assertThat(encodedSize(initial, protocolVersion)) 66 | .isEqualTo((PrimitiveSizes.INT + bytes.length) + PrimitiveSizes.SHORT + PrimitiveSizes.INT); 67 | 68 | // The codec always decodes as a regular Query with DseQueryOptions, so do not cover that again 69 | } 70 | } 71 | -------------------------------------------------------------------------------- /src/test/java/com/datastax/dse/protocol/internal/request/ReviseTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 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.dse.protocol.internal.request; 17 | 18 | import static org.assertj.core.api.Assertions.assertThat; 19 | 20 | import com.datastax.dse.protocol.internal.DseProtocolConstants; 21 | import com.datastax.dse.protocol.internal.DseTestDataProviders; 22 | import com.datastax.oss.protocol.internal.Message; 23 | import com.datastax.oss.protocol.internal.MessageTestBase; 24 | import com.datastax.oss.protocol.internal.PrimitiveSizes; 25 | import com.datastax.oss.protocol.internal.binary.MockBinaryString; 26 | import com.tngtech.java.junit.dataprovider.DataProviderRunner; 27 | import com.tngtech.java.junit.dataprovider.UseDataProvider; 28 | import org.junit.Test; 29 | import org.junit.runner.RunWith; 30 | 31 | @RunWith(DataProviderRunner.class) 32 | public class ReviseTest extends MessageTestBase { 33 | 34 | public ReviseTest() { 35 | super(Revise.class); 36 | } 37 | 38 | @Override 39 | protected Message.Codec newCodec(int protocolVersion) { 40 | return new Revise.Codec(protocolVersion); 41 | } 42 | 43 | @Test 44 | @UseDataProvider(location = DseTestDataProviders.class, value = "protocolDseV1OrAbove") 45 | public void should_encode_and_decode_cancel_continuous_paging(int protocolVersion) { 46 | Revise initial = Revise.cancelContinuousPaging(42); 47 | MockBinaryString encoded = encode(initial, protocolVersion); 48 | 49 | assertThat(encoded) 50 | .isEqualTo( 51 | new MockBinaryString() 52 | .int_(DseProtocolConstants.RevisionType.CANCEL_CONTINUOUS_PAGING) 53 | .int_(42)); 54 | assertThat(encodedSize(initial, protocolVersion)).isEqualTo(PrimitiveSizes.INT * 2); 55 | 56 | Revise decoded = decode(encoded, protocolVersion); 57 | 58 | assertThat(decoded.revisionType) 59 | .isEqualTo(DseProtocolConstants.RevisionType.CANCEL_CONTINUOUS_PAGING); 60 | assertThat(decoded.streamId).isEqualTo(42); 61 | assertThat(decoded.nextPages).isEqualTo(-1); 62 | } 63 | 64 | @Test 65 | @UseDataProvider(location = DseTestDataProviders.class, value = "protocolDseV2OrAbove") 66 | public void should_encode_and_decode_request_for_more_pages_in_dse_v2_and_above( 67 | int protocolVersion) { 68 | Revise initial = Revise.requestMoreContinuousPages(42, 10); 69 | MockBinaryString encoded = encode(initial, protocolVersion); 70 | 71 | assertThat(encoded) 72 | .isEqualTo( 73 | new MockBinaryString() 74 | .int_(DseProtocolConstants.RevisionType.MORE_CONTINUOUS_PAGES) 75 | .int_(42) 76 | .int_(10)); 77 | assertThat(encodedSize(initial, protocolVersion)).isEqualTo(PrimitiveSizes.INT * 3); 78 | 79 | Revise decoded = decode(encoded, protocolVersion); 80 | 81 | assertThat(decoded.revisionType) 82 | .isEqualTo(DseProtocolConstants.RevisionType.MORE_CONTINUOUS_PAGES); 83 | assertThat(decoded.streamId).isEqualTo(42); 84 | assertThat(decoded.nextPages).isEqualTo(10); 85 | } 86 | } 87 | -------------------------------------------------------------------------------- /src/test/java/com/datastax/dse/protocol/internal/request/query/DseQueryOptionsBuilder.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 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.dse.protocol.internal.request.query; 17 | 18 | import com.datastax.oss.protocol.internal.request.query.QueryOptionsBuilderBase; 19 | 20 | public class DseQueryOptionsBuilder 21 | extends QueryOptionsBuilderBase { 22 | 23 | protected boolean isPageSizeInBytes; 24 | protected boolean hasContinuousPagingOptions; 25 | protected int maxPages; 26 | protected int pagesPerSecond; 27 | protected int nextPages; 28 | 29 | public DseQueryOptionsBuilder withPageSizeInBytes() { 30 | this.isPageSizeInBytes = true; 31 | this.hasContinuousPagingOptions = true; 32 | return this; 33 | } 34 | 35 | public DseQueryOptionsBuilder withMaxPages(int maxPages) { 36 | this.maxPages = maxPages; 37 | this.hasContinuousPagingOptions = true; 38 | return this; 39 | } 40 | 41 | public DseQueryOptionsBuilder withPagesPerSecond(int pagesPerSecond) { 42 | this.pagesPerSecond = pagesPerSecond; 43 | this.hasContinuousPagingOptions = true; 44 | return this; 45 | } 46 | 47 | public DseQueryOptionsBuilder withNextPages(int nextPages) { 48 | this.nextPages = nextPages; 49 | return this; 50 | } 51 | 52 | @Override 53 | public DseQueryOptions build() { 54 | return new DseQueryOptions( 55 | consistency, 56 | positionalValues, 57 | namedValues, 58 | skipMetadata, 59 | pageSize, 60 | pagingState, 61 | serialConsistency, 62 | defaultTimestamp, 63 | keyspace, 64 | isPageSizeInBytes, 65 | hasContinuousPagingOptions 66 | ? new ContinuousPagingOptions(maxPages, pagesPerSecond, nextPages) 67 | : null); 68 | } 69 | } 70 | -------------------------------------------------------------------------------- /src/test/java/com/datastax/dse/protocol/internal/response/DseErrorTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 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.dse.protocol.internal.response; 17 | 18 | import static com.datastax.dse.protocol.internal.DseProtocolConstants.ErrorCode.CLIENT_WRITE_FAILURE; 19 | import static org.assertj.core.api.Assertions.assertThat; 20 | 21 | import com.datastax.dse.protocol.internal.DseProtocolConstants.ErrorCode; 22 | import com.datastax.dse.protocol.internal.DseTestDataProviders; 23 | import com.datastax.oss.protocol.internal.Message; 24 | import com.datastax.oss.protocol.internal.MessageTestBase; 25 | import com.datastax.oss.protocol.internal.PrimitiveSizes; 26 | import com.datastax.oss.protocol.internal.binary.MockBinaryString; 27 | import com.datastax.oss.protocol.internal.response.Error; 28 | import com.datastax.oss.protocol.internal.response.Error.SingleMessageSubCodec; 29 | import com.tngtech.java.junit.dataprovider.DataProviderRunner; 30 | import com.tngtech.java.junit.dataprovider.UseDataProvider; 31 | import org.junit.Test; 32 | import org.junit.runner.RunWith; 33 | 34 | @RunWith(DataProviderRunner.class) 35 | public class DseErrorTest extends MessageTestBase { 36 | 37 | private static final String MOCK_MESSAGE = "mock message"; 38 | 39 | public DseErrorTest() { 40 | super(Error.class); 41 | } 42 | 43 | @Override 44 | protected Message.Codec newCodec(int protocolVersion) { 45 | return new Error.Codec( 46 | protocolVersion, new SingleMessageSubCodec(CLIENT_WRITE_FAILURE, protocolVersion)); 47 | } 48 | 49 | @Test 50 | @UseDataProvider(location = DseTestDataProviders.class, value = "protocolDseV1OrAbove") 51 | public void should_encode_and_decode_client_write_failure(int protocolVersion) { 52 | int errorCode = ErrorCode.CLIENT_WRITE_FAILURE; 53 | Error initial = new Error(errorCode, MOCK_MESSAGE); 54 | MockBinaryString encoded = encode(initial, protocolVersion); 55 | assertThat(encoded).isEqualTo(new MockBinaryString().int_(errorCode).string(MOCK_MESSAGE)); 56 | assertThat(encodedSize(initial, protocolVersion)) 57 | .isEqualTo(PrimitiveSizes.INT + (PrimitiveSizes.SHORT + MOCK_MESSAGE.length())); 58 | Error decoded = decode(encoded, protocolVersion); 59 | assertThat(decoded.code).isEqualTo(errorCode); 60 | assertThat(decoded.message).isEqualTo(MOCK_MESSAGE); 61 | } 62 | } 63 | -------------------------------------------------------------------------------- /src/test/java/com/datastax/oss/protocol/internal/Assertions.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 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.oss.protocol.internal; 17 | 18 | import com.datastax.oss.protocol.internal.response.result.Rows; 19 | import com.datastax.oss.protocol.internal.response.result.RowsMetadata; 20 | 21 | public class Assertions extends org.assertj.core.api.Assertions { 22 | 23 | public static RowsAssert assertThat(Rows rows) { 24 | return new RowsAssert(rows); 25 | } 26 | 27 | public static RowsMetadataAssert assertThat(RowsMetadata metadata) { 28 | return new RowsMetadataAssert(metadata); 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /src/test/java/com/datastax/oss/protocol/internal/FrameCodecTestBase.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 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.oss.protocol.internal; 17 | 18 | import com.datastax.oss.protocol.internal.util.Bytes; 19 | import com.datastax.oss.protocol.internal.util.collection.NullAllowingImmutableList; 20 | import java.nio.ByteBuffer; 21 | import java.util.Collections; 22 | import java.util.LinkedHashMap; 23 | import java.util.List; 24 | import java.util.Map; 25 | import java.util.UUID; 26 | 27 | public abstract class FrameCodecTestBase { 28 | protected static final int STREAM_ID = 2; 29 | protected static final UUID TRACING_ID = UUID.randomUUID(); 30 | protected static final Map SOME_PAYLOAD; 31 | protected static final List NO_WARNINGS = Collections.emptyList(); 32 | protected static final List SOME_WARNINGS = 33 | NullAllowingImmutableList.of("warning 1", "warning 2"); 34 | 35 | static { 36 | Map tmp = new LinkedHashMap<>(); 37 | tmp.put("foo", Bytes.fromHexString("0x0a")); 38 | tmp.put("bar", Bytes.fromHexString("0x0b")); 39 | SOME_PAYLOAD = Collections.unmodifiableMap(tmp); 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /src/test/java/com/datastax/oss/protocol/internal/MessageTestBase.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 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.oss.protocol.internal; 17 | 18 | import static org.assertj.core.api.Assertions.assertThat; 19 | 20 | import com.datastax.oss.protocol.internal.binary.MockBinaryString; 21 | import com.datastax.oss.protocol.internal.binary.MockPrimitiveCodec; 22 | 23 | public abstract class MessageTestBase { 24 | 25 | private final Class messageClass; 26 | 27 | protected MessageTestBase(Class messageClass) { 28 | this.messageClass = messageClass; 29 | } 30 | 31 | protected abstract Message.Codec newCodec(int protocolVersion); 32 | 33 | protected MockBinaryString encode(M message, int protocolVersion) { 34 | MockBinaryString dest = new MockBinaryString(); 35 | Message.Codec codec = newCodec(protocolVersion); 36 | assertThat(codec.opcode).isEqualTo(message.opcode); 37 | codec.encode(dest, message, MockPrimitiveCodec.INSTANCE); 38 | return dest; 39 | } 40 | 41 | protected int encodedSize(M message, int protocolVersion) { 42 | Message.Codec codec = newCodec(protocolVersion); 43 | return codec.encodedSize(message); 44 | } 45 | 46 | protected M decode(MockBinaryString source, int protocolVersion) { 47 | Message.Codec codec = newCodec(protocolVersion); 48 | Message message = codec.decode(source, MockPrimitiveCodec.INSTANCE); 49 | assertThat(message).isInstanceOf(messageClass); 50 | assertThat(message.opcode).isEqualTo(codec.opcode); 51 | return messageClass.cast(message); 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /src/test/java/com/datastax/oss/protocol/internal/PrimitiveSizesTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 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.oss.protocol.internal; 17 | 18 | import static org.assertj.core.api.Assertions.assertThat; 19 | 20 | import java.nio.charset.Charset; 21 | import java.nio.charset.StandardCharsets; 22 | import org.junit.Test; 23 | 24 | public class PrimitiveSizesTest { 25 | 26 | /** 27 | * Check that {@link PrimitiveSizes#encodedUTF8Length} returns the correct value for every 28 | * possible Unicode code point. 29 | * 30 | *

We compare with {@link String#getBytes(Charset)}, since that's how we encode strings in the 31 | * driver. 32 | */ 33 | @Test 34 | public void should_measure_size_of_encoded_codepoint() { 35 | // Basic Multilingual Plane 36 | // Note that this includes all surrogates (0xD800 => 0xDFFF), which are invalid as a single 37 | // character. String.getBytes encodes them as "?". 38 | for (char codePoint = 0; codePoint < Character.MAX_VALUE; codePoint++) { 39 | String s = new String(new char[] {codePoint}); 40 | assertThat(PrimitiveSizes.encodedUTF8Length(s)) 41 | .as(String.format("BMP %d", (int) codePoint)) 42 | .isEqualTo(s.getBytes(StandardCharsets.UTF_8).length); 43 | } 44 | // Other planes (require a surrogate pair) 45 | for (int codePoint = 0x10000; codePoint < 0x10FFFF; codePoint++) { 46 | char highSurrogate = Character.highSurrogate(codePoint); 47 | char lowSurrogate = Character.lowSurrogate(codePoint); 48 | String s = new String(new char[] {highSurrogate, lowSurrogate}); 49 | assertThat(PrimitiveSizes.encodedUTF8Length(s)) 50 | .as( 51 | String.format( 52 | "%d (high %d, low %d)", codePoint, (int) highSurrogate, (int) lowSurrogate)) 53 | .isEqualTo(s.getBytes(StandardCharsets.UTF_8).length); 54 | } 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /src/test/java/com/datastax/oss/protocol/internal/RowsAssert.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 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.oss.protocol.internal; 17 | 18 | import static org.assertj.core.api.Assertions.assertThat; 19 | 20 | import com.datastax.oss.protocol.internal.response.result.Rows; 21 | import com.datastax.oss.protocol.internal.util.Bytes; 22 | import java.nio.ByteBuffer; 23 | import java.util.List; 24 | import org.assertj.core.api.AbstractAssert; 25 | 26 | public class RowsAssert extends AbstractAssert { 27 | public RowsAssert(Rows actual) { 28 | super(actual, RowsAssert.class); 29 | } 30 | 31 | /** Note: this consumes the row */ 32 | public RowsAssert hasNextRow(String... columnHexStrings) { 33 | ByteBuffer[] buffers = new ByteBuffer[columnHexStrings.length]; 34 | for (int i = 0; i < columnHexStrings.length; i++) 35 | buffers[i] = Bytes.fromHexString(columnHexStrings[i]); 36 | 37 | List row = actual.getData().poll(); 38 | assertThat(row).containsExactly(buffers); 39 | return this; 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /src/test/java/com/datastax/oss/protocol/internal/RowsMetadataAssert.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 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.oss.protocol.internal; 17 | 18 | import static org.assertj.core.api.Assertions.assertThat; 19 | 20 | import com.datastax.oss.protocol.internal.response.result.ColumnSpec; 21 | import com.datastax.oss.protocol.internal.response.result.RowsMetadata; 22 | import com.datastax.oss.protocol.internal.util.Bytes; 23 | import java.util.List; 24 | import org.assertj.core.api.AbstractAssert; 25 | 26 | public class RowsMetadataAssert extends AbstractAssert { 27 | public RowsMetadataAssert(RowsMetadata actual) { 28 | super(actual, RowsMetadataAssert.class); 29 | } 30 | 31 | public RowsMetadataAssert hasPagingState(String expected) { 32 | assertThat(actual.pagingState).isEqualTo(Bytes.fromHexString(expected)); 33 | return this; 34 | } 35 | 36 | public RowsMetadataAssert hasNoPagingState() { 37 | assertThat(actual.pagingState).isNull(); 38 | return this; 39 | } 40 | 41 | public RowsMetadataAssert hasColumnSpecs(ColumnSpec... expected) { 42 | assertThat(actual.columnSpecs).containsExactly(expected); 43 | return this; 44 | } 45 | 46 | public RowsMetadataAssert hasColumnSpecs(List expected) { 47 | assertThat(actual.columnSpecs).isEqualTo(expected); 48 | return this; 49 | } 50 | 51 | public RowsMetadataAssert hasColumnCount(int expected) { 52 | assertThat(actual.columnCount).isEqualTo(expected); 53 | return this; 54 | } 55 | 56 | public RowsMetadataAssert hasNoColumnSpecs() { 57 | assertThat(actual.columnSpecs).isEmpty(); 58 | return this; 59 | } 60 | 61 | public RowsMetadataAssert hasPkIndices(int... pkIndices) { 62 | assertThat(actual.pkIndices).containsExactly(pkIndices); 63 | return this; 64 | } 65 | 66 | public RowsMetadataAssert hasNoPkIndices() { 67 | assertThat(actual.pkIndices).isNull(); 68 | return this; 69 | } 70 | 71 | public RowsMetadataAssert hasNewResultMetadataId(String expected) { 72 | assertThat(Bytes.toHexString(actual.newResultMetadataId)).isEqualTo(expected); 73 | return this; 74 | } 75 | 76 | public RowsMetadataAssert hasNoNewResultMetadataId() { 77 | assertThat(actual.newResultMetadataId).isNull(); 78 | return this; 79 | } 80 | } 81 | -------------------------------------------------------------------------------- /src/test/java/com/datastax/oss/protocol/internal/binary/MockCompressor.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 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.oss.protocol.internal.binary; 17 | 18 | import static org.assertj.core.api.Assertions.assertThat; 19 | 20 | import com.datastax.oss.protocol.internal.Compressor; 21 | import java.util.HashMap; 22 | import java.util.Map; 23 | 24 | public class MockCompressor implements Compressor { 25 | 26 | private final Map decompressedToCompressed = new HashMap<>(); 27 | private final Map compressedToDecompressed = new HashMap<>(); 28 | 29 | /** 30 | * "Primes" the given decompressed<->compressed bidirectional mapping. Future attempts to compress 31 | * or decompress the corresponding value will return the other value. 32 | * 33 | *

If {@code decompressed} was already primed, this method has no effect, and it returns the 34 | * previously associated compressed value (that is still in effect). {@code compressed} is 35 | * ignored. 36 | * 37 | *

Otherwise, the method returns {@code compressed}. 38 | * 39 | * @throws IllegalArgumentException if {@code compressed} was already used for another mapping. 40 | */ 41 | public MockBinaryString prime(MockBinaryString decompressed, MockBinaryString compressed) { 42 | if (decompressedToCompressed.containsKey(decompressed)) { 43 | return decompressedToCompressed.get(decompressed); 44 | } else if (compressedToDecompressed.containsKey(compressed)) { 45 | throw new IllegalArgumentException( 46 | String.format( 47 | "%s is already used as the compressed form of %s", 48 | compressed, compressedToDecompressed.get(compressed))); 49 | } else { 50 | decompressedToCompressed.put(decompressed, compressed); 51 | compressedToDecompressed.put(compressed, decompressed); 52 | return compressed; 53 | } 54 | } 55 | 56 | @Override 57 | public String algorithm() { 58 | return "MOCK"; 59 | } 60 | 61 | @Override 62 | public MockBinaryString compress(MockBinaryString uncompressed) { 63 | MockBinaryString compressed = decompressedToCompressed.get(uncompressed); 64 | if (compressed == null) { 65 | throw new IllegalStateException( 66 | String.format("Unknown uncompressed input %s, must be primed first", uncompressed)); 67 | } 68 | return compressed.copy(); 69 | } 70 | 71 | @Override 72 | public MockBinaryString decompress(MockBinaryString compressed) { 73 | MockBinaryString decompressed = compressedToDecompressed.get(compressed); 74 | if (decompressed == null) { 75 | throw new IllegalStateException( 76 | String.format("Unknown compressed input %s, must be primed first", compressed)); 77 | } 78 | return decompressed.copy(); 79 | } 80 | 81 | @Override 82 | public MockBinaryString compressWithoutLength(MockBinaryString uncompressed) { 83 | // The two sets of methods are used in different contexts, for tests it doesn't matter if they 84 | // use the same implementation 85 | return compress(uncompressed); 86 | } 87 | 88 | @Override 89 | public MockBinaryString decompressWithoutLength( 90 | MockBinaryString compressed, int uncompressedLength) { 91 | MockBinaryString uncompressed = decompress(compressed); 92 | assertThat(uncompressed.size()).isEqualTo(uncompressedLength); 93 | return uncompressed; 94 | } 95 | } 96 | -------------------------------------------------------------------------------- /src/test/java/com/datastax/oss/protocol/internal/request/AuthResponseTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 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.oss.protocol.internal.request; 17 | 18 | import static com.datastax.oss.protocol.internal.Assertions.assertThat; 19 | 20 | import com.datastax.oss.protocol.internal.*; 21 | import com.datastax.oss.protocol.internal.binary.MockBinaryString; 22 | import com.datastax.oss.protocol.internal.util.Bytes; 23 | import com.tngtech.java.junit.dataprovider.DataProviderRunner; 24 | import com.tngtech.java.junit.dataprovider.UseDataProvider; 25 | import java.nio.ByteBuffer; 26 | import org.junit.Test; 27 | import org.junit.runner.RunWith; 28 | 29 | @RunWith(DataProviderRunner.class) 30 | public class AuthResponseTest extends MessageTestBase { 31 | 32 | public AuthResponseTest() { 33 | super(AuthResponse.class); 34 | } 35 | 36 | @Override 37 | protected Message.Codec newCodec(int protocolVersion) { 38 | return new AuthResponse.Codec(protocolVersion); 39 | } 40 | 41 | @Test 42 | @UseDataProvider(location = TestDataProviders.class, value = "protocolV3OrAbove") 43 | public void should_encode_and_decode(int protocolVersion) { 44 | ByteBuffer token = Bytes.fromHexString("0xcafebabe"); 45 | byte[] tokenBytes = token.array(); 46 | AuthResponse initial = new AuthResponse(token); 47 | 48 | int encodedSize = encodedSize(initial, protocolVersion); 49 | MockBinaryString encoded = encode(initial, protocolVersion); 50 | 51 | assertThat(encoded).isEqualTo(new MockBinaryString().bytes("0xcafebabe")); 52 | assertThat(encodedSize).isEqualTo(PrimitiveSizes.INT + "cafebabe".length() / 2); 53 | 54 | // Check that the token was consumed, and the contents were cleared 55 | assertThat(token.hasRemaining()).isFalse(); 56 | assertThat(tokenBytes).containsOnly(0); 57 | 58 | AuthResponse decoded = decode(encoded, protocolVersion); 59 | 60 | assertThat(Bytes.toHexString(decoded.token)).isEqualTo("0xcafebabe"); 61 | } 62 | 63 | @Test 64 | public void should_not_attempt_to_clear_token_if_read_only() { 65 | ByteBuffer token = Bytes.fromHexString("0xcafebabe"); 66 | byte[] tokenBytes = token.array(); 67 | 68 | encode(new AuthResponse(token.asReadOnlyBuffer()), ProtocolConstants.Version.V4); 69 | 70 | // Check that the contents are still intact 71 | assertThat(tokenBytes).containsExactly(0xca, 0xfe, 0xba, 0xbe); 72 | } 73 | 74 | @Test 75 | @UseDataProvider(location = TestDataProviders.class, value = "protocolV3OrAbove") 76 | public void should_encode_and_decode_null_token(int protocolVersion) { 77 | AuthResponse initial = new AuthResponse(null); 78 | int encodedSize = encodedSize(initial, protocolVersion); 79 | MockBinaryString encoded = encode(initial, protocolVersion); 80 | assertThat(encoded).isEqualTo(new MockBinaryString().bytes("0x")); 81 | assertThat(encodedSize).isEqualTo(PrimitiveSizes.INT); 82 | AuthResponse decoded = decode(encoded, protocolVersion); 83 | assertThat(Bytes.toHexString(decoded.token)).isEqualTo("0x"); 84 | } 85 | } 86 | -------------------------------------------------------------------------------- /src/test/java/com/datastax/oss/protocol/internal/request/OptionsTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 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.oss.protocol.internal.request; 17 | 18 | import static com.datastax.oss.protocol.internal.Assertions.assertThat; 19 | 20 | import com.datastax.oss.protocol.internal.Message; 21 | import com.datastax.oss.protocol.internal.MessageTestBase; 22 | import com.datastax.oss.protocol.internal.TestDataProviders; 23 | import com.datastax.oss.protocol.internal.binary.MockBinaryString; 24 | import com.tngtech.java.junit.dataprovider.DataProviderRunner; 25 | import com.tngtech.java.junit.dataprovider.UseDataProvider; 26 | import org.junit.Test; 27 | import org.junit.runner.RunWith; 28 | 29 | @RunWith(DataProviderRunner.class) 30 | public class OptionsTest extends MessageTestBase { 31 | 32 | public OptionsTest() { 33 | super(Options.class); 34 | } 35 | 36 | @Override 37 | protected Message.Codec newCodec(int protocolVersion) { 38 | return new Options.Codec(protocolVersion); 39 | } 40 | 41 | @Test 42 | @UseDataProvider(location = TestDataProviders.class, value = "protocolV3OrAbove") 43 | public void should_encode_as_empty_message(int protocolVersion) { 44 | assertThat(encode(Options.INSTANCE, protocolVersion)).isEqualTo(new MockBinaryString()); 45 | assertThat(encodedSize(Options.INSTANCE, protocolVersion)).isEqualTo(0); 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /src/test/java/com/datastax/oss/protocol/internal/request/PrepareTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 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.oss.protocol.internal.request; 17 | 18 | import static com.datastax.oss.protocol.internal.Assertions.assertThat; 19 | 20 | import com.datastax.oss.protocol.internal.Message; 21 | import com.datastax.oss.protocol.internal.MessageTestBase; 22 | import com.datastax.oss.protocol.internal.PrimitiveSizes; 23 | import com.datastax.oss.protocol.internal.TestDataProviders; 24 | import com.datastax.oss.protocol.internal.binary.MockBinaryString; 25 | import com.tngtech.java.junit.dataprovider.DataProviderRunner; 26 | import com.tngtech.java.junit.dataprovider.UseDataProvider; 27 | import org.junit.Test; 28 | import org.junit.runner.RunWith; 29 | 30 | @RunWith(DataProviderRunner.class) 31 | public class PrepareTest extends MessageTestBase { 32 | 33 | public PrepareTest() { 34 | super(Prepare.class); 35 | } 36 | 37 | @Override 38 | protected Message.Codec newCodec(int protocolVersion) { 39 | return new Prepare.Codec(protocolVersion); 40 | } 41 | 42 | @Test 43 | @UseDataProvider(location = TestDataProviders.class, value = "protocolV3OrV4") 44 | public void should_encode_and_decode_in_protocol_v3_or_v4(int protocolVersion) { 45 | Prepare initial = new Prepare("SELECT * FROM foo"); 46 | 47 | MockBinaryString encoded = encode(initial, protocolVersion); 48 | 49 | assertThat(encoded).isEqualTo(new MockBinaryString().longString("SELECT * FROM foo")); 50 | assertThat(encodedSize(initial, protocolVersion)) 51 | .isEqualTo(PrimitiveSizes.INT + "SELECT * FROM foo".length()); 52 | 53 | Prepare decoded = decode(encoded, protocolVersion); 54 | 55 | assertThat(decoded.cqlQuery).isEqualTo(initial.cqlQuery); 56 | } 57 | 58 | @Test 59 | @UseDataProvider(location = TestDataProviders.class, value = "protocolV5OrAbove") 60 | public void should_encode_and_decode_with_keyspace_in_protocol_v5_or_above(int protocolVersion) { 61 | Prepare initial = new Prepare("SELECT * FROM foo", "ks"); 62 | 63 | MockBinaryString encoded = encode(initial, protocolVersion); 64 | 65 | assertThat(encoded) 66 | .isEqualTo(new MockBinaryString().longString("SELECT * FROM foo").int_(0x01).string("ks")); 67 | assertThat(encodedSize(initial, protocolVersion)) 68 | .isEqualTo( 69 | (PrimitiveSizes.INT + "SELECT * FROM foo".length()) 70 | + PrimitiveSizes.INT 71 | + (PrimitiveSizes.SHORT + "ks".length())); 72 | 73 | Prepare decoded = decode(encoded, protocolVersion); 74 | 75 | assertThat(decoded.cqlQuery).isEqualTo(initial.cqlQuery); 76 | assertThat(decoded.keyspace).isEqualTo(initial.keyspace); 77 | } 78 | 79 | @Test 80 | @UseDataProvider(location = TestDataProviders.class, value = "protocolV5OrAbove") 81 | public void should_encode_and_decode_without_keyspace_in_protocol_v5_or_above( 82 | int protocolVersion) { 83 | Prepare initial = new Prepare("SELECT * FROM foo"); 84 | 85 | MockBinaryString encoded = encode(initial, protocolVersion); 86 | 87 | assertThat(encoded) 88 | .isEqualTo(new MockBinaryString().longString("SELECT * FROM foo").int_(0x00)); 89 | assertThat(encodedSize(initial, protocolVersion)) 90 | .isEqualTo((PrimitiveSizes.INT + "SELECT * FROM foo".length()) + PrimitiveSizes.INT); 91 | 92 | Prepare decoded = decode(encoded, protocolVersion); 93 | 94 | assertThat(decoded.cqlQuery).isEqualTo(initial.cqlQuery); 95 | assertThat(decoded.keyspace).isEqualTo(initial.keyspace); 96 | } 97 | } 98 | -------------------------------------------------------------------------------- /src/test/java/com/datastax/oss/protocol/internal/request/RegisterTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 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.oss.protocol.internal.request; 17 | 18 | import static com.datastax.oss.protocol.internal.Assertions.assertThat; 19 | 20 | import com.datastax.oss.protocol.internal.Message; 21 | import com.datastax.oss.protocol.internal.MessageTestBase; 22 | import com.datastax.oss.protocol.internal.PrimitiveSizes; 23 | import com.datastax.oss.protocol.internal.ProtocolConstants; 24 | import com.datastax.oss.protocol.internal.TestDataProviders; 25 | import com.datastax.oss.protocol.internal.binary.MockBinaryString; 26 | import com.datastax.oss.protocol.internal.util.collection.NullAllowingImmutableList; 27 | import com.tngtech.java.junit.dataprovider.DataProviderRunner; 28 | import com.tngtech.java.junit.dataprovider.UseDataProvider; 29 | import java.util.List; 30 | import org.junit.Test; 31 | import org.junit.runner.RunWith; 32 | 33 | @RunWith(DataProviderRunner.class) 34 | public class RegisterTest extends MessageTestBase { 35 | 36 | public RegisterTest() { 37 | super(Register.class); 38 | } 39 | 40 | @Override 41 | protected Message.Codec newCodec(int protocolVersion) { 42 | return new Register.Codec(protocolVersion); 43 | } 44 | 45 | @Test 46 | @UseDataProvider(location = TestDataProviders.class, value = "protocolV3OrAbove") 47 | public void should_encode_and_decode(int protocolVersion) { 48 | List eventTypes = 49 | NullAllowingImmutableList.of( 50 | ProtocolConstants.EventType.SCHEMA_CHANGE, ProtocolConstants.EventType.STATUS_CHANGE); 51 | Register initial = new Register(eventTypes); 52 | 53 | MockBinaryString encoded = encode(initial, protocolVersion); 54 | 55 | assertThat(encoded) 56 | .isEqualTo( 57 | new MockBinaryString() 58 | .unsignedShort(2) 59 | .string(ProtocolConstants.EventType.SCHEMA_CHANGE) 60 | .string(ProtocolConstants.EventType.STATUS_CHANGE)); 61 | 62 | assertThat(encodedSize(initial, protocolVersion)) 63 | .isEqualTo( 64 | PrimitiveSizes.SHORT 65 | + (PrimitiveSizes.SHORT + ProtocolConstants.EventType.SCHEMA_CHANGE.length()) 66 | + (PrimitiveSizes.SHORT + ProtocolConstants.EventType.STATUS_CHANGE.length())); 67 | 68 | Register decoded = decode(encoded, protocolVersion); 69 | 70 | assertThat(decoded.eventTypes) 71 | .containsExactly( 72 | ProtocolConstants.EventType.SCHEMA_CHANGE, ProtocolConstants.EventType.STATUS_CHANGE); 73 | } 74 | } 75 | -------------------------------------------------------------------------------- /src/test/java/com/datastax/oss/protocol/internal/request/query/QueryOptionsBuilder.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 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.oss.protocol.internal.request.query; 17 | 18 | public class QueryOptionsBuilder 19 | extends QueryOptionsBuilderBase { 20 | 21 | @Override 22 | public QueryOptions build() { 23 | return new QueryOptions( 24 | consistency, 25 | positionalValues, 26 | namedValues, 27 | skipMetadata, 28 | pageSize, 29 | pagingState, 30 | serialConsistency, 31 | defaultTimestamp, 32 | keyspace, 33 | nowInSeconds); 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /src/test/java/com/datastax/oss/protocol/internal/request/query/QueryOptionsBuilderBase.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 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.oss.protocol.internal.request.query; 17 | 18 | import com.datastax.oss.protocol.internal.ProtocolConstants; 19 | import com.datastax.oss.protocol.internal.util.Bytes; 20 | import java.nio.ByteBuffer; 21 | import java.util.ArrayList; 22 | import java.util.HashMap; 23 | import java.util.List; 24 | import java.util.Map; 25 | 26 | public abstract class QueryOptionsBuilderBase< 27 | ResultT extends QueryOptions, SelfT extends QueryOptionsBuilderBase> { 28 | 29 | protected int consistency = ProtocolConstants.ConsistencyLevel.ONE; 30 | protected List positionalValues = new ArrayList<>(); 31 | protected Map namedValues = new HashMap<>(); 32 | protected boolean skipMetadata = false; 33 | protected int pageSize = -1; 34 | protected ByteBuffer pagingState = null; 35 | protected int serialConsistency = ProtocolConstants.ConsistencyLevel.SERIAL; 36 | protected long defaultTimestamp = QueryOptions.NO_DEFAULT_TIMESTAMP; 37 | protected String keyspace = null; 38 | protected int nowInSeconds = QueryOptions.NO_NOW_IN_SECONDS; 39 | 40 | @SuppressWarnings("unchecked") 41 | private SelfT self() { 42 | return ((SelfT) this); 43 | } 44 | 45 | public SelfT withConsistencyLevel(int consistency) { 46 | this.consistency = consistency; 47 | return self(); 48 | } 49 | 50 | public SelfT withSkipMetadata() { 51 | this.skipMetadata = true; 52 | return self(); 53 | } 54 | 55 | public SelfT withPagingState(String hexString) { 56 | pagingState = Bytes.fromHexString(hexString); 57 | return self(); 58 | } 59 | 60 | public SelfT withPageSize(int pageSize) { 61 | this.pageSize = pageSize; 62 | return self(); 63 | } 64 | 65 | public SelfT withSerialConsistency(int serialConsistency) { 66 | this.serialConsistency = serialConsistency; 67 | return self(); 68 | } 69 | 70 | public SelfT withDefaultTimestamp(long defaultTimestamp) { 71 | this.defaultTimestamp = defaultTimestamp; 72 | return self(); 73 | } 74 | 75 | public SelfT withKeyspace(String keyspace) { 76 | this.keyspace = keyspace; 77 | return self(); 78 | } 79 | 80 | public SelfT withNowInSeconds(int nowInSeconds) { 81 | this.nowInSeconds = nowInSeconds; 82 | return self(); 83 | } 84 | 85 | public SelfT withPositionalValue(String hexString) { 86 | positionalValues.add(Bytes.fromHexString(hexString)); 87 | return self(); 88 | } 89 | 90 | public SelfT withNamedValue(String name, String hexString) { 91 | namedValues.put(name, Bytes.fromHexString(hexString)); 92 | return self(); 93 | } 94 | 95 | public abstract ResultT build(); 96 | } 97 | -------------------------------------------------------------------------------- /src/test/java/com/datastax/oss/protocol/internal/response/AuthChallengeTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 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.oss.protocol.internal.response; 17 | 18 | import static com.datastax.oss.protocol.internal.Assertions.assertThat; 19 | 20 | import com.datastax.oss.protocol.internal.Message; 21 | import com.datastax.oss.protocol.internal.MessageTestBase; 22 | import com.datastax.oss.protocol.internal.PrimitiveSizes; 23 | import com.datastax.oss.protocol.internal.TestDataProviders; 24 | import com.datastax.oss.protocol.internal.binary.MockBinaryString; 25 | import com.datastax.oss.protocol.internal.util.Bytes; 26 | import com.tngtech.java.junit.dataprovider.DataProviderRunner; 27 | import com.tngtech.java.junit.dataprovider.UseDataProvider; 28 | import java.nio.ByteBuffer; 29 | import org.junit.Test; 30 | import org.junit.runner.RunWith; 31 | 32 | @RunWith(DataProviderRunner.class) 33 | public class AuthChallengeTest extends MessageTestBase { 34 | 35 | public AuthChallengeTest() { 36 | super(AuthChallenge.class); 37 | } 38 | 39 | @Override 40 | protected Message.Codec newCodec(int protocolVersion) { 41 | return new AuthChallenge.Codec(protocolVersion); 42 | } 43 | 44 | @Test 45 | @UseDataProvider(location = TestDataProviders.class, value = "protocolV3OrAbove") 46 | public void should_encode_and_decode(int protocolVersion) { 47 | ByteBuffer token = Bytes.fromHexString("0xcafebabe"); 48 | AuthChallenge initial = new AuthChallenge(token); 49 | 50 | MockBinaryString encoded = encode(initial, protocolVersion); 51 | 52 | assertThat(encoded).isEqualTo(new MockBinaryString().bytes("0xcafebabe")); 53 | assertThat(encodedSize(initial, protocolVersion)) 54 | .isEqualTo(PrimitiveSizes.INT + "cafebabe".length() / 2); 55 | 56 | AuthChallenge decoded = decode(encoded, protocolVersion); 57 | 58 | assertThat(Bytes.toHexString(decoded.token)).isEqualTo("0xcafebabe"); 59 | } 60 | } 61 | -------------------------------------------------------------------------------- /src/test/java/com/datastax/oss/protocol/internal/response/AuthSuccessTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 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.oss.protocol.internal.response; 17 | 18 | import static com.datastax.oss.protocol.internal.Assertions.assertThat; 19 | 20 | import com.datastax.oss.protocol.internal.Message; 21 | import com.datastax.oss.protocol.internal.MessageTestBase; 22 | import com.datastax.oss.protocol.internal.PrimitiveSizes; 23 | import com.datastax.oss.protocol.internal.TestDataProviders; 24 | import com.datastax.oss.protocol.internal.binary.MockBinaryString; 25 | import com.datastax.oss.protocol.internal.util.Bytes; 26 | import com.tngtech.java.junit.dataprovider.DataProviderRunner; 27 | import com.tngtech.java.junit.dataprovider.UseDataProvider; 28 | import java.nio.ByteBuffer; 29 | import org.junit.Test; 30 | import org.junit.runner.RunWith; 31 | 32 | @RunWith(DataProviderRunner.class) 33 | public class AuthSuccessTest extends MessageTestBase { 34 | 35 | public AuthSuccessTest() { 36 | super(AuthSuccess.class); 37 | } 38 | 39 | @Override 40 | protected Message.Codec newCodec(int protocolVersion) { 41 | return new AuthSuccess.Codec(protocolVersion); 42 | } 43 | 44 | @Test 45 | @UseDataProvider(location = TestDataProviders.class, value = "protocolV3OrAbove") 46 | public void should_encode_and_decode(int protocolVersion) { 47 | ByteBuffer token = Bytes.fromHexString("0xcafebabe"); 48 | AuthSuccess initial = new AuthSuccess(token); 49 | 50 | MockBinaryString encoded = encode(initial, protocolVersion); 51 | 52 | assertThat(encoded).isEqualTo(new MockBinaryString().bytes("0xcafebabe")); 53 | assertThat(encodedSize(initial, protocolVersion)) 54 | .isEqualTo(PrimitiveSizes.INT + "cafebabe".length() / 2); 55 | 56 | AuthSuccess decoded = decode(encoded, protocolVersion); 57 | 58 | assertThat(Bytes.toHexString(decoded.token)).isEqualTo("0xcafebabe"); 59 | } 60 | } 61 | -------------------------------------------------------------------------------- /src/test/java/com/datastax/oss/protocol/internal/response/AuthenticateTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 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.oss.protocol.internal.response; 17 | 18 | import static org.assertj.core.api.Assertions.assertThat; 19 | 20 | import com.datastax.oss.protocol.internal.Message; 21 | import com.datastax.oss.protocol.internal.MessageTestBase; 22 | import com.datastax.oss.protocol.internal.PrimitiveSizes; 23 | import com.datastax.oss.protocol.internal.TestDataProviders; 24 | import com.datastax.oss.protocol.internal.binary.MockBinaryString; 25 | import com.tngtech.java.junit.dataprovider.DataProviderRunner; 26 | import com.tngtech.java.junit.dataprovider.UseDataProvider; 27 | import org.junit.Test; 28 | import org.junit.runner.RunWith; 29 | 30 | @RunWith(DataProviderRunner.class) 31 | public class AuthenticateTest extends MessageTestBase { 32 | 33 | public AuthenticateTest() { 34 | super(Authenticate.class); 35 | } 36 | 37 | @Override 38 | protected Message.Codec newCodec(int protocolVersion) { 39 | return new Authenticate.Codec(protocolVersion); 40 | } 41 | 42 | @Test 43 | @UseDataProvider(location = TestDataProviders.class, value = "protocolV3OrAbove") 44 | public void should_encode_and_decode(int protocolVersion) { 45 | String authenticator = "MockAuthenticator"; 46 | Authenticate initial = new Authenticate(authenticator); 47 | 48 | MockBinaryString encoded = encode(initial, protocolVersion); 49 | 50 | assertThat(encoded).isEqualTo(new MockBinaryString().string(authenticator)); 51 | assertThat(encodedSize(initial, protocolVersion)) 52 | .isEqualTo(PrimitiveSizes.SHORT + authenticator.length()); 53 | 54 | Authenticate decoded = decode(encoded, protocolVersion); 55 | assertThat(decoded.authenticator).isEqualTo(authenticator); 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /src/test/java/com/datastax/oss/protocol/internal/response/ReadyTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 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.oss.protocol.internal.response; 17 | 18 | import static com.datastax.oss.protocol.internal.Assertions.assertThat; 19 | 20 | import com.datastax.oss.protocol.internal.Message; 21 | import com.datastax.oss.protocol.internal.MessageTestBase; 22 | import com.datastax.oss.protocol.internal.TestDataProviders; 23 | import com.datastax.oss.protocol.internal.binary.MockBinaryString; 24 | import com.tngtech.java.junit.dataprovider.DataProviderRunner; 25 | import com.tngtech.java.junit.dataprovider.UseDataProvider; 26 | import org.junit.Test; 27 | import org.junit.runner.RunWith; 28 | 29 | @RunWith(DataProviderRunner.class) 30 | public class ReadyTest extends MessageTestBase { 31 | public ReadyTest() { 32 | super(Ready.class); 33 | } 34 | 35 | @Override 36 | protected Message.Codec newCodec(int protocolVersion) { 37 | return new Ready.Codec(protocolVersion); 38 | } 39 | 40 | @Test 41 | @UseDataProvider(location = TestDataProviders.class, value = "protocolV3OrAbove") 42 | public void should_encode_and_decode(int protocolVersion) { 43 | Ready initial = new Ready(); 44 | 45 | MockBinaryString encoded = encode(initial, protocolVersion); 46 | 47 | assertThat(encoded).isEqualTo(new MockBinaryString()); 48 | assertThat(encodedSize(initial, protocolVersion)).isEqualTo(0); 49 | 50 | Ready decoded = decode(encoded, protocolVersion); 51 | 52 | assertThat(decoded).isInstanceOf(Ready.class); 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /src/test/java/com/datastax/oss/protocol/internal/response/event/StatusChangeEventTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 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.oss.protocol.internal.response.event; 17 | 18 | import static com.datastax.oss.protocol.internal.Assertions.assertThat; 19 | 20 | import com.datastax.oss.protocol.internal.Message; 21 | import com.datastax.oss.protocol.internal.MessageTestBase; 22 | import com.datastax.oss.protocol.internal.PrimitiveSizes; 23 | import com.datastax.oss.protocol.internal.ProtocolConstants; 24 | import com.datastax.oss.protocol.internal.TestDataProviders; 25 | import com.datastax.oss.protocol.internal.binary.MockBinaryString; 26 | import com.datastax.oss.protocol.internal.response.Event; 27 | import com.tngtech.java.junit.dataprovider.DataProviderRunner; 28 | import com.tngtech.java.junit.dataprovider.UseDataProvider; 29 | import java.net.InetSocketAddress; 30 | import org.junit.Test; 31 | import org.junit.runner.RunWith; 32 | 33 | @RunWith(DataProviderRunner.class) 34 | public class StatusChangeEventTest extends MessageTestBase { 35 | 36 | public StatusChangeEventTest() { 37 | super(StatusChangeEvent.class); 38 | } 39 | 40 | @Override 41 | protected Message.Codec newCodec(int protocolVersion) { 42 | return new Event.Codec(protocolVersion); 43 | } 44 | 45 | @Test 46 | @UseDataProvider(location = TestDataProviders.class, value = "protocolV3OrAbove") 47 | public void should_encode_and_decode(int protocolVersion) { 48 | InetSocketAddress addr = new InetSocketAddress("127.0.0.1", 9042); 49 | StatusChangeEvent initial = new StatusChangeEvent(ProtocolConstants.StatusChangeType.UP, addr); 50 | 51 | MockBinaryString encoded = encode(initial, protocolVersion); 52 | 53 | assertThat(encoded) 54 | .isEqualTo( 55 | new MockBinaryString() 56 | .string(ProtocolConstants.EventType.STATUS_CHANGE) 57 | .string(ProtocolConstants.StatusChangeType.UP) 58 | .inetAddr(addr.getAddress()) 59 | .int_(addr.getPort())); 60 | assertThat(encodedSize(initial, protocolVersion)) 61 | .isEqualTo( 62 | (PrimitiveSizes.SHORT + ProtocolConstants.EventType.STATUS_CHANGE.length()) 63 | + (PrimitiveSizes.SHORT + ProtocolConstants.StatusChangeType.UP.length()) 64 | + (PrimitiveSizes.BYTE + 4 + PrimitiveSizes.INT)); 65 | 66 | StatusChangeEvent decoded = decode(encoded, protocolVersion); 67 | 68 | assertThat(decoded.type).isEqualTo(ProtocolConstants.EventType.STATUS_CHANGE); 69 | assertThat(decoded.changeType).isEqualTo(ProtocolConstants.StatusChangeType.UP); 70 | assertThat(decoded.address.getAddress().getHostAddress()).isEqualTo("127.0.0.1"); 71 | assertThat(decoded.address.getPort()).isEqualTo(9042); 72 | } 73 | } 74 | -------------------------------------------------------------------------------- /src/test/java/com/datastax/oss/protocol/internal/response/event/TopologyChangeEventTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 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.oss.protocol.internal.response.event; 17 | 18 | import static com.datastax.oss.protocol.internal.Assertions.assertThat; 19 | 20 | import com.datastax.oss.protocol.internal.Message; 21 | import com.datastax.oss.protocol.internal.MessageTestBase; 22 | import com.datastax.oss.protocol.internal.PrimitiveSizes; 23 | import com.datastax.oss.protocol.internal.ProtocolConstants; 24 | import com.datastax.oss.protocol.internal.TestDataProviders; 25 | import com.datastax.oss.protocol.internal.binary.MockBinaryString; 26 | import com.datastax.oss.protocol.internal.response.Event; 27 | import com.tngtech.java.junit.dataprovider.DataProviderRunner; 28 | import com.tngtech.java.junit.dataprovider.UseDataProvider; 29 | import java.net.InetSocketAddress; 30 | import org.junit.Test; 31 | import org.junit.runner.RunWith; 32 | 33 | @RunWith(DataProviderRunner.class) 34 | public class TopologyChangeEventTest extends MessageTestBase { 35 | 36 | public TopologyChangeEventTest() { 37 | super(TopologyChangeEvent.class); 38 | } 39 | 40 | @Override 41 | protected Message.Codec newCodec(int protocolVersion) { 42 | return new Event.Codec(protocolVersion); 43 | } 44 | 45 | @Test 46 | @UseDataProvider(location = TestDataProviders.class, value = "protocolV3OrAbove") 47 | public void should_encode_and_decode(int protocolVersion) { 48 | InetSocketAddress addr = new InetSocketAddress("127.0.0.1", 9042); 49 | TopologyChangeEvent initial = 50 | new TopologyChangeEvent(ProtocolConstants.TopologyChangeType.NEW_NODE, addr); 51 | 52 | MockBinaryString encoded = encode(initial, protocolVersion); 53 | 54 | assertThat(encoded) 55 | .isEqualTo( 56 | new MockBinaryString() 57 | .string(ProtocolConstants.EventType.TOPOLOGY_CHANGE) 58 | .string(ProtocolConstants.TopologyChangeType.NEW_NODE) 59 | .inetAddr(addr.getAddress()) 60 | .int_(addr.getPort())); 61 | assertThat(encodedSize(initial, protocolVersion)) 62 | .isEqualTo( 63 | (PrimitiveSizes.SHORT + ProtocolConstants.EventType.TOPOLOGY_CHANGE.length()) 64 | + (PrimitiveSizes.SHORT + ProtocolConstants.TopologyChangeType.NEW_NODE.length()) 65 | + (PrimitiveSizes.BYTE + 4 + PrimitiveSizes.INT)); 66 | 67 | TopologyChangeEvent decoded = decode(encoded, protocolVersion); 68 | 69 | assertThat(decoded.type).isEqualTo(ProtocolConstants.EventType.TOPOLOGY_CHANGE); 70 | assertThat(decoded.changeType).isEqualTo(ProtocolConstants.TopologyChangeType.NEW_NODE); 71 | assertThat(decoded.address.getAddress().getHostAddress()).isEqualTo("127.0.0.1"); 72 | assertThat(decoded.address.getPort()).isEqualTo(9042); 73 | } 74 | } 75 | -------------------------------------------------------------------------------- /src/test/java/com/datastax/oss/protocol/internal/response/result/SetKeyspaceTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 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.oss.protocol.internal.response.result; 17 | 18 | import static org.assertj.core.api.Assertions.assertThat; 19 | 20 | import com.datastax.oss.protocol.internal.Message; 21 | import com.datastax.oss.protocol.internal.MessageTestBase; 22 | import com.datastax.oss.protocol.internal.PrimitiveSizes; 23 | import com.datastax.oss.protocol.internal.ProtocolConstants; 24 | import com.datastax.oss.protocol.internal.TestDataProviders; 25 | import com.datastax.oss.protocol.internal.binary.MockBinaryString; 26 | import com.datastax.oss.protocol.internal.response.Result; 27 | import com.tngtech.java.junit.dataprovider.DataProviderRunner; 28 | import com.tngtech.java.junit.dataprovider.UseDataProvider; 29 | import org.junit.Test; 30 | import org.junit.runner.RunWith; 31 | 32 | @RunWith(DataProviderRunner.class) 33 | public class SetKeyspaceTest extends MessageTestBase { 34 | public SetKeyspaceTest() { 35 | super(SetKeyspace.class); 36 | } 37 | 38 | @Override 39 | protected Message.Codec newCodec(int protocolVersion) { 40 | return new Result.Codec(protocolVersion); 41 | } 42 | 43 | @Test 44 | @UseDataProvider(location = TestDataProviders.class, value = "protocolV3OrAbove") 45 | public void should_encode_and_decode(int protocolVersion) { 46 | SetKeyspace initial = new SetKeyspace("ks"); 47 | 48 | MockBinaryString encoded = encode(initial, protocolVersion); 49 | 50 | assertThat(encoded) 51 | .isEqualTo( 52 | new MockBinaryString().int_(ProtocolConstants.ResultKind.SET_KEYSPACE).string("ks")); 53 | assertThat(encodedSize(initial, protocolVersion)) 54 | .isEqualTo(PrimitiveSizes.INT + (PrimitiveSizes.SHORT + "ks".length())); 55 | 56 | SetKeyspace decoded = decode(encoded, protocolVersion); 57 | assertThat(decoded.keyspace).isEqualTo("ks"); 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /src/test/java/com/datastax/oss/protocol/internal/response/result/VoidTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 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.oss.protocol.internal.response.result; 17 | 18 | import static com.datastax.oss.protocol.internal.Assertions.assertThat; 19 | 20 | import com.datastax.oss.protocol.internal.Message; 21 | import com.datastax.oss.protocol.internal.MessageTestBase; 22 | import com.datastax.oss.protocol.internal.ProtocolConstants; 23 | import com.datastax.oss.protocol.internal.TestDataProviders; 24 | import com.datastax.oss.protocol.internal.binary.MockBinaryString; 25 | import com.datastax.oss.protocol.internal.response.Result; 26 | import com.tngtech.java.junit.dataprovider.DataProviderRunner; 27 | import com.tngtech.java.junit.dataprovider.UseDataProvider; 28 | import org.junit.Test; 29 | import org.junit.runner.RunWith; 30 | 31 | @RunWith(DataProviderRunner.class) 32 | public class VoidTest extends MessageTestBase { 33 | 34 | public VoidTest() { 35 | super(Void.class); 36 | } 37 | 38 | @Override 39 | protected Message.Codec newCodec(int protocolVersion) { 40 | return new Result.Codec(protocolVersion); 41 | } 42 | 43 | @Test 44 | @UseDataProvider(location = TestDataProviders.class, value = "protocolV3OrAbove") 45 | public void should_encode_and_decode(int protocolVersion) { 46 | MockBinaryString encoded = encode(Void.INSTANCE, protocolVersion); 47 | 48 | assertThat(encoded).isEqualTo(new MockBinaryString().int_(ProtocolConstants.ResultKind.VOID)); 49 | assertThat(encodedSize(Void.INSTANCE, protocolVersion)).isEqualTo(4); 50 | 51 | Void decoded = decode(encoded, protocolVersion); 52 | 53 | assertThat(decoded).isEqualTo(Void.INSTANCE); 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /src/test/java/com/datastax/oss/protocol/internal/util/IntIntMapTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 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.oss.protocol.internal.util; 17 | 18 | import static com.datastax.oss.protocol.internal.Assertions.assertThat; 19 | 20 | import org.junit.Test; 21 | 22 | public class IntIntMapTest { 23 | 24 | @Test 25 | public void should_store_and_retrieve_entries() { 26 | IntIntMap map = 27 | IntIntMap.builder() 28 | .put(1, 1, "foo11") 29 | .put(1, 2, "foo12") 30 | .put(1, 3, "foo13") 31 | .put(2, 1, "foo21") 32 | .put(3, 1, "foo31") 33 | .build(); 34 | 35 | assertThat(map.get(1, 1)).isEqualTo("foo11"); 36 | assertThat(map.get(1, 2)).isEqualTo("foo12"); 37 | assertThat(map.get(1, 3)).isEqualTo("foo13"); 38 | assertThat(map.get(2, 1)).isEqualTo("foo21"); 39 | assertThat(map.get(3, 1)).isEqualTo("foo31"); 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /src/test/java/com/datastax/oss/protocol/internal/util/IntMapTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 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.oss.protocol.internal.util; 17 | 18 | import static com.datastax.oss.protocol.internal.Assertions.assertThat; 19 | 20 | import org.junit.Test; 21 | 22 | public class IntMapTest { 23 | 24 | @Test(expected = IllegalArgumentException.class) 25 | public void should_not_allow_negative_keys() { 26 | IntMap.builder().put(-1, "a"); 27 | } 28 | 29 | @Test 30 | public void should_store_and_retrieve_entries() { 31 | IntMap map = IntMap.builder().put(1, "foo").put(3, "bar").build(); 32 | assertThat(map.get(0)).isNull(); 33 | assertThat(map.get(1)).isEqualTo("foo"); 34 | assertThat(map.get(2)).isNull(); 35 | assertThat(map.get(3)).isEqualTo("bar"); 36 | assertThat(map.get(4)).isNull(); 37 | } 38 | 39 | @Test(expected = IllegalArgumentException.class) 40 | public void should_fail_if_key_already_exists() { 41 | IntMap.builder().put(1, "foo").put(1, "bar"); 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /src/test/java/com/datastax/oss/protocol/internal/util/SerializationHelper.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 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.oss.protocol.internal.util; 17 | 18 | import static org.assertj.core.api.Assertions.fail; 19 | 20 | import java.io.ByteArrayInputStream; 21 | import java.io.ByteArrayOutputStream; 22 | import java.io.ObjectInputStream; 23 | import java.io.ObjectOutputStream; 24 | 25 | public abstract class SerializationHelper { 26 | 27 | public static byte[] serialize(T t) { 28 | try { 29 | ByteArrayOutputStream bytes = new ByteArrayOutputStream(); 30 | ObjectOutputStream out = new ObjectOutputStream(bytes); 31 | out.writeObject(t); 32 | return bytes.toByteArray(); 33 | } catch (Exception e) { 34 | fail("Unexpected error", e); 35 | throw new AssertionError(); // never reached 36 | } 37 | } 38 | 39 | // the calling code performs validations on the result, so this doesn't matter 40 | @SuppressWarnings("TypeParameterUnusedInFormals") 41 | public static T deserialize(byte[] bytes) { 42 | try { 43 | ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(bytes)); 44 | @SuppressWarnings("unchecked") 45 | T t = (T) in.readObject(); 46 | return t; 47 | } catch (Exception e) { 48 | fail("Unexpected error", e); 49 | throw new AssertionError(); // never reached 50 | } 51 | } 52 | 53 | public static T serializeAndDeserialize(T t) { 54 | return deserialize(serialize(t)); 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /src/test/java/com/datastax/oss/protocol/internal/util/collection/NullAllowingImmutableListTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 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.oss.protocol.internal.util.collection; 17 | 18 | import static org.assertj.core.api.Assertions.assertThat; 19 | 20 | import com.datastax.oss.protocol.internal.util.SerializationHelper; 21 | import java.util.ArrayList; 22 | import java.util.List; 23 | import java.util.stream.Collectors; 24 | import java.util.stream.IntStream; 25 | import org.junit.Test; 26 | 27 | public class NullAllowingImmutableListTest { 28 | 29 | @Test 30 | public void should_create_from_elements() { 31 | assertThat(NullAllowingImmutableList.of(1)).hasSize(1).containsExactly(1); 32 | assertThat(NullAllowingImmutableList.of((Integer) null)) 33 | .hasSize(1) 34 | .containsExactly((Integer) null); 35 | assertThat(NullAllowingImmutableList.of(1, 2)).hasSize(2).containsExactly(1, 2); 36 | assertThat(NullAllowingImmutableList.of(1, 2, 3)).hasSize(3).containsExactly(1, 2, 3); 37 | assertThat(NullAllowingImmutableList.of(1, 2, 3, 4, 5)) 38 | .hasSize(5) 39 | .containsExactly(1, 2, 3, 4, 5); 40 | } 41 | 42 | @Test 43 | public void should_create_from_collection() { 44 | List collection = new ArrayList<>(); 45 | collection.add(1); 46 | collection.add(null); 47 | collection.add(3); 48 | assertThat(NullAllowingImmutableList.copyOf(collection)).hasSize(3).containsExactly(1, null, 3); 49 | } 50 | 51 | @Test 52 | public void should_create_with_builder() { 53 | assertThat(NullAllowingImmutableList.builder().add(1).add(null).add(3).build()) 54 | .hasSize(3) 55 | .containsExactly(1, null, 3); 56 | } 57 | 58 | @Test 59 | public void should_serialize_and_deserialize() { 60 | NullAllowingImmutableList in = NullAllowingImmutableList.of(1, null, 3); 61 | NullAllowingImmutableList out = SerializationHelper.serializeAndDeserialize(in); 62 | assertThat(out).hasSize(3).containsExactly(1, null, 3); 63 | } 64 | 65 | @Test 66 | public void should_return_singleton_empty_instance() { 67 | NullAllowingImmutableList l1 = NullAllowingImmutableList.of(); 68 | NullAllowingImmutableList l2 = NullAllowingImmutableList.of(); 69 | assertThat(l1).isSameAs(l2); 70 | } 71 | 72 | @Test 73 | public void should_resize_builder_internal_capacity() { 74 | // will resize once 75 | assertThat( 76 | NullAllowingImmutableList.builder(10) 77 | .addAll(IntStream.range(0, 20).boxed().collect(Collectors.toList())) 78 | .build()) 79 | .hasSize(20); 80 | // will resize twice 81 | assertThat( 82 | NullAllowingImmutableList.builder(10) 83 | .addAll(IntStream.range(0, 30).boxed().collect(Collectors.toList())) 84 | .build()) 85 | .hasSize(30); 86 | } 87 | } 88 | --------------------------------------------------------------------------------