atg.service.idgen
Class ObfuscatedSQLIdGenerator

java.lang.Object
  extended by atg.nucleus.logging.VariableArgumentApplicationLoggingImpl
      extended by atg.nucleus.GenericService
          extended by atg.service.idgen.AbstractSequentialIdGenerator
              extended by atg.service.idgen.PersistentIdGenerator
                  extended by atg.service.idgen.SQLIdGenerator
                      extended by atg.service.idgen.ObfuscatedSQLIdGenerator
All Implemented Interfaces:
atg.naming.AbsoluteNameable, NameContextBindingListener, NameContextElement, atg.naming.NameContextParentable, NameResolver, AdminableService, atg.nucleus.Configured, ApplicationLogging, atg.nucleus.logging.ApplicationLoggingSender, atg.nucleus.logging.TraceApplicationLogging, VariableArgumentApplicationLogging, ComponentNameResolver, Service, ServiceListener, IdGenerator, java.util.EventListener

public class ObfuscatedSQLIdGenerator
extends SQLIdGenerator

Persistent, obfuscated id generator that keeps track of ids given out using an SQL DB table.

This implementation obfuscates the generated ids in two ways. First, for a given batch of reservered ids this implementation only gives out a few ids. Second, ids can be optionally hex encoded when being used as String ids. Both the String and long ids generated use a pseudorandom number generator to get a long id from the current batch of ids. In addition to not giving out the same id twice, this implementation will not given out adjacent long ids (or String ids that come from adjacent long ids).

This implementation is suitable for use in a distributed system. Transactionality integrity is provided by the underlying data store, which is an SQL database.

The IdSpace properties batchSize and idsPerBatch are used in conjunction. batchSize works as in the SQLIdGenerator. idsPerBatch is the maximum never of ids that will be given out in any given batch.

It is strongly recommended that idsPerBatch be less than 1% of the batchSize. This is both for security and performance. For security a sparse (i.e., less dense) id space makes it harder to guess ids. Since this implementation does not give out adjacent ids, it could be forced to do more work to find suitable ids if the id space is too dense. This implementation will not allow an id space to be added that is denser than 10%. That is, idsPerBatch / batchSize must be less than .1. Always set these two properties together to maintain the 1% density goal.

The recommended values for batchSize and idsPerBatch are 100,000 and 997, respectively. These numbers are not magic. 100,000 is the default batch size. 997 is a prime number that is slightly less than 1% of the batch size.

See Also:
PersistentIdGenerator, SQLIdGenerator

Field Summary
static java.lang.String CLASS_VERSION
          Class version string
protected static java.lang.String ERR_TOO_DENSE
           
static float MAX_DENSITY
          maximum density for obfuscated id generators
 
Fields inherited from class atg.service.idgen.SQLIdGenerator
ERR_HWM_BAD_STATE, ERR_HWM_FAIL, ERR_NULL_DS, ERR_QUERY_TABLE, ERR_RESERVE_BAD_STATE
 
Fields inherited from class atg.service.idgen.PersistentIdGenerator
ERR_ADD_BAD_STATE, ERR_ADD_NULL_SPACE, ERR_READING_BOOTSTRAP_SPACES, STATE_ERROR, STATE_INITIALIZING, STATE_NEW, STATE_RUNNING, WARN_PICKING_DEFAULT
 
Fields inherited from class atg.service.idgen.AbstractSequentialIdGenerator
ERR_BAD_SPACE, ERR_ROLLBACK, ERR_UNSUPPORTED, mSpaces, RESOURCE_BUNDLE_NAME, sResourceBundle
 
Fields inherited from class atg.nucleus.GenericService
SERVICE_INFO_KEY
 
Fields inherited from interface atg.nucleus.logging.TraceApplicationLogging
DEFAULT_LOG_TRACE_STATUS
 
Fields inherited from interface atg.nucleus.logging.ApplicationLogging
DEFAULT_LOG_DEBUG_STATUS, DEFAULT_LOG_ERROR_STATUS, DEFAULT_LOG_INFO_STATUS, DEFAULT_LOG_WARNING_STATUS
 
Constructor Summary
ObfuscatedSQLIdGenerator()
          Construct a generator
 
Method Summary
 boolean addIdSpace(IdSpace pSpace)
          Add an id space to the id generator.
protected  boolean addPersistentIdSpace(IdSpace pSpace)
          Add an id space to the data store.
protected  void createNewDefaultIdSpace()
          Make sure the default id space exists, creating it if necessary.
 boolean getHexEncode()
          Get property hexEncode
 java.lang.String getIdsPerBatchColumn()
          Get property idsPerBatchColumn
 java.lang.String getInsertSQL()
          Get property InsertSQL.
 java.lang.String getSelectSQL()
          Get property SelectSQL.
 java.lang.String getUpdateSQL()
          Get property UpdateSQL.
 boolean isHexEncode()
          Test property hexEncode
protected  void reserveSeeds(IdSpace pSpace)
          Reserve a batch of seeds for the specified id space.
 void setHexEncode(boolean pHexEncode)
          Set property hexEncode
 void setIdsPerBatchColumn(java.lang.String pIdsPerBatchColumn)
          Set property idsPerBatchColumn
 
Methods inherited from class atg.service.idgen.SQLIdGenerator
getBatchSizeColumn, getDataSource, getNameColumn, getPrefixColumn, getSeedColumn, getSuffixColumn, getTableName, getUseRequiredTransactionMode, initializeIdSpaces, removePersistentIdSpaces, setBatchSizeColumn, setDataSource, setHighWaterMark, setNameColumn, setPrefixColumn, setSeedColumn, setSuffixColumn, setTableName, setUseRequiredTransactionMode
 
Methods inherited from class atg.service.idgen.PersistentIdGenerator
getInitialIdSpaces, getLoadInitialIdSpacesAtStartup, getTransactionManager, getXMLToolsFactory, initialize, removeAllIdSpaces, removeIdSpace, setInitialIdSpaces, setLoadInitialIdSpacesAtStartup, setTransactionManager, setXMLToolsFactory
 
Methods inherited from class atg.service.idgen.AbstractSequentialIdGenerator
doStartService, format, format, format, format, generateLongId, generateLongId, generateStringId, generateStringId, getAutoCreate, getBundle, getDefaultIdSpace, getDefaultIdSpaceName, getIdSpace, getIdSpaces, getResourceBundleName, isAutoCreate, postGenerateLongId, postGenerateStringId, setAutoCreate, setDefaultIdSpace, setDefaultIdSpaceName, toString
 
Methods inherited from class atg.nucleus.GenericService
addLogListener, createAdminServlet, doStopService, getAbsoluteName, getAdminServlet, getAdminServletOutputStreamEncoding, getLoggingForVlogging, getLogListenerCount, getLogListeners, getName, getNameContext, getNucleus, getRoot, getServiceConfiguration, getServiceInfo, isAdminServletUseServletOutputStream, isLoggingDebug, isLoggingError, isLoggingInfo, isLoggingTrace, isLoggingWarning, isRunning, logDebug, logDebug, logDebug, logError, logError, logError, logInfo, logInfo, logInfo, logTrace, logTrace, logTrace, logWarning, logWarning, logWarning, nameContextElementBound, nameContextElementUnbound, removeLogListener, reResolveThis, resolveName, resolveName, resolveName, resolveName, sendLogEvent, setAdminServletOutputStreamEncoding, setAdminServletUseServletOutputStream, setLoggingDebug, setLoggingError, setLoggingInfo, setLoggingTrace, setLoggingWarning, setNucleus, setServiceInfo, startService, stopService
 
Methods inherited from class atg.nucleus.logging.VariableArgumentApplicationLoggingImpl
vlogDebug, vlogDebug, vlogDebug, vlogDebug, vlogDebugTrace, vlogError, vlogError, vlogError, vlogError, vlogInfo, vlogInfo, vlogInfo, vlogInfo, vlogTrace, vlogTrace, vlogTrace, vlogTrace, vlogWarning, vlogWarning, vlogWarning, vlogWarning
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface atg.service.idgen.IdGenerator
generateLongId, generateLongId, generateStringId, generateStringId, getAutoCreate, getDefaultIdSpace, getIdSpace, getIdSpaces, isAutoCreate, setAutoCreate
 

Field Detail

CLASS_VERSION

public static java.lang.String CLASS_VERSION
Class version string


MAX_DENSITY

public static float MAX_DENSITY
maximum density for obfuscated id generators


ERR_TOO_DENSE

protected static java.lang.String ERR_TOO_DENSE
Constructor Detail

ObfuscatedSQLIdGenerator

public ObfuscatedSQLIdGenerator()
Construct a generator

Method Detail

setIdsPerBatchColumn

public void setIdsPerBatchColumn(java.lang.String pIdsPerBatchColumn)
Set property idsPerBatchColumn

Parameters:
pIdsPerBatchColumn - new value to set

getIdsPerBatchColumn

public java.lang.String getIdsPerBatchColumn()
Get property idsPerBatchColumn

Returns:
idsPerBatchColumn

setHexEncode

public void setHexEncode(boolean pHexEncode)
Set property hexEncode

Parameters:
pHexEncode - new value to set

getHexEncode

public boolean getHexEncode()
Get property hexEncode

Returns:
hexEncode

isHexEncode

public boolean isHexEncode()
Test property hexEncode

Returns:
hexEncode

getSelectSQL

public java.lang.String getSelectSQL()
Get property SelectSQL. The SQL is lazily generated.

Overrides:
getSelectSQL in class SQLIdGenerator
Returns:
SelectSQL

getInsertSQL

public java.lang.String getInsertSQL()
Get property InsertSQL. The SQL is lazily generated.

Overrides:
getInsertSQL in class SQLIdGenerator
Returns:
InsertSQL

getUpdateSQL

public java.lang.String getUpdateSQL()
Get property UpdateSQL. The SQL is lazily generated.

Overrides:
getUpdateSQL in class SQLIdGenerator
Returns:
UpdateSQL

addIdSpace

public boolean addIdSpace(IdSpace pSpace)
                   throws IdGeneratorException
Add an id space to the id generator. The id space must not be null. This method ensures that there is an at least somewhat reasonable value for the idsPerBatch property of the id space. Somewhat reasonable means a prime number not large than roughly 1% of the batch size:
batchSize idsPerBatch
>=100,000997
>=10,00089
>=else7

Specified by:
addIdSpace in interface IdGenerator
Overrides:
addIdSpace in class PersistentIdGenerator
Parameters:
pSpace - space to add, cannot be null
Returns:
true if the space is added, or false if a space with the same name already exists in this id generator
Throws:
IdGeneratorException - if there is trouble adding the space

addPersistentIdSpace

protected boolean addPersistentIdSpace(IdSpace pSpace)
                                throws IdGeneratorException
Add an id space to the data store. This should only be called from addIdSpace().

Overrides:
addPersistentIdSpace in class SQLIdGenerator
Parameters:
pSpace - space to add, cannot be null
Returns:
true if the space is added, or false if a space with the same name already exists in this id generator
Throws:
IdGeneratorException - if there is trouble adding id space

createNewDefaultIdSpace

protected void createNewDefaultIdSpace()
Make sure the default id space exists, creating it if necessary.


reserveSeeds

protected void reserveSeeds(IdSpace pSpace)
                     throws IdGeneratorException
Reserve a batch of seeds for the specified id space. This method updates the id space itself with the new seed information. This method gets a connection from the DataSource for use and closes that connection when done. This method also runs in its own new transaction.

Overrides:
reserveSeeds in class SQLIdGenerator
Parameters:
pSpace - id space to reserve seeds in, cannot be null
Throws:
IdGeneratorException - if there is DB trouble