修改约束条件
This commit is contained in:
@@ -10,7 +10,7 @@ namespace Yitter.OrgSystem.TestA
|
|||||||
{
|
{
|
||||||
|
|
||||||
// 测试参数(默认配置下,最佳性能是10W/s)
|
// 测试参数(默认配置下,最佳性能是10W/s)
|
||||||
static int genIdCount = 5000; // 计算ID数量(如果要验证50W效率,请将TopOverCostCount设置为20000或适当增加SeqBitLength)
|
static int genIdCount = 50000; // 计算ID数量(如果要验证50W效率,请将TopOverCostCount设置为20000或适当增加SeqBitLength)
|
||||||
static short method = 1; // 1-漂移算法,2-传统算法
|
static short method = 1; // 1-漂移算法,2-传统算法
|
||||||
|
|
||||||
|
|
||||||
@@ -30,11 +30,11 @@ namespace Yitter.OrgSystem.TestA
|
|||||||
Method = method,
|
Method = method,
|
||||||
WorkerId = 1,
|
WorkerId = 1,
|
||||||
|
|
||||||
TopOverCostCount = 2000,
|
//TopOverCostCount = 2000,
|
||||||
WorkerIdBitLength = 1,
|
//WorkerIdBitLength = 1,
|
||||||
SeqBitLength = 2,
|
//SeqBitLength = 2,
|
||||||
|
|
||||||
MinSeqNumber = 1,
|
//MinSeqNumber = 1,
|
||||||
|
|
||||||
// MaxSeqNumber = 200,
|
// MaxSeqNumber = 200,
|
||||||
|
|
||||||
|
|||||||
@@ -30,12 +30,13 @@ namespace Yitter.IdGenerator
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// 机器码
|
/// 机器码
|
||||||
/// 与 WorkerIdBitLength 有关系
|
/// 与 WorkerIdBitLength 有关系
|
||||||
|
/// (ushort类型,最大值65535,如果有更高要求,请修改数据类型,或联系作者)
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public virtual ushort WorkerId { get; set; } = 0;
|
public virtual ushort WorkerId { get; set; } = 0;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 机器码位长
|
/// 机器码位长
|
||||||
/// 范围:2-21(要求:序列数位长+机器码位长不超过22)。
|
/// 范围:1-21(要求:序列数位长+机器码位长不超过22)。
|
||||||
/// 建议范围:6-12。
|
/// 建议范围:6-12。
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public virtual byte WorkerIdBitLength { get; set; } = 6;//10;
|
public virtual byte WorkerIdBitLength { get; set; } = 6;//10;
|
||||||
@@ -55,7 +56,7 @@ namespace Yitter.IdGenerator
|
|||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 最小序列数(含)
|
/// 最小序列数(含)
|
||||||
/// 默认5,不小于1,不大于MaxSeqNumber-2
|
/// 默认5,不小于1,不大于MaxSeqNumber
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public virtual ushort MinSeqNumber { get; set; } = 5;
|
public virtual ushort MinSeqNumber { get; set; } = 5;
|
||||||
|
|
||||||
|
|||||||
@@ -30,24 +30,21 @@ namespace Yitter.IdGenerator
|
|||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 机器码位长
|
/// 机器码位长
|
||||||
/// (机器码+序列数<=22位)
|
|
||||||
/// </summary>
|
/// </summary>
|
||||||
protected readonly byte WorkerIdBitLength = 0;
|
protected readonly byte WorkerIdBitLength = 0;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 自增序列数位长
|
/// 自增序列数位长
|
||||||
/// (机器码+序列数<=22位)
|
|
||||||
/// </summary>
|
/// </summary>
|
||||||
protected readonly byte SeqBitLength = 0;
|
protected readonly byte SeqBitLength = 0;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 最大序列数(含此值)
|
/// 最大序列数(含)
|
||||||
/// 超过最大值,就会从MinSeqNumber开始
|
|
||||||
/// </summary>
|
/// </summary>
|
||||||
protected readonly int MaxSeqNumber = 0;
|
protected readonly int MaxSeqNumber = 0;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 最小序列数(含此值)
|
/// 最小序列数(含)
|
||||||
/// </summary>
|
/// </summary>
|
||||||
protected readonly ushort MinSeqNumber = 0;
|
protected readonly ushort MinSeqNumber = 0;
|
||||||
|
|
||||||
|
|||||||
@@ -57,19 +57,15 @@ namespace Yitter.IdGenerator
|
|||||||
}
|
}
|
||||||
|
|
||||||
var maxSeqNumber = Math.Pow(2, options.SeqBitLength) - 1;
|
var maxSeqNumber = Math.Pow(2, options.SeqBitLength) - 1;
|
||||||
//if (options.MaxSeqNumber > maxSeqNumber)
|
|
||||||
//{
|
|
||||||
// options.MaxSeqNumber = (int)maxSeqNumber;
|
|
||||||
//}
|
|
||||||
if (options.MaxSeqNumber < 0 || options.MaxSeqNumber > maxSeqNumber)
|
if (options.MaxSeqNumber < 0 || options.MaxSeqNumber > maxSeqNumber)
|
||||||
{
|
{
|
||||||
throw new ApplicationException("MaxSeqNumber error. (range:[1, " + maxSeqNumber + "]");
|
throw new ApplicationException("MaxSeqNumber error. (range:[1, " + maxSeqNumber + "]");
|
||||||
}
|
}
|
||||||
|
|
||||||
var maxValue = maxSeqNumber;// maxSeqNumber - 2;
|
var maxValue = maxSeqNumber; // maxSeqNumber - 1;
|
||||||
if (options.MinSeqNumber < 1 || options.MinSeqNumber > maxValue)
|
if (options.MinSeqNumber < 1 || options.MinSeqNumber > maxValue)
|
||||||
{
|
{
|
||||||
throw new ApplicationException("MinSeqNumber error. (range:[5, " + maxValue + "]");
|
throw new ApplicationException("MinSeqNumber error. (range:[1, " + maxValue + "]");
|
||||||
}
|
}
|
||||||
|
|
||||||
switch (options.Method)
|
switch (options.Method)
|
||||||
|
|||||||
@@ -18,7 +18,7 @@
|
|||||||
<Copyright>Yitter</Copyright>
|
<Copyright>Yitter</Copyright>
|
||||||
<PackageProjectUrl>https://gitee.com/yitter/idgenerator</PackageProjectUrl>
|
<PackageProjectUrl>https://gitee.com/yitter/idgenerator</PackageProjectUrl>
|
||||||
<PackageLicenseExpression>MIT</PackageLicenseExpression>
|
<PackageLicenseExpression>MIT</PackageLicenseExpression>
|
||||||
<Version>1.0.6</Version>
|
<Version>1.0.7</Version>
|
||||||
<PackageReleaseNotes></PackageReleaseNotes>
|
<PackageReleaseNotes></PackageReleaseNotes>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
|
|||||||
@@ -25,12 +25,13 @@ public class IdGeneratorOptions {
|
|||||||
/**
|
/**
|
||||||
* 机器码,必须由外部系统设置
|
* 机器码,必须由外部系统设置
|
||||||
* 与 WorkerIdBitLength 有关系
|
* 与 WorkerIdBitLength 有关系
|
||||||
|
* (short类型,最大值32766,如果有更高要求,请修改数据类型,或联系作者)
|
||||||
*/
|
*/
|
||||||
public short WorkerId = 0;
|
public short WorkerId = 0;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 机器码位长
|
* 机器码位长
|
||||||
* 范围:2-21(要求:序列数位长+机器码位长不超过22)。
|
* 范围:1-21(要求:序列数位长+机器码位长不超过22)。
|
||||||
* 建议范围:6-12。
|
* 建议范围:6-12。
|
||||||
*/
|
*/
|
||||||
public byte WorkerIdBitLength = 6;
|
public byte WorkerIdBitLength = 6;
|
||||||
@@ -50,7 +51,7 @@ public class IdGeneratorOptions {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* 最小序列数(含)
|
* 最小序列数(含)
|
||||||
* 默认5,不小于1,不大于MaxSeqNumber-1
|
* 默认5,不小于1,不大于MaxSeqNumber
|
||||||
*/
|
*/
|
||||||
public short MinSeqNumber = 5;
|
public short MinSeqNumber = 5;
|
||||||
|
|
||||||
|
|||||||
@@ -23,24 +23,21 @@ public class SnowWorkerM1 implements ISnowWorker {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* 机器码位长
|
* 机器码位长
|
||||||
* (机器码+序列数<=22位)
|
|
||||||
*/
|
*/
|
||||||
protected final byte WorkerIdBitLength;
|
protected final byte WorkerIdBitLength;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 自增序列数位长
|
* 自增序列数位长
|
||||||
* (机器码+序列数<=22位)
|
|
||||||
*/
|
*/
|
||||||
protected final byte SeqBitLength;
|
protected final byte SeqBitLength;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 最大序列数(含此值)
|
* 最大序列数(含)
|
||||||
* 超过最大值,就会从MinSeqNumber开始
|
|
||||||
*/
|
*/
|
||||||
protected final int MaxSeqNumber;
|
protected final int MaxSeqNumber;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 最小序列数(含此值)
|
* 最小序列数(含)
|
||||||
*/
|
*/
|
||||||
protected final short MinSeqNumber;
|
protected final short MinSeqNumber;
|
||||||
|
|
||||||
@@ -64,7 +61,7 @@ public class SnowWorkerM1 implements ISnowWorker {
|
|||||||
WorkerId = options.WorkerId;
|
WorkerId = options.WorkerId;
|
||||||
WorkerIdBitLength = options.WorkerIdBitLength == 0 ? 6 : options.WorkerIdBitLength;
|
WorkerIdBitLength = options.WorkerIdBitLength == 0 ? 6 : options.WorkerIdBitLength;
|
||||||
SeqBitLength = options.SeqBitLength == 0 ? 6 : options.SeqBitLength;
|
SeqBitLength = options.SeqBitLength == 0 ? 6 : options.SeqBitLength;
|
||||||
MaxSeqNumber = options.MaxSeqNumber > 0 ? options.MaxSeqNumber : (int) Math.pow(2, SeqBitLength);
|
MaxSeqNumber = options.MaxSeqNumber > 0 ? options.MaxSeqNumber : (int) Math.pow(2, SeqBitLength) - 1;
|
||||||
MinSeqNumber = options.MinSeqNumber;
|
MinSeqNumber = options.MinSeqNumber;
|
||||||
TopOverCostCount = options.TopOverCostCount;
|
TopOverCostCount = options.TopOverCostCount;
|
||||||
BaseTime = options.BaseTime != 0 ? options.BaseTime : 1582136402000L;
|
BaseTime = options.BaseTime != 0 ? options.BaseTime : 1582136402000L;
|
||||||
|
|||||||
@@ -30,7 +30,7 @@ public class DefaultIdGenerator implements IIdGenerator {
|
|||||||
}
|
}
|
||||||
|
|
||||||
double maxWorkerIdNumber = Math.pow(2, options.WorkerIdBitLength) - 1;
|
double maxWorkerIdNumber = Math.pow(2, options.WorkerIdBitLength) - 1;
|
||||||
if (options.WorkerId < 1 || options.WorkerId > maxWorkerIdNumber) {
|
if (options.WorkerId < 0 || options.WorkerId > maxWorkerIdNumber) {
|
||||||
throw new IdGeneratorException("WorkerId error. (range:[1, " + maxWorkerIdNumber + "]");
|
throw new IdGeneratorException("WorkerId error. (range:[1, " + maxWorkerIdNumber + "]");
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -43,9 +43,9 @@ public class DefaultIdGenerator implements IIdGenerator {
|
|||||||
throw new IdGeneratorException("MaxSeqNumber error. (range:[1, " + maxSeqNumber + "]");
|
throw new IdGeneratorException("MaxSeqNumber error. (range:[1, " + maxSeqNumber + "]");
|
||||||
}
|
}
|
||||||
|
|
||||||
double maxValue = maxSeqNumber - 2;
|
double maxValue = maxSeqNumber;
|
||||||
if (options.MinSeqNumber < 5 || options.MinSeqNumber > maxValue) {
|
if (options.MinSeqNumber < 1 || options.MinSeqNumber > maxValue) {
|
||||||
throw new IdGeneratorException("MinSeqNumber error. (range:[5, " + maxValue + "]");
|
throw new IdGeneratorException("MinSeqNumber error. (range:[1, " + maxValue + "]");
|
||||||
}
|
}
|
||||||
|
|
||||||
switch (options.Method) {
|
switch (options.Method) {
|
||||||
|
|||||||
Reference in New Issue
Block a user