| @@ -5,27 +5,27 @@ | |||||
| * 版权说明:只要保留本版权,你可以免费使用、修改、分发本代码。 | * 版权说明:只要保留本版权,你可以免费使用、修改、分发本代码。 | ||||
| * 免责条款:任何因为本代码产生的系统、法律、政治、宗教问题,均与版权所有者无关。 | * 免责条款:任何因为本代码产生的系统、法律、政治、宗教问题,均与版权所有者无关。 | ||||
| * | * | ||||
| */ | |||||
| using System; | |||||
| using System.Threading; | |||||
| */ | |||||
| using System; | |||||
| using System.Threading; | |||||
| using System.Threading.Tasks; | using System.Threading.Tasks; | ||||
| namespace Yitter.IdGenerator | |||||
| { | |||||
| /// <summary> | |||||
| /// 雪花漂移算法 | |||||
| /// </summary> | |||||
| internal class SnowWorkerM1 : ISnowWorker | |||||
| namespace Yitter.IdGenerator | |||||
| { | |||||
| /// <summary> | |||||
| /// 雪花漂移算法 | |||||
| /// </summary> | |||||
| internal class SnowWorkerM1 : ISnowWorker | |||||
| { | { | ||||
| /// <summary> | /// <summary> | ||||
| /// 基础时间 | /// 基础时间 | ||||
| /// </summary> | /// </summary> | ||||
| protected readonly DateTime BaseTime; | |||||
| protected readonly DateTime BaseTime; | |||||
| /// <summary> | |||||
| /// 机器码 | |||||
| /// </summary> | |||||
| /// <summary> | |||||
| /// 机器码 | |||||
| /// </summary> | |||||
| protected readonly ushort WorkerId = 0; | protected readonly ushort WorkerId = 0; | ||||
| /// <summary> | /// <summary> | ||||
| @@ -40,7 +40,7 @@ namespace Yitter.IdGenerator | |||||
| /// <summary> | /// <summary> | ||||
| /// 最大序列数(含) | /// 最大序列数(含) | ||||
| /// </summary> | |||||
| /// </summary> | |||||
| protected readonly int MaxSeqNumber = 0; | protected readonly int MaxSeqNumber = 0; | ||||
| /// <summary> | /// <summary> | ||||
| @@ -57,11 +57,11 @@ namespace Yitter.IdGenerator | |||||
| protected static object _SyncLock = new object(); | protected static object _SyncLock = new object(); | ||||
| protected ushort _CurrentSeqNumber; | protected ushort _CurrentSeqNumber; | ||||
| protected long _LastTimeTick = 0; // -1L | |||||
| protected long _TurnBackTimeTick = 0; // -1L; | |||||
| protected byte _TurnBackIndex = 0; | |||||
| protected bool _IsOverCost = false; | |||||
| protected long _LastTimeTick = 0; // -1L | |||||
| protected long _TurnBackTimeTick = 0; // -1L; | |||||
| protected byte _TurnBackIndex = 0; | |||||
| protected bool _IsOverCost = false; | |||||
| protected int _OverCostCountInOneTerm = 0; | protected int _OverCostCountInOneTerm = 0; | ||||
| protected int _GenCountInOneTerm = 0; | protected int _GenCountInOneTerm = 0; | ||||
| protected int _TermIndex = 0; | protected int _TermIndex = 0; | ||||
| @@ -71,8 +71,8 @@ namespace Yitter.IdGenerator | |||||
| public Action<OverCostActionArg> GenAction { get; set; } | public Action<OverCostActionArg> GenAction { get; set; } | ||||
| public SnowWorkerM1(IdGeneratorOptions options) | |||||
| public SnowWorkerM1(IdGeneratorOptions options) | |||||
| { | { | ||||
| // 1.BaseTime | // 1.BaseTime | ||||
| if (options.BaseTime != DateTime.MinValue) | if (options.BaseTime != DateTime.MinValue) | ||||
| @@ -117,17 +117,17 @@ namespace Yitter.IdGenerator | |||||
| MinSeqNumber = options.MinSeqNumber; | MinSeqNumber = options.MinSeqNumber; | ||||
| // 7.Others | // 7.Others | ||||
| TopOverCostCount = options.TopOverCostCount; | |||||
| TopOverCostCount = options.TopOverCostCount; | |||||
| if (TopOverCostCount == 0) | if (TopOverCostCount == 0) | ||||
| { | { | ||||
| TopOverCostCount = 2000; | TopOverCostCount = 2000; | ||||
| } | } | ||||
| _TimestampShift = (byte)(WorkerIdBitLength + SeqBitLength); | |||||
| _CurrentSeqNumber = options.MinSeqNumber; | |||||
| //_BaseTimeTick = BaseTime.Ticks; | |||||
| //_StartTimeTick = (long)(DateTime.UtcNow.Subtract(BaseTime).TotalMilliseconds) - Environment.TickCount; | |||||
| _TimestampShift = (byte)(WorkerIdBitLength + SeqBitLength); | |||||
| _CurrentSeqNumber = options.MinSeqNumber; | |||||
| //_BaseTimeTick = BaseTime.Ticks; | |||||
| //_StartTimeTick = (long)(DateTime.UtcNow.Subtract(BaseTime).TotalMilliseconds) - Environment.TickCount; | |||||
| } | } | ||||
| @@ -137,7 +137,7 @@ namespace Yitter.IdGenerator | |||||
| { | { | ||||
| GenAction(arg); | GenAction(arg); | ||||
| }); | }); | ||||
| } | |||||
| } | |||||
| private void BeginOverCostAction(in long useTimeTick) | private void BeginOverCostAction(in long useTimeTick) | ||||
| { | { | ||||
| @@ -338,11 +338,11 @@ namespace Yitter.IdGenerator | |||||
| return result; | return result; | ||||
| } | } | ||||
| protected virtual long GetCurrentTimeTick() | |||||
| { | |||||
| //return (long)(DateTime.UtcNow - BaseTime).Ticks; | |||||
| //return (long)(_StartTimeTick + Environment.TickCount); | |||||
| return (long)(DateTime.UtcNow - BaseTime).TotalMilliseconds; | |||||
| protected virtual long GetCurrentTimeTick() | |||||
| { | |||||
| //return (long)(DateTime.UtcNow - BaseTime).Ticks; | |||||
| //return (long)(_StartTimeTick + Environment.TickCount); | |||||
| return (long)(DateTime.UtcNow - BaseTime).TotalMilliseconds; | |||||
| } | } | ||||
| protected virtual long GetNextTimeTick() | protected virtual long GetNextTimeTick() | ||||
| @@ -364,6 +364,6 @@ namespace Yitter.IdGenerator | |||||
| { | { | ||||
| return _IsOverCost ? NextOverCostId() : NextNormalId(); | return _IsOverCost ? NextOverCostId() : NextNormalId(); | ||||
| } | } | ||||
| } | |||||
| } | |||||
| } | |||||
| } | |||||
| } | |||||
| } | |||||