org.sandev.tools.JDBC
Class THD_TableKeyCounter

java.lang.Object
  extended byorg.sandev.tools.JDBC.THD_TableKeyCounter
All Implemented Interfaces:
org.sandev.basics.DataManager.UniqueIDManager

public class THD_TableKeyCounter
extends java.lang.Object
implements org.sandev.basics.DataManager.UniqueIDManager

A UniqueIDManager that finds the lowest unused uniqueID across all tables in the database, and then keeps track of the next available uniqueID via a counter kept in memory. It is nearly optimal with respect to overall keyspace utilization, but does not recycle keys. AUTOGENERATED FILE, DO NOT EDIT DIRECTLY.
source: declared in build
generator: org.sandev.generator.TableKeyCounterGenerator
time: Dec 2, 2005 6:38:59 PM

The TableKeyCounter is not appropriate for use in situations where the in-memory counter value may overlap with other database users. This could lead to primary key conflicts if additional rows are inserted while the app is running, since the in-memory will not be updated.

The TableKeyCounter assumes that the uniqueID values will write directly into the target database. If the storage of a java long value is smaller than 64bits, then target database will be limited to that maximum number of records. In the case where the target db holds smaller values (such as 32bits), then the recommended alternative is to use a UniqueIDManager with numbered tables and a separate counter for each table. IDs in high usage tables can then be rolled over without affecting the keys for other tables, allowing overall data growth to be contained within the limited keyspace while preserving critical data.

For example you might use the top 6 bits for the persistent message type (table ID), to allow for up to 64 different persistent object types, and the bottom 26 bits for the persistent message uniqueID values, to provide for up to 67,108,864 object instances per message type. By archiving and deleting older data over the working life of the app, the high usage tables can safely wrap back to zero while maintaining data integrity. It is also possible to use this approach only for the high volume tables and use a TableKeyCounter approach for the rest. Custom UniqueIDManagers are developed and optimized on a per application basis since the design tradeoffs are application specific.


Field Summary
protected  long highest
          The current highest ID in use.
 
Constructor Summary
THD_TableKeyCounter()
          default ctor, does nothing
 
Method Summary
protected  long getMessageID(org.sandev.basics.structs.SandUpdateMessage sum)
          Gets the instance out of the given update message, downcasts it to a SandPeristMessage and returns its uniqueID.
protected  long getNextID()
          Return the next available uniqueID for this message class.
 long getNextID(org.sandev.basics.structs.SandUpdateMessage msg)
          Return the next available uniqueID for this message class.
 void init(org.sandev.basics.DataManager.DataManagerNode dm)
          Set up for work.
 void notifyImport(org.sandev.basics.structs.SandPersistMessage spm)
          Make sure our uniqueID counter is one more than the given value, and update if not.
 void remapIDs(long oldID, org.sandev.basics.structs.SandUpdateMessage msg, org.sandev.basics.structs.SandUpdateMessage[] remaps)
          Walk through the remap messages specified and change any oldID references to the new ID.
protected  void setMessageID(org.sandev.basics.structs.SandUpdateMessage sum, long val)
          Gets the instance out of the given update message, downcasts it to a SandPeristMessage and sets its uniqueID.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

highest

protected long highest
The current highest ID in use.

Constructor Detail

THD_TableKeyCounter

public THD_TableKeyCounter()
default ctor, does nothing

Method Detail

getNextID

protected long getNextID()
Return the next available uniqueID for this message class. One plus the current highest value.


getMessageID

protected long getMessageID(org.sandev.basics.structs.SandUpdateMessage sum)
Gets the instance out of the given update message, downcasts it to a SandPeristMessage and returns its uniqueID.


setMessageID

protected void setMessageID(org.sandev.basics.structs.SandUpdateMessage sum,
                            long val)
Gets the instance out of the given update message, downcasts it to a SandPeristMessage and sets its uniqueID.


init

public void init(org.sandev.basics.DataManager.DataManagerNode dm)
          throws org.sandev.basics.DataManager.PersisterException
Set up for work. This method is called once before any persistence work is done. The implementation assumes that the DataManager is using a JDBCPersister, which we access to get a database connection.

Specified by:
init in interface org.sandev.basics.DataManager.UniqueIDManager
Throws:
org.sandev.basics.DataManager.PersisterException

getNextID

public long getNextID(org.sandev.basics.structs.SandUpdateMessage msg)
               throws org.sandev.basics.DataManager.PersisterException
Return the next available uniqueID for this message class. This method must operate in an atomic fashion so that two of the same IDs will never be returned regardless of the number of threads running concurrently.

Specified by:
getNextID in interface org.sandev.basics.DataManager.UniqueIDManager
Throws:
org.sandev.basics.DataManager.PersisterException

remapIDs

public void remapIDs(long oldID,
                     org.sandev.basics.structs.SandUpdateMessage msg,
                     org.sandev.basics.structs.SandUpdateMessage[] remaps)
              throws org.sandev.basics.DataManager.PersisterException
Walk through the remap messages specified and change any oldID references to the new ID. The oldID is given, and the message which was changed to a new ID is also given. The remaps is an array of all the messages to be remapped, and they are changed in place. The message with the new ID is given so that it can be identified (if necessary) within the array of remaps.

Specified by:
remapIDs in interface org.sandev.basics.DataManager.UniqueIDManager
Throws:
org.sandev.basics.DataManager.PersisterException

notifyImport

public void notifyImport(org.sandev.basics.structs.SandPersistMessage spm)
                  throws org.sandev.basics.DataManager.PersisterException
Make sure our uniqueID counter is one more than the given value, and update if not. Note that we are assuming that nobody is trying to do a regular update at the same time, otherwise we could have an ID collision.

Specified by:
notifyImport in interface org.sandev.basics.DataManager.UniqueIDManager
Throws:
org.sandev.basics.DataManager.PersisterException