Skip to content

Commit

Permalink
Remove getValue and getValues from Field (#79516)
Browse files Browse the repository at this point in the history
With duck-typing in Painless and potentially non-dynamic languages requiring a cast under the 
current design, this change removes unnecessary methods from the Field base class. The removal of 
getValue and getValues allows sub classes to return primitives from these methods and opens up 
potential design space for performance improvements.

Note this removes getLong from the unsigned long field; however, this is currently undocumented and 
unused behavior.
  • Loading branch information
jdconrad authored Oct 19, 2021
1 parent 675c1f4 commit 541a4fd
Show file tree
Hide file tree
Showing 15 changed files with 37 additions and 87 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,6 @@ class org.elasticsearch.script.field.Field @dynamic_type {
String getName()
boolean isEmpty()
int size()
List getValues()
def getValue(def)
def getValue(int, def)
}

class org.elasticsearch.script.DocBasedScript {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public interface LeafFieldData extends Accountable, Releasable {
/**
* Returns an {@code Field} for use in accessing field values in scripting.
*/
default DocValuesField<?> getScriptField(String name) {
default DocValuesField getScriptField(String name) {
throw new UnsupportedOperationException();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,14 @@ public DocBasedScript(DocReader docReader) {
this.docReader = docReader;
}

public Field<?> field(String fieldName) {
public Field field(String fieldName) {
if (docReader == null) {
return new EmptyField<>(fieldName);
return new EmptyField(fieldName);
}
return docReader.field(fieldName);
}

public Stream<Field<?>> fields(String fieldGlob) {
public Stream<Field> fields(String fieldGlob) {
if (docReader == null) {
return Stream.empty();
}
Expand Down
4 changes: 2 additions & 2 deletions server/src/main/java/org/elasticsearch/script/DocReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@
*/
public interface DocReader {
/** New-style field access */
Field<?> field(String fieldName);
Field field(String fieldName);

/** New-style field iterator */
Stream<Field<?>> fields(String fieldGlob);
Stream<Field> fields(String fieldGlob);

/** Set the underlying docId */
void setDocument(int docID);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,19 +39,19 @@ public DocValuesDocReader(SearchLookup searchLookup, LeafReaderContext leafConte
}

@Override
public Field<?> field(String fieldName) {
public Field field(String fieldName) {
LeafDocLookup leafDocLookup = leafSearchLookup.doc();

if (leafDocLookup.containsKey(fieldName) == false) {
return new EmptyField<>(fieldName);
return new EmptyField(fieldName);
}

return leafDocLookup.getScriptField(fieldName);
}


@Override
public Stream<Field<?>> fields(String fieldGlob) {
public Stream<Field> fields(String fieldGlob) {
throw new UnsupportedOperationException("not implemented");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

import java.io.IOException;

public interface DocValuesField<T> extends Field<T> {
public interface DocValuesField extends Field {

/** Set the current document ID. */
void setNextDocId(int docId) throws IOException;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,10 @@

package org.elasticsearch.script.field;

import java.util.Collections;
import java.util.List;

/**
* Script field with no mapping, always returns {@code defaultValue}.
*/
public class EmptyField<T> implements Field<T> {
public class EmptyField implements Field {

private final String name;

Expand All @@ -36,19 +33,4 @@ public boolean isEmpty() {
public int size() {
return 0;
}

@Override
public List<T> getValues() {
return Collections.emptyList();
}

@Override
public T getValue(T defaultValue) {
return defaultValue;
}

@Override
public T getValue(int index, T defaultValue) {
return defaultValue;
}
}
13 changes: 1 addition & 12 deletions server/src/main/java/org/elasticsearch/script/field/Field.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,8 @@

package org.elasticsearch.script.field;

import java.util.List;

/** A field in a document accessible via scripting. */
public interface Field<T> {
public interface Field {

/** Returns the name of this field. */
String getName();
Expand All @@ -22,13 +20,4 @@ public interface Field<T> {

/** Returns the number of values this field has. */
int size();

/** Get all values of a multivalued field. If {@code isEmpty()} this returns an empty list. */
List<T> getValues();

/** Get the first value of a field, if {@code isEmpty()} return defaultValue instead */
T getValue(T defaultValue);

/** Get the value of a field as the specified index. */
T getValue(int index, T defaultValue);
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@

public class LeafDocLookup implements Map<String, ScriptDocValues<?>> {

private final Map<String, DocValuesField<?>> localCacheScriptFieldData = new HashMap<>(4);
private final Map<String, DocValuesField> localCacheScriptFieldData = new HashMap<>(4);
private final Map<String, ScriptDocValues<?>> localCacheFieldData = new HashMap<>(4);
private final Function<String, MappedFieldType> fieldTypeLookup;
private final Function<MappedFieldType, IndexFieldData<?>> fieldDataLookup;
Expand All @@ -45,8 +45,8 @@ public void setDocument(int docId) {
this.docId = docId;
}

public DocValuesField<?> getScriptField(String fieldName) {
DocValuesField<?> field = localCacheScriptFieldData.get(fieldName);
public DocValuesField getScriptField(String fieldName) {
DocValuesField field = localCacheScriptFieldData.get(fieldName);

if (field == null) {
final MappedFieldType fieldType = fieldTypeLookup.apply(fieldName);
Expand All @@ -57,9 +57,9 @@ public DocValuesField<?> getScriptField(String fieldName) {

// Load the field data on behalf of the script. Otherwise, it would require
// additional permissions to deal with pagedbytes/ramusagestimator/etc.
field = AccessController.doPrivileged(new PrivilegedAction<DocValuesField<?>>() {
field = AccessController.doPrivileged(new PrivilegedAction<DocValuesField>() {
@Override
public DocValuesField<?> run() {
public DocValuesField run() {
return fieldDataLookup.apply(fieldType).load(reader).getScriptField(fieldName);
}
});
Expand Down
8 changes: 7 additions & 1 deletion x-pack/plugin/mapper-unsigned-long/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,10 @@ restResources {
restApi {
include '_common', 'bulk', 'indices', 'index', 'search', 'xpack'
}
}
}

tasks.named("yamlRestTestV7CompatTest").configure {
systemProperty 'tests.rest.blacklist', [
'50_script_values/Scripted fields values return Long'
].join(',')
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import static org.elasticsearch.search.DocValueFormat.MASK_2_63;
import static org.elasticsearch.xpack.unsignedlong.UnsignedLongFieldMapper.BIGINTEGER_2_64_MINUS_ONE;

public class UnsignedLongDocValuesField implements UnsignedLongField, DocValuesField<Long> {
public class UnsignedLongDocValuesField implements UnsignedLongField, DocValuesField {

private final SortedNumericDocValues input;
private long[] values = new long[0];
Expand Down Expand Up @@ -89,26 +89,12 @@ public List<Long> getValues() {
}

@Override
public Long getValue(Long defaultValue) {
public long getValue(long defaultValue) {
return getValue(0, defaultValue);
}

@Override
public Long getValue(int index, Long defaultValue) {
if (isEmpty() || index < 0 || index >= count) {
return defaultValue;
}

return toFormatted(index);
}

@Override
public long getLong(long defaultValue) {
return getLong(0, defaultValue);
}

@Override
public long getLong(int index, long defaultValue) {
public long getValue(int index, long defaultValue) {
if (isEmpty() || index < 0 || index >= count) {
return defaultValue;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,16 @@
import java.math.BigInteger;
import java.util.List;

public interface UnsignedLongField extends Field<Long> {
public interface UnsignedLongField extends Field {

/** Returns the 0th index value as an {@code long} if it exists, otherwise {@code defaultValue}. */
long getLong(long defaultValue);
long getValue(long defaultValue);

/** Returns the value at {@code index} as an {@code long} if it exists, otherwise {@code defaultValue}. */
long getLong(int index, long defaultValue);
long getValue(int index, long defaultValue);

/** Return all the values as a {@code List}. */
List<Long> getValues();

/** Returns the 0th index value as a {@code BigInteger} if it exists, otherwise {@code defaultValue}. */
BigInteger getBigInteger(BigInteger defaultValue);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public ScriptDocValues<?> getScriptValues() {
}

@Override
public DocValuesField<?> getScriptField(String name) {
public DocValuesField getScriptField(String name) {
return new UnsignedLongDocValuesField(getLongValues(), name);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@ class org.elasticsearch.xpack.unsignedlong.UnsignedLongScriptDocValues {
}

class org.elasticsearch.xpack.unsignedlong.UnsignedLongField @dynamic_type {
long getLong(long)
long getLong(int, long)
long getValue(long)
long getValue(int, long)
List getValues()
BigInteger getBigInteger(BigInteger)
BigInteger getBigInteger(int, BigInteger)
List getBigIntegers()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,21 +46,7 @@ setup:
- match: { hits.hits.2.fields.scripted_ul.0: -9223372036854775808 }
- match: { hits.hits.3.fields.scripted_ul.0: 9223372036854775807 }
- match: { hits.hits.4.fields.scripted_ul.0: 0 }
- do:
search:
index: test1
body:
sort: [ { ul: desc } ]
script_fields:
scripted_ul:
script:
source: "field('ul').getLong(1000L)"

- match: { hits.hits.0.fields.scripted_ul.0: -1 }
- match: { hits.hits.1.fields.scripted_ul.0: -3 }
- match: { hits.hits.2.fields.scripted_ul.0: -9223372036854775808 }
- match: { hits.hits.3.fields.scripted_ul.0: 9223372036854775807 }
- match: { hits.hits.4.fields.scripted_ul.0: 0 }
- do:
search:
index: test1
Expand Down

0 comments on commit 541a4fd

Please sign in to comment.