Skip to content

Commit 1fd70bf

Browse files
Introduce Constant for Common Settings toXContent Params (#84217)
Random find when looking into cluster state -> xcontent serialization. We're using this specific param a lot so lets make it a constant to make the serialization loop over settings a little tighter.
1 parent b5a354e commit 1fd70bf

File tree

5 files changed

+13
-9
lines changed

5 files changed

+13
-9
lines changed

server/src/main/java/org/elasticsearch/cluster/metadata/IndexMetadata.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2234,7 +2234,7 @@ public static void toXContent(IndexMetadata indexMetadata, XContentBuilder build
22342234

22352235
builder.startObject(KEY_SETTINGS);
22362236
if (context != Metadata.XContentContext.API) {
2237-
indexMetadata.getSettings().toXContent(builder, new MapParams(Collections.singletonMap("flat_settings", "true")));
2237+
indexMetadata.getSettings().toXContent(builder, Settings.FLAT_SETTINGS_TRUE);
22382238
} else {
22392239
indexMetadata.getSettings().toXContent(builder, params);
22402240
}

server/src/main/java/org/elasticsearch/common/settings/Settings.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,10 @@ public final class Settings implements ToXContentFragment, Writeable, Diffable<S
7878

7979
public static final Settings EMPTY = new Settings(Map.of(), null);
8080

81+
public static final String FLAT_SETTINGS_PARAM = "flat_settings";
82+
83+
public static final MapParams FLAT_SETTINGS_TRUE = new MapParams(Map.of(FLAT_SETTINGS_PARAM, "true"));
84+
8185
/** The raw settings from the full key to raw string value. */
8286
private final NavigableMap<String, Object> settings;
8387

@@ -661,7 +665,7 @@ public static Builder builder() {
661665
@Override
662666
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
663667
Settings settings = SettingsFilter.filterSettings(params, this);
664-
if (params.paramAsBoolean("flat_settings", false) == false) {
668+
if (params.paramAsBoolean(FLAT_SETTINGS_PARAM, false) == false) {
665669
toXContentFlat(builder, settings);
666670
} else {
667671
toXContent(builder, settings);
@@ -806,7 +810,7 @@ private static void validateValue(String key, Object currentValue, XContentParse
806810
}
807811
}
808812

809-
public static final Set<String> FORMAT_PARAMS = Set.of("settings_filter", "flat_settings");
813+
public static final Set<String> FORMAT_PARAMS = Set.of("settings_filter", FLAT_SETTINGS_PARAM);
810814

811815
/**
812816
* Returns {@code true} if this settings object contains no settings
@@ -1530,7 +1534,7 @@ public void writeTo(StreamOutput out) throws IOException {
15301534

15311535
@Override
15321536
public String toString() {
1533-
return Strings.toString(this, new MapParams(Collections.singletonMap("flat_settings", "true")));
1537+
return Strings.toString(this, FLAT_SETTINGS_TRUE);
15341538
}
15351539

15361540
private static String toString(Object o) {

server/src/main/java/org/elasticsearch/common/settings/SettingsModule.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
import org.elasticsearch.common.Strings;
1414
import org.elasticsearch.common.inject.Binder;
1515
import org.elasticsearch.common.inject.Module;
16-
import org.elasticsearch.xcontent.ToXContent;
1716
import org.elasticsearch.xcontent.XContentBuilder;
1817
import org.elasticsearch.xcontent.XContentType;
1918

@@ -132,7 +131,7 @@ public SettingsModule(
132131
try (XContentBuilder xContentBuilder = XContentBuilder.builder(XContentType.JSON.xContent())) {
133132
xContentBuilder.prettyPrint();
134133
xContentBuilder.startObject();
135-
indexSettings.toXContent(xContentBuilder, new ToXContent.MapParams(Collections.singletonMap("flat_settings", "true")));
134+
indexSettings.toXContent(xContentBuilder, Settings.FLAT_SETTINGS_TRUE);
136135
xContentBuilder.endObject();
137136
builder.append(Strings.toString(xContentBuilder));
138137
}

server/src/main/java/org/elasticsearch/rest/action/admin/indices/RestGetSettingsAction.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import org.elasticsearch.action.support.IndicesOptions;
1313
import org.elasticsearch.client.internal.node.NodeClient;
1414
import org.elasticsearch.common.Strings;
15+
import org.elasticsearch.common.settings.Settings;
1516
import org.elasticsearch.rest.BaseRestHandler;
1617
import org.elasticsearch.rest.RestRequest;
1718
import org.elasticsearch.rest.action.RestChunkedToXContentListener;
@@ -44,7 +45,7 @@ public RestChannelConsumer prepareRequest(final RestRequest request, final NodeC
4445
final String[] names = request.paramAsStringArrayOrEmptyIfAll("name");
4546
final boolean renderDefaults = request.paramAsBoolean("include_defaults", false);
4647
// This is required so the "flat_settings" parameter counts as consumed
47-
request.paramAsBoolean("flat_settings", false);
48+
request.paramAsBoolean(Settings.FLAT_SETTINGS_PARAM, false);
4849
GetSettingsRequest getSettingsRequest = new GetSettingsRequest().indices(Strings.splitStringByCommaToArray(request.param("index")))
4950
.indicesOptions(IndicesOptions.fromRequest(request, IndicesOptions.strictExpandOpen()))
5051
.humanReadable(request.hasParam("human"))

server/src/test/java/org/elasticsearch/common/settings/SettingsTests.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -543,7 +543,7 @@ public void testToAndFromXContent() throws IOException {
543543
final boolean flatSettings = randomBoolean();
544544
XContentBuilder builder = XContentBuilder.builder(XContentType.JSON.xContent());
545545
builder.startObject();
546-
settings.toXContent(builder, new ToXContent.MapParams(Collections.singletonMap("flat_settings", "" + flatSettings)));
546+
settings.toXContent(builder, new ToXContent.MapParams(Collections.singletonMap(Settings.FLAT_SETTINGS_PARAM, "" + flatSettings)));
547547
builder.endObject();
548548
XContentParser parser = createParser(builder);
549549
Settings build = Settings.fromXContent(parser);
@@ -591,7 +591,7 @@ public void testToXContent() throws IOException {
591591

592592
builder = XContentBuilder.builder(XContentType.JSON.xContent());
593593
builder.startObject();
594-
test.toXContent(builder, new ToXContent.MapParams(Collections.singletonMap("flat_settings", "true")));
594+
test.toXContent(builder, Settings.FLAT_SETTINGS_TRUE);
595595
builder.endObject();
596596
assertEquals("""
597597
{"foo.bar":["1","2","3"]}""", Strings.toString(builder));

0 commit comments

Comments
 (0)