Skip to content

Commit

Permalink
Rename DocValueFetcher.Leaf to FormattedDocValues (#68818)
Browse files Browse the repository at this point in the history
Also moves it to a top-level interface in fielddata. It is not only used by
DocValueFetcher any more, and Leaf does not really describe what
it does or what it provides.
  • Loading branch information
romseygeek authored Feb 15, 2021
1 parent c10ba9e commit dbff7be
Show file tree
Hide file tree
Showing 10 changed files with 71 additions and 57 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import org.elasticsearch.common.xcontent.XContentParser.Token;
import org.elasticsearch.common.xcontent.support.XContentMapValues;
import org.elasticsearch.index.fielddata.FieldData;
import org.elasticsearch.index.fielddata.FormattedDocValues;
import org.elasticsearch.index.fielddata.IndexFieldData;
import org.elasticsearch.index.fielddata.IndexNumericFieldData;
import org.elasticsearch.index.fielddata.LeafNumericFieldData;
Expand Down Expand Up @@ -517,9 +518,9 @@ public int docValueCount() {
}

@Override
public DocValueFetcher.Leaf getLeafValueFetcher(DocValueFormat format) {
public FormattedDocValues getFormattedValues(DocValueFormat format) {
SortedNumericDoubleValues values = getDoubleValues();
return new DocValueFetcher.Leaf() {
return new FormattedDocValues() {
@Override
public boolean advanceExact(int docId) throws IOException {
return values.advanceExact(docId);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/

package org.elasticsearch.index.fielddata;

import java.io.IOException;

public interface FormattedDocValues {
/**
* Advance the doc values reader to the provided doc.
*
* @return false if there are no values for this document, true otherwise
*/
boolean advanceExact(int docId) throws IOException;

/**
* A count of the number of values at this document.
*/
int docValueCount() throws IOException;

/**
* Load and format the next value.
*/
Object nextValue() throws IOException;
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@

import org.apache.lucene.util.Accountable;
import org.elasticsearch.common.lease.Releasable;
import org.elasticsearch.index.mapper.DocValueFetcher;
import org.elasticsearch.search.DocValueFormat;

import java.io.IOException;
Expand All @@ -31,18 +30,18 @@ public interface LeafFieldData extends Accountable, Releasable {
SortedBinaryDocValues getBytesValues();

/**
* Return a value fetcher for this leaf implementation.
* Return a formatted representation of the values
*/
default DocValueFetcher.Leaf getLeafValueFetcher(DocValueFormat format) {
default FormattedDocValues getFormattedValues(DocValueFormat format) {
SortedBinaryDocValues values = getBytesValues();
return new DocValueFetcher.Leaf() {
return new FormattedDocValues() {
@Override
public boolean advanceExact(int docId) throws IOException {
return values.advanceExact(docId);
}

@Override
public int docValueCount() throws IOException {
public int docValueCount() {
return values.docValueCount();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@
import org.apache.lucene.index.SortedNumericDocValues;
import org.apache.lucene.util.Accountable;
import org.elasticsearch.index.fielddata.FieldData;
import org.elasticsearch.index.fielddata.FormattedDocValues;
import org.elasticsearch.index.fielddata.LeafNumericFieldData;
import org.elasticsearch.index.fielddata.ScriptDocValues;
import org.elasticsearch.index.fielddata.SortedBinaryDocValues;
import org.elasticsearch.index.fielddata.SortedNumericDoubleValues;
import org.elasticsearch.index.mapper.DocValueFetcher;
import org.elasticsearch.search.DocValueFormat;

import java.io.IOException;
Expand Down Expand Up @@ -71,9 +71,9 @@ public Collection<Accountable> getChildResources() {
}

@Override
public DocValueFetcher.Leaf getLeafValueFetcher(DocValueFormat format) {
public FormattedDocValues getFormattedValues(DocValueFormat format) {
SortedNumericDoubleValues values = getDoubleValues();
return new DocValueFetcher.Leaf() {
return new FormattedDocValues() {
@Override
public boolean advanceExact(int docId) throws IOException {
return values.advanceExact(docId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@

import org.apache.lucene.index.SortedNumericDocValues;
import org.elasticsearch.index.fielddata.FieldData;
import org.elasticsearch.index.fielddata.FormattedDocValues;
import org.elasticsearch.index.fielddata.IndexNumericFieldData.NumericType;
import org.elasticsearch.index.fielddata.LeafNumericFieldData;
import org.elasticsearch.index.fielddata.ScriptDocValues;
import org.elasticsearch.index.fielddata.SortedBinaryDocValues;
import org.elasticsearch.index.fielddata.SortedNumericDoubleValues;
import org.elasticsearch.index.mapper.DocValueFetcher;
import org.elasticsearch.search.DocValueFormat;

import java.io.IOException;
Expand Down Expand Up @@ -68,9 +68,9 @@ public final SortedNumericDoubleValues getDoubleValues() {
}

@Override
public DocValueFetcher.Leaf getLeafValueFetcher(DocValueFormat format) {
public FormattedDocValues getFormattedValues(DocValueFormat format) {
SortedNumericDocValues values = getLongValues();
return new DocValueFetcher.Leaf() {
return new FormattedDocValues() {
@Override
public boolean advanceExact(int docId) throws IOException {
return values.advanceExact(docId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import org.apache.lucene.util.NumericUtils;
import org.elasticsearch.common.time.DateUtils;
import org.elasticsearch.index.fielddata.FieldData;
import org.elasticsearch.index.fielddata.FormattedDocValues;
import org.elasticsearch.index.fielddata.IndexFieldData;
import org.elasticsearch.index.fielddata.IndexFieldData.XFieldComparatorSource.Nested;
import org.elasticsearch.index.fielddata.IndexFieldDataCache;
Expand All @@ -27,7 +28,6 @@
import org.elasticsearch.index.fielddata.NumericDoubleValues;
import org.elasticsearch.index.fielddata.SortedNumericDoubleValues;
import org.elasticsearch.index.fielddata.fieldcomparator.LongValuesComparatorSource;
import org.elasticsearch.index.mapper.DocValueFetcher;
import org.elasticsearch.indices.breaker.CircuitBreakerService;
import org.elasticsearch.search.DocValueFormat;
import org.elasticsearch.search.MultiValueMode;
Expand Down Expand Up @@ -164,10 +164,10 @@ public SortedNumericDocValues getLongValuesAsNanos() {
}

@Override
public DocValueFetcher.Leaf getLeafValueFetcher(DocValueFormat format) {
public FormattedDocValues getFormattedValues(DocValueFormat format) {
DocValueFormat nanosFormat = DocValueFormat.withNanosecondResolution(format);
SortedNumericDocValues values = getLongValuesAsNanos();
return new DocValueFetcher.Leaf() {
return new FormattedDocValues() {
@Override
public boolean advanceExact(int docId) throws IOException {
return values.advanceExact(docId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
package org.elasticsearch.index.mapper;

import org.apache.lucene.index.LeafReaderContext;
import org.elasticsearch.index.fielddata.FormattedDocValues;
import org.elasticsearch.index.fielddata.IndexFieldData;
import org.elasticsearch.search.DocValueFormat;
import org.elasticsearch.search.lookup.SourceLookup;
Expand All @@ -26,7 +27,7 @@
public final class DocValueFetcher implements ValueFetcher {
private final DocValueFormat format;
private final IndexFieldData<?> ifd;
private Leaf leaf;
private FormattedDocValues formattedDocValues;

public DocValueFetcher(DocValueFormat format, IndexFieldData<?> ifd) {
this.format = format;
Expand All @@ -35,36 +36,19 @@ public DocValueFetcher(DocValueFormat format, IndexFieldData<?> ifd) {

@Override
public void setNextReader(LeafReaderContext context) {
leaf = ifd.load(context).getLeafValueFetcher(format);
formattedDocValues = ifd.load(context).getFormattedValues(format);
}

@Override
public List<Object> fetchValues(SourceLookup lookup, Set<String> ignoredFields) throws IOException {
if (false == leaf.advanceExact(lookup.docId())) {
if (false == formattedDocValues.advanceExact(lookup.docId())) {
return emptyList();
}
List<Object> result = new ArrayList<Object>(leaf.docValueCount());
for (int i = 0, count = leaf.docValueCount(); i < count; ++i) {
result.add(leaf.nextValue());
List<Object> result = new ArrayList<>(formattedDocValues.docValueCount());
for (int i = 0, count = formattedDocValues.docValueCount(); i < count; ++i) {
result.add(formattedDocValues.nextValue());
}
return result;
}

public interface Leaf {
/**
* Advance the doc values reader to the provided doc.
* @return false if there are no values for this document, true otherwise
*/
boolean advanceExact(int docId) throws IOException;

/**
* A count of the number of values at this document.
*/
int docValueCount() throws IOException;

/**
* Load and format the next value.
*/
Object nextValue() throws IOException;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@
import org.apache.lucene.index.NumericDocValues;
import org.apache.lucene.index.SortedNumericDocValues;
import org.elasticsearch.index.fielddata.FieldData;
import org.elasticsearch.index.fielddata.FormattedDocValues;
import org.elasticsearch.index.fielddata.LeafNumericFieldData;
import org.elasticsearch.index.fielddata.NumericDoubleValues;
import org.elasticsearch.index.fielddata.ScriptDocValues;
import org.elasticsearch.index.fielddata.SortedBinaryDocValues;
import org.elasticsearch.index.fielddata.SortedNumericDoubleValues;
import org.elasticsearch.index.mapper.DocValueFetcher;
import org.elasticsearch.search.DocValueFormat;

import java.io.IOException;
Expand Down Expand Up @@ -94,9 +94,9 @@ public void close() {
}

@Override
public DocValueFetcher.Leaf getLeafValueFetcher(DocValueFormat format) {
public FormattedDocValues getFormattedValues(DocValueFormat format) {
SortedNumericDocValues values = getLongValues();
return new DocValueFetcher.Leaf() {
return new FormattedDocValues() {
@Override
public boolean advanceExact(int docId) throws IOException {
return values.advanceExact(docId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@

import org.apache.lucene.index.LeafReaderContext;
import org.apache.lucene.util.BytesRef;
import org.elasticsearch.index.fielddata.FormattedDocValues;
import org.elasticsearch.index.fielddata.IndexFieldData;
import org.elasticsearch.index.mapper.DocValueFetcher;
import org.elasticsearch.index.mapper.MappedFieldType;
import org.elasticsearch.index.query.SearchExecutionContext;
import org.elasticsearch.search.DocValueFormat;
Expand Down Expand Up @@ -46,9 +46,9 @@ protected FieldValueFetcher(String name,
this.valueFunc = valueFunc;
}

DocValueFetcher.Leaf getLeaf(LeafReaderContext context) {
final DocValueFetcher.Leaf delegate = fieldData.load(context).getLeafValueFetcher(DocValueFormat.RAW);
return new DocValueFetcher.Leaf() {
FormattedDocValues getLeaf(LeafReaderContext context) {
final FormattedDocValues delegate = fieldData.load(context).getFormattedValues(DocValueFormat.RAW);
return new FormattedDocValues() {
@Override
public boolean advanceExact(int docId) throws IOException {
return delegate.advanceExact(docId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@
import org.elasticsearch.core.internal.io.IOUtils;
import org.elasticsearch.index.IndexService;
import org.elasticsearch.index.engine.Engine;
import org.elasticsearch.index.fielddata.FormattedDocValues;
import org.elasticsearch.index.mapper.DateFieldMapper;
import org.elasticsearch.index.mapper.DocCountFieldMapper;
import org.elasticsearch.index.mapper.DocValueFetcher;
import org.elasticsearch.index.mapper.MappedFieldType;
import org.elasticsearch.index.query.SearchExecutionContext;
import org.elasticsearch.index.shard.IndexShard;
Expand Down Expand Up @@ -432,8 +432,8 @@ private BucketCollector(long timestamp,

@Override
public LeafCollector getLeafCollector(LeafReaderContext context) {
final List<DocValueFetcher.Leaf> groupFieldLeaves = leafFetchers(context, groupFieldFetchers);
final List<DocValueFetcher.Leaf> metricsFieldLeaves = leafFetchers(context, metricsFieldFetchers);
final List<FormattedDocValues> groupFieldLeaves = leafFetchers(context, groupFieldFetchers);
final List<FormattedDocValues> metricsFieldLeaves = leafFetchers(context, metricsFieldFetchers);
return new LeafCollector() {
@Override
public void setScorer(Scorable scorer) {
Expand All @@ -442,7 +442,7 @@ public void setScorer(Scorable scorer) {
@Override
public void collect(int docID) throws IOException {
List<List<Object>> combinationKeys = new ArrayList<>();
for (DocValueFetcher.Leaf leafField : groupFieldLeaves) {
for (FormattedDocValues leafField : groupFieldLeaves) {
if (leafField.advanceExact(docID)) {
List<Object> lst = new ArrayList<>();
for (int i = 0; i < leafField.docValueCount(); i++) {
Expand All @@ -456,11 +456,11 @@ public void collect(int docID) throws IOException {

final BytesRef valueBytes;
try (BytesStreamOutput out = new BytesStreamOutput()) {
for (DocValueFetcher.Leaf leaf : metricsFieldLeaves) {
if (leaf.advanceExact(docID)) {
out.writeVInt(leaf.docValueCount());
for (int i = 0; i < leaf.docValueCount(); i++) {
Object obj = leaf.nextValue();
for (FormattedDocValues formattedDocValues : metricsFieldLeaves) {
if (formattedDocValues.advanceExact(docID)) {
out.writeVInt(formattedDocValues.docValueCount());
for (int i = 0; i < formattedDocValues.docValueCount(); i++) {
Object obj = formattedDocValues.nextValue();
if (obj instanceof Number == false) {
throw new IllegalArgumentException("Expected [Number], got [" + obj.getClass() + "]");
}
Expand All @@ -485,8 +485,8 @@ public void collect(int docID) throws IOException {
};
}

private List<DocValueFetcher.Leaf> leafFetchers(LeafReaderContext context, List<FieldValueFetcher> fetchers) {
List<DocValueFetcher.Leaf> leaves = new ArrayList<>();
private List<FormattedDocValues> leafFetchers(LeafReaderContext context, List<FieldValueFetcher> fetchers) {
List<FormattedDocValues> leaves = new ArrayList<>();
for (FieldValueFetcher fetcher : fetchers) {
leaves.add(fetcher.getLeaf(context));
}
Expand Down

0 comments on commit dbff7be

Please sign in to comment.