use of com.thinkaurelius.titan.diskstorage.keycolumnvalue.KeyRange in project titan by thinkaurelius.
the class CassandraHelper method transformRange.
public static KeyRange transformRange(Token leftKeyExclusive, Token rightKeyInclusive) {
if (!(leftKeyExclusive instanceof BytesToken))
throw new UnsupportedOperationException();
// if left part is BytesToken, right part should be too, otherwise there is no sense in the ring
assert rightKeyInclusive instanceof BytesToken;
// l is exclusive, r is inclusive
BytesToken l = (BytesToken) leftKeyExclusive;
BytesToken r = (BytesToken) rightKeyInclusive;
byte[] leftTokenValue = l.getTokenValue();
byte[] rightTokenValue = r.getTokenValue();
Preconditions.checkArgument(leftTokenValue.length == rightTokenValue.length, "Tokens have unequal length");
int tokenLength = leftTokenValue.length;
byte[][] tokens = new byte[][] { leftTokenValue, rightTokenValue };
byte[][] plusOne = new byte[2][tokenLength];
for (int j = 0; j < 2; j++) {
boolean carry = true;
for (int i = tokenLength - 1; i >= 0; i--) {
byte b = tokens[j][i];
if (carry) {
b++;
carry = false;
}
if (b == 0)
carry = true;
plusOne[j][i] = b;
}
}
StaticBuffer lb = StaticArrayBuffer.of(plusOne[0]);
StaticBuffer rb = StaticArrayBuffer.of(plusOne[1]);
Preconditions.checkArgument(lb.length() == tokenLength, lb.length());
Preconditions.checkArgument(rb.length() == tokenLength, rb.length());
return new KeyRange(lb, rb);
}
use of com.thinkaurelius.titan.diskstorage.keycolumnvalue.KeyRange in project titan by thinkaurelius.
the class PartitionIDRangeTest method convert.
public static List<KeyRange> convert(long lower, long upper, int bitwidth) {
StaticBuffer lowerBuffer = BufferUtil.getLongBuffer(convert(lower, bitwidth));
StaticBuffer upperBuffer = BufferUtil.getLongBuffer(convert(upper, bitwidth));
// Preconditions.checkArgument(lowerBuffer.compareTo(upperBuffer) < 0, "%s vs %s",lowerBuffer,upperBuffer);
return Lists.newArrayList(new KeyRange(lowerBuffer, upperBuffer));
}
use of com.thinkaurelius.titan.diskstorage.keycolumnvalue.KeyRange in project titan by thinkaurelius.
the class PartitionIDRange method getIDRanges.
public static List<PartitionIDRange> getIDRanges(final int partitionBits, final List<KeyRange> locals) {
Preconditions.checkArgument(partitionBits > 0 && partitionBits < (Integer.SIZE - 1));
Preconditions.checkArgument(locals != null && !locals.isEmpty(), "KeyRanges are empty");
final int partitionIdBound = (1 << (partitionBits));
final int backShift = Integer.SIZE - partitionBits;
List<PartitionIDRange> partitionRanges = Lists.newArrayList();
for (KeyRange local : locals) {
Preconditions.checkArgument(local.getStart().length() >= 4);
Preconditions.checkArgument(local.getEnd().length() >= 4);
if (local.getStart().equals(local.getEnd())) {
//Start=End => Partition spans entire range
partitionRanges.add(new PartitionIDRange(0, partitionIdBound, partitionIdBound));
continue;
}
int startInt = local.getStart().getInt(0);
int lowerID = startInt >>> backShift;
assert lowerID >= 0 && lowerID < partitionIdBound;
//Lower id must be inclusive, so check that we did not truncate anything!
boolean truncatedBits = (lowerID << backShift) != startInt;
StaticBuffer start = local.getAt(0);
for (int i = 4; i < start.length() && !truncatedBits; i++) {
if (start.getByte(i) != 0)
truncatedBits = true;
}
//adjust to make sure we are inclusive
if (truncatedBits)
lowerID += 1;
//upper id is exclusive
int upperID = local.getEnd().getInt(0) >>> backShift;
//Check that we haven't jumped order indicating that the interval was too small
if ((local.getStart().compareTo(local.getEnd()) < 0 && lowerID >= upperID)) {
discardRange(local);
continue;
}
//ensure that lowerID remains within range
lowerID = lowerID % partitionIdBound;
if (lowerID == upperID) {
//After re-normalizing, check for interval colision
discardRange(local);
continue;
}
partitionRanges.add(new PartitionIDRange(lowerID, upperID, partitionIdBound));
}
return partitionRanges;
}
Aggregations