Run spotless and exclude checkstyle on plugins module (#1417)

Signed-off-by: Owais Kazi <owaiskazi19@gmail.com>
This commit is contained in:
Owais Kazi 2021-10-21 17:49:03 -07:00 committed by GitHub
parent 8b4a7683d5
commit d02443a265
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
19 changed files with 109 additions and 77 deletions

View File

@ -21,6 +21,8 @@
<suppress files="server" checks="." /> <suppress files="server" checks="." />
<!-- Excludes checkstyle run on client module --> <!-- Excludes checkstyle run on client module -->
<suppress files="client" checks="." /> <suppress files="client" checks="." />
<!-- Excludes checkstyle run on plugins/examples -->
<suppress files="plugins[/\\]examples" checks="." />
<!-- <!--
Truly temporary suppressions suppression of snippets included in Truly temporary suppressions suppression of snippets included in

View File

@ -57,13 +57,6 @@ import org.opensearch.gradle.BuildPlugin
// Do not add new sub-projects here! // Do not add new sub-projects here!
def projectPathsToExclude = [ def projectPathsToExclude = [
':example-plugins:custom-settings',
':example-plugins:custom-significance-heuristic',
':example-plugins:custom-suggester',
':example-plugins:painless-whitelist',
':example-plugins:rescore',
':example-plugins:rest-handler',
':example-plugins:script-expert-scoring',
':libs:opensearch-cli', ':libs:opensearch-cli',
':libs:opensearch-core', ':libs:opensearch-core',
':libs:opensearch-dissect', ':libs:opensearch-dissect',

View File

@ -81,9 +81,12 @@ public class ExampleCustomSettingsConfig {
/** /**
* A setting that consists of a list of integers * A setting that consists of a list of integers
*/ */
static final Setting<List<Integer>> LIST_SETTING = static final Setting<List<Integer>> LIST_SETTING = Setting.listSetting(
Setting.listSetting("custom.list", Collections.emptyList(), Integer::valueOf, Property.NodeScope); "custom.list",
Collections.emptyList(),
Integer::valueOf,
Property.NodeScope
);
private final String simple; private final String simple;
private final String validated; private final String validated;

View File

@ -58,12 +58,14 @@ public class ExampleCustomSettingsPlugin extends Plugin {
*/ */
@Override @Override
public List<Setting<?>> getSettings() { public List<Setting<?>> getSettings() {
return Arrays.asList(ExampleCustomSettingsConfig.SIMPLE_SETTING, return Arrays.asList(
ExampleCustomSettingsConfig.SIMPLE_SETTING,
ExampleCustomSettingsConfig.BOOLEAN_SETTING, ExampleCustomSettingsConfig.BOOLEAN_SETTING,
ExampleCustomSettingsConfig.VALIDATED_SETTING, ExampleCustomSettingsConfig.VALIDATED_SETTING,
ExampleCustomSettingsConfig.FILTERED_SETTING, ExampleCustomSettingsConfig.FILTERED_SETTING,
ExampleCustomSettingsConfig.SECURED_SETTING, ExampleCustomSettingsConfig.SECURED_SETTING,
ExampleCustomSettingsConfig.LIST_SETTING); ExampleCustomSettingsConfig.LIST_SETTING
);
} }
@Override @Override

View File

@ -50,8 +50,10 @@ public class ExampleCustomSettingsConfigTests extends OpenSearchTestCase {
final String actual = VALIDATED_SETTING.get(Settings.builder().put(VALIDATED_SETTING.getKey(), expected).build()); final String actual = VALIDATED_SETTING.get(Settings.builder().put(VALIDATED_SETTING.getKey(), expected).build());
assertEquals(expected, actual); assertEquals(expected, actual);
final IllegalArgumentException exception = expectThrows(IllegalArgumentException.class, () -> final IllegalArgumentException exception = expectThrows(
VALIDATED_SETTING.get(Settings.builder().put("custom.validated", "it's forbidden").build())); IllegalArgumentException.class,
() -> VALIDATED_SETTING.get(Settings.builder().put("custom.validated", "it's forbidden").build())
);
assertEquals("Setting must not contain [forbidden]", exception.getMessage()); assertEquals("Setting must not contain [forbidden]", exception.getMessage());
} }
} }

View File

@ -47,8 +47,7 @@ public class SimpleHeuristic extends SignificanceHeuristic {
public static final String NAME = "simple"; public static final String NAME = "simple";
public static final ObjectParser<SimpleHeuristic, Void> PARSER = new ObjectParser<>(NAME, SimpleHeuristic::new); public static final ObjectParser<SimpleHeuristic, Void> PARSER = new ObjectParser<>(NAME, SimpleHeuristic::new);
public SimpleHeuristic() { public SimpleHeuristic() {}
}
/** /**
* Read from a stream. * Read from a stream.

View File

@ -49,20 +49,26 @@ public class CustomSuggester extends Suggester<CustomSuggestionContext> {
String name, String name,
CustomSuggestionContext suggestion, CustomSuggestionContext suggestion,
IndexSearcher searcher, IndexSearcher searcher,
CharsRefBuilder spare) throws IOException { CharsRefBuilder spare
) throws IOException {
// create two suggestions with 12 and 123 appended // create two suggestions with 12 and 123 appended
CustomSuggestion response = emptySuggestion(name, suggestion, spare); CustomSuggestion response = emptySuggestion(name, suggestion, spare);
CustomSuggestion.Entry entry = response.getEntries().get(0); CustomSuggestion.Entry entry = response.getEntries().get(0);
String text = entry.getText().string(); String text = entry.getText().string();
String firstOption = String firstOption = String.format(Locale.ROOT, "%s-%s-%s-%s", text, suggestion.getField(), suggestion.options.get("suffix"), "12");
String.format(Locale.ROOT, "%s-%s-%s-%s", text, suggestion.getField(), suggestion.options.get("suffix"), "12");
CustomSuggestion.Entry.Option option12 = new CustomSuggestion.Entry.Option(new Text(firstOption), 0.9f, "option-dummy-value-1"); CustomSuggestion.Entry.Option option12 = new CustomSuggestion.Entry.Option(new Text(firstOption), 0.9f, "option-dummy-value-1");
entry.addOption(option12); entry.addOption(option12);
String secondOption = String secondOption = String.format(
String.format(Locale.ROOT, "%s-%s-%s-%s", text, suggestion.getField(), suggestion.options.get("suffix"), "123"); Locale.ROOT,
"%s-%s-%s-%s",
text,
suggestion.getField(),
suggestion.options.get("suffix"),
"123"
);
CustomSuggestion.Entry.Option option123 = new CustomSuggestion.Entry.Option(new Text(secondOption), 0.8f, "option-dummy-value-2"); CustomSuggestion.Entry.Option option123 = new CustomSuggestion.Entry.Option(new Text(secondOption), 0.8f, "option-dummy-value-2");
entry.addOption(option123); entry.addOption(option123);
@ -70,8 +76,7 @@ public class CustomSuggester extends Suggester<CustomSuggestionContext> {
} }
@Override @Override
protected CustomSuggestion emptySuggestion(String name, CustomSuggestionContext suggestion, protected CustomSuggestion emptySuggestion(String name, CustomSuggestionContext suggestion, CharsRefBuilder spare) throws IOException {
CharsRefBuilder spare) throws IOException {
String text = suggestion.getText().utf8ToString(); String text = suggestion.getText().utf8ToString();
CustomSuggestion response = new CustomSuggestion(name, suggestion.getSize(), "suggestion-dummy-value"); CustomSuggestion response = new CustomSuggestion(name, suggestion.getSize(), "suggestion-dummy-value");
CustomSuggestion.Entry entry = new CustomSuggestion.Entry(new Text(text), 0, text.length(), "entry-dummy-value"); CustomSuggestion.Entry entry = new CustomSuggestion.Entry(new Text(text), 0, text.length(), "entry-dummy-value");

View File

@ -169,13 +169,15 @@ public class CustomSuggestion extends Suggest.Suggestion<CustomSuggestion.Entry>
public static class Option extends Suggest.Suggestion.Entry.Option { public static class Option extends Suggest.Suggestion.Entry.Option {
private static final ConstructingObjectParser<Option, Void> PARSER = new ConstructingObjectParser<>( private static final ConstructingObjectParser<Option, Void> PARSER = new ConstructingObjectParser<>(
"CustomSuggestionObjectParser", true, "CustomSuggestionObjectParser",
true,
args -> { args -> {
Text text = new Text((String) args[0]); Text text = new Text((String) args[0]);
float score = (float) args[1]; float score = (float) args[1];
String dummy = (String) args[2]; String dummy = (String) args[2];
return new Option(text, score, dummy); return new Option(text, score, dummy);
}); }
);
static { static {
PARSER.declareString(constructorArg(), TEXT); PARSER.declareString(constructorArg(), TEXT);

View File

@ -119,8 +119,7 @@ public class CustomSuggestionBuilder extends SuggestionBuilder<CustomSuggestionB
suffix = parser.text(); suffix = parser.text();
} }
} else { } else {
throw new ParsingException(parser.getTokenLocation(), throw new ParsingException(parser.getTokenLocation(), "suggester[custom] doesn't support field [" + currentFieldName + "]");
"suggester[custom] doesn't support field [" + currentFieldName + "]");
} }
} }

View File

@ -53,16 +53,32 @@ public class ExampleWhitelistExtension implements PainlessExtension {
public Map<ScriptContext<?>, List<Whitelist>> getContextWhitelists() { public Map<ScriptContext<?>, List<Whitelist>> getContextWhitelists() {
Map<String, WhitelistAnnotationParser> parsers = new HashMap<>(WhitelistAnnotationParser.BASE_ANNOTATION_PARSERS); Map<String, WhitelistAnnotationParser> parsers = new HashMap<>(WhitelistAnnotationParser.BASE_ANNOTATION_PARSERS);
parsers.put(ExamplePainlessAnnotation.NAME, ExampleWhitelistAnnotationParser.INSTANCE); parsers.put(ExamplePainlessAnnotation.NAME, ExampleWhitelistAnnotationParser.INSTANCE);
Whitelist classWhitelist = Whitelist classWhitelist = WhitelistLoader.loadFromResourceFiles(ExampleWhitelistExtension.class, parsers, "example_whitelist.txt");
WhitelistLoader.loadFromResourceFiles(ExampleWhitelistExtension.class, parsers, "example_whitelist.txt");
ExampleWhitelistedInstance ewi = new ExampleWhitelistedInstance(1); ExampleWhitelistedInstance ewi = new ExampleWhitelistedInstance(1);
WhitelistInstanceBinding addValue = new WhitelistInstanceBinding("example addValue", ewi, WhitelistInstanceBinding addValue = new WhitelistInstanceBinding(
"addValue", "int", Collections.singletonList("int"), Collections.emptyList()); "example addValue",
WhitelistInstanceBinding getValue = new WhitelistInstanceBinding("example getValue", ewi, ewi,
"getValue", "int", Collections.emptyList(), Collections.emptyList()); "addValue",
Whitelist instanceWhitelist = new Whitelist(ewi.getClass().getClassLoader(), Collections.emptyList(), "int",
Collections.emptyList(), Collections.emptyList(), Arrays.asList(addValue, getValue)); Collections.singletonList("int"),
Collections.emptyList()
);
WhitelistInstanceBinding getValue = new WhitelistInstanceBinding(
"example getValue",
ewi,
"getValue",
"int",
Collections.emptyList(),
Collections.emptyList()
);
Whitelist instanceWhitelist = new Whitelist(
ewi.getClass().getClassLoader(),
Collections.emptyList(),
Collections.emptyList(),
Collections.emptyList(),
Arrays.asList(addValue, getValue)
);
return Collections.singletonMap(FieldScript.CONTEXT, Arrays.asList(classWhitelist, instanceWhitelist)); return Collections.singletonMap(FieldScript.CONTEXT, Arrays.asList(classWhitelist, instanceWhitelist));
} }

View File

@ -48,4 +48,3 @@ public class PainlessWhitelistClientYamlTestSuiteIT extends OpenSearchClientYaml
return OpenSearchClientYamlSuiteTestCase.createParameters(); return OpenSearchClientYamlSuiteTestCase.createParameters();
} }
} }

View File

@ -101,6 +101,7 @@ public class ExampleRescoreBuilder extends RescorerBuilder<ExampleRescoreBuilder
private static final ParseField FACTOR = new ParseField("factor"); private static final ParseField FACTOR = new ParseField("factor");
private static final ParseField FACTOR_FIELD = new ParseField("factor_field"); private static final ParseField FACTOR_FIELD = new ParseField("factor_field");
@Override @Override
protected void doXContent(XContentBuilder builder, Params params) throws IOException { protected void doXContent(XContentBuilder builder, Params params) throws IOException {
builder.field(FACTOR.getPreferredName(), factor); builder.field(FACTOR.getPreferredName(), factor);
@ -109,20 +110,22 @@ public class ExampleRescoreBuilder extends RescorerBuilder<ExampleRescoreBuilder
} }
} }
private static final ConstructingObjectParser<ExampleRescoreBuilder, Void> PARSER = new ConstructingObjectParser<>(NAME, private static final ConstructingObjectParser<ExampleRescoreBuilder, Void> PARSER = new ConstructingObjectParser<>(
args -> new ExampleRescoreBuilder((float) args[0], (String) args[1])); NAME,
args -> new ExampleRescoreBuilder((float) args[0], (String) args[1])
);
static { static {
PARSER.declareFloat(constructorArg(), FACTOR); PARSER.declareFloat(constructorArg(), FACTOR);
PARSER.declareString(optionalConstructorArg(), FACTOR_FIELD); PARSER.declareString(optionalConstructorArg(), FACTOR_FIELD);
} }
public static ExampleRescoreBuilder fromXContent(XContentParser parser) { public static ExampleRescoreBuilder fromXContent(XContentParser parser) {
return PARSER.apply(parser, null); return PARSER.apply(parser, null);
} }
@Override @Override
public RescoreContext innerBuildContext(int windowSize, QueryShardContext context) throws IOException { public RescoreContext innerBuildContext(int windowSize, QueryShardContext context) throws IOException {
IndexFieldData<?> factorField = IndexFieldData<?> factorField = this.factorField == null ? null : context.getForField(context.fieldMapper(this.factorField));
this.factorField == null ? null : context.getForField(context.fieldMapper(this.factorField));
return new ExampleRescoreContext(windowSize, factor, factorField); return new ExampleRescoreContext(windowSize, factor, factorField);
} }
@ -132,8 +135,7 @@ public class ExampleRescoreBuilder extends RescorerBuilder<ExampleRescoreBuilder
return false; return false;
} }
ExampleRescoreBuilder other = (ExampleRescoreBuilder) obj; ExampleRescoreBuilder other = (ExampleRescoreBuilder) obj;
return factor == other.factor return factor == other.factor && Objects.equals(factorField, other.factorField);
&& Objects.equals(factorField, other.factorField);
} }
@Override @Override
@ -203,12 +205,22 @@ public class ExampleRescoreBuilder extends RescorerBuilder<ExampleRescoreBuilder
data = ((LeafNumericFieldData) fd).getDoubleValues(); data = ((LeafNumericFieldData) fd).getDoubleValues();
} }
if (false == data.advanceExact(topDocs.scoreDocs[i].doc - leaf.docBase)) { if (false == data.advanceExact(topDocs.scoreDocs[i].doc - leaf.docBase)) {
throw new IllegalArgumentException("document [" + topDocs.scoreDocs[i].doc throw new IllegalArgumentException(
+ "] does not have the field [" + context.factorField.getFieldName() + "]"); "document ["
+ topDocs.scoreDocs[i].doc
+ "] does not have the field ["
+ context.factorField.getFieldName()
+ "]"
);
} }
if (data.docValueCount() > 1) { if (data.docValueCount() > 1) {
throw new IllegalArgumentException("document [" + topDocs.scoreDocs[i].doc throw new IllegalArgumentException(
+ "] has more than one value for [" + context.factorField.getFieldName() + "]"); "document ["
+ topDocs.scoreDocs[i].doc
+ "] has more than one value for ["
+ context.factorField.getFieldName()
+ "]"
);
} }
topDocs.scoreDocs[i].score *= data.nextValue(); topDocs.scoreDocs[i].score *= data.nextValue();
} }
@ -228,8 +240,8 @@ public class ExampleRescoreBuilder extends RescorerBuilder<ExampleRescoreBuilder
} }
@Override @Override
public Explanation explain(int topLevelDocId, IndexSearcher searcher, RescoreContext rescoreContext, public Explanation explain(int topLevelDocId, IndexSearcher searcher, RescoreContext rescoreContext, Explanation sourceExplanation)
Explanation sourceExplanation) throws IOException { throws IOException {
ExampleRescoreContext context = (ExampleRescoreContext) rescoreContext; ExampleRescoreContext context = (ExampleRescoreContext) rescoreContext;
// Note that this is inaccurate because it ignores factor field // Note that this is inaccurate because it ignores factor field
return Explanation.match(context.factor, "test", singletonList(sourceExplanation)); return Explanation.match(context.factor, "test", singletonList(sourceExplanation));

View File

@ -43,6 +43,7 @@ public class ExampleRescorePlugin extends Plugin implements SearchPlugin {
@Override @Override
public List<RescorerSpec<?>> getRescorers() { public List<RescorerSpec<?>> getRescorers() {
return singletonList( return singletonList(
new RescorerSpec<>(ExampleRescoreBuilder.NAME, ExampleRescoreBuilder::new, ExampleRescoreBuilder::fromXContent)); new RescorerSpec<>(ExampleRescoreBuilder.NAME, ExampleRescoreBuilder::new, ExampleRescoreBuilder::fromXContent)
);
} }
} }

View File

@ -59,14 +59,16 @@ public class ExampleRescoreBuilderTests extends AbstractWireSerializingTestCase<
protected ExampleRescoreBuilder mutateInstance(ExampleRescoreBuilder instance) throws IOException { protected ExampleRescoreBuilder mutateInstance(ExampleRescoreBuilder instance) throws IOException {
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
Supplier<ExampleRescoreBuilder> supplier = randomFrom( Supplier<ExampleRescoreBuilder> supplier = randomFrom(
() -> new ExampleRescoreBuilder(instance.factor(), instance.factorField()) () -> new ExampleRescoreBuilder(instance.factor(), instance.factorField()).windowSize(
.windowSize(randomValueOtherThan(instance.windowSize(), () -> between(0, Integer.MAX_VALUE))), randomValueOtherThan(instance.windowSize(), () -> between(0, Integer.MAX_VALUE))
),
() -> new ExampleRescoreBuilder( () -> new ExampleRescoreBuilder(
randomValueOtherThan(instance.factor(), OpenSearchTestCase::randomFloat), instance.factorField()) randomValueOtherThan(instance.factor(), OpenSearchTestCase::randomFloat),
.windowSize(instance.windowSize()), instance.factorField()
() -> new ExampleRescoreBuilder( ).windowSize(instance.windowSize()),
instance.factor(), randomValueOtherThan(instance.factorField(), () -> randomAlphaOfLength(5))) () -> new ExampleRescoreBuilder(instance.factor(), randomValueOtherThan(instance.factorField(), () -> randomAlphaOfLength(5)))
.windowSize(instance.windowSize())); .windowSize(instance.windowSize())
);
return supplier.get(); return supplier.get();
} }

View File

@ -48,4 +48,3 @@ public class ExampleRescoreClientYamlTestSuiteIT extends OpenSearchClientYamlSui
return OpenSearchClientYamlSuiteTestCase.createParameters(); return OpenSearchClientYamlSuiteTestCase.createParameters();
} }
} }

View File

@ -52,9 +52,7 @@ public class ExampleCatAction extends AbstractCatAction {
@Override @Override
public List<Route> routes() { public List<Route> routes() {
return unmodifiableList(asList( return unmodifiableList(asList(new Route(GET, "/_cat/example"), new Route(POST, "/_cat/example")));
new Route(GET, "/_cat/example"),
new Route(POST, "/_cat/example")));
} }
@Override @Override

View File

@ -51,13 +51,15 @@ import static java.util.Collections.singletonList;
public class ExampleRestHandlerPlugin extends Plugin implements ActionPlugin { public class ExampleRestHandlerPlugin extends Plugin implements ActionPlugin {
@Override @Override
public List<RestHandler> getRestHandlers(final Settings settings, public List<RestHandler> getRestHandlers(
final Settings settings,
final RestController restController, final RestController restController,
final ClusterSettings clusterSettings, final ClusterSettings clusterSettings,
final IndexScopedSettings indexScopedSettings, final IndexScopedSettings indexScopedSettings,
final SettingsFilter settingsFilter, final SettingsFilter settingsFilter,
final IndexNameExpressionResolver indexNameExpressionResolver, final IndexNameExpressionResolver indexNameExpressionResolver,
final Supplier<DiscoveryNodes> nodesInCluster) { final Supplier<DiscoveryNodes> nodesInCluster
) {
return singletonList(new ExampleCatAction()); return singletonList(new ExampleCatAction());
} }

View File

@ -59,10 +59,7 @@ import java.util.Set;
public class ExpertScriptPlugin extends Plugin implements ScriptPlugin { public class ExpertScriptPlugin extends Plugin implements ScriptPlugin {
@Override @Override
public ScriptEngine getScriptEngine( public ScriptEngine getScriptEngine(Settings settings, Collection<ScriptContext<?>> contexts) {
Settings settings,
Collection<ScriptContext<?>> contexts
) {
return new MyExpertScriptEngine(); return new MyExpertScriptEngine();
} }

View File

@ -48,4 +48,3 @@ public class ExpertScriptClientYamlTestSuiteIT extends OpenSearchClientYamlSuite
return OpenSearchClientYamlSuiteTestCase.createParameters(); return OpenSearchClientYamlSuiteTestCase.createParameters();
} }
} }