Skip to content

Commit c06bde4

Browse files
Cleanup duplication and dead code around ChunkedToXContentHelper (#123217)
Cleans up a couple things that are obviously broken: * duplicate array and object constructs where the helper utility generates the exact same iterator * unused helper methods * single iterator concatenations
1 parent f5e2a92 commit c06bde4

File tree

15 files changed

+57
-110
lines changed

15 files changed

+57
-110
lines changed

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
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;
2120
import org.elasticsearch.common.xcontent.ChunkedToXContentHelper;
@@ -92,7 +91,7 @@ public static IngestGeoIpMetadata fromXContent(XContentParser parser) throws IOE
9291

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

9897
@Override

server/src/main/java/org/elasticsearch/action/admin/cluster/allocation/ClusterAllocationExplanation.java

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -196,11 +196,7 @@ public Iterator<? extends ToXContent> toXContentChunked(ToXContent.Params params
196196
return builder;
197197
}),
198198
this.clusterInfo != null
199-
? Iterators.concat(
200-
ChunkedToXContentHelper.startObject("cluster_info"),
201-
this.clusterInfo.toXContentChunked(params),
202-
ChunkedToXContentHelper.endObject()
203-
)
199+
? ChunkedToXContentHelper.object("cluster_info", this.clusterInfo.toXContentChunked(params))
204200
: Collections.emptyIterator(),
205201
getShardAllocationDecisionChunked(params),
206202
Iterators.single((builder, p) -> builder.endObject())

server/src/main/java/org/elasticsearch/action/admin/cluster/allocation/DesiredBalanceResponse.java

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import org.elasticsearch.common.io.stream.StreamInput;
2121
import org.elasticsearch.common.io.stream.StreamOutput;
2222
import org.elasticsearch.common.io.stream.Writeable;
23+
import org.elasticsearch.common.xcontent.ChunkedToXContentHelper;
2324
import org.elasticsearch.common.xcontent.ChunkedToXContentObject;
2425
import org.elasticsearch.core.Nullable;
2526
import org.elasticsearch.xcontent.ToXContent;
@@ -34,8 +35,6 @@
3435
import java.util.Set;
3536

3637
import static org.elasticsearch.common.xcontent.ChunkedToXContentHelper.chunk;
37-
import static org.elasticsearch.common.xcontent.ChunkedToXContentHelper.endObject;
38-
import static org.elasticsearch.common.xcontent.ChunkedToXContentHelper.startObject;
3938

4039
public class DesiredBalanceResponse extends ActionResponse implements ChunkedToXContentObject {
4140

@@ -96,16 +95,15 @@ public Iterator<? extends ToXContent> toXContentChunked(ToXContent.Params params
9695
),
9796
Iterators.flatMap(
9897
routingTable.entrySet().iterator(),
99-
indexEntry -> Iterators.concat(
100-
startObject(indexEntry.getKey()),
98+
indexEntry -> ChunkedToXContentHelper.object(
99+
indexEntry.getKey(),
101100
Iterators.flatMap(
102101
indexEntry.getValue().entrySet().iterator(),
103102
shardEntry -> Iterators.concat(
104103
chunk((builder, p) -> builder.field(String.valueOf(shardEntry.getKey()))),
105104
shardEntry.getValue().toXContentChunked(params)
106105
)
107-
),
108-
endObject()
106+
)
109107
)
110108
),
111109
chunk((builder, p) -> builder.endObject().startObject("cluster_info")),

server/src/main/java/org/elasticsearch/action/admin/cluster/node/stats/NodesStatsResponse.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,14 +42,13 @@ protected void writeNodesTo(StreamOutput out, List<NodeStats> nodes) throws IOEx
4242

4343
@Override
4444
protected Iterator<? extends ToXContent> xContentChunks(ToXContent.Params outerParams) {
45-
return Iterators.concat(
46-
ChunkedToXContentHelper.startObject("nodes"),
45+
return ChunkedToXContentHelper.object(
46+
"nodes",
4747
Iterators.flatMap(getNodes().iterator(), nodeStats -> Iterators.concat(Iterators.single((builder, params) -> {
4848
builder.startObject(nodeStats.getNode().getId());
4949
builder.field("timestamp", nodeStats.getTimestamp());
5050
return builder;
51-
}), nodeStats.toXContentChunked(outerParams), ChunkedToXContentHelper.endObject())),
52-
ChunkedToXContentHelper.endObject()
51+
}), nodeStats.toXContentChunked(outerParams), ChunkedToXContentHelper.endObject()))
5352
);
5453
}
5554

server/src/main/java/org/elasticsearch/action/admin/indices/segments/IndicesSegmentResponse.java

Lines changed: 21 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -71,19 +71,17 @@ public void writeTo(StreamOutput out) throws IOException {
7171

7272
@Override
7373
protected Iterator<ToXContent> customXContentChunks(ToXContent.Params params) {
74-
return Iterators.concat(
75-
76-
ChunkedToXContentHelper.startObject(Fields.INDICES),
74+
return ChunkedToXContentHelper.object(
75+
Fields.INDICES,
7776
Iterators.flatMap(
7877
getIndices().values().iterator(),
7978
indexSegments -> Iterators.concat(
8079

8180
ChunkedToXContentHelper.chunk((builder, p) -> builder.startObject(indexSegments.getIndex()).startObject(Fields.SHARDS)),
8281
Iterators.flatMap(
8382
indexSegments.iterator(),
84-
indexSegment -> Iterators.concat(
85-
86-
ChunkedToXContentHelper.startArray(Integer.toString(indexSegment.shardId().id())),
83+
indexSegment -> ChunkedToXContentHelper.array(
84+
Integer.toString(indexSegment.shardId().id()),
8785
Iterators.flatMap(
8886
indexSegment.iterator(),
8987
shardSegments -> Iterators.concat(
@@ -141,14 +139,12 @@ protected Iterator<ToXContent> customXContentChunks(ToXContent.Params params) {
141139
),
142140
ChunkedToXContentHelper.chunk((builder, p) -> builder.endObject().endObject())
143141
)
144-
),
145-
ChunkedToXContentHelper.endArray()
142+
)
146143
)
147144
),
148145
ChunkedToXContentHelper.chunk((builder, p) -> builder.endObject().endObject())
149146
)
150-
),
151-
ChunkedToXContentHelper.endObject()
147+
)
152148
);
153149
}
154150

@@ -157,25 +153,21 @@ private static Iterator<ToXContent> getSegmentSortChunks(@Nullable Sort segmentS
157153
return Collections.emptyIterator();
158154
}
159155

160-
return Iterators.concat(
161-
ChunkedToXContentHelper.startArray("sort"),
162-
Iterators.map(Iterators.forArray(segmentSort.getSort()), field -> (builder, p) -> {
163-
builder.startObject();
164-
builder.field("field", field.getField());
165-
if (field instanceof SortedNumericSortField sortedNumericSortField) {
166-
builder.field("mode", sortedNumericSortField.getSelector().toString().toLowerCase(Locale.ROOT));
167-
} else if (field instanceof SortedSetSortField sortedSetSortField) {
168-
builder.field("mode", sortedSetSortField.getSelector().toString().toLowerCase(Locale.ROOT));
169-
}
170-
if (field.getMissingValue() != null) {
171-
builder.field("missing", field.getMissingValue().toString());
172-
}
173-
builder.field("reverse", field.getReverse());
174-
builder.endObject();
175-
return builder;
176-
}),
177-
ChunkedToXContentHelper.endArray()
178-
);
156+
return ChunkedToXContentHelper.array("sort", Iterators.map(Iterators.forArray(segmentSort.getSort()), field -> (builder, p) -> {
157+
builder.startObject();
158+
builder.field("field", field.getField());
159+
if (field instanceof SortedNumericSortField sortedNumericSortField) {
160+
builder.field("mode", sortedNumericSortField.getSelector().toString().toLowerCase(Locale.ROOT));
161+
} else if (field instanceof SortedSetSortField sortedSetSortField) {
162+
builder.field("mode", sortedSetSortField.getSelector().toString().toLowerCase(Locale.ROOT));
163+
}
164+
if (field.getMissingValue() != null) {
165+
builder.field("missing", field.getMissingValue().toString());
166+
}
167+
builder.field("reverse", field.getReverse());
168+
builder.endObject();
169+
return builder;
170+
}));
179171
}
180172

181173
static final class Fields {

server/src/main/java/org/elasticsearch/action/admin/indices/shards/IndicesShardStoresResponse.java

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -279,13 +279,7 @@ public Iterator<? extends ToXContent> toXContentChunked(ToXContent.Params outerP
279279
return Iterators.concat(
280280
ChunkedToXContentHelper.startObject(),
281281

282-
failures.isEmpty()
283-
? Collections.emptyIterator()
284-
: Iterators.concat(
285-
ChunkedToXContentHelper.startArray(Fields.FAILURES),
286-
failures.iterator(),
287-
ChunkedToXContentHelper.endArray()
288-
),
282+
failures.isEmpty() ? Collections.emptyIterator() : ChunkedToXContentHelper.array(Fields.FAILURES, failures.iterator()),
289283

290284
ChunkedToXContentHelper.startObject(Fields.INDICES),
291285

server/src/main/java/org/elasticsearch/action/admin/indices/stats/IndicesStatsResponse.java

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -232,22 +232,20 @@ protected Iterator<ToXContent> customXContentChunks(ToXContent.Params params) {
232232
}),
233233

234234
level == ClusterStatsLevel.SHARDS
235-
? Iterators.concat(
236-
ChunkedToXContentHelper.startObject(Fields.SHARDS),
235+
? ChunkedToXContentHelper.object(
236+
Fields.SHARDS,
237237
Iterators.flatMap(
238238
indexStats.iterator(),
239-
indexShardStats -> Iterators.concat(
240-
ChunkedToXContentHelper.startArray(Integer.toString(indexShardStats.getShardId().id())),
241-
Iterators.<ShardStats, ToXContent>map(indexShardStats.iterator(), shardStats -> (builder, p) -> {
239+
indexShardStats -> ChunkedToXContentHelper.array(
240+
Integer.toString(indexShardStats.getShardId().id()),
241+
Iterators.map(indexShardStats.iterator(), shardStats -> (builder, p) -> {
242242
builder.startObject();
243243
shardStats.toXContent(builder, p);
244244
builder.endObject();
245245
return builder;
246-
}),
247-
ChunkedToXContentHelper.endArray()
246+
})
248247
)
249-
),
250-
ChunkedToXContentHelper.endObject()
248+
)
251249
)
252250
: Collections.emptyIterator(),
253251

server/src/main/java/org/elasticsearch/cluster/ClusterState.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -747,10 +747,9 @@ public Iterator<? extends ToXContent> toXContentChunked(ToXContent.Params outerP
747747
metrics.contains(Metric.ROUTING_NODES),
748748
(builder, params) -> builder.startObject("nodes"),
749749
getRoutingNodes().iterator(),
750-
routingNode -> Iterators.concat(
751-
ChunkedToXContentHelper.startArray(routingNode.nodeId() == null ? "null" : routingNode.nodeId()),
752-
routingNode.iterator(),
753-
ChunkedToXContentHelper.endArray()
750+
routingNode -> ChunkedToXContentHelper.array(
751+
routingNode.nodeId() == null ? "null" : routingNode.nodeId(),
752+
routingNode.iterator()
754753
),
755754
(builder, params) -> builder.endObject().endObject()
756755
),

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import org.elasticsearch.common.io.stream.StreamInput;
1818
import org.elasticsearch.common.io.stream.StreamOutput;
1919
import org.elasticsearch.common.io.stream.Writeable;
20+
import org.elasticsearch.common.xcontent.ChunkedToXContentHelper;
2021
import org.elasticsearch.common.xcontent.ChunkedToXContentObject;
2122
import org.elasticsearch.core.Nullable;
2223
import org.elasticsearch.xcontent.ToXContent;
@@ -170,7 +171,7 @@ public Iterator<? extends ToXContent> toXContentChunked(ToXContent.Params params
170171
startObject(),
171172
chunk((builder, p) -> buildHeader(builder)),
172173
Objects.nonNull(allocationDecision)
173-
? Iterators.concat(startObject(NODE_ALLOCATION_DECISION_KEY), allocationDecision.toXContentChunked(params), endObject())
174+
? ChunkedToXContentHelper.object(NODE_ALLOCATION_DECISION_KEY, allocationDecision.toXContentChunked(params))
174175
: Collections.emptyIterator(),
175176
endObject()
176177
);

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

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -83,10 +83,6 @@ public static Iterator<ToXContent> field(String name, ChunkedToXContentObject va
8383
return Iterators.concat(Iterators.single((builder, innerParam) -> builder.field(name)), value.toXContentChunked(params));
8484
}
8585

86-
public static Iterator<ToXContent> array(Iterator<? extends ToXContent> contents) {
87-
return Iterators.concat(startArray(), contents, endArray());
88-
}
89-
9086
public static Iterator<ToXContent> array(String name, Iterator<? extends ToXContent> contents) {
9187
return Iterators.concat(startArray(name), contents, endArray());
9288
}
@@ -95,10 +91,6 @@ public static <T> Iterator<ToXContent> array(Iterator<T> items, Function<T, ToXC
9591
return Iterators.concat(startArray(), Iterators.map(items, toXContent), endArray());
9692
}
9793

98-
public static <T> Iterator<ToXContent> array(String name, Iterator<T> items, Function<T, ToXContent> toXContent) {
99-
return Iterators.concat(startArray(name), Iterators.map(items, toXContent), endArray());
100-
}
101-
10294
/**
10395
* Creates an Iterator to serialize a named field where the value is represented by an iterator of {@link ChunkedToXContentObject}.
10496
* Chunked equivalent for {@code XContentBuilder array(String name, ToXContent value)}
@@ -108,7 +100,7 @@ public static <T> Iterator<ToXContent> array(String name, Iterator<T> items, Fun
108100
* @return Iterator composing field name and value serialization
109101
*/
110102
public static Iterator<ToXContent> array(String name, Iterator<? extends ChunkedToXContentObject> contents, ToXContent.Params params) {
111-
return Iterators.concat(startArray(name), Iterators.flatMap(contents, c -> c.toXContentChunked(params)), endArray());
103+
return array(name, Iterators.flatMap(contents, c -> c.toXContentChunked(params)));
112104
}
113105

114106
/**
@@ -129,14 +121,4 @@ public static Iterator<ToXContent> chunk(ToXContent item) {
129121
return Iterators.single(item);
130122
}
131123

132-
/**
133-
* Creates an Iterator of a single ToXContent object that serializes the given object as a single chunk. Just wraps {@link
134-
* Iterators#single}, but still useful because it avoids any type ambiguity.
135-
*
136-
* @param item Item to wrap
137-
* @return Singleton iterator for the given item.
138-
*/
139-
public static Iterator<ToXContent> singleChunk(ToXContent item) {
140-
return Iterators.single(item);
141-
}
142124
}

0 commit comments

Comments
 (0)