Skip to content

Commit 11c2eb2

Browse files
authored
Convert a few more implementations to ChunkedXContentBuilder (#113125)
Remove the complex methods from ChunkedXContentHelper
1 parent 7dfd3f5 commit 11c2eb2

File tree

29 files changed

+240
-259
lines changed

29 files changed

+240
-259
lines changed

modules/ingest-geoip/src/main/java/org/elasticsearch/ingest/geoip/IngestGeoIpMetadata.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,9 @@
1515
import org.elasticsearch.cluster.DiffableUtils;
1616
import org.elasticsearch.cluster.NamedDiff;
1717
import org.elasticsearch.cluster.metadata.Metadata;
18-
import org.elasticsearch.common.collect.Iterators;
1918
import org.elasticsearch.common.io.stream.StreamInput;
2019
import org.elasticsearch.common.io.stream.StreamOutput;
21-
import org.elasticsearch.common.xcontent.ChunkedToXContentHelper;
20+
import org.elasticsearch.common.xcontent.ChunkedToXContent;
2221
import org.elasticsearch.ingest.geoip.direct.DatabaseConfigurationMetadata;
2322
import org.elasticsearch.xcontent.ConstructingObjectParser;
2423
import org.elasticsearch.xcontent.ParseField;
@@ -91,8 +90,8 @@ public static IngestGeoIpMetadata fromXContent(XContentParser parser) throws IOE
9190
}
9291

9392
@Override
94-
public Iterator<? extends ToXContent> toXContentChunked(ToXContent.Params ignored) {
95-
return Iterators.concat(ChunkedToXContentHelper.xContentValuesMap(DATABASES_FIELD.getPreferredName(), databases));
93+
public Iterator<? extends ToXContent> toXContentChunked(ToXContent.Params params) {
94+
return ChunkedToXContent.builder(params).xContentObjectFields(DATABASES_FIELD.getPreferredName(), databases);
9695
}
9796

9897
@Override

modules/lang-mustache/src/main/java/org/elasticsearch/script/mustache/SearchTemplateResponse.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws
107107

108108
void innerToXContent(XContentBuilder builder, Params params) throws IOException {
109109
if (hasResponse()) {
110-
ChunkedToXContent.wrapAsToXContent(p -> response.innerToXContentChunked(p)).toXContent(builder, params);
110+
ChunkedToXContent.wrapAsToXContent(response::innerToXContentChunked).toXContent(builder, params);
111111
} else {
112112
// we can assume the template is always json as we convert it before compiling it
113113
try (InputStream stream = source.streamInput()) {

server/src/main/java/org/elasticsearch/action/bulk/BulkResponse.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import org.elasticsearch.common.collect.Iterators;
1515
import org.elasticsearch.common.io.stream.StreamInput;
1616
import org.elasticsearch.common.io.stream.StreamOutput;
17+
import org.elasticsearch.common.xcontent.ChunkedToXContent;
1718
import org.elasticsearch.common.xcontent.ChunkedToXContentObject;
1819
import org.elasticsearch.core.TimeValue;
1920
import org.elasticsearch.xcontent.ToXContent;
@@ -157,14 +158,13 @@ public void writeTo(StreamOutput out) throws IOException {
157158

158159
@Override
159160
public Iterator<? extends ToXContent> toXContentChunked(ToXContent.Params params) {
160-
return Iterators.concat(Iterators.single((builder, p) -> {
161-
builder.startObject();
162-
builder.field(ERRORS, hasFailures());
163-
builder.field(TOOK, tookInMillis);
161+
return ChunkedToXContent.builder(params).object(ob -> {
162+
ob.field(ERRORS, hasFailures());
163+
ob.field(TOOK, tookInMillis);
164164
if (ingestTookInMillis != BulkResponse.NO_INGEST_TOOK) {
165-
builder.field(INGEST_TOOK, ingestTookInMillis);
165+
ob.field(INGEST_TOOK, ingestTookInMillis);
166166
}
167-
return builder.startArray(ITEMS);
168-
}), Iterators.forArray(responses), Iterators.<ToXContent>single((builder, p) -> builder.endArray().endObject()));
167+
ob.array(ITEMS, Iterators.forArray(responses));
168+
});
169169
}
170170
}

server/src/main/java/org/elasticsearch/action/search/SearchResponse.java

Lines changed: 9 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,12 @@
1414
import org.elasticsearch.action.OriginalIndices;
1515
import org.elasticsearch.common.Strings;
1616
import org.elasticsearch.common.bytes.BytesReference;
17-
import org.elasticsearch.common.collect.Iterators;
1817
import org.elasticsearch.common.io.stream.StreamInput;
1918
import org.elasticsearch.common.io.stream.StreamOutput;
2019
import org.elasticsearch.common.io.stream.Writeable;
2120
import org.elasticsearch.common.lucene.Lucene;
2221
import org.elasticsearch.common.util.concurrent.ConcurrentCollections;
23-
import org.elasticsearch.common.xcontent.ChunkedToXContentHelper;
22+
import org.elasticsearch.common.xcontent.ChunkedToXContent;
2423
import org.elasticsearch.common.xcontent.ChunkedToXContentObject;
2524
import org.elasticsearch.core.Nullable;
2625
import org.elasticsearch.core.RefCounted;
@@ -383,39 +382,17 @@ public Clusters getClusters() {
383382
@Override
384383
public Iterator<? extends ToXContent> toXContentChunked(ToXContent.Params params) {
385384
assert hasReferences();
386-
return Iterators.concat(
387-
ChunkedToXContentHelper.startObject(),
388-
this.innerToXContentChunked(params),
389-
ChunkedToXContentHelper.endObject()
390-
);
385+
return ChunkedToXContent.builder(params).xContentObject(innerToXContentChunked(params));
391386
}
392387

393388
public Iterator<? extends ToXContent> innerToXContentChunked(ToXContent.Params params) {
394-
return Iterators.concat(
395-
ChunkedToXContentHelper.singleChunk(SearchResponse.this::headerToXContent),
396-
Iterators.single(clusters),
397-
Iterators.concat(
398-
Iterators.flatMap(Iterators.single(hits), r -> r.toXContentChunked(params)),
399-
Iterators.single((ToXContent) (b, p) -> {
400-
if (aggregations != null) {
401-
aggregations.toXContent(b, p);
402-
}
403-
return b;
404-
}),
405-
Iterators.single((b, p) -> {
406-
if (suggest != null) {
407-
suggest.toXContent(b, p);
408-
}
409-
return b;
410-
}),
411-
Iterators.single((b, p) -> {
412-
if (profileResults != null) {
413-
profileResults.toXContent(b, p);
414-
}
415-
return b;
416-
})
417-
)
418-
);
389+
return ChunkedToXContent.builder(params)
390+
.append(SearchResponse.this::headerToXContent)
391+
.append(clusters)
392+
.append(hits)
393+
.appendIfPresent(aggregations)
394+
.appendIfPresent(suggest)
395+
.appendIfPresent(profileResults);
419396
}
420397

421398
public XContentBuilder headerToXContent(XContentBuilder builder, ToXContent.Params params) throws IOException {

server/src/main/java/org/elasticsearch/action/support/broadcast/ChunkedBroadcastResponse.java

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99
package org.elasticsearch.action.support.broadcast;
1010

1111
import org.elasticsearch.action.support.DefaultShardOperationFailedException;
12-
import org.elasticsearch.common.collect.Iterators;
1312
import org.elasticsearch.common.io.stream.StreamInput;
13+
import org.elasticsearch.common.xcontent.ChunkedToXContent;
1414
import org.elasticsearch.common.xcontent.ChunkedToXContentObject;
1515
import org.elasticsearch.rest.action.RestActions;
1616
import org.elasticsearch.xcontent.ToXContent;
@@ -35,11 +35,8 @@ public ChunkedBroadcastResponse(
3535

3636
@Override
3737
public final Iterator<ToXContent> toXContentChunked(ToXContent.Params params) {
38-
return Iterators.concat(Iterators.single((b, p) -> {
39-
b.startObject();
40-
RestActions.buildBroadcastShardsHeader(b, p, this);
41-
return b;
42-
}), customXContentChunks(params), Iterators.single((builder, p) -> builder.endObject()));
38+
return ChunkedToXContent.builder(params)
39+
.object(ob -> ob.append((b, p) -> RestActions.buildBroadcastShardsHeader(b, p, this)).append(this::customXContentChunks));
4340
}
4441

4542
protected abstract Iterator<ToXContent> customXContentChunks(ToXContent.Params params);

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
import org.elasticsearch.common.Strings;
1818
import org.elasticsearch.common.io.stream.StreamInput;
1919
import org.elasticsearch.common.io.stream.StreamOutput;
20-
import org.elasticsearch.common.xcontent.ChunkedToXContentHelper;
20+
import org.elasticsearch.common.xcontent.ChunkedToXContent;
2121
import org.elasticsearch.xcontent.ConstructingObjectParser;
2222
import org.elasticsearch.xcontent.ParseField;
2323
import org.elasticsearch.xcontent.ToXContent;
@@ -102,8 +102,8 @@ public static ComponentTemplateMetadata fromXContent(XContentParser parser) thro
102102
}
103103

104104
@Override
105-
public Iterator<? extends ToXContent> toXContentChunked(ToXContent.Params ignored) {
106-
return ChunkedToXContentHelper.xContentValuesMap(COMPONENT_TEMPLATE.getPreferredName(), componentTemplates);
105+
public Iterator<? extends ToXContent> toXContentChunked(ToXContent.Params params) {
106+
return ChunkedToXContent.builder(params).xContentObjectFields(COMPONENT_TEMPLATE.getPreferredName(), componentTemplates);
107107
}
108108

109109
@Override

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
import org.elasticsearch.common.Strings;
1818
import org.elasticsearch.common.io.stream.StreamInput;
1919
import org.elasticsearch.common.io.stream.StreamOutput;
20-
import org.elasticsearch.common.xcontent.ChunkedToXContentHelper;
20+
import org.elasticsearch.common.xcontent.ChunkedToXContent;
2121
import org.elasticsearch.xcontent.ConstructingObjectParser;
2222
import org.elasticsearch.xcontent.ParseField;
2323
import org.elasticsearch.xcontent.ToXContent;
@@ -103,8 +103,8 @@ public void writeTo(StreamOutput out) throws IOException {
103103
}
104104

105105
@Override
106-
public Iterator<? extends ToXContent> toXContentChunked(ToXContent.Params ignored) {
107-
return ChunkedToXContentHelper.xContentValuesMap(INDEX_TEMPLATE.getPreferredName(), indexTemplates);
106+
public Iterator<? extends ToXContent> toXContentChunked(ToXContent.Params params) {
107+
return ChunkedToXContent.builder(params).xContentObjectFields(INDEX_TEMPLATE.getPreferredName(), indexTemplates);
108108
}
109109

110110
@Override

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ public static DataStreamMetadata fromXContent(XContentParser parser) throws IOEx
233233
@Override
234234
public Iterator<? extends ToXContent> toXContentChunked(ToXContent.Params params) {
235235
return ChunkedToXContent.builder(params)
236-
.object(DATA_STREAM.getPreferredName(), b -> b.appendXContentFields(dataStreams))
236+
.xContentObjectFields(DATA_STREAM.getPreferredName(), dataStreams)
237237
.xContentObject(DATA_STREAM_ALIASES.getPreferredName(), dataStreamAliases.values().iterator());
238238
}
239239

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
import org.elasticsearch.cluster.SimpleDiffable;
1818
import org.elasticsearch.common.io.stream.StreamInput;
1919
import org.elasticsearch.common.io.stream.StreamOutput;
20-
import org.elasticsearch.common.xcontent.ChunkedToXContentHelper;
20+
import org.elasticsearch.common.xcontent.ChunkedToXContent;
2121
import org.elasticsearch.core.Nullable;
2222
import org.elasticsearch.xcontent.ConstructingObjectParser;
2323
import org.elasticsearch.xcontent.ParseField;
@@ -190,8 +190,8 @@ public int hashCode() {
190190
}
191191

192192
@Override
193-
public Iterator<? extends ToXContent> toXContentChunked(ToXContent.Params ignored) {
194-
return ChunkedToXContentHelper.xContentValuesMap(NODES_FIELD.getPreferredName(), nodes);
193+
public Iterator<? extends ToXContent> toXContentChunked(ToXContent.Params params) {
194+
return ChunkedToXContent.builder(params).xContentObjectFields(NODES_FIELD.getPreferredName(), nodes);
195195
}
196196

197197
/**

server/src/main/java/org/elasticsearch/common/xcontent/ChunkedToXContentBuilder.java

Lines changed: 96 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,46 @@ public ChunkedToXContentBuilder xContentObject(String name, Iterator<? extends T
110110
return this;
111111
}
112112

113+
/**
114+
* Creates an object with the contents of each field created from each entry in {@code map}
115+
*/
116+
public ChunkedToXContentBuilder xContentObjectFields(Map<String, ? extends ToXContent> map) {
117+
startObject();
118+
map.forEach(this::field);
119+
endObject();
120+
return this;
121+
}
122+
123+
/**
124+
* Creates an object named {@code name}, with the contents of each field created from each entry in {@code map}
125+
*/
126+
public ChunkedToXContentBuilder xContentObjectFields(String name, Map<String, ? extends ToXContent> map) {
127+
startObject(name);
128+
map.forEach(this::field);
129+
endObject();
130+
return this;
131+
}
132+
133+
/**
134+
* Creates an object with the contents of each field each another object created from each entry in {@code map}
135+
*/
136+
public ChunkedToXContentBuilder xContentObjectFieldObjects(Map<String, ? extends ToXContent> map) {
137+
startObject();
138+
map.forEach(this::xContentObject);
139+
endObject();
140+
return this;
141+
}
142+
143+
/**
144+
* Creates an object named {@code name}, with the contents of each field each another object created from each entry in {@code map}
145+
*/
146+
public ChunkedToXContentBuilder xContentObjectFieldObjects(String name, Map<String, ? extends ToXContent> map) {
147+
startObject(name);
148+
map.forEach(this::xContentObject);
149+
endObject();
150+
return this;
151+
}
152+
113153
/**
114154
* Creates an object, with the contents set by {@code contents}
115155
*/
@@ -172,6 +212,26 @@ public <T> ChunkedToXContentBuilder object(String name, Iterator<T> items, Funct
172212
return this;
173213
}
174214

215+
/**
216+
* Creates an object with the contents of each field set by {@code map}
217+
*/
218+
public ChunkedToXContentBuilder object(Map<String, ?> map) {
219+
startObject();
220+
map.forEach(this::field);
221+
endObject();
222+
return this;
223+
}
224+
225+
/**
226+
* Creates an object named {@code name}, with the contents of each field set by {@code map}
227+
*/
228+
public ChunkedToXContentBuilder object(String name, Map<String, ?> map) {
229+
startObject(name);
230+
map.forEach(this::field);
231+
endObject();
232+
return this;
233+
}
234+
175235
private void startArray() {
176236
addChunk((b, p) -> b.startArray());
177237
}
@@ -223,7 +283,7 @@ public <T> ChunkedToXContentBuilder array(String name, Iterator<T> items, BiCons
223283
/**
224284
* Creates an array named {@code name}, with the contents set by appending together the contents of {@code items}
225285
*/
226-
public <T> ChunkedToXContentBuilder array(String name, Iterator<? extends ToXContent> items) {
286+
public ChunkedToXContentBuilder array(String name, Iterator<? extends ToXContent> items) {
227287
startArray(name);
228288
items.forEachRemaining(this::append);
229289
endArray();
@@ -246,16 +306,51 @@ public ChunkedToXContentBuilder field(String name) {
246306
return this;
247307
}
248308

309+
public ChunkedToXContentBuilder field(String name, boolean value) {
310+
addChunk((b, p) -> b.field(name, value));
311+
return this;
312+
}
313+
314+
public ChunkedToXContentBuilder field(String name, Boolean value) {
315+
addChunk((b, p) -> b.field(name, value));
316+
return this;
317+
}
318+
319+
public ChunkedToXContentBuilder field(String name, int value) {
320+
addChunk((b, p) -> b.field(name, value));
321+
return this;
322+
}
323+
324+
public ChunkedToXContentBuilder field(String name, Integer value) {
325+
addChunk((b, p) -> b.field(name, value));
326+
return this;
327+
}
328+
249329
public ChunkedToXContentBuilder field(String name, long value) {
250330
addChunk((b, p) -> b.field(name, value));
251331
return this;
252332
}
253333

334+
public ChunkedToXContentBuilder field(String name, Long value) {
335+
addChunk((b, p) -> b.field(name, value));
336+
return this;
337+
}
338+
254339
public ChunkedToXContentBuilder field(String name, String value) {
255340
addChunk((b, p) -> b.field(name, value));
256341
return this;
257342
}
258343

344+
public ChunkedToXContentBuilder field(String name, Enum<?> value) {
345+
addChunk((b, p) -> b.field(name, value));
346+
return this;
347+
}
348+
349+
public ChunkedToXContentBuilder field(String name, ToXContent value) {
350+
addChunk((b, p) -> b.field(name, value, p));
351+
return this;
352+
}
353+
259354
public ChunkedToXContentBuilder field(String name, Object value) {
260355
addChunk((b, p) -> b.field(name, value));
261356
return this;
@@ -289,30 +384,6 @@ public <T> ChunkedToXContentBuilder forEach(Iterator<T> items, Function<? super
289384
return this;
290385
}
291386

292-
/**
293-
* Each value in {@code map} is added to the builder as a separate field, named by its key.
294-
* <p>
295-
* Note that any {@link ToXContent} objects in {@code map} will be passed an empty {@link ToXContent.Params},
296-
* rather than the {@code params} given to this builder in the constructor.
297-
*/
298-
public ChunkedToXContentBuilder appendEntries(Map<String, ?> map) {
299-
return forEach(map.entrySet().iterator(), (b, e) -> b.field(e.getKey(), e.getValue()));
300-
}
301-
302-
/**
303-
* Each value in {@code map} is added to the builder as a separate object, named by its key.
304-
*/
305-
public ChunkedToXContentBuilder appendXContentObjects(Map<String, ? extends ToXContent> map) {
306-
return forEach(map.entrySet().iterator(), (b, e) -> b.xContentObject(e.getKey(), e.getValue()));
307-
}
308-
309-
/**
310-
* Each value in {@code map} is added to the builder as a separate field, named by its key.
311-
*/
312-
public ChunkedToXContentBuilder appendXContentFields(Map<String, ? extends ToXContent> map) {
313-
return forEach(map.entrySet().iterator(), (b, e) -> b.field(e.getKey()).append(e.getValue()));
314-
}
315-
316387
public ChunkedToXContentBuilder append(ToXContent xContent) {
317388
if (xContent != ToXContent.EMPTY) {
318389
addChunk(xContent);

0 commit comments

Comments
 (0)