|
34 | 34 | import java.util.List; |
35 | 35 | import java.util.Objects; |
36 | 36 | import java.util.Optional; |
| 37 | +import java.util.function.Supplier; |
37 | 38 |
|
38 | 39 | import static org.elasticsearch.TransportVersions.ESQL_DOCUMENTS_FOUND_AND_VALUES_LOADED; |
39 | 40 |
|
@@ -122,7 +123,7 @@ static EsqlQueryResponse deserialize(BlockStreamInput in) throws IOException { |
122 | 123 | long documentsFound = in.getTransportVersion().onOrAfter(ESQL_DOCUMENTS_FOUND_AND_VALUES_LOADED) ? in.readVLong() : 0; |
123 | 124 | long valuesLoaded = in.getTransportVersion().onOrAfter(ESQL_DOCUMENTS_FOUND_AND_VALUES_LOADED) ? in.readVLong() : 0; |
124 | 125 | if (in.getTransportVersion().onOrAfter(TransportVersions.V_8_12_0)) { |
125 | | - profile = in.readOptionalWriteable(Profile::new); |
| 126 | + profile = in.readOptionalWriteable(Profile::readFrom); |
126 | 127 | } |
127 | 128 | boolean columnar = in.readBoolean(); |
128 | 129 | EsqlExecutionInfo executionInfo = null; |
@@ -224,75 +225,68 @@ public EsqlExecutionInfo getExecutionInfo() { |
224 | 225 | return executionInfo; |
225 | 226 | } |
226 | 227 |
|
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 | | - |
241 | 228 | @Override |
242 | 229 | public Iterator<? extends ToXContent> toXContentChunked(ToXContent.Params params) { |
243 | 230 | boolean dropNullColumns = params.paramAsBoolean(DROP_NULL_COLUMNS_OPTION, false); |
244 | 231 | boolean[] nullColumns = dropNullColumns ? nullColumns() : null; |
245 | 232 |
|
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(); |
272 | 233 | return Iterators.concat( |
273 | 234 | 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 | + ), |
281 | 281 | ChunkedToXContentHelper.endObject() |
282 | 282 | ); |
283 | 283 | } |
284 | 284 |
|
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(); |
296 | 290 | } |
297 | 291 |
|
298 | 292 | public boolean[] nullColumns() { |
@@ -396,41 +390,15 @@ public EsqlResponse responseInternal() { |
396 | 390 | return esqlResponse; |
397 | 391 | } |
398 | 392 |
|
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 { |
405 | 394 |
|
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)); |
408 | 397 | } |
409 | 398 |
|
410 | 399 | @Override |
411 | 400 | public void writeTo(StreamOutput out) throws IOException { |
412 | 401 | out.writeCollection(drivers); |
413 | 402 | } |
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 | | - } |
435 | 403 | } |
436 | 404 | } |
0 commit comments