Skip to content

Commit

Permalink
HHH-7495 HHH-7492 Move RegionFactory to session factory scope service…
Browse files Browse the repository at this point in the history
… registry
  • Loading branch information
stliu committed Jul 31, 2012
1 parent 9632e01 commit 7f3ad01
Show file tree
Hide file tree
Showing 69 changed files with 1,172 additions and 1,130 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

import org.hibernate.cache.CacheException;
import org.hibernate.cache.NoCacheRegionFactoryAvailableException;
import org.hibernate.cache.spi.AbstractRegionFactory;
import org.hibernate.cache.spi.CacheDataDescription;
import org.hibernate.cache.spi.CollectionRegion;
import org.hibernate.cache.spi.EntityRegion;
Expand All @@ -42,48 +43,48 @@
*
* @author Steve Ebersole
*/
public class NoCachingRegionFactory implements RegionFactory {
public class NoCachingRegionFactory extends AbstractRegionFactory {
public static RegionFactory INSTANCE = new NoCachingRegionFactory();
public NoCachingRegionFactory() {
}

public void start(Settings settings, Properties properties) throws CacheException {
@Override
public void start() {
}

@Override
public void stop() {
}

@Override
public boolean isMinimalPutsEnabledByDefault() {
return false;
}

@Override
public AccessType getDefaultAccessType() {
return null;
}

@Override
public long nextTimestamp() {
return System.currentTimeMillis() / 100;
}

@Override
public EntityRegion buildEntityRegion(String regionName, Properties properties, CacheDataDescription metadata)
throws CacheException {
throw new NoCacheRegionFactoryAvailableException();
}

@Override
public NaturalIdRegion buildNaturalIdRegion(String regionName, Properties properties, CacheDataDescription metadata)
throws CacheException {
throw new NoCacheRegionFactoryAvailableException();
}

@Override
public CollectionRegion buildCollectionRegion(String regionName, Properties properties, CacheDataDescription metadata)
throws CacheException {
throw new NoCacheRegionFactoryAvailableException();
}

@Override
public QueryResultsRegion buildQueryResultsRegion(String regionName, Properties properties) throws CacheException {
throw new NoCacheRegionFactoryAvailableException();
}

@Override
public TimestampsRegion buildTimestampsRegion(String regionName, Properties properties) throws CacheException {
throw new NoCacheRegionFactoryAvailableException();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,24 +29,28 @@

import org.hibernate.cache.spi.RegionFactory;
import org.hibernate.cfg.AvailableSettings;
import org.hibernate.cfg.Configuration;
import org.hibernate.cfg.Environment;
import org.hibernate.engine.spi.SessionFactoryImplementor;
import org.hibernate.internal.CoreMessageLogger;
import org.hibernate.internal.util.config.ConfigurationHelper;
import org.hibernate.metamodel.spi.source.MetadataImplementor;
import org.hibernate.service.classloading.spi.ClassLoaderService;
import org.hibernate.service.config.spi.ConfigurationService;
import org.hibernate.service.config.spi.StandardConverters;
import org.hibernate.service.spi.BasicServiceInitiator;
import org.hibernate.service.spi.ServiceException;
import org.hibernate.service.spi.ServiceRegistryImplementor;
import org.hibernate.service.spi.SessionFactoryServiceInitiator;

/**
* Initiator for the {@link RegionFactory} service.
*
* @author Hardy Ferentschik
*/
public class RegionFactoryInitiator implements BasicServiceInitiator<RegionFactory> {
public class RegionFactoryInitiator implements SessionFactoryServiceInitiator<RegionFactory> {
public static final RegionFactoryInitiator INSTANCE = new RegionFactoryInitiator();
public static final String DEF_CACHE_REG_FACTORY = NoCachingRegionFactory.class.getName();
private static final String DEFAULT_IMPL = NoCachingRegionFactory.class.getName();
private static final CoreMessageLogger LOG = Logger.getMessageLogger(
CoreMessageLogger.class,
RegionFactoryInitiator.class.getName()
Expand All @@ -63,43 +67,60 @@ public Class<RegionFactory> getServiceInitiated() {
}

@Override
@SuppressWarnings( { "unchecked" })
public RegionFactory initiateService(Map configurationValues, ServiceRegistryImplementor registry) {
final Object impl = configurationValues.get( IMPL_NAME );
boolean isCacheEnabled = isCacheEnabled(registry);
if(!isCacheEnabled){
LOG.debugf( "Second level cache has been disabled, so using % as cache region factory", NoCachingRegionFactory.class.getName() );
return NoCachingRegionFactory.INSTANCE;
public RegionFactory initiateService(SessionFactoryImplementor sessionFactory, Configuration configuration, ServiceRegistryImplementor registry) {
return initiateService(sessionFactory, registry);
}

@Override
public RegionFactory initiateService(SessionFactoryImplementor sessionFactory, MetadataImplementor metadata, ServiceRegistryImplementor registry) {
return initiateService(sessionFactory, registry);
}

private RegionFactory initiateService(SessionFactoryImplementor sessionFactory, ServiceRegistryImplementor registry){
final Object impl = registry.getService( ConfigurationService.class ).getSettings().get( IMPL_NAME );
boolean isCacheEnabled = isCacheEnabled( registry );
RegionFactory factory;
if ( !isCacheEnabled ) {
LOG.debugf(
"Second level cache has been disabled, so using % as cache region factory",
NoCachingRegionFactory.class.getName()
);
factory = NoCachingRegionFactory.INSTANCE;
}
if ( impl == null ) {
else if ( impl == null ) {
LOG.debugf(
"No 'hibernate.cache.region.factory_class' is provided, so using %s as default",
NoCachingRegionFactory.class.getName()
);
return NoCachingRegionFactory.INSTANCE;
}
LOG.debugf( "Cache region factory : %s", impl.toString() );
if ( getServiceInitiated().isInstance( impl ) ) {
return (RegionFactory) impl;
}

Class<? extends RegionFactory> customImplClass = null;
if ( Class.class.isInstance( impl ) ) {
customImplClass = (Class<? extends RegionFactory>) impl;
factory = NoCachingRegionFactory.INSTANCE;
}
else {
customImplClass = registry.getService( ClassLoaderService.class )
.classForName( mapLegacyNames( impl.toString() ) );
}
LOG.debugf( "Cache region factory : %s", impl.toString() );
if ( getServiceInitiated().isInstance( impl ) ) {
factory = (RegionFactory) impl;
}
else {
Class<? extends RegionFactory> customImplClass = null;
if ( Class.class.isInstance( impl ) ) {
customImplClass = (Class<? extends RegionFactory>) impl;
}
else {
customImplClass = registry.getService( ClassLoaderService.class )
.classForName( mapLegacyNames( impl.toString() ) );
}

try {
return customImplClass.newInstance();
}
catch ( Exception e ) {
throw new ServiceException(
"Could not initialize custom RegionFactory impl [" + customImplClass.getName() + "]", e
);
try {
factory = customImplClass.newInstance();
}
catch ( Exception e ) {
throw new ServiceException(
"Could not initialize custom RegionFactory impl [" + customImplClass.getName() + "]", e
);
}
}
}

return factory;
}

private static boolean isCacheEnabled(ServiceRegistryImplementor serviceRegistry) {
Expand All @@ -111,7 +132,8 @@ private static boolean isCacheEnabled(ServiceRegistryImplementor serviceRegistry
);
final boolean useQueryCache = configurationService.getSetting(
AvailableSettings.USE_QUERY_CACHE,
StandardConverters.BOOLEAN
StandardConverters.BOOLEAN,
false
);
return useSecondLevelCache || useQueryCache;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,15 @@
import org.hibernate.cache.spi.QueryCache;
import org.hibernate.cache.spi.QueryKey;
import org.hibernate.cache.spi.QueryResultsRegion;
import org.hibernate.cache.spi.RegionFactory;
import org.hibernate.cache.spi.UpdateTimestampsCache;
import org.hibernate.cfg.AvailableSettings;
import org.hibernate.cfg.Settings;
import org.hibernate.engine.spi.SessionFactoryImplementor;
import org.hibernate.engine.spi.SessionImplementor;
import org.hibernate.internal.CoreMessageLogger;
import org.hibernate.service.config.spi.ConfigurationService;
import org.hibernate.service.config.spi.StandardConverters;
import org.hibernate.type.Type;
import org.hibernate.type.TypeHelper;

Expand All @@ -64,28 +69,30 @@ public class StandardQueryCache implements QueryCache {
private QueryResultsRegion cacheRegion;
private UpdateTimestampsCache updateTimestampsCache;

public void clear() throws CacheException {
cacheRegion.evictAll();
}

public StandardQueryCache(
final Settings settings,
final Properties props,
final UpdateTimestampsCache updateTimestampsCache,
String regionName) throws HibernateException {
public StandardQueryCache(SessionFactoryImplementor sessionFactoryImplementor, UpdateTimestampsCache updateTimestampsCache, String regionName) {
if ( regionName == null ) {
regionName = StandardQueryCache.class.getName();
}
String prefix = settings.getCacheRegionPrefix();
String prefix = sessionFactoryImplementor.getServiceRegistry()
.getService( ConfigurationService.class )
.getSetting(
AvailableSettings.CACHE_REGION_PREFIX, StandardConverters.STRING, null
);
if ( prefix != null ) {
regionName = prefix + '.' + regionName;
}
LOG.startingQueryCache( regionName );

this.cacheRegion = settings.getRegionFactory().buildQueryResultsRegion( regionName, props );
this.cacheRegion = sessionFactoryImplementor.getServiceRegistry()
.getService( RegionFactory.class )
.buildQueryResultsRegion( regionName, sessionFactoryImplementor.getProperties() );
this.updateTimestampsCache = updateTimestampsCache;
}

public void clear() throws CacheException {
cacheRegion.evictAll();
}

@SuppressWarnings({ "UnnecessaryBoxing", "unchecked" })
public boolean put(
QueryKey key,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,25 +23,21 @@
*/
package org.hibernate.cache.internal;

import java.util.Properties;

import org.hibernate.HibernateException;
import org.hibernate.cache.spi.QueryCache;
import org.hibernate.cache.spi.QueryCacheFactory;
import org.hibernate.cache.spi.UpdateTimestampsCache;
import org.hibernate.cfg.Settings;
import org.hibernate.engine.spi.SessionFactoryImplementor;

/**
* Standard Hibernate implementation of the QueryCacheFactory interface. Returns instances of
* {@link StandardQueryCache}.
*/
public class StandardQueryCacheFactory implements QueryCacheFactory {
@Override
public QueryCache getQueryCache(
final String regionName,
final UpdateTimestampsCache updateTimestampsCache,
final Settings settings,
final Properties props) throws HibernateException {
return new StandardQueryCache(settings, props, updateTimestampsCache, regionName);
public QueryCache getQueryCache(String regionName, UpdateTimestampsCache updateTimestampsCache, SessionFactoryImplementor sessionFactoryImplementor)
throws HibernateException {
return new StandardQueryCache( sessionFactoryImplementor, updateTimestampsCache, regionName );
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package org.hibernate.cache.spi;

import java.util.Properties;

import org.jboss.logging.Logger;

import org.hibernate.cache.CacheException;
import org.hibernate.cfg.AvailableSettings;
import org.hibernate.cfg.Settings;
import org.hibernate.cfg.SettingsFactory;
import org.hibernate.internal.CoreMessageLogger;
import org.hibernate.service.config.spi.ConfigurationService;
import org.hibernate.service.config.spi.StandardConverters;
import org.hibernate.service.spi.ServiceRegistryAwareService;
import org.hibernate.service.spi.ServiceRegistryImplementor;

/**
* @author Strong Liu <stliu@hibernate.org>
*/
public abstract class AbstractRegionFactory implements RegionFactory, ServiceRegistryAwareService {
private static final CoreMessageLogger LOG = Logger.getMessageLogger(
CoreMessageLogger.class,
AbstractRegionFactory.class.getName()
);

private ServiceRegistryImplementor serviceRegistry;
private boolean isMinimalPutsEnabled;

public ServiceRegistryImplementor getServiceRegistry() {
return serviceRegistry;
}

@Override
public void injectServices(ServiceRegistryImplementor serviceRegistry) {
this.serviceRegistry = serviceRegistry;
this.isMinimalPutsEnabled = serviceRegistry.getService( ConfigurationService.class ).getSetting( AvailableSettings.USE_MINIMAL_PUTS,
StandardConverters.BOOLEAN, isMinimalPutsEnabledByDefault()
);
LOG.debugf( "Optimize cache for minimal puts: %s", SettingsFactory.enabledDisabled( isMinimalPutsEnabled ) );
}

@Override
public void start(Settings settings, Properties properties) throws CacheException {
start();
}

@Override
public boolean isMinimalPutsEnabled() {
return isMinimalPutsEnabled;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,8 @@
*/
package org.hibernate.cache.spi;

import java.util.Properties;

import org.hibernate.HibernateException;
import org.hibernate.cfg.Settings;
import org.hibernate.engine.spi.SessionFactoryImplementor;

/**
* Defines a factory for query cache instances. These factories are responsible for
Expand All @@ -36,8 +34,7 @@
*/
public interface QueryCacheFactory {
public QueryCache getQueryCache(
String regionName,
UpdateTimestampsCache updateTimestampsCache,
Settings settings,
Properties props) throws HibernateException;
String regionName,
UpdateTimestampsCache updateTimestampsCache,
SessionFactoryImplementor sessionFactoryImplementor) throws HibernateException;
}
Loading

0 comments on commit 7f3ad01

Please sign in to comment.