Search in sources :

Example 1 with Token

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);
}
Also used : DefaultTokenMap(com.datastax.oss.driver.internal.core.metadata.token.DefaultTokenMap) Token(com.datastax.oss.driver.api.core.metadata.token.Token) TokenFactory(com.datastax.oss.driver.internal.core.metadata.token.TokenFactory) CqlSession(com.datastax.oss.driver.api.core.CqlSession) BoundStatement(com.datastax.oss.driver.api.core.cql.BoundStatement)

Example 2 with Token

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);
}
Also used : Token(com.datastax.oss.driver.api.core.metadata.token.Token) PreparedStatement(com.datastax.oss.driver.api.core.cql.PreparedStatement) Row(com.datastax.oss.driver.api.core.cql.Row) Test(org.junit.Test)

Example 3 with Token

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);
}
Also used : ResultSet(com.datastax.oss.driver.api.core.cql.ResultSet) Token(com.datastax.oss.driver.api.core.metadata.token.Token) PreparedStatement(com.datastax.oss.driver.api.core.cql.PreparedStatement) Row(com.datastax.oss.driver.api.core.cql.Row) Test(org.junit.Test)

Example 4 with Token

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);
}
Also used : Token(com.datastax.oss.driver.api.core.metadata.token.Token) Row(com.datastax.oss.driver.api.core.cql.Row) TokenMap(com.datastax.oss.driver.api.core.metadata.TokenMap) ProtocolVersion(com.datastax.oss.driver.api.core.ProtocolVersion) Test(org.junit.Test)

Example 5 with Token

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;
}
Also used : BigInteger(java.math.BigInteger) Token(com.datastax.oss.driver.api.core.metadata.token.Token)

Aggregations

Token (com.datastax.oss.driver.api.core.metadata.token.Token)22 Node (com.datastax.oss.driver.api.core.metadata.Node)6 Test (org.junit.Test)6 TokenRange (com.datastax.oss.driver.api.core.metadata.token.TokenRange)5 TokenMap (com.datastax.oss.driver.api.core.metadata.TokenMap)4 ImmutableMap (com.datastax.oss.driver.shaded.guava.common.collect.ImmutableMap)4 ByteBuffer (java.nio.ByteBuffer)4 CqlIdentifier (com.datastax.oss.driver.api.core.CqlIdentifier)3 PreparedStatement (com.datastax.oss.driver.api.core.cql.PreparedStatement)3 Row (com.datastax.oss.driver.api.core.cql.Row)3 NonNull (edu.umd.cs.findbugs.annotations.NonNull)3 BigInteger (java.math.BigInteger)3 Set (java.util.Set)3 CqlSession (com.datastax.oss.driver.api.core.CqlSession)2 BoundStatement (com.datastax.oss.driver.api.core.cql.BoundStatement)2 DefaultNode (com.datastax.oss.driver.internal.core.metadata.DefaultNode)2 ByteOrderedToken (com.datastax.oss.driver.internal.core.metadata.token.ByteOrderedToken)2 Murmur3Token (com.datastax.oss.driver.internal.core.metadata.token.Murmur3Token)2 RandomToken (com.datastax.oss.driver.internal.core.metadata.token.RandomToken)2 ImmutableSet (com.datastax.oss.driver.shaded.guava.common.collect.ImmutableSet)2