Search in sources :

Example 21 with Token

use of com.datastax.oss.driver.api.core.metadata.token.Token in project java-driver by datastax.

the class ByteOrderedTokenRange method split.

@Override
protected List<Token> split(Token rawStartToken, Token rawEndToken, int numberOfSplits) {
    int tokenOrder = rawStartToken.compareTo(rawEndToken);
    // we can't come up with a magic end value that would cover the whole ring
    if (tokenOrder == 0 && rawStartToken.equals(ByteOrderedTokenFactory.MIN_TOKEN)) {
        throw new IllegalArgumentException("Cannot split whole ring with ordered partitioner");
    }
    ByteOrderedToken startToken = (ByteOrderedToken) rawStartToken;
    ByteOrderedToken endToken = (ByteOrderedToken) rawEndToken;
    int significantBytes;
    BigInteger start, end, range, ringEnd, ringLength;
    BigInteger bigNumberOfSplits = BigInteger.valueOf(numberOfSplits);
    if (tokenOrder < 0) {
        // Since tokens are compared lexicographically, convert to integers using the largest length
        // (ex: given 0x0A and 0x0BCD, switch to 0x0A00 and 0x0BCD)
        significantBytes = Math.max(startToken.getValue().capacity(), endToken.getValue().capacity());
        // If the number of splits does not fit in the difference between the two integers, use more
        // bytes (ex: cannot fit 4 splits between 0x01 and 0x03, so switch to 0x0100 and 0x0300)
        // At most 4 additional bytes will be needed, since numberOfSplits is an integer.
        int addedBytes = 0;
        while (true) {
            start = toBigInteger(startToken.getValue(), significantBytes);
            end = toBigInteger(endToken.getValue(), significantBytes);
            range = end.subtract(start);
            if (addedBytes == 4 || start.equals(end) || range.compareTo(bigNumberOfSplits) >= 0) {
                break;
            }
            significantBytes += 1;
            addedBytes += 1;
        }
        // won't be used
        ringEnd = ringLength = null;
    } else {
        // Same logic except that we wrap around the ring
        significantBytes = Math.max(startToken.getValue().capacity(), endToken.getValue().capacity());
        int addedBytes = 0;
        while (true) {
            start = toBigInteger(startToken.getValue(), significantBytes);
            end = toBigInteger(endToken.getValue(), significantBytes);
            ringLength = TWO.pow(significantBytes * 8);
            ringEnd = ringLength.subtract(BigInteger.ONE);
            range = end.subtract(start).add(ringLength);
            if (addedBytes == 4 || range.compareTo(bigNumberOfSplits) >= 0) {
                break;
            }
            significantBytes += 1;
            addedBytes += 1;
        }
    }
    List<BigInteger> values = super.split(start, range, ringEnd, ringLength, numberOfSplits);
    List<Token> tokens = Lists.newArrayListWithExpectedSize(values.size());
    for (BigInteger value : values) {
        tokens.add(new ByteOrderedToken(toBytes(value, significantBytes)));
    }
    return tokens;
}
Also used : BigInteger(java.math.BigInteger) Token(com.datastax.oss.driver.api.core.metadata.token.Token)

Example 22 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_tokens_and_ranges.

@Test
public void should_create_tokens_and_ranges() {
    TokenMap tokenMap = getTokenMap();
    // Pick a random range
    TokenRange range = tokenMap.getTokenRanges().iterator().next();
    Token start = tokenMap.parse(tokenMap.format(range.getStart()));
    Token end = tokenMap.parse(tokenMap.format(range.getEnd()));
    assertThat(tokenMap.newTokenRange(start, end)).isEqualTo(range);
}
Also used : TokenRange(com.datastax.oss.driver.api.core.metadata.token.TokenRange) Token(com.datastax.oss.driver.api.core.metadata.token.Token) TokenMap(com.datastax.oss.driver.api.core.metadata.TokenMap) Test(org.junit.Test)

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