优化参数判断逻辑
This commit is contained in:
@@ -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)
|
||||
{
|
||||
SeqBitLength = 6;
|
||||
}
|
||||
|
||||
if (WorkerIdBitLength == 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;
|
||||
}
|
||||
else
|
||||
{
|
||||
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>
|
||||
|
||||
|
||||
Reference in New Issue
Block a user