HHH-13546 Make the sessionFactory field in StatisticsImpl required

This commit is contained in:
Sanne Grinovero 2019-08-07 11:18:34 +01:00
parent 41cab3bfce
commit 8460cd8796
1 changed files with 5 additions and 50 deletions

View File

@ -6,6 +6,7 @@
*/ */
package org.hibernate.stat.internal; package org.hibernate.stat.internal;
import java.util.Objects;
import java.util.concurrent.atomic.AtomicLong; import java.util.concurrent.atomic.AtomicLong;
import java.util.concurrent.atomic.LongAdder; import java.util.concurrent.atomic.LongAdder;
@ -31,6 +32,7 @@ import static org.hibernate.internal.CoreLogging.messageLogger;
*/ */
@SuppressWarnings({ "unchecked" }) @SuppressWarnings({ "unchecked" })
public class StatisticsImpl implements StatisticsImplementor, Service, Manageable { public class StatisticsImpl implements StatisticsImplementor, Service, Manageable {
private static final CoreMessageLogger LOG = messageLogger( StatisticsImpl.class ); private static final CoreMessageLogger LOG = messageLogger( StatisticsImpl.class );
private final SessionFactoryImplementor sessionFactory; private final SessionFactoryImplementor sessionFactory;
@ -104,14 +106,8 @@ public class StatisticsImpl implements StatisticsImplementor, Service, Manageabl
private final StatsNamedContainer<DeprecatedNaturalIdCacheStatisticsImpl> deprecatedNaturalIdStatsMap = new StatsNamedContainer(); private final StatsNamedContainer<DeprecatedNaturalIdCacheStatisticsImpl> deprecatedNaturalIdStatsMap = new StatsNamedContainer();
@SuppressWarnings({ "UnusedDeclaration" })
public StatisticsImpl() {
this( null );
}
public StatisticsImpl(SessionFactoryImplementor sessionFactory) { public StatisticsImpl(SessionFactoryImplementor sessionFactory) {
this.sessionFactory = sessionFactory; this.sessionFactory = Objects.requireNonNull( sessionFactory );
this.queryStatsMap = new StatsNamedContainer( this.queryStatsMap = new StatsNamedContainer(
sessionFactory != null ? sessionFactory != null ?
sessionFactory.getSessionFactoryOptions().getQueryStatisticsMaxSize() : sessionFactory.getSessionFactoryOptions().getQueryStatisticsMaxSize() :
@ -208,20 +204,11 @@ public class StatisticsImpl implements StatisticsImplementor, Service, Manageabl
@Override @Override
public String[] getEntityNames() { public String[] getEntityNames() {
if ( sessionFactory == null ) { return sessionFactory.getMetamodel().getAllEntityNames();
return entityStatsMap.keysAsArray();
}
else {
return sessionFactory.getMetamodel().getAllEntityNames();
}
} }
@Override @Override
public EntityStatisticsImpl getEntityStatistics(String entityName) { public EntityStatisticsImpl getEntityStatistics(String entityName) {
if ( sessionFactory == null ) {
return null;
}
return entityStatsMap.getOrCompute( return entityStatsMap.getOrCompute(
entityName, entityName,
s -> new EntityStatisticsImpl( sessionFactory.getMetamodel().entityPersister( s ) ) s -> new EntityStatisticsImpl( sessionFactory.getMetamodel().entityPersister( s ) )
@ -321,20 +308,11 @@ public class StatisticsImpl implements StatisticsImplementor, Service, Manageabl
@Override @Override
public String[] getCollectionRoleNames() { public String[] getCollectionRoleNames() {
if ( sessionFactory == null ) { return sessionFactory.getMetamodel().getAllCollectionRoles();
return collectionStatsMap.keysAsArray();
}
else {
return sessionFactory.getMetamodel().getAllCollectionRoles();
}
} }
@Override @Override
public CollectionStatisticsImpl getCollectionStatistics(String role) { public CollectionStatisticsImpl getCollectionStatistics(String role) {
if ( sessionFactory == null ) {
return null;
}
return collectionStatsMap.getOrCompute( return collectionStatsMap.getOrCompute(
role, role,
s -> new CollectionStatisticsImpl( sessionFactory.getMetamodel().collectionPersister( s ) ) s -> new CollectionStatisticsImpl( sessionFactory.getMetamodel().collectionPersister( s ) )
@ -423,10 +401,6 @@ public class StatisticsImpl implements StatisticsImplementor, Service, Manageabl
@Override @Override
public NaturalIdStatisticsImpl getNaturalIdStatistics(String rootEntityName) { public NaturalIdStatisticsImpl getNaturalIdStatistics(String rootEntityName) {
if ( sessionFactory == null ) {
return null;
}
return naturalIdQueryStatsMap.getOrCompute( return naturalIdQueryStatsMap.getOrCompute(
rootEntityName, rootEntityName,
s -> { s -> {
@ -569,19 +543,11 @@ public class StatisticsImpl implements StatisticsImplementor, Service, Manageabl
@Override @Override
public String[] getSecondLevelCacheRegionNames() { public String[] getSecondLevelCacheRegionNames() {
if ( sessionFactory == null ) {
throw new IllegalStateException( "Statistics no longer associated with SessionFactory - cannot get (legacy) region names" );
}
return sessionFactory.getCache().getSecondLevelCacheRegionNames(); return sessionFactory.getCache().getSecondLevelCacheRegionNames();
} }
@Override @Override
public CacheRegionStatisticsImpl getDomainDataRegionStatistics(String regionName) { public CacheRegionStatisticsImpl getDomainDataRegionStatistics(String regionName) {
if ( sessionFactory == null ) {
return null;
}
return l2CacheStatsMap.getOrCompute( return l2CacheStatsMap.getOrCompute(
regionName, regionName,
s -> { s -> {
@ -609,10 +575,6 @@ public class StatisticsImpl implements StatisticsImplementor, Service, Manageabl
return existing; return existing;
} }
if ( sessionFactory == null ) {
return null;
}
final QueryResultsCache regionAccess = sessionFactory.getCache() final QueryResultsCache regionAccess = sessionFactory.getCache()
.getQueryResultsCacheStrictly( regionName ); .getQueryResultsCacheStrictly( regionName );
if ( regionAccess == null ) { if ( regionAccess == null ) {
@ -627,10 +589,6 @@ public class StatisticsImpl implements StatisticsImplementor, Service, Manageabl
@Override @Override
public CacheRegionStatisticsImpl getCacheRegionStatistics(String regionName) { public CacheRegionStatisticsImpl getCacheRegionStatistics(String regionName) {
if ( sessionFactory == null ) {
return null;
}
if ( ! sessionFactory.getSessionFactoryOptions().isSecondLevelCacheEnabled() ) { if ( ! sessionFactory.getSessionFactoryOptions().isSecondLevelCacheEnabled() ) {
return null; return null;
} }
@ -658,9 +616,6 @@ public class StatisticsImpl implements StatisticsImplementor, Service, Manageabl
@Override @Override
public CacheRegionStatisticsImpl getSecondLevelCacheStatistics(String regionName) { public CacheRegionStatisticsImpl getSecondLevelCacheStatistics(String regionName) {
if ( sessionFactory == null ) {
return null;
}
return getCacheRegionStatistics( sessionFactory.getCache().unqualifyRegionName( regionName ) ); return getCacheRegionStatistics( sessionFactory.getCache().unqualifyRegionName( regionName ) );
} }