重构StartTime
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
|
||||
## <20><><EFBFBD>л<EFBFBD><D0BB><EFBFBD>
|
||||
|
||||
JDK 1.8
|
||||
JDK 1.8+
|
||||
|
||||
## <20><><EFBFBD><EFBFBD> maven <20><>
|
||||
```
|
||||
@@ -21,56 +21,8 @@ long newId = IdHelper.nextId();
|
||||
```
|
||||
<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>DI<EFBFBD><EFBFBD><EFBFBD>ܼ<EFBFBD><EFBFBD>ɣ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>Բο<EFBFBD> IdHelper ȥ<><C8A5><EFBFBD><EFBFBD> IdGenerator <20><><EFBFBD><EFBFBD><F3A3ACB1><EFBFBD>ʹ<EFBFBD><CAB9>**<2A><><EFBFBD><EFBFBD>**ģʽ<C4A3><CABD>
|
||||
|
||||
## options˵<73><CBB5>
|
||||
```
|
||||
/**
|
||||
* ѩ<><D1A9><EFBFBD><EFBFBD><EFBFBD>㷽<EFBFBD><E3B7BD>
|
||||
* <20><>1-Ư<><C6AF><EFBFBD>㷨|2-<2D><>ͳ<EFBFBD>㷨<EFBFBD><E3B7A8><EFBFBD><EFBFBD>Ĭ<EFBFBD><C4AC>1
|
||||
*/
|
||||
public short Method = 1;
|
||||
|
||||
/**
|
||||
* <20><>ʼʱ<CABC><CAB1>
|
||||
* <20><><EFBFBD>ܳ<EFBFBD><DCB3><EFBFBD><EFBFBD><EFBFBD>ǰϵͳʱ<CDB3><CAB1>
|
||||
*/
|
||||
public long StartTime = 0;
|
||||
## options Ĭ<><C4AC>ֵ<EFBFBD><D6B5>˵<EFBFBD><CBB5>
|
||||
|
||||
/**
|
||||
* <20><><EFBFBD><EFBFBD><EFBFBD>룬<EFBFBD><EBA3AC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ⲿϵͳ<CFB5><CDB3><EFBFBD><EFBFBD>
|
||||
* <20><> WorkerIdBitLength <20>й<EFBFBD>ϵ
|
||||
*/
|
||||
public short WorkerId = 0;
|
||||
<EFBFBD>ο<EFBFBD>Դ<EFBFBD>룺/contract/IdGeneratorOptions.java
|
||||
|
||||
/**
|
||||
* <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>λ<EFBFBD><CEBB>
|
||||
* <20><>Χ<EFBFBD><CEA7>2-21<32><31>Ҫ<EFBFBD><D2AA><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>λ<EFBFBD><CEBB>+<2B><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>λ<EFBFBD><CEBB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>22<32><32><EFBFBD><EFBFBD>
|
||||
* <20><><EFBFBD>鷶Χ<E9B7B6><CEA7>6-12<31><32>
|
||||
*/
|
||||
public byte WorkerIdBitLength = 6;
|
||||
|
||||
/**
|
||||
* <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>λ<EFBFBD><CEBB>
|
||||
* <20><>Χ<EFBFBD><CEA7>2-21<32><31>Ҫ<EFBFBD><D2AA><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>λ<EFBFBD><CEBB>+<2B><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>λ<EFBFBD><CEBB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>22<32><32><EFBFBD><EFBFBD>
|
||||
* <20><><EFBFBD>鷶Χ<E9B7B6><CEA7>6-14<31><34>
|
||||
*/
|
||||
public byte SeqBitLength = 6;
|
||||
|
||||
/**
|
||||
* <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
* <20><><EFBFBD><EFBFBD>SeqBitLength<74><68><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֵ<EFBFBD><D6B5>
|
||||
*/
|
||||
public short MaxSeqNumber = 0;
|
||||
|
||||
/**
|
||||
* <20><>С<EFBFBD><D0A1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
* Ĭ<><C4AC>11<31><31><EFBFBD><EFBFBD>С<EFBFBD><D0A1>5<EFBFBD><35><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>MaxSeqNumber-2
|
||||
*/
|
||||
public short MinSeqNumber = 11;
|
||||
|
||||
/**
|
||||
* <20><><EFBFBD><EFBFBD>Ư<EFBFBD>ƴ<EFBFBD><C6B4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
* Ĭ<><C4AC>2000<30><30><EFBFBD>Ƽ<EFBFBD><C6BC><EFBFBD>Χ500-10000<30><30><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>йأ<D0B9>
|
||||
*/
|
||||
public short TopOverCostCount = 2000;
|
||||
|
||||
```
|
||||
@@ -20,7 +20,7 @@ public class IdGeneratorOptions {
|
||||
* 开始时间
|
||||
* 不能超过当前系统时间
|
||||
*/
|
||||
public long StartTime = 0;
|
||||
public long BaseTime = 1582136402000L;
|
||||
|
||||
/**
|
||||
* 机器码,必须由外部系统设置
|
||||
|
||||
@@ -14,7 +14,7 @@ public class SnowWorkerM1 implements ISnowWorker {
|
||||
/**
|
||||
* 基础时间
|
||||
*/
|
||||
protected final long StartTimeUtc;
|
||||
protected final long BaseTime;
|
||||
|
||||
/**
|
||||
* 机器码
|
||||
@@ -67,7 +67,7 @@ public class SnowWorkerM1 implements ISnowWorker {
|
||||
MaxSeqNumber = options.MaxSeqNumber > 0 ? options.MaxSeqNumber : (int) Math.pow(2, SeqBitLength);
|
||||
MinSeqNumber = options.MinSeqNumber;
|
||||
TopOverCostCount = options.TopOverCostCount;
|
||||
StartTimeUtc = options.StartTime != 0 ? options.StartTime : 1582136402000L;
|
||||
BaseTime = options.BaseTime != 0 ? options.BaseTime : 1582136402000L;
|
||||
_TimestampShift = (byte) (WorkerIdBitLength + SeqBitLength);
|
||||
_CurrentSeqNumber = options.MinSeqNumber;
|
||||
}
|
||||
@@ -224,7 +224,7 @@ public class SnowWorkerM1 implements ISnowWorker {
|
||||
|
||||
protected long GetCurrentTimeTick() {
|
||||
long millis = System.currentTimeMillis();
|
||||
return millis - StartTimeUtc;
|
||||
return millis - BaseTime;
|
||||
}
|
||||
|
||||
protected long GetNextTimeTick() {
|
||||
|
||||
@@ -17,46 +17,38 @@ public class DefaultIdGenerator implements IIdGenerator {
|
||||
private final ISnowWorker _SnowWorker;
|
||||
|
||||
public DefaultIdGenerator(IdGeneratorOptions options) throws IdGeneratorException {
|
||||
if (options == null)
|
||||
{
|
||||
if (options == null) {
|
||||
throw new IdGeneratorException("options error.");
|
||||
}
|
||||
|
||||
if (options.StartTime > System.currentTimeMillis())
|
||||
{
|
||||
throw new IdGeneratorException("StartTime error.");
|
||||
if (options.BaseTime < 315504000000L || options.BaseTime > System.currentTimeMillis()) {
|
||||
throw new IdGeneratorException("BaseTime error.");
|
||||
}
|
||||
|
||||
if (options.SeqBitLength + options.WorkerIdBitLength > 22)
|
||||
{
|
||||
if (options.SeqBitLength + options.WorkerIdBitLength > 22) {
|
||||
throw new IdGeneratorException("error:WorkerIdBitLength + SeqBitLength <= 22");
|
||||
}
|
||||
|
||||
double maxWorkerIdNumber = Math.pow(2, options.WorkerIdBitLength) - 1;
|
||||
if (options.WorkerId < 1 || options.WorkerId > maxWorkerIdNumber)
|
||||
{
|
||||
if (options.WorkerId < 1 || options.WorkerId > maxWorkerIdNumber) {
|
||||
throw new IdGeneratorException("WorkerId error. (range:[1, " + maxWorkerIdNumber + "]");
|
||||
}
|
||||
|
||||
if (options.SeqBitLength < 2 || options.SeqBitLength > 21)
|
||||
{
|
||||
if (options.SeqBitLength < 2 || options.SeqBitLength > 21) {
|
||||
throw new IdGeneratorException("SeqBitLength error. (range:[2, 21])");
|
||||
}
|
||||
|
||||
double maxSeqNumber = Math.pow(2, options.SeqBitLength) - 1;
|
||||
if (options.MaxSeqNumber < 0 || options.MaxSeqNumber > maxSeqNumber)
|
||||
{
|
||||
if (options.MaxSeqNumber < 0 || options.MaxSeqNumber > maxSeqNumber) {
|
||||
throw new IdGeneratorException("MaxSeqNumber error. (range:[1, " + maxSeqNumber + "]");
|
||||
}
|
||||
|
||||
double maxValue = maxSeqNumber - 2;
|
||||
if (options.MinSeqNumber < 5 || options.MinSeqNumber > maxValue)
|
||||
{
|
||||
if (options.MinSeqNumber < 5 || options.MinSeqNumber > maxValue) {
|
||||
throw new IdGeneratorException("MinSeqNumber error. (range:[5, " + maxValue + "]");
|
||||
}
|
||||
|
||||
switch (options.Method)
|
||||
{
|
||||
switch (options.Method) {
|
||||
case 1:
|
||||
_SnowWorker = new SnowWorkerM1(options);
|
||||
break;
|
||||
@@ -68,8 +60,7 @@ public class DefaultIdGenerator implements IIdGenerator {
|
||||
break;
|
||||
}
|
||||
|
||||
if (options.Method == 1)
|
||||
{
|
||||
if (options.Method == 1) {
|
||||
try {
|
||||
Thread.sleep(500);
|
||||
} catch (InterruptedException e) {
|
||||
@@ -79,7 +70,7 @@ public class DefaultIdGenerator implements IIdGenerator {
|
||||
}
|
||||
|
||||
@Override
|
||||
public long newLong() {
|
||||
public long newLong() {
|
||||
return _SnowWorker.nextId();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,12 +1,15 @@
|
||||
package com.yitter.test;
|
||||
|
||||
import com.yitter.contract.IIdGenerator;
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
|
||||
public class GenTest {
|
||||
|
||||
private IIdGenerator IdGen;
|
||||
private int GenIdCount;
|
||||
private int WorkerId;
|
||||
private Set IdSet = new HashSet();
|
||||
|
||||
public GenTest(IIdGenerator idGen, int genIdCount, int workerId) {
|
||||
GenIdCount = genIdCount;
|
||||
@@ -19,6 +22,7 @@ public class GenTest {
|
||||
|
||||
for (int i = 0; i < GenIdCount; i++) {
|
||||
long id = IdGen.newLong();
|
||||
// IdSet.add(id);
|
||||
}
|
||||
|
||||
long end = System.currentTimeMillis();
|
||||
|
||||
@@ -31,7 +31,7 @@ public class StartUp {
|
||||
// options.MaxSeqNumber = 200;
|
||||
|
||||
options.Method = method;
|
||||
options.StartTime = 1582206693000L; // (2020-2-20)
|
||||
options.BaseTime = 1582206693000L;
|
||||
options.WorkerId = 1;
|
||||
|
||||
IIdGenerator IdGen = new DefaultIdGenerator(options);
|
||||
|
||||
Reference in New Issue
Block a user