Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Watcher: Reduce script cache churn by checking for mustache tags #33978

Merged
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,12 @@ public String render(TextTemplate textTemplate, Map<String, Object> model) {
String mediaType = compileParams(detectContentType(template));
template = trimContentType(textTemplate);

int indexStartMustacheExpression = template.indexOf("{{");
int indexEndMustacheExpression = template.indexOf("}}");
if (indexStartMustacheExpression == -1 || indexStartMustacheExpression > indexEndMustacheExpression) {
spinscale marked this conversation as resolved.
Show resolved Hide resolved
return template;
}

Map<String, Object> mergedModel = new HashMap<>();
if (textTemplate.getParams() != null) {
mergedModel.putAll(textTemplate.getParams());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import static org.hamcrest.Matchers.notNullValue;
import static org.hamcrest.Matchers.nullValue;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verifyZeroInteractions;
import static org.mockito.Mockito.when;

public class TextTemplateTests extends ESTestCase {
Expand All @@ -47,7 +48,7 @@ public void init() throws Exception {
}

public void testRender() throws Exception {
String templateText = "_template";
String templateText = "{{_template}}";
Map<String, Object> params = singletonMap("param_key", "param_val");
Map<String, Object> model = singletonMap("model_key", "model_val");
Map<String, Object> merged = new HashMap<>(params);
Expand All @@ -72,7 +73,7 @@ public String execute() {
}

public void testRenderOverridingModel() throws Exception {
String templateText = "_template";
String templateText = "{{_template}}";
Map<String, Object> params = singletonMap("key", "param_val");
Map<String, Object> model = singletonMap("key", "model_val");
ScriptType type = randomFrom(ScriptType.values());
Expand All @@ -94,7 +95,7 @@ public String execute() {
}

public void testRenderDefaults() throws Exception {
String templateText = "_template";
String templateText = "{{_template}}";
Map<String, Object> model = singletonMap("key", "model_val");

TemplateScript.Factory compiledTemplate = templateParams ->
Expand All @@ -113,6 +114,13 @@ public String execute() {
assertThat(engine.render(template, model), is("rendered_text"));
}

public void testDontInvokeScriptServiceOnNonMustacheText() {
String input = rarely() ? "}}{{" : "this is my text";
spinscale marked this conversation as resolved.
Show resolved Hide resolved
String output = engine.render(new TextTemplate(input), Collections.emptyMap());
assertThat(input, is(output));
verifyZeroInteractions(service);
}

public void testParser() throws Exception {
ScriptType type = randomScriptType();
TextTemplate template =
Expand Down