org.apache.avalon.excalibur.datasource.ids
Class TableIdGenerator

java.lang.Object
  extended by org.apache.avalon.framework.logger.AbstractLogEnabled
      extended by org.apache.avalon.excalibur.datasource.ids.AbstractIdGenerator
          extended by org.apache.avalon.excalibur.datasource.ids.AbstractDataSourceIdGenerator
              extended by org.apache.avalon.excalibur.datasource.ids.AbstractDataSourceBlockIdGenerator
                  extended by org.apache.avalon.excalibur.datasource.ids.TableIdGenerator
All Implemented Interfaces:
IdGenerator, org.apache.avalon.framework.activity.Disposable, org.apache.avalon.framework.activity.Initializable, org.apache.avalon.framework.component.Component, org.apache.avalon.framework.configuration.Configurable, org.apache.avalon.framework.logger.LogEnabled, org.apache.avalon.framework.service.Serviceable, org.apache.avalon.framework.thread.ThreadSafe

public class TableIdGenerator
extends AbstractDataSourceBlockIdGenerator

The TableIdGenerator requests blocks of ids from a Database table. The table consists of two columns one called table_name of type CHAR or VARCHAR, and the second called next_id of an integer type large enough to hold your largest ids.

The Configuration to use a TableIdGenerator looks like the following:

   <id-generators>
       <table name="user-ids" big-decimals="true" block-size="1" table="ids"
           key-table="event-type" logger="cm.ids">
           <dbpool>user-db</dbpool>
       </table>
   </id-generators>
 
Where user-db is the name of a DataSource configured in a datasources element, block-size is the number if ids that are allocated with each query to the databse (defaults to "10"), table is the name of the table which contains the ids (defaults to "ids"), and key-table is the table_name of the row from which the block of ids are allocated (defaults to "id").

With the following roles declaration:

   <role name="org.apache.avalon.excalibur.datasource.ids.IdGeneratorSelector"
         shorthand="id-generators"
         default-class="org.apache.avalon.excalibur.component.ExcaliburComponentSelector">
       <hint shorthand="table"
             class="org.apache.avalon.excalibur.datasource.ids.TableIdGenerator"/>
   </role>
 
To configure your component to use the IdGenerator declared above, its configuration should look something like the following:
   <user-service logger="cm">
       <dbpool>user-db</dbpool>
       <id-generator>user-ids</id-generator>
   </user-service>
 
Your component obtains a reference to an IdGenerator using the same method as it obtains a DataSource, by making use of a ComponentSelector.

Depending on your database, the ids table should look something like the following:

   CREATE TABLE ids (
       table_name varchar(16) NOT NULL,
       next_id INTEGER NOT NULL,
       PRIMARY KEY (table_name)
   );
 

Since:
4.1
Version:
CVS $Revision: 1.4 $ $Date: 2004/02/28 11:47:17 $
Author:
Avalon Development Team

Field Summary
private  java.lang.String m_table
          The name of the table containing the ids.
private  java.lang.String m_tableName
          TableName used to reference which ids to allocate.
 
Fields inherited from class org.apache.avalon.excalibur.datasource.ids.AbstractDataSourceIdGenerator
m_dataSource, m_nextId
 
Fields inherited from interface org.apache.avalon.excalibur.datasource.ids.IdGenerator
ROLE
 
Constructor Summary
TableIdGenerator()
           
 
Method Summary
protected  java.math.BigDecimal allocateBigDecimalIdBlock(int blockSize)
          Allocates a block, of the given size, of ids from the database.
private  java.lang.Object allocateIdBlock(int blockSize, boolean useBigDecimals)
          Allocates a block of ids of the given size and returns the first id.
protected  long allocateLongIdBlock(int blockSize)
          Allocates a block, of the given size, of ids from the database.
 void configure(org.apache.avalon.framework.configuration.Configuration configuration)
          Called by the Container to configure the component.
 
Methods inherited from class org.apache.avalon.excalibur.datasource.ids.AbstractDataSourceBlockIdGenerator
getNextBigDecimalIdInner, getNextLongIdInner, initialize
 
Methods inherited from class org.apache.avalon.excalibur.datasource.ids.AbstractDataSourceIdGenerator
dispose, getConnection, service
 
Methods inherited from class org.apache.avalon.excalibur.datasource.ids.AbstractIdGenerator
getNextBigDecimalId, getNextByteId, getNextIntegerId, getNextLongId, getNextLongIdChecked, getNextShortId, isUsingBigDecimals, setUseBigDecimals
 
Methods inherited from class org.apache.avalon.framework.logger.AbstractLogEnabled
enableLogging, getLogger, setupLogger, setupLogger, setupLogger
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

m_table

private java.lang.String m_table
The name of the table containing the ids.


m_tableName

private java.lang.String m_tableName
TableName used to reference which ids to allocate.

Constructor Detail

TableIdGenerator

public TableIdGenerator()
Method Detail

allocateIdBlock

private java.lang.Object allocateIdBlock(int blockSize,
                                         boolean useBigDecimals)
                                  throws IdException
Allocates a block of ids of the given size and returns the first id.

Parameters:
blockSize - number of ids to allocate.
useBigDecimals - returns the first id as a BigDecimal if true, otherwise as a Long.
Returns:
either a Long or a BigDecimal depending on the value of useBigDecimals
Throws:
IdException - if a block of ids can not be allocated.

allocateBigDecimalIdBlock

protected java.math.BigDecimal allocateBigDecimalIdBlock(int blockSize)
                                                  throws IdException
Allocates a block, of the given size, of ids from the database.

Specified by:
allocateBigDecimalIdBlock in class AbstractDataSourceBlockIdGenerator
Parameters:
blockSize - number of Ids which are to be allocated.
Returns:
The first id in the allocated block.
Throws:
IdException - if there it was not possible to allocate a block of ids.

allocateLongIdBlock

protected long allocateLongIdBlock(int blockSize)
                            throws IdException
Allocates a block, of the given size, of ids from the database.

Specified by:
allocateLongIdBlock in class AbstractDataSourceBlockIdGenerator
Parameters:
blockSize - number of Ids which are to be allocated.
Returns:
The first id in the allocated block.
Throws:
IdException - if there it was not possible to allocate a block of ids.

configure

public void configure(org.apache.avalon.framework.configuration.Configuration configuration)
               throws org.apache.avalon.framework.configuration.ConfigurationException
Called by the Container to configure the component.

Specified by:
configure in interface org.apache.avalon.framework.configuration.Configurable
Overrides:
configure in class AbstractDataSourceBlockIdGenerator
Parameters:
configuration - configuration info used to setup the component.
Throws:
org.apache.avalon.framework.configuration.ConfigurationException - if there are any problems with the configuration.