Skip to content

Commit

Permalink
Convert a few more implementations to ChunkedXContentBuilder (elastic…
Browse files Browse the repository at this point in the history
…#113125)

Remove the complex methods from ChunkedXContentHelper
  • Loading branch information
thecoop authored Oct 4, 2024
1 parent 7dfd3f5 commit 11c2eb2
Show file tree
Hide file tree
Showing 29 changed files with 240 additions and 259 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,9 @@
import org.elasticsearch.cluster.DiffableUtils;
import org.elasticsearch.cluster.NamedDiff;
import org.elasticsearch.cluster.metadata.Metadata;
import org.elasticsearch.common.collect.Iterators;
import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.io.stream.StreamOutput;
import org.elasticsearch.common.xcontent.ChunkedToXContentHelper;
import org.elasticsearch.common.xcontent.ChunkedToXContent;
import org.elasticsearch.ingest.geoip.direct.DatabaseConfigurationMetadata;
import org.elasticsearch.xcontent.ConstructingObjectParser;
import org.elasticsearch.xcontent.ParseField;
Expand Down Expand Up @@ -91,8 +90,8 @@ public static IngestGeoIpMetadata fromXContent(XContentParser parser) throws IOE
}

@Override
public Iterator<? extends ToXContent> toXContentChunked(ToXContent.Params ignored) {
return Iterators.concat(ChunkedToXContentHelper.xContentValuesMap(DATABASES_FIELD.getPreferredName(), databases));
public Iterator<? extends ToXContent> toXContentChunked(ToXContent.Params params) {
return ChunkedToXContent.builder(params).xContentObjectFields(DATABASES_FIELD.getPreferredName(), databases);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws

void innerToXContent(XContentBuilder builder, Params params) throws IOException {
if (hasResponse()) {
ChunkedToXContent.wrapAsToXContent(p -> response.innerToXContentChunked(p)).toXContent(builder, params);
ChunkedToXContent.wrapAsToXContent(response::innerToXContentChunked).toXContent(builder, params);
} else {
// we can assume the template is always json as we convert it before compiling it
try (InputStream stream = source.streamInput()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import org.elasticsearch.common.collect.Iterators;
import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.io.stream.StreamOutput;
import org.elasticsearch.common.xcontent.ChunkedToXContent;
import org.elasticsearch.common.xcontent.ChunkedToXContentObject;
import org.elasticsearch.core.TimeValue;
import org.elasticsearch.xcontent.ToXContent;
Expand Down Expand Up @@ -157,14 +158,13 @@ public void writeTo(StreamOutput out) throws IOException {

@Override
public Iterator<? extends ToXContent> toXContentChunked(ToXContent.Params params) {
return Iterators.concat(Iterators.single((builder, p) -> {
builder.startObject();
builder.field(ERRORS, hasFailures());
builder.field(TOOK, tookInMillis);
return ChunkedToXContent.builder(params).object(ob -> {
ob.field(ERRORS, hasFailures());
ob.field(TOOK, tookInMillis);
if (ingestTookInMillis != BulkResponse.NO_INGEST_TOOK) {
builder.field(INGEST_TOOK, ingestTookInMillis);
ob.field(INGEST_TOOK, ingestTookInMillis);
}
return builder.startArray(ITEMS);
}), Iterators.forArray(responses), Iterators.<ToXContent>single((builder, p) -> builder.endArray().endObject()));
ob.array(ITEMS, Iterators.forArray(responses));
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,12 @@
import org.elasticsearch.action.OriginalIndices;
import org.elasticsearch.common.Strings;
import org.elasticsearch.common.bytes.BytesReference;
import org.elasticsearch.common.collect.Iterators;
import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.io.stream.StreamOutput;
import org.elasticsearch.common.io.stream.Writeable;
import org.elasticsearch.common.lucene.Lucene;
import org.elasticsearch.common.util.concurrent.ConcurrentCollections;
import org.elasticsearch.common.xcontent.ChunkedToXContentHelper;
import org.elasticsearch.common.xcontent.ChunkedToXContent;
import org.elasticsearch.common.xcontent.ChunkedToXContentObject;
import org.elasticsearch.core.Nullable;
import org.elasticsearch.core.RefCounted;
Expand Down Expand Up @@ -383,39 +382,17 @@ public Clusters getClusters() {
@Override
public Iterator<? extends ToXContent> toXContentChunked(ToXContent.Params params) {
assert hasReferences();
return Iterators.concat(
ChunkedToXContentHelper.startObject(),
this.innerToXContentChunked(params),
ChunkedToXContentHelper.endObject()
);
return ChunkedToXContent.builder(params).xContentObject(innerToXContentChunked(params));
}

public Iterator<? extends ToXContent> innerToXContentChunked(ToXContent.Params params) {
return Iterators.concat(
ChunkedToXContentHelper.singleChunk(SearchResponse.this::headerToXContent),
Iterators.single(clusters),
Iterators.concat(
Iterators.flatMap(Iterators.single(hits), r -> r.toXContentChunked(params)),
Iterators.single((ToXContent) (b, p) -> {
if (aggregations != null) {
aggregations.toXContent(b, p);
}
return b;
}),
Iterators.single((b, p) -> {
if (suggest != null) {
suggest.toXContent(b, p);
}
return b;
}),
Iterators.single((b, p) -> {
if (profileResults != null) {
profileResults.toXContent(b, p);
}
return b;
})
)
);
return ChunkedToXContent.builder(params)
.append(SearchResponse.this::headerToXContent)
.append(clusters)
.append(hits)
.appendIfPresent(aggregations)
.appendIfPresent(suggest)
.appendIfPresent(profileResults);
}

public XContentBuilder headerToXContent(XContentBuilder builder, ToXContent.Params params) throws IOException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
package org.elasticsearch.action.support.broadcast;

import org.elasticsearch.action.support.DefaultShardOperationFailedException;
import org.elasticsearch.common.collect.Iterators;
import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.xcontent.ChunkedToXContent;
import org.elasticsearch.common.xcontent.ChunkedToXContentObject;
import org.elasticsearch.rest.action.RestActions;
import org.elasticsearch.xcontent.ToXContent;
Expand All @@ -35,11 +35,8 @@ public ChunkedBroadcastResponse(

@Override
public final Iterator<ToXContent> toXContentChunked(ToXContent.Params params) {
return Iterators.concat(Iterators.single((b, p) -> {
b.startObject();
RestActions.buildBroadcastShardsHeader(b, p, this);
return b;
}), customXContentChunks(params), Iterators.single((builder, p) -> builder.endObject()));
return ChunkedToXContent.builder(params)
.object(ob -> ob.append((b, p) -> RestActions.buildBroadcastShardsHeader(b, p, this)).append(this::customXContentChunks));
}

protected abstract Iterator<ToXContent> customXContentChunks(ToXContent.Params params);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import org.elasticsearch.common.Strings;
import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.io.stream.StreamOutput;
import org.elasticsearch.common.xcontent.ChunkedToXContentHelper;
import org.elasticsearch.common.xcontent.ChunkedToXContent;
import org.elasticsearch.xcontent.ConstructingObjectParser;
import org.elasticsearch.xcontent.ParseField;
import org.elasticsearch.xcontent.ToXContent;
Expand Down Expand Up @@ -102,8 +102,8 @@ public static ComponentTemplateMetadata fromXContent(XContentParser parser) thro
}

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

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import org.elasticsearch.common.Strings;
import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.io.stream.StreamOutput;
import org.elasticsearch.common.xcontent.ChunkedToXContentHelper;
import org.elasticsearch.common.xcontent.ChunkedToXContent;
import org.elasticsearch.xcontent.ConstructingObjectParser;
import org.elasticsearch.xcontent.ParseField;
import org.elasticsearch.xcontent.ToXContent;
Expand Down Expand Up @@ -103,8 +103,8 @@ public void writeTo(StreamOutput out) throws IOException {
}

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

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ public static DataStreamMetadata fromXContent(XContentParser parser) throws IOEx
@Override
public Iterator<? extends ToXContent> toXContentChunked(ToXContent.Params params) {
return ChunkedToXContent.builder(params)
.object(DATA_STREAM.getPreferredName(), b -> b.appendXContentFields(dataStreams))
.xContentObjectFields(DATA_STREAM.getPreferredName(), dataStreams)
.xContentObject(DATA_STREAM_ALIASES.getPreferredName(), dataStreamAliases.values().iterator());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import org.elasticsearch.cluster.SimpleDiffable;
import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.io.stream.StreamOutput;
import org.elasticsearch.common.xcontent.ChunkedToXContentHelper;
import org.elasticsearch.common.xcontent.ChunkedToXContent;
import org.elasticsearch.core.Nullable;
import org.elasticsearch.xcontent.ConstructingObjectParser;
import org.elasticsearch.xcontent.ParseField;
Expand Down Expand Up @@ -190,8 +190,8 @@ public int hashCode() {
}

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

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,46 @@ public ChunkedToXContentBuilder xContentObject(String name, Iterator<? extends T
return this;
}

/**
* Creates an object with the contents of each field created from each entry in {@code map}
*/
public ChunkedToXContentBuilder xContentObjectFields(Map<String, ? extends ToXContent> map) {
startObject();
map.forEach(this::field);
endObject();
return this;
}

/**
* Creates an object named {@code name}, with the contents of each field created from each entry in {@code map}
*/
public ChunkedToXContentBuilder xContentObjectFields(String name, Map<String, ? extends ToXContent> map) {
startObject(name);
map.forEach(this::field);
endObject();
return this;
}

/**
* Creates an object with the contents of each field each another object created from each entry in {@code map}
*/
public ChunkedToXContentBuilder xContentObjectFieldObjects(Map<String, ? extends ToXContent> map) {
startObject();
map.forEach(this::xContentObject);
endObject();
return this;
}

/**
* Creates an object named {@code name}, with the contents of each field each another object created from each entry in {@code map}
*/
public ChunkedToXContentBuilder xContentObjectFieldObjects(String name, Map<String, ? extends ToXContent> map) {
startObject(name);
map.forEach(this::xContentObject);
endObject();
return this;
}

/**
* Creates an object, with the contents set by {@code contents}
*/
Expand Down Expand Up @@ -172,6 +212,26 @@ public <T> ChunkedToXContentBuilder object(String name, Iterator<T> items, Funct
return this;
}

/**
* Creates an object with the contents of each field set by {@code map}
*/
public ChunkedToXContentBuilder object(Map<String, ?> map) {
startObject();
map.forEach(this::field);
endObject();
return this;
}

/**
* Creates an object named {@code name}, with the contents of each field set by {@code map}
*/
public ChunkedToXContentBuilder object(String name, Map<String, ?> map) {
startObject(name);
map.forEach(this::field);
endObject();
return this;
}

private void startArray() {
addChunk((b, p) -> b.startArray());
}
Expand Down Expand Up @@ -223,7 +283,7 @@ public <T> ChunkedToXContentBuilder array(String name, Iterator<T> items, BiCons
/**
* Creates an array named {@code name}, with the contents set by appending together the contents of {@code items}
*/
public <T> ChunkedToXContentBuilder array(String name, Iterator<? extends ToXContent> items) {
public ChunkedToXContentBuilder array(String name, Iterator<? extends ToXContent> items) {
startArray(name);
items.forEachRemaining(this::append);
endArray();
Expand All @@ -246,16 +306,51 @@ public ChunkedToXContentBuilder field(String name) {
return this;
}

public ChunkedToXContentBuilder field(String name, boolean value) {
addChunk((b, p) -> b.field(name, value));
return this;
}

public ChunkedToXContentBuilder field(String name, Boolean value) {
addChunk((b, p) -> b.field(name, value));
return this;
}

public ChunkedToXContentBuilder field(String name, int value) {
addChunk((b, p) -> b.field(name, value));
return this;
}

public ChunkedToXContentBuilder field(String name, Integer value) {
addChunk((b, p) -> b.field(name, value));
return this;
}

public ChunkedToXContentBuilder field(String name, long value) {
addChunk((b, p) -> b.field(name, value));
return this;
}

public ChunkedToXContentBuilder field(String name, Long value) {
addChunk((b, p) -> b.field(name, value));
return this;
}

public ChunkedToXContentBuilder field(String name, String value) {
addChunk((b, p) -> b.field(name, value));
return this;
}

public ChunkedToXContentBuilder field(String name, Enum<?> value) {
addChunk((b, p) -> b.field(name, value));
return this;
}

public ChunkedToXContentBuilder field(String name, ToXContent value) {
addChunk((b, p) -> b.field(name, value, p));
return this;
}

public ChunkedToXContentBuilder field(String name, Object value) {
addChunk((b, p) -> b.field(name, value));
return this;
Expand Down Expand Up @@ -289,30 +384,6 @@ public <T> ChunkedToXContentBuilder forEach(Iterator<T> items, Function<? super
return this;
}

/**
* Each value in {@code map} is added to the builder as a separate field, named by its key.
* <p>
* Note that any {@link ToXContent} objects in {@code map} will be passed an empty {@link ToXContent.Params},
* rather than the {@code params} given to this builder in the constructor.
*/
public ChunkedToXContentBuilder appendEntries(Map<String, ?> map) {
return forEach(map.entrySet().iterator(), (b, e) -> b.field(e.getKey(), e.getValue()));
}

/**
* Each value in {@code map} is added to the builder as a separate object, named by its key.
*/
public ChunkedToXContentBuilder appendXContentObjects(Map<String, ? extends ToXContent> map) {
return forEach(map.entrySet().iterator(), (b, e) -> b.xContentObject(e.getKey(), e.getValue()));
}

/**
* Each value in {@code map} is added to the builder as a separate field, named by its key.
*/
public ChunkedToXContentBuilder appendXContentFields(Map<String, ? extends ToXContent> map) {
return forEach(map.entrySet().iterator(), (b, e) -> b.field(e.getKey()).append(e.getValue()));
}

public ChunkedToXContentBuilder append(ToXContent xContent) {
if (xContent != ToXContent.EMPTY) {
addChunk(xContent);
Expand Down
Loading

0 comments on commit 11c2eb2

Please sign in to comment.