Example usage for org.apache.cassandra.dht IPartitioner midpoint

List of usage examples for org.apache.cassandra.dht IPartitioner midpoint

Introduction

In this page you can find the example usage for org.apache.cassandra.dht IPartitioner midpoint.

Prototype

public Token midpoint(Token left, Token right);

Source Link

Document

Calculate a Token representing the approximate "middle" of the given range.

Usage

From source file:com.stratio.deep.cassandra.cql.RangeUtils.java

License:Apache License

/**
 * Recursive function that splits a given token range to a given number of token ranges.
 *
 * @param range        the token range to be splitted.
 * @param partitioner  the cassandra partitioner.
 * @param bisectFactor the actual number of pieces the original token range will be splitted to.
 * @param accumulator  a token range accumulator (ne
 *///w  w w.j  av  a2  s  .com
private static void bisectTokeRange(DeepTokenRange range, final IPartitioner partitioner,
        final int bisectFactor, final List<DeepTokenRange> accumulator) {

    final AbstractType tkValidator = partitioner.getTokenValidator();

    Token leftToken = partitioner.getTokenFactory().fromByteArray(tkValidator.decompose(range.getStartToken()));
    Token rightToken = partitioner.getTokenFactory().fromByteArray(tkValidator.decompose(range.getEndToken()));
    Token midToken = partitioner.midpoint(leftToken, rightToken);

    Comparable midpoint = (Comparable) tkValidator.compose(tkValidator.fromString(midToken.toString()));

    DeepTokenRange left = new DeepTokenRange(range.getStartToken(), midpoint, range.getReplicas());
    DeepTokenRange right = new DeepTokenRange(midpoint, range.getEndToken(), range.getReplicas());

    if (bisectFactor / 2 <= 1) {
        accumulator.add(left);
        accumulator.add(right);
    } else {
        bisectTokeRange(left, partitioner, bisectFactor / 2, accumulator);
        bisectTokeRange(right, partitioner, bisectFactor / 2, accumulator);
    }
}

From source file:kina.cql.RangeUtils.java

License:Apache License

/**
 * Recursive function that splits a given token range to a given number of tolen ranges.
 *
 * @param range the token range to be splitted.
 * @param partitioner the cassandra partitioner.
 * @param bisectFactor the actual number of pieces the original token range will be splitted to.
 * @param accumulator a token range accumulator (ne
 *///ww  w .ja v a 2s . c  o  m
private static void bisectTokeRange(Range range, final IPartitioner partitioner, final int bisectFactor,
        final List<Range> accumulator) {

    final AbstractType tkValidator = partitioner.getTokenValidator();

    Token leftToken = partitioner.getTokenFactory().fromByteArray(tkValidator.decompose(range.getStartToken()));
    Token rightToken = partitioner.getTokenFactory().fromByteArray(tkValidator.decompose(range.getEndToken()));
    Token midToken = partitioner.midpoint(leftToken, rightToken);

    Comparable midpoint = (Comparable) tkValidator.compose(tkValidator.fromString(midToken.toString()));

    Range left = new Range(range.getStartToken(), midpoint, range.getReplicas());
    Range right = new Range(midpoint, range.getEndToken(), range.getReplicas());

    if (bisectFactor / 2 <= 1) {
        accumulator.add(left);
        accumulator.add(right);
    } else {
        bisectTokeRange(left, partitioner, bisectFactor / 2, accumulator);
        bisectTokeRange(right, partitioner, bisectFactor / 2, accumulator);
    }
}