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

Update DeprecationMap to DynamicMap #56149

Merged
merged 3 commits into from
May 5, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
response to pr comments
  • Loading branch information
jdconrad committed May 5, 2020
commit f91cadfc80286540750da3943b476a6066d532e3
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ abstract class AbstractSortScript implements ScorerAware {

private static final DeprecationLogger deprecationLogger =
new DeprecationLogger(LogManager.getLogger(DynamicMap.class));
private static final Map<String, Function<Object, Object>> FUNCTIONS = Map.of(
private static final Map<String, Function<Object, Object>> PARAMS_FUNCTIONS = Map.of(
"doc", value -> {
deprecationLogger.deprecatedAndMaybeLog("sort-script_doc",
"Accessing variable [doc] via [params.doc] from within an sort-script "
Expand Down Expand Up @@ -68,7 +68,7 @@ abstract class AbstractSortScript implements ScorerAware {
this.leafLookup = lookup.getLeafSearchLookup(leafContext);
Map<String, Object> parameters = new HashMap<>(params);
parameters.putAll(leafLookup.asMap());
this.params = new DynamicMap(parameters, FUNCTIONS);
this.params = new DynamicMap(parameters, PARAMS_FUNCTIONS);
}

protected AbstractSortScript() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public abstract class AggregationScript implements ScorerAware {

private static final DeprecationLogger deprecationLogger =
new DeprecationLogger(LogManager.getLogger(DynamicMap.class));
private static final Map<String, Function<Object, Object>> FUNCTIONS = Map.of(
private static final Map<String, Function<Object, Object>> PARAMS_FUNCTIONS = Map.of(
"doc", value -> {
deprecationLogger.deprecatedAndMaybeLog("aggregation-script_doc",
"Accessing variable [doc] via [params.doc] from within an aggregation-script "
Expand Down Expand Up @@ -73,7 +73,7 @@ public abstract class AggregationScript implements ScorerAware {
private Object value;

public AggregationScript(Map<String, Object> params, SearchLookup lookup, LeafReaderContext leafContext) {
this.params = new DynamicMap(new HashMap<>(params), FUNCTIONS);
this.params = new DynamicMap(new HashMap<>(params), PARAMS_FUNCTIONS);
this.leafLookup = lookup.getLeafSearchLookup(leafContext);
this.params.putAll(leafLookup.asMap());
}
Expand Down
6 changes: 6 additions & 0 deletions server/src/main/java/org/elasticsearch/script/DynamicMap.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@
import java.util.Set;
import java.util.function.Function;

/**
* DynamicMap is used to wrap a Map for a script parameter. A set of
* functions is provided for the overridden values where the function
* is applied to the existing value when one exists for the
* corresponding key.
*/
public final class DynamicMap implements Map<String, Object> {
jdconrad marked this conversation as resolved.
Show resolved Hide resolved

private final Map<String, Object> delegate;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public abstract class FieldScript {

private static final DeprecationLogger deprecationLogger =
new DeprecationLogger(LogManager.getLogger(DynamicMap.class));
private static final Map<String, Function<Object, Object>> FUNCTIONS = Map.of(
private static final Map<String, Function<Object, Object>> PARAMS_FUNCTIONS = Map.of(
"doc", value -> {
deprecationLogger.deprecatedAndMaybeLog("field-script_doc",
"Accessing variable [doc] via [params.doc] from within an field-script "
Expand All @@ -64,7 +64,7 @@ public FieldScript(Map<String, Object> params, SearchLookup lookup, LeafReaderCo
this.leafLookup = lookup.getLeafSearchLookup(leafContext);
params = new HashMap<>(params);
params.putAll(leafLookup.asMap());
this.params = new DynamicMap(params, FUNCTIONS);
this.params = new DynamicMap(params, PARAMS_FUNCTIONS);
}

// for expression engine
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public Explanation get(double score, Explanation subQueryExplanation) {

private static final DeprecationLogger deprecationLogger =
new DeprecationLogger(LogManager.getLogger(DynamicMap.class));
private static final Map<String, Function<Object, Object>> FUNCTIONS = Map.of(
private static final Map<String, Function<Object, Object>> PARAMS_FUNCTIONS = Map.of(
"doc", value -> {
deprecationLogger.deprecatedAndMaybeLog("score-script_doc",
"Accessing variable [doc] via [params.doc] from within an score-script "
Expand Down Expand Up @@ -108,7 +108,7 @@ public ScoreScript(Map<String, Object> params, SearchLookup lookup, LeafReaderCo
this.leafLookup = lookup.getLeafSearchLookup(leafContext);
params = new HashMap<>(params);
params.putAll(leafLookup.asMap());
this.params = new DynamicMap(params, FUNCTIONS);
this.params = new DynamicMap(params, PARAMS_FUNCTIONS);
this.docBase = leafContext.docBase;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public abstract static class MapScript {

private static final DeprecationLogger deprecationLogger =
new DeprecationLogger(LogManager.getLogger(DynamicMap.class));
private static final Map<String, Function<Object, Object>> FUNCTIONS = Map.of(
private static final Map<String, Function<Object, Object>> PARAMS_FUNCTIONS = Map.of(
"doc", value -> {
deprecationLogger.deprecatedAndMaybeLog("map-script_doc",
"Accessing variable [doc] via [params.doc] from within an scripted metric agg map script "
Expand Down Expand Up @@ -97,7 +97,7 @@ public MapScript(Map<String, Object> params, Map<String, Object> state, SearchLo
if (leafLookup != null) {
params = new HashMap<>(params); // copy params so we aren't modifying input
params.putAll(leafLookup.asMap()); // add lookup vars
params = new DynamicMap(params, FUNCTIONS); // wrap with deprecations
params = new DynamicMap(params, PARAMS_FUNCTIONS); // wrap with deprecations
}
this.params = params;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public abstract class TermsSetQueryScript {

private static final DeprecationLogger deprecationLogger =
new DeprecationLogger(LogManager.getLogger(DynamicMap.class));
private static final Map<String, Function<Object, Object>> FUNCTIONS = Map.of(
private static final Map<String, Function<Object, Object>> PARAMS_FUNCTIONS = Map.of(
"doc", value -> {
deprecationLogger.deprecatedAndMaybeLog("terms-set-query-script_doc",
"Accessing variable [doc] via [params.doc] from within an terms-set-query-script "
Expand Down Expand Up @@ -66,7 +66,7 @@ public TermsSetQueryScript(Map<String, Object> params, SearchLookup lookup, Leaf
Map<String, Object> parameters = new HashMap<>(params);
this.leafLookup = lookup.getLeafSearchLookup(leafContext);
parameters.putAll(leafLookup.asMap());
this.params = new DynamicMap(parameters, FUNCTIONS);
this.params = new DynamicMap(parameters, PARAMS_FUNCTIONS);
}

protected TermsSetQueryScript() {
Expand Down