Skip to content

Commit 3adb2c3

Browse files
committed
some minor cleanup in stat.internal package
1 parent a961a48 commit 3adb2c3

File tree

9 files changed

+74
-85
lines changed

9 files changed

+74
-85
lines changed

hibernate-core/src/main/java/org/hibernate/stat/internal/AbstractCacheableDataStatistics.java

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -114,17 +114,13 @@ public void incrementCacheRemoveCount() {
114114
NullnessUtil.castNonNull( cacheRemoveCount ).increment();
115115
}
116116

117-
protected void appendCacheStats(StringBuilder buf) {
118-
buf.append( ",cacheRegion=" ).append( cacheRegionName );
119-
120-
if ( cacheRegionName == null ) {
121-
return;
117+
protected void appendCacheStats(StringBuilder text) {
118+
text.append( ",cacheRegion=" ).append( cacheRegionName );
119+
if ( cacheRegionName != null ) {
120+
text.append( ",cacheHitCount=" ).append( getCacheHitCount() )
121+
.append( ",cacheMissCount=" ).append( getCacheMissCount() )
122+
.append( ",cachePutCount=" ).append( getCachePutCount() )
123+
.append( ",cacheRemoveCount=" ).append( getCacheRemoveCount() );
122124
}
123-
124-
buf.append( ",cacheHitCount=" ).append( getCacheHitCount() )
125-
.append( ",cacheMissCount=" ).append( getCacheMissCount() )
126-
.append( ",cachePutCount=" ).append( getCachePutCount() )
127-
.append( ",cacheRemoveCount=" ).append( getCacheRemoveCount() );
128-
129125
}
130126
}

hibernate-core/src/main/java/org/hibernate/stat/internal/CacheRegionStatisticsImpl.java

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -93,16 +93,15 @@ public void incrementRemoveCount() {
9393

9494
@Override
9595
public String toString() {
96-
String buf = "CacheRegionStatistics" +
97-
"[region=" + region.getName() +
98-
",hitCount=" + this.hitCount +
99-
",missCount=" + this.missCount +
100-
",putCount=" + this.putCount +
101-
",removeCount=" + this.removeCount +
102-
",elementCountInMemory=" + this.getElementCountInMemory() +
103-
",elementCountOnDisk=" + this.getElementCountOnDisk() +
104-
",sizeInMemory=" + this.getSizeInMemory() +
105-
']';
106-
return buf;
96+
return "CacheRegionStatistics"
97+
+ "[region=" + region.getName()
98+
+ ",hitCount=" + hitCount
99+
+ ",missCount=" + missCount
100+
+ ",putCount=" + putCount
101+
+ ",removeCount=" + removeCount
102+
+ ",elementCountInMemory=" + getElementCountInMemory()
103+
+ ",elementCountOnDisk=" + getElementCountOnDisk()
104+
+ ",sizeInMemory=" + getSizeInMemory()
105+
+ ']';
107106
}
108107
}

hibernate-core/src/main/java/org/hibernate/stat/internal/CollectionStatisticsImpl.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,15 +75,15 @@ void incrementRemoveCount() {
7575
}
7676

7777
public String toString() {
78-
final StringBuilder buffer = new StringBuilder()
78+
final var text = new StringBuilder()
7979
.append( "CollectionStatistics" )
8080
.append( "[collectionRole=" ).append( collectionRole )
8181
.append( ",loadCount=" ).append( this.loadCount )
8282
.append( ",fetchCount=" ).append( this.fetchCount )
8383
.append( ",recreateCount=" ).append( this.recreateCount )
8484
.append( ",removeCount=" ).append( this.removeCount )
8585
.append( ",updateCount=" ).append( this.updateCount );
86-
appendCacheStats( buffer );
87-
return buffer.append(']').toString();
86+
appendCacheStats( text );
87+
return text.append(']').toString();
8888
}
8989
}

hibernate-core/src/main/java/org/hibernate/stat/internal/EntityStatisticsImpl.java

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,10 @@ public class EntityStatisticsImpl extends AbstractCacheableDataStatistics implem
2727
private final LongAdder optimisticFailureCount = new LongAdder();
2828

2929
EntityStatisticsImpl(EntityPersister rootEntityDescriptor) {
30-
super(
31-
() -> rootEntityDescriptor.getCacheAccessStrategy() != null
32-
? rootEntityDescriptor.getCacheAccessStrategy().getRegion()
33-
: null
34-
);
30+
super( () -> {
31+
final var cache = rootEntityDescriptor.getCacheAccessStrategy();
32+
return cache != null ? cache.getRegion() : null;
33+
} );
3534
this.rootEntityName = rootEntityDescriptor.getRootEntityName();
3635
}
3736

@@ -92,7 +91,7 @@ void incrementOptimisticFailureCount() {
9291
}
9392

9493
public String toString() {
95-
final StringBuilder buffer = new StringBuilder()
94+
final var text = new StringBuilder()
9695
.append( "EntityStatistics" )
9796
.append( "[rootEntityName=" ).append( rootEntityName )
9897
.append( ",loadCount=" ).append( this.loadCount )
@@ -102,7 +101,7 @@ public String toString() {
102101
.append( ",deleteCount=" ).append( this.deleteCount )
103102
.append( ",fetchCount=" ).append( this.fetchCount )
104103
.append( ",optimisticLockFailureCount=" ).append( this.optimisticFailureCount );
105-
appendCacheStats( buffer );
106-
return buffer.append( ']' ).toString();
104+
appendCacheStats( text );
105+
return text.append( ']' ).toString();
107106
}
108107
}

hibernate-core/src/main/java/org/hibernate/stat/internal/NaturalIdStatisticsImpl.java

Lines changed: 27 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -30,23 +30,22 @@ public class NaturalIdStatisticsImpl extends AbstractCacheableDataStatistics imp
3030
private final Lock writeLock;
3131

3232
NaturalIdStatisticsImpl(EntityPersister rootEntityDescriptor) {
33-
super(
34-
() -> rootEntityDescriptor.getNaturalIdCacheAccessStrategy() != null
35-
? rootEntityDescriptor.getNaturalIdCacheAccessStrategy().getRegion()
36-
: null
37-
);
38-
this.rootEntityName = rootEntityDescriptor.getRootEntityName();
33+
super( () -> {
34+
final var cache = rootEntityDescriptor.getNaturalIdCacheAccessStrategy();
35+
return cache != null ? cache.getRegion() : null;
36+
} );
37+
rootEntityName = rootEntityDescriptor.getRootEntityName();
3938
final ReadWriteLock lock = new ReentrantReadWriteLock();
40-
this.readLock = lock.readLock();
41-
this.writeLock = lock.writeLock();
39+
readLock = lock.readLock();
40+
writeLock = lock.writeLock();
4241
}
4342

4443
/**
4544
* queries executed to the DB
4645
*/
4746
@Override
4847
public long getExecutionCount() {
49-
return this.executionCount.get();
48+
return executionCount.get();
5049
}
5150

5251
/**
@@ -57,16 +56,16 @@ public long getExecutionAvgTime() {
5756
// We write lock here to be sure that we always calculate the average time
5857
// with all updates from the executed applied: executionCount and totalExecutionTime
5958
// both used in the calculation
60-
this.writeLock.lock();
59+
writeLock.lock();
6160
try {
6261
long avgExecutionTime = 0;
6362
if ( this.executionCount.get() > 0 ) {
64-
avgExecutionTime = this.totalExecutionTime.get() / this.executionCount.get();
63+
avgExecutionTime = totalExecutionTime.get() / executionCount.get();
6564
}
6665
return avgExecutionTime;
6766
}
6867
finally {
69-
this.writeLock.unlock();
68+
writeLock.unlock();
7069
}
7170
}
7271

@@ -75,47 +74,51 @@ public long getExecutionAvgTime() {
7574
*/
7675
@Override
7776
public long getExecutionMaxTime() {
78-
return this.executionMaxTime.get();
77+
return executionMaxTime.get();
7978
}
8079

8180
/**
8281
* min time in ms taken by the execution of this query onto the DB
8382
*/
8483
@Override
8584
public long getExecutionMinTime() {
86-
return this.executionMinTime.get();
85+
return executionMinTime.get();
8786
}
8887

8988
void queryExecuted(long time) {
9089
// read lock is enough, concurrent updates are supported by the underlying type AtomicLong
9190
// this only guards executed(long, long) to be called, when another thread is executing getExecutionAvgTime()
92-
this.readLock.lock();
91+
readLock.lock();
9392
try {
94-
// Less chances for a context switch
93+
// Less chance for a context switch
9594
//noinspection StatementWithEmptyBody
96-
for ( long old = this.executionMinTime.get(); time < old && !this.executionMinTime.compareAndSet( old, time ); old = this.executionMinTime.get() ) {
95+
for ( long old = executionMinTime.get();
96+
time < old && !executionMinTime.compareAndSet( old, time );
97+
old = executionMinTime.get() ) {
9798
}
9899
//noinspection StatementWithEmptyBody
99-
for ( long old = this.executionMaxTime.get(); time > old && !this.executionMaxTime.compareAndSet( old, time ); old = this.executionMaxTime.get() ) {
100+
for ( long old = executionMaxTime.get();
101+
time > old && !executionMaxTime.compareAndSet( old, time );
102+
old = executionMaxTime.get() ) {
100103
}
101-
this.executionCount.getAndIncrement();
102-
this.totalExecutionTime.addAndGet( time );
104+
executionCount.getAndIncrement();
105+
totalExecutionTime.addAndGet( time );
103106
}
104107
finally {
105-
this.readLock.unlock();
108+
readLock.unlock();
106109
}
107110
}
108111

109112
@Override
110113
public String toString() {
111-
final StringBuilder buf = new StringBuilder()
114+
final var text = new StringBuilder()
112115
.append( "NaturalIdCacheStatistics" )
113116
.append( "[rootEntityName=" ).append( rootEntityName )
114117
.append( ",executionCount=" ).append( this.executionCount )
115118
.append( ",executionAvgTime=" ).append( this.getExecutionAvgTime() )
116119
.append( ",executionMinTime=" ).append( this.executionMinTime )
117120
.append( ",executionMaxTime=" ).append( this.executionMaxTime );
118-
appendCacheStats( buf );
119-
return buf.append( ']' ).toString();
121+
appendCacheStats( text );
122+
return text.append( ']' ).toString();
120123
}
121124
}

hibernate-core/src/main/java/org/hibernate/stat/internal/QueryStatisticsImpl.java

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -207,17 +207,17 @@ void incrementPlanCacheMissCount() {
207207

208208
public String toString() {
209209
return "QueryStatistics"
210-
+ "[query=" + query
211-
+ ",cacheHitCount=" + this.cacheHitCount
212-
+ ",cacheMissCount=" + this.cacheMissCount
213-
+ ",cachePutCount=" + this.cachePutCount
214-
+ ",planCacheHitCount=" + this.planCacheHitCount
215-
+ ",planCacheMissCount=" + this.planCacheMissCount
216-
+ ",executionCount=" + this.executionCount
217-
+ ",executionRowCount=" + this.executionRowCount
218-
+ ",executionAvgTime=" + this.getExecutionAvgTime()
219-
+ ",executionMaxTime=" + this.executionMaxTime
220-
+ ",executionMinTime=" + this.executionMinTime
221-
+ ']';
210+
+ "[query=" + query
211+
+ ",cacheHitCount=" + cacheHitCount
212+
+ ",cacheMissCount=" + cacheMissCount
213+
+ ",cachePutCount=" + cachePutCount
214+
+ ",planCacheHitCount=" + planCacheHitCount
215+
+ ",planCacheMissCount=" + planCacheMissCount
216+
+ ",executionCount=" + executionCount
217+
+ ",executionRowCount=" + executionRowCount
218+
+ ",executionAvgTime=" + getExecutionAvgTime()
219+
+ ",executionMaxTime=" + executionMaxTime
220+
+ ",executionMinTime=" + executionMinTime
221+
+ ']';
222222
}
223223
}

hibernate-core/src/main/java/org/hibernate/stat/internal/SessionStatisticsImpl.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,10 @@ public Set<?> getCollectionKeys() {
3838
}
3939

4040
public String toString() {
41-
return "SessionStatistics[" +
42-
"entity count=" + getEntityCount() +
43-
",collection count=" + getCollectionCount() +
44-
']';
41+
return "SessionStatistics["
42+
+ "entity count=" + getEntityCount()
43+
+ ",collection count=" + getCollectionCount()
44+
+ ']';
4545
}
4646

4747
}

hibernate-core/src/main/java/org/hibernate/stat/internal/StatisticsImpl.java

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -736,11 +736,10 @@ public void queryExecuted(String hql, int rows, long time) {
736736
boolean isLongestQuery;
737737
//noinspection StatementWithEmptyBody
738738
for ( long old = queryExecutionMaxTime.get();
739-
( isLongestQuery = time > old ) && ( ! queryExecutionMaxTime.compareAndSet( old, time ) );
739+
( isLongestQuery = time > old ) && !queryExecutionMaxTime.compareAndSet( old, time );
740740
old = queryExecutionMaxTime.get() ) {
741741
// nothing to do here given the odd loop structure...
742742
}
743-
744743
if ( isLongestQuery ) {
745744
queryExecutionMaxTimeQueryString = hql;
746745
}
@@ -1018,17 +1017,14 @@ private NaturalIdStatisticsImpl instantiateNaturalStatistics(final String entity
10181017

10191018
private CacheRegionStatisticsImpl instantiateCacheRegionStatistics(final String regionName) {
10201019
final Region region = cache.getRegion( regionName );
1021-
10221020
if ( region == null ) {
10231021
throw new IllegalArgumentException( "Unknown cache region : " + regionName );
10241022
}
1025-
10261023
if ( region instanceof QueryResultsRegion ) {
10271024
throw new IllegalArgumentException(
10281025
"Region name [" + regionName + "] referred to a query result region, not a domain data region"
10291026
);
10301027
}
1031-
10321028
return new CacheRegionStatisticsImpl( region );
10331029
}
10341030

@@ -1038,18 +1034,14 @@ private CacheRegionStatisticsImpl instantiateCacheRegionStatsForQueryResults(fin
10381034

10391035
private @Nullable CacheRegionStatisticsImpl createCacheRegionStatistics(final String regionName) {
10401036
Region region = cache.getRegion( regionName );
1041-
10421037
if ( region == null ) {
1043-
10441038
if ( !queryCacheEnabled ) {
10451039
return null;
10461040
}
1047-
10481041
// this is the pre-5.3 behavior. and since this is a pre-5.3 method it should behave consistently
10491042
// NOTE that this method is deprecated
10501043
region = cache.getQueryResultsCache( regionName ).getRegion();
10511044
}
1052-
10531045
return new CacheRegionStatisticsImpl( region );
10541046
}
10551047

hibernate-core/src/main/java/org/hibernate/stat/internal/StatisticsInitiator.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@ public StatisticsImplementor initiateService(SessionFactoryServiceInitiatorConte
4040
final Object configValue =
4141
context.getServiceRegistry().requireService( ConfigurationService.class )
4242
.getSettings().get( STATS_BUILDER );
43-
final StatisticsFactory statisticsFactory = statisticsFactory( configValue, context.getServiceRegistry() );
44-
final StatisticsImplementor statistics = statisticsFactory.buildStatistics( context.getSessionFactory() );
43+
final var statisticsFactory = statisticsFactory( configValue, context.getServiceRegistry() );
44+
final var statistics = statisticsFactory.buildStatistics( context.getSessionFactory() );
4545
final boolean enabled = context.getSessionFactoryOptions().isStatisticsEnabled();
4646
log.statisticsInitialized();
4747
statistics.setStatisticsEnabled( enabled );
@@ -58,7 +58,7 @@ else if ( configValue instanceof StatisticsFactory factory ) {
5858
}
5959
else {
6060
// assume it names the factory class
61-
final ClassLoaderService classLoaderService = registry.requireService( ClassLoaderService.class );
61+
final var classLoaderService = registry.requireService( ClassLoaderService.class );
6262
try {
6363
return (StatisticsFactory) classLoaderService.classForName( configValue.toString() ).newInstance();
6464
}

0 commit comments

Comments
 (0)