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();
}
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;
}
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();
}
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;
}
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;
}
Aggregations