use of org.apache.cassandra.db.DecoratedKey in project cassandra by apache.
the class OnDiskIndexTest method testSparseMode.
@Test
public void testSparseMode() throws Exception {
OnDiskIndexBuilder builder = new OnDiskIndexBuilder(UTF8Type.instance, LongType.instance, OnDiskIndexBuilder.Mode.SPARSE);
final long start = System.currentTimeMillis();
final int numIterations = 100000;
for (long i = 0; i < numIterations; i++) builder.add(LongType.instance.decompose(start + i), keyAt(i), i);
File index = FileUtils.createTempFile("on-disk-sa-sparse", "db");
index.deleteOnExit();
builder.finish(index);
OnDiskIndex onDisk = new OnDiskIndex(index, LongType.instance, new KeyConverter());
ThreadLocalRandom random = ThreadLocalRandom.current();
for (long step = start; step < (start + numIterations); step += 1000) {
boolean lowerInclusive = random.nextBoolean();
boolean upperInclusive = random.nextBoolean();
long limit = random.nextLong(step, start + numIterations);
RangeIterator<Long, Token> rows = onDisk.search(expressionFor(step, lowerInclusive, limit, upperInclusive));
long lowerKey = step - start;
long upperKey = lowerKey + (limit - step);
if (!lowerInclusive)
lowerKey += 1;
if (upperInclusive)
upperKey += 1;
Set<DecoratedKey> actual = convert(rows);
for (long key = lowerKey; key < upperKey; key++) Assert.assertTrue("key" + key + " wasn't found", actual.contains(keyAt(key)));
Assert.assertEquals((upperKey - lowerKey), actual.size());
}
// let's also explicitly test whole range search
RangeIterator<Long, Token> rows = onDisk.search(expressionFor(start, true, start + numIterations, true));
Set<DecoratedKey> actual = convert(rows);
Assert.assertEquals(numIterations, actual.size());
}
use of org.apache.cassandra.db.DecoratedKey in project cassandra by apache.
the class OnDiskIndexTest method keyBuilder.
private static TokenTreeBuilder keyBuilder(Long... keys) {
TokenTreeBuilder builder = new DynamicTokenTreeBuilder();
for (final Long key : keys) {
DecoratedKey dk = keyAt(key);
builder.add((Long) dk.getToken().getTokenValue(), key);
}
return builder.finish();
}
use of org.apache.cassandra.db.DecoratedKey in project cassandra by apache.
the class OnDiskIndexTest method convert.
private static Set<DecoratedKey> convert(TokenTreeBuilder offsets) {
Set<DecoratedKey> result = new HashSet<>();
Iterator<Pair<Long, LongSet>> offsetIter = offsets.iterator();
while (offsetIter.hasNext()) {
LongSet v = offsetIter.next().right;
for (LongCursor offset : v) result.add(keyAt(offset.value));
}
return result;
}
use of org.apache.cassandra.db.DecoratedKey in project cassandra by apache.
the class TokenTreeTest method testTokenMerge.
public void testTokenMerge(boolean isStatic) throws Exception {
final long min = 0, max = 1000;
// two different trees with the same offsets
TokenTree treeA = generateTree(min, max, isStatic);
TokenTree treeB = generateTree(min, max, isStatic);
RangeIterator<Long, Token> a = treeA.iterator(new KeyConverter());
RangeIterator<Long, Token> b = treeB.iterator(new KeyConverter());
long count = min;
while (a.hasNext() && b.hasNext()) {
final Token tokenA = a.next();
final Token tokenB = b.next();
// merging of two OnDiskToken
tokenA.merge(tokenB);
// merging with RAM Token with different offset
tokenA.merge(new TokenWithOffsets(tokenA.get(), convert(count + 1)));
// and RAM token with the same offset
tokenA.merge(new TokenWithOffsets(tokenA.get(), convert(count)));
// should fail when trying to merge different tokens
try {
tokenA.merge(new TokenWithOffsets(tokenA.get() + 1, convert(count)));
Assert.fail();
} catch (IllegalArgumentException e) {
// expected
}
final Set<Long> offsets = new TreeSet<>();
for (DecoratedKey key : tokenA) offsets.add(LongType.instance.compose(key.getKey()));
Set<Long> expected = new TreeSet<>();
{
expected.add(count);
expected.add(count + 1);
}
Assert.assertEquals(expected, offsets);
count++;
}
Assert.assertEquals(max, count - 1);
}
use of org.apache.cassandra.db.DecoratedKey in project eiger by wlloyd.
the class RandomPartitioner method convertFromDiskFormat.
public DecoratedKey<BigIntegerToken> convertFromDiskFormat(ByteBuffer fromdisk) {
// find the delimiter position
int splitPoint = -1;
for (int i = fromdisk.position(); i < fromdisk.limit(); i++) {
if (fromdisk.get(i) == DELIMITER_BYTE) {
splitPoint = i;
break;
}
}
assert splitPoint != -1;
// and decode the token and key
String token = null;
try {
token = ByteBufferUtil.string(fromdisk, fromdisk.position(), splitPoint - fromdisk.position());
} catch (CharacterCodingException e) {
throw new RuntimeException(e);
}
ByteBuffer key = fromdisk.duplicate();
key.position(splitPoint + 1);
return new DecoratedKey<BigIntegerToken>(new BigIntegerToken(token), key);
}
Aggregations