Skip to content

Commit b10cce5

Browse files
authored
IngestDocument readability improvements (#124322) (#124407)
1 parent dc44c4c commit b10cce5

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
@@ -541,18 +541,38 @@ public static TemplateScript.Factory compileTemplate(
541541
Script script = new Script(ScriptType.INLINE, DEFAULT_TEMPLATE_LANG, propertyValue, Map.of());
542542
return scriptService.compile(script, TemplateScript.CONTEXT);
543543
} else {
544-
return (params) -> new TemplateScript(params) {
545-
@Override
546-
public String execute() {
547-
return propertyValue;
548-
}
549-
};
544+
return new ConstantTemplateScriptFactory(propertyValue);
550545
}
551546
} catch (Exception e) {
552547
throw ConfigurationUtils.newConfigurationException(processorType, processorTag, propertyName, e);
553548
}
554549
}
555550

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

0 commit comments

Comments
 (0)