@@ -56,7 +56,7 @@ namespace Yitter.IdGenerator | |||
/// <summary> | |||
/// 最小序列数(含) | |||
/// 默认5,不小于1,不大于MaxSeqNumber | |||
/// 默认5,不小于5,不大于MaxSeqNumber | |||
/// </summary> | |||
public virtual ushort MinSeqNumber { get; set; } = 5; | |||
@@ -57,8 +57,8 @@ namespace Yitter.IdGenerator | |||
protected static object _SyncLock = new object(); | |||
protected ushort _CurrentSeqNumber; | |||
protected long _LastTimeTick = -1L; | |||
protected long _TurnBackTimeTick = -1L; | |||
protected long _LastTimeTick = 0; // -1L | |||
protected long _TurnBackTimeTick = 0; // -1L; | |||
protected byte _TurnBackIndex = 0; | |||
protected bool _IsOverCost = false; | |||
@@ -73,34 +73,56 @@ namespace Yitter.IdGenerator | |||
public SnowWorkerM1(IdGeneratorOptions options) | |||
{ | |||
WorkerId = options.WorkerId; | |||
WorkerIdBitLength = options.WorkerIdBitLength; | |||
SeqBitLength = options.SeqBitLength; | |||
MaxSeqNumber = options.MaxSeqNumber; | |||
MinSeqNumber = options.MinSeqNumber; | |||
TopOverCostCount = options.TopOverCostCount; | |||
{ | |||
// 1.BaseTime | |||
if (options.BaseTime != DateTime.MinValue) | |||
{ | |||
BaseTime = options.BaseTime; | |||
} | |||
if (SeqBitLength == 0) | |||
} | |||
// 2.WorkerIdBitLength | |||
if (options.WorkerIdBitLength == 0) | |||
{ | |||
WorkerIdBitLength = 6; | |||
} | |||
else | |||
{ | |||
WorkerIdBitLength = options.WorkerIdBitLength; | |||
} | |||
// 3.WorkerId | |||
WorkerId = options.WorkerId; | |||
// 4.SeqBitLength | |||
if (options.SeqBitLength == 0) | |||
{ | |||
SeqBitLength = 6; | |||
} | |||
if (WorkerIdBitLength == 0) | |||
} | |||
else | |||
{ | |||
WorkerIdBitLength = 6; | |||
} | |||
SeqBitLength = options.SeqBitLength; | |||
} | |||
// 5.MaxSeqNumber | |||
if (MaxSeqNumber == 0) | |||
{ | |||
MaxSeqNumber = (1 << SeqBitLength) - 1; | |||
} | |||
} | |||
else | |||
{ | |||
MaxSeqNumber = options.MaxSeqNumber; | |||
} | |||
// 6.MinSeqNumber | |||
MinSeqNumber = options.MinSeqNumber; | |||
// 7.Others | |||
TopOverCostCount = options.TopOverCostCount; | |||
if (TopOverCostCount == 0) | |||
{ | |||
TopOverCostCount = 2000; | |||
} | |||
_TimestampShift = (byte)(WorkerIdBitLength + SeqBitLength); | |||
_CurrentSeqNumber = options.MinSeqNumber; | |||
@@ -35,11 +35,13 @@ namespace Yitter.IdGenerator | |||
throw new ApplicationException("options error."); | |||
} | |||
// 1.BaseTime | |||
if (options.BaseTime < DateTime.Now.AddYears(-50) || options.BaseTime > DateTime.Now) | |||
{ | |||
throw new ApplicationException("BaseTime error."); | |||
} | |||
// 2.WorkerIdBitLength | |||
if (options.WorkerIdBitLength <= 0) | |||
{ | |||
throw new ApplicationException("WorkerIdBitLength error.(range:[1, 21])"); | |||
@@ -49,27 +51,38 @@ namespace Yitter.IdGenerator | |||
throw new ApplicationException("error:WorkerIdBitLength + SeqBitLength <= 22"); | |||
} | |||
// 3.WorkerId | |||
var maxWorkerIdNumber = (1 << options.WorkerIdBitLength) - 1; | |||
if (maxWorkerIdNumber == 0) | |||
{ | |||
maxWorkerIdNumber = 63; | |||
} | |||
if (options.WorkerId < 0 || options.WorkerId > maxWorkerIdNumber) | |||
{ | |||
throw new ApplicationException("WorkerId error. (range:[0, " + (maxWorkerIdNumber > 0 ? maxWorkerIdNumber : 63) + "]"); | |||
throw new ApplicationException("WorkerId error. (range:[0, " + maxWorkerIdNumber + "]"); | |||
} | |||
// 4.SeqBitLength | |||
if (options.SeqBitLength < 2 || options.SeqBitLength > 21) | |||
{ | |||
throw new ApplicationException("SeqBitLength error. (range:[2, 21])"); | |||
} | |||
// 5.MaxSeqNumber | |||
var maxSeqNumber = (1 << options.SeqBitLength) - 1; | |||
if (maxSeqNumber == 0) | |||
{ | |||
maxSeqNumber = 63; | |||
} | |||
if (options.MaxSeqNumber < 0 || options.MaxSeqNumber > maxSeqNumber) | |||
{ | |||
throw new ApplicationException("MaxSeqNumber error. (range:[1, " + maxSeqNumber + "]"); | |||
} | |||
var maxValue = maxSeqNumber; | |||
if (options.MinSeqNumber < 1 || options.MinSeqNumber > maxValue) | |||
// 6.MinSeqNumber | |||
if (options.MinSeqNumber < 5 || options.MinSeqNumber > maxSeqNumber) | |||
{ | |||
throw new ApplicationException("MinSeqNumber error. (range:[1, " + maxValue + "]"); | |||
throw new ApplicationException("MinSeqNumber error. (range:[5, " + maxSeqNumber + "]"); | |||
} | |||
switch (options.Method) | |||
@@ -18,7 +18,7 @@ | |||
<Copyright>Yitter</Copyright> | |||
<PackageProjectUrl>https://github.com/yitter/idgenerator</PackageProjectUrl> | |||
<PackageLicenseExpression>MIT</PackageLicenseExpression> | |||
<Version>1.0.10</Version> | |||
<Version>1.0.11</Version> | |||
<PackageReleaseNotes></PackageReleaseNotes> | |||
</PropertyGroup> | |||
@@ -34,7 +34,7 @@ extern void SetOptions(IdGeneratorOptions options) { | |||
exit(1); | |||
} | |||
// BaseTime | |||
// 1.BaseTime | |||
if (options.BaseTime == 0) { | |||
_idGenerator->Worker->BaseTime = 1582136402000; | |||
} else if (options.BaseTime < 631123200000 || options.BaseTime > GetCurrentTime()) { | |||
@@ -44,7 +44,7 @@ extern void SetOptions(IdGeneratorOptions options) { | |||
_idGenerator->Worker->BaseTime = options.BaseTime; | |||
} | |||
// WorkerIdBitLength | |||
// 2.WorkerIdBitLength | |||
if (options.WorkerIdBitLength <= 0) { | |||
perror("WorkerIdBitLength error.(range:[1, 21])"); | |||
exit(1); | |||
@@ -53,12 +53,15 @@ extern void SetOptions(IdGeneratorOptions options) { | |||
perror("error:WorkerIdBitLength + SeqBitLength <= 22"); | |||
exit(1); | |||
} else { | |||
// _idGenerator->Worker->WorkerIdBitLength = options.WorkerIdBitLength; | |||
// _idGenerator->Worker->WorkerIdBitLength = options.WorkerIdBitLength; | |||
_idGenerator->Worker->WorkerIdBitLength = options.WorkerIdBitLength <= 0 ? 6 : options.WorkerIdBitLength; | |||
} | |||
// WorkerId | |||
// 3.WorkerId | |||
uint32_t maxWorkerIdNumber = (1 << options.WorkerIdBitLength) - 1; | |||
if (maxWorkerIdNumber == 0) { | |||
maxWorkerIdNumber = 63; | |||
} | |||
if (options.WorkerId < 0 || options.WorkerId > maxWorkerIdNumber) { | |||
perror("WorkerId error. (range:[0, {2^options.WorkerIdBitLength-1]}"); | |||
exit(1); | |||
@@ -66,17 +69,20 @@ extern void SetOptions(IdGeneratorOptions options) { | |||
_idGenerator->Worker->WorkerId = options.WorkerId; | |||
} | |||
// SeqBitLength | |||
// 4.SeqBitLength | |||
if (options.SeqBitLength < 2 || options.SeqBitLength > 21) { | |||
perror("SeqBitLength error. (range:[2, 21])"); | |||
exit(1); | |||
} else { | |||
// _idGenerator->Worker->SeqBitLength = options.SeqBitLength; | |||
// _idGenerator->Worker->SeqBitLength = options.SeqBitLength; | |||
_idGenerator->Worker->SeqBitLength = options.SeqBitLength <= 0 ? 6 : options.SeqBitLength; | |||
} | |||
// MaxSeqNumber | |||
// 5.MaxSeqNumber | |||
uint32_t maxSeqNumber = (1 << options.SeqBitLength) - 1; | |||
if (maxSeqNumber == 0) { | |||
maxSeqNumber = 63; | |||
} | |||
if (options.MaxSeqNumber > maxSeqNumber) { | |||
perror("MaxSeqNumber error. (range:[1, {2^options.SeqBitLength-1}]"); | |||
exit(1); | |||
@@ -84,17 +90,19 @@ extern void SetOptions(IdGeneratorOptions options) { | |||
_idGenerator->Worker->MaxSeqNumber = options.MaxSeqNumber <= 0 ? maxSeqNumber : options.MaxSeqNumber; | |||
} | |||
// MinSeqNumber | |||
if (options.MinSeqNumber > maxSeqNumber || options.MinSeqNumber < 5) { | |||
// 6.MinSeqNumber | |||
if (options.MinSeqNumber < 5 || options.MinSeqNumber > maxSeqNumber) { | |||
perror("MinSeqNumber error. (range:[5, {options.MinSeqNumber}]"); | |||
exit(1); | |||
} else { | |||
_idGenerator->Worker->MinSeqNumber = options.MinSeqNumber <= 0 ? 5 : options.MinSeqNumber; | |||
} | |||
// 7.Others | |||
_idGenerator->Worker->TopOverCostCount = options.TopOverCostCount <= 0 ? 2000 : options.TopOverCostCount; | |||
_idGenerator->Worker->_TimestampShift = options.WorkerIdBitLength + options.SeqBitLength; | |||
_idGenerator->Worker->_CurrentSeqNumber = options.MinSeqNumber; | |||
_idGenerator->Worker->_TimestampShift = | |||
_idGenerator->Worker->WorkerIdBitLength + _idGenerator->Worker->SeqBitLength; | |||
_idGenerator->Worker->_CurrentSeqNumber = _idGenerator->Worker->MinSeqNumber; | |||
_idGenerator->Worker->Method = options.Method; | |||
if (options.Method == 2) { | |||
@@ -22,36 +22,49 @@ func NewDefaultIdGenerator(options *IdGeneratorOptions) *DefaultIdGenerator { | |||
panic("dig.Options error.") | |||
} | |||
// 1.BaseTime | |||
minTime := int64(631123200000) // time.Now().AddDate(-30, 0, 0).UnixNano() / 1e6 | |||
if options.BaseTime < minTime || options.BaseTime > time.Now().UnixNano()/1e6 { | |||
panic("BaseTime error.") | |||
} | |||
if options.SeqBitLength+options.WorkerIdBitLength > 22 { | |||
// 2.WorkerIdBitLength | |||
if options.WorkerIdBitLength <= 0 { | |||
panic("WorkerIdBitLength error.(range:[1, 21])") | |||
} | |||
if options.WorkerIdBitLength+options.SeqBitLength > 22 { | |||
panic("error:WorkerIdBitLength + SeqBitLength <= 22") | |||
} | |||
maxWorkerIDNumber := uint16(1<<options.WorkerIdBitLength) - 1 | |||
if options.WorkerId > maxWorkerIDNumber { | |||
panic("WorkerId error. (range:[1, " + strconv.FormatUint(uint64(maxWorkerIDNumber), 10) + "]") | |||
// 3.WorkerId | |||
maxWorkerIdNumber := uint16(1<<options.WorkerIdBitLength) - 1 | |||
if maxWorkerIdNumber == 0 { | |||
maxWorkerIdNumber = 63 | |||
} | |||
if options.WorkerId < 0 || options.WorkerId > maxWorkerIdNumber { | |||
panic("WorkerId error. (range:[0, " + strconv.FormatUint(uint64(maxWorkerIdNumber), 10) + "]") | |||
} | |||
// 4.SeqBitLength | |||
if options.SeqBitLength < 2 || options.SeqBitLength > 21 { | |||
panic("SeqBitLength error. (range:[2, 21])") | |||
} | |||
// 5.MaxSeqNumber | |||
maxSeqNumber := uint32(1<<options.SeqBitLength) - 1 | |||
if options.MaxSeqNumber > maxSeqNumber { | |||
if maxSeqNumber == 0 { | |||
maxSeqNumber = 63 | |||
} | |||
if options.MaxSeqNumber < 0 || options.MaxSeqNumber > maxSeqNumber { | |||
panic("MaxSeqNumber error. (range:[1, " + strconv.FormatUint(uint64(maxSeqNumber), 10) + "]") | |||
} | |||
if options.MinSeqNumber > maxSeqNumber { | |||
panic("MinSeqNumber error. (range:[1, " + strconv.FormatUint(uint64(maxSeqNumber), 10) + "]") | |||
// 6.MinSeqNumber | |||
if options.MinSeqNumber < 5 || options.MinSeqNumber > maxSeqNumber { | |||
panic("MinSeqNumber error. (range:[5, " + strconv.FormatUint(uint64(maxSeqNumber), 10) + "]") | |||
} | |||
var snowWorker ISnowWorker | |||
switch options.Method { | |||
case 1: | |||
snowWorker = NewSnowWorkerM1(options) | |||
@@ -64,6 +77,7 @@ func NewDefaultIdGenerator(options *IdGeneratorOptions) *DefaultIdGenerator { | |||
if options.Method == 1 { | |||
time.Sleep(time.Duration(500) * time.Microsecond) | |||
} | |||
return &DefaultIdGenerator{ | |||
Options: options, | |||
SnowWorker: snowWorker, | |||
@@ -13,15 +13,16 @@ import ( | |||
// SnowWorkerM1 . | |||
type SnowWorkerM1 struct { | |||
BaseTime int64 //基础时间 | |||
WorkerId uint16 //机器码 | |||
WorkerIdBitLength byte //机器码位长 | |||
SeqBitLength byte //自增序列数位长 | |||
MaxSeqNumber uint32 //最大序列数(含) | |||
MinSeqNumber uint32 //最小序列数(含) | |||
TopOverCostCount uint32 //最大漂移次数 | |||
_TimestampShift byte | |||
_CurrentSeqNumber uint32 | |||
BaseTime int64 //基础时间 | |||
WorkerId uint16 //机器码 | |||
WorkerIdBitLength byte //机器码位长 | |||
SeqBitLength byte //自增序列数位长 | |||
MaxSeqNumber uint32 //最大序列数(含) | |||
MinSeqNumber uint32 //最小序列数(含) | |||
TopOverCostCount uint32 //最大漂移次数 | |||
_TimestampShift byte | |||
_CurrentSeqNumber uint32 | |||
_LastTimeTick int64 | |||
_TurnBackTimeTick int64 | |||
_TurnBackIndex byte | |||
@@ -29,6 +30,7 @@ type SnowWorkerM1 struct { | |||
_OverCostCountInOneTerm uint32 | |||
_GenCountInOneTerm uint32 | |||
_TermIndex uint32 | |||
sync.Mutex | |||
} | |||
@@ -38,46 +40,69 @@ func NewSnowWorkerM1(options *IdGeneratorOptions) ISnowWorker { | |||
var seqBitLength byte | |||
var maxSeqNumber uint32 | |||
var workerId = options.WorkerId | |||
// 1.BaseTime | |||
var baseTime int64 | |||
if options.BaseTime != 0 { | |||
baseTime = options.BaseTime | |||
} else { | |||
baseTime = 1582136402000 | |||
} | |||
// 2.WorkerIdBitLength | |||
if options.WorkerIdBitLength == 0 { | |||
workerIdBitLength = 6 | |||
} else { | |||
workerIdBitLength = options.WorkerIdBitLength | |||
} | |||
// 3.WorkerId | |||
var workerId = options.WorkerId | |||
// 4.SeqBitLength | |||
if options.SeqBitLength == 0 { | |||
seqBitLength = 6 | |||
} else { | |||
seqBitLength = options.SeqBitLength | |||
} | |||
if options.MaxSeqNumber > 0 { | |||
maxSeqNumber = options.MaxSeqNumber | |||
} else { | |||
// 5.MaxSeqNumber | |||
if options.MaxSeqNumber <= 0 { | |||
maxSeqNumber = (1 << seqBitLength) - 1 | |||
} else { | |||
maxSeqNumber = options.MaxSeqNumber | |||
} | |||
// 6.MinSeqNumber | |||
var minSeqNumber = options.MinSeqNumber | |||
var topOverCostCount = options.TopOverCostCount | |||
var baseTime int64 | |||
if options.BaseTime != 0 { | |||
baseTime = options.BaseTime | |||
} else { | |||
baseTime = 1582136402000 | |||
// 7.Others | |||
var topOverCostCount = options.TopOverCostCount | |||
if topOverCostCount == 0 { | |||
topOverCostCount = 2000 | |||
} | |||
timestampShift := (byte)(options.WorkerIdBitLength + options.SeqBitLength) | |||
currentSeqNumber := options.MinSeqNumber | |||
timestampShift := (byte)(workerIdBitLength + seqBitLength) | |||
currentSeqNumber := minSeqNumber | |||
return &SnowWorkerM1{ | |||
BaseTime: baseTime, | |||
WorkerId: workerId, | |||
WorkerIdBitLength: workerIdBitLength, | |||
WorkerId: workerId, | |||
SeqBitLength: seqBitLength, | |||
MaxSeqNumber: maxSeqNumber, | |||
MinSeqNumber: minSeqNumber, | |||
TopOverCostCount: topOverCostCount, | |||
_TimestampShift: timestampShift, | |||
_CurrentSeqNumber: currentSeqNumber} | |||
_CurrentSeqNumber: currentSeqNumber, | |||
_LastTimeTick: 0, | |||
_TurnBackTimeTick: 0, | |||
_TurnBackIndex: 0, | |||
_IsOverCost: false, | |||
_OverCostCountInOneTerm: 0, | |||
_GenCountInOneTerm: 0, | |||
_TermIndex: 0, | |||
} | |||
} | |||
// DoGenIDAction . | |||
@@ -1,34 +1,34 @@ | |||
## 运行环境 | |||
## 杩愯�鐜�� | |||
JDK 1.8+ | |||
## 引用 maven 包 | |||
## 寮曠敤 maven 鍖� | |||
``` | |||
<dependency> | |||
<groupId>com.github.yitter</groupId> | |||
<artifactId>yitter-idgenerator</artifactId> | |||
<version>1.0.3</version> | |||
<version>1.0.5</version> | |||
</dependency> | |||
``` | |||
## 调用示例 | |||
## 璋冪敤绀轰緥 | |||
``` | |||
// 全局初始化设置WorkerId,默认最大2^16-1,可通过调整 WorkerIdBitLength 增加最大值 | |||
// 鍏ㄥ眬鍒濆�鍖栬�缃甒orkerId锛岄粯璁ゆ渶澶�2^16-1锛屽彲閫氳繃璋冩暣 WorkerIdBitLength 澧炲姞鏈€澶у€� | |||
IdGeneratorOptions options = new IdGeneratorOptions(); | |||
options.WorkerId = 1; | |||
YitIdHelper.setIdGenerator(options); | |||
// 以上初始化过程全局只需一次,且必须最先设置 | |||
// 浠ヤ笂鍒濆�鍖栬繃绋嬪叏灞€鍙�渶涓€娆★紝涓斿繀椤绘渶鍏堣�缃� | |||
// 初始化以后,即可在需要生成ID的地方,调用以下方法。 | |||
// 鍒濆�鍖栦互鍚庯紝鍗冲彲鍦ㄩ渶瑕佺敓鎴怚D鐨勫湴鏂癸紝璋冪敤浠ヤ笅鏂规硶銆� | |||
long newId = YitIdHelper.nextId(); | |||
``` | |||
如果基于DI框架集成,可以参考 YitIdHelper 去管理 IdGenerator 对象,须使用**单例**模式。 | |||
濡傛灉鍩轰簬DI妗嗘灦闆嗘垚锛屽彲浠ュ弬鑰� YitIdHelper 鍘荤�鐞� IdGenerator 瀵硅薄锛岄』浣跨敤**鍗曚緥**妯″紡銆� | |||
## options 默认值及说明 | |||
## options 榛樿�鍊煎強璇存槑 | |||
参考源码:/contract/IdGeneratorOptions.java | |||
鍙傝€冩簮鐮侊細/contract/IdGeneratorOptions.java | |||
@@ -6,7 +6,7 @@ | |||
<groupId>com.github.yitter</groupId> | |||
<artifactId>yitter-idgenerator</artifactId> | |||
<version>1.0.4-SNAPSHOT</version> | |||
<version>1.0.5</version> | |||
<packaging>jar</packaging> | |||
<name>yitter-idgenerator</name> | |||
@@ -50,8 +50,8 @@ public class SnowWorkerM1 implements ISnowWorker { | |||
protected final static byte[] _SyncLock = new byte[0]; | |||
protected short _CurrentSeqNumber; | |||
protected long _LastTimeTick = -1L; | |||
protected long _TurnBackTimeTick = -1L; | |||
protected long _LastTimeTick = 0; | |||
protected long _TurnBackTimeTick = 0; | |||
protected byte _TurnBackIndex = 0; | |||
protected boolean _IsOverCost = false; | |||
@@ -60,15 +60,15 @@ public class SnowWorkerM1 implements ISnowWorker { | |||
protected int _TermIndex = 0; | |||
public SnowWorkerM1(IdGeneratorOptions options) { | |||
WorkerId = options.WorkerId; | |||
BaseTime = options.BaseTime != 0 ? options.BaseTime : 1582136402000L; | |||
WorkerIdBitLength = options.WorkerIdBitLength == 0 ? 6 : options.WorkerIdBitLength; | |||
WorkerId = options.WorkerId; | |||
SeqBitLength = options.SeqBitLength == 0 ? 6 : options.SeqBitLength; | |||
MaxSeqNumber = options.MaxSeqNumber > 0 ? options.MaxSeqNumber : (1 << SeqBitLength) - 1; | |||
MaxSeqNumber = options.MaxSeqNumber <= 0 ? (1 << SeqBitLength) - 1 : options.MaxSeqNumber; | |||
MinSeqNumber = options.MinSeqNumber; | |||
TopOverCostCount = options.TopOverCostCount; | |||
BaseTime = options.BaseTime != 0 ? options.BaseTime : 1582136402000L; | |||
TopOverCostCount = options.TopOverCostCount == 0 ? 2000 : options.TopOverCostCount; | |||
_TimestampShift = (byte) (WorkerIdBitLength + SeqBitLength); | |||
_CurrentSeqNumber = options.MinSeqNumber; | |||
_CurrentSeqNumber = MinSeqNumber; | |||
} | |||
private void DoGenIdAction(OverCostActionArg arg) { | |||
@@ -182,11 +182,12 @@ public class SnowWorkerM1 implements ISnowWorker { | |||
BeginTurnBackAction(_TurnBackTimeTick); | |||
} | |||
try { | |||
// Thread.sleep(1); | |||
} catch (InterruptedException e) { | |||
e.printStackTrace(); | |||
} | |||
// try { | |||
// Thread.sleep(1); | |||
// } catch (InterruptedException e) { | |||
// e.printStackTrace(); | |||
// } | |||
return CalcTurnBackId(_TurnBackTimeTick); | |||
} | |||
@@ -21,34 +21,45 @@ public class DefaultIdGenerator implements IIdGenerator { | |||
throw new IdGeneratorException("options error."); | |||
} | |||
// 1.BaseTime | |||
if (options.BaseTime < 315504000000L || options.BaseTime > System.currentTimeMillis()) { | |||
throw new IdGeneratorException("BaseTime error."); | |||
} | |||
// 2.WorkerIdBitLength | |||
if (options.WorkerIdBitLength <= 0) { | |||
throw new IdGeneratorException("WorkerIdBitLength error.(range:[1, 21])"); | |||
} | |||
if (options.SeqBitLength + options.WorkerIdBitLength > 22) { | |||
if (options.WorkerIdBitLength + options.SeqBitLength > 22) { | |||
throw new IdGeneratorException("error:WorkerIdBitLength + SeqBitLength <= 22"); | |||
} | |||
// 3.WorkerId | |||
int maxWorkerIdNumber = (1 << options.WorkerIdBitLength) - 1; | |||
if (maxWorkerIdNumber == 0) { | |||
maxWorkerIdNumber = 63; | |||
} | |||
if (options.WorkerId < 0 || options.WorkerId > maxWorkerIdNumber) { | |||
throw new IdGeneratorException("WorkerId error. (range:[0, " + (maxWorkerIdNumber > 0 ? maxWorkerIdNumber : 63) + "]"); | |||
} | |||
// 4.SeqBitLength | |||
if (options.SeqBitLength < 2 || options.SeqBitLength > 21) { | |||
throw new IdGeneratorException("SeqBitLength error. (range:[2, 21])"); | |||
} | |||
// 5.MaxSeqNumber | |||
int maxSeqNumber = (1 << options.SeqBitLength) - 1; | |||
if (maxSeqNumber == 0) { | |||
maxSeqNumber = 63; | |||
} | |||
if (options.MaxSeqNumber < 0 || options.MaxSeqNumber > maxSeqNumber) { | |||
throw new IdGeneratorException("MaxSeqNumber error. (range:[1, " + maxSeqNumber + "]"); | |||
} | |||
int maxValue = maxSeqNumber; | |||
if (options.MinSeqNumber < 5 || options.MinSeqNumber > maxValue) { | |||
throw new IdGeneratorException("MinSeqNumber error. (range:[5, " + maxValue + "]"); | |||
// 6.MinSeqNumber | |||
if (options.MinSeqNumber < 5 || options.MinSeqNumber > maxSeqNumber) { | |||
throw new IdGeneratorException("MinSeqNumber error. (range:[5, " + maxSeqNumber + "]"); | |||
} | |||
switch (options.Method) { | |||
@@ -53,3 +53,5 @@ public class StartUp { | |||
} | |||
} | |||
} | |||
// mvn clean deploy -P release |
@@ -43,7 +43,7 @@ impl SnowWorkerM1 { | |||
pub fn SetOptions(&mut self, options: IdGeneratorOptions) { | |||
// BaseTime | |||
// 1.BaseTime | |||
if options.BaseTime == 0 { | |||
self.BaseTime = 1582136402000; | |||
} else if options.BaseTime < 631123200000 || options.BaseTime > Utc::now().timestamp_millis() { | |||
@@ -52,7 +52,7 @@ impl SnowWorkerM1 { | |||
self.BaseTime = options.BaseTime; | |||
} | |||
// WorkerIdBitLength | |||
// 2.WorkerIdBitLength | |||
if options.WorkerIdBitLength <= 0 | |||
{ | |||
panic!("WorkerIdBitLength error.(range:[1, 21])"); | |||
@@ -64,15 +64,18 @@ impl SnowWorkerM1 { | |||
self.WorkerIdBitLength = if options.WorkerIdBitLength <= 0 { 6 } else { options.WorkerIdBitLength }; | |||
} | |||
// WorkerId | |||
let maxWorkerIdNumber = (1 << options.WorkerIdBitLength) - 1; | |||
// 3.WorkerId | |||
let mut maxWorkerIdNumber = (1 << options.WorkerIdBitLength) - 1; | |||
if maxWorkerIdNumber == 0 { | |||
maxWorkerIdNumber = 63; | |||
} | |||
if options.WorkerId < 0 || options.WorkerId > maxWorkerIdNumber { | |||
panic!("WorkerId error. (range:[0, {} ]", if maxWorkerIdNumber <= 0 { 63 } else { maxWorkerIdNumber }); | |||
panic!("WorkerId error. (range:[0, {} ]", maxWorkerIdNumber); | |||
} else { | |||
self.WorkerId = options.WorkerId; | |||
} | |||
// SeqBitLength | |||
// 4.SeqBitLength | |||
if options.SeqBitLength < 2 || options.SeqBitLength > 21 { | |||
panic!("SeqBitLength error. (range:[2, 21])"); | |||
} else { | |||
@@ -80,24 +83,29 @@ impl SnowWorkerM1 { | |||
self.SeqBitLength = if options.SeqBitLength <= 0 { 6 } else { options.SeqBitLength }; | |||
} | |||
// MaxSeqNumber | |||
let maxSeqNumber = (1 << options.SeqBitLength) - 1; | |||
if options.MaxSeqNumber > maxSeqNumber { | |||
// 5.MaxSeqNumber | |||
let mut maxSeqNumber = (1 << options.SeqBitLength) - 1; | |||
if maxSeqNumber == 0 { | |||
maxSeqNumber = 63; | |||
} | |||
if options.MaxSeqNumber < 0 || options.MaxSeqNumber > maxSeqNumber { | |||
panic!("MaxSeqNumber error. (range:[1, {}]", maxSeqNumber); | |||
} else { | |||
self.MaxSeqNumber = if options.MaxSeqNumber <= 0 { maxSeqNumber } else { options.MaxSeqNumber }; | |||
self.MaxSeqNumber = if options.MaxSeqNumber == 0 { maxSeqNumber } else { options.MaxSeqNumber }; | |||
} | |||
// MinSeqNumber | |||
if options.MinSeqNumber > maxSeqNumber || options.MinSeqNumber < 5 { | |||
// 6.MinSeqNumber | |||
if options.MinSeqNumber < 5 || options.MinSeqNumber > maxSeqNumber { | |||
panic!("MinSeqNumber error. (range:[5, {}]", maxSeqNumber); | |||
} else { | |||
self.MinSeqNumber = if options.MinSeqNumber <= 0 { 5 } else { options.MinSeqNumber }; | |||
self.MinSeqNumber = options.MinSeqNumber; | |||
// self.MinSeqNumber = if options.MinSeqNumber <= 0 { 5 } else { options.MinSeqNumber }; | |||
} | |||
// 7.Others | |||
self.TopOverCostCount = if options.TopOverCostCount == 0 { 2000 } else { options.TopOverCostCount }; | |||
self._TimestampShift = options.WorkerIdBitLength + options.SeqBitLength; | |||
self._CurrentSeqNumber = options.MinSeqNumber; | |||
self._TimestampShift = self.WorkerIdBitLength + self.SeqBitLength; | |||
self._CurrentSeqNumber = self.MinSeqNumber; | |||
if options.Method == 1 { | |||
sleep(std::time::Duration::from_millis(500)); | |||
@@ -107,14 +115,15 @@ impl SnowWorkerM1 { | |||
pub fn New(options: IdGeneratorOptions) -> SnowWorkerM1 { | |||
let mut worker = SnowWorkerM1 { | |||
BaseTime: 1582136402000, | |||
WorkerId: 0, | |||
WorkerIdBitLength: 0, | |||
WorkerId: 0, | |||
SeqBitLength: 0, | |||
MaxSeqNumber: 0, | |||
MinSeqNumber: 0, | |||
TopOverCostCount: 0, | |||
_TimestampShift: 0, | |||
_CurrentSeqNumber: 0, | |||
_LastTimeTick: 0, | |||
_TurnBackTimeTick: 0, | |||
_TurnBackIndex: 0, | |||