-
Notifications
You must be signed in to change notification settings - Fork 25.6k
Scripting: Replace Update Context #32096
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
Merged
original-brownbear
merged 24 commits into
elastic:master
from
original-brownbear:replace-update-context
Aug 9, 2018
Merged
Changes from 12 commits
Commits
Show all changes
24 commits
Select commit
Hold shift + click to select a range
44bc7e8
SCRIPTING: Move Update Scripts to their own context
original-brownbear c4c572e
SCRIPTING: Move Update Scripts to their own context
original-brownbear a560645
SCRIPTING: Move Update Scripts to their own context
original-brownbear 38601f9
SCRIPTING: Move Update Scripts to their own context
original-brownbear 3bb2eea
SCRIPTING: Move Update Scripts to their own context
original-brownbear 228e465
Merge remote-tracking branch 'elastic/master' into replace-update-con…
original-brownbear aa74ff8
Merge remote-tracking branch 'elastic/master' into replace-update-con…
original-brownbear 78d6945
Merge remote-tracking branch 'elastic/master' into replace-update-con…
original-brownbear 7700395
Merge remote-tracking branch 'elastic/master' into replace-update-con…
original-brownbear 8473a26
Merge branch 'master' into replace-update-context
original-brownbear aa12c43
SCRIPTING: Move Update Scripts to their own context
original-brownbear 049606c
Merge remote-tracking branch 'elastic/master' into replace-update-con…
original-brownbear 49e1875
merge in master
original-brownbear 77d3e60
CR: Add backwards compatibility with by putting ctx back into params
original-brownbear 6083196
CR: Add backwards compatibility with by putting ctx back into params
original-brownbear 2ef1e5c
CR: Add backwards compatibility with by putting ctx back into params
original-brownbear 21728f4
Merge remote-tracking branch 'elastic/master' into replace-update-con…
original-brownbear 699f5eb
fix integ tests
original-brownbear f526340
fix integ tests
original-brownbear 5671b5e
Merge remote-tracking branch 'elastic/master' into replace-update-con…
original-brownbear 9cf2034
Merge remote-tracking branch 'elastic/master' into replace-update-con…
original-brownbear 0df2ce0
CR nits
original-brownbear 35d9445
Merge remote-tracking branch 'elastic/master' into replace-update-con…
original-brownbear 329feea
CR nits
original-brownbear File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -26,8 +26,10 @@ | |
| import org.elasticsearch.action.index.IndexRequest; | ||
| import org.elasticsearch.script.ExecutableScript; | ||
| import org.elasticsearch.script.ScriptService; | ||
| import org.elasticsearch.script.UpdateScript; | ||
| import org.junit.Before; | ||
|
|
||
| import java.util.Collections; | ||
| import java.util.Map; | ||
| import java.util.function.Consumer; | ||
|
|
||
|
|
@@ -54,10 +56,16 @@ public void setupScriptService() { | |
| protected <T extends ActionRequest> T applyScript(Consumer<Map<String, Object>> scriptBody) { | ||
| IndexRequest index = new IndexRequest("index", "type", "1").source(singletonMap("foo", "bar")); | ||
| ScrollableHitSource.Hit doc = new ScrollableHitSource.BasicHit("test", "type", "id", 0); | ||
| ExecutableScript executableScript = new SimpleExecutableScript(scriptBody); | ||
| ExecutableScript.Factory factory = params -> executableScript; | ||
| when(scriptService.compile(any(), eq(ExecutableScript.CONTEXT))).thenReturn(factory); | ||
| when(scriptService.compile(any(), eq(ExecutableScript.UPDATE_CONTEXT))).thenReturn(factory); | ||
| UpdateScript executableScript = new UpdateScript(Collections.emptyMap()) { | ||
|
||
| @Override | ||
| public void execute(Map<String, Object> ctx) { | ||
| scriptBody.accept(ctx); | ||
| } | ||
| }; | ||
| UpdateScript.Factory factory = params -> executableScript; | ||
| ExecutableScript simpleExecutableScript = new SimpleExecutableScript(scriptBody); | ||
| when(scriptService.compile(any(), eq(ExecutableScript.CONTEXT))).thenReturn(params -> simpleExecutableScript); | ||
| when(scriptService.compile(any(), eq(UpdateScript.CONTEXT))).thenReturn(factory); | ||
| AbstractAsyncBulkByScrollAction<Request> action = action(scriptService, request().setScript(mockScript(""))); | ||
| RequestWrapper<?> result = action.buildScriptApplier().apply(AbstractAsyncBulkByScrollAction.wrap(index), doc); | ||
| return (result != null) ? (T) result.self() : null; | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 52 additions & 0 deletions
52
server/src/main/java/org/elasticsearch/script/UpdateScript.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
|
|
||
| /* | ||
| * Licensed to Elasticsearch under one or more contributor | ||
| * license agreements. See the NOTICE file distributed with | ||
| * this work for additional information regarding copyright | ||
| * ownership. Elasticsearch licenses this file to you under | ||
| * the Apache License, Version 2.0 (the "License"); you may | ||
| * not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, | ||
| * software distributed under the License is distributed on an | ||
| * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| * KIND, either express or implied. See the License for the | ||
| * specific language governing permissions and limitations | ||
| * under the License. | ||
| */ | ||
|
|
||
| package org.elasticsearch.script; | ||
|
|
||
| import java.util.Map; | ||
|
|
||
| /** | ||
| * An update script. | ||
| */ | ||
| public abstract class UpdateScript { | ||
|
|
||
| public static final String[] PARAMETERS = { "ctx" }; | ||
|
|
||
| /** The context used to compile {@link UpdateScript} factories. */ | ||
| public static final ScriptContext<Factory> CONTEXT = new ScriptContext<>("update", Factory.class); | ||
|
|
||
| /** The generic runtime parameters for the script. */ | ||
| private final Map<String, Object> params; | ||
|
|
||
| public UpdateScript(Map<String, Object> params) { | ||
| this.params = params; | ||
| } | ||
|
|
||
| /** Return the parameters for this script. */ | ||
| public Map<String, Object> getParams() { | ||
| return params; | ||
| } | ||
|
|
||
| public abstract void execute(Map<String, Object> ctx); | ||
|
|
||
| public interface Factory { | ||
| UpdateScript newInstance(Map<String, Object> params); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Isn't this creating a cycle in _source?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@rjernst that's the point, see what it matches a few lines down and my PR description https://github.com/elastic/elasticsearch/pull/32096/files/049606c19854ac69ee07f2ff5d772f02532f769a#diff-585f6613de46bec6547c7b2aed89db8bR140 :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, thanks, sorry I missed that.