Search in sources :

Example 1 with Expression

use of org.apache.cassandra.index.sasi.plan.Expression in project cassandra by apache.

the class SkipListMemIndex method search.

public RangeIterator<Long, Token> search(Expression expression) {
    ByteBuffer min = expression.lower == null ? null : expression.lower.value;
    ByteBuffer max = expression.upper == null ? null : expression.upper.value;
    SortedMap<ByteBuffer, ConcurrentSkipListSet<DecoratedKey>> search;
    if (min == null && max == null) {
        throw new IllegalArgumentException();
    }
    if (min != null && max != null) {
        search = index.subMap(min, expression.lower.inclusive, max, expression.upper.inclusive);
    } else if (min == null) {
        search = index.headMap(max, expression.upper.inclusive);
    } else {
        search = index.tailMap(min, expression.lower.inclusive);
    }
    RangeUnionIterator.Builder<Long, Token> builder = RangeUnionIterator.builder();
    search.values().stream().filter(keys -> !keys.isEmpty()).forEach(keys -> builder.add(new KeyRangeIterator(keys)));
    return builder.build();
}
Also used : RangeIterator(org.apache.cassandra.index.sasi.utils.RangeIterator) java.util(java.util) ConcurrentSkipListMap(java.util.concurrent.ConcurrentSkipListMap) DecoratedKey(org.apache.cassandra.db.DecoratedKey) ConcurrentSkipListSet(java.util.concurrent.ConcurrentSkipListSet) Expression(org.apache.cassandra.index.sasi.plan.Expression) Token(org.apache.cassandra.index.sasi.disk.Token) RangeUnionIterator(org.apache.cassandra.index.sasi.utils.RangeUnionIterator) AbstractType(org.apache.cassandra.db.marshal.AbstractType) ByteBuffer(java.nio.ByteBuffer) ColumnIndex(org.apache.cassandra.index.sasi.conf.ColumnIndex) ConcurrentSkipListSet(java.util.concurrent.ConcurrentSkipListSet) RangeUnionIterator(org.apache.cassandra.index.sasi.utils.RangeUnionIterator) Token(org.apache.cassandra.index.sasi.disk.Token) ByteBuffer(java.nio.ByteBuffer)

Example 2 with Expression

use of org.apache.cassandra.index.sasi.plan.Expression in project cassandra by apache.

the class OnDiskIndexTest method expressionForNot.

private static Expression expressionForNot(AbstractType<?> validator, ByteBuffer lower, ByteBuffer upper, Iterable<ByteBuffer> terms) {
    Expression expression = new Expression("", validator);
    expression.setOp(Expression.Op.RANGE);
    expression.setLower(new Expression.Bound(lower, true));
    expression.setUpper(new Expression.Bound(upper, true));
    for (ByteBuffer term : terms) expression.add(Operator.NEQ, term);
    return expression;
}
Also used : Expression(org.apache.cassandra.index.sasi.plan.Expression) ByteBuffer(java.nio.ByteBuffer)

Example 3 with Expression

use of org.apache.cassandra.index.sasi.plan.Expression in project cassandra by apache.

the class OnDiskIndexTest method testRangeQueryWithExclusions.

@Test
public void testRangeQueryWithExclusions() throws Exception {
    final long lower = 0;
    final long upper = 100000;
    OnDiskIndexBuilder builder = new OnDiskIndexBuilder(UTF8Type.instance, LongType.instance, OnDiskIndexBuilder.Mode.SPARSE);
    for (long i = lower; i <= upper; i++) builder.add(LongType.instance.decompose(i), keyAt(i), i);
    File index = File.createTempFile("on-disk-sa-except-long-ranges", "db");
    index.deleteOnExit();
    builder.finish(index);
    OnDiskIndex onDisk = new OnDiskIndex(index, LongType.instance, new KeyConverter());
    ThreadLocalRandom random = ThreadLocalRandom.current();
    // single exclusion
    // let's do small range first to figure out if searchPoint works properly
    validateExclusions(onDisk, lower, 50, Sets.newHashSet(42L));
    // now let's do whole data set to test SPARSE searching
    validateExclusions(onDisk, lower, upper, Sets.newHashSet(31337L));
    // pair of exclusions which would generate a split
    validateExclusions(onDisk, lower, random.nextInt(400, 800), Sets.newHashSet(42L, 154L));
    validateExclusions(onDisk, lower, upper, Sets.newHashSet(31337L, 54631L));
    // 3 exclusions which would generate a split and change bounds
    validateExclusions(onDisk, lower, random.nextInt(400, 800), Sets.newHashSet(42L, 154L));
    validateExclusions(onDisk, lower, upper, Sets.newHashSet(31337L, 54631L));
    validateExclusions(onDisk, lower, random.nextLong(400, upper), Sets.newHashSet(42L, 55L));
    validateExclusions(onDisk, lower, random.nextLong(400, upper), Sets.newHashSet(42L, 55L, 93L));
    validateExclusions(onDisk, lower, random.nextLong(400, upper), Sets.newHashSet(42L, 55L, 93L, 205L));
    Set<Long> exclusions = Sets.newHashSet(3L, 12L, 13L, 14L, 27L, 54L, 81L, 125L, 384L, 771L, 1054L, 2048L, 78834L);
    // test that exclusions are properly bound by lower/upper of the expression
    Assert.assertEquals(392, validateExclusions(onDisk, lower, 400, exclusions, false));
    Assert.assertEquals(101, validateExclusions(onDisk, lower, 100, Sets.newHashSet(-10L, -5L, -1L), false));
    validateExclusions(onDisk, lower, upper, exclusions);
    Assert.assertEquals(100000, convert(onDisk.search(new Expression("", LongType.instance).add(Operator.NEQ, LongType.instance.decompose(100L)))).size());
    Assert.assertEquals(49, convert(onDisk.search(new Expression("", LongType.instance).add(Operator.LT, LongType.instance.decompose(50L)).add(Operator.NEQ, LongType.instance.decompose(10L)))).size());
    Assert.assertEquals(99998, convert(onDisk.search(new Expression("", LongType.instance).add(Operator.GT, LongType.instance.decompose(1L)).add(Operator.NEQ, LongType.instance.decompose(20L)))).size());
    onDisk.close();
}
Also used : Expression(org.apache.cassandra.index.sasi.plan.Expression) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) File(java.io.File) Test(org.junit.Test)

Example 4 with Expression

use of org.apache.cassandra.index.sasi.plan.Expression in project cassandra by apache.

the class OnDiskIndexTest method expressionFor.

private static Expression expressionFor(long lower, boolean lowerInclusive, long upper, boolean upperInclusive) {
    Expression expression = new Expression("", LongType.instance);
    expression.add(lowerInclusive ? Operator.GTE : Operator.GT, LongType.instance.decompose(lower));
    expression.add(upperInclusive ? Operator.LTE : Operator.LT, LongType.instance.decompose(upper));
    return expression;
}
Also used : Expression(org.apache.cassandra.index.sasi.plan.Expression)

Example 5 with Expression

use of org.apache.cassandra.index.sasi.plan.Expression in project cassandra by apache.

the class OnDiskIndexTest method expressionFor.

private static Expression expressionFor(Operator op, AbstractType<?> validator, ByteBuffer term) {
    Expression expression = new Expression("", validator);
    expression.add(op, term);
    return expression;
}
Also used : Expression(org.apache.cassandra.index.sasi.plan.Expression)

Aggregations

Expression (org.apache.cassandra.index.sasi.plan.Expression)7 ByteBuffer (java.nio.ByteBuffer)3 RangeUnionIterator (org.apache.cassandra.index.sasi.utils.RangeUnionIterator)2 File (java.io.File)1 java.util (java.util)1 ConcurrentSkipListMap (java.util.concurrent.ConcurrentSkipListMap)1 ConcurrentSkipListSet (java.util.concurrent.ConcurrentSkipListSet)1 ThreadLocalRandom (java.util.concurrent.ThreadLocalRandom)1 DecoratedKey (org.apache.cassandra.db.DecoratedKey)1 AbstractType (org.apache.cassandra.db.marshal.AbstractType)1 ColumnIndex (org.apache.cassandra.index.sasi.conf.ColumnIndex)1 Token (org.apache.cassandra.index.sasi.disk.Token)1 RangeIterator (org.apache.cassandra.index.sasi.utils.RangeIterator)1 Test (org.junit.Test)1