|
|
@ -24,13 +24,13 @@ import java.util.Objects; |
|
|
|
public class SnowFlakeUtils { |
|
|
|
public class SnowFlakeUtils { |
|
|
|
// start timestamp
|
|
|
|
// start timestamp
|
|
|
|
private static final long START_TIMESTAMP = 1609430400000L; //2021-01-01 00:00:00
|
|
|
|
private static final long START_TIMESTAMP = 1609430400000L; //2021-01-01 00:00:00
|
|
|
|
// Number of digits
|
|
|
|
// Each machine generates 32 in the same millisecond
|
|
|
|
private static final long SEQUENCE_BIT = 13; |
|
|
|
private static final long SEQUENCE_BIT = 5; |
|
|
|
private static final long MACHINE_BIT = 2; |
|
|
|
private static final long MACHINE_BIT = 2; |
|
|
|
private static final long MAX_SEQUENCE = ~(-1L << SEQUENCE_BIT); |
|
|
|
private static final long MAX_SEQUENCE = ~(-1L << SEQUENCE_BIT); |
|
|
|
// The displacement to the left
|
|
|
|
// The displacement to the left
|
|
|
|
private static final long MACHINE_LEFT = SEQUENCE_BIT; |
|
|
|
private static final long MACHINE_LEFT = SEQUENCE_BIT + MACHINE_BIT; |
|
|
|
private static final long TIMESTAMP_LEFT = SEQUENCE_BIT + MACHINE_BIT; |
|
|
|
private static final long TIMESTAMP_LEFT = SEQUENCE_BIT + MACHINE_BIT + MACHINE_LEFT; |
|
|
|
private final int machineId; |
|
|
|
private final int machineId; |
|
|
|
private long sequence = 0L; |
|
|
|
private long sequence = 0L; |
|
|
|
private long lastTimestamp = -1L; |
|
|
|
private long lastTimestamp = -1L; |
|
|
@ -40,7 +40,7 @@ public class SnowFlakeUtils { |
|
|
|
|
|
|
|
|
|
|
|
private SnowFlakeUtils() throws SnowFlakeException { |
|
|
|
private SnowFlakeUtils() throws SnowFlakeException { |
|
|
|
try { |
|
|
|
try { |
|
|
|
this.machineId = Math.abs(Objects.hash(InetAddress.getLocalHost().getHostName())) % 32; |
|
|
|
this.machineId = Math.abs(Objects.hash(InetAddress.getLocalHost().getHostName())) % 4; |
|
|
|
} catch (UnknownHostException e) { |
|
|
|
} catch (UnknownHostException e) { |
|
|
|
throw new SnowFlakeException(e.getMessage()); |
|
|
|
throw new SnowFlakeException(e.getMessage()); |
|
|
|
} |
|
|
|
} |
|
|
|