Skip to content

Commit 54ed9c5

Browse files
committed
Simplify EsqlQueryResponse
1 parent 3f03775 commit 54ed9c5

File tree

1 file changed

+56
-88
lines changed

1 file changed

+56
-88
lines changed

x-pack/plugin/esql/src/main/java/org/elasticsearch/xpack/esql/action/EsqlQueryResponse.java

Lines changed: 56 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
import java.util.List;
3535
import java.util.Objects;
3636
import java.util.Optional;
37+
import java.util.function.Supplier;
3738

3839
import static org.elasticsearch.TransportVersions.ESQL_DOCUMENTS_FOUND_AND_VALUES_LOADED;
3940

@@ -122,7 +123,7 @@ static EsqlQueryResponse deserialize(BlockStreamInput in) throws IOException {
122123
long documentsFound = in.getTransportVersion().onOrAfter(ESQL_DOCUMENTS_FOUND_AND_VALUES_LOADED) ? in.readVLong() : 0;
123124
long valuesLoaded = in.getTransportVersion().onOrAfter(ESQL_DOCUMENTS_FOUND_AND_VALUES_LOADED) ? in.readVLong() : 0;
124125
if (in.getTransportVersion().onOrAfter(TransportVersions.V_8_12_0)) {
125-
profile = in.readOptionalWriteable(Profile::new);
126+
profile = in.readOptionalWriteable(Profile::readFrom);
126127
}
127128
boolean columnar = in.readBoolean();
128129
EsqlExecutionInfo executionInfo = null;
@@ -224,75 +225,68 @@ public EsqlExecutionInfo getExecutionInfo() {
224225
return executionInfo;
225226
}
226227

227-
private Iterator<? extends ToXContent> asyncPropertiesOrEmpty() {
228-
if (isAsync) {
229-
return ChunkedToXContentHelper.chunk((builder, params) -> {
230-
if (asyncExecutionId != null) {
231-
builder.field("id", asyncExecutionId);
232-
}
233-
builder.field("is_running", isRunning);
234-
return builder;
235-
});
236-
} else {
237-
return Collections.emptyIterator();
238-
}
239-
}
240-
241228
@Override
242229
public Iterator<? extends ToXContent> toXContentChunked(ToXContent.Params params) {
243230
boolean dropNullColumns = params.paramAsBoolean(DROP_NULL_COLUMNS_OPTION, false);
244231
boolean[] nullColumns = dropNullColumns ? nullColumns() : null;
245232

246-
Iterator<ToXContent> tookTime;
247-
if (executionInfo != null && executionInfo.overallTook() != null) {
248-
tookTime = ChunkedToXContentHelper.chunk(
249-
(builder, p) -> builder.field("took", executionInfo.overallTook().millis())
250-
.field(EsqlExecutionInfo.IS_PARTIAL_FIELD.getPreferredName(), executionInfo.isPartial())
251-
);
252-
} else {
253-
tookTime = Collections.emptyIterator();
254-
}
255-
256-
Iterator<ToXContent> meta = ChunkedToXContentHelper.chunk((builder, p) -> {
257-
builder.field("documents_found", documentsFound);
258-
builder.field("values_loaded", valuesLoaded);
259-
return builder;
260-
});
261-
262-
Iterator<? extends ToXContent> columnHeadings = dropNullColumns
263-
? Iterators.concat(
264-
ResponseXContentUtils.allColumns(columns, "all_columns"),
265-
ResponseXContentUtils.nonNullColumns(columns, nullColumns, "columns")
266-
)
267-
: ResponseXContentUtils.allColumns(columns, "columns");
268-
Iterator<? extends ToXContent> valuesIt = ResponseXContentUtils.columnValues(this.columns, this.pages, columnar, nullColumns);
269-
Iterator<ToXContent> executionInfoRender = executionInfo != null && executionInfo.hasMetadataToReport()
270-
? ChunkedToXContentHelper.field("_clusters", executionInfo, params)
271-
: Collections.emptyIterator();
272233
return Iterators.concat(
273234
ChunkedToXContentHelper.startObject(),
274-
asyncPropertiesOrEmpty(),
275-
tookTime,
276-
meta,
277-
columnHeadings,
278-
ChunkedToXContentHelper.array("values", valuesIt),
279-
executionInfoRender,
280-
profileRenderer(params),
235+
conditionalChunkedXContent(isAsync, () -> ChunkedToXContentHelper.chunk((builder, p) -> {
236+
if (asyncExecutionId != null) {
237+
builder.field("id", asyncExecutionId);
238+
}
239+
builder.field("is_running", isRunning);
240+
return builder;
241+
})),
242+
conditionalChunkedXContent(
243+
executionInfo != null && executionInfo.overallTook() != null,
244+
() -> ChunkedToXContentHelper.chunk(
245+
(builder, p) -> builder //
246+
.field("took", executionInfo.overallTook().millis())
247+
.field(EsqlExecutionInfo.IS_PARTIAL_FIELD.getPreferredName(), executionInfo.isPartial())
248+
)
249+
),
250+
ChunkedToXContentHelper.chunk(
251+
(builder, p) -> builder //
252+
.field("documents_found", documentsFound)
253+
.field("values_loaded", valuesLoaded)
254+
),
255+
dropNullColumns
256+
? Iterators.concat(
257+
ResponseXContentUtils.allColumns(columns, "all_columns"),
258+
ResponseXContentUtils.nonNullColumns(columns, nullColumns, "columns")
259+
)
260+
: ResponseXContentUtils.allColumns(columns, "columns"),
261+
ChunkedToXContentHelper.array("values", ResponseXContentUtils.columnValues(this.columns, this.pages, columnar, nullColumns)),
262+
conditionalChunkedXContent(
263+
executionInfo != null && executionInfo.hasMetadataToReport(),
264+
() -> ChunkedToXContentHelper.field("_clusters", executionInfo, params)
265+
),
266+
conditionalChunkedXContent(
267+
profile != null,
268+
() -> Iterators.concat(
269+
ChunkedToXContentHelper.startObject("profile"), //
270+
ChunkedToXContentHelper.chunk((b, p) -> {
271+
if (executionInfo != null) {
272+
b.field("query", executionInfo.overallTimeSpan());
273+
b.field("planning", executionInfo.planningTimeSpan());
274+
}
275+
return b;
276+
}),
277+
ChunkedToXContentHelper.array("drivers", profile.drivers.iterator(), params),
278+
ChunkedToXContentHelper.endObject()
279+
)
280+
),
281281
ChunkedToXContentHelper.endObject()
282282
);
283283
}
284284

285-
private Iterator<ToXContent> profileRenderer(ToXContent.Params params) {
286-
if (profile == null) {
287-
return Collections.emptyIterator();
288-
}
289-
return Iterators.concat(ChunkedToXContentHelper.startObject("profile"), ChunkedToXContentHelper.chunk((b, p) -> {
290-
if (executionInfo != null) {
291-
b.field("query", executionInfo.overallTimeSpan());
292-
b.field("planning", executionInfo.planningTimeSpan());
293-
}
294-
return b;
295-
}), ChunkedToXContentHelper.array("drivers", profile.drivers.iterator(), params), ChunkedToXContentHelper.endObject());
285+
private Iterator<? extends ToXContent> conditionalChunkedXContent(
286+
boolean condition,
287+
Supplier<Iterator<? extends ToXContent>> chunkedXContent
288+
) {
289+
return condition ? chunkedXContent.get() : Collections.emptyIterator();
296290
}
297291

298292
public boolean[] nullColumns() {
@@ -396,41 +390,15 @@ public EsqlResponse responseInternal() {
396390
return esqlResponse;
397391
}
398392

399-
public static class Profile implements Writeable {
400-
private final List<DriverProfile> drivers;
401-
402-
public Profile(List<DriverProfile> drivers) {
403-
this.drivers = drivers;
404-
}
393+
public record Profile(List<DriverProfile> drivers) implements Writeable {
405394

406-
public Profile(StreamInput in) throws IOException {
407-
this.drivers = in.readCollectionAsImmutableList(DriverProfile::readFrom);
395+
public static Profile readFrom(StreamInput in) throws IOException {
396+
return new Profile(in.readCollectionAsImmutableList(DriverProfile::readFrom));
408397
}
409398

410399
@Override
411400
public void writeTo(StreamOutput out) throws IOException {
412401
out.writeCollection(drivers);
413402
}
414-
415-
@Override
416-
public boolean equals(Object o) {
417-
if (this == o) {
418-
return true;
419-
}
420-
if (o == null || getClass() != o.getClass()) {
421-
return false;
422-
}
423-
Profile profile = (Profile) o;
424-
return Objects.equals(drivers, profile.drivers);
425-
}
426-
427-
@Override
428-
public int hashCode() {
429-
return Objects.hash(drivers);
430-
}
431-
432-
List<DriverProfile> drivers() {
433-
return drivers;
434-
}
435403
}
436404
}

0 commit comments

Comments
 (0)