Skip to content

Commit 95cdff7

Browse files
committed
Revert "Convert double script to return array (#61504)"
This reverts commit 6d8170c. We've decided to stick with the `value` style functions for runtime fields.
1 parent c9bd8c8 commit 95cdff7

File tree

21 files changed

+103
-175
lines changed

21 files changed

+103
-175
lines changed

x-pack/plugin/runtime-fields/qa/rest/src/yamlRestTest/java/org/elasticsearch/xpack/runtimefields/rest/CoreTestsWithRuntimeFieldsIT.java

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -166,9 +166,6 @@ private static String painlessToLoadFromSource(String name, String type) {
166166
return null;
167167
}
168168
StringBuilder b = new StringBuilder();
169-
if ("double".equals(type)) {
170-
b.append("List result = new ArrayList();");
171-
}
172169
b.append("def v = source['").append(name).append("'];\n");
173170
b.append("if (v instanceof Iterable) {\n");
174171
b.append(" for (def vv : ((Iterable) v)) {\n");
@@ -183,9 +180,6 @@ private static String painlessToLoadFromSource(String name, String type) {
183180
b.append(" ").append(emit).append("\n");
184181
b.append(" }\n");
185182
b.append("}\n");
186-
if ("double".equals(type)) {
187-
b.append("return result;");
188-
}
189183
return b.toString();
190184
}
191185

@@ -194,7 +188,7 @@ private static String painlessToLoadFromSource(String name, String type) {
194188
Map.entry(DateFieldMapper.CONTENT_TYPE, "millis(parse(value.toString()));"),
195189
Map.entry(
196190
NumberType.DOUBLE.typeName(),
197-
"result.add(value instanceof Number ? ((Number) value).doubleValue() : Double.parseDouble(value.toString()));"
191+
"value(value instanceof Number ? ((Number) value).doubleValue() : Double.parseDouble(value.toString()));"
198192
),
199193
Map.entry(KeywordFieldMapper.CONTENT_TYPE, "value(value.toString());"),
200194
Map.entry(IpFieldMapper.CONTENT_TYPE, "stringValue(value.toString());"),

x-pack/plugin/runtime-fields/src/main/java/org/elasticsearch/xpack/runtimefields/AbstractLongScriptFieldScript.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,6 @@ public AbstractLongScriptFieldScript(Map<String, Object> params, SearchLookup se
2323
super(params, searchLookup, ctx);
2424
}
2525

26-
public abstract void execute();
27-
2826
/**
2927
* Execute the script for the provided {@code docId}.
3028
*/

x-pack/plugin/runtime-fields/src/main/java/org/elasticsearch/xpack/runtimefields/AbstractScriptFieldScript.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,4 +81,6 @@ public final Map<String, Object> getSource() {
8181
public final Map<String, ScriptDocValues<?>> getDoc() {
8282
return leafSearchLookup.doc();
8383
}
84+
85+
public abstract void execute();
8486
}

x-pack/plugin/runtime-fields/src/main/java/org/elasticsearch/xpack/runtimefields/BooleanScriptFieldScript.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,6 @@ public BooleanScriptFieldScript(Map<String, Object> params, SearchLookup searchL
4242
super(params, searchLookup, ctx);
4343
}
4444

45-
public abstract void execute();
46-
4745
/**
4846
* Execute the script for the provided {@code docId}.
4947
*/

x-pack/plugin/runtime-fields/src/main/java/org/elasticsearch/xpack/runtimefields/DoubleScriptFieldScript.java

Lines changed: 35 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,14 @@
77
package org.elasticsearch.xpack.runtimefields;
88

99
import org.apache.lucene.index.LeafReaderContext;
10+
import org.apache.lucene.util.ArrayUtil;
1011
import org.elasticsearch.painless.spi.Whitelist;
1112
import org.elasticsearch.painless.spi.WhitelistLoader;
1213
import org.elasticsearch.script.ScriptContext;
1314
import org.elasticsearch.script.ScriptFactory;
1415
import org.elasticsearch.search.lookup.SearchLookup;
1516

1617
import java.io.IOException;
17-
import java.util.Collection;
1818
import java.util.List;
1919
import java.util.Map;
2020

@@ -35,40 +35,55 @@ public interface LeafFactory {
3535
DoubleScriptFieldScript newInstance(LeafReaderContext ctx) throws IOException;
3636
}
3737

38+
private double[] values = new double[1];
39+
private int count;
40+
3841
public DoubleScriptFieldScript(Map<String, Object> params, SearchLookup searchLookup, LeafReaderContext ctx) {
3942
super(params, searchLookup, ctx);
4043
}
4144

42-
public abstract double[] execute();
43-
4445
/**
4546
* Execute the script for the provided {@code docId}.
4647
*/
47-
public final double[] runForDoc(int docId) {
48+
public final void runForDoc(int docId) {
49+
count = 0;
4850
setDocument(docId);
49-
return execute();
51+
execute();
5052
}
5153

52-
public static double[] convertFromDouble(double v) {
53-
return new double[] { v };
54+
/**
55+
* Values from the last time {@link #runForDoc(int)} was called. This array
56+
* is mutable and will change with the next call of {@link #runForDoc(int)}.
57+
* It is also oversized and will contain garbage at all indices at and
58+
* above {@link #count()}.
59+
*/
60+
public final double[] values() {
61+
return values;
5462
}
5563

56-
public static double[] convertFromCollection(Collection<?> v) {
57-
double[] result = new double[v.size()];
58-
int i = 0;
59-
for (Object o : v) {
60-
result[i++] = ((Number) o).doubleValue();
64+
/**
65+
* The number of results produced the last time {@link #runForDoc(int)} was called.
66+
*/
67+
public final int count() {
68+
return count;
69+
}
70+
71+
private void collectValue(double v) {
72+
if (values.length < count + 1) {
73+
values = ArrayUtil.grow(values, count + 1);
6174
}
62-
return result;
75+
values[count++] = v;
6376
}
6477

65-
public static double[] convertFromDef(Object o) {
66-
if (o instanceof Number) {
67-
return convertFromDouble(((Number) o).doubleValue());
68-
} else if (o instanceof Collection) {
69-
return convertFromCollection((Collection<?>) o);
70-
} else {
71-
return (double[]) o;
78+
public static class Value {
79+
private final DoubleScriptFieldScript script;
80+
81+
public Value(DoubleScriptFieldScript script) {
82+
this.script = script;
83+
}
84+
85+
public void value(double v) {
86+
script.collectValue(v);
7287
}
7388
}
7489
}

x-pack/plugin/runtime-fields/src/main/java/org/elasticsearch/xpack/runtimefields/IpScriptFieldScript.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,6 @@ public IpScriptFieldScript(Map<String, Object> params, SearchLookup searchLookup
6363
super(params, searchLookup, ctx);
6464
}
6565

66-
public abstract void execute();
67-
6866
/**
6967
* Execute the script for the provided {@code docId}.
7068
*/

x-pack/plugin/runtime-fields/src/main/java/org/elasticsearch/xpack/runtimefields/StringScriptFieldScript.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,6 @@ public StringScriptFieldScript(Map<String, Object> params, SearchLookup searchLo
4141
super(params, searchLookup, ctx);
4242
}
4343

44-
public abstract void execute();
45-
4644
/**
4745
* Execute the script for the provided {@code docId}.
4846
* <p>

x-pack/plugin/runtime-fields/src/main/java/org/elasticsearch/xpack/runtimefields/fielddata/ScriptDoubleDocValues.java

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414

1515
public final class ScriptDoubleDocValues extends SortedNumericDoubleValues {
1616
private final DoubleScriptFieldScript script;
17-
private double[] values;
1817
private int cursor;
1918

2019
ScriptDoubleDocValues(DoubleScriptFieldScript script) {
@@ -23,22 +22,22 @@ public final class ScriptDoubleDocValues extends SortedNumericDoubleValues {
2322

2423
@Override
2524
public boolean advanceExact(int docId) {
26-
values = script.runForDoc(docId);
27-
if (values.length == 0) {
25+
script.runForDoc(docId);
26+
if (script.count() == 0) {
2827
return false;
2928
}
30-
Arrays.sort(values);
29+
Arrays.sort(script.values(), 0, script.count());
3130
cursor = 0;
3231
return true;
3332
}
3433

3534
@Override
3635
public double nextValue() throws IOException {
37-
return values[cursor++];
36+
return script.values()[cursor++];
3837
}
3938

4039
@Override
4140
public int docValueCount() {
42-
return values.length;
41+
return script.count();
4342
}
4443
}

x-pack/plugin/runtime-fields/src/main/java/org/elasticsearch/xpack/runtimefields/query/AbstractDoubleScriptFieldQuery.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ abstract class AbstractDoubleScriptFieldQuery extends AbstractScriptFieldQuery {
3636
/**
3737
* Does the value match this query?
3838
*/
39-
protected abstract boolean matches(double[] values);
39+
protected abstract boolean matches(double[] values, int count);
4040

4141
@Override
4242
public final Weight createWeight(IndexSearcher searcher, ScoreMode scoreMode, float boost) throws IOException {
@@ -53,7 +53,8 @@ public Scorer scorer(LeafReaderContext ctx) throws IOException {
5353
TwoPhaseIterator twoPhase = new TwoPhaseIterator(approximation) {
5454
@Override
5555
public boolean matches() throws IOException {
56-
return AbstractDoubleScriptFieldQuery.this.matches(script.runForDoc(approximation().docID()));
56+
script.runForDoc(approximation().docID());
57+
return AbstractDoubleScriptFieldQuery.this.matches(script.values(), script.count());
5758
}
5859

5960
@Override

x-pack/plugin/runtime-fields/src/main/java/org/elasticsearch/xpack/runtimefields/query/DoubleScriptFieldExistsQuery.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ public DoubleScriptFieldExistsQuery(Script script, DoubleScriptFieldScript.LeafF
1515
}
1616

1717
@Override
18-
protected boolean matches(double[] values) {
19-
return values.length > 0;
18+
protected boolean matches(double[] values, int count) {
19+
return count > 0;
2020
}
2121

2222
@Override

0 commit comments

Comments
 (0)