Skip to content

Commit

Permalink
Split up fields into different field types for Painless Fields API (e…
Browse files Browse the repository at this point in the history
…lastic#77732)

This change refactors the inner fields classes and inner converter classes into field class split by type 
for the Painless Fields API.

Each field class now contains the following:

* it's own members
* a converter that converts from other types of fields to its own type of field
* conversion helper methods to go from its own type of field to other types of fields
* conversion helper methods to go from its own type to other types
  • Loading branch information
jdconrad authored Sep 15, 2021
1 parent 9a545fb commit 25ae136
Show file tree
Hide file tree
Showing 20 changed files with 542 additions and 496 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@

# API
class org.elasticsearch.script.field.Field {
org.elasticsearch.script.field.Converter BigInteger
org.elasticsearch.script.field.Converter Long
org.elasticsearch.script.field.Converter BigInteger @augmented[augmented_canonical_class_name="org.elasticsearch.script.field.BigIntegerField"]
org.elasticsearch.script.field.Converter Long @augmented[augmented_canonical_class_name="org.elasticsearch.script.field.LongField"]
String getName()
boolean isEmpty()
List getValues()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import org.elasticsearch.geometry.utils.Geohash;
import org.elasticsearch.script.field.BooleanField;
import org.elasticsearch.script.field.BytesRefField;
import org.elasticsearch.script.field.Converters;
import org.elasticsearch.script.field.DateMillisField;
import org.elasticsearch.script.field.DateNanosField;
import org.elasticsearch.script.field.DoubleField;
Expand Down Expand Up @@ -249,9 +248,9 @@ void refreshArray() throws IOException {
public long getLongValue() {
throwIfEmpty();
if (isNanos) {
return Converters.convertDateNanosToLong(dates[0]);
return DateNanosField.toLong(dates[0]);
}
return Converters.convertDateMillisToLong(dates[0]);
return DateMillisField.toLong(dates[0]);
}

@Override
Expand Down Expand Up @@ -594,13 +593,13 @@ private static boolean[] grow(boolean[] array, int minSize) {
@Override
public long getLongValue() {
throwIfEmpty();
return Converters.convertBooleanToLong(values[0]);
return BooleanField.toLong(values[0]);
}

@Override
public double getDoubleValue() {
throwIfEmpty();
return Converters.convertBooleanToDouble(values[0]);
return BooleanField.toDouble(values[0]);
}

@Override
Expand Down Expand Up @@ -682,12 +681,12 @@ public final String getValue() {

@Override
public long getLongValue() {
return Converters.convertStringToLong(get(0));
return StringField.toLong(get(0));
}

@Override
public double getDoubleValue() {
return Converters.convertStringToDouble(get(0));
return StringField.toDouble(get(0));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,102 @@
package org.elasticsearch.script.field;

import java.math.BigInteger;
import java.util.List;
import java.util.stream.Collectors;

public class BigIntegerField extends Field<java.math.BigInteger> {

/* ---- Conversion Class From Other Fields ----*/

/**
* Convert to a {@link BigIntegerField} from Long, Double or String Fields.
* Longs and Doubles are wrapped as BigIntegers.
* Strings are parsed as either Longs or Doubles and wrapped in a BigInteger.
*/
public static final Converter<BigInteger, BigIntegerField> BigInteger;

static {
BigInteger = new Converter<BigInteger, BigIntegerField>() {
@Override
public BigIntegerField convert(Field<?> sourceField) {
if (sourceField instanceof LongField) {
return LongField.toBigIntegerField((LongField) sourceField);
}

if (sourceField instanceof DoubleField) {
return DoubleField.toBigIntegerField((DoubleField) sourceField);
}

if (sourceField instanceof StringField) {
return StringField.toBigIntegerField((StringField) sourceField);
}

if (sourceField instanceof DateMillisField) {
return LongField.toBigIntegerField(DateMillisField.toLongField((DateMillisField) sourceField));
}

if (sourceField instanceof DateNanosField) {
return LongField.toBigIntegerField(DateNanosField.toLongField((DateNanosField) sourceField));
}

if (sourceField instanceof BooleanField) {
return LongField.toBigIntegerField(BooleanField.toLongField((BooleanField) sourceField));
}

throw new InvalidConversion(sourceField.getClass(), getFieldClass());
}

@Override
public Class<BigIntegerField> getFieldClass() {
return BigIntegerField.class;
}

@Override
public Class<BigInteger> getTargetClass() {
return BigInteger.class;
}
};
}

/* ---- Conversion Helpers To Other Fields ---- */

public static LongField toLongField(BigIntegerField sourceField) {
FieldValues<BigInteger> fv = sourceField.getFieldValues();
return new LongField(sourceField.getName(), new DelegatingFieldValues<Long, BigInteger>(fv) {
@Override
public List<Long> getValues() {
return values.getValues().stream().map(BigIntegerField::toLong).collect(Collectors.toList());
}

@Override
public Long getNonPrimitiveValue() {
return toLong(values.getNonPrimitiveValue());
}

@Override
public long getLongValue() {
return toLong(values.getNonPrimitiveValue());
}

@Override
public double getDoubleValue() {
return toDouble(values.getNonPrimitiveValue());
}
});
}

/* ---- Conversion Helpers To Other Types ---- */

public static long toLong(BigInteger bigInteger) {
return bigInteger.longValue();
}

public static double toDouble(BigInteger bigInteger) {
return bigInteger.doubleValue();
}

/* ---- Big Integer Field Members ---- */

public BigIntegerField(String name, FieldValues<BigInteger> values) {
super(name, values);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,50 @@

package org.elasticsearch.script.field;

import java.util.List;
import java.util.stream.Collectors;

public class BooleanField extends Field<Boolean> {

/* ---- Conversion Helpers To Other Fields ---- */

public static LongField toLongField(BooleanField sourceField) {
FieldValues<Boolean> fv = sourceField.getFieldValues();
return new LongField(sourceField.getName(), new DelegatingFieldValues<Long, Boolean>(fv) {
@Override
public List<Long> getValues() {
return values.getValues().stream().map(bool -> bool ? 1L : 0L).collect(Collectors.toList());
}

@Override
public Long getNonPrimitiveValue() {
return toLong(values.getNonPrimitiveValue());
}

@Override
public long getLongValue() {
return toLong(values.getNonPrimitiveValue());
}

@Override
public double getDoubleValue() {
return toLong(values.getNonPrimitiveValue());
}
});
}

/* ---- Conversion Helpers To Other Types ---- */

public static long toLong(boolean bool) {
return bool ? 1L : 0L;
}

public static double toDouble(boolean bool) {
return bool ? 1.0d : 0.0d;
}

/* ---- Boolean Field Members ---- */

public BooleanField(String name, FieldValues<Boolean> values) {
super(name, values);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@

public class BytesRefField extends Field<BytesRef> {

/* ---- Bytes Ref Field Members ---- */

public BytesRefField(String name, FieldValues<BytesRef> values) {
super(name, values);
}
Expand Down
Loading

0 comments on commit 25ae136

Please sign in to comment.