private ProtocolVersion protocolVersion() { // Since the QueryLogger can be registered before the Cluster was initialized, we can't retrieve // it at construction time. Cache it field at first use (a volatile field is good enough since // we // don't need mutual exclusion). if (protocolVersion == null) { protocolVersion = cluster.getConfiguration().getProtocolOptions().getProtocolVersion(); // At least one connection was established when QueryLogger is invoked assert protocolVersion != null : "protocol version should be defined"; } return protocolVersion; }
private static Map<String, DataType> buildArguments( KeyspaceMetadata ksm, List<String> names, List<String> types, VersionNumber version, Cluster cluster) { if (names.isEmpty()) return Collections.emptyMap(); ImmutableMap.Builder<String, DataType> builder = ImmutableMap.builder(); CodecRegistry codecRegistry = cluster.getConfiguration().getCodecRegistry(); ProtocolVersion protocolVersion = cluster.getConfiguration().getProtocolOptions().getProtocolVersion(); Iterator<String> iterTypes = types.iterator(); for (String name : names) { DataType type; if (version.getMajor() >= 3) { type = DataTypeCqlNameParser.parse( iterTypes.next(), cluster, ksm.getName(), ksm.userTypes, null, false, false); } else { type = DataTypeClassNameParser.parseOne(iterTypes.next(), protocolVersion, codecRegistry); } builder.put(name, type); } return builder.build(); }
@BeforeMethod(groups = "unit") public void setUpQueryBuilder() throws Exception { CodecRegistry codecRegistry = new CodecRegistry(); cluster = mock(Cluster.class); Configuration configuration = mock(Configuration.class); ProtocolOptions protocolOptions = mock(ProtocolOptions.class); when(cluster.getConfiguration()).thenReturn(configuration); when(configuration.getCodecRegistry()).thenReturn(codecRegistry); when(configuration.getProtocolOptions()).thenReturn(protocolOptions); when(protocolOptions.getProtocolVersion()).thenReturn(ProtocolVersion.NEWEST_SUPPORTED); }
ResultSetFuture executeQuery(Message.Request msg, Statement statement) { DefaultResultSetFuture future = new DefaultResultSetFuture( this, configuration().getProtocolOptions().getProtocolVersion(), msg); execute(future, statement); return future; }
private static List<DataType> parseTypes( KeyspaceMetadata ksm, List<String> types, VersionNumber version, Cluster cluster) { if (types.isEmpty()) return Collections.emptyList(); CodecRegistry codecRegistry = cluster.getConfiguration().getCodecRegistry(); ProtocolVersion protocolVersion = cluster.getConfiguration().getProtocolOptions().getProtocolVersion(); ImmutableList.Builder<DataType> builder = ImmutableList.builder(); for (String name : types) { DataType type; if (version.getMajor() >= 3) { type = DataTypeCqlNameParser.parse( name, cluster, ksm.getName(), ksm.userTypes, null, false, false); } else { type = DataTypeClassNameParser.parseOne(name, protocolVersion, codecRegistry); } builder.add(type); } return builder.build(); }
private ProtocolVersion actualProtocolVersion(Cluster cluster) { return cluster.getConfiguration().getProtocolOptions().getProtocolVersion(); } }
@Override public void init(Cluster cluster, Collection<Host> hosts) { clusterMetadata = cluster.getMetadata(); protocolVersion = cluster.getConfiguration().getProtocolOptions().getProtocolVersion(); codecRegistry = cluster.getConfiguration().getCodecRegistry(); childPolicy.init(cluster, hosts); }
@BeforeMethod(groups = "unit") public void initMocks() { CodecRegistry codecRegistry = new CodecRegistry(); cluster = mock(Cluster.class); Configuration configuration = mock(Configuration.class); ProtocolOptions protocolOptions = mock(ProtocolOptions.class); Metadata metadata = mock(Metadata.class); childPolicy = mock(LoadBalancingPolicy.class); when(cluster.getConfiguration()).thenReturn(configuration); when(configuration.getCodecRegistry()).thenReturn(codecRegistry); when(configuration.getProtocolOptions()).thenReturn(protocolOptions); when(protocolOptions.getProtocolVersion()).thenReturn(ProtocolVersion.NEWEST_SUPPORTED); when(cluster.getMetadata()).thenReturn(metadata); when(metadata.getReplicas(Metadata.quote("keyspace"), routingKey)) .thenReturn(Sets.newLinkedHashSet(host1, host2)); when(childPolicy.newQueryPlan("keyspace", statement)) .thenReturn(Sets.newLinkedHashSet(host4, host3, host2, host1).iterator()); when(childPolicy.distance(any(Host.class))).thenReturn(HostDistance.LOCAL); when(host1.isUp()).thenReturn(true); when(host2.isUp()).thenReturn(true); when(host3.isUp()).thenReturn(true); when(host4.isUp()).thenReturn(true); }
/** @jira_ticket JAVA-1209 */ @Test(groups = "short") public void getProtocolVersion_should_return_version() throws InterruptedException { ProtocolVersion version = cluster().getConfiguration().getProtocolOptions().getProtocolVersion(); assertThat(version).isNotNull(); } }
/** @jira_ticket JAVA-1209 */ @Test(groups = "unit") public void getProtocolVersion_should_return_null_if_not_connected() { Cluster cluster = Cluster.builder().addContactPoint("127.0.0.1").build(); assertThat(cluster.getConfiguration().getProtocolOptions().getProtocolVersion()).isNull(); }
@Test(groups = "short") public void textRoutingKeyTest() throws Exception { BuiltStatement query; TableMetadata table = cluster().getMetadata().getKeyspace(keyspace).getTable(TABLE_TEXT); assertNotNull(table); ProtocolVersion protocolVersion = cluster().getConfiguration().getProtocolOptions().getProtocolVersion(); CodecRegistry codecRegistry = CodecRegistry.DEFAULT_INSTANCE; String txt = "If she weighs the same as a duck... she's made of wood."; query = insertInto(table).values(new String[] {"k", "a", "b"}, new Object[] {txt, 1, 2}); assertEquals( query.getRoutingKey(protocolVersion, codecRegistry), ByteBuffer.wrap(txt.getBytes())); session().execute(query); query = select().from(table).where(eq("k", txt)); assertEquals( query.getRoutingKey(protocolVersion, codecRegistry), ByteBuffer.wrap(txt.getBytes())); Row row = session().execute(query).one(); assertEquals(row.getString("k"), txt); assertEquals(row.getInt("a"), 1); assertEquals(row.getInt("b"), 2); }
@Test(groups = "short") public void batchTest() throws Exception { try { PreparedStatement ps1 = session().prepare("INSERT INTO " + SIMPLE_TABLE2 + "(k, v) VALUES (?, ?)"); PreparedStatement ps2 = session().prepare("INSERT INTO " + SIMPLE_TABLE2 + "(k, v) VALUES (?, 'bar')"); BatchStatement bs = new BatchStatement(); bs.add(ps1.bind("one", "foo")); bs.add(ps2.bind("two")); bs.add( new SimpleStatement( "INSERT INTO " + SIMPLE_TABLE2 + " (k, v) VALUES ('three', 'foobar')")); session().execute(bs); List<Row> all = session().execute("SELECT * FROM " + SIMPLE_TABLE2).all(); assertEquals("three", all.get(0).getString("k")); assertEquals("foobar", all.get(0).getString("v")); assertEquals("one", all.get(1).getString("k")); assertEquals("foo", all.get(1).getString("v")); assertEquals("two", all.get(2).getString("k")); assertEquals("bar", all.get(2).getString("v")); } catch (UnsupportedFeatureException e) { // This is expected when testing the protocol v1 if (cluster().getConfiguration().getProtocolOptions().getProtocolVersion() != ProtocolVersion.V1) throw e; } }
@Test(groups = "short") public void intRoutingKeyTest() throws Exception { BuiltStatement query; TableMetadata table = cluster().getMetadata().getKeyspace(keyspace).getTable(TABLE_INT); assertNotNull(table); ProtocolVersion protocolVersion = cluster().getConfiguration().getProtocolOptions().getProtocolVersion(); CodecRegistry codecRegistry = CodecRegistry.DEFAULT_INSTANCE; query = insertInto(table).values(new String[] {"k", "a", "b"}, new Object[] {42, 1, 2}); ByteBuffer bb = ByteBuffer.allocate(4); bb.putInt(0, 42); assertEquals(query.getRoutingKey(protocolVersion, codecRegistry), bb); session().execute(query); query = select().from(table).where(eq("k", 42)); assertEquals(query.getRoutingKey(protocolVersion, codecRegistry), bb); Row row = session().execute(query).one(); assertEquals(row.getInt("k"), 42); assertEquals(row.getInt("a"), 1); assertEquals(row.getInt("b"), 2); }
@Test(groups = "short") public void should_create_token_from_partition_key() { Metadata metadata = cluster().getMetadata(); Row row = session().execute("SELECT token(i) FROM foo WHERE i = 1").one(); Token expected = row.getToken(0); ProtocolVersion protocolVersion = cluster().getConfiguration().getProtocolOptions().getProtocolVersion(); assertThat(metadata.newToken(TypeCodec.cint().serialize(1, protocolVersion))) .isEqualTo(expected); }
@Test(groups = "short") public void routingKeyColumnCaseSensitivityForQuotedIdentifiersTest() throws Exception { BuiltStatement query; TableMetadata table = cluster().getMetadata().getKeyspace(keyspace).getTable(TABLE_CASE_QUOTED); assertNotNull(table); ProtocolVersion protocolVersion = cluster().getConfiguration().getProtocolOptions().getProtocolVersion(); CodecRegistry codecRegistry = CodecRegistry.DEFAULT_INSTANCE; query = insertInto(table) .values( new String[] {"\"theKey\"", "a", "b", "\"tHEkEY\""}, new Object[] {42, 1, 2, 3}); ByteBuffer bb = ByteBuffer.allocate(4); bb.putInt(0, 42); assertEquals(query.getRoutingKey(protocolVersion, codecRegistry), bb); query = insertInto(table) .values(new String[] {"theKey", "a", "b", "\"tHEkEY\""}, new Object[] {42, 1, 2, 3}); assertNull(query.getRoutingKey(protocolVersion, codecRegistry)); query = insertInto(table) .values(new String[] {"theKey", "a", "b", "theKey"}, new Object[] {42, 1, 2, 3}); assertNull(query.getRoutingKey(protocolVersion, codecRegistry)); }
/** * Verifies that the driver can connect to 3.10 with the following combination of options: Version * UNSET Flag UNSET Expected version: V4 * * @jira_ticket JAVA-1248 */ @Test(groups = "short") public void should_connect_after_renegotiation_when_no_version_explicitly_required_and_flag_not_set() throws Exception { // Note: when the driver's ProtocolVersion.NEWEST_SUPPORTED will be incremented to V6 or higher // the renegotiation will start downgrading the version from V6 to V4 instead of V5 to V4, // but the test should remain valid since it's executed against 3.10 exclusively Cluster cluster = Cluster.builder() .addContactPoints(getContactPoints()) .withPort(ccm().getBinaryPort()) .build(); cluster.connect(); assertThat(cluster.getConfiguration().getProtocolOptions().getProtocolVersion()).isEqualTo(V4); } }
static DefaultPreparedStatement fromMessage( Responses.Result.Prepared msg, Cluster cluster, String query, String queryKeyspace) { assert msg.metadata.columns != null; ColumnDefinitions defs = msg.metadata.columns; ProtocolVersion protocolVersion = cluster.getConfiguration().getProtocolOptions().getProtocolVersion(); PreparedId.PreparedMetadata boundValuesMetadata = new PreparedId.PreparedMetadata(msg.statementId, defs); PreparedId.PreparedMetadata resultSetMetadata = new PreparedId.PreparedMetadata(msg.resultMetadataId, msg.resultMetadata.columns); int[] pkIndices = null; if (defs.size() > 0) { pkIndices = (protocolVersion.compareTo(V4) >= 0) ? msg.metadata.pkIndices : computePkIndices(cluster.getMetadata(), defs); } PreparedId preparedId = new PreparedId(boundValuesMetadata, resultSetMetadata, pkIndices, protocolVersion); return new DefaultPreparedStatement( preparedId, query, queryKeyspace, msg.getCustomPayload(), cluster); }
/** * Verifies that the driver can connect to 3.10 with the following combination of options: Version * UNSET Flag SET Expected version: V5 * * @jira_ticket JAVA-1248 */ @Test(groups = "short") public void should_connect_with_beta_when_no_version_explicitly_required_and_flag_set() throws Exception { // Note: when the driver's ProtocolVersion.NEWEST_SUPPORTED will be incremented to V6 or higher // a renegotiation will start taking place here and will downgrade the version from V6 to V5, // but the test should remain valid since it's executed against 3.10 exclusively Cluster cluster = Cluster.builder() .addContactPoints(getContactPoints()) .withPort(ccm().getBinaryPort()) .allowBetaProtocolVersion() .build(); cluster.connect(); assertThat(cluster.getConfiguration().getProtocolOptions().getProtocolVersion()).isEqualTo(V5); }
@Override public PreparedStatement apply(PreparedStatement prepared) { ProtocolVersion protocolVersion = getCluster().getConfiguration().getProtocolOptions().getProtocolVersion(); ByteBuffer routingKey = statement.getRoutingKey(protocolVersion, codecRegistry); if (routingKey != null) prepared.setRoutingKey(routingKey); if (statement.getConsistencyLevel() != null) prepared.setConsistencyLevel(statement.getConsistencyLevel()); if (statement.getSerialConsistencyLevel() != null) prepared.setSerialConsistencyLevel(statement.getSerialConsistencyLevel()); if (statement.isTracing()) prepared.enableTracing(); prepared.setRetryPolicy(statement.getRetryPolicy()); prepared.setOutgoingPayload(statement.getOutgoingPayload()); prepared.setIdempotent(statement.isIdempotent()); return prepared; } });
@Override public void onTestContextInitialized() { protocolVersion = cluster().getConfiguration().getProtocolOptions().getProtocolVersion(); String createTable = "CREATE TABLE indexing (" + "id int," + "id2 int," + "map_values map<text, int>," + "map_keys map<text, int>," + "map_entries map<text, int>," + "map_all map<text, int>," + "text_column text, " + "\"MixedCaseColumn\" list<text>," + // Frozen collections was introduced only in C* 2.1.3 (ccm().getCassandraVersion().compareTo(VersionNumber.parse("2.1.3")) >= 0 ? ", map_full frozen<map<text, int>>," + "set_full frozen<set<text>>," + "list_full frozen<list<text>>," : "") + "PRIMARY KEY (id, id2));"; execute(createTable); }