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;
}
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);
}
Aggregations