Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -24,20 +24,20 @@ public abstract class AbstractCacheableDataStatistics implements CacheableDataSt
private final @Nullable LongAdder cacheRemoveCount;

public AbstractCacheableDataStatistics(Supplier<@Nullable Region> regionSupplier) {
final Region region = regionSupplier.get();
final var region = regionSupplier.get();
if ( region == null ) {
this.cacheRegionName = null;
this.cacheHitCount = null;
this.cacheMissCount = null;
this.cachePutCount = null;
this.cacheRemoveCount = null;
cacheRegionName = null;
cacheHitCount = null;
cacheMissCount = null;
cachePutCount = null;
cacheRemoveCount = null;
}
else {
this.cacheRegionName = region.getName();
this.cacheHitCount = new LongAdder();
this.cacheMissCount = new LongAdder();
this.cachePutCount = new LongAdder();
this.cacheRemoveCount = new LongAdder();
cacheRegionName = region.getName();
cacheHitCount = new LongAdder();
cacheMissCount = new LongAdder();
cachePutCount = new LongAdder();
cacheRemoveCount = new LongAdder();
}
}

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

@Override
public long getCacheHitCount() {
if ( cacheRegionName == null ) {
return NOT_CACHED_COUNT;
}
return cacheRegionName == null
? NOT_CACHED_COUNT
: NullnessUtil.castNonNull( cacheHitCount ).sum();

return NullnessUtil.castNonNull( cacheHitCount ).sum();
}

@Override
public long getCachePutCount() {
if ( cacheRegionName == null ) {
return NOT_CACHED_COUNT;
}
return cacheRegionName == null
? NOT_CACHED_COUNT
: NullnessUtil.castNonNull( cachePutCount ).sum();

return NullnessUtil.castNonNull( cachePutCount ).sum();
}

@Override
public long getCacheMissCount() {
if ( cacheRegionName == null ) {
return NOT_CACHED_COUNT;
}
return cacheRegionName == null
? NOT_CACHED_COUNT
: NullnessUtil.castNonNull( cacheMissCount ).sum();

return NullnessUtil.castNonNull( cacheMissCount ).sum();
}

@Override
public long getCacheRemoveCount() {
if ( cacheRegionName == null ) {
return NOT_CACHED_COUNT;
}
return cacheRegionName == null
? NOT_CACHED_COUNT
: NullnessUtil.castNonNull( cacheRemoveCount ).sum();

return NullnessUtil.castNonNull( cacheRemoveCount ).sum();
}

public void incrementCacheHitCount() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,11 @@ public class CollectionStatisticsImpl extends AbstractCacheableDataStatistics im
private final LongAdder recreateCount = new LongAdder();

CollectionStatisticsImpl(CollectionPersister persister) {
super(
() -> persister.getCacheAccessStrategy() != null
? persister.getCacheAccessStrategy().getRegion()
: null
);

this.collectionRole = persister.getRole();
super( () -> {
final var cache = persister.getCacheAccessStrategy();
return cache == null ? null : cache.getRegion();
} );
collectionRole = persister.getRole();
}

public long getLoadCount() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public class EntityStatisticsImpl extends AbstractCacheableDataStatistics implem
final var cache = rootEntityDescriptor.getCacheAccessStrategy();
return cache != null ? cache.getRegion() : null;
} );
this.rootEntityName = rootEntityDescriptor.getRootEntityName();
rootEntityName = rootEntityDescriptor.getRootEntityName();
}

public long getDeleteCount() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import java.io.Serializable;
import java.util.concurrent.atomic.AtomicLong;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;

import org.hibernate.persister.entity.EntityPersister;
Expand Down Expand Up @@ -35,7 +34,7 @@ public class NaturalIdStatisticsImpl extends AbstractCacheableDataStatistics imp
return cache != null ? cache.getRegion() : null;
} );
rootEntityName = rootEntityDescriptor.getRootEntityName();
final ReadWriteLock lock = new ReentrantReadWriteLock();
final var lock = new ReentrantReadWriteLock();
readLock = lock.readLock();
writeLock = lock.writeLock();
}
Expand All @@ -58,11 +57,8 @@ public long getExecutionAvgTime() {
// both used in the calculation
writeLock.lock();
try {
long avgExecutionTime = 0;
if ( this.executionCount.get() > 0 ) {
avgExecutionTime = totalExecutionTime.get() / executionCount.get();
}
return avgExecutionTime;
final long count = executionCount.get();
return count > 0 ? totalExecutionTime.get() / count : 0;
}
finally {
writeLock.unlock();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import java.util.concurrent.atomic.AtomicLong;
import java.util.concurrent.atomic.LongAdder;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;

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

public QueryStatisticsImpl(String query) {
this.query = query;
ReadWriteLock lock = new ReentrantReadWriteLock();
this.readLock = lock.readLock();
this.writeLock = lock.writeLock();
final var lock = new ReentrantReadWriteLock();
readLock = lock.readLock();
writeLock = lock.writeLock();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,37 +4,39 @@
*/
package org.hibernate.stat.internal;

import java.util.Collections;
import java.util.Set;

import org.hibernate.engine.spi.PersistenceContext;
import org.hibernate.engine.spi.SessionImplementor;
import org.hibernate.stat.SessionStatistics;

import static java.util.Collections.unmodifiableSet;

/**
* @author Gavin King
*/
public class SessionStatisticsImpl implements SessionStatistics {

private final SessionImplementor session;
private final PersistenceContext persistenceContext;

public SessionStatisticsImpl(SessionImplementor session) {
this.session = session;
persistenceContext = session.getPersistenceContextInternal();
}

public int getEntityCount() {
return session.getPersistenceContextInternal().getNumberOfManagedEntities();
return persistenceContext.getNumberOfManagedEntities();
}

public int getCollectionCount() {
return session.getPersistenceContextInternal().getCollectionEntriesSize();
return persistenceContext.getCollectionEntriesSize();
}

public Set<?> getEntityKeys() {
return Collections.unmodifiableSet( session.getPersistenceContextInternal().getEntitiesByKey().keySet() );
return unmodifiableSet( persistenceContext.getEntitiesByKey().keySet() );
}

public Set<?> getCollectionKeys() {
return Collections.unmodifiableSet( session.getPersistenceContextInternal().getCollectionsByKey().keySet() );
return unmodifiableSet( persistenceContext.getCollectionsByKey().keySet() );
}

public String toString() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,7 @@
import java.util.concurrent.atomic.LongAdder;
import java.util.function.Function;

import org.hibernate.boot.spi.SessionFactoryOptions;
import org.hibernate.cache.spi.CacheImplementor;
import org.hibernate.cache.spi.QueryResultsCache;
import org.hibernate.cache.spi.QueryResultsRegion;
import org.hibernate.cache.spi.Region;
import org.hibernate.engine.spi.SessionFactoryImplementor;
Expand Down Expand Up @@ -125,8 +123,8 @@ public class StatisticsImpl implements StatisticsImplementor, Service {

public StatisticsImpl(SessionFactoryImplementor sessionFactory) {
Objects.requireNonNull( sessionFactory );
SessionFactoryOptions sessionFactoryOptions = sessionFactory.getSessionFactoryOptions();
this.queryStatsMap = new StatsNamedContainer<>(
final var sessionFactoryOptions = sessionFactory.getSessionFactoryOptions();
queryStatsMap = new StatsNamedContainer<>(
sessionFactoryOptions.getQueryStatisticsMaxSize(),
20
);
Expand All @@ -137,12 +135,12 @@ public StatisticsImpl(SessionFactoryImplementor sessionFactory) {
queryCacheEnabled = sessionFactoryOptions.isQueryCacheEnabled();

final List<String> entityNames = new ArrayList<>();
metamodel.forEachEntityDescriptor( (entityDescriptor) -> entityNames.add( entityDescriptor.getEntityName() ) );
this.allEntityNames = entityNames.toArray( new String[0] );
metamodel.forEachEntityDescriptor( entity -> entityNames.add( entity.getEntityName() ) );
allEntityNames = entityNames.toArray( new String[0] );

final List<String> collectionRoles = new ArrayList<>();
metamodel.forEachCollectionDescriptor( (collectionDescriptor) -> collectionRoles.add( collectionDescriptor.getRole() ) );
this.allCollectionRoles = collectionRoles.toArray( new String[0] );
metamodel.forEachCollectionDescriptor( collection -> collectionRoles.add( collection.getRole() ) );
allCollectionRoles = collectionRoles.toArray( new String[0] );
}

/**
Expand Down Expand Up @@ -593,7 +591,6 @@ public CacheRegionStatisticsImpl getDomainDataRegionStatistics(String regionName
public @Nullable CacheRegionStatisticsImpl getQueryRegionStatistics(final String regionName) {
return l2CacheStatsMap.getOrCompute(
regionName,

new Function<>() {
@Override
public @Nullable CacheRegionStatisticsImpl apply(String regionName1) {
Expand All @@ -604,13 +601,10 @@ public CacheRegionStatisticsImpl getDomainDataRegionStatistics(String regionName
}

private @Nullable CacheRegionStatisticsImpl computeQueryRegionStatistics(final String regionName) {
final QueryResultsCache regionAccess = cache.getQueryResultsCacheStrictly( regionName );
if ( regionAccess == null ) {
return null; //this null value will be cached
}
else {
return new CacheRegionStatisticsImpl( regionAccess.getRegion() );
}
final var regionAccess = cache.getQueryResultsCacheStrictly( regionName );
return regionAccess == null
? null
: new CacheRegionStatisticsImpl( regionAccess.getRegion() ); //this null value will be cached
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,9 @@ else if ( configValue instanceof StatisticsFactory factory ) {
// assume it names the factory class
final var classLoaderService = registry.requireService( ClassLoaderService.class );
try {
return (StatisticsFactory) classLoaderService.classForName( configValue.toString() ).newInstance();
return (StatisticsFactory)
classLoaderService.classForName( configValue.toString() )
.newInstance();
}
catch (HibernateException e) {
throw e;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,11 @@ public class StatsHelper {

public static NavigableRole getRootEntityRole(EntityPersister entityDescriptor) {
final String rootEntityName = entityDescriptor.getRootEntityName();
if ( entityDescriptor.getEntityName().equals( rootEntityName ) ) {
return entityDescriptor.getNavigableRole();
}
else {
return entityDescriptor.getFactory().getMappingMetamodel()
.getEntityDescriptor( rootEntityName )
.getNavigableRole();
}
return entityDescriptor.getEntityName().equals( rootEntityName )
? entityDescriptor.getNavigableRole()
: entityDescriptor.getFactory().getMappingMetamodel()
.getEntityDescriptor( rootEntityName )
.getNavigableRole();
}

private StatsHelper() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public final class StatsNamedContainer<V> {
* Creates a bounded container - based on BoundedConcurrentHashMap
*/
public StatsNamedContainer(int capacity, int concurrencyLevel) {
this.map = new BoundedConcurrentHashMap<>( capacity, concurrencyLevel, LRU );
map = new BoundedConcurrentHashMap<>( capacity, concurrencyLevel, LRU );
}

/**
Expand Down
Loading