2021-10-14 23:04:34 +02:00
|
|
|
package it.cavallium.dbengine.lucene;
|
|
|
|
|
2021-10-15 22:03:53 +02:00
|
|
|
import static org.apache.lucene.search.SortField.STRING_LAST;
|
|
|
|
|
2022-04-06 02:41:32 +02:00
|
|
|
import it.cavallium.dbengine.database.disk.LLTempHugePqEnv;
|
2021-10-15 22:03:53 +02:00
|
|
|
import it.cavallium.dbengine.lucene.comparators.DoubleComparator;
|
|
|
|
import it.cavallium.dbengine.lucene.comparators.FloatComparator;
|
|
|
|
import it.cavallium.dbengine.lucene.comparators.IntComparator;
|
|
|
|
import it.cavallium.dbengine.lucene.comparators.LongComparator;
|
|
|
|
import it.cavallium.dbengine.lucene.comparators.RelevanceComparator;
|
|
|
|
import it.cavallium.dbengine.lucene.comparators.TermOrdValComparator;
|
|
|
|
import java.io.IOException;
|
|
|
|
import org.apache.commons.lang3.NotImplementedException;
|
|
|
|
import org.apache.lucene.index.DocValues;
|
|
|
|
import org.apache.lucene.index.LeafReaderContext;
|
|
|
|
import org.apache.lucene.index.NumericDocValues;
|
2021-10-14 23:04:34 +02:00
|
|
|
import org.apache.lucene.search.FieldComparator;
|
2021-10-15 22:03:53 +02:00
|
|
|
import org.apache.lucene.search.LeafFieldComparator;
|
2021-10-14 23:04:34 +02:00
|
|
|
import org.apache.lucene.search.SortField;
|
2021-10-15 22:03:53 +02:00
|
|
|
import org.apache.lucene.search.SortedNumericSelector;
|
2022-04-09 02:45:42 +02:00
|
|
|
import it.cavallium.dbengine.lucene.hugepq.search.comparators.HugePqDocComparator;
|
2021-10-14 23:04:34 +02:00
|
|
|
|
2022-04-06 02:41:32 +02:00
|
|
|
public class HugePqComparator {
|
2021-10-14 23:04:34 +02:00
|
|
|
|
2022-04-06 02:41:32 +02:00
|
|
|
public static FieldComparator<?> getComparator(LLTempHugePqEnv env, SortField sortField,
|
2022-04-30 21:56:42 +02:00
|
|
|
int numHits, boolean enableSkipping) {
|
2021-10-15 22:03:53 +02:00
|
|
|
var sortFieldClass = sortField.getClass();
|
|
|
|
if (sortFieldClass == org.apache.lucene.search.SortedNumericSortField.class) {
|
|
|
|
var nf = (org.apache.lucene.search.SortedNumericSortField) sortField;
|
|
|
|
var type = nf.getNumericType();
|
|
|
|
var missingValue = nf.getMissingValue();
|
|
|
|
var reverse = nf.getReverse();
|
|
|
|
var selector = nf.getSelector();
|
|
|
|
final FieldComparator<?> fieldComparator = switch (type) {
|
2022-04-30 21:56:42 +02:00
|
|
|
case INT -> new IntComparator(env, numHits, nf.getField(), (Integer) missingValue, reverse, enableSkipping) {
|
2021-10-15 22:03:53 +02:00
|
|
|
@Override
|
|
|
|
public LeafFieldComparator getLeafComparator(LeafReaderContext context) throws IOException {
|
|
|
|
return new IntLeafComparator(context) {
|
|
|
|
@Override
|
|
|
|
protected NumericDocValues getNumericDocValues(LeafReaderContext context, String field)
|
|
|
|
throws IOException {
|
|
|
|
return SortedNumericSelector.wrap(DocValues.getSortedNumeric(context.reader(), field), selector, type);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
};
|
2022-04-30 21:56:42 +02:00
|
|
|
case FLOAT -> new FloatComparator(env, numHits, nf.getField(), (Float) missingValue, reverse, enableSkipping) {
|
2021-10-15 22:03:53 +02:00
|
|
|
@Override
|
|
|
|
public LeafFieldComparator getLeafComparator(LeafReaderContext context) throws IOException {
|
|
|
|
return new FloatLeafComparator(context) {
|
|
|
|
@Override
|
|
|
|
protected NumericDocValues getNumericDocValues(LeafReaderContext context, String field)
|
|
|
|
throws IOException {
|
|
|
|
return SortedNumericSelector.wrap(DocValues.getSortedNumeric(context.reader(), field), selector, type);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
};
|
2022-04-30 21:56:42 +02:00
|
|
|
case LONG -> new LongComparator(env, numHits, nf.getField(), (Long) missingValue, reverse, enableSkipping) {
|
2021-10-15 22:03:53 +02:00
|
|
|
@Override
|
|
|
|
public LeafFieldComparator getLeafComparator(LeafReaderContext context) throws IOException {
|
|
|
|
return new LongLeafComparator(context) {
|
|
|
|
@Override
|
|
|
|
protected NumericDocValues getNumericDocValues(LeafReaderContext context, String field)
|
|
|
|
throws IOException {
|
|
|
|
return SortedNumericSelector.wrap(DocValues.getSortedNumeric(context.reader(), field), selector, type);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
};
|
2022-04-30 21:56:42 +02:00
|
|
|
case DOUBLE -> new DoubleComparator(env, numHits, nf.getField(), (Double) missingValue, reverse, enableSkipping) {
|
2021-10-15 22:03:53 +02:00
|
|
|
@Override
|
|
|
|
public LeafFieldComparator getLeafComparator(LeafReaderContext context) throws IOException {
|
|
|
|
return new DoubleLeafComparator(context) {
|
|
|
|
@Override
|
|
|
|
protected NumericDocValues getNumericDocValues(LeafReaderContext context, String field)
|
|
|
|
throws IOException {
|
|
|
|
return SortedNumericSelector.wrap(DocValues.getSortedNumeric(context.reader(), field), selector, type);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
};
|
|
|
|
case CUSTOM, DOC, REWRITEABLE, STRING_VAL, SCORE, STRING -> throw new AssertionError();
|
|
|
|
};
|
|
|
|
if (!nf.getOptimizeSortWithPoints()) {
|
|
|
|
fieldComparator.disableSkipping();
|
|
|
|
}
|
|
|
|
return fieldComparator;
|
|
|
|
} else if (sortFieldClass == SortField.class) {
|
|
|
|
var missingValue = sortField.getMissingValue();
|
|
|
|
var reverse = sortField.getReverse();
|
|
|
|
var field = sortField.getField();
|
|
|
|
var comparatorSource = sortField.getComparatorSource();
|
|
|
|
return switch (sortField.getType()) {
|
|
|
|
case SCORE -> new RelevanceComparator(env, numHits);
|
2022-04-30 21:56:42 +02:00
|
|
|
case DOC -> new HugePqDocComparator(env, numHits, reverse, enableSkipping);
|
2021-10-15 22:03:53 +02:00
|
|
|
case INT -> new IntComparator(env, numHits, field, (Integer) missingValue,
|
2022-04-30 21:56:42 +02:00
|
|
|
reverse, enableSkipping);
|
2021-10-15 22:03:53 +02:00
|
|
|
case FLOAT -> new FloatComparator(env, numHits, field, (Float) missingValue,
|
2022-04-30 21:56:42 +02:00
|
|
|
reverse, enableSkipping);
|
2021-10-15 22:03:53 +02:00
|
|
|
case LONG -> new LongComparator(env, numHits, field, (Long) missingValue,
|
2022-04-30 21:56:42 +02:00
|
|
|
reverse, enableSkipping);
|
2021-10-15 22:03:53 +02:00
|
|
|
case DOUBLE -> new DoubleComparator(env, numHits, field, (Double) missingValue,
|
2022-04-30 21:56:42 +02:00
|
|
|
reverse, enableSkipping);
|
2021-10-15 22:03:53 +02:00
|
|
|
case CUSTOM -> {
|
|
|
|
assert comparatorSource != null;
|
2022-04-30 21:56:42 +02:00
|
|
|
yield comparatorSource.newComparator(field, numHits, enableSkipping, reverse);
|
2021-10-15 22:03:53 +02:00
|
|
|
}
|
|
|
|
case STRING -> new TermOrdValComparator(env, numHits, field, missingValue == STRING_LAST);
|
|
|
|
case STRING_VAL -> throw new NotImplementedException("String val sort field not implemented");
|
|
|
|
case REWRITEABLE -> throw new IllegalStateException(
|
|
|
|
"SortField needs to be rewritten through Sort.rewrite(..) and SortField.rewrite(..)");
|
|
|
|
};
|
|
|
|
} else {
|
|
|
|
throw new NotImplementedException("SortField type not implemented: " + sortFieldClass.getName());
|
|
|
|
}
|
2021-10-14 23:04:34 +02:00
|
|
|
}
|
|
|
|
}
|