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 <jaymode@users.noreply.github.com>
This commit is contained in:
Armin Braun 2020-09-01 19:46:10 +02:00 committed by GitHub
parent f8158bdb2d
commit 28710c985d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
12 changed files with 29 additions and 98 deletions

View File

@ -25,8 +25,6 @@ import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.xcontent.ToXContentFragment; import org.elasticsearch.common.xcontent.ToXContentFragment;
import org.elasticsearch.common.xcontent.ToXContentObject; import org.elasticsearch.common.xcontent.ToXContentObject;
import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.common.xcontent.XContentType;
import java.io.IOException; import java.io.IOException;
import java.util.ArrayList; import java.util.ArrayList;
@ -207,13 +205,7 @@ public class AnalyzeRequest implements Validatable, ToXContentObject {
NameOrDefinition(Map<String, ?> definition) { NameOrDefinition(Map<String, ?> definition) {
this.name = null; this.name = null;
Objects.requireNonNull(definition); Objects.requireNonNull(definition);
try { this.definition = Settings.builder().loadFromMap(definition).build();
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);
}
} }
@Override @Override

View File

@ -127,13 +127,7 @@ public class CreateIndexRequest extends TimedRequest implements Validatable, ToX
* The settings to create the index with (either json/yaml/properties format) * The settings to create the index with (either json/yaml/properties format)
*/ */
public CreateIndexRequest settings(Map<String, ?> source) { public CreateIndexRequest settings(Map<String, ?> source) {
try { this.settings = Settings.builder().loadFromMap(source).build();
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);
}
return this; return this;
} }

View File

@ -26,7 +26,6 @@ import org.elasticsearch.action.admin.indices.alias.Alias;
import org.elasticsearch.action.support.IndicesOptions; import org.elasticsearch.action.support.IndicesOptions;
import org.elasticsearch.action.support.master.MasterNodeRequest; import org.elasticsearch.action.support.master.MasterNodeRequest;
import org.elasticsearch.common.Nullable; import org.elasticsearch.common.Nullable;
import org.elasticsearch.common.Strings;
import org.elasticsearch.common.bytes.BytesArray; import org.elasticsearch.common.bytes.BytesArray;
import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.bytes.BytesReference;
import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.settings.Settings;
@ -188,13 +187,7 @@ public class PutIndexTemplateRequest extends MasterNodeRequest<PutIndexTemplateR
* The settings to create the index template with (either json or yaml format). * The settings to create the index template with (either json or yaml format).
*/ */
public PutIndexTemplateRequest settings(Map<String, Object> source) { public PutIndexTemplateRequest settings(Map<String, Object> source) {
try { this.settings = Settings.builder().loadFromMap(source).build();
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);
}
return this; return this;
} }

View File

@ -19,13 +19,10 @@
package org.elasticsearch.client.ml.job.config; package org.elasticsearch.client.ml.job.config;
import org.elasticsearch.common.ParseField; import org.elasticsearch.common.ParseField;
import org.elasticsearch.common.Strings;
import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.xcontent.ToXContentFragment; import org.elasticsearch.common.xcontent.ToXContentFragment;
import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.common.xcontent.XContentParser; import org.elasticsearch.common.xcontent.XContentParser;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.rest.action.admin.indices.RestAnalyzeAction; import org.elasticsearch.rest.action.admin.indices.RestAnalyzeAction;
import java.io.IOException; import java.io.IOException;
@ -160,10 +157,8 @@ public class CategorizationAnalyzerConfig implements ToXContentFragment {
this.name = null; this.name = null;
Objects.requireNonNull(definition); Objects.requireNonNull(definition);
try { try {
XContentBuilder builder = XContentFactory.contentBuilder(XContentType.JSON); this.definition = Settings.builder().loadFromMap(definition).build();
builder.map(definition); } catch (Exception e) {
this.definition = Settings.builder().loadFromSource(Strings.toString(builder), builder.contentType()).build();
} catch (IOException e) {
throw new IllegalArgumentException("Failed to parse [" + definition + "] in [" + field.getPreferredName() + "]", e); throw new IllegalArgumentException("Failed to parse [" + definition + "] in [" + field.getPreferredName() + "]", e);
} }
} }

View File

@ -19,16 +19,13 @@
package org.elasticsearch.action.admin.cluster.repositories.put; package org.elasticsearch.action.admin.cluster.repositories.put;
import org.elasticsearch.ElasticsearchGenerationException;
import org.elasticsearch.action.ActionRequestValidationException; import org.elasticsearch.action.ActionRequestValidationException;
import org.elasticsearch.action.support.master.AcknowledgedRequest; 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.StreamInput;
import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.io.stream.StreamOutput;
import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.xcontent.ToXContentObject; import org.elasticsearch.common.xcontent.ToXContentObject;
import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.common.xcontent.XContentType; import org.elasticsearch.common.xcontent.XContentType;
import java.io.IOException; import java.io.IOException;
@ -168,13 +165,7 @@ public class PutRepositoryRequest extends AcknowledgedRequest<PutRepositoryReque
* @return this request * @return this request
*/ */
public PutRepositoryRequest settings(Map<String, Object> source) { public PutRepositoryRequest settings(Map<String, Object> source) {
try { this.settings = Settings.builder().loadFromMap(source).build();
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);
}
return this; return this;
} }

View File

@ -19,18 +19,15 @@
package org.elasticsearch.action.admin.cluster.settings; package org.elasticsearch.action.admin.cluster.settings;
import org.elasticsearch.ElasticsearchGenerationException;
import org.elasticsearch.action.ActionRequestValidationException; import org.elasticsearch.action.ActionRequestValidationException;
import org.elasticsearch.action.support.master.AcknowledgedRequest; import org.elasticsearch.action.support.master.AcknowledgedRequest;
import org.elasticsearch.common.ParseField; import org.elasticsearch.common.ParseField;
import org.elasticsearch.common.Strings;
import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.io.stream.StreamOutput;
import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.xcontent.ObjectParser; import org.elasticsearch.common.xcontent.ObjectParser;
import org.elasticsearch.common.xcontent.ToXContentObject; import org.elasticsearch.common.xcontent.ToXContentObject;
import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.common.xcontent.XContentParser; import org.elasticsearch.common.xcontent.XContentParser;
import org.elasticsearch.common.xcontent.XContentType; import org.elasticsearch.common.xcontent.XContentType;
@ -115,13 +112,7 @@ public class ClusterUpdateSettingsRequest extends AcknowledgedRequest<ClusterUpd
* Sets the transient settings to be updated. They will not survive a full cluster restart * Sets the transient settings to be updated. They will not survive a full cluster restart
*/ */
public ClusterUpdateSettingsRequest transientSettings(Map<String, ?> source) { public ClusterUpdateSettingsRequest transientSettings(Map<String, ?> source) {
try { this.transientSettings = Settings.builder().loadFromMap(source).build();
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);
}
return this; return this;
} }
@ -153,13 +144,7 @@ public class ClusterUpdateSettingsRequest extends AcknowledgedRequest<ClusterUpd
* Sets the persistent settings to be updated. They will get applied cross restarts * Sets the persistent settings to be updated. They will get applied cross restarts
*/ */
public ClusterUpdateSettingsRequest persistentSettings(Map<String, ?> source) { public ClusterUpdateSettingsRequest persistentSettings(Map<String, ?> source) {
try { this.persistentSettings = Settings.builder().loadFromMap(source).build();
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);
}
return this; return this;
} }
@ -183,7 +168,7 @@ public class ClusterUpdateSettingsRequest extends AcknowledgedRequest<ClusterUpd
return builder; return builder;
} }
public static ClusterUpdateSettingsRequest fromXContent(XContentParser parser) throws IOException { public static ClusterUpdateSettingsRequest fromXContent(XContentParser parser) {
return PARSER.apply(parser, null); return PARSER.apply(parser, null);
} }
} }

View File

@ -19,7 +19,6 @@
package org.elasticsearch.action.admin.cluster.snapshots.restore; package org.elasticsearch.action.admin.cluster.snapshots.restore;
import org.elasticsearch.ElasticsearchGenerationException;
import org.elasticsearch.Version; import org.elasticsearch.Version;
import org.elasticsearch.action.ActionRequestValidationException; import org.elasticsearch.action.ActionRequestValidationException;
import org.elasticsearch.action.support.IndicesOptions; import org.elasticsearch.action.support.IndicesOptions;
@ -32,7 +31,6 @@ import org.elasticsearch.common.logging.DeprecationLogger;
import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.xcontent.ToXContentObject; import org.elasticsearch.common.xcontent.ToXContentObject;
import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.common.xcontent.XContentType; import org.elasticsearch.common.xcontent.XContentType;
import java.io.IOException; import java.io.IOException;
@ -434,13 +432,7 @@ public class RestoreSnapshotRequest extends MasterNodeRequest<RestoreSnapshotReq
* Sets settings that should be added/changed in all restored indices * Sets settings that should be added/changed in all restored indices
*/ */
public RestoreSnapshotRequest indexSettings(Map<String, Object> source) { public RestoreSnapshotRequest indexSettings(Map<String, Object> source) {
try { this.indexSettings = Settings.builder().loadFromMap(source).build();
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);
}
return this; return this;
} }

View File

@ -219,13 +219,7 @@ public class CreateIndexRequest extends AcknowledgedRequest<CreateIndexRequest>
* The settings to create the index with (either json/yaml/properties format) * The settings to create the index with (either json/yaml/properties format)
*/ */
public CreateIndexRequest settings(Map<String, ?> source) { public CreateIndexRequest settings(Map<String, ?> source) {
try { this.settings = Settings.builder().loadFromMap(source).build();
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);
}
return this; return this;
} }

View File

@ -19,7 +19,6 @@
package org.elasticsearch.action.admin.indices.settings.put; package org.elasticsearch.action.admin.indices.settings.put;
import org.elasticsearch.ElasticsearchGenerationException;
import org.elasticsearch.action.ActionRequestValidationException; import org.elasticsearch.action.ActionRequestValidationException;
import org.elasticsearch.action.IndicesRequest; import org.elasticsearch.action.IndicesRequest;
import org.elasticsearch.action.support.IndicesOptions; 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.settings.Settings;
import org.elasticsearch.common.xcontent.ToXContentObject; import org.elasticsearch.common.xcontent.ToXContentObject;
import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.common.xcontent.XContentParser; import org.elasticsearch.common.xcontent.XContentParser;
import org.elasticsearch.common.xcontent.XContentType; import org.elasticsearch.common.xcontent.XContentType;
@ -169,13 +167,7 @@ public class UpdateSettingsRequest extends AcknowledgedRequest<UpdateSettingsReq
* Sets the settings to be updated (either json or yaml format) * Sets the settings to be updated (either json or yaml format)
*/ */
public UpdateSettingsRequest settings(Map<String, ?> source) { public UpdateSettingsRequest settings(Map<String, ?> source) {
try { this.settings = Settings.builder().loadFromMap(source).build();
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);
}
return this; return this;
} }

View File

@ -226,13 +226,7 @@ public class PutIndexTemplateRequest extends MasterNodeRequest<PutIndexTemplateR
* The settings to create the index template with (either json or yaml format). * The settings to create the index template with (either json or yaml format).
*/ */
public PutIndexTemplateRequest settings(Map<String, Object> source) { public PutIndexTemplateRequest settings(Map<String, Object> source) {
try { this.settings = Settings.builder().loadFromMap(source).build();
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);
}
return this; return this;
} }

View File

@ -21,6 +21,7 @@ package org.elasticsearch.common.settings;
import org.apache.logging.log4j.Level; import org.apache.logging.log4j.Level;
import org.apache.lucene.util.SetOnce; import org.apache.lucene.util.SetOnce;
import org.elasticsearch.ElasticsearchGenerationException;
import org.elasticsearch.ElasticsearchParseException; import org.elasticsearch.ElasticsearchParseException;
import org.elasticsearch.Version; import org.elasticsearch.Version;
import org.elasticsearch.common.Booleans; import org.elasticsearch.common.Booleans;
@ -1050,6 +1051,19 @@ public final class Settings implements ToXContentFragment {
} }
} }
/**
* Loads settings from a map.
*/
public Builder loadFromMap(Map<String, ?> 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)} * Loads settings from the actual string content that represents them using {@link #fromXContent(XContentParser)}
*/ */

View File

@ -19,17 +19,14 @@
package org.elasticsearch.index.analysis; package org.elasticsearch.index.analysis;
import org.elasticsearch.common.Strings;
import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.io.stream.StreamOutput;
import org.elasticsearch.common.io.stream.Writeable; import org.elasticsearch.common.io.stream.Writeable;
import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.xcontent.ToXContentFragment; import org.elasticsearch.common.xcontent.ToXContentFragment;
import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.common.xcontent.XContentParseException; import org.elasticsearch.common.xcontent.XContentParseException;
import org.elasticsearch.common.xcontent.XContentParser; import org.elasticsearch.common.xcontent.XContentParser;
import org.elasticsearch.common.xcontent.XContentType;
import java.io.IOException; import java.io.IOException;
import java.util.Map; import java.util.Map;
@ -49,10 +46,8 @@ public class NameOrDefinition implements Writeable, ToXContentFragment {
this.name = null; this.name = null;
Objects.requireNonNull(definition); Objects.requireNonNull(definition);
try { try {
XContentBuilder builder = XContentFactory.contentBuilder(XContentType.JSON); this.definition = Settings.builder().loadFromMap(definition).build();
builder.map(definition); } catch (Exception e) {
this.definition = Settings.builder().loadFromSource(Strings.toString(builder), builder.contentType()).build();
} catch (IOException e) {
throw new IllegalArgumentException("Failed to parse [" + definition + "]", e); throw new IllegalArgumentException("Failed to parse [" + definition + "]", e);
} }
} }