Last active
February 8, 2016 06:40
-
-
Save ProgrammerDan/b4755434137748219f8c to your computer and use it in GitHub Desktop.
Needs fixing up but borrowed from http://braveo.blogspot.com/2013/05/uuidrandomuuid-is-slow.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.net.InetAddress; | |
import java.net.NetworkInterface; | |
import java.util.Random; | |
import java.util.UUID; | |
import java.util.concurrent.atomic.AtomicLong; | |
import org.apache.commons.logging.Log; | |
import org.apache.commons.logging.LogFactory; | |
import org.springframework.integration.MessageHeaders.IdGenerator; | |
public class MyIdGenerator { | |
private final long hostId = getHostId(); | |
private final static int mask = 0xFFFF; | |
private final static long longMask = mask; | |
private final AtomicLong lastNano = new AtomicLong(); | |
private final AtomicLong counter = new AtomicLong(); | |
private static final long [] DISTURBUNCES = new long[mask + 1]; | |
private static final long LUCKY = -2079234210399081837L; | |
static | |
{ | |
Random r = new Random(); | |
for(int k=0; k<DISTURBUNCES.length; ++k) | |
{ | |
DISTURBUNCES[k] = k; | |
} | |
for(int k=0; k<DISTURBUNCES.length; ++k) | |
{ | |
int next = r.nextInt(mask); | |
long tmp = DISTURBUNCES[k]; | |
DISTURBUNCES[k] = DISTURBUNCES[next]; | |
DISTURBUNCES[next] = tmp; | |
} | |
} | |
private static long getHostId() { | |
try { | |
InetAddress address = InetAddress.getLocalHost(); | |
NetworkInterface netInterface = NetworkInterface.getByInetAddress(address); | |
if(netInterface != null) { | |
byte[] mac = netInterface.getHardwareAddress(); | |
long value = 0; | |
for(int k=0; k<mac.length; ++k) { | |
value |= ((long)mac[k] & 0xFF) << (( 8 - k - 1 ) * 8); | |
} | |
return value; | |
} | |
return new Random().nextLong(); //return | |
} catch (Exception ex) { | |
return new Random().nextLong(); //return | |
} | |
} | |
public UUID generateId() { | |
long currentNano = System.nanoTime(); | |
boolean replaced = lastNano.compareAndSet(0, currentNano); | |
while(!replaced) | |
{ | |
long cLast = lastNano.get(); | |
if(currentNano > cLast) | |
{ | |
replaced = lastNano.compareAndSet(cLast, currentNano); | |
} | |
else | |
{ | |
currentNano = lastNano.incrementAndGet(); | |
replaced = true; | |
} | |
} | |
long myCounter = counter.incrementAndGet(); | |
long disturbance = DISTURBUNCES[(int)(myCounter & longMask)]; | |
long lessSig = ((currentNano >> 6 * 8) & longMask) | hostId ^ LUCKY; | |
long v = currentNano & ~ ( longMask << 6 * 8); | |
long mostSig = v | (disturbance << 6 * 8); | |
return new UUID(mostSig, lessSig); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment