org.equanda.persistence
Class UoidGenerator
java.lang.Object
org.equanda.persistence.UoidGenerator
public final class UoidGenerator
- extends Object
generator for Uoid values for the newly created records We need a universal object if which is fast to generate, has
a minimal (no) configuration overhead and cross platform. In accordance with Scott Ambler's article about this
("Enterprise Ready Object IDs", @link http://www.sdmagazine.com/print/documentID=11250) we use a HIGH-LOW scheme.
Values are unique accross tables and normally also accross databases. This is very important when a database is
remotely added to (eg on a portable) and new data has to be merged later.
I am concerned about mistakes caused
by improper configuration and therefore do not want to use a configurable HIGH value. A PC's IP address is not
guaranteed unique when PC's are connecting and disconnecting from the network. I do not know how to obtain a network
card's MAC address in a cross platform method, so I have to search for a different mechanism. A popular fix to this
is using a database table to store generated values. I don't like that because lack of transactions can make this
very dangerous on some databases. The idea is to use the time of initialisation of this class as base for the
HIGH address. This should prove to be quite random and chances are very, very slim that a second system which needs
the same database will also use this timestamp as base for HIGH value. However, to decrease the likelyhood even
further, the HIGH value also contains a random number which does not use the time at it's seed. For LOW values
we just use a counter and we force a new HIGH value when the counter runs out. Contrary to normal practice, the LOW
counter does not start at zero, but at a random number. This increases the distribution of Uoid values (which should
improve the quality of the indexes). Also, in the unlikely event that HIGH values would clash, this reduces the
chance of duplicate Uoid values. The Uoid consists of a 36bit time, and 30bit random number, giving a 66bit high
value, and a 18bit LOW value, giving a 84 bit key. This is converted to a 14 digit String using base64 encoding.
However, the digits are encoded back to front to make the Uoid strings discriminate from the start. There are
some things which can be chosen in this scheme. The random part of the HIGH value could be regenerated for each Uoid
(giving a HIGH/RAND/LOW scheme), or just once (as in true HIGH/LOW scheme). When going for a once generated random,
the question is whether you change just the time part or both time and random parts when you run out of LOW values
and have to generate a new HIGH as a result of that.
- Author:
- Joachim Van der Auwera
Method Summary |
static Uoid |
get()
get a new Uoid |
Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
get
public static Uoid get()
- get a new Uoid
- Returns:
- Uoid (14 character String)
Copyright © 2007-2009. All Rights Reserved.