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
Next Next commit
change deprecation map to dynamic map
  • Loading branch information
jdconrad committed May 4, 2020
commit 1a6bad076073b0e8dcc9de5f44cd2994be7caa5a
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@

package org.elasticsearch.ingest;

import org.elasticsearch.script.DeprecationMap;
import org.apache.logging.log4j.LogManager;
import org.elasticsearch.common.logging.DeprecationLogger;
import org.elasticsearch.script.DynamicMap;
import org.elasticsearch.script.IngestConditionalScript;
import org.elasticsearch.script.Script;
import org.elasticsearch.script.ScriptService;
Expand All @@ -35,13 +37,20 @@
import java.util.Set;
import java.util.concurrent.TimeUnit;
import java.util.function.BiConsumer;
import java.util.function.Function;
import java.util.function.LongSupplier;
import java.util.stream.Collectors;

public class ConditionalProcessor extends AbstractProcessor implements WrappingProcessor {

private static final Map<String, String> DEPRECATIONS =
Map.of("_type", "[types removal] Looking up doc types [_type] in scripts is deprecated.");
private static final DeprecationLogger deprecationLogger =
new DeprecationLogger(LogManager.getLogger(DynamicMap.class));
private static final Map<String, Function<Object, Object>> FUNCTIONS = Map.of(
"_type", value -> {
deprecationLogger.deprecatedAndMaybeLog("conditional-processor__type",
"[types removal] Looking up doc types [_type] in scripts is deprecated.");
return value;
});

static final String TYPE = "conditional";

Expand Down Expand Up @@ -102,8 +111,7 @@ public IngestDocument execute(IngestDocument ingestDocument) throws Exception {
boolean evaluate(IngestDocument ingestDocument) {
IngestConditionalScript script =
scriptService.compile(condition, IngestConditionalScript.CONTEXT).newInstance(condition.getParams());
return script.execute(new UnmodifiableIngestData(
new DeprecationMap(ingestDocument.getSourceAndMetadata(), DEPRECATIONS, "conditional-processor")));
return script.execute(new UnmodifiableIngestData(new DynamicMap(ingestDocument.getSourceAndMetadata(), FUNCTIONS)));
}

public Processor getInnerProcessor() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,11 @@
*/
package org.elasticsearch.script;

import org.apache.logging.log4j.LogManager;
import org.apache.lucene.index.LeafReaderContext;
import org.apache.lucene.search.Scorable;
import org.elasticsearch.ElasticsearchException;
import org.elasticsearch.common.logging.DeprecationLogger;
import org.elasticsearch.common.lucene.ScorerAware;
import org.elasticsearch.index.fielddata.ScriptDocValues;
import org.elasticsearch.search.lookup.LeafSearchLookup;
Expand All @@ -29,14 +31,25 @@
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.function.Function;

abstract class AbstractSortScript implements ScorerAware {

private static final Map<String, String> DEPRECATIONS = Map.of(
"doc",
"Accessing variable [doc] via [params.doc] from within a sort-script is deprecated in favor of directly accessing [doc].",
"_doc",
"Accessing variable [doc] via [params._doc] from within a sort-script is deprecated in favor of directly accessing [doc].");
private static final DeprecationLogger deprecationLogger =
new DeprecationLogger(LogManager.getLogger(DynamicMap.class));
private static final Map<String, Function<Object, Object>> FUNCTIONS = Map.of(
jdconrad marked this conversation as resolved.
Show resolved Hide resolved
"doc", value -> {
deprecationLogger.deprecatedAndMaybeLog("sort-script_doc",
"Accessing variable [doc] via [params.doc] from within an sort-script "
+ "is deprecated in favor of directly accessing [doc].");
return value;
},
"_doc", value -> {
deprecationLogger.deprecatedAndMaybeLog("sort-script__doc",
"Accessing variable [doc] via [params._doc] from within an sort-script "
+ "is deprecated in favor of directly accessing [doc].");
return value;
});

/**
* The generic runtime parameters for the script.
Expand All @@ -55,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 DeprecationMap(parameters, DEPRECATIONS, "sort-script");
this.params = new DynamicMap(parameters, FUNCTIONS);
}

protected AbstractSortScript() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,11 @@
*/
package org.elasticsearch.script;

import org.apache.logging.log4j.LogManager;
import org.apache.lucene.index.LeafReaderContext;
import org.apache.lucene.search.Scorable;
import org.elasticsearch.ElasticsearchException;
import org.elasticsearch.common.logging.DeprecationLogger;
import org.elasticsearch.common.lucene.ScorerAware;
import org.elasticsearch.index.fielddata.ScriptDocValues;
import org.elasticsearch.search.lookup.LeafSearchLookup;
Expand All @@ -29,20 +31,29 @@
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.function.Function;

public abstract class AggregationScript implements ScorerAware {

public static final String[] PARAMETERS = {};

public static final ScriptContext<Factory> CONTEXT = new ScriptContext<>("aggs", Factory.class);

private static final Map<String, String> DEPRECATIONS = Map.of(
"doc",
"Accessing variable [doc] via [params.doc] from within an aggregation-script "
+ "is deprecated in favor of directly accessing [doc].",
"_doc",
"Accessing variable [doc] via [params._doc] from within an aggregation-script "
+ "is deprecated in favor of directly accessing [doc].");
private static final DeprecationLogger deprecationLogger =
new DeprecationLogger(LogManager.getLogger(DynamicMap.class));
private static final Map<String, Function<Object, Object>> FUNCTIONS = Map.of(
"doc", value -> {
deprecationLogger.deprecatedAndMaybeLog("aggregation-script_doc",
"Accessing variable [doc] via [params.doc] from within an aggregation-script "
+ "is deprecated in favor of directly accessing [doc].");
return value;
},
"_doc", value -> {
deprecationLogger.deprecatedAndMaybeLog("aggregation-script__doc",
"Accessing variable [doc] via [params._doc] from within an aggregation-script "
+ "is deprecated in favor of directly accessing [doc].");
return value;
});

/**
* The generic runtime parameters for the script.
Expand All @@ -62,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 DeprecationMap(new HashMap<>(params), DEPRECATIONS, "aggregation-script");
this.params = new DynamicMap(new HashMap<>(params), FUNCTIONS);
this.leafLookup = lookup.getLeafSearchLookup(leafContext);
this.params.putAll(leafLookup.asMap());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,28 +19,20 @@

package org.elasticsearch.script;

import org.apache.logging.log4j.LogManager;
import org.elasticsearch.common.logging.DeprecationLogger;

import java.util.Collection;
import java.util.Map;
import java.util.Set;
import java.util.function.Function;

public final class DeprecationMap implements Map<String, Object> {

private static final DeprecationLogger deprecationLogger =
new DeprecationLogger(LogManager.getLogger(DeprecationMap.class));
public final class DynamicMap implements Map<String, Object> {
jdconrad marked this conversation as resolved.
Show resolved Hide resolved

private final Map<String, Object> delegate;

private final Map<String, String> deprecations;

private final String logKeyPrefix;
private final Map<String, Function<Object, Object>> functions;

public DeprecationMap(Map<String, Object> delegate, Map<String, String> deprecations, String logKeyPrefix) {
public DynamicMap(Map<String, Object> delegate, Map<String, Function<Object, Object>> functions) {
this.delegate = delegate;
this.deprecations = deprecations;
this.logKeyPrefix = logKeyPrefix;
this.functions = functions;
}

@Override
Expand All @@ -65,11 +57,12 @@ public boolean containsValue(final Object value) {

@Override
public Object get(final Object key) {
String deprecationMessage = deprecations.get(key);
if (deprecationMessage != null) {
deprecationLogger.deprecatedAndMaybeLog(logKeyPrefix + "_" + key, deprecationMessage);
Object value = delegate.get(key);
Function<Object, Object> function = functions.get(key);
if (function != null) {
value = function.apply(value);
}
return delegate.get(key);
return value;
}

@Override
Expand Down
25 changes: 19 additions & 6 deletions server/src/main/java/org/elasticsearch/script/FieldScript.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,17 @@

package org.elasticsearch.script;

import org.apache.logging.log4j.LogManager;
import org.apache.lucene.index.LeafReaderContext;
import org.elasticsearch.common.logging.DeprecationLogger;
import org.elasticsearch.index.fielddata.ScriptDocValues;
import org.elasticsearch.search.lookup.LeafSearchLookup;
import org.elasticsearch.search.lookup.SearchLookup;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.function.Function;

/**
* A script to produce dynamic values for return fields.
Expand All @@ -35,11 +38,21 @@ public abstract class FieldScript {

public static final String[] PARAMETERS = {};

private static final Map<String, String> DEPRECATIONS = Map.of(
"doc",
"Accessing variable [doc] via [params.doc] from within a field script is deprecated in favor of directly accessing [doc].",
"_doc",
"Accessing variable [doc] via [params._doc] from within a field script is deprecated in favor of directly accessing [doc].");
private static final DeprecationLogger deprecationLogger =
new DeprecationLogger(LogManager.getLogger(DynamicMap.class));
private static final Map<String, Function<Object, Object>> FUNCTIONS = Map.of(
"doc", value -> {
deprecationLogger.deprecatedAndMaybeLog("field-script_doc",
"Accessing variable [doc] via [params.doc] from within an field-script "
+ "is deprecated in favor of directly accessing [doc].");
return value;
},
"_doc", value -> {
deprecationLogger.deprecatedAndMaybeLog("field-script__doc",
"Accessing variable [doc] via [params._doc] from within an field-script "
+ "is deprecated in favor of directly accessing [doc].");
return value;
});

/** The generic runtime parameters for the script. */
private final Map<String, Object> params;
Expand All @@ -51,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 DeprecationMap(params, DEPRECATIONS, "field-script");
this.params = new DynamicMap(params, FUNCTIONS);
}

// for expression engine
Expand Down
26 changes: 19 additions & 7 deletions server/src/main/java/org/elasticsearch/script/ScoreScript.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,12 @@
*/
package org.elasticsearch.script;

import org.apache.logging.log4j.LogManager;
import org.apache.lucene.index.LeafReaderContext;
import org.apache.lucene.search.Explanation;
import org.apache.lucene.search.Scorable;
import org.elasticsearch.Version;
import org.elasticsearch.common.logging.DeprecationLogger;
import org.elasticsearch.index.fielddata.ScriptDocValues;
import org.elasticsearch.search.lookup.LeafSearchLookup;
import org.elasticsearch.search.lookup.SearchLookup;
Expand All @@ -31,6 +33,7 @@
import java.util.HashMap;
import java.util.Map;
import java.util.function.DoubleSupplier;
import java.util.function.Function;

/**
* A script used for adjusting the score on a per document basis.
Expand Down Expand Up @@ -61,12 +64,21 @@ public Explanation get(double score, Explanation subQueryExplanation) {
}
}

private static final Map<String, String> DEPRECATIONS = Map.of(
"doc",
"Accessing variable [doc] via [params.doc] from within a score script "
+ "is deprecated in favor of directly accessing [doc].",
"_doc", "Accessing variable [doc] via [params._doc] from within a score script "
+ "is deprecated in favor of directly accessing [doc].");
private static final DeprecationLogger deprecationLogger =
new DeprecationLogger(LogManager.getLogger(DynamicMap.class));
private static final Map<String, Function<Object, Object>> FUNCTIONS = Map.of(
"doc", value -> {
deprecationLogger.deprecatedAndMaybeLog("score-script_doc",
"Accessing variable [doc] via [params.doc] from within an score-script "
+ "is deprecated in favor of directly accessing [doc].");
return value;
},
"_doc", value -> {
deprecationLogger.deprecatedAndMaybeLog("score-script__doc",
"Accessing variable [doc] via [params._doc] from within an score-script "
+ "is deprecated in favor of directly accessing [doc].");
return value;
});

public static final String[] PARAMETERS = new String[]{ "explanation" };

Expand Down Expand Up @@ -96,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 DeprecationMap(params, DEPRECATIONS, "score-script");
this.params = new DynamicMap(params, FUNCTIONS);
this.docBase = leafContext.docBase;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,11 @@

package org.elasticsearch.script;

import org.apache.logging.log4j.LogManager;
import org.apache.lucene.index.LeafReaderContext;
import org.apache.lucene.search.Scorable;
import org.elasticsearch.ElasticsearchException;
import org.elasticsearch.common.logging.DeprecationLogger;
import org.elasticsearch.index.fielddata.ScriptDocValues;
import org.elasticsearch.search.lookup.LeafSearchLookup;
import org.elasticsearch.search.lookup.SearchLookup;
Expand All @@ -30,6 +32,7 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Function;

public class ScriptedMetricAggContexts {

Expand Down Expand Up @@ -61,14 +64,27 @@ public interface Factory extends ScriptFactory {
}

public abstract static class MapScript {
private static final Map<String, String> DEPRECATIONS = Map.of(
"doc",
"Accessing variable [doc] via [params.doc] from within a scripted metric agg map script "
+ "is deprecated in favor of directly accessing [doc].",
"_doc", "Accessing variable [doc] via [params._doc] from within a scripted metric agg map script "
+ "is deprecated in favor of directly accessing [doc].",
"_agg", "Accessing variable [_agg] via [params._agg] from within a scripted metric agg map script "
+ "is deprecated in favor of using [state].");

private static final DeprecationLogger deprecationLogger =
new DeprecationLogger(LogManager.getLogger(DynamicMap.class));
private static final Map<String, Function<Object, Object>> FUNCTIONS = Map.of(
"doc", value -> {
deprecationLogger.deprecatedAndMaybeLog("map-script_doc",
"Accessing variable [doc] via [params.doc] from within an scripted metric agg map script "
+ "is deprecated in favor of directly accessing [doc].");
return value;
},
"_doc", value -> {
deprecationLogger.deprecatedAndMaybeLog("map-script__doc",
"Accessing variable [doc] via [params._doc] from within an scripted metric agg map script "
+ "is deprecated in favor of directly accessing [doc].");
return value;
}, "_agg", value -> {
deprecationLogger.deprecatedAndMaybeLog("map-script__agg",
"Accessing variable [_agg] via [params._agg] from within a scripted metric agg map script "
+ "is deprecated in favor of using [state].");
return value;
});

private final Map<String, Object> params;
private final Map<String, Object> state;
Expand All @@ -81,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 DeprecationMap(params, DEPRECATIONS, "map-script"); // wrap with deprecations
params = new DynamicMap(params, FUNCTIONS); // wrap with deprecations
}
this.params = params;
}
Expand Down
Loading