From 28710c985d7f294a38bcf666a367449e5c486578 Mon Sep 17 00:00:00 2001 From: Armin Braun Date: Tue, 1 Sep 2020 19:46:10 +0200 Subject: [PATCH] Dry up Settings from Map Construction (#61778) (#61803) We used the same hack all over the place. At least drying it up to a single place. Co-authored-by: Jay Modi --- .../client/indices/AnalyzeRequest.java | 10 +-------- .../client/indices/CreateIndexRequest.java | 8 +------ .../indices/PutIndexTemplateRequest.java | 9 +------- .../config/CategorizationAnalyzerConfig.java | 9 ++------ .../put/PutRepositoryRequest.java | 11 +--------- .../ClusterUpdateSettingsRequest.java | 21 +++---------------- .../restore/RestoreSnapshotRequest.java | 10 +-------- .../indices/create/CreateIndexRequest.java | 8 +------ .../settings/put/UpdateSettingsRequest.java | 10 +-------- .../template/put/PutIndexTemplateRequest.java | 8 +------ .../common/settings/Settings.java | 14 +++++++++++++ .../index/analysis/NameOrDefinition.java | 9 ++------ 12 files changed, 29 insertions(+), 98 deletions(-) diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/indices/AnalyzeRequest.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/indices/AnalyzeRequest.java index 1aed59227e8..4f93a5dd5d5 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/indices/AnalyzeRequest.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/indices/AnalyzeRequest.java @@ -25,8 +25,6 @@ import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.xcontent.ToXContentFragment; import org.elasticsearch.common.xcontent.ToXContentObject; import org.elasticsearch.common.xcontent.XContentBuilder; -import org.elasticsearch.common.xcontent.XContentFactory; -import org.elasticsearch.common.xcontent.XContentType; import java.io.IOException; import java.util.ArrayList; @@ -207,13 +205,7 @@ public class AnalyzeRequest implements Validatable, ToXContentObject { NameOrDefinition(Map definition) { this.name = null; Objects.requireNonNull(definition); - try { - XContentBuilder builder = XContentFactory.contentBuilder(XContentType.JSON); - builder.map(definition); - this.definition = Settings.builder().loadFromSource(Strings.toString(builder), builder.contentType()).build(); - } catch (IOException e) { - throw new IllegalArgumentException("Failed to parse [" + definition + "]", e); - } + this.definition = Settings.builder().loadFromMap(definition).build(); } @Override diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/indices/CreateIndexRequest.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/indices/CreateIndexRequest.java index 1a018591dc7..290fca008fe 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/indices/CreateIndexRequest.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/indices/CreateIndexRequest.java @@ -127,13 +127,7 @@ public class CreateIndexRequest extends TimedRequest implements Validatable, ToX * The settings to create the index with (either json/yaml/properties format) */ public CreateIndexRequest settings(Map source) { - try { - XContentBuilder builder = XContentFactory.contentBuilder(XContentType.JSON); - builder.map(source); - settings(Strings.toString(builder), XContentType.JSON); - } catch (IOException e) { - throw new ElasticsearchGenerationException("Failed to generate [" + source + "]", e); - } + this.settings = Settings.builder().loadFromMap(source).build(); return this; } diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/indices/PutIndexTemplateRequest.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/indices/PutIndexTemplateRequest.java index 5e10ae1b8f7..54e6f350f65 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/indices/PutIndexTemplateRequest.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/indices/PutIndexTemplateRequest.java @@ -26,7 +26,6 @@ import org.elasticsearch.action.admin.indices.alias.Alias; import org.elasticsearch.action.support.IndicesOptions; import org.elasticsearch.action.support.master.MasterNodeRequest; import org.elasticsearch.common.Nullable; -import org.elasticsearch.common.Strings; import org.elasticsearch.common.bytes.BytesArray; import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.settings.Settings; @@ -188,13 +187,7 @@ public class PutIndexTemplateRequest extends MasterNodeRequest source) { - try { - XContentBuilder builder = XContentFactory.contentBuilder(XContentType.JSON); - builder.map(source); - settings(Strings.toString(builder), XContentType.JSON); - } catch (IOException e) { - throw new ElasticsearchGenerationException("Failed to generate [" + source + "]", e); - } + this.settings = Settings.builder().loadFromMap(source).build(); return this; } diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/job/config/CategorizationAnalyzerConfig.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/job/config/CategorizationAnalyzerConfig.java index 3a2243d6548..362b1705efd 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/job/config/CategorizationAnalyzerConfig.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/ml/job/config/CategorizationAnalyzerConfig.java @@ -19,13 +19,10 @@ package org.elasticsearch.client.ml.job.config; import org.elasticsearch.common.ParseField; -import org.elasticsearch.common.Strings; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.xcontent.ToXContentFragment; import org.elasticsearch.common.xcontent.XContentBuilder; -import org.elasticsearch.common.xcontent.XContentFactory; import org.elasticsearch.common.xcontent.XContentParser; -import org.elasticsearch.common.xcontent.XContentType; import org.elasticsearch.rest.action.admin.indices.RestAnalyzeAction; import java.io.IOException; @@ -160,10 +157,8 @@ public class CategorizationAnalyzerConfig implements ToXContentFragment { this.name = null; Objects.requireNonNull(definition); try { - XContentBuilder builder = XContentFactory.contentBuilder(XContentType.JSON); - builder.map(definition); - this.definition = Settings.builder().loadFromSource(Strings.toString(builder), builder.contentType()).build(); - } catch (IOException e) { + this.definition = Settings.builder().loadFromMap(definition).build(); + } catch (Exception e) { throw new IllegalArgumentException("Failed to parse [" + definition + "] in [" + field.getPreferredName() + "]", e); } } diff --git a/server/src/main/java/org/elasticsearch/action/admin/cluster/repositories/put/PutRepositoryRequest.java b/server/src/main/java/org/elasticsearch/action/admin/cluster/repositories/put/PutRepositoryRequest.java index c9a2c6b7517..7b25b8237d8 100644 --- a/server/src/main/java/org/elasticsearch/action/admin/cluster/repositories/put/PutRepositoryRequest.java +++ b/server/src/main/java/org/elasticsearch/action/admin/cluster/repositories/put/PutRepositoryRequest.java @@ -19,16 +19,13 @@ package org.elasticsearch.action.admin.cluster.repositories.put; -import org.elasticsearch.ElasticsearchGenerationException; import org.elasticsearch.action.ActionRequestValidationException; import org.elasticsearch.action.support.master.AcknowledgedRequest; -import org.elasticsearch.common.Strings; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.xcontent.ToXContentObject; import org.elasticsearch.common.xcontent.XContentBuilder; -import org.elasticsearch.common.xcontent.XContentFactory; import org.elasticsearch.common.xcontent.XContentType; import java.io.IOException; @@ -168,13 +165,7 @@ public class PutRepositoryRequest extends AcknowledgedRequest source) { - try { - XContentBuilder builder = XContentFactory.contentBuilder(XContentType.JSON); - builder.map(source); - settings(Strings.toString(builder), builder.contentType()); - } catch (IOException e) { - throw new ElasticsearchGenerationException("Failed to generate [" + source + "]", e); - } + this.settings = Settings.builder().loadFromMap(source).build(); return this; } diff --git a/server/src/main/java/org/elasticsearch/action/admin/cluster/settings/ClusterUpdateSettingsRequest.java b/server/src/main/java/org/elasticsearch/action/admin/cluster/settings/ClusterUpdateSettingsRequest.java index 41ef1e230d7..eb9c16f8eb0 100644 --- a/server/src/main/java/org/elasticsearch/action/admin/cluster/settings/ClusterUpdateSettingsRequest.java +++ b/server/src/main/java/org/elasticsearch/action/admin/cluster/settings/ClusterUpdateSettingsRequest.java @@ -19,18 +19,15 @@ package org.elasticsearch.action.admin.cluster.settings; -import org.elasticsearch.ElasticsearchGenerationException; import org.elasticsearch.action.ActionRequestValidationException; import org.elasticsearch.action.support.master.AcknowledgedRequest; import org.elasticsearch.common.ParseField; -import org.elasticsearch.common.Strings; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.xcontent.ObjectParser; import org.elasticsearch.common.xcontent.ToXContentObject; import org.elasticsearch.common.xcontent.XContentBuilder; -import org.elasticsearch.common.xcontent.XContentFactory; import org.elasticsearch.common.xcontent.XContentParser; import org.elasticsearch.common.xcontent.XContentType; @@ -115,13 +112,7 @@ public class ClusterUpdateSettingsRequest extends AcknowledgedRequest source) { - try { - XContentBuilder builder = XContentFactory.contentBuilder(XContentType.JSON); - builder.map(source); - transientSettings(Strings.toString(builder), builder.contentType()); - } catch (IOException e) { - throw new ElasticsearchGenerationException("Failed to generate [" + source + "]", e); - } + this.transientSettings = Settings.builder().loadFromMap(source).build(); return this; } @@ -153,13 +144,7 @@ public class ClusterUpdateSettingsRequest extends AcknowledgedRequest source) { - try { - XContentBuilder builder = XContentFactory.contentBuilder(XContentType.JSON); - builder.map(source); - persistentSettings(Strings.toString(builder), builder.contentType()); - } catch (IOException e) { - throw new ElasticsearchGenerationException("Failed to generate [" + source + "]", e); - } + this.persistentSettings = Settings.builder().loadFromMap(source).build(); return this; } @@ -183,7 +168,7 @@ public class ClusterUpdateSettingsRequest extends AcknowledgedRequest source) { - try { - XContentBuilder builder = XContentFactory.contentBuilder(XContentType.JSON); - builder.map(source); - indexSettings(Strings.toString(builder), builder.contentType()); - } catch (IOException e) { - throw new ElasticsearchGenerationException("Failed to generate [" + source + "]", e); - } + this.indexSettings = Settings.builder().loadFromMap(source).build(); return this; } diff --git a/server/src/main/java/org/elasticsearch/action/admin/indices/create/CreateIndexRequest.java b/server/src/main/java/org/elasticsearch/action/admin/indices/create/CreateIndexRequest.java index 45825a4fa24..766a6fa939e 100644 --- a/server/src/main/java/org/elasticsearch/action/admin/indices/create/CreateIndexRequest.java +++ b/server/src/main/java/org/elasticsearch/action/admin/indices/create/CreateIndexRequest.java @@ -219,13 +219,7 @@ public class CreateIndexRequest extends AcknowledgedRequest * The settings to create the index with (either json/yaml/properties format) */ public CreateIndexRequest settings(Map source) { - try { - XContentBuilder builder = XContentFactory.contentBuilder(XContentType.JSON); - builder.map(source); - settings(Strings.toString(builder), XContentType.JSON); - } catch (IOException e) { - throw new ElasticsearchGenerationException("Failed to generate [" + source + "]", e); - } + this.settings = Settings.builder().loadFromMap(source).build(); return this; } diff --git a/server/src/main/java/org/elasticsearch/action/admin/indices/settings/put/UpdateSettingsRequest.java b/server/src/main/java/org/elasticsearch/action/admin/indices/settings/put/UpdateSettingsRequest.java index d5144bfbb78..7bb6d672b63 100644 --- a/server/src/main/java/org/elasticsearch/action/admin/indices/settings/put/UpdateSettingsRequest.java +++ b/server/src/main/java/org/elasticsearch/action/admin/indices/settings/put/UpdateSettingsRequest.java @@ -19,7 +19,6 @@ package org.elasticsearch.action.admin.indices.settings.put; -import org.elasticsearch.ElasticsearchGenerationException; import org.elasticsearch.action.ActionRequestValidationException; import org.elasticsearch.action.IndicesRequest; import org.elasticsearch.action.support.IndicesOptions; @@ -30,7 +29,6 @@ import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.xcontent.ToXContentObject; import org.elasticsearch.common.xcontent.XContentBuilder; -import org.elasticsearch.common.xcontent.XContentFactory; import org.elasticsearch.common.xcontent.XContentParser; import org.elasticsearch.common.xcontent.XContentType; @@ -169,13 +167,7 @@ public class UpdateSettingsRequest extends AcknowledgedRequest source) { - try { - XContentBuilder builder = XContentFactory.contentBuilder(XContentType.JSON); - builder.map(source); - settings(Strings.toString(builder), builder.contentType()); - } catch (IOException e) { - throw new ElasticsearchGenerationException("Failed to generate [" + source + "]", e); - } + this.settings = Settings.builder().loadFromMap(source).build(); return this; } diff --git a/server/src/main/java/org/elasticsearch/action/admin/indices/template/put/PutIndexTemplateRequest.java b/server/src/main/java/org/elasticsearch/action/admin/indices/template/put/PutIndexTemplateRequest.java index 8f99d8a1c82..c9be1c3102e 100644 --- a/server/src/main/java/org/elasticsearch/action/admin/indices/template/put/PutIndexTemplateRequest.java +++ b/server/src/main/java/org/elasticsearch/action/admin/indices/template/put/PutIndexTemplateRequest.java @@ -226,13 +226,7 @@ public class PutIndexTemplateRequest extends MasterNodeRequest source) { - try { - XContentBuilder builder = XContentFactory.contentBuilder(XContentType.JSON); - builder.map(source); - settings(Strings.toString(builder), XContentType.JSON); - } catch (IOException e) { - throw new ElasticsearchGenerationException("Failed to generate [" + source + "]", e); - } + this.settings = Settings.builder().loadFromMap(source).build(); return this; } diff --git a/server/src/main/java/org/elasticsearch/common/settings/Settings.java b/server/src/main/java/org/elasticsearch/common/settings/Settings.java index 10f854fc03d..6c31687d384 100644 --- a/server/src/main/java/org/elasticsearch/common/settings/Settings.java +++ b/server/src/main/java/org/elasticsearch/common/settings/Settings.java @@ -21,6 +21,7 @@ package org.elasticsearch.common.settings; import org.apache.logging.log4j.Level; import org.apache.lucene.util.SetOnce; +import org.elasticsearch.ElasticsearchGenerationException; import org.elasticsearch.ElasticsearchParseException; import org.elasticsearch.Version; import org.elasticsearch.common.Booleans; @@ -1050,6 +1051,19 @@ public final class Settings implements ToXContentFragment { } } + /** + * Loads settings from a map. + */ + public Builder loadFromMap(Map map) { + // TODO: do this without a serialization round-trip + try (XContentBuilder builder = XContentFactory.contentBuilder(XContentType.JSON)) { + builder.map(map); + return loadFromSource(Strings.toString(builder), builder.contentType()); + } catch (IOException e) { + throw new ElasticsearchGenerationException("Failed to generate [" + map + "]", e); + } + } + /** * Loads settings from the actual string content that represents them using {@link #fromXContent(XContentParser)} */ diff --git a/server/src/main/java/org/elasticsearch/index/analysis/NameOrDefinition.java b/server/src/main/java/org/elasticsearch/index/analysis/NameOrDefinition.java index 1831904de6b..b21b9b3d621 100644 --- a/server/src/main/java/org/elasticsearch/index/analysis/NameOrDefinition.java +++ b/server/src/main/java/org/elasticsearch/index/analysis/NameOrDefinition.java @@ -19,17 +19,14 @@ package org.elasticsearch.index.analysis; -import org.elasticsearch.common.Strings; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.io.stream.Writeable; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.xcontent.ToXContentFragment; import org.elasticsearch.common.xcontent.XContentBuilder; -import org.elasticsearch.common.xcontent.XContentFactory; import org.elasticsearch.common.xcontent.XContentParseException; import org.elasticsearch.common.xcontent.XContentParser; -import org.elasticsearch.common.xcontent.XContentType; import java.io.IOException; import java.util.Map; @@ -49,10 +46,8 @@ public class NameOrDefinition implements Writeable, ToXContentFragment { this.name = null; Objects.requireNonNull(definition); try { - XContentBuilder builder = XContentFactory.contentBuilder(XContentType.JSON); - builder.map(definition); - this.definition = Settings.builder().loadFromSource(Strings.toString(builder), builder.contentType()).build(); - } catch (IOException e) { + this.definition = Settings.builder().loadFromMap(definition).build(); + } catch (Exception e) { throw new IllegalArgumentException("Failed to parse [" + definition + "]", e); } }