2021-10-15 22:03:53 +02:00
|
|
|
/*
|
|
|
|
* Licensed to the Apache Software Foundation (ASF) under one or more
|
|
|
|
* contributor license agreements. See the NOTICE file distributed with
|
|
|
|
* this work for additional information regarding copyright ownership.
|
|
|
|
* The ASF licenses this file to You under the Apache License, Version 2.0
|
|
|
|
* (the "License"); you may not use this file except in compliance with
|
|
|
|
* the License. You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package it.cavallium.dbengine.lucene.comparators;
|
|
|
|
|
2021-12-18 21:01:14 +01:00
|
|
|
import it.cavallium.dbengine.database.SafeCloseable;
|
2021-10-15 22:03:53 +02:00
|
|
|
import it.cavallium.dbengine.database.disk.LLTempLMDBEnv;
|
|
|
|
import it.cavallium.dbengine.lucene.DoubleCodec;
|
|
|
|
import it.cavallium.dbengine.lucene.IArray;
|
|
|
|
import it.cavallium.dbengine.lucene.LMDBArray;
|
|
|
|
import it.cavallium.dbengine.lucene.SortFieldCodec;
|
2021-12-18 21:01:14 +01:00
|
|
|
import java.io.Closeable;
|
2021-10-15 22:03:53 +02:00
|
|
|
import java.io.IOException;
|
|
|
|
import org.apache.lucene.document.DoublePoint;
|
|
|
|
import org.apache.lucene.index.LeafReaderContext;
|
|
|
|
import org.apache.lucene.search.LeafFieldComparator;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Comparator based on {@link Double#compare} for {@code numHits}. This comparator provides a
|
|
|
|
* skipping functionality - an iterator that can skip over non-competitive documents.
|
2021-11-16 23:19:13 +01:00
|
|
|
* Based on {@link org.apache.lucene.search.comparators.DoubleComparator}
|
2021-10-15 22:03:53 +02:00
|
|
|
*/
|
2021-12-18 21:01:14 +01:00
|
|
|
public class DoubleComparator extends NumericComparator<Double> implements SafeCloseable {
|
2021-10-15 22:03:53 +02:00
|
|
|
private final IArray<Double> values;
|
|
|
|
protected double topValue;
|
|
|
|
protected double bottom;
|
|
|
|
|
|
|
|
public DoubleComparator(LLTempLMDBEnv env,
|
|
|
|
int numHits, String field, Double missingValue, boolean reverse, int sortPos) {
|
|
|
|
super(field, missingValue != null ? missingValue : 0.0, reverse, sortPos, Double.BYTES);
|
|
|
|
values = new LMDBArray<>(env, new DoubleCodec(), numHits, 0d);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int compare(int slot1, int slot2) {
|
|
|
|
return Double.compare(values.getOrDefault(slot1, 0d), values.getOrDefault(slot2, 0d));
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void setTopValue(Double value) {
|
|
|
|
super.setTopValue(value);
|
|
|
|
topValue = value;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public Double value(int slot) {
|
|
|
|
return values.getOrDefault(slot, 0d);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public LeafFieldComparator getLeafComparator(LeafReaderContext context) throws IOException {
|
|
|
|
return new DoubleLeafComparator(context);
|
|
|
|
}
|
|
|
|
|
2021-12-18 21:01:14 +01:00
|
|
|
@Override
|
|
|
|
public void close() {
|
|
|
|
if (values instanceof SafeCloseable closeable) {
|
|
|
|
closeable.close();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Leaf comparator for {@link DoubleComparator} that provides skipping functionality */
|
2021-10-15 22:03:53 +02:00
|
|
|
public class DoubleLeafComparator extends NumericLeafComparator {
|
|
|
|
|
|
|
|
public DoubleLeafComparator(LeafReaderContext context) throws IOException {
|
|
|
|
super(context);
|
|
|
|
}
|
|
|
|
|
|
|
|
private double getValueForDoc(int doc) throws IOException {
|
|
|
|
if (docValues.advanceExact(doc)) {
|
|
|
|
return Double.longBitsToDouble(docValues.longValue());
|
|
|
|
} else {
|
|
|
|
return missingValue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void setBottom(int slot) throws IOException {
|
|
|
|
bottom = values.getOrDefault(slot, 0d);
|
|
|
|
super.setBottom(slot);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int compareBottom(int doc) throws IOException {
|
|
|
|
return Double.compare(bottom, getValueForDoc(doc));
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int compareTop(int doc) throws IOException {
|
|
|
|
return Double.compare(topValue, getValueForDoc(doc));
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void copy(int slot, int doc) throws IOException {
|
|
|
|
values.set(slot, getValueForDoc(doc));
|
|
|
|
super.copy(slot, doc);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected boolean isMissingValueCompetitive() {
|
|
|
|
int result = Double.compare(missingValue, bottom);
|
|
|
|
return reverse ? (result >= 0) : (result <= 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void encodeBottom(byte[] packedValue) {
|
|
|
|
DoublePoint.encodeDimension(bottom, packedValue, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void encodeTop(byte[] packedValue) {
|
|
|
|
DoublePoint.encodeDimension(topValue, packedValue, 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|