You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

DefaultIdGenerator.cs 4.6 kB

4 years ago
4 years ago
4 years ago
4 years ago
2 years ago
4 years ago
4 years ago
4 years ago
2 years ago
4 years ago
4 years ago
4 years ago
2 years ago
4 years ago
3 years ago
4 years ago
2 years ago
4 years ago
3 years ago
4 years ago
2 years ago
4 years ago
4 years ago
3 years ago
4 years ago
4 years ago
2 years ago
4 years ago
3 years ago
2 years ago
3 years ago
4 years ago
2 years ago
4 years ago
4 years ago
4 years ago
2 years ago
4 years ago
4 years ago
2 years ago
4 years ago
2 years ago
4 years ago
3 years ago
4 years ago
3 years ago
4 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /*
  2. * 版权属于:yitter(yitter@126.com)
  3. * 开源地址:https://github.com/yitter/idgenerator
  4. * 版权协议:MIT
  5. * 版权说明:只要保留本版权,你可以免费使用、修改、分发本代码。
  6. * 免责条款:任何因为本代码产生的系统、法律、政治、宗教问题,均与版权所有者无关。
  7. *
  8. */
  9. using System;
  10. using System.Collections.Generic;
  11. using System.Text;
  12. using System.Threading;
  13. namespace Yitter.IdGenerator
  14. {
  15. /// <summary>
  16. /// 默认实现
  17. /// </summary>
  18. public class DefaultIdGenerator : IIdGenerator
  19. {
  20. private ISnowWorker _SnowWorker { get; set; }
  21. //public Action<OverCostActionArg> GenIdActionAsync
  22. //{
  23. // get => _SnowWorker.GenAction;
  24. // set => _SnowWorker.GenAction = value;
  25. //}
  26. public DefaultIdGenerator(IdGeneratorOptions options)
  27. {
  28. if (options == null)
  29. {
  30. throw new ArgumentException("options error.");
  31. }
  32. // 1.BaseTime
  33. if (options.BaseTime < DateTime.Now.AddYears(-50) || options.BaseTime > DateTime.Now)
  34. {
  35. throw new ArgumentException("BaseTime error.");
  36. }
  37. // 2.WorkerIdBitLength
  38. int maxLength = options.TimestampType == 0 ? 22 : 31; // (秒级时间戳时放大到31位)
  39. if (options.WorkerIdBitLength <= 0)
  40. {
  41. throw new ArgumentException("WorkerIdBitLength error.(range:[1, 21])");
  42. }
  43. if (options.DataCenterIdBitLength + options.WorkerIdBitLength + options.SeqBitLength > maxLength)
  44. {
  45. throw new ArgumentException("error:DataCenterIdBitLength + WorkerIdBitLength + SeqBitLength <= " + maxLength);
  46. }
  47. // 3.WorkerId & DataCenterId
  48. var maxWorkerIdNumber = (1 << options.WorkerIdBitLength) - 1;
  49. if (maxWorkerIdNumber == 0)
  50. {
  51. maxWorkerIdNumber = 63;
  52. }
  53. if (options.WorkerId < 0 || options.WorkerId > maxWorkerIdNumber)
  54. {
  55. throw new ArgumentException("WorkerId error. (range:[0, " + maxWorkerIdNumber + "]");
  56. }
  57. var maxDataCenterIdNumber = (1 << options.DataCenterIdBitLength) - 1;
  58. if (options.DataCenterId < 0 || options.DataCenterId > maxDataCenterIdNumber)
  59. {
  60. throw new ArgumentException("DataCenterId error. (range:[0, " + maxDataCenterIdNumber + "]");
  61. }
  62. // 4.SeqBitLength
  63. if (options.SeqBitLength < 2 || options.SeqBitLength > 21)
  64. {
  65. throw new ArgumentException("SeqBitLength error. (range:[2, 21])");
  66. }
  67. // 5.MaxSeqNumber
  68. var maxSeqNumber = (1 << options.SeqBitLength) - 1;
  69. if (maxSeqNumber == 0)
  70. {
  71. maxSeqNumber = 63;
  72. }
  73. if (options.MaxSeqNumber < 0 || options.MaxSeqNumber > maxSeqNumber)
  74. {
  75. throw new ArgumentException("MaxSeqNumber error. (range:[1, " + maxSeqNumber + "]");
  76. }
  77. // 6.MinSeqNumber
  78. if (options.MinSeqNumber < 5 || options.MinSeqNumber > maxSeqNumber)
  79. {
  80. throw new ArgumentException("MinSeqNumber error. (range:[5, " + maxSeqNumber + "]");
  81. }
  82. // 7.TopOverCostCount
  83. if (options.TopOverCostCount < 0 || options.TopOverCostCount > 10000)
  84. {
  85. throw new ArgumentException("TopOverCostCount error. (range:[0, 10000]");
  86. }
  87. switch (options.Method)
  88. {
  89. case 2:
  90. _SnowWorker = new SnowWorkerM2(options);
  91. break;
  92. default:
  93. if (options.DataCenterIdBitLength == 0 && options.TimestampType == 0)
  94. {
  95. _SnowWorker = new SnowWorkerM1(options);
  96. }
  97. else
  98. {
  99. _SnowWorker = new SnowWorkerM3(options);
  100. }
  101. break;
  102. }
  103. if (options.Method != 2)
  104. {
  105. Thread.Sleep(500);
  106. }
  107. }
  108. public long NewLong()
  109. {
  110. return _SnowWorker.NextId();
  111. }
  112. }
  113. }