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

java.lang.Object
  extended byorg.apache.avalon.framework.logger.AbstractLogEnabled
      extended byorg.apache.avalon.excalibur.datasource.ids.AbstractIdGenerator
          extended byorg.apache.avalon.excalibur.datasource.ids.AbstractDataSourceIdGenerator
              extended byorg.apache.avalon.excalibur.datasource.ids.AbstractDataSourceBlockIdGenerator
                  extended byorg.apache.avalon.excalibur.datasource.ids.TableIdGenerator
All Implemented Interfaces:
Component, Configurable, Disposable, IdGenerator, Initializable, LogEnabled, Serviceable, 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
 
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.
protected  long allocateLongIdBlock(int blockSize)
          Allocates a block, of the given size, of ids from the database.
 void configure(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
 

Constructor Detail

TableIdGenerator

public TableIdGenerator()
Method Detail

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(Configuration configuration)
               throws ConfigurationException
Called by the Container to configure the component.

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


Copyright © 1997-2005 The Apache Software Foundation. All Rights Reserved.