[DEV] generate a definable UUID
This commit is contained in:
parent
0fe769a203
commit
d35c83fcbf
2
pom.xml
2
pom.xml
@ -3,7 +3,7 @@
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<groupId>kangaroo-and-rabbit</groupId>
|
||||
<artifactId>archidata</artifactId>
|
||||
<version>0.7.0-dev</version>
|
||||
<version>0.7.1</version>
|
||||
<properties>
|
||||
<maven.compiler.version>3.1</maven.compiler.version>
|
||||
<maven.compiler.source>21</maven.compiler.source>
|
||||
|
@ -835,7 +835,8 @@ public class DataAccess {
|
||||
UUID uuid = null;
|
||||
if (generateUUID) {
|
||||
firstField = false;
|
||||
uuid = UUID.randomUUID();
|
||||
// uuid = UUID.randomUUID();
|
||||
uuid = UuidUtils.nextUUID();
|
||||
addElement(ps, uuid, iii);
|
||||
iii.inc();
|
||||
}
|
||||
|
@ -2,6 +2,10 @@ package org.kar.archidata.tools;
|
||||
|
||||
import java.math.BigInteger;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.time.Instant;
|
||||
import java.time.LocalDate;
|
||||
import java.time.ZoneOffset;
|
||||
import java.time.temporal.ChronoUnit;
|
||||
import java.util.UUID;
|
||||
|
||||
public class UuidUtils {
|
||||
@ -25,4 +29,41 @@ public class UuidUtils {
|
||||
bb.putLong(uuid.getLeastSignificantBits());
|
||||
return bb.array();
|
||||
}
|
||||
|
||||
private static class Generator {
|
||||
private long base;
|
||||
private final long offset;
|
||||
private long previous;
|
||||
|
||||
public Generator() {
|
||||
this.offset = System.currentTimeMillis();
|
||||
// The local method never generate new UUID in the past, then we use the creation function time to prevent 2038 error
|
||||
final Instant startingUUID = LocalDate.of(2024, 03, 19).atStartOfDay(ZoneOffset.UTC).toInstant();
|
||||
this.base = startingUUID.until(Instant.now(), ChronoUnit.SECONDS);
|
||||
final String serveurBaseUUID = System.getenv("UUID_SERVER_ID");
|
||||
if (serveurBaseUUID != null) {
|
||||
long serverId = Long.valueOf(serveurBaseUUID);
|
||||
serverId %= 0xFFFF;
|
||||
this.base += (serverId << (64 - 16));
|
||||
} else {
|
||||
this.base += (1L << (64 - 16));
|
||||
}
|
||||
}
|
||||
|
||||
public synchronized UUID next() {
|
||||
long tmp = System.currentTimeMillis();
|
||||
if (this.previous >= tmp) {
|
||||
tmp = this.previous + 1;
|
||||
}
|
||||
this.previous = tmp;
|
||||
tmp -= this.offset;
|
||||
return new UUID(Long.reverseBytes(tmp), this.base);
|
||||
}
|
||||
}
|
||||
|
||||
private static Generator generator = new Generator();
|
||||
|
||||
public static UUID nextUUID() {
|
||||
return generator.next();
|
||||
}
|
||||
}
|
||||
|
@ -1 +1 @@
|
||||
0.7.0-dev
|
||||
0.7.1
|
||||
|
Loading…
Reference in New Issue
Block a user