Skip to content

Commit ba87bad

Browse files
Dry up Settings from Map Construction (#61778)
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]>
1 parent f3c8792 commit ba87bad

File tree

12 files changed

+29
-98
lines changed

12 files changed

+29
-98
lines changed

client/rest-high-level/src/main/java/org/elasticsearch/client/indices/AnalyzeRequest.java

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,6 @@
2525
import org.elasticsearch.common.xcontent.ToXContentFragment;
2626
import org.elasticsearch.common.xcontent.ToXContentObject;
2727
import org.elasticsearch.common.xcontent.XContentBuilder;
28-
import org.elasticsearch.common.xcontent.XContentFactory;
29-
import org.elasticsearch.common.xcontent.XContentType;
3028

3129
import java.io.IOException;
3230
import java.util.ArrayList;
@@ -207,13 +205,7 @@ static class NameOrDefinition implements ToXContentFragment {
207205
NameOrDefinition(Map<String, ?> definition) {
208206
this.name = null;
209207
Objects.requireNonNull(definition);
210-
try {
211-
XContentBuilder builder = XContentFactory.contentBuilder(XContentType.JSON);
212-
builder.map(definition);
213-
this.definition = Settings.builder().loadFromSource(Strings.toString(builder), builder.contentType()).build();
214-
} catch (IOException e) {
215-
throw new IllegalArgumentException("Failed to parse [" + definition + "]", e);
216-
}
208+
this.definition = Settings.builder().loadFromMap(definition).build();
217209
}
218210

219211
@Override

client/rest-high-level/src/main/java/org/elasticsearch/client/indices/CreateIndexRequest.java

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -127,13 +127,7 @@ public CreateIndexRequest settings(XContentBuilder builder) {
127127
* The settings to create the index with (either json/yaml/properties format)
128128
*/
129129
public CreateIndexRequest settings(Map<String, ?> source) {
130-
try {
131-
XContentBuilder builder = XContentFactory.contentBuilder(XContentType.JSON);
132-
builder.map(source);
133-
settings(Strings.toString(builder), XContentType.JSON);
134-
} catch (IOException e) {
135-
throw new ElasticsearchGenerationException("Failed to generate [" + source + "]", e);
136-
}
130+
this.settings = Settings.builder().loadFromMap(source).build();
137131
return this;
138132
}
139133

client/rest-high-level/src/main/java/org/elasticsearch/client/indices/PutIndexTemplateRequest.java

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
import org.elasticsearch.ElasticsearchParseException;
2323
import org.elasticsearch.action.admin.indices.alias.Alias;
2424
import org.elasticsearch.client.TimedRequest;
25-
import org.elasticsearch.common.Strings;
2625
import org.elasticsearch.common.bytes.BytesArray;
2726
import org.elasticsearch.common.bytes.BytesReference;
2827
import org.elasticsearch.common.settings.Settings;
@@ -170,13 +169,7 @@ public PutIndexTemplateRequest settings(String source, XContentType xContentType
170169
* The settings to create the index template with (either json or yaml format).
171170
*/
172171
public PutIndexTemplateRequest settings(Map<String, Object> source) {
173-
try {
174-
XContentBuilder builder = XContentFactory.contentBuilder(XContentType.JSON);
175-
builder.map(source);
176-
settings(Strings.toString(builder), XContentType.JSON);
177-
} catch (IOException e) {
178-
throw new ElasticsearchGenerationException("Failed to generate [" + source + "]", e);
179-
}
172+
this.settings = Settings.builder().loadFromMap(source).build();
180173
return this;
181174
}
182175

client/rest-high-level/src/main/java/org/elasticsearch/client/ml/job/config/CategorizationAnalyzerConfig.java

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,10 @@
1919
package org.elasticsearch.client.ml.job.config;
2020

2121
import org.elasticsearch.common.ParseField;
22-
import org.elasticsearch.common.Strings;
2322
import org.elasticsearch.common.settings.Settings;
2423
import org.elasticsearch.common.xcontent.ToXContentFragment;
2524
import org.elasticsearch.common.xcontent.XContentBuilder;
26-
import org.elasticsearch.common.xcontent.XContentFactory;
2725
import org.elasticsearch.common.xcontent.XContentParser;
28-
import org.elasticsearch.common.xcontent.XContentType;
2926
import org.elasticsearch.rest.action.admin.indices.RestAnalyzeAction;
3027

3128
import java.io.IOException;
@@ -160,10 +157,8 @@ public static final class NameOrDefinition implements ToXContentFragment {
160157
this.name = null;
161158
Objects.requireNonNull(definition);
162159
try {
163-
XContentBuilder builder = XContentFactory.contentBuilder(XContentType.JSON);
164-
builder.map(definition);
165-
this.definition = Settings.builder().loadFromSource(Strings.toString(builder), builder.contentType()).build();
166-
} catch (IOException e) {
160+
this.definition = Settings.builder().loadFromMap(definition).build();
161+
} catch (Exception e) {
167162
throw new IllegalArgumentException("Failed to parse [" + definition + "] in [" + field.getPreferredName() + "]", e);
168163
}
169164
}

server/src/main/java/org/elasticsearch/action/admin/cluster/repositories/put/PutRepositoryRequest.java

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,13 @@
1919

2020
package org.elasticsearch.action.admin.cluster.repositories.put;
2121

22-
import org.elasticsearch.ElasticsearchGenerationException;
2322
import org.elasticsearch.action.ActionRequestValidationException;
2423
import org.elasticsearch.action.support.master.AcknowledgedRequest;
25-
import org.elasticsearch.common.Strings;
2624
import org.elasticsearch.common.io.stream.StreamInput;
2725
import org.elasticsearch.common.io.stream.StreamOutput;
2826
import org.elasticsearch.common.settings.Settings;
2927
import org.elasticsearch.common.xcontent.ToXContentObject;
3028
import org.elasticsearch.common.xcontent.XContentBuilder;
31-
import org.elasticsearch.common.xcontent.XContentFactory;
3229
import org.elasticsearch.common.xcontent.XContentType;
3330

3431
import java.io.IOException;
@@ -168,13 +165,7 @@ public PutRepositoryRequest settings(String source, XContentType xContentType) {
168165
* @return this request
169166
*/
170167
public PutRepositoryRequest settings(Map<String, Object> source) {
171-
try {
172-
XContentBuilder builder = XContentFactory.contentBuilder(XContentType.JSON);
173-
builder.map(source);
174-
settings(Strings.toString(builder), builder.contentType());
175-
} catch (IOException e) {
176-
throw new ElasticsearchGenerationException("Failed to generate [" + source + "]", e);
177-
}
168+
this.settings = Settings.builder().loadFromMap(source).build();
178169
return this;
179170
}
180171

server/src/main/java/org/elasticsearch/action/admin/cluster/settings/ClusterUpdateSettingsRequest.java

Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -19,18 +19,15 @@
1919

2020
package org.elasticsearch.action.admin.cluster.settings;
2121

22-
import org.elasticsearch.ElasticsearchGenerationException;
2322
import org.elasticsearch.action.ActionRequestValidationException;
2423
import org.elasticsearch.action.support.master.AcknowledgedRequest;
2524
import org.elasticsearch.common.ParseField;
26-
import org.elasticsearch.common.Strings;
2725
import org.elasticsearch.common.io.stream.StreamInput;
2826
import org.elasticsearch.common.io.stream.StreamOutput;
2927
import org.elasticsearch.common.settings.Settings;
3028
import org.elasticsearch.common.xcontent.ObjectParser;
3129
import org.elasticsearch.common.xcontent.ToXContentObject;
3230
import org.elasticsearch.common.xcontent.XContentBuilder;
33-
import org.elasticsearch.common.xcontent.XContentFactory;
3431
import org.elasticsearch.common.xcontent.XContentParser;
3532
import org.elasticsearch.common.xcontent.XContentType;
3633

@@ -115,13 +112,7 @@ public ClusterUpdateSettingsRequest transientSettings(String source, XContentTyp
115112
* Sets the transient settings to be updated. They will not survive a full cluster restart
116113
*/
117114
public ClusterUpdateSettingsRequest transientSettings(Map<String, ?> source) {
118-
try {
119-
XContentBuilder builder = XContentFactory.contentBuilder(XContentType.JSON);
120-
builder.map(source);
121-
transientSettings(Strings.toString(builder), builder.contentType());
122-
} catch (IOException e) {
123-
throw new ElasticsearchGenerationException("Failed to generate [" + source + "]", e);
124-
}
115+
this.transientSettings = Settings.builder().loadFromMap(source).build();
125116
return this;
126117
}
127118

@@ -153,13 +144,7 @@ public ClusterUpdateSettingsRequest persistentSettings(String source, XContentTy
153144
* Sets the persistent settings to be updated. They will get applied cross restarts
154145
*/
155146
public ClusterUpdateSettingsRequest persistentSettings(Map<String, ?> source) {
156-
try {
157-
XContentBuilder builder = XContentFactory.contentBuilder(XContentType.JSON);
158-
builder.map(source);
159-
persistentSettings(Strings.toString(builder), builder.contentType());
160-
} catch (IOException e) {
161-
throw new ElasticsearchGenerationException("Failed to generate [" + source + "]", e);
162-
}
147+
this.persistentSettings = Settings.builder().loadFromMap(source).build();
163148
return this;
164149
}
165150

@@ -183,7 +168,7 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws
183168
return builder;
184169
}
185170

186-
public static ClusterUpdateSettingsRequest fromXContent(XContentParser parser) throws IOException {
171+
public static ClusterUpdateSettingsRequest fromXContent(XContentParser parser) {
187172
return PARSER.apply(parser, null);
188173
}
189174
}

server/src/main/java/org/elasticsearch/action/admin/cluster/snapshots/restore/RestoreSnapshotRequest.java

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919

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

22-
import org.elasticsearch.ElasticsearchGenerationException;
2322
import org.elasticsearch.action.ActionRequestValidationException;
2423
import org.elasticsearch.action.support.IndicesOptions;
2524
import org.elasticsearch.action.support.master.MasterNodeRequest;
@@ -30,7 +29,6 @@
3029
import org.elasticsearch.common.settings.Settings;
3130
import org.elasticsearch.common.xcontent.ToXContentObject;
3231
import org.elasticsearch.common.xcontent.XContentBuilder;
33-
import org.elasticsearch.common.xcontent.XContentFactory;
3432
import org.elasticsearch.common.xcontent.XContentType;
3533

3634
import java.io.IOException;
@@ -417,13 +415,7 @@ public RestoreSnapshotRequest indexSettings(String source, XContentType xContent
417415
* Sets settings that should be added/changed in all restored indices
418416
*/
419417
public RestoreSnapshotRequest indexSettings(Map<String, Object> source) {
420-
try {
421-
XContentBuilder builder = XContentFactory.contentBuilder(XContentType.JSON);
422-
builder.map(source);
423-
indexSettings(Strings.toString(builder), builder.contentType());
424-
} catch (IOException e) {
425-
throw new ElasticsearchGenerationException("Failed to generate [" + source + "]", e);
426-
}
418+
this.indexSettings = Settings.builder().loadFromMap(source).build();
427419
return this;
428420
}
429421

server/src/main/java/org/elasticsearch/action/admin/indices/create/CreateIndexRequest.java

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -208,13 +208,7 @@ public CreateIndexRequest settings(XContentBuilder builder) {
208208
* The settings to create the index with (either json/yaml/properties format)
209209
*/
210210
public CreateIndexRequest settings(Map<String, ?> source) {
211-
try {
212-
XContentBuilder builder = XContentFactory.contentBuilder(XContentType.JSON);
213-
builder.map(source);
214-
settings(Strings.toString(builder), XContentType.JSON);
215-
} catch (IOException e) {
216-
throw new ElasticsearchGenerationException("Failed to generate [" + source + "]", e);
217-
}
211+
this.settings = Settings.builder().loadFromMap(source).build();
218212
return this;
219213
}
220214

server/src/main/java/org/elasticsearch/action/admin/indices/settings/put/UpdateSettingsRequest.java

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919

2020
package org.elasticsearch.action.admin.indices.settings.put;
2121

22-
import org.elasticsearch.ElasticsearchGenerationException;
2322
import org.elasticsearch.action.ActionRequestValidationException;
2423
import org.elasticsearch.action.IndicesRequest;
2524
import org.elasticsearch.action.support.IndicesOptions;
@@ -30,7 +29,6 @@
3029
import org.elasticsearch.common.settings.Settings;
3130
import org.elasticsearch.common.xcontent.ToXContentObject;
3231
import org.elasticsearch.common.xcontent.XContentBuilder;
33-
import org.elasticsearch.common.xcontent.XContentFactory;
3432
import org.elasticsearch.common.xcontent.XContentParser;
3533
import org.elasticsearch.common.xcontent.XContentType;
3634

@@ -169,13 +167,7 @@ public UpdateSettingsRequest setPreserveExisting(boolean preserveExisting) {
169167
* Sets the settings to be updated (either json or yaml format)
170168
*/
171169
public UpdateSettingsRequest settings(Map<String, ?> source) {
172-
try {
173-
XContentBuilder builder = XContentFactory.contentBuilder(XContentType.JSON);
174-
builder.map(source);
175-
settings(Strings.toString(builder), builder.contentType());
176-
} catch (IOException e) {
177-
throw new ElasticsearchGenerationException("Failed to generate [" + source + "]", e);
178-
}
170+
this.settings = Settings.builder().loadFromMap(source).build();
179171
return this;
180172
}
181173

server/src/main/java/org/elasticsearch/action/admin/indices/template/put/PutIndexTemplateRequest.java

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -211,13 +211,7 @@ public PutIndexTemplateRequest settings(String source, XContentType xContentType
211211
* The settings to create the index template with (either json or yaml format).
212212
*/
213213
public PutIndexTemplateRequest settings(Map<String, Object> source) {
214-
try {
215-
XContentBuilder builder = XContentFactory.contentBuilder(XContentType.JSON);
216-
builder.map(source);
217-
settings(Strings.toString(builder), XContentType.JSON);
218-
} catch (IOException e) {
219-
throw new ElasticsearchGenerationException("Failed to generate [" + source + "]", e);
220-
}
214+
this.settings = Settings.builder().loadFromMap(source).build();
221215
return this;
222216
}
223217

0 commit comments

Comments
 (0)