Skip to content

Commit

Permalink
Share common runForDoc impl in AbstractFieldScript (elastic#92450)
Browse files Browse the repository at this point in the history
Each runtime field script class implements its own slighly different variant of the runForDoc method, sometimes called resultsForDoc in case it also returns the results.

For simplicity, we can unify runForDoc in the base class and introduce additional getValues methods where missing. This should also simplify unifying error handling down the line introducing the error handling logic directly in runForDoc.
  • Loading branch information
javanna authored Dec 19, 2022
1 parent 2d2b82b commit 4be7743
Show file tree
Hide file tree
Showing 12 changed files with 48 additions and 52 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -663,7 +663,8 @@ static Response innerShardOperation(Request request, ScriptService scriptService
context.lookup()
);
CompositeFieldScript compositeFieldScript = leafFactory.newInstance(leafReaderContext);
return new Response(compositeFieldScript.runForDoc(0));
compositeFieldScript.runForDoc(0);
return new Response(compositeFieldScript.getFieldValues());
}, indexService);
} else {
throw new UnsupportedOperationException("unsupported context [" + scriptContext.name + "]");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ public final class StringScriptDocValues extends SortingBinaryDocValues {

@Override
public boolean advanceExact(int docId) {
List<String> results = script.resultsForDoc(docId);
script.runForDoc(docId);
List<String> results = script.getValues();
count = results.size();
if (count == 0) {
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,5 +133,16 @@ protected final void checkMaxSize(int currentSize) {
}
}

protected abstract void prepareExecute();

/**
* Execute the script for the provided {@code docId}.
*/
public final void runForDoc(int docId) {
prepareExecute();
setDocument(docId);
execute();
}

public abstract void execute();
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,9 @@ public AbstractLongFieldScript(String fieldName, Map<String, Object> params, Sea
super(fieldName, params, searchLookup, ctx);
}

/**
* Execute the script for the provided {@code docId}.
*/
public final void runForDoc(int docId) {
@Override
protected void prepareExecute() {
count = 0;
setDocument(docId);
execute();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,14 +75,10 @@ public BooleanFieldScript(String fieldName, Map<String, Object> params, SearchLo
super(fieldName, params, searchLookup, ctx);
}

/**
* Execute the script for the provided {@code docId}.
*/
public final void runForDoc(int docId) {
@Override
protected void prepareExecute() {
trues = 0;
falses = 0;
setDocument(docId);
execute();
}

public final void runForDoc(int docId, Consumer<Boolean> consumer) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,17 +47,19 @@ public CompositeFieldScript(String fieldName, Map<String, Object> params, Search
*/
public final List<Object> getValues(String field) {
// TODO for now we re-run the script every time a leaf field is accessed, but we could cache the values?
fieldValues.clear();
prepareExecute();
execute();
List<Object> values = fieldValues.get(field);
fieldValues.clear(); // don't hold on to values unnecessarily
return values;
}

public final Map<String, List<Object>> runForDoc(int doc) {
setDocument(doc);
@Override
protected void prepareExecute() {
fieldValues.clear();
execute();
}

public final Map<String, List<Object>> getFieldValues() {
return fieldValues;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,13 +74,9 @@ public DoubleFieldScript(String fieldName, Map<String, Object> params, SearchLoo
super(fieldName, params, searchLookup, ctx);
}

/**
* Execute the script for the provided {@code docId}.
*/
public final void runForDoc(int docId) {
@Override
protected void prepareExecute() {
count = 0;
setDocument(docId);
execute();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,13 +85,9 @@ public GeoPointFieldScript(String fieldName, Map<String, Object> params, SearchL
super(fieldName, params, searchLookup, ctx);
}

/**
* Execute the script for the provided {@code docId}.
*/
public final void runForDoc(int docId) {
@Override
protected void prepareExecute() {
count = 0;
setDocument(docId);
execute();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,13 +95,9 @@ public IpFieldScript(String fieldName, Map<String, Object> params, SearchLookup
super(fieldName, params, searchLookup, ctx);
}

/**
* Execute the script for the provided {@code docId}.
*/
public final void runForDoc(int docId) {
@Override
protected void prepareExecute() {
count = 0;
setDocument(docId);
execute();
}

public final void runForDoc(int docId, Consumer<InetAddress> consumer) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,22 +81,22 @@ public StringFieldScript(String fieldName, Map<String, Object> params, SearchLoo
super(fieldName, params, searchLookup, ctx);
}

/**
* Execute the script for the provided {@code docId}.
*
* @return a mutable {@link List} that contains the results of the script
* and will be modified the next time you call {@linkplain #resultsForDoc}.
*/
public final List<String> resultsForDoc(int docId) {
@Override
protected void prepareExecute() {
results.clear();
chars = 0;
setDocument(docId);
execute();
return results;
}

public final void runForDoc(int docId, Consumer<String> consumer) {
resultsForDoc(docId).forEach(consumer);
runForDoc(docId);
results.forEach(consumer);
}

/**
* Values from the last time runForDoc(int) was called. This list is mutable and will change with the next call of runForDoc(int).
*/
public List<String> getValues() {
return results;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ abstract class AbstractStringScriptFieldQuery extends AbstractScriptFieldQuery<S

@Override
protected final boolean matches(StringFieldScript scriptContext, int docId) {
return matches(scriptContext.resultsForDoc(docId));
scriptContext.runForDoc(docId);
return matches(scriptContext.getValues());
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,8 +133,8 @@ public final void testFromSourceDoesNotEnforceValuesLimit() throws IOException {
new SearchLookup(field -> null, (ft, lookup, fdt) -> null, new SourceLookup.ReaderSourceProvider())
);
StringFieldScript stringFieldScript = leafFactory.newInstance(reader.leaves().get(0));
List<String> results = stringFieldScript.resultsForDoc(0);
assertEquals(numValues, results.size());
stringFieldScript.runForDoc(0);
assertEquals(numValues, stringFieldScript.getValues().size());
}
}
}
Expand Down Expand Up @@ -162,8 +162,8 @@ public final void testFromSourceDoesNotEnforceCharsLimit() throws IOException {
new SearchLookup(field -> null, (ft, lookup, fdt) -> null, new SourceLookup.ReaderSourceProvider())
);
StringFieldScript stringFieldScript = leafFactory.newInstance(reader.leaves().get(0));
List<String> results = stringFieldScript.resultsForDoc(0);
assertEquals(5, results.size());
stringFieldScript.runForDoc(0);
assertEquals(5, stringFieldScript.getValues().size());
}
}
}
Expand Down

0 comments on commit 4be7743

Please sign in to comment.