ERXLongPrimaryKeyFactory Class Reference

Collaboration diagram for ERXLongPrimaryKeyFactory:

Collaboration graph
[legend]

List of all members.

Public Member Functions

EOEntity subEntityForEntity (EOEntity entity, NSDictionary pkDict)

Static Public Member Functions

static synchronized NSDictionary primaryKeyDictionary (EOEnterpriseObject eo)
static synchronized Object primaryKeyValue (String entityName)

Private Member Functions

Long cachedPkValue (String ename)
Stack cacheStack (String ename)
boolean encodeEntityInPkValue ()
boolean encodeHostInPkValue ()
void fillPkCache (Stack s, String ename)
Long getNextPkValueForEntity (String ename)
Long getNextPkValueForEntityIncreaseBy (String entityName, int count, int increasePkBy)
int hostCode ()
int increaseBy ()
long maxIdFromTable (String ename)
NSDictionary primaryKeyDictionary (String entityName)

Static Private Member Functions

static ERXLongPrimaryKeyFactory factory ()

Private Attributes

Boolean encodeEntityInPkValue
Boolean encodeHostInPkValue
Integer hostCode
Integer increaseBy
Hashtable pkCache = new Hashtable()

Static Private Attributes

static ERXLongPrimaryKeyFactory _factory
static final int CODE_LENGTH = 6
static final String HOST_CODE_KEY = "er.extensions.ERXLongPrimaryKeyFactory.hostCode"
static final int HOST_CODE_LENGTH = 10
static final Logger log = Logger.getLogger(ERXLongPrimaryKeyFactory.class)
static long MAX_PK_VALUE = (long) Math.pow(2, 48)


Detailed Description

Author:
david@cluster9.com

Automatically generates Long primary keys for entities. Features a cache which reduces database roundtrips as well as optionally encoding Entity type in PK value.

usage:

override either the ERXGeneratesPrimaryKey interface like this:
 private NSDictionary _primaryKeyDictionary = null;
public NSDictionary primaryKeyDictionary(boolean inTransaction) { if (_primaryKeyDictionary == null) { _primaryKeyDictionary = ERXLongPrimaryKeyFactory.primaryKeyDictionary(this); } return _primaryKeyDictionary; }
or manually call
ERXLongPrimaryKeyFactory.primaryKeyDictionary(EOEnterpriseObject eo);

the necessary database table is generated on the fly.

Encoding Entity in PK values
If the system property ERXIntegerPrimaryKeyFactory.encodeEntityInPkValue is set to true then the last 6 bits from the 64 bit primary key is used to encode the Subentity in the pk value. This speeds up inheritance with multiple tables. In order to support this you must add an entry to the userInfo from the Subentities:

key=entityCode
value= lt;lt; an unique integer, no longer than 6 bit - 1

Member Function Documentation

Long cachedPkValue ( String  ename  )  [private]

Returns a new integer based PkValue for the specified entity. If the cache is empty it is refilled again.

Parameters:
ename,the entity name for which this method should return a new primary key
Returns:
a new Integer based primary key for the specified entity.

Stack cacheStack ( String  ename  )  [private]

looks in the cache hashtable if there is already an Stack for the specified entity name. If there is no Stack a new Stack object will be created.

Parameters:
ename,the name of the entity for which this method should return the Stack
Returns:
the Stack with primary key values for the specified entity.

boolean encodeEntityInPkValue (  )  [private]

boolean encodeHostInPkValue (  )  [private]

static ERXLongPrimaryKeyFactory factory (  )  [static, private]

void fillPkCache ( Stack  s,
String  ename 
) [private]

creates x primary key values for the specified entity and updates the database, where x is the number specified in increaseBy

Parameters:
s,the stack into which the pk values should be inserted
ename,the entity name for which the pk values should be generated

Long getNextPkValueForEntity ( String  ename  )  [private]

Long getNextPkValueForEntityIncreaseBy ( String  entityName,
int  count,
int  increasePkBy 
) [private]

returns a new primary key for the specified entity.

Parameters:
ename,the entity name for which this method should return a new primary key
count,the number of times the method should try to get a value from the database if something went wrong (a deadlock in the db for example -> high traffic with multiple instances)
increaseBy,if > 1 then the value in the database is increased by this factor. This is usefull to 'get' 10000 pk values at once for caching. Removes a lot of db roundtrips.
Returns:
a new pk values for the specified entity.

int hostCode (  )  [private]

int increaseBy (  )  [private]

The amount of cached keys, set the property er.extensions.ERXLongPrimaryKeyFactory.increaseBy to the interval you want to use.

Returns:
the interval to use for cached keys

long maxIdFromTable ( String  ename  )  [private]

Retrieves the maxValue from id from the specified entity. If hosts and entities are encoded, then these values are stripped first

Parameters:
ename 
pk 
Returns:

NSDictionary primaryKeyDictionary ( String  entityName  )  [private]

static synchronized NSDictionary primaryKeyDictionary ( EOEnterpriseObject  eo  )  [static]

static synchronized Object primaryKeyValue ( String  entityName  )  [static]

EOEntity subEntityForEntity ( EOEntity  entity,
NSDictionary  pkDict 
)


Member Data Documentation

final int CODE_LENGTH = 6 [static, private]

Boolean encodeEntityInPkValue [private]

Boolean encodeHostInPkValue [private]

final String HOST_CODE_KEY = "er.extensions.ERXLongPrimaryKeyFactory.hostCode" [static, private]

final int HOST_CODE_LENGTH = 10 [static, private]

Integer hostCode [private]

Integer increaseBy [private]

final Logger log = Logger.getLogger(ERXLongPrimaryKeyFactory.class) [static, private]

long MAX_PK_VALUE = (long) Math.pow(2, 48) [static, private]

Hashtable pkCache = new Hashtable() [private]


The documentation for this class was generated from the following file:

Generated on Sat May 26 06:43:11 2012 for Project Wonder by  doxygen 1.5.8