Skip to content

Commit 4df04a2

Browse files
committed
8366809: JFR: Use factory for aggregator functions
Reviewed-by: mgronlun
1 parent fd30ae9 commit 4df04a2

File tree

3 files changed

+38
-30
lines changed

3 files changed

+38
-30
lines changed

src/jdk.jfr/share/classes/jdk/jfr/internal/query/Field.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import jdk.jfr.internal.query.Configuration.Truncate;
3434
import jdk.jfr.internal.query.Query.Grouper;
3535
import jdk.jfr.internal.query.Query.OrderElement;
36+
import jdk.jfr.internal.query.Function.FunctionFactory;
3637

3738
/**
3839
* Field is the core class of the package.
@@ -137,6 +138,8 @@ final class Field {
137138

138139
public int precision = -1;
139140

141+
public FunctionFactory functionFactory;
142+
140143
public Field(FilteredType type, String name) {
141144
this.type = type;
142145
this.name = name;

src/jdk.jfr/share/classes/jdk/jfr/internal/query/Function.java

Lines changed: 31 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2023, 2024, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2023, 2025, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -35,45 +35,49 @@
3535

3636
abstract class Function {
3737

38+
interface FunctionFactory {
39+
Function newFunction();
40+
}
41+
3842
public abstract void add(Object value);
3943

4044
public abstract Object result();
4145

42-
public static Function create(Field field) {
46+
public static FunctionFactory createFactory(Field field) {
4347
Aggregator aggregator = field.aggregator;
4448

4549
if (field.grouper != null || aggregator == Aggregator.MISSING) {
46-
return new FirstNonNull();
50+
return () -> new FirstNonNull();
4751
}
4852
if (aggregator == Aggregator.LIST) {
49-
return new Container(new ArrayList<>());
53+
return () -> new Container(new ArrayList<>());
5054
}
5155

5256
if (aggregator == Aggregator.SET) {
53-
return new Container(new LinkedHashSet<>());
57+
return () -> new Container(new LinkedHashSet<>());
5458
}
5559

5660
if (aggregator == Aggregator.DIFFERENCE) {
5761
if (field.timestamp) {
58-
return new TimeDifference();
62+
return () -> new TimeDifference();
5963
} else {
60-
return new Difference();
64+
return () -> new Difference();
6165
}
6266
}
6367

6468
if (aggregator == Aggregator.STANDARD_DEVIATION) {
6569
if (field.timespan) {
66-
return new TimespanFunction(new StandardDeviation());
70+
return () -> new TimespanFunction(new StandardDeviation());
6771
} else {
68-
return new StandardDeviation();
72+
return () -> new StandardDeviation();
6973
}
7074
}
7175

7276
if (aggregator == Aggregator.MEDIAN) {
7377
if (field.timespan) {
74-
return new TimespanFunction(new Median());
78+
return () -> new TimespanFunction(new Median());
7579
} else {
76-
return new Median();
80+
return () -> new Median();
7781
}
7882
}
7983

@@ -83,7 +87,6 @@ public static Function create(Field field) {
8387

8488
if (aggregator == Aggregator.P95) {
8589
return createPercentile(field, 0.95);
86-
8790
}
8891
if (aggregator == Aggregator.P99) {
8992
return createPercentile(field, 0.99);
@@ -92,46 +95,46 @@ public static Function create(Field field) {
9295
return createPercentile(field, 0.999);
9396
}
9497
if (aggregator == Aggregator.MAXIMUM) {
95-
return new Maximum();
98+
return () -> new Maximum();
9699
}
97100
if (aggregator == Aggregator.MINIMUM) {
98-
return new Minimum();
101+
return () -> new Minimum();
99102
}
100103
if (aggregator == Aggregator.SUM) {
101104
if (field.timespan) {
102-
return new SumDuration();
105+
return () -> new SumDuration();
103106
}
104107
if (field.fractionalType) {
105-
return new SumDouble();
108+
return () -> new SumDouble();
106109
}
107110
if (field.integralType) {
108-
return new SumLong();
111+
return () -> new SumLong();
109112
}
110113
}
111114

112115
if (aggregator == Aggregator.FIRST) {
113-
return new First();
116+
return () -> new First();
114117
}
115118
if (aggregator == Aggregator.LAST_BATCH) {
116-
return new LastBatch(field);
119+
return () -> new LastBatch(field);
117120
}
118121
if (aggregator == Aggregator.LAST) {
119-
return new Last();
122+
return () -> new Last();
120123
}
121124
if (aggregator == Aggregator.AVERAGE) {
122125
if (field.timespan) {
123-
return new AverageDuration();
126+
return () -> new AverageDuration();
124127
} else {
125-
return new Average();
128+
return () -> new Average();
126129
}
127130
}
128131
if (aggregator == Aggregator.COUNT) {
129-
return new Count();
132+
return () -> new Count();
130133
}
131134
if (aggregator == Aggregator.UNIQUE) {
132-
return new Unique();
135+
return () -> new Unique();
133136
}
134-
return new Null();
137+
return () -> new Null();
135138
}
136139

137140
// **** AVERAGE ****
@@ -507,12 +510,11 @@ public Object result() {
507510
}
508511

509512
// **** PERCENTILE ****
510-
private static Function createPercentile(Field field, double percentile) {
511-
Percentile p = new Percentile(percentile);
513+
private static FunctionFactory createPercentile(Field field, double percentile) {
512514
if (field.timespan) {
513-
return new TimespanFunction(p);
515+
return () -> new TimespanFunction(new Percentile(percentile));
514516
} else {
515-
return p;
517+
return () -> new Percentile(percentile);
516518
}
517519
}
518520

src/jdk.jfr/share/classes/jdk/jfr/internal/query/Histogram.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,9 @@ public boolean equals(Object object) {
122122

123123
public void addFields(List<Field> fields) {
124124
this.fields.addAll(fields);
125+
for (Field field : fields) {
126+
field.functionFactory = Function.createFactory(field);
127+
}
125128
}
126129

127130
public void add(RecordedEvent e, FilteredType type, List<Field> sourceFields) {
@@ -168,7 +171,7 @@ public List<Row> toRows() {
168171
private Function[] createFunctions() {
169172
Function[] functions = new Function[fields.size()];
170173
for (int i = 0; i < functions.length; i++) {
171-
functions[i] = Function.create(fields.get(i));
174+
functions[i] = fields.get(i).functionFactory.newFunction();
172175
}
173176
return functions;
174177
}

0 commit comments

Comments
 (0)