|
12 | 12 | import org.elasticsearch.action.ActionResponse;
|
13 | 13 | import org.elasticsearch.action.support.DefaultShardOperationFailedException;
|
14 | 14 | import org.elasticsearch.cluster.node.DiscoveryNode;
|
| 15 | +import org.elasticsearch.common.collect.Iterators; |
15 | 16 | import org.elasticsearch.common.io.stream.StreamInput;
|
16 | 17 | import org.elasticsearch.common.io.stream.StreamOutput;
|
17 | 18 | import org.elasticsearch.common.io.stream.Writeable;
|
| 19 | +import org.elasticsearch.common.xcontent.ChunkedToXContentHelper; |
| 20 | +import org.elasticsearch.common.xcontent.ChunkedToXContentObject; |
| 21 | +import org.elasticsearch.xcontent.ToXContent; |
18 | 22 | import org.elasticsearch.xcontent.ToXContentFragment;
|
19 | 23 | import org.elasticsearch.xcontent.XContentBuilder;
|
20 | 24 |
|
21 | 25 | import java.io.IOException;
|
| 26 | +import java.util.Collections; |
| 27 | +import java.util.Iterator; |
22 | 28 | import java.util.List;
|
23 | 29 | import java.util.Map;
|
24 | 30 |
|
|
28 | 34 | * Consists of {@link StoreStatus}s for requested indices grouped by
|
29 | 35 | * indices and shard ids and a list of encountered node {@link Failure}s
|
30 | 36 | */
|
31 |
| -public class IndicesShardStoresResponse extends ActionResponse implements ToXContentFragment { |
| 37 | +public class IndicesShardStoresResponse extends ActionResponse implements ChunkedToXContentObject { |
32 | 38 |
|
33 | 39 | /**
|
34 | 40 | * Shard store information from a node
|
@@ -196,7 +202,7 @@ public int compareTo(StoreStatus other) {
|
196 | 202 | * Single node failure while retrieving shard store information
|
197 | 203 | */
|
198 | 204 | public static class Failure extends DefaultShardOperationFailedException {
|
199 |
| - private String nodeId; |
| 205 | + private final String nodeId; |
200 | 206 |
|
201 | 207 | public Failure(String nodeId, String index, int shardId, Throwable reason) {
|
202 | 208 | super(index, shardId, reason);
|
@@ -273,38 +279,45 @@ public void writeTo(StreamOutput out) throws IOException {
|
273 | 279 | }
|
274 | 280 |
|
275 | 281 | @Override
|
276 |
| - public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { |
277 |
| - if (failures.size() > 0) { |
278 |
| - builder.startArray(Fields.FAILURES); |
279 |
| - for (Failure failure : failures) { |
280 |
| - failure.toXContent(builder, params); |
281 |
| - } |
282 |
| - builder.endArray(); |
283 |
| - } |
284 |
| - |
285 |
| - builder.startObject(Fields.INDICES); |
286 |
| - for (Map.Entry<String, Map<Integer, List<StoreStatus>>> indexShards : storeStatuses.entrySet()) { |
287 |
| - builder.startObject(indexShards.getKey()); |
288 |
| - |
289 |
| - builder.startObject(Fields.SHARDS); |
290 |
| - for (Map.Entry<Integer, List<StoreStatus>> shardStatusesEntry : indexShards.getValue().entrySet()) { |
291 |
| - builder.startObject(String.valueOf(shardStatusesEntry.getKey())); |
292 |
| - builder.startArray(Fields.STORES); |
293 |
| - for (StoreStatus storeStatus : shardStatusesEntry.getValue()) { |
294 |
| - builder.startObject(); |
295 |
| - storeStatus.toXContent(builder, params); |
296 |
| - builder.endObject(); |
297 |
| - } |
298 |
| - builder.endArray(); |
299 |
| - |
300 |
| - builder.endObject(); |
301 |
| - } |
302 |
| - builder.endObject(); |
303 |
| - |
304 |
| - builder.endObject(); |
305 |
| - } |
306 |
| - builder.endObject(); |
307 |
| - return builder; |
| 282 | + public Iterator<? extends ToXContent> toXContentChunked(ToXContent.Params outerParams) { |
| 283 | + return Iterators.concat( |
| 284 | + ChunkedToXContentHelper.startObject(), |
| 285 | + |
| 286 | + failures.isEmpty() |
| 287 | + ? Collections.emptyIterator() |
| 288 | + : Iterators.concat( |
| 289 | + ChunkedToXContentHelper.startArray(Fields.FAILURES), |
| 290 | + failures.iterator(), |
| 291 | + ChunkedToXContentHelper.endArray() |
| 292 | + ), |
| 293 | + |
| 294 | + ChunkedToXContentHelper.startObject(Fields.INDICES), |
| 295 | + |
| 296 | + Iterators.flatMap( |
| 297 | + storeStatuses.entrySet().iterator(), |
| 298 | + indexShards -> Iterators.concat( |
| 299 | + ChunkedToXContentHelper.startObject(indexShards.getKey()), |
| 300 | + ChunkedToXContentHelper.startObject(Fields.SHARDS), |
| 301 | + Iterators.flatMap( |
| 302 | + indexShards.getValue().entrySet().iterator(), |
| 303 | + shardStatusesEntry -> Iterators.single((ToXContent) (builder, params) -> { |
| 304 | + builder.startObject(String.valueOf(shardStatusesEntry.getKey())).startArray(Fields.STORES); |
| 305 | + for (StoreStatus storeStatus : shardStatusesEntry.getValue()) { |
| 306 | + builder.startObject(); |
| 307 | + storeStatus.toXContent(builder, params); |
| 308 | + builder.endObject(); |
| 309 | + } |
| 310 | + return builder.endArray().endObject(); |
| 311 | + }) |
| 312 | + ), |
| 313 | + ChunkedToXContentHelper.endObject(), |
| 314 | + ChunkedToXContentHelper.endObject() |
| 315 | + ) |
| 316 | + ), |
| 317 | + |
| 318 | + ChunkedToXContentHelper.endObject(), |
| 319 | + ChunkedToXContentHelper.endObject() |
| 320 | + ); |
308 | 321 | }
|
309 | 322 |
|
310 | 323 | static final class Fields {
|
|
0 commit comments