Skip to content

Commit b9b2d06

Browse files
committed
misc cleanups in statistics package
1 parent 9e20bd0 commit b9b2d06

10 files changed

+63
-79
lines changed

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

Lines changed: 23 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -24,20 +24,20 @@ public abstract class AbstractCacheableDataStatistics implements CacheableDataSt
2424
private final @Nullable LongAdder cacheRemoveCount;
2525

2626
public AbstractCacheableDataStatistics(Supplier<@Nullable Region> regionSupplier) {
27-
final Region region = regionSupplier.get();
27+
final var region = regionSupplier.get();
2828
if ( region == null ) {
29-
this.cacheRegionName = null;
30-
this.cacheHitCount = null;
31-
this.cacheMissCount = null;
32-
this.cachePutCount = null;
33-
this.cacheRemoveCount = null;
29+
cacheRegionName = null;
30+
cacheHitCount = null;
31+
cacheMissCount = null;
32+
cachePutCount = null;
33+
cacheRemoveCount = null;
3434
}
3535
else {
36-
this.cacheRegionName = region.getName();
37-
this.cacheHitCount = new LongAdder();
38-
this.cacheMissCount = new LongAdder();
39-
this.cachePutCount = new LongAdder();
40-
this.cacheRemoveCount = new LongAdder();
36+
cacheRegionName = region.getName();
37+
cacheHitCount = new LongAdder();
38+
cacheMissCount = new LongAdder();
39+
cachePutCount = new LongAdder();
40+
cacheRemoveCount = new LongAdder();
4141
}
4242
}
4343

@@ -48,38 +48,34 @@ public AbstractCacheableDataStatistics(Supplier<@Nullable Region> regionSupplier
4848

4949
@Override
5050
public long getCacheHitCount() {
51-
if ( cacheRegionName == null ) {
52-
return NOT_CACHED_COUNT;
53-
}
51+
return cacheRegionName == null
52+
? NOT_CACHED_COUNT
53+
: NullnessUtil.castNonNull( cacheHitCount ).sum();
5454

55-
return NullnessUtil.castNonNull( cacheHitCount ).sum();
5655
}
5756

5857
@Override
5958
public long getCachePutCount() {
60-
if ( cacheRegionName == null ) {
61-
return NOT_CACHED_COUNT;
62-
}
59+
return cacheRegionName == null
60+
? NOT_CACHED_COUNT
61+
: NullnessUtil.castNonNull( cachePutCount ).sum();
6362

64-
return NullnessUtil.castNonNull( cachePutCount ).sum();
6563
}
6664

6765
@Override
6866
public long getCacheMissCount() {
69-
if ( cacheRegionName == null ) {
70-
return NOT_CACHED_COUNT;
71-
}
67+
return cacheRegionName == null
68+
? NOT_CACHED_COUNT
69+
: NullnessUtil.castNonNull( cacheMissCount ).sum();
7270

73-
return NullnessUtil.castNonNull( cacheMissCount ).sum();
7471
}
7572

7673
@Override
7774
public long getCacheRemoveCount() {
78-
if ( cacheRegionName == null ) {
79-
return NOT_CACHED_COUNT;
80-
}
75+
return cacheRegionName == null
76+
? NOT_CACHED_COUNT
77+
: NullnessUtil.castNonNull( cacheRemoveCount ).sum();
8178

82-
return NullnessUtil.castNonNull( cacheRemoveCount ).sum();
8379
}
8480

8581
public void incrementCacheHitCount() {

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

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,11 @@ public class CollectionStatisticsImpl extends AbstractCacheableDataStatistics im
2525
private final LongAdder recreateCount = new LongAdder();
2626

2727
CollectionStatisticsImpl(CollectionPersister persister) {
28-
super(
29-
() -> persister.getCacheAccessStrategy() != null
30-
? persister.getCacheAccessStrategy().getRegion()
31-
: null
32-
);
33-
34-
this.collectionRole = persister.getRole();
28+
super( () -> {
29+
final var cache = persister.getCacheAccessStrategy();
30+
return cache == null ? null : cache.getRegion();
31+
} );
32+
collectionRole = persister.getRole();
3533
}
3634

3735
public long getLoadCount() {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public class EntityStatisticsImpl extends AbstractCacheableDataStatistics implem
3131
final var cache = rootEntityDescriptor.getCacheAccessStrategy();
3232
return cache != null ? cache.getRegion() : null;
3333
} );
34-
this.rootEntityName = rootEntityDescriptor.getRootEntityName();
34+
rootEntityName = rootEntityDescriptor.getRootEntityName();
3535
}
3636

3737
public long getDeleteCount() {

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

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
import java.io.Serializable;
88
import java.util.concurrent.atomic.AtomicLong;
99
import java.util.concurrent.locks.Lock;
10-
import java.util.concurrent.locks.ReadWriteLock;
1110
import java.util.concurrent.locks.ReentrantReadWriteLock;
1211

1312
import org.hibernate.persister.entity.EntityPersister;
@@ -35,7 +34,7 @@ public class NaturalIdStatisticsImpl extends AbstractCacheableDataStatistics imp
3534
return cache != null ? cache.getRegion() : null;
3635
} );
3736
rootEntityName = rootEntityDescriptor.getRootEntityName();
38-
final ReadWriteLock lock = new ReentrantReadWriteLock();
37+
final var lock = new ReentrantReadWriteLock();
3938
readLock = lock.readLock();
4039
writeLock = lock.writeLock();
4140
}
@@ -58,11 +57,8 @@ public long getExecutionAvgTime() {
5857
// both used in the calculation
5958
writeLock.lock();
6059
try {
61-
long avgExecutionTime = 0;
62-
if ( this.executionCount.get() > 0 ) {
63-
avgExecutionTime = totalExecutionTime.get() / executionCount.get();
64-
}
65-
return avgExecutionTime;
60+
final long count = executionCount.get();
61+
return count > 0 ? totalExecutionTime.get() / count : 0;
6662
}
6763
finally {
6864
writeLock.unlock();

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

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
import java.util.concurrent.atomic.AtomicLong;
88
import java.util.concurrent.atomic.LongAdder;
99
import java.util.concurrent.locks.Lock;
10-
import java.util.concurrent.locks.ReadWriteLock;
1110
import java.util.concurrent.locks.ReentrantReadWriteLock;
1211

1312
import org.hibernate.query.Query;
@@ -42,9 +41,9 @@ public class QueryStatisticsImpl implements QueryStatistics {
4241

4342
public QueryStatisticsImpl(String query) {
4443
this.query = query;
45-
ReadWriteLock lock = new ReentrantReadWriteLock();
46-
this.readLock = lock.readLock();
47-
this.writeLock = lock.writeLock();
44+
final var lock = new ReentrantReadWriteLock();
45+
readLock = lock.readLock();
46+
writeLock = lock.writeLock();
4847
}
4948

5049
/**

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

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,37 +4,39 @@
44
*/
55
package org.hibernate.stat.internal;
66

7-
import java.util.Collections;
87
import java.util.Set;
98

9+
import org.hibernate.engine.spi.PersistenceContext;
1010
import org.hibernate.engine.spi.SessionImplementor;
1111
import org.hibernate.stat.SessionStatistics;
1212

13+
import static java.util.Collections.unmodifiableSet;
14+
1315
/**
1416
* @author Gavin King
1517
*/
1618
public class SessionStatisticsImpl implements SessionStatistics {
1719

18-
private final SessionImplementor session;
20+
private final PersistenceContext persistenceContext;
1921

2022
public SessionStatisticsImpl(SessionImplementor session) {
21-
this.session = session;
23+
persistenceContext = session.getPersistenceContextInternal();
2224
}
2325

2426
public int getEntityCount() {
25-
return session.getPersistenceContextInternal().getNumberOfManagedEntities();
27+
return persistenceContext.getNumberOfManagedEntities();
2628
}
2729

2830
public int getCollectionCount() {
29-
return session.getPersistenceContextInternal().getCollectionEntriesSize();
31+
return persistenceContext.getCollectionEntriesSize();
3032
}
3133

3234
public Set<?> getEntityKeys() {
33-
return Collections.unmodifiableSet( session.getPersistenceContextInternal().getEntitiesByKey().keySet() );
35+
return unmodifiableSet( persistenceContext.getEntitiesByKey().keySet() );
3436
}
3537

3638
public Set<?> getCollectionKeys() {
37-
return Collections.unmodifiableSet( session.getPersistenceContextInternal().getCollectionsByKey().keySet() );
39+
return unmodifiableSet( persistenceContext.getCollectionsByKey().keySet() );
3840
}
3941

4042
public String toString() {

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

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,7 @@
1414
import java.util.concurrent.atomic.LongAdder;
1515
import java.util.function.Function;
1616

17-
import org.hibernate.boot.spi.SessionFactoryOptions;
1817
import org.hibernate.cache.spi.CacheImplementor;
19-
import org.hibernate.cache.spi.QueryResultsCache;
2018
import org.hibernate.cache.spi.QueryResultsRegion;
2119
import org.hibernate.cache.spi.Region;
2220
import org.hibernate.engine.spi.SessionFactoryImplementor;
@@ -125,8 +123,8 @@ public class StatisticsImpl implements StatisticsImplementor, Service {
125123

126124
public StatisticsImpl(SessionFactoryImplementor sessionFactory) {
127125
Objects.requireNonNull( sessionFactory );
128-
SessionFactoryOptions sessionFactoryOptions = sessionFactory.getSessionFactoryOptions();
129-
this.queryStatsMap = new StatsNamedContainer<>(
126+
final var sessionFactoryOptions = sessionFactory.getSessionFactoryOptions();
127+
queryStatsMap = new StatsNamedContainer<>(
130128
sessionFactoryOptions.getQueryStatisticsMaxSize(),
131129
20
132130
);
@@ -137,12 +135,12 @@ public StatisticsImpl(SessionFactoryImplementor sessionFactory) {
137135
queryCacheEnabled = sessionFactoryOptions.isQueryCacheEnabled();
138136

139137
final List<String> entityNames = new ArrayList<>();
140-
metamodel.forEachEntityDescriptor( (entityDescriptor) -> entityNames.add( entityDescriptor.getEntityName() ) );
141-
this.allEntityNames = entityNames.toArray( new String[0] );
138+
metamodel.forEachEntityDescriptor( entity -> entityNames.add( entity.getEntityName() ) );
139+
allEntityNames = entityNames.toArray( new String[0] );
142140

143141
final List<String> collectionRoles = new ArrayList<>();
144-
metamodel.forEachCollectionDescriptor( (collectionDescriptor) -> collectionRoles.add( collectionDescriptor.getRole() ) );
145-
this.allCollectionRoles = collectionRoles.toArray( new String[0] );
142+
metamodel.forEachCollectionDescriptor( collection -> collectionRoles.add( collection.getRole() ) );
143+
allCollectionRoles = collectionRoles.toArray( new String[0] );
146144
}
147145

148146
/**
@@ -593,7 +591,6 @@ public CacheRegionStatisticsImpl getDomainDataRegionStatistics(String regionName
593591
public @Nullable CacheRegionStatisticsImpl getQueryRegionStatistics(final String regionName) {
594592
return l2CacheStatsMap.getOrCompute(
595593
regionName,
596-
597594
new Function<>() {
598595
@Override
599596
public @Nullable CacheRegionStatisticsImpl apply(String regionName1) {
@@ -604,13 +601,10 @@ public CacheRegionStatisticsImpl getDomainDataRegionStatistics(String regionName
604601
}
605602

606603
private @Nullable CacheRegionStatisticsImpl computeQueryRegionStatistics(final String regionName) {
607-
final QueryResultsCache regionAccess = cache.getQueryResultsCacheStrictly( regionName );
608-
if ( regionAccess == null ) {
609-
return null; //this null value will be cached
610-
}
611-
else {
612-
return new CacheRegionStatisticsImpl( regionAccess.getRegion() );
613-
}
604+
final var regionAccess = cache.getQueryResultsCacheStrictly( regionName );
605+
return regionAccess == null
606+
? null
607+
: new CacheRegionStatisticsImpl( regionAccess.getRegion() ); //this null value will be cached
614608
}
615609

616610

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,9 @@ else if ( configValue instanceof StatisticsFactory factory ) {
5757
// assume it names the factory class
5858
final var classLoaderService = registry.requireService( ClassLoaderService.class );
5959
try {
60-
return (StatisticsFactory) classLoaderService.classForName( configValue.toString() ).newInstance();
60+
return (StatisticsFactory)
61+
classLoaderService.classForName( configValue.toString() )
62+
.newInstance();
6163
}
6264
catch (HibernateException e) {
6365
throw e;

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

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,11 @@ public class StatsHelper {
1616

1717
public static NavigableRole getRootEntityRole(EntityPersister entityDescriptor) {
1818
final String rootEntityName = entityDescriptor.getRootEntityName();
19-
if ( entityDescriptor.getEntityName().equals( rootEntityName ) ) {
20-
return entityDescriptor.getNavigableRole();
21-
}
22-
else {
23-
return entityDescriptor.getFactory().getMappingMetamodel()
24-
.getEntityDescriptor( rootEntityName )
25-
.getNavigableRole();
26-
}
19+
return entityDescriptor.getEntityName().equals( rootEntityName )
20+
? entityDescriptor.getNavigableRole()
21+
: entityDescriptor.getFactory().getMappingMetamodel()
22+
.getEntityDescriptor( rootEntityName )
23+
.getNavigableRole();
2724
}
2825

2926
private StatsHelper() {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public final class StatsNamedContainer<V> {
3333
* Creates a bounded container - based on BoundedConcurrentHashMap
3434
*/
3535
public StatsNamedContainer(int capacity, int concurrencyLevel) {
36-
this.map = new BoundedConcurrentHashMap<>( capacity, concurrencyLevel, LRU );
36+
map = new BoundedConcurrentHashMap<>( capacity, concurrencyLevel, LRU );
3737
}
3838

3939
/**

0 commit comments

Comments
 (0)