Skip to content
This repository has been archived by the owner on May 27, 2020. It is now read-only.

Commit

Permalink
Improve token range query planning.
Browse files Browse the repository at this point in the history
  • Loading branch information
adelapena committed May 24, 2016
1 parent 3651e2c commit f8ff3de
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -283,16 +283,10 @@ ByteBuffer clusteringKey(Composite composite) {
* @param key the partition key
* @param start the start clustering prefix
* @param stop the stop clustering prefix
* @param acceptLowerConflicts if rows with the same token before key should be accepted
* @param acceptUpperConflicts if rows with the same token after key should be accepted
* @return the Lucene query
*/
public Query query(DecoratedKey key,
Composite start,
Composite stop,
boolean acceptLowerConflicts,
boolean acceptUpperConflicts) {
return new KeyQuery(this, key, start, stop, acceptLowerConflicts, acceptUpperConflicts);
public Query query(DecoratedKey key, Composite start, Composite stop) {
return new KeyQuery(this, key, start, stop);
}

/**
Expand Down
35 changes: 11 additions & 24 deletions plugin/src/main/java/com/stratio/cassandra/lucene/key/KeyQuery.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@
*/
package com.stratio.cassandra.lucene.key;

import com.google.common.base.Objects;
import org.apache.cassandra.db.DecoratedKey;
import org.apache.cassandra.db.composites.CellNameType;
import org.apache.cassandra.db.composites.Composite;
import org.apache.cassandra.db.marshal.UTF8Type;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.lucene.index.FilteredTermsEnum;
import org.apache.lucene.index.Terms;
import org.apache.lucene.index.TermsEnum;
Expand All @@ -31,8 +31,6 @@

import java.io.IOException;
import java.nio.ByteBuffer;
import java.util.Arrays;
import java.util.List;

/**
* {@link MultiTermQuery} to get a range of clustering keys.
Expand All @@ -46,7 +44,6 @@ class KeyQuery extends MultiTermQuery {
private final ByteBuffer collatedToken;
private final Composite start, stop;
private final CellNameType clusteringComparator;
private final boolean acceptLowerConflicts, acceptUpperConflicts;
private final BytesRef seek;

/**
Expand All @@ -56,23 +53,17 @@ class KeyQuery extends MultiTermQuery {
* @param key the partition key
* @param start the start clustering
* @param stop the stop clustering
* @param acceptLowerConflicts if accept lower token conflicts
* @param acceptUpperConflicts if accept upper token conflicts
*/
KeyQuery(KeyMapper mapper,
DecoratedKey key,
Composite start,
Composite stop,
boolean acceptLowerConflicts,
boolean acceptUpperConflicts) {
Composite stop) {
super(KeyMapper.FIELD_NAME);
this.mapper = mapper;
this.key = key;
this.collatedToken = TokenMapper.toCollated(key.getToken());
this.start = start;
this.stop = stop;
this.acceptLowerConflicts = acceptLowerConflicts;
this.acceptUpperConflicts = acceptUpperConflicts;
clusteringComparator = mapper.clusteringComparator();
seek = mapper.seek(key);
}
Expand All @@ -86,22 +77,19 @@ protected TermsEnum getTermsEnum(Terms terms, AttributeSource atts) throws IOExc
/** {@inheritDoc} */
@Override
public String toString(String field) {
return new ToStringBuilder(this).append("field", field)
.append("key", key)
.append("start", start == null ? null : mapper.toString(start))
.append("stop", stop == null ? null : mapper.toString(stop))

.toString();
return Objects.toStringHelper(this)
.add("field", field)
.add("key", key)
.add("start", start == null ? null : mapper.toString(start))
.add("stop", stop == null ? null : mapper.toString(stop))
.toString();
}

private class FullKeyDataRangeFilteredTermsEnum extends FilteredTermsEnum {

FullKeyDataRangeFilteredTermsEnum(TermsEnum tenum) {
super(tenum);
if (start != null) {
List<ByteBuffer> list = Arrays.asList(mapper.clusteringType().split(mapper.clusteringKey(start)));
setInitialSeekTerm(seek);
}
setInitialSeekTerm(seek);
}

/** {@inheritDoc} */
Expand All @@ -121,16 +109,15 @@ protected AcceptStatus accept(BytesRef term) {

// Check partition key
Integer keyComparison = entry.getDecoratedKey().compareTo(key);
if (keyComparison < 0 && !acceptLowerConflicts) {
if (keyComparison < 0) {
return AcceptStatus.NO;
}
if (keyComparison > 0 && !acceptUpperConflicts) {
if (keyComparison > 0) {
return AcceptStatus.NO;
}

// Check clustering key range
Composite clustering = entry.getComposite();

if (start != null && !start.isEmpty() && clusteringComparator.compare(start, clustering) > 0) {
return AcceptStatus.NO;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ public Term term(DecoratedKey partitionKey, CellName clusteringKey) {
/** {@inheritDoc} */
@Override
public Query query(DataRange dataRange) {

RowPosition startPosition = dataRange.startKey();
RowPosition stopPosition = dataRange.stopKey();
Token startToken = startPosition.getToken();
Expand All @@ -153,21 +154,21 @@ public Query query(DataRange dataRange) {
Composite stopName = sqf.finish();

if ((isSameToken) && (startPosition instanceof DecoratedKey)) {
return keyMapper.query((DecoratedKey) startPosition, startName, stopName, includeStart, includeStop);
return keyMapper.query((DecoratedKey) startPosition, startName, stopName);
}

BooleanQuery.Builder builder = new BooleanQuery.Builder();

if (!startName.isEmpty()) {
DecoratedKey startKey = (DecoratedKey) startPosition;
Query query = keyMapper.query(startKey, startName, null, false, true);
Query query = keyMapper.query(startKey, startName, null);
builder.add(query, occur);
includeStart = false;
}

if (!stopName.isEmpty()) {
DecoratedKey stopKey = (DecoratedKey) stopPosition;
Query query = keyMapper.query(stopKey, null, stopName, true, false);
Query query = keyMapper.query(stopKey, null, stopName);
builder.add(query, occur);
includeStop = false;
}
Expand Down Expand Up @@ -204,7 +205,7 @@ public Query query(RowKey rowKey) {
* RangeTombstone}.
*/
public Query query(DecoratedKey partitionKey, RangeTombstone rangeTombstone) {
return keyMapper.query(partitionKey, rangeTombstone.min, rangeTombstone.max, false, false);
return keyMapper.query(partitionKey, rangeTombstone.min, rangeTombstone.max);
}

/**
Expand Down

0 comments on commit f8ff3de

Please sign in to comment.