package org.hibernate.id.enhanced;
import java.io.Serializable;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import org.hibernate.HibernateException;
import org.hibernate.id.IntegralDataTypeHolder;
import org.hibernate.internal.CoreMessageLogger;
import org.jboss.logging.Logger;
public class PooledOptimizer extends AbstractOptimizer implements InitialValueAwareOptimizer {
private static final CoreMessageLogger log = Logger.getMessageLogger(
CoreMessageLogger.class,
PooledOptimizer.class.getName()
);
private static class GenerationState {
private IntegralDataTypeHolder hiValue;
private IntegralDataTypeHolder value;
}
private long initialValue = -1;
public PooledOptimizer(Class returnClass, int incrementSize) {
super( returnClass, incrementSize );
if ( incrementSize < 1 ) {
throw new HibernateException( "increment size cannot be less than 1" );
}
if ( log.isTraceEnabled() ) {
log.tracev(
"Creating pooled optimizer with [incrementSize={0}; returnClass={1}]",
incrementSize,
returnClass.getName()
);
}
}
@Override
public synchronized Serializable generate(AccessCallback callback) {
final GenerationState generationState = locateGenerationState( callback.getTenantIdentifier() );
if ( generationState.hiValue == null ) {
generationState.value = callback.getNextValue();
if ( generationState.value.lt( 1 ) ) {
log.pooledOptimizerReportedInitialValue( generationState.value );
}
if ( ( initialValue == -1
&& generationState.value.lt( incrementSize ) )
|| generationState.value.eq( initialValue ) ) {
generationState.hiValue = callback.getNextValue();
}
else {
generationState.hiValue = generationState.value;
generationState.value = generationState.hiValue.copy().subtract( incrementSize - 1 );
}
}
else if ( generationState.value.gt( generationState.hiValue ) ) {
generationState.hiValue = callback.getNextValue();
generationState.value = generationState.hiValue.copy().subtract( incrementSize - 1 );
}
return generationState.value.makeValueThenIncrement();
}
private GenerationState noTenantState;
private Map<String,GenerationState> tenantSpecificState;
private GenerationState locateGenerationState(String tenantIdentifier) {
if ( tenantIdentifier == null ) {
if ( noTenantState == null ) {
noTenantState = new GenerationState();
}
return noTenantState;
}
else {
GenerationState state;
if ( tenantSpecificState == null ) {
tenantSpecificState = new ConcurrentHashMap<String, GenerationState>();
state = new GenerationState();
tenantSpecificState.put( tenantIdentifier, state );
}
else {
state = tenantSpecificState.get( tenantIdentifier );
if ( state == null ) {
state = new GenerationState();
tenantSpecificState.put( tenantIdentifier, state );
}
}
return state;
}
}
private GenerationState noTenantGenerationState() {
if ( noTenantState == null ) {
throw new IllegalStateException( "Could not locate previous generation state for no-tenant" );
}
return noTenantState;
}
@Override
public IntegralDataTypeHolder getLastSourceValue() {
return noTenantGenerationState().hiValue;
}
@Override
public boolean applyIncrementSizeToSourceValues() {
return true;
}
public IntegralDataTypeHolder getLastValue() {
return noTenantGenerationState().value.copy().decrement();
}
@Override
public void injectInitialValue(long initialValue) {
this.initialValue = initialValue;
}
}