Search in sources :

Example 26 with DecoratedKey

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());
}
Also used : DecoratedKey(org.apache.cassandra.db.DecoratedKey) BufferDecoratedKey(org.apache.cassandra.db.BufferDecoratedKey) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) File(org.apache.cassandra.io.util.File) Test(org.junit.Test)

Example 27 with DecoratedKey

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();
}
Also used : DecoratedKey(org.apache.cassandra.db.DecoratedKey) BufferDecoratedKey(org.apache.cassandra.db.BufferDecoratedKey)

Example 28 with DecoratedKey

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;
}
Also used : LongCursor(com.carrotsearch.hppc.cursors.LongCursor) DecoratedKey(org.apache.cassandra.db.DecoratedKey) BufferDecoratedKey(org.apache.cassandra.db.BufferDecoratedKey) LongSet(com.carrotsearch.hppc.LongSet) Pair(org.apache.cassandra.utils.Pair)

Example 29 with DecoratedKey

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);
}
Also used : DecoratedKey(org.apache.cassandra.db.DecoratedKey) BufferDecoratedKey(org.apache.cassandra.db.BufferDecoratedKey)

Example 30 with DecoratedKey

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);
}
Also used : DecoratedKey(org.apache.cassandra.db.DecoratedKey) CharacterCodingException(java.nio.charset.CharacterCodingException) ByteBuffer(java.nio.ByteBuffer)

Aggregations

DecoratedKey (org.apache.cassandra.db.DecoratedKey)80 ColumnFamilyStore (org.apache.cassandra.db.ColumnFamilyStore)31 Test (org.junit.Test)28 SSTableReader (org.apache.cassandra.io.sstable.format.SSTableReader)22 ByteBuffer (java.nio.ByteBuffer)21 TableMetadata (org.apache.cassandra.schema.TableMetadata)20 Keyspace (org.apache.cassandra.db.Keyspace)18 RowUpdateBuilder (org.apache.cassandra.db.RowUpdateBuilder)16 ColumnFamily (org.apache.cassandra.db.ColumnFamily)8 QueryPath (org.apache.cassandra.db.filter.QueryPath)8 File (org.apache.cassandra.io.util.File)8 ArrayList (java.util.ArrayList)7 UUID (java.util.UUID)7 RowMutation (org.apache.cassandra.db.RowMutation)7 Table (org.apache.cassandra.db.Table)7 Row (org.apache.cassandra.db.rows.Row)7 UnfilteredRowIterator (org.apache.cassandra.db.rows.UnfilteredRowIterator)7 Token (org.apache.cassandra.dht.Token)7 BufferDecoratedKey (org.apache.cassandra.db.BufferDecoratedKey)6 RowIndexEntry (org.apache.cassandra.db.RowIndexEntry)5