use of com.datastax.oss.driver.api.core.metadata.token.Token in project java-driver by datastax.
the class PreparedStatementIT method should_infer_routing_information_when_partition_key_is_bound.
private void should_infer_routing_information_when_partition_key_is_bound(String queryString) {
CqlSession session = sessionRule.session();
TokenFactory tokenFactory = ((DefaultTokenMap) session.getMetadata().getTokenMap().orElseThrow(AssertionError::new)).getTokenFactory();
// We'll bind a=1 in the query, check what token this is supposed to produce
Token expectedToken = session.execute("SELECT token(a) FROM prepared_statement_test WHERE a = 1").one().getToken(0);
BoundStatement boundStatement = session.prepare(queryString).bind().setInt("a", 1);
assertThat(boundStatement.getRoutingKeyspace()).isEqualTo(sessionRule.keyspace());
assertThat(tokenFactory.hash(boundStatement.getRoutingKey())).isEqualTo(expectedToken);
}
use of com.datastax.oss.driver.api.core.metadata.token.Token in project java-driver by datastax.
the class TokenITBase method should_get_token_from_row_and_set_token_in_query_with_binding_and_aliasing.
/**
* Validates that a {@link Token} can be retrieved and parsed by using bind variables and
* aliasing.
*
* <p>This test does the following: retrieve the token by alias for the key '1', and ensure it
* matches the token by index; select data by token using setToken by name.
*/
@Test
public void should_get_token_from_row_and_set_token_in_query_with_binding_and_aliasing() {
Row row = session().execute("SELECT token(i) AS t FROM foo WHERE i = 1").one();
assertThat(row).isNotNull();
Token token = row.getToken("t");
assertThat(token).isNotNull().isInstanceOf(expectedTokenType);
PreparedStatement pst = session().prepare("SELECT * FROM foo WHERE token(i) = :myToken");
row = session().execute(pst.bind().setToken("myToken", token)).one();
assertThat(row).isNotNull();
assertThat(row.getInt(0)).isEqualTo(1);
row = session().execute(SimpleStatement.newInstance("SELECT * FROM foo WHERE token(i) = ?", token)).one();
assertThat(row).isNotNull();
assertThat(row.getInt(0)).isEqualTo(1);
}
use of com.datastax.oss.driver.api.core.metadata.token.Token in project java-driver by datastax.
the class TokenITBase method should_get_token_from_row_and_set_token_in_query.
/**
* Validates that a {@link Token} can be retrieved and parsed by executing 'select token(name)'
* and then used to find data matching that token.
*
* <p>This test does the following: retrieve the token for the key with value '1', get it by
* index, and ensure if is of the expected token type; select data by token with a BoundStatement;
* select data by token using setToken by index.
*
* @test_category token
* @expected_result tokens are selectable, properly parsed, and usable as input.
* @jira_ticket JAVA-312
* @since 2.0.10, 2.1.5
*/
@Test
public void should_get_token_from_row_and_set_token_in_query() {
ResultSet rs = session().execute("SELECT token(i) FROM foo WHERE i = 1");
Row row = rs.one();
assertThat(row).isNotNull();
// Get by index:
Token token = row.getToken(0);
assertThat(token).isNotNull().isInstanceOf(expectedTokenType);
// Get by name: the generated column name depends on the Cassandra version.
String tokenColumnName = rs.getColumnDefinitions().contains("token(i)") ? "token(i)" : "system.token(i)";
assertThat(row.getToken(tokenColumnName)).isEqualTo(token);
PreparedStatement pst = session().prepare("SELECT * FROM foo WHERE token(i) = ?");
// Bind with bind(...)
row = session().execute(pst.bind(token)).iterator().next();
assertThat(row.getInt(0)).isEqualTo(1);
// Bind with setToken by index
row = session().execute(pst.bind().setToken(0, token)).one();
assertThat(row).isNotNull();
assertThat(row.getInt(0)).isEqualTo(1);
// Bind with setToken by name
row = session().execute(pst.bind().setToken("partition key token", token)).one();
assertThat(row).isNotNull();
assertThat(row.getInt(0)).isEqualTo(1);
}
use of com.datastax.oss.driver.api.core.metadata.token.Token in project java-driver by datastax.
the class TokenITBase method should_create_token_from_partition_key.
@Test
public void should_create_token_from_partition_key() {
TokenMap tokenMap = getTokenMap();
Row row = session().execute("SELECT token(i) FROM foo WHERE i = 1").one();
assertThat(row).isNotNull();
Token expected = row.getToken(0);
ProtocolVersion protocolVersion = session().getContext().getProtocolVersion();
assertThat(tokenMap.newToken(TypeCodecs.INT.encodePrimitive(1, protocolVersion))).isEqualTo(expected);
}
use of com.datastax.oss.driver.api.core.metadata.token.Token in project java-driver by datastax.
the class RandomTokenRange method split.
@Override
protected List<Token> split(Token startToken, Token endToken, int numberOfSplits) {
// edge case: ]min, min] means the whole ring
if (startToken.equals(endToken) && startToken.equals(RandomTokenFactory.MIN_TOKEN)) {
endToken = RandomTokenFactory.MAX_TOKEN;
}
BigInteger start = ((RandomToken) startToken).getValue();
BigInteger end = ((RandomToken) endToken).getValue();
BigInteger range = end.subtract(start);
if (range.compareTo(BigInteger.ZERO) < 0) {
range = range.add(RING_LENGTH);
}
List<BigInteger> values = super.split(start, range, MAX_VALUE, RING_LENGTH, numberOfSplits);
List<Token> tokens = Lists.newArrayListWithExpectedSize(values.size());
for (BigInteger value : values) {
tokens.add(new RandomToken(value));
}
return tokens;
}
Aggregations