From 3dac7f2e457756f7226db7a6339fe71be9ac4d38 Mon Sep 17 00:00:00 2001 From: Daniel Mitterdorfer Date: Thu, 1 Feb 2024 07:52:42 +0100 Subject: [PATCH] Replace lambdas with dedicated mappers --- .../profiling/GetFlamegraphResponse.java | 129 +++++++++++++----- 1 file changed, 93 insertions(+), 36 deletions(-) diff --git a/x-pack/plugin/profiling/src/main/java/org/elasticsearch/xpack/profiling/GetFlamegraphResponse.java b/x-pack/plugin/profiling/src/main/java/org/elasticsearch/xpack/profiling/GetFlamegraphResponse.java index 54779aa4e3a53..cb49f98dcb709 100644 --- a/x-pack/plugin/profiling/src/main/java/org/elasticsearch/xpack/profiling/GetFlamegraphResponse.java +++ b/x-pack/plugin/profiling/src/main/java/org/elasticsearch/xpack/profiling/GetFlamegraphResponse.java @@ -15,11 +15,14 @@ import org.elasticsearch.common.xcontent.ChunkedToXContentObject; import org.elasticsearch.telemetry.tracing.TraceLogger; import org.elasticsearch.xcontent.ToXContent; +import org.elasticsearch.xcontent.XContent; +import org.elasticsearch.xcontent.XContentBuilder; import java.io.IOException; import java.util.Iterator; import java.util.List; import java.util.Map; +import java.util.function.Function; public class GetFlamegraphResponse extends ActionResponse implements ChunkedToXContentObject { private final int size; @@ -173,55 +176,109 @@ public long getTotalSamples() { @Override public Iterator toXContentChunked(ToXContent.Params params) { + Mapper stringMapper = new Mapper<>(); + Mapper intMapper = new Mapper<>(); + Mapper boolMapper = new Mapper<>(); + Mapper longMapper = new Mapper<>(); + Mapper doubleMapper = new Mapper<>(); + MapValueMapper edgeValueMapper = new MapValueMapper<>(); + return Iterators.concat( ChunkedToXContentHelper.startObject(), - traced("Edges", ChunkedToXContentHelper.array( + traced( "Edges", - Iterators.flatMap( - edges.iterator(), - perNodeEdges -> Iterators.concat( - ChunkedToXContentHelper.startArray(), - Iterators.map(perNodeEdges.entrySet().iterator(), edge -> (b, p) -> b.value(edge.getValue())), - ChunkedToXContentHelper.endArray() + ChunkedToXContentHelper.array( + "Edges", + Iterators.flatMap( + edges.iterator(), + perNodeEdges -> Iterators.concat( + ChunkedToXContentHelper.startArray(), + Iterators.map(perNodeEdges.entrySet().iterator(), edgeValueMapper), + ChunkedToXContentHelper.endArray() + ) ) ) - )), - traced("FileID", ChunkedToXContentHelper.array("FileID", Iterators.map(fileIds.iterator(), e -> (b, p) -> b.value(e)))), - traced("FrameType", ChunkedToXContentHelper.array("FrameType", Iterators.map(frameTypes.iterator(), e -> (b, p) -> b.value(e)))), - traced("Inline", ChunkedToXContentHelper.array("Inline", Iterators.map(inlineFrames.iterator(), e -> (b, p) -> b.value(e)))), - traced("ExeFilename", ChunkedToXContentHelper.array("ExeFilename", Iterators.map(fileNames.iterator(), e -> (b, p) -> b.value(e)))), - traced("AddressOrLine", ChunkedToXContentHelper.array("AddressOrLine", Iterators.map(addressOrLines.iterator(), e -> (b, p) -> b.value(e)))), - traced("FunctionName", ChunkedToXContentHelper.array("FunctionName", Iterators.map(functionNames.iterator(), e -> (b, p) -> b.value(e)))), - traced("FunctionOffset", ChunkedToXContentHelper.array("FunctionOffset", Iterators.map(functionOffsets.iterator(), e -> (b, p) -> b.value(e)))), - traced("SourceFilename", ChunkedToXContentHelper.array("SourceFilename", Iterators.map(sourceFileNames.iterator(), e -> (b, p) -> b.value(e)))), - traced("SourceLine", ChunkedToXContentHelper.array("SourceLine", Iterators.map(sourceLines.iterator(), e -> (b, p) -> b.value(e)))), - traced("CountInclusive", ChunkedToXContentHelper.array("CountInclusive", Iterators.map(countInclusive.iterator(), e -> (b, p) -> b.value(e)))), - traced("CountExclusive", ChunkedToXContentHelper.array("CountExclusive", Iterators.map(countExclusive.iterator(), e -> (b, p) -> b.value(e)))), - traced("AnnualCO2TonsInclusive", ChunkedToXContentHelper.array( + ), + traced("FileID", ChunkedToXContentHelper.array("FileID", Iterators.map(fileIds.iterator(), stringMapper))), + traced("FrameType", ChunkedToXContentHelper.array("FrameType", Iterators.map(frameTypes.iterator(), intMapper))), + traced("Inline", ChunkedToXContentHelper.array("Inline", Iterators.map(inlineFrames.iterator(), boolMapper))), + traced("ExeFilename", ChunkedToXContentHelper.array("ExeFilename", Iterators.map(fileNames.iterator(), stringMapper))), + traced("AddressOrLine", ChunkedToXContentHelper.array("AddressOrLine", Iterators.map(addressOrLines.iterator(), intMapper))), + traced("FunctionName", ChunkedToXContentHelper.array("FunctionName", Iterators.map(functionNames.iterator(), stringMapper))), + traced("FunctionOffset", ChunkedToXContentHelper.array("FunctionOffset", Iterators.map(functionOffsets.iterator(), intMapper))), + traced( + "SourceFilename", + ChunkedToXContentHelper.array("SourceFilename", Iterators.map(sourceFileNames.iterator(), stringMapper)) + ), + traced("SourceLine", ChunkedToXContentHelper.array("SourceLine", Iterators.map(sourceLines.iterator(), intMapper))), + traced("CountInclusive", ChunkedToXContentHelper.array("CountInclusive", Iterators.map(countInclusive.iterator(), longMapper))), + traced("CountExclusive", ChunkedToXContentHelper.array("CountExclusive", Iterators.map(countExclusive.iterator(), longMapper))), + traced( "AnnualCO2TonsInclusive", - Iterators.map(annualCO2TonsInclusive.iterator(), e -> (b, p) -> b.value(e)) - )), - traced("AnnualCO2TonsExclusive", ChunkedToXContentHelper.array( + ChunkedToXContentHelper.array("AnnualCO2TonsInclusive", Iterators.map(annualCO2TonsInclusive.iterator(), doubleMapper)) + ), + traced( "AnnualCO2TonsExclusive", - Iterators.map(annualCO2TonsExclusive.iterator(), e -> (b, p) -> b.value(e)) - )), - traced("AnnualCostsUSDInclusive", ChunkedToXContentHelper.array( + ChunkedToXContentHelper.array("AnnualCO2TonsExclusive", Iterators.map(annualCO2TonsExclusive.iterator(), doubleMapper)) + ), + traced( "AnnualCostsUSDInclusive", - Iterators.map(annualCostsUSDInclusive.iterator(), e -> (b, p) -> b.value(e)) - )), - traced("AnnualCostsUSDExclusive", ChunkedToXContentHelper.array( + ChunkedToXContentHelper.array("AnnualCostsUSDInclusive", Iterators.map(annualCostsUSDInclusive.iterator(), doubleMapper)) + ), + traced( "AnnualCostsUSDExclusive", - Iterators.map(annualCostsUSDExclusive.iterator(), e -> (b, p) -> b.value(e)) - )), - Iterators.single((b, p) -> b.field("Size", size)), - Iterators.single((b, p) -> b.field("SamplingRate", samplingRate)), - Iterators.single((b, p) -> b.field("SelfCPU", selfCPU)), - Iterators.single((b, p) -> b.field("TotalCPU", totalCPU)), - Iterators.single((b, p) -> b.field("TotalSamples", totalSamples)), + ChunkedToXContentHelper.array("AnnualCostsUSDExclusive", Iterators.map(annualCostsUSDExclusive.iterator(), doubleMapper)) + ), + traced("Size", Iterators.single((b, p) -> b.field("Size", size))), + traced("SamplingRate", Iterators.single((b, p) -> b.field("SamplingRate", samplingRate))), + traced("SelfCPU", Iterators.single((b, p) -> b.field("SelfCPU", selfCPU))), + traced("TotalCPU", Iterators.single((b, p) -> b.field("TotalCPU", totalCPU))), + traced("TotalSamples", Iterators.single((b, p) -> b.field("TotalSamples", totalSamples))), ChunkedToXContentHelper.endObject() ); } + private static final class Mapper implements Function, ToXContent { + private T value; + + @Override + public ToXContent apply(T v) { + value = v; + return this; + // return new ValueToXContent<>(s); + } + + @Override + public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { + return builder.value(value); + } + } + + private static final class MapValueMapper implements Function, ToXContent>, ToXContent { + private V value; + + @Override + public ToXContent apply(Map.Entry entry) { + value = entry.getValue(); + return this; + // return new ValueToXContent<>(s); + } + + @Override + public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { + return builder.value(value); + } + } + /* + + private record ValueToXContent(T value) implements ToXContent { + @Override + public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { + return builder.value(value); + } + } + */ + private static Iterator traced(String name, Iterator iterator) { return new Iterator<>() { private boolean traceStarted = false;