Skip to content

Commit

Permalink
Dry up Settings from Map Construction (#61778)
Browse files Browse the repository at this point in the history
We used the same hack all over the place. At least drying it up to a single place.

Co-authored-by: Jay Modi <[email protected]>
  • Loading branch information
original-brownbear and jaymode authored Sep 1, 2020
1 parent f3c8792 commit ba87bad
Show file tree
Hide file tree
Showing 12 changed files with 29 additions and 98 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@
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;
Expand Down Expand Up @@ -207,13 +205,7 @@ static class NameOrDefinition implements ToXContentFragment {
NameOrDefinition(Map<String, ?> 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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,13 +127,7 @@ public CreateIndexRequest settings(XContentBuilder builder) {
* The settings to create the index with (either json/yaml/properties format)
*/
public CreateIndexRequest settings(Map<String, ?> 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;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import org.elasticsearch.ElasticsearchParseException;
import org.elasticsearch.action.admin.indices.alias.Alias;
import org.elasticsearch.client.TimedRequest;
import org.elasticsearch.common.Strings;
import org.elasticsearch.common.bytes.BytesArray;
import org.elasticsearch.common.bytes.BytesReference;
import org.elasticsearch.common.settings.Settings;
Expand Down Expand Up @@ -170,13 +169,7 @@ public PutIndexTemplateRequest settings(String source, XContentType xContentType
* The settings to create the index template with (either json or yaml format).
*/
public PutIndexTemplateRequest settings(Map<String, Object> 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;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -160,10 +157,8 @@ public static final class NameOrDefinition 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);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -168,13 +165,7 @@ public PutRepositoryRequest settings(String source, XContentType xContentType) {
* @return this request
*/
public PutRepositoryRequest settings(Map<String, Object> 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;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -115,13 +112,7 @@ public ClusterUpdateSettingsRequest transientSettings(String source, XContentTyp
* Sets the transient settings to be updated. They will not survive a full cluster restart
*/
public ClusterUpdateSettingsRequest transientSettings(Map<String, ?> 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;
}

Expand Down Expand Up @@ -153,13 +144,7 @@ public ClusterUpdateSettingsRequest persistentSettings(String source, XContentTy
* Sets the persistent settings to be updated. They will get applied cross restarts
*/
public ClusterUpdateSettingsRequest persistentSettings(Map<String, ?> 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;
}

Expand All @@ -183,7 +168,7 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws
return builder;
}

public static ClusterUpdateSettingsRequest fromXContent(XContentParser parser) throws IOException {
public static ClusterUpdateSettingsRequest fromXContent(XContentParser parser) {
return PARSER.apply(parser, null);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@

package org.elasticsearch.action.admin.cluster.snapshots.restore;

import org.elasticsearch.ElasticsearchGenerationException;
import org.elasticsearch.action.ActionRequestValidationException;
import org.elasticsearch.action.support.IndicesOptions;
import org.elasticsearch.action.support.master.MasterNodeRequest;
Expand All @@ -30,7 +29,6 @@
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;
Expand Down Expand Up @@ -417,13 +415,7 @@ public RestoreSnapshotRequest indexSettings(String source, XContentType xContent
* Sets settings that should be added/changed in all restored indices
*/
public RestoreSnapshotRequest indexSettings(Map<String, Object> 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;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -208,13 +208,7 @@ public CreateIndexRequest settings(XContentBuilder builder) {
* The settings to create the index with (either json/yaml/properties format)
*/
public CreateIndexRequest settings(Map<String, ?> 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;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -30,7 +29,6 @@
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;

Expand Down Expand Up @@ -169,13 +167,7 @@ public UpdateSettingsRequest setPreserveExisting(boolean preserveExisting) {
* Sets the settings to be updated (either json or yaml format)
*/
public UpdateSettingsRequest settings(Map<String, ?> 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;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -211,13 +211,7 @@ public PutIndexTemplateRequest settings(String source, XContentType xContentType
* The settings to create the index template with (either json or yaml format).
*/
public PutIndexTemplateRequest settings(Map<String, Object> 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;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

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;
Expand Down Expand Up @@ -1019,6 +1020,19 @@ private void processLegacyLists(Map<String, Object> map) {
}
}

/**
* 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)}
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -49,10 +46,8 @@ public NameOrDefinition(Map<String, ?> 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) {
this.definition = Settings.builder().loadFromMap(definition).build();
} catch (Exception e) {
throw new IllegalArgumentException("Failed to parse [" + definition + "]", e);
}
}
Expand Down

0 comments on commit ba87bad

Please sign in to comment.