Aggregations: clean-up comparisons.

This commit is contained in:
Adrien Grand 2014-01-09 16:15:57 +01:00
parent ca454248a7
commit 71aad7cddc
11 changed files with 79 additions and 74 deletions

View File

@ -28,10 +28,6 @@ import org.apache.lucene.util.IntroSorter;
public enum CollectionUtils { public enum CollectionUtils {
; ;
private static int compare(long i, long j) {
return i < j ? -1 : (i == j ? 0 : 1);
}
public static void sort(LongArrayList list) { public static void sort(LongArrayList list) {
sort(list.buffer, list.size()); sort(list.buffer, list.size());
} }
@ -50,7 +46,7 @@ public enum CollectionUtils {
@Override @Override
protected int compare(int i, int j) { protected int compare(int i, int j) {
return CollectionUtils.compare(array[i], array[j]); return Comparators.compare(array[i], array[j]);
} }
@Override @Override
@ -60,7 +56,7 @@ public enum CollectionUtils {
@Override @Override
protected int comparePivot(int j) { protected int comparePivot(int j) {
return CollectionUtils.compare(pivot, array[j]); return Comparators.compare(pivot, array[j]);
} }
}.sort(0, len); }.sort(0, len);

View File

@ -0,0 +1,34 @@
/*
* Licensed to Elasticsearch under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch 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 org.elasticsearch.common.util;
/** Comparison utility methods. */
public enum Comparators {
;
public static int compare(long a, long b) {
return a < b ? -1 : a == b ? 0 : 1;
}
public static int compare(double a, double b, boolean asc) {
return asc ? Double.compare(a, b) : Double.compare(b, a);
}
}

View File

@ -20,6 +20,7 @@
package org.elasticsearch.search.aggregations.bucket; package org.elasticsearch.search.aggregations.bucket;
import org.elasticsearch.ElasticsearchIllegalArgumentException; import org.elasticsearch.ElasticsearchIllegalArgumentException;
import org.elasticsearch.common.util.Comparators;
import org.elasticsearch.search.aggregations.Aggregations; import org.elasticsearch.search.aggregations.Aggregations;
import org.elasticsearch.search.aggregations.metrics.MetricsAggregation; import org.elasticsearch.search.aggregations.metrics.MetricsAggregation;
@ -75,12 +76,7 @@ public interface Bucket {
public int compare(B b1, B b2) { public int compare(B b1, B b2) {
double v1 = value(b1); double v1 = value(b1);
double v2 = value(b2); double v2 = value(b2);
if (v1 > v2) { return Comparators.compare(v1, v2, asc);
return asc ? 1 : -1;
} else if (v1 < v2) {
return asc ? -1 : 1;
}
return 0;
} }
private double value(B bucket) { private double value(B bucket) {

View File

@ -19,6 +19,7 @@
package org.elasticsearch.search.aggregations.bucket.histogram; package org.elasticsearch.search.aggregations.bucket.histogram;
import org.elasticsearch.common.util.Comparators;
import org.elasticsearch.common.xcontent.ToXContent; import org.elasticsearch.common.xcontent.ToXContent;
import org.elasticsearch.search.aggregations.Aggregation; import org.elasticsearch.search.aggregations.Aggregation;
@ -61,33 +62,25 @@ interface HistogramBase<B extends HistogramBase.Bucket> extends Aggregation, Ite
public static final Order KEY_ASC = new InternalOrder((byte) 1, "_key", true, new Comparator<HistogramBase.Bucket>() { public static final Order KEY_ASC = new InternalOrder((byte) 1, "_key", true, new Comparator<HistogramBase.Bucket>() {
@Override @Override
public int compare(HistogramBase.Bucket b1, HistogramBase.Bucket b2) { public int compare(HistogramBase.Bucket b1, HistogramBase.Bucket b2) {
if (b1.getKey() > b2.getKey()) { return Comparators.compare(b1.getKey(), b2.getKey());
return 1;
}
if (b1.getKey() < b2.getKey()) {
return -1;
}
return 0;
} }
}); });
public static final Order KEY_DESC = new InternalOrder((byte) 2, "_key", false, new Comparator<HistogramBase.Bucket>() { public static final Order KEY_DESC = new InternalOrder((byte) 2, "_key", false, new Comparator<HistogramBase.Bucket>() {
@Override @Override
public int compare(HistogramBase.Bucket b1, HistogramBase.Bucket b2) { public int compare(HistogramBase.Bucket b1, HistogramBase.Bucket b2) {
return -KEY_ASC.comparator().compare(b1, b2); return - Comparators.compare(b1.getKey(), b2.getKey());
} }
}); });
public static final Order COUNT_ASC = new InternalOrder((byte) 3, "_count", true, new Comparator<HistogramBase.Bucket>() { public static final Order COUNT_ASC = new InternalOrder((byte) 3, "_count", true, new Comparator<HistogramBase.Bucket>() {
@Override @Override
public int compare(HistogramBase.Bucket b1, HistogramBase.Bucket b2) { public int compare(HistogramBase.Bucket b1, HistogramBase.Bucket b2) {
if (b1.getDocCount() > b2.getDocCount()) { int cmp = Comparators.compare(b1.getDocCount(), b2.getDocCount());
return 1; if (cmp == 0) {
cmp = Comparators.compare(b1.getKey(), b2.getKey());
} }
if (b1.getDocCount() < b2.getDocCount()) { return cmp;
return -1;
}
return 0;
} }
}); });
@ -95,7 +88,11 @@ interface HistogramBase<B extends HistogramBase.Bucket> extends Aggregation, Ite
public static final Order COUNT_DESC = new InternalOrder((byte) 4, "_count", false, new Comparator<HistogramBase.Bucket>() { public static final Order COUNT_DESC = new InternalOrder((byte) 4, "_count", false, new Comparator<HistogramBase.Bucket>() {
@Override @Override
public int compare(HistogramBase.Bucket b1, HistogramBase.Bucket b2) { public int compare(HistogramBase.Bucket b1, HistogramBase.Bucket b2) {
return -COUNT_ASC.comparator().compare(b1, b2); int cmp = - Comparators.compare(b1.getDocCount(), b2.getDocCount());
if (cmp == 0) {
cmp = Comparators.compare(b1.getKey(), b2.getKey());
}
return cmp;
} }
}); });

View File

@ -79,15 +79,10 @@ public class DoubleTerms extends InternalTerms {
} }
@Override @Override
protected int compareTerm(Terms.Bucket other) { int compareTerm(Terms.Bucket other) {
if (term > other.getKeyAsNumber().doubleValue()) { return Double.compare(term, other.getKeyAsNumber().doubleValue());
return 1;
}
if (term < other.getKeyAsNumber().doubleValue()) {
return -1;
}
return 0;
} }
} }
private ValueFormatter valueFormatter; private ValueFormatter valueFormatter;

View File

@ -21,6 +21,7 @@ package org.elasticsearch.search.aggregations.bucket.terms;
import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.io.stream.StreamOutput;
import org.elasticsearch.common.util.Comparators;
import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.search.aggregations.AggregationExecutionException; import org.elasticsearch.search.aggregations.AggregationExecutionException;
import org.elasticsearch.search.aggregations.Aggregator; import org.elasticsearch.search.aggregations.Aggregator;
@ -41,14 +42,11 @@ class InternalOrder extends Terms.Order {
public static final InternalOrder COUNT_DESC = new InternalOrder((byte) 1, "_count", false, new Comparator<Terms.Bucket>() { public static final InternalOrder COUNT_DESC = new InternalOrder((byte) 1, "_count", false, new Comparator<Terms.Bucket>() {
@Override @Override
public int compare(Terms.Bucket o1, Terms.Bucket o2) { public int compare(Terms.Bucket o1, Terms.Bucket o2) {
long i = o2.getDocCount() - o1.getDocCount(); int cmp = - Comparators.compare(o1.getDocCount(), o2.getDocCount());
if (i == 0) { if (cmp == 0) {
i = o2.compareTo(o1); cmp = o1.compareTerm(o2);
if (i == 0) {
i = System.identityHashCode(o2) - System.identityHashCode(o1);
}
} }
return i > 0 ? 1 : -1; return cmp;
} }
}); });
@ -59,7 +57,11 @@ class InternalOrder extends Terms.Order {
@Override @Override
public int compare(Terms.Bucket o1, Terms.Bucket o2) { public int compare(Terms.Bucket o1, Terms.Bucket o2) {
return -COUNT_DESC.comparator(null).compare(o1, o2); int cmp = Comparators.compare(o1.getDocCount(), o2.getDocCount());
if (cmp == 0) {
cmp = o1.compareTerm(o2);
}
return cmp;
} }
}); });
@ -70,7 +72,7 @@ class InternalOrder extends Terms.Order {
@Override @Override
public int compare(Terms.Bucket o1, Terms.Bucket o2) { public int compare(Terms.Bucket o1, Terms.Bucket o2) {
return o2.compareTo(o1); return - o1.compareTerm(o2);
} }
}); });
@ -81,7 +83,7 @@ class InternalOrder extends Terms.Order {
@Override @Override
public int compare(Terms.Bucket o1, Terms.Bucket o2) { public int compare(Terms.Bucket o1, Terms.Bucket o2) {
return -TERM_DESC.comparator(null).compare(o1, o2); return o1.compareTerm(o2);
} }
}); });

View File

@ -36,7 +36,7 @@ import java.util.*;
*/ */
public abstract class InternalTerms extends InternalAggregation implements Terms, ToXContent, Streamable { public abstract class InternalTerms extends InternalAggregation implements Terms, ToXContent, Streamable {
public static abstract class Bucket implements Terms.Bucket { public static abstract class Bucket extends Terms.Bucket {
long bucketOrd; long bucketOrd;
@ -58,20 +58,6 @@ public abstract class InternalTerms extends InternalAggregation implements Terms
return aggregations; return aggregations;
} }
@Override
public int compareTo(Terms.Bucket o) {
long i = compareTerm(o);
if (i == 0) {
i = docCount - o.getDocCount();
if (i == 0) {
i = System.identityHashCode(this) - System.identityHashCode(o);
}
}
return i > 0 ? 1 : -1;
}
protected abstract int compareTerm(Terms.Bucket other);
public Bucket reduce(List<? extends Bucket> buckets, CacheRecycler cacheRecycler) { public Bucket reduce(List<? extends Bucket> buckets, CacheRecycler cacheRecycler) {
if (buckets.size() == 1) { if (buckets.size() == 1) {
return buckets.get(0); return buckets.get(0);

View File

@ -25,6 +25,7 @@ import org.elasticsearch.common.io.stream.StreamOutput;
import org.elasticsearch.common.recycler.Recycler; import org.elasticsearch.common.recycler.Recycler;
import org.elasticsearch.common.text.StringText; import org.elasticsearch.common.text.StringText;
import org.elasticsearch.common.text.Text; import org.elasticsearch.common.text.Text;
import org.elasticsearch.common.util.Comparators;
import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.search.aggregations.AggregationStreams; import org.elasticsearch.search.aggregations.AggregationStreams;
import org.elasticsearch.search.aggregations.InternalAggregation; import org.elasticsearch.search.aggregations.InternalAggregation;
@ -80,15 +81,8 @@ public class LongTerms extends InternalTerms {
} }
@Override @Override
protected int compareTerm(Terms.Bucket other) { int compareTerm(Terms.Bucket other) {
long otherTerm = other.getKeyAsNumber().longValue(); return Comparators.compare(term, other.getKeyAsNumber().longValue());
if (this.term > otherTerm) {
return 1;
}
if (this.term < otherTerm) {
return -1;
}
return 0;
} }
} }

View File

@ -77,7 +77,7 @@ public class StringTerms extends InternalTerms {
} }
@Override @Override
protected int compareTerm(Terms.Bucket other) { int compareTerm(Terms.Bucket other) {
return BytesRef.getUTF8SortedAsUnicodeComparator().compare(termBytes, ((Bucket) other).termBytes); return BytesRef.getUTF8SortedAsUnicodeComparator().compare(termBytes, ((Bucket) other).termBytes);
} }
} }

View File

@ -20,6 +20,7 @@
package org.elasticsearch.search.aggregations.bucket.terms; package org.elasticsearch.search.aggregations.bucket.terms;
import org.elasticsearch.common.text.Text; import org.elasticsearch.common.text.Text;
import org.elasticsearch.common.util.Comparators;
import org.elasticsearch.common.xcontent.ToXContent; import org.elasticsearch.common.xcontent.ToXContent;
import org.elasticsearch.search.aggregations.Aggregation; import org.elasticsearch.search.aggregations.Aggregation;
import org.elasticsearch.search.aggregations.Aggregator; import org.elasticsearch.search.aggregations.Aggregator;
@ -59,11 +60,14 @@ public interface Terms extends Aggregation, Iterable<Terms.Bucket> {
} }
} }
static interface Bucket extends Comparable<Bucket>, org.elasticsearch.search.aggregations.bucket.Bucket { static abstract class Bucket implements org.elasticsearch.search.aggregations.bucket.Bucket {
Text getKey(); public abstract Text getKey();
public abstract Number getKeyAsNumber();
abstract int compareTerm(Terms.Bucket other);
Number getKeyAsNumber();
} }
Collection<Bucket> buckets(); Collection<Bucket> buckets();

View File

@ -26,6 +26,7 @@ import org.apache.lucene.util.BytesRefHash;
import org.apache.lucene.util.InPlaceMergeSorter; import org.apache.lucene.util.InPlaceMergeSorter;
import org.elasticsearch.common.lucene.ReaderContextAware; import org.elasticsearch.common.lucene.ReaderContextAware;
import org.elasticsearch.common.util.CollectionUtils; import org.elasticsearch.common.util.CollectionUtils;
import org.elasticsearch.common.util.Comparators;
import org.elasticsearch.index.fielddata.*; import org.elasticsearch.index.fielddata.*;
import org.elasticsearch.index.fielddata.AtomicFieldData.Order; import org.elasticsearch.index.fielddata.AtomicFieldData.Order;
import org.elasticsearch.script.SearchScript; import org.elasticsearch.script.SearchScript;
@ -632,7 +633,7 @@ public abstract class FieldDataSource {
protected int compare(int i, int j) { protected int compare(int i, int j) {
final long l1 = array[i]; final long l1 = array[i];
final long l2 = array[j]; final long l2 = array[j];
return l1 < l2 ? -1 : l1 == l2 ? 0 : 1; return Comparators.compare(l1, l2);
} }
}; };