Skip to content

Commit

Permalink
Split off the values supplier for ScriptDocValues (#80635)
Browse files Browse the repository at this point in the history
This change makes all ScriptDocValues purely a wrapper around a supplier. (Similar to what 
FieldValues was.) However, there are some important differences:

* This is meant to be transitory. As more DocValuesFields are completed, more of the simple 
suppliers (ones that aren't DocValuesFields) can be removed.
* ScriptDocValues is the wrapper rather than the supplier. DocValuesFields are eventually the target 
suppliers which makes it really easy to remove the simple suppliers once they are no longer 
necessary.
* ScriptDocValues can be easily deprecated and removed without having to move their code to 
DocValuesFields. Once ScriptDocValues is removed we can remove the supplier code from 
DocValuesFields.
* DelegateDocValuesField ensures that any ScriptDocValues field are not supplied by another 
DocValuesField with an assert statement. This helps us to identify bugs during testing.
* ScriptDocValues no longer have setNextDocId. This helps us identify bugs during compilation.
* Conversions will not share/wrap suppliers since the suppliers are transitory.
  • Loading branch information
jdconrad committed Nov 29, 2021
1 parent 5c8e7c6 commit 1adb59c
Show file tree
Hide file tree
Showing 44 changed files with 666 additions and 357 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import org.elasticsearch.index.fielddata.LeafNumericFieldData;
import org.elasticsearch.index.fielddata.NumericDoubleValues;
import org.elasticsearch.index.fielddata.ScriptDocValues.Doubles;
import org.elasticsearch.index.fielddata.ScriptDocValues.DoublesSupplier;
import org.elasticsearch.index.fielddata.SortedBinaryDocValues;
import org.elasticsearch.index.fielddata.SortedNumericDoubleValues;
import org.elasticsearch.index.fielddata.plain.SortedNumericIndexFieldData;
Expand Down Expand Up @@ -269,7 +270,7 @@ public IndexFieldData.Builder fielddataBuilder(String fullyQualifiedIndexName, S
return new ScaledFloatIndexFieldData(
scaledValues,
scalingFactor,
(dv, n) -> new DelegateDocValuesField(new Doubles(dv), n)
(dv, n) -> new DelegateDocValuesField(new Doubles(new DoublesSupplier(dv)), n)
);
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import org.elasticsearch.index.fielddata.IndexFieldData;
import org.elasticsearch.index.fielddata.IndexNumericFieldData.NumericType;
import org.elasticsearch.index.fielddata.ScriptDocValues.Longs;
import org.elasticsearch.index.fielddata.ScriptDocValues.LongsSupplier;
import org.elasticsearch.index.fielddata.plain.SortedNumericIndexFieldData;
import org.elasticsearch.index.mapper.DocumentParserContext;
import org.elasticsearch.index.mapper.FieldMapper;
Expand Down Expand Up @@ -83,7 +84,10 @@ public Murmur3FieldMapper build(MapperBuilderContext context) {
// this only exists so a check can be done to match the field type to using murmur3 hashing...
public static class Murmur3FieldType extends MappedFieldType {

public static final ToScriptField<SortedNumericDocValues> TO_SCRIPT_FIELD = (dv, n) -> new DelegateDocValuesField(new Longs(dv), n);
public static final ToScriptField<SortedNumericDocValues> TO_SCRIPT_FIELD = (dv, n) -> new DelegateDocValuesField(
new Longs(new LongsSupplier(dv)),
n
);

private Murmur3FieldType(String name, boolean isStored, Map<String, String> meta) {
super(name, false, isStored, true, TextSearchInfo.NONE, meta);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
import org.elasticsearch.common.bytes.BytesArray;
import org.elasticsearch.common.bytes.BytesReference;
import org.elasticsearch.common.network.InetAddresses;
import org.elasticsearch.index.fielddata.ScriptDocValues.Strings;
import org.elasticsearch.index.fielddata.ScriptDocValues.StringsSupplier;
import org.elasticsearch.index.mapper.IpFieldMapper;
import org.elasticsearch.indices.breaker.CircuitBreakerService;
import org.elasticsearch.script.IpFieldScript;
Expand Down Expand Up @@ -53,7 +55,7 @@ public BinaryScriptLeafFieldData loadDirect(LeafReaderContext context) throws Ex
return new BinaryScriptLeafFieldData() {
@Override
public DocValuesField<?> getScriptField(String name) {
return new DelegateDocValuesField(new IpScriptDocValues(getBytesValues()), name);
return new DelegateDocValuesField(new Strings(new IpSupplier(getBytesValues())), name);
}

@Override
Expand All @@ -69,18 +71,19 @@ public ValuesSourceType getValuesSourceType() {
}

/**
* Doc values implementation for ips. We can't share
* Doc values supplier implementation for ips. We can't share
* {@link IpFieldMapper.IpFieldType.IpScriptDocValues} because it is based
* on global ordinals and we don't have those.
*/
public static class IpScriptDocValues extends ScriptDocValues.Strings {
public IpScriptDocValues(SortedBinaryDocValues in) {
public static class IpSupplier extends StringsSupplier {

public IpSupplier(SortedBinaryDocValues in) {
super(in);
}

@Override
protected String bytesToString(BytesRef bytes) {
InetAddress addr = InetAddressPoint.decode(BytesReference.toBytes(new BytesArray(bytes)));
protected String bytesToString(BytesRef bytesRef) {
InetAddress addr = InetAddressPoint.decode(BytesReference.toBytes(new BytesArray(bytesRef)));
return InetAddresses.toAddrString(addr);
}
}
Expand Down

0 comments on commit 1adb59c

Please sign in to comment.