From 761020f3ebe8b666a9f317f88baecf05999fcb9c Mon Sep 17 00:00:00 2001 From: zhouzj Date: Fri, 12 Mar 2021 22:21:43 +0800 Subject: [PATCH] code_editreadme --- README.md | 10 ++++----- ...itter.IdGenerator.sln => Yitter.IdGen.sln} | 0 src/Yitter.IdGenTest/Program.cs | 10 ++++----- .../{ => Contract}/IIdGenerator.cs | 7 ++++++ .../{ => Contract}/ISnowWorker.cs | 0 .../{ => Contract}/IdGeneratorOptions.cs | 0 .../{ => Contract}/OverCostActionArg.cs | 22 +++++++++++++++++++ .../{ => Core}/SnowWorkerM1.cs | 0 .../{ => Core}/SnowWorkerM2.cs | 0 ...{YidGenerator.cs => DefaultIdGenerator.cs} | 15 ++++++++----- .../{YidHelper.cs => IdHelper.cs} | 16 +++++--------- .../Yitter.IdGenerator.csproj | 3 --- 12 files changed, 54 insertions(+), 29 deletions(-) rename src/{Yitter.IdGenerator.sln => Yitter.IdGen.sln} (100%) rename src/Yitter.IdGenerator/{ => Contract}/IIdGenerator.cs (72%) rename src/Yitter.IdGenerator/{ => Contract}/ISnowWorker.cs (100%) rename src/Yitter.IdGenerator/{ => Contract}/IdGeneratorOptions.cs (100%) rename src/Yitter.IdGenerator/{ => Contract}/OverCostActionArg.cs (66%) rename src/Yitter.IdGenerator/{ => Core}/SnowWorkerM1.cs (100%) rename src/Yitter.IdGenerator/{ => Core}/SnowWorkerM2.cs (100%) rename src/Yitter.IdGenerator/{YidGenerator.cs => DefaultIdGenerator.cs} (78%) rename src/Yitter.IdGenerator/{YidHelper.cs => IdHelper.cs} (77%) diff --git a/README.md b/README.md index 65d3e5e..349f7dd 100644 --- a/README.md +++ b/README.md @@ -58,7 +58,7 @@ 1.js Number 类型最大数值:9007199254740992,本算法在保持并发性能(5W+/0.01s)和最大64个 WorkerId(6bit)的同时,能用70年才到 js Number Max 值。 -2.增加WorkerId位数到8bit(128节点)时,15年达到 js Number Max 值。 +2.增加WorkerId位数到8bit(256节点)时,15年达到 js Number Max 值。 3.极致性能:500W/1s。 @@ -167,14 +167,14 @@ ``` // 全局初始化设置WorkerId,默认最大2^16-1。(初始化过程全局只需一次,且必须最先设置) var options = new IdGeneratorOptions(){ WorkerId = 1}; -YidHelper.SetIdGenerator(options); +IdHelper.SetIdGenerator(options); // 初始化以后,就可以在需要的地方调用方法生成ID。 -var newId = YidHelper.NextId(); +var newId = IdHelper.NextId(); -// 可通过 YidHelper.IdGenInstance 订阅 GenIdActionAsync 事件。 +// 可通过 IdHelper.IdGenInstance 订阅 GenIdActionAsync 事件。 ``` -如果基于DI框架集成,可以参考 YidHelper 去管理 IdGenerator 对象,必须使用**单例**模式。 +如果基于DI框架集成,可以参考 IdHelper 去管理 IdGenerator 对象,必须使用**单例**模式。 #### options说明 ``` diff --git a/src/Yitter.IdGenerator.sln b/src/Yitter.IdGen.sln similarity index 100% rename from src/Yitter.IdGenerator.sln rename to src/Yitter.IdGen.sln diff --git a/src/Yitter.IdGenTest/Program.cs b/src/Yitter.IdGenTest/Program.cs index 52c0387..ef0af92 100644 --- a/src/Yitter.IdGenTest/Program.cs +++ b/src/Yitter.IdGenTest/Program.cs @@ -44,13 +44,13 @@ namespace Yitter.OrgSystem.TestA WorkerId = 1, TopOverCostCount = 2000, - //WorkerIdBitLength = 6, - //SeqBitLength = 6, + WorkerIdBitLength = 10, + SeqBitLength = 6, //MinSeqNumber = 11, //MaxSeqNumber = 200, - //StartTime = DateTime.Now.AddYears(-1), + StartTime = DateTime.Now.AddYears(-5), }; // ++++++++++++++++++++++++++++++++ @@ -59,7 +59,7 @@ namespace Yitter.OrgSystem.TestA IdGeneratorOptions options1 = (newConfig); if (IdGen == null) { - IdGen = new YidGenerator(options1); + IdGen = new DefaultIdGenerator(options1); } if (outputLog) @@ -105,7 +105,7 @@ namespace Yitter.OrgSystem.TestA }; Console.WriteLine("Gen:" + i); - var idGen2 = new YidGenerator(options); + var idGen2 = new DefaultIdGenerator(options); var test = new GenTest(idGen2, genIdCount, i); if (outputLog) diff --git a/src/Yitter.IdGenerator/IIdGenerator.cs b/src/Yitter.IdGenerator/Contract/IIdGenerator.cs similarity index 72% rename from src/Yitter.IdGenerator/IIdGenerator.cs rename to src/Yitter.IdGenerator/Contract/IIdGenerator.cs index d96e225..22bd86e 100644 --- a/src/Yitter.IdGenerator/IIdGenerator.cs +++ b/src/Yitter.IdGenerator/Contract/IIdGenerator.cs @@ -13,8 +13,15 @@ namespace Yitter.IdGenerator { public interface IIdGenerator { + /// + /// 生成过程中产生的事件 + /// Action GenIdActionAsync { get; set; } + /// + /// 生成新的long型Id + /// + /// long NewLong(); // Guid NewGuid(); diff --git a/src/Yitter.IdGenerator/ISnowWorker.cs b/src/Yitter.IdGenerator/Contract/ISnowWorker.cs similarity index 100% rename from src/Yitter.IdGenerator/ISnowWorker.cs rename to src/Yitter.IdGenerator/Contract/ISnowWorker.cs diff --git a/src/Yitter.IdGenerator/IdGeneratorOptions.cs b/src/Yitter.IdGenerator/Contract/IdGeneratorOptions.cs similarity index 100% rename from src/Yitter.IdGenerator/IdGeneratorOptions.cs rename to src/Yitter.IdGenerator/Contract/IdGeneratorOptions.cs diff --git a/src/Yitter.IdGenerator/OverCostActionArg.cs b/src/Yitter.IdGenerator/Contract/OverCostActionArg.cs similarity index 66% rename from src/Yitter.IdGenerator/OverCostActionArg.cs rename to src/Yitter.IdGenerator/Contract/OverCostActionArg.cs index 2bd0745..84d3e84 100644 --- a/src/Yitter.IdGenerator/OverCostActionArg.cs +++ b/src/Yitter.IdGenerator/Contract/OverCostActionArg.cs @@ -13,13 +13,35 @@ using System.Text; namespace Yitter.IdGenerator { + /// + /// Id生成时回调参数 + /// public class OverCostActionArg { + /// + /// 事件类型 + /// 1-开始,2-结束,8-漂移 + /// public int ActionType { get; set; } + /// + /// 时间戳 + /// public long TimeTick { get; set; } + /// + /// 机器码 + /// public ushort WorkerId { get; set; } + /// + /// 漂移计算次数 + /// public int OverCostCountInOneTerm { get; set; } + /// + /// 漂移期间生产ID个数 + /// public int GenCountInOneTerm { get; set; } + /// + /// 漂移周期 + /// public int TermIndex { get; set; } public OverCostActionArg(ushort workerId, long timeTick, int actionType = 0, int overCostCountInOneTerm = 0, int genCountWhenOverCost = 0,int index=0) diff --git a/src/Yitter.IdGenerator/SnowWorkerM1.cs b/src/Yitter.IdGenerator/Core/SnowWorkerM1.cs similarity index 100% rename from src/Yitter.IdGenerator/SnowWorkerM1.cs rename to src/Yitter.IdGenerator/Core/SnowWorkerM1.cs diff --git a/src/Yitter.IdGenerator/SnowWorkerM2.cs b/src/Yitter.IdGenerator/Core/SnowWorkerM2.cs similarity index 100% rename from src/Yitter.IdGenerator/SnowWorkerM2.cs rename to src/Yitter.IdGenerator/Core/SnowWorkerM2.cs diff --git a/src/Yitter.IdGenerator/YidGenerator.cs b/src/Yitter.IdGenerator/DefaultIdGenerator.cs similarity index 78% rename from src/Yitter.IdGenerator/YidGenerator.cs rename to src/Yitter.IdGenerator/DefaultIdGenerator.cs index c7be142..deb6ed7 100644 --- a/src/Yitter.IdGenerator/YidGenerator.cs +++ b/src/Yitter.IdGenerator/DefaultIdGenerator.cs @@ -14,7 +14,10 @@ using System.Threading; namespace Yitter.IdGenerator { - public class YidGenerator : IIdGenerator + /// + /// 默认实现 + /// + public class DefaultIdGenerator : IIdGenerator { private ISnowWorker _SnowWorker { get; set; } @@ -25,7 +28,7 @@ namespace Yitter.IdGenerator } - public YidGenerator(IdGeneratorOptions options) + public DefaultIdGenerator(IdGeneratorOptions options) { if (options == null) { @@ -45,24 +48,24 @@ namespace Yitter.IdGenerator var maxWorkerIdNumber = Math.Pow(2, options.WorkerIdBitLength) - 1; if (options.WorkerId < 1 || options.WorkerId > maxWorkerIdNumber) { - throw new ApplicationException("WorkerId is error. (range:[1, " + maxWorkerIdNumber + "]"); + throw new ApplicationException("WorkerId error. (range:[1, " + maxWorkerIdNumber + "]"); } if (options.SeqBitLength < 2 || options.SeqBitLength > 21) { - throw new ApplicationException("SeqBitLength is error. (range:[2, 21])"); + throw new ApplicationException("SeqBitLength error. (range:[2, 21])"); } var maxSeqNumber = Math.Pow(2, options.SeqBitLength) - 1; if (options.MaxSeqNumber < 0 || options.MaxSeqNumber > maxSeqNumber) { - throw new ApplicationException("MaxSeqNumber is error. (range:[1, " + maxSeqNumber + "]"); + throw new ApplicationException("MaxSeqNumber error. (range:[1, " + maxSeqNumber + "]"); } var maxValue = maxSeqNumber - 2; if (options.MinSeqNumber < 5 || options.MinSeqNumber > maxValue) { - throw new ApplicationException("MinSeqNumber is error. (range:[5, " + maxValue + "]"); + throw new ApplicationException("MinSeqNumber error. (range:[5, " + maxValue + "]"); } switch (options.Method) diff --git a/src/Yitter.IdGenerator/YidHelper.cs b/src/Yitter.IdGenerator/IdHelper.cs similarity index 77% rename from src/Yitter.IdGenerator/YidHelper.cs rename to src/Yitter.IdGenerator/IdHelper.cs index 83960de..2354882 100644 --- a/src/Yitter.IdGenerator/YidHelper.cs +++ b/src/Yitter.IdGenerator/IdHelper.cs @@ -16,17 +16,11 @@ namespace Yitter.IdGenerator /// /// 这是一个调用的例子,默认情况下,单机集成者可以直接使用 NewId()。 /// - public class YidHelper + public class IdHelper { private static IIdGenerator _IdGenInstance = null; - public static IIdGenerator IdGenInstance - { - get - { - return _IdGenInstance; - } - } + public static IIdGenerator IdGenInstance => _IdGenInstance; /// /// 设置参数,建议程序初始化时执行一次 @@ -34,7 +28,7 @@ namespace Yitter.IdGenerator /// public static void SetIdGenerator(IdGeneratorOptions options) { - _IdGenInstance = new YidGenerator(options); + _IdGenInstance = new DefaultIdGenerator(options); } /// @@ -47,7 +41,9 @@ namespace Yitter.IdGenerator { if (_IdGenInstance == null) { - _IdGenInstance = new YidGenerator(new IdGeneratorOptions() { WorkerId = 1 }); + _IdGenInstance = new DefaultIdGenerator( + new IdGeneratorOptions() { WorkerId = 1 } + ); } return _IdGenInstance.NewLong(); diff --git a/src/Yitter.IdGenerator/Yitter.IdGenerator.csproj b/src/Yitter.IdGenerator/Yitter.IdGenerator.csproj index ef2d4c2..4cafd7e 100644 --- a/src/Yitter.IdGenerator/Yitter.IdGenerator.csproj +++ b/src/Yitter.IdGenerator/Yitter.IdGenerator.csproj @@ -12,7 +12,4 @@ 5 - - -