Skip to content

Commit

Permalink
Replace lambdas with dedicated mappers
Browse files Browse the repository at this point in the history
  • Loading branch information
danielmitterdorfer committed Feb 1, 2024
1 parent 69fa5c1 commit 3dac7f2
Showing 1 changed file with 93 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -173,55 +176,109 @@ public long getTotalSamples() {

@Override
public Iterator<? extends ToXContent> toXContentChunked(ToXContent.Params params) {
Mapper<String> stringMapper = new Mapper<>();
Mapper<Integer> intMapper = new Mapper<>();
Mapper<Boolean> boolMapper = new Mapper<>();
Mapper<Long> longMapper = new Mapper<>();
Mapper<Double> doubleMapper = new Mapper<>();
MapValueMapper<String, Integer> 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<T> implements Function<T, ToXContent>, 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<K, V> implements Function<Map.Entry<K, V>, ToXContent>, ToXContent {
private V value;

@Override
public ToXContent apply(Map.Entry<K, V> 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>(T value) implements ToXContent {
@Override
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
return builder.value(value);
}
}
*/

private static <T> Iterator<? extends ToXContent> traced(String name, Iterator<? extends ToXContent> iterator) {
return new Iterator<>() {
private boolean traceStarted = false;
Expand Down

0 comments on commit 3dac7f2

Please sign in to comment.