Skip to content

Commit 9fead41

Browse files
joegallogeorgewallace
authored andcommitted
IngestDocument readability improvements (elastic#124322)
1 parent d74dc02 commit 9fead41

File tree

4 files changed

+152
-153
lines changed

4 files changed

+152
-153
lines changed

server/src/main/java/org/elasticsearch/common/util/LocaleUtils.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,15 @@
1414
import java.util.MissingResourceException;
1515

1616
/**
17-
* Utilities for for dealing with {@link Locale} objects
17+
* Utilities for dealing with {@link Locale} objects
1818
*/
1919
public class LocaleUtils {
2020

2121
/**
2222
* Parse the given locale as {@code language}, {@code language-country} or
2323
* {@code language-country-variant}.
24-
* Either underscores or hyphens may be used as separators, but consistently, ie.
25-
* you may not use an hyphen to separate the language from the country and an
24+
* Either underscores or hyphens may be used as separators, but consistently, i.e.
25+
* you may not use a hyphen to separate the language from the country and an
2626
* underscore to separate the country from the variant.
2727
* @throws IllegalArgumentException if there are too many parts in the locale string
2828
* @throws IllegalArgumentException if the language or country is not recognized

server/src/main/java/org/elasticsearch/ingest/ConfigurationUtils.java

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -543,18 +543,38 @@ public static TemplateScript.Factory compileTemplate(
543543
Script script = new Script(ScriptType.INLINE, DEFAULT_TEMPLATE_LANG, propertyValue, Map.of());
544544
return scriptService.compile(script, TemplateScript.CONTEXT);
545545
} else {
546-
return (params) -> new TemplateScript(params) {
547-
@Override
548-
public String execute() {
549-
return propertyValue;
550-
}
551-
};
546+
return new ConstantTemplateScriptFactory(propertyValue);
552547
}
553548
} catch (Exception e) {
554549
throw ConfigurationUtils.newConfigurationException(processorType, processorTag, propertyName, e);
555550
}
556551
}
557552

553+
/**
554+
* A 'template script' that ignores the model to which it is applied and just always returns a constant String.
555+
* <p>
556+
* Having a separate named class for this allows for some hot code paths to pre-apply the 'template script' statically,
557+
* rather than bothering to invoke it per-document. Note that this is probably only useful if something expensive
558+
* is being done with the result of calling the script, and the code can then avoid doing that thing per-document.
559+
*/
560+
public static class ConstantTemplateScriptFactory implements TemplateScript.Factory {
561+
final TemplateScript script;
562+
563+
private ConstantTemplateScriptFactory(String value) {
564+
this.script = new TemplateScript(Map.of()) {
565+
@Override
566+
public String execute() {
567+
return value;
568+
}
569+
};
570+
}
571+
572+
@Override
573+
public TemplateScript newInstance(Map<String, Object> params) {
574+
return script;
575+
}
576+
}
577+
558578
private static void addMetadataToException(
559579
ElasticsearchException exception,
560580
String processorType,

0 commit comments

Comments
 (0)