Skip to content

Commit 0f8306a

Browse files
committed
[refactor] Clean up code, fix sonar warnings
Signed-off-by: Patrick Reinhart <[email protected]>
1 parent 824d65a commit 0f8306a

File tree

1 file changed

+32
-52
lines changed

1 file changed

+32
-52
lines changed

exist-core/src/main/java/org/exist/xquery/PerformanceStatsImpl.java

Lines changed: 32 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -48,21 +48,39 @@
4848
*/
4949
@NotThreadSafe
5050
public class PerformanceStatsImpl implements PerformanceStats {
51-
public static final String CDATA = "CDATA";
52-
public static final String SOURCE = "source";
53-
public static final String ELAPSED = "elapsed";
54-
public static final String CALLS = "calls";
55-
public static final String QUERY = "query";
56-
public static final String NAME = "name";
57-
public static final String TYPE = "type";
51+
private static final String CALLS = "calls";
52+
private static final String CDATA = "CDATA";
53+
private static final String ELAPSED = "elapsed";
54+
private static final String NAME = "name";
55+
private static final String OPTIMIZATION_LEVEL = "optimization-level";
56+
private static final String QUERY = "query";
57+
private static final String SOURCE = "source";
58+
private static final String TYPE = "type";
5859

59-
private static class IndexStats {
60+
private final Map<String, QueryStats> queries = new HashMap<>();
61+
private final Map<FunctionStats, FunctionStats> functions = new HashMap<>();
62+
private final Map<IndexStats, IndexStats> indexStats = new HashMap<>();
63+
private final Set<OptimizationStats> optimizations = new HashSet<>();
64+
private final Enabler enabler;
65+
66+
private boolean enabled;
6067

68+
public PerformanceStatsImpl(final boolean enabled) {
69+
this(enabled, x -> x);
70+
}
71+
72+
public PerformanceStatsImpl(final boolean enabled, final Enabler enabler) {
73+
this.enabled = enabled;
74+
this.enabler = enabler;
75+
}
76+
77+
private static class IndexStats {
6178
final String source;
6279
final String indexType;
6380
final int line;
6481
final int column;
6582
final IndexOptimizationLevel indexOptimizationLevel;
83+
6684
int usageCount = 1;
6785
long executionTime = 0;
6886

@@ -102,13 +120,13 @@ public boolean equals(final Object obj) {
102120
}
103121

104122
private static class QueryStats {
105-
106123
final String source;
124+
107125
long executionTime = 0;
108126
int callCount = 1;
109127

110128
QueryStats(final String source) {
111-
this.source = (source != null ? source : "");
129+
this.source = source == null ? "" : source;
112130
}
113131

114132
public static QueryStats copy(final QueryStats other) {
@@ -169,36 +187,16 @@ public boolean equals(final Object obj) {
169187
}
170188

171189
@ThreadSafe
172-
private static class OptimizationStats {
173-
final String source;
174-
final OptimizationType type;
175-
final int line;
176-
final int column;
177-
190+
private record OptimizationStats(String source, OptimizationType type, int line, int column) {
178191
OptimizationStats(final String source, final OptimizationType type, final int line, final int column) {
179-
this.source = source != null ? source : "";
192+
this.source = source == null ? "" : source;
180193
this.type = type;
181194
this.line = line;
182195
this.column = column;
183196
}
184-
185-
@Override
186-
public int hashCode() {
187-
return 32 * type.hashCode() + source.hashCode() + line + column;
188-
}
189-
190-
@Override
191-
public boolean equals(final Object obj) {
192-
if (obj instanceof OptimizationStats other) {
193-
return source.equals(other.source) && type == other.type &&
194-
line == other.line && column == other.column;
195-
}
196-
return false;
197-
}
198197
}
199198

200199
private static class CompareByTime implements Comparator<FunctionStats> {
201-
202200
@Override
203201
public int compare(final FunctionStats o1, final FunctionStats o2) {
204202
return Long.compare(o1.executionTime, o2.executionTime);
@@ -210,24 +208,6 @@ public interface Enabler {
210208
boolean enabled(final boolean enabled);
211209
}
212210

213-
private final Map<String, QueryStats> queries = new HashMap<>();
214-
private final Map<FunctionStats, FunctionStats> functions = new HashMap<>();
215-
private final Map<IndexStats, IndexStats> indexStats = new HashMap<>();
216-
private final Set<OptimizationStats> optimizations = new HashSet<>();
217-
218-
private final Enabler enabler;
219-
220-
private boolean enabled = false;
221-
222-
public PerformanceStatsImpl(final boolean enabled) {
223-
this(enabled, x -> x);
224-
}
225-
226-
public PerformanceStatsImpl(final boolean enabled, final Enabler enabler) {
227-
this.enabled = enabled;
228-
this.enabler = enabler;
229-
}
230-
231211
@Override
232212
public void setEnabled(final boolean enabled) {
233213
this.enabled = enabled;
@@ -388,7 +368,7 @@ public void serialize(final MemTreeBuilder builder) {
388368
attrs.addAttribute("", NAME, NAME, CDATA, stats.qname.getStringValue());
389369
attrs.addAttribute("", ELAPSED, ELAPSED, CDATA, Double.toString(stats.executionTime / 1000.0));
390370
attrs.addAttribute("", CALLS, CALLS, CDATA, Integer.toString(stats.callCount));
391-
if (stats.source != null) {
371+
if (!stats.source.isEmpty()) {
392372
attrs.addAttribute("", SOURCE, SOURCE, CDATA, stats.source);
393373
}
394374
builder.startElement(new QName("function", XML_NAMESPACE, XML_PREFIX), attrs);
@@ -401,7 +381,7 @@ public void serialize(final MemTreeBuilder builder) {
401381
attrs.addAttribute("", SOURCE, SOURCE, CDATA, "%s [%s:%s]".formatted(stats.source, stats.line, stats.column));
402382
attrs.addAttribute("", ELAPSED, ELAPSED, CDATA, Double.toString(stats.executionTime / 1000.0));
403383
attrs.addAttribute("", CALLS, CALLS, CDATA, Integer.toString(stats.usageCount));
404-
attrs.addAttribute("", "optimization-level", "optimization", CDATA, stats.indexOptimizationLevel.name());
384+
attrs.addAttribute("", OPTIMIZATION_LEVEL, OPTIMIZATION_LEVEL, CDATA, stats.indexOptimizationLevel.name());
405385
builder.startElement(new QName("index", XML_NAMESPACE, XML_PREFIX), attrs);
406386
builder.endElement();
407387
}

0 commit comments

Comments
 (0)