From 5819e57baa65263b212ee4cb2015956782c961c0 Mon Sep 17 00:00:00 2001 From: kel Date: Wed, 31 Jan 2018 22:42:11 +0800 Subject: [PATCH] Replace Bits with new abstract class to respresent documents that have a value (#24088) (#28334) --- .../index/fielddata/DocValueBits.java | 32 +++++++ .../index/fielddata/FieldData.java | 96 +++++-------------- .../bucket/missing/MissingAggregator.java | 14 ++- .../aggregations/support/ValuesSource.java | 27 +++--- 4 files changed, 77 insertions(+), 92 deletions(-) create mode 100644 server/src/main/java/org/elasticsearch/index/fielddata/DocValueBits.java diff --git a/server/src/main/java/org/elasticsearch/index/fielddata/DocValueBits.java b/server/src/main/java/org/elasticsearch/index/fielddata/DocValueBits.java new file mode 100644 index 00000000000..fcfe7c9a8ce --- /dev/null +++ b/server/src/main/java/org/elasticsearch/index/fielddata/DocValueBits.java @@ -0,0 +1,32 @@ +/* + * 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.index.fielddata; + +import java.io.IOException; + +public abstract class DocValueBits { + + /** + * Advance this instance to the given document id + * + * @return true if there is a value for this document + */ + public abstract boolean advanceExact(int doc) throws IOException; +} diff --git a/server/src/main/java/org/elasticsearch/index/fielddata/FieldData.java b/server/src/main/java/org/elasticsearch/index/fielddata/FieldData.java index a1ac341b56f..eb569aa4c2b 100644 --- a/server/src/main/java/org/elasticsearch/index/fielddata/FieldData.java +++ b/server/src/main/java/org/elasticsearch/index/fielddata/FieldData.java @@ -24,7 +24,6 @@ import org.apache.lucene.index.DocValues; import org.apache.lucene.index.NumericDocValues; import org.apache.lucene.index.SortedNumericDocValues; import org.apache.lucene.index.SortedSetDocValues; -import org.apache.lucene.util.Bits; import org.apache.lucene.util.BytesRef; import org.elasticsearch.common.geo.GeoPoint; @@ -92,109 +91,64 @@ public enum FieldData { } /** - * Returns a {@link Bits} representing all documents from dv that have a value. + * Returns a {@link DocValueBits} representing all documents from values that have a value. */ - public static Bits docsWithValue(final SortedBinaryDocValues dv, final int maxDoc) { - return new Bits() { + public static DocValueBits docsWithValue(final SortedBinaryDocValues values) { + return new DocValueBits() { @Override - public boolean get(int index) { - try { - return dv.advanceExact(index); - } catch (IOException e) { - throw new RuntimeException(e); - } - } - - @Override - public int length() { - return maxDoc; + public boolean advanceExact(int doc) throws IOException { + return values.advanceExact(doc); } }; } /** - * Returns a {@link Bits} representing all documents from dv + * Returns a {@link DocValueBits} representing all documents from docValues * that have a value. */ - public static Bits docsWithValue(final SortedSetDocValues dv, final int maxDoc) { - return new Bits() { + public static DocValueBits docsWithValue(final SortedSetDocValues docValues) { + return new DocValueBits() { @Override - public boolean get(int index) { - try { - return dv.advanceExact(index); - } catch (IOException e) { - throw new RuntimeException(e); - } - } - - @Override - public int length() { - return maxDoc; + public boolean advanceExact(int doc) throws IOException { + return docValues.advanceExact(doc); } }; } /** - * Returns a Bits representing all documents from dv that have + * Returns a {@link DocValueBits} representing all documents from pointValues that have * a value. */ - public static Bits docsWithValue(final MultiGeoPointValues dv, final int maxDoc) { - return new Bits() { + public static DocValueBits docsWithValue(final MultiGeoPointValues pointValues) { + return new DocValueBits() { @Override - public boolean get(int index) { - try { - return dv.advanceExact(index); - } catch (IOException e) { - throw new RuntimeException(e); - } - } - - @Override - public int length() { - return maxDoc; + public boolean advanceExact(int doc) throws IOException { + return pointValues.advanceExact(doc); } }; } /** - * Returns a Bits representing all documents from dv that have a value. + * Returns a {@link DocValueBits} representing all documents from doubleValues that have a value. */ - public static Bits docsWithValue(final SortedNumericDoubleValues dv, final int maxDoc) { - return new Bits() { + public static DocValueBits docsWithValue(final SortedNumericDoubleValues doubleValues) { + return new DocValueBits() { @Override - public boolean get(int index) { - try { - return dv.advanceExact(index); - } catch (IOException e) { - throw new RuntimeException(e); - } - } - - @Override - public int length() { - return maxDoc; + public boolean advanceExact(int doc) throws IOException { + return doubleValues.advanceExact(doc); } }; } /** - * Returns a Bits representing all documents from dv that have + * Returns a {@link DocValueBits} representing all documents from docValues that have * a value. */ - public static Bits docsWithValue(final SortedNumericDocValues dv, final int maxDoc) { - return new Bits() { + public static DocValueBits docsWithValue(final SortedNumericDocValues docValues) { + return new DocValueBits() { @Override - public boolean get(int index) { - try { - return dv.advanceExact(index); - } catch (IOException e) { - throw new RuntimeException(e); - } - } - - @Override - public int length() { - return maxDoc; + public boolean advanceExact(int doc) throws IOException { + return docValues.advanceExact(doc); } }; } diff --git a/server/src/main/java/org/elasticsearch/search/aggregations/bucket/missing/MissingAggregator.java b/server/src/main/java/org/elasticsearch/search/aggregations/bucket/missing/MissingAggregator.java index 5864f5c5ca7..c4be1eef808 100644 --- a/server/src/main/java/org/elasticsearch/search/aggregations/bucket/missing/MissingAggregator.java +++ b/server/src/main/java/org/elasticsearch/search/aggregations/bucket/missing/MissingAggregator.java @@ -19,7 +19,7 @@ package org.elasticsearch.search.aggregations.bucket.missing; import org.apache.lucene.index.LeafReaderContext; -import org.apache.lucene.util.Bits; +import org.elasticsearch.index.fielddata.DocValueBits; import org.elasticsearch.search.aggregations.Aggregator; import org.elasticsearch.search.aggregations.AggregatorFactories; import org.elasticsearch.search.aggregations.InternalAggregation; @@ -49,17 +49,21 @@ public class MissingAggregator extends BucketsAggregator implements SingleBucket @Override public LeafBucketCollector getLeafCollector(LeafReaderContext ctx, final LeafBucketCollector sub) throws IOException { - - final Bits docsWithValue; + final DocValueBits docsWithValue; if (valuesSource != null) { docsWithValue = valuesSource.docsWithValue(ctx); } else { - docsWithValue = new Bits.MatchNoBits(ctx.reader().maxDoc()); + docsWithValue = new DocValueBits() { + @Override + public boolean advanceExact(int doc) throws IOException { + return false; + } + }; } return new LeafBucketCollectorBase(sub, docsWithValue) { @Override public void collect(int doc, long bucket) throws IOException { - if (docsWithValue != null && !docsWithValue.get(doc)) { + if (docsWithValue.advanceExact(doc) == false) { collectBucket(sub, doc, bucket); } } diff --git a/server/src/main/java/org/elasticsearch/search/aggregations/support/ValuesSource.java b/server/src/main/java/org/elasticsearch/search/aggregations/support/ValuesSource.java index 6dc2758fa5c..c3b67f1c9b3 100644 --- a/server/src/main/java/org/elasticsearch/search/aggregations/support/ValuesSource.java +++ b/server/src/main/java/org/elasticsearch/search/aggregations/support/ValuesSource.java @@ -27,12 +27,12 @@ import org.apache.lucene.index.SortedNumericDocValues; import org.apache.lucene.index.SortedSetDocValues; import org.apache.lucene.search.IndexSearcher; import org.apache.lucene.search.Scorer; -import org.apache.lucene.util.Bits; import org.apache.lucene.util.BytesRef; import org.elasticsearch.common.lucene.ScorerAware; import org.elasticsearch.common.util.CollectionUtils; import org.elasticsearch.index.fielddata.AbstractSortingNumericDocValues; import org.elasticsearch.index.fielddata.AtomicOrdinalsFieldData; +import org.elasticsearch.index.fielddata.DocValueBits; import org.elasticsearch.index.fielddata.IndexFieldData; import org.elasticsearch.index.fielddata.IndexGeoPointFieldData; import org.elasticsearch.index.fielddata.IndexNumericFieldData; @@ -58,7 +58,7 @@ public abstract class ValuesSource { */ public abstract SortedBinaryDocValues bytesValues(LeafReaderContext context) throws IOException; - public abstract Bits docsWithValue(LeafReaderContext context) throws IOException; + public abstract DocValueBits docsWithValue(LeafReaderContext context) throws IOException; /** Whether this values source needs scores. */ public boolean needsScores() { @@ -68,10 +68,9 @@ public abstract class ValuesSource { public abstract static class Bytes extends ValuesSource { @Override - public Bits docsWithValue(LeafReaderContext context) throws IOException { + public DocValueBits docsWithValue(LeafReaderContext context) throws IOException { final SortedBinaryDocValues bytes = bytesValues(context); - return org.elasticsearch.index.fielddata.FieldData.docsWithValue(bytes, - context.reader().maxDoc()); + return org.elasticsearch.index.fielddata.FieldData.docsWithValue(bytes); } public abstract static class WithOrdinals extends Bytes { @@ -101,10 +100,9 @@ public abstract class ValuesSource { }; @Override - public Bits docsWithValue(LeafReaderContext context) throws IOException { + public DocValueBits docsWithValue(LeafReaderContext context) throws IOException { final SortedSetDocValues ordinals = ordinalsValues(context); - return org.elasticsearch.index.fielddata.FieldData.docsWithValue(ordinals, - context.reader().maxDoc()); + return org.elasticsearch.index.fielddata.FieldData.docsWithValue(ordinals); } public abstract SortedSetDocValues ordinalsValues(LeafReaderContext context) @@ -241,15 +239,13 @@ public abstract class ValuesSource { public abstract SortedNumericDoubleValues doubleValues(LeafReaderContext context) throws IOException; @Override - public Bits docsWithValue(LeafReaderContext context) throws IOException { + public DocValueBits docsWithValue(LeafReaderContext context) throws IOException { if (isFloatingPoint()) { final SortedNumericDoubleValues values = doubleValues(context); - return org.elasticsearch.index.fielddata.FieldData.docsWithValue(values, - context.reader().maxDoc()); + return org.elasticsearch.index.fielddata.FieldData.docsWithValue(values); } else { final SortedNumericDocValues values = longValues(context); - return org.elasticsearch.index.fielddata.FieldData.docsWithValue(values, - context.reader().maxDoc()); + return org.elasticsearch.index.fielddata.FieldData.docsWithValue(values); } } @@ -493,10 +489,9 @@ public abstract class ValuesSource { }; @Override - public Bits docsWithValue(LeafReaderContext context) { + public DocValueBits docsWithValue(LeafReaderContext context) throws IOException { final MultiGeoPointValues geoPoints = geoPointValues(context); - return org.elasticsearch.index.fielddata.FieldData.docsWithValue(geoPoints, - context.reader().maxDoc()); + return org.elasticsearch.index.fielddata.FieldData.docsWithValue(geoPoints); } public abstract MultiGeoPointValues geoPointValues(LeafReaderContext context);