Skip to content

Commit 28710c9

Browse files
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 <[email protected]>
1 parent f8158bd commit 28710c9

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
@@ -26,7 +26,6 @@
2626
import org.elasticsearch.action.support.IndicesOptions;
2727
import org.elasticsearch.action.support.master.MasterNodeRequest;
2828
import org.elasticsearch.common.Nullable;
29-
import org.elasticsearch.common.Strings;
3029
import org.elasticsearch.common.bytes.BytesArray;
3130
import org.elasticsearch.common.bytes.BytesReference;
3231
import org.elasticsearch.common.settings.Settings;
@@ -188,13 +187,7 @@ public PutIndexTemplateRequest settings(String source, XContentType xContentType
188187
* The settings to create the index template with (either json or yaml format).
189188
*/
190189
public PutIndexTemplateRequest settings(Map<String, Object> source) {
191-
try {
192-
XContentBuilder builder = XContentFactory.contentBuilder(XContentType.JSON);
193-
builder.map(source);
194-
settings(Strings.toString(builder), XContentType.JSON);
195-
} catch (IOException e) {
196-
throw new ElasticsearchGenerationException("Failed to generate [" + source + "]", e);
197-
}
190+
this.settings = Settings.builder().loadFromMap(source).build();
198191
return this;
199192
}
200193

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.Version;
2423
import org.elasticsearch.action.ActionRequestValidationException;
2524
import org.elasticsearch.action.support.IndicesOptions;
@@ -32,7 +31,6 @@
3231
import org.elasticsearch.common.settings.Settings;
3332
import org.elasticsearch.common.xcontent.ToXContentObject;
3433
import org.elasticsearch.common.xcontent.XContentBuilder;
35-
import org.elasticsearch.common.xcontent.XContentFactory;
3634
import org.elasticsearch.common.xcontent.XContentType;
3735

3836
import java.io.IOException;
@@ -434,13 +432,7 @@ public RestoreSnapshotRequest indexSettings(String source, XContentType xContent
434432
* Sets settings that should be added/changed in all restored indices
435433
*/
436434
public RestoreSnapshotRequest indexSettings(Map<String, Object> source) {
437-
try {
438-
XContentBuilder builder = XContentFactory.contentBuilder(XContentType.JSON);
439-
builder.map(source);
440-
indexSettings(Strings.toString(builder), builder.contentType());
441-
} catch (IOException e) {
442-
throw new ElasticsearchGenerationException("Failed to generate [" + source + "]", e);
443-
}
435+
this.indexSettings = Settings.builder().loadFromMap(source).build();
444436
return this;
445437
}
446438

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
@@ -219,13 +219,7 @@ public CreateIndexRequest settings(XContentBuilder builder) {
219219
* The settings to create the index with (either json/yaml/properties format)
220220
*/
221221
public CreateIndexRequest settings(Map<String, ?> source) {
222-
try {
223-
XContentBuilder builder = XContentFactory.contentBuilder(XContentType.JSON);
224-
builder.map(source);
225-
settings(Strings.toString(builder), XContentType.JSON);
226-
} catch (IOException e) {
227-
throw new ElasticsearchGenerationException("Failed to generate [" + source + "]", e);
228-
}
222+
this.settings = Settings.builder().loadFromMap(source).build();
229223
return this;
230224
}
231225

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
@@ -226,13 +226,7 @@ public PutIndexTemplateRequest settings(String source, XContentType xContentType
226226
* The settings to create the index template with (either json or yaml format).
227227
*/
228228
public PutIndexTemplateRequest settings(Map<String, Object> source) {
229-
try {
230-
XContentBuilder builder = XContentFactory.contentBuilder(XContentType.JSON);
231-
builder.map(source);
232-
settings(Strings.toString(builder), XContentType.JSON);
233-
} catch (IOException e) {
234-
throw new ElasticsearchGenerationException("Failed to generate [" + source + "]", e);
235-
}
229+
this.settings = Settings.builder().loadFromMap(source).build();
236230
return this;
237231
}
238232

0 commit comments

Comments
 (0)